Flowmonitor problem

742 views
Skip to first unread message

rauf

unread,
Nov 23, 2010, 1:08:29 AM11/23/10
to ns-3-users
Hello,

Could you help me with below codes. I am facing problem with flow-
monitor. I want to print packet flow from each node to other (or
source to sink) after every 20 s. That's why flow-monitor has been
inserted into AdvancePosition function in problematic code. It's not
working! But if I use it in the main function after simulation::run(),
it works fine though it's not providing 20s update packet flow. I have
attached both code here. It's urgent :(. I don't know the relationship
of flow-monitor and Simulation::Run().

Problematic code:




#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/flow-monitor-module.h"


using namespace ns3;
using namespace std;

NS_LOG_COMPONENT_DEFINE ("n2");


void getdistance (NodeContainer wifiNodes)
{
double dst;
for ( int i = 0; i<3; i++)
{
cout <<"\n";
for (int j =0; j<3; j++)
{
Ptr<Node> a = wifiNodes.Get (i);
Ptr<Node> b = wifiNodes.Get (j);
dst = a->GetObject<MobilityModel> ()->GetDistanceFrom (b-
>GetObject<MobilityModel> ());
cout <<dst<<" ";
}
}

}


static Vector
GetVelocity (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetVelocity();
}


static Vector
GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition();
}

static void
AdvancePosition (NodeContainer wifiNodes)
{

for (int j=0; j<3; j++)
{

if (j==0) cout <<"\n";
Ptr<Node> node = wifiNodes.Get (j);
std::cout << "Node:" <<j<<endl;
std::cout << "Position " << GetPosition (node) << std::endl;
std::cout << "Velocity " << GetVelocity (node) << std::endl;

}
cout <<"Nodes Connectivity:\n";
getdistance(wifiNodes);
cout <<"\n";
cout <<"hi";


//////////Problematic/////////////////////////////////


FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.Install(wifiNodes);

monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";

}


////////////////////////////////////////////////////




}


int
main (int argc, char *argv[])
{

uint32_t nwifi = 3;

Config::SetDefault ("ns3::OnOffApplication::PacketSize",
UintegerValue (1000));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue
("10kb/s"));

CommandLine cmd;
cmd.AddValue ("nwifi", "Number of wifi STA devices", nwifi);


cmd.Parse (argc,argv);


NodeContainer wifiNodes;
wifiNodes.Create (nwifi);


YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();


YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();

phy.SetChannel (channel.Create ());

WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();




mac.SetType ("ns3::AdhocWifiMac");







NetDeviceContainer wifiDevices;
wifiDevices = wifi.Install (phy, mac, wifiNodes);



MobilityHelper mobility;

mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (10.0),
"DeltaY", DoubleValue (20.0),
"GridWidth", UintegerValue (10),
"LayoutType", StringValue ("RowFirst"));

mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-500, 500, -500, 500)));
mobility.Install (wifiNodes);



InternetStackHelper stack;
//stack.Install (wifiApNode);
stack.Install (wifiNodes);

Ipv4AddressHelper address;

address.SetBase ("10.1.3.0", "255.255.255.0");

Ipv4InterfaceContainer wifiNodesInterfaces;
//Ipv4InterfaceContainer apNodeInterface;

wifiNodesInterfaces = address.Assign (wifiDevices);
//apNodeInterface = address.Assign (apDevices);




OnOffHelper onoff ("ns3::TcpSocketFactory",InetSocketAddress
("255.255.255.255", 1025));

onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(10)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));

ApplicationContainer apps;

for (int i=0; i<nwifi; i++)
{
apps = onoff.Install (wifiNodes.Get (i));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
}



PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress
("10.1.3.1", 1025));
apps = sink.Install (wifiNodes.Get (0)); //node1



FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();



Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

Simulator::Stop (Seconds (1001.0));

// phy.EnablePcap ("wifi", apDevices.Get (0));


for (int k=0; k<=1000; k=k+20)
Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes);


Simulator::Run ();

/*
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";

}

*/////////////////////////////////////////////////////////////

Simulator::Destroy ();
return 0;
}







Running code:
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/flow-monitor-module.h"


using namespace ns3;
using namespace std;

NS_LOG_COMPONENT_DEFINE ("Assignment2");


void getdistance (NodeContainer wifiNodes)
{
double dst;
for ( int i = 0; i<3; i++)
{
cout <<"\n";
for (int j =0; j<3; j++)
{
Ptr<Node> a = wifiNodes.Get (i);
Ptr<Node> b = wifiNodes.Get (j);
dst = a->GetObject<MobilityModel> ()->GetDistanceFrom (b-
>GetObject<MobilityModel> ());
cout <<dst<<" ";
}
}

}


static Vector
GetVelocity (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetVelocity();
}


static Vector
GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition();
}

static void
AdvancePosition (NodeContainer wifiNodes)
{

for (int j=0; j<3; j++)
{

if (j==0) cout <<"\n";
Ptr<Node> node = wifiNodes.Get (j);
std::cout << "Node:" <<j<<endl;
std::cout << "Position " << GetPosition (node) << std::endl;
std::cout << "Velocity " << GetVelocity (node) << std::endl;

}
cout <<"Nodes Connectivity:\n";
getdistance(wifiNodes);
cout <<"\n";

}


int
main (int argc, char *argv[])
{

uint32_t nwifi = 3;

Config::SetDefault ("ns3::OnOffApplication::PacketSize",
UintegerValue (1000));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue
("10kb/s"));

CommandLine cmd;
cmd.AddValue ("nwifi", "Number of wifi STA devices", nwifi);


cmd.Parse (argc,argv);



// NodeContainer wifiApNode;
// wifiApNode.Create (1);

NodeContainer wifiNodes;
wifiNodes.Create (nwifi);


YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();


YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();

phy.SetChannel (channel.Create ());

WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();




mac.SetType ("ns3::AdhocWifiMac");







NetDeviceContainer wifiDevices;
wifiDevices = wifi.Install (phy, mac, wifiNodes);

// mac.SetType ("ns3::NqapWifiMac",
// "Ssid", SsidValue (ssid));

// NetDeviceContainer apDevices;
// apDevices = wifi.Install (phy, mac, wifiApNode);


MobilityHelper mobility;

mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (10.0),
"GridWidth", UintegerValue (5),
"LayoutType", StringValue ("RowFirst"));

mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-500, 500, -500, 500)));
mobility.Install (wifiNodes);

//mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
//mobility.Install (wifiApNode);


InternetStackHelper stack;
//stack.Install (wifiApNode);
stack.Install (wifiNodes);

Ipv4AddressHelper address;

address.SetBase ("10.1.3.0", "255.255.255.0");

Ipv4InterfaceContainer wifiNodesInterfaces;
//Ipv4InterfaceContainer apNodeInterface;

wifiNodesInterfaces = address.Assign (wifiDevices);
//apNodeInterface = address.Assign (apDevices);

OnOffHelper onoff ("ns3::TcpSocketFactory",InetSocketAddress
("10.1.3.1", 1025));

onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(1)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));

ApplicationContainer apps;

for (int i=0; i<nwifi; i++)
{
apps = onoff.Install (wifiNodes.Get (i));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
}



PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress
("10.1.3.11", 1025));
apps = sink.Install (wifiNodes.Get (0)); //node1


FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

Simulator::Stop (Seconds (1001.0));

// phy.EnablePcap ("wifi", apDevices.Get (0));


for (int k=0; k<=1000; k=k+20)
Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes);


Simulator::Run ();

///Flowmonitor after simulator::run()

monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";

}


Simulator::Destroy ();
return 0;
}

Gustavo Carneiro

unread,
Nov 23, 2010, 5:45:25 AM11/23/10
to ns-3-...@googlegroups.com
I see a problem in your "problematic code".  Flow Monitor was not designed to be Install()ed multiple times on nodes.  You should call FlowMonitorHelper::Install() only once per simulation.  In the AdvancePosition function, pass an additional parameter

static void
AdvancePosition (NodeContainer wifiNodes, Ptr<FlowMonitor> monitor)
{
[...]
  // use the flow monitor here
}

Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes, monitor);

There's a catch: between successive calls to AdvancePosition, the flow monitor statistics will accumulate, not reset to zero.  So, if this is not what you want, you have to substract the current stats from the previous stats to get the stats for that interval alone.

Actually providing a better API for this sort of periodic snapshot was on my future work for Flow Monitor, but I haven't found time to do it yet :-/ 



--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.




--
Gustavo J. A. M. Carneiro
INESC Porto, UTM, WiN, http://win.inescporto.pt/gjc
"The universe is always one step beyond logic." -- Frank Herbert

rauf

unread,
Nov 23, 2010, 10:51:11 AM11/23/10
to ns-3-users
Thanks Gustavo for reply..Sorry I made a mistake ...I forgot to
comment out below flow-monitor portion of code declared in main()
while uploading the code..so, problametic code was actually running
with it like below... it is not muliple declared..

/*
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
*/

You see, in AdvancePosition function I have used..

FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.Install(wifiNodes); //It is a
part of NodeContainer wifiNodes ...Is it OK?

But it's not working...and not showing any error either...I am
confused whether below for loops executed ...


for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i
=stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);

cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";

}









On Nov 23, 5:45 am, Gustavo Carneiro <gjcarne...@gmail.com> wrote:
> I see a problem in your "problematic code".  Flow Monitor was not designed
> to be Install()ed multiple times on nodes.  You should call
> FlowMonitorHelper::Install() only once per simulation.  In the
> AdvancePosition function, pass an additional parameter
>
> static void
> AdvancePosition (NodeContainer wifiNodes, Ptr<FlowMonitor> monitor)
> {
> [...]
>   // use the flow monitor here
>
> }
>
> Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes, monitor);
>
> There's a catch: between successive calls to AdvancePosition, the flow
> monitor statistics will accumulate, not reset to zero.  So, if this is not
> what you want, you have to substract the current stats from the previous
> stats to get the stats for that interval alone.
>
> Actually providing a better API for this sort of periodic snapshot was on my
> future work for Flow Monitor, but I haven't found time to do it yet :-/
>
> ...
>
> read more »

Gustavo Carneiro

unread,
Nov 23, 2010, 12:52:17 PM11/23/10
to ns-3-...@googlegroups.com
On Tue, Nov 23, 2010 at 15:51, rauf <rauf...@gmail.com> wrote:
Thanks Gustavo for reply..Sorry I made a mistake ...I forgot to
comment out below flow-monitor portion of code declared in main()
while uploading the code..so, problametic code was actually running
with it like below... it is not muliple declared..

It is multiple declared, but apparently it is ok to declare multiple flow monitor instances, along with probes and classifiers.  I wrote the code but didn't remember the details.

But this is not going to work.  Creating one flow monitor and probes per xx seconds interval is not efficient: the simulation will begin to run progressively slower.

Otherwise I do not know exactly why it fails.  Just don't create a FlowMonitor inside a function that is called multiple times during the simulation.  This is just wrong.  Instead, create a single one and pass a pointer to it to the callback, as I explained before.
 

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.

Message has been deleted

Hadi Samani

unread,
Nov 17, 2016, 7:15:38 AM11/17/16
to ns-3-users
Hello

That is an old post but I have the same problem.
I can not use Flowmonitor for  a specific node

With this code There is no problem. :
  
FlowMonitorHelper fmHelper;        //Flow Monitor Declaration
Ptr<FlowMonitor> fMonitor;
fMonitor = fmHelper.InstallAll();

but when I change it to this to get thethroughput for that node, it does not work.

FlowMonitorHelper fmHelper;        //Flow Monitor Declaration
Ptr<FlowMonitor> fMonitor;
fMonitor = fmHelper.Install(
wifiStaNodeNet1.Get (1));

I attached my script.
i would be grateful if somebody can help me.

Best Regards,
Hadi
wifi-spectrum11.cc

Konstantinos

unread,
Nov 17, 2016, 10:12:13 AM11/17/16
to ns-3-users
Hi,

Please do not post into un-related topics. The fact that another user had an issue with FlowMonitor, 6yrs ago, does not mean you have the same problem!

You are not supposed to install FlowMonitor only on a single node. You need to install it at least on two; the two end points of your communication path.
If you are only interested to analyse the performance of a single node, you simple filter the collected statistics.

Regards
K.

Hadi Samani

unread,
Nov 18, 2016, 2:38:58 AM11/18/16
to ns-3-users
HI, 

OK Sorry :)

Thank you very much for your answer. I Installed it on my 2 Nodes in a link and it works now,

Best Regards
Hadi
Reply all
Reply to author
Forward
0 new messages