ns-3.8/examples/flowmon

1,111 views
Skip to first unread message

Paulo Serralheiro

unread,
Jun 8, 2010, 6:30:27 AM6/8/10
to ns-3-users
hi all users,

I am working on ns-3 which plays an important role in my thesis. I
want to plot the graphs of throughput and delay.
I found an example that explain how i can plot the delay in ns-3.8/
examples/flowmon but it's written in python and i tried translate to C+
+.

I have a problem on "classifier =
flowmon_helper.GetClassifier()" (line 140), i consider the variable
"classifier" of type ns3::FlowClassifier, however it's not possible
use "t = classifier.FindFlow(flow_id)" (line 144).
The "flowmon_helper.GetClassifier()" should return a variable of type
"ns3::Ipv4FlowClassifier" to use "t = classifier.FindFlow(flow_id)".

Anyone can i help me?

Regards
Paulo Serralheiro

Gustavo Carneiro

unread,
Jun 8, 2010, 6:38:04 AM6/8/10
to ns-3-...@googlegroups.com
You should use smart pointers for most classes that are not "helper classes".

Instead of:

   FlowClassifier classifier = flowmon_helper.GetClassifier();

Write: 

   Ptr<FlowClassifier> classifier = flowmon_helper.GetClassifier();

Then use -> instead of . as you would with a normal pointer.

I hope this helps.

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

Julien Mineraud

unread,
Jun 8, 2010, 9:27:25 AM6/8/10
to ns-3-users
Hi Paulo,

Have a look in the examples/routing/simple-global-routing.cc.
In the main function, it uses the FlowMonitor in C++

Regards

Julien


On 8 juin, 11:30, Paulo Serralheiro <serralheiro.pa...@gmail.com>
wrote:

Paulo Serralheiro

unread,
Jun 8, 2010, 10:05:09 AM6/8/10
to ns-3-users
you are right, but i've already had it in mind.
i show my program below:

// Delay Gnuplot

#include <fstream>
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/common-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/mobility-module.h"
#include "ns3/contrib-module.h"
#include "ns3/wifi-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/olsr-routing-protocol.h"
#include "ns3/ipv4-routing-protocol.h"
#include "ns3/flow-monitor.h"
#include "ns3/ipv4-address.h"
#include "ns3/flow-classifier.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("test");

double DISTANCE = 150; //m
uint32_t NUM_NODES_SIDE = 3;

void
print_stats (FlowMonitor::FlowStats st)
{
std::cout << " Tx Bytes: " << st.txBytes << std::endl;
std::cout << " Rx Bytes: " << st.rxBytes << std::endl;
std::cout << " Tx Packets: " << st.txPackets << std::endl;
std::cout << " Rx Packets: " << st.rxPackets << std::endl;
std::cout << " Lost Packets: " << st.lostPackets << std::endl;

if (st.rxPackets > 0)
{
std::cout << " Mean{Delay}: " << (st.delaySum.GetSeconds() /
st.rxPackets);
std::cout << " Mean{Jitter}: " << (st.jitterSum.GetSeconds() /
(st.rxPackets-1));
std::cout << " Mean{Hop Count}: " << st.timesForwarded /
st.rxPackets + 1;
}

if (false)
{
std::cout << "Delay Histogram" << std::endl;
for (uint32_t i=0; i<st.delayHistogram.GetNBins (); i++)
std::cout << " " << i << "(" <<
st.delayHistogram.GetBinStart (i) << "-"
<< st.delayHistogram.GetBinEnd (i) << "): " <<
st.delayHistogram.GetBinCount (i) << std::endl;

std::cout << "Jitter Histogram" << std::endl;
for (uint32_t i=0; i<st.jitterHistogram.GetNBins (); i++ )
std::cout << " " << i << "(" <<
st.jitterHistogram.GetBinStart (i) << "-"
<< st.jitterHistogram.GetBinEnd (i) << "): " <<
st.jitterHistogram.GetBinCount (i) << std::endl;

std::cout << "PacketSize Histogram "<< std::endl;
for (uint32_t i=0; i<st.packetSizeHistogram.GetNBins (); i+
+ )
std::cout << " " << i << "(" << st.packetSizeHistogram.GetBinStart
(i) << "-"
<< st.packetSizeHistogram.GetBinEnd (i) << "): " <<
st.packetSizeHistogram.GetBinCount (i) << std::endl;
}

for (uint32_t i=0; i<st.packetsDropped.size (); i++)
std::cout << " Packets dropped by reason " << i << ": " <<
st.packetsDropped [i] << std::endl;
// for (uint32_t i=0; i<st.bytesDropped.size(); i++)
// std::cout << "Bytes dropped by reason " << i << ": " <<
st.bytesDropped[i] << std::endl;
}

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

uint32_t NumNodesSide = 0;
string Results("");
bool Plot = false;

CommandLine cmd;
cmd.AddValue ("NumNodesSide", "Grid side number of nodes (total
number of nodes will be this number squared)", NumNodesSide);
cmd.AddValue ("Results", "Write XML results to file", Results);
cmd.AddValue ("Plot", "Plot the results using the matplotlib python
module", Plot);
cmd.Parse (argc,argv);

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default
(); // Create a channel helper in a default working state. By
default, we create a channel model with a propagation delay equal to a
constant, the speed of light, and a propagation loss based on a log
distance model with a reference loss of 46.6777 dB at reference
distance of 1m.
// wifiChannel.SetPropagationDelay
("ns3::ConstantSpeedPropagationDelayModel");
// wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); //
Create a phy helper in a default working state.
wifiPhy.SetChannel (wifiChannel.Create ());

NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); //
Create a mac helper in a default working state. i.e., this is an adhoc
mac by default.
Ssid ssid = Ssid("wifi-default");
wifiMac.SetType ("ns3::AdhocWifiMac", "Ssid",
SsidValue(ssid)); // the NqosWifiMacHelper is going to
create MAC layers of the “ns3::NqapWifiMac” (Non-Qos Access Point)
type. We set the “BeaconGeneration” Attribute to true and also set an
interval between beacons of 2.5 seconds

WifiHelper wifi = WifiHelper::Default (); // Returns: a new
WifiHelper in a default state
wifi.SetRemoteStationManager ("ArfWifiManager"); // The
default state is defined as being an Adhoc MAC layer with an ARF rate
control algorithm and both objects using their default attribute
values. By default, configure MAC and PHY for 802.11a.

OlsrHelper olsr_routing;
Ipv4StaticRoutingHelper static_routing;
Ipv4ListRoutingHelper list_routing;
list_routing.Add (static_routing, 0);
list_routing.Add (olsr_routing, 100);
InternetStackHelper internet;
internet.SetRoutingHelper (list_routing);

Ipv4AddressHelper ipv4Address;
ipv4Address.SetBase ("10.0.0.0", "255.255.255.0");

uint16_t port = 9; // Discard port(RFC 863)
OnOffHelper onoff ("ns3::UdpSocketFactory",
Address(InetSocketAddress(Ipv4Address ("10.0.0.1"), port)));
onoff.SetAttribute ("DataRate", DataRateValue(DataRate
("100kbps")));
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(1)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));

Ipv4InterfaceContainer addresses;
NodeContainer nodes;

if(NumNodesSide == 0)
NumNodesSide = NUM_NODES_SIDE;

for(uint32_t xi=0; xi<NumNodesSide; xi++)
{
for(uint32_t yi=0; yi<NumNodesSide; yi++)
{
Ptr<Node> node = CreateObject<Node>();
nodes.Add(node);

internet.Install (node);

Ptr<ConstantPositionMobilityModel> mobility =
CreateObject<ConstantPositionMobilityModel>();
mobility->SetPosition(Vector(xi*DISTANCE, yi*DISTANCE, 0));
node->AggregateObject(mobility);

NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac,
node);
Ipv4InterfaceContainer ipv4_interfaces = ipv4Address.Assign
(devices);
addresses.Add(ipv4_interfaces.Get(0));
}
}

OnOffHelper onOffHelper ("ns3::UdpSocketFactory", Address ());
for(uint32_t i=0; i<nodes.GetN (); i++)
{
Ipv4Address destaddr = addresses.GetAddress ((addresses.GetN () - 1
- i) % addresses.GetN ());
onOffHelper.SetAttribute ("Remote",
AddressValue(InetSocketAddress(destaddr, port)));
ApplicationContainer app = onOffHelper.Install (nodes.Get(i));
app.Start (Seconds (UniformVariable (20, 30).GetValue ()));
}

// internet.EnablePcapAll("wifi-olsr")
Ptr<FlowMonitor> monitor;
FlowMonitorHelper flowmon_helper;
// flowmon_helper.SetMonitorAttribute("StartTime",
TimeValue(Seconds(31)))
monitor = flowmon_helper.InstallAll();
monitor->SetAttribute("DelayBinWidth", DoubleValue(0.001));
monitor->SetAttribute("JitterBinWidth", DoubleValue(0.001));
monitor->SetAttribute("PacketSizeBinWidth", DoubleValue(20));

Simulator::Stop (Seconds (44.0));
Simulator::Run ();

monitor->CheckForLostPackets();
Ptr<FlowClassifier> classifier = flowmon_helper.GetClassifier();

if(Results == "")
{
string proto;
for (std::map< FlowId, FlowMonitor::FlowStats >::iterator
flow=monitor->GetFlowStats().begin(); flow!=monitor-
>GetFlowStats().end(); flow++)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(flow-
>first);
switch(t.protocol)
{
case(6):
proto = "TCP";
break;
case(17):
proto = "UDP";
break;
default:
exit(1);
}
std::cout << "FlowID: " << flow->first << "(" << proto << " "
<< t.sourceAddress << "/" << t.sourcePort << " --> "
<< t.destinationAddress << "/" << t.destinationPort << ")" <<
std::endl;
print_stats(flow->second);
}
}
else
monitor->SerializeToXmlFile(Results, true, true);

if(Plot)
{
Gnuplot gnuplot("DELAYSbyFLOW.png");
Gnuplot2dDataset dataset;
dataset.SetStyle(Gnuplot2dDataset::HISTEPS);
for (std::map< FlowId, FlowMonitor::FlowStats >::iterator
flow=monitor->GetFlowStats().begin(); flow!=monitor-
>GetFlowStats().end(); flow++)
{
Ipv4FlowClassifier::FiveTuple tupl = classifier->FindFlow(flow-
>first);
if(tupl.protocol == 17 && tupl.sourcePort == 698)
continue;
dataset.Add((double)flow->first, (double)flow-
>second.delaySum.GetSeconds() / (double)flow->second.rxPackets);
}
gnuplot.AddDataset(dataset);
gnuplot.GenerateOutput(std::cout);
}

Simulator::Destroy ();

return 0;
}




In this line "Ptr<FlowClassifier> classifier =
flowmon_helper.GetClassifier();", "flowmon_helper.GetClassifier()"
should return variable of type ns3::Ipv4FlowClassifier to variable
"classifier" and not of type ns3::FlowClassifier, because after it's
needed to use "classifier->FindFlow(flow->first)" that return a
pointer to ns3::Ipv4FlowClassifier::FiveTuple struct
("Ipv4FlowClassifier::FiveTuple tupl = classifier->FindFlow(flow-
>first);") and the class ns3::FlowClassifier (http://www.nsnam.org/
doxygen-release/classns3_1_1_flow_classifier.html) don't have function
"FindFlow".


Gustavo Carneiro

unread,
Jun 8, 2010, 10:21:45 AM6/8/10
to ns-3-...@googlegroups.com
Ah, I see your problem now.  Try to use DynamicCast, like this:

Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon_helper.GetClassifier ());
 
In Python the dynamic cast is automatic, that's why the code is simpler.



--
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.

Paulo Serralheiro

unread,
Jun 9, 2010, 10:02:25 AM6/9/10
to ns-3-users
Thanks Gustavo.
That is the problem and it's solve.

I leave here the program examples/flowmon/wifi-oslr-flowmon.py
translate to C++.

#include <fstream>
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/common-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/mobility-module.h"
#include "ns3/contrib-module.h"
#include "ns3/wifi-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/olsr-routing-protocol.h"
#include "ns3/ipv4-routing-protocol.h"
#include "ns3/flow-monitor-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("test");

double DISTANCE = 150; //m
uint32_t NUM_NODES_SIDE = 3;

void
printStats (FlowMonitor::FlowStats st)
{
std::cout << " Tx Bytes: " << st.txBytes << std::endl;
std::cout << " Rx Bytes: " << st.rxBytes << std::endl;
std::cout << " Tx Packets: " << st.txPackets << std::endl;
std::cout << " Rx Packets: " << st.rxPackets << std::endl;
std::cout << " Lost Packets: " << st.lostPackets << std::endl;
if (st.rxPackets > 0)
{
std::cout << " Mean{Delay}: " << (st.delaySum.GetSeconds() /
st.rxPackets) << std::endl;
std::cout << " Mean{Jitter}: " << (st.jitterSum.GetSeconds() /
(st.rxPackets-1)) << std::endl;
std::cout << " Mean{Hop Count}: " << st.timesForwarded /
st.rxPackets + 1 << std::endl;
std::cout << std::endl;
wifi.SetRemoteStationManager ("ns3::ArfWifiManager"); // The
// monitor->SerializeToXmlFile("Results.xml", true, true);
monitor->CheckForLostPackets();
Ptr<Ipv4FlowClassifier> classifier =
DynamicCast<Ipv4FlowClassifier>(flowmon_helper.GetClassifier());

if(Results == "")
{
string proto;
std::map< FlowId, FlowMonitor::FlowStats > stats = monitor-
>GetFlowStats();
for (std::map< FlowId, FlowMonitor::FlowStats >::iterator
flow=stats.begin(); flow!=stats.end(); flow++)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(flow-
>first);
switch(t.protocol)
{
case(6):
proto = "TCP";
break;
case(17):
proto = "UDP";
break;
default:
exit(1);
}
std::cout << "FlowID: " << flow->first << " (" << proto << " "
<< t.sourceAddress << "/" << t.sourcePort << " --> "
<< t.destinationAddress << "/" << t.destinationPort << ")" <<
std::endl;
printStats(flow->second);
}
}
else
monitor->SerializeToXmlFile(Results, true, true);

if(Plot)
{
Gnuplot gnuplot("DELAYSbyFLOW.png");
Gnuplot2dDataset dataset("Delay of each flow");
dataset.SetStyle(Gnuplot2dDataset::HISTEPS);
std::map< FlowId, FlowMonitor::FlowStats > stats = monitor-
>GetFlowStats();
for (std::map< FlowId, FlowMonitor::FlowStats >::iterator
flow=stats.begin(); flow!=stats.end(); flow++)

Ulfat Hussain

unread,
Jul 6, 2013, 3:28:26 AM7/6/13
to ns-3-...@googlegroups.com
i used that source code
     std::cout << std::endl; 
 
         wifi.SetRemoteStationManager ("ns3::ArfWifiManager");                                                                // The 
 
Simulator::Run ();
Simulator::Destory;
return 0;
}
}
and try to simulate but the following errors are produced

ulfat@ulfat-Inspiron-N4050:~/workspace/ns-allinone-3.17/ns-3.17$ ./waf --run scratch/test
Waf: Entering directory `/home/ulfat/workspace/ns-allinone-3.17/ns-3.17/build'
[ 749/2092] cxx: scratch/test.cc -> build/scratch/test.cc.5.o
[ 751/2092] cxx: scratch/ulfat.cc -> build/scratch/ulfat.cc.2.o
../scratch/ulfat.cc:21:29: fatal error: ns3/node-module.h: No such file or directory
compilation terminated.
../scratch/test.cc:4:32: fatal error: ns3/common-module.h: No such file or directory
compilation terminated.
Waf: Leaving directory `/home/ulfat/workspace/ns-allinone-3.17/ns-3.17/build'
Build failed
 -> task in 'ulfat' failed (exit status 1):
    {task 150052332: cxx ulfat.cc -> ulfat.cc.2.o}
['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-pthread', '-pthread', '-fno-strict-aliasing', '-fwrapv', '-fstack-protector', '-fno-strict-aliasing', '-I.', '-I..', '-I/usr/include/gtk-2.0', '-I/usr/lib/i386-linux-gnu/gtk-2.0/include', '-I/usr/include/atk-1.0', '-I/usr/include/cairo', '-I/usr/include/gdk-pixbuf-2.0', '-I/usr/include/pango-1.0', '-I/usr/include/gio-unix-2.0', '-I/usr/include/glib-2.0', '-I/usr/lib/i386-linux-gnu/glib-2.0/include', '-I/usr/include/pixman-1', '-I/usr/include/freetype2', '-I/usr/include/libpng12', '-I/usr/include/libxml2', '-I/usr/include/python2.7', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DHAVE_SYS_IOCTL_H=1', '-DHAVE_IF_NETS_H=1', '-DHAVE_PACKET_H=1', '-DHAVE_SQLITE3=1', '-DHAVE_IF_TUN_H=1', '-DHAVE_GSL=1', '-DNDEBUG', '../scratch/ulfat.cc', '-c', '-o', 'scratch/ulfat.cc.2.o']
 -> task in 'test' failed (exit status 1):
    {task 150059692: cxx test.cc -> test.cc.5.o}
['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-pthread', '-pthread', '-fno-strict-aliasing', '-fwrapv', '-fstack-protector', '-fno-strict-aliasing', '-I.', '-I..', '-I/usr/include/gtk-2.0', '-I/usr/lib/i386-linux-gnu/gtk-2.0/include', '-I/usr/include/atk-1.0', '-I/usr/include/cairo', '-I/usr/include/gdk-pixbuf-2.0', '-I/usr/include/pango-1.0', '-I/usr/include/gio-unix-2.0', '-I/usr/include/glib-2.0', '-I/usr/lib/i386-linux-gnu/glib-2.0/include', '-I/usr/include/pixman-1', '-I/usr/include/freetype2', '-I/usr/include/libpng12', '-I/usr/include/libxml2', '-I/usr/include/python2.7', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DHAVE_SYS_IOCTL_H=1', '-DHAVE_IF_NETS_H=1', '-DHAVE_PACKET_H=1', '-DHAVE_SQLITE3=1', '-DHAVE_IF_TUN_H=1', '-DHAVE_GSL=1', '-DNDEBUG', '../scratch/test.cc', '-c', '-o', 'scratch/test.cc.5.o']


how i can resolve as i was a new user on ns3 so with the help of you people i will become expert very soon

walid zahri

unread,
Jul 6, 2013, 11:48:56 AM7/6/13
to ns-3-...@googlegroups.com
Hii Paulo,

Can u plz help me to extract figure from my code (test3) i want to see lostpacket in this code , i try to simulate handover in LTE.

Tnx !
test3.cc
Reply all
Reply to author
Forward
0 new messages