以wifi-example-sim.cc为例说明NS3统计数据模型


http://blog.csdn.net/yanerhao/article/details/53979539


利用NS3已有的Trace系统或者Log机制收集记录和统计数据,例如MAC层收发帧数目,网络层以上收发包数目的跟踪与统计,这里选取example/stats/wifi-example-sim.cc为例来很好说明问题:

这个仿真程序是一个简单的实验,包括两个节点,基于AdhocMAC信道模型,包含NS3仿真所需常见模型如节点/网络设备/协议栈和应用进程,这里的应用进程Sender   和Receiver,基于UDP的不可靠连接。

  1. #include <ctime>  
  2. #include <sstream>  
  3. #include "ns3/core-module.h"  
  4. #include "ns3/network-module.h"  
  5. #include "ns3/mobility-module.h"  
  6. #include "ns3/wifi-module.h"  
  7. #include "ns3/internet-module.h"  
  8. #include "ns3/stats-module.h"  
  9. #include "wifi-example-apps.h"  
  10. using namespace ns3;  
  11. using namespace std;  
  12. NS_LOG_COMPONENT_DEFINE ("WiFiDistanceExperiment");  
  13. void TxCallback (Ptr<CounterCalculator<uint32_t> > datac,  
  14.                  std::string path, Ptr<const Packet> packet) {  
  15.   NS_LOG_INFO ("Sent frame counted in " <<  
  16.                datac->GetKey ());  
  17.   datac->Update ();  
  18.   // end TxCallback  
  19. }  
  20. //----------------------------------------------------------------------  
  21. //-- main  
  22. //----------------------------------------------  
  23. int main (int argc, char *argv[]) {  
  24.   
  25.   double distance = 50.0;  
  26.   string format ("omnet");  
  27.   
  28.   string experiment ("wifi-distance-test");  
  29.   string strategy ("wifi-default");  
  30.   string input;  
  31.   string runID;  
  32.   
  33.   {  
  34.     stringstream sstr;  
  35.     sstr << "run-" << time (NULL);  
  36.     runID = sstr.str ();  
  37.   }  
  38.   
  39.   // Set up command line parameters used to control the experiment.  
  40.   CommandLine cmd;  
  41.   cmd.AddValue ("distance""Distance apart to place nodes (in meters).",  
  42.                 distance);  
  43.   cmd.AddValue ("format""Format to use for data output.",  
  44.                 format);  
  45.   cmd.AddValue ("experiment""Identifier for experiment.",  
  46.                 experiment);  
  47.   cmd.AddValue ("strategy""Identifier for strategy.",  
  48.                 strategy);  
  49.   cmd.AddValue ("run""Identifier for run.",  
  50.                 runID);  
  51.   cmd.Parse (argc, argv);  
  52.   
  53.   if (format != "omnet" && format != "db") {  
  54.       NS_LOG_ERROR ("Unknown output format '" << format << "'");  
  55.       return -1;  
  56.     }  
  57.   
  58.   #ifndef STATS_HAS_SQLITE3  
  59.   if (format == "db") {  
  60.       NS_LOG_ERROR ("sqlite support not compiled in.");  
  61.       return -1;  
  62.     }  
  63.   #endif  
  64.   
  65.   {  
  66.     stringstream sstr ("");  
  67.     sstr << distance;  
  68.     input = sstr.str ();  
  69.   }  
  70.   //------------------------------------------------------------  
  71.   //-- Create nodes and network stacks  
  72.   //--------------------------------------------  
  73.   NS_LOG_INFO ("Creating nodes.");  
  74.   NodeContainer nodes;  
  75.   nodes.Create (2);  
  76.   
  77.   NS_LOG_INFO ("Installing WiFi and Internet stack.");  
  78.   WifiHelper wifi;  
  79.   WifiMacHelper wifiMac;  
  80.   wifiMac.SetType ("ns3::AdhocWifiMac");  
  81.   YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();  
  82.   YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();  
  83.   wifiPhy.SetChannel (wifiChannel.Create ());  
  84.   NetDeviceContainer nodeDevices = wifi.Install (wifiPhy, wifiMac, nodes);  
  85.   
  86.   InternetStackHelper internet;  
  87.   internet.Install (nodes);  
  88.   Ipv4AddressHelper ipAddrs;  
  89.   ipAddrs.SetBase ("192.168.0.0""255.255.255.0");  
  90.   ipAddrs.Assign (nodeDevices);  
  91.   //------------------------------------------------------------  
  92.   //-- Setup physical layout  
  93.   //--------------------------------------------  
  94.   NS_LOG_INFO ("Installing static mobility; distance " << distance << " .");  
  95.   MobilityHelper mobility;  
  96.   Ptr<ListPositionAllocator> positionAlloc =  
  97.     CreateObject<ListPositionAllocator>();  
  98.   positionAlloc->Add (Vector (0.0, 0.0, 0.0));  
  99.   positionAlloc->Add (Vector (0.0, distance, 0.0));  
  100.   mobility.SetPositionAllocator (positionAlloc);  
  101.   mobility.Install (nodes);  
  102.   //------------------------------------------------------------  
  103.   //-- Create a custom traffic source and sink  
  104.   //--------------------------------------------  
  105.   NS_LOG_INFO ("Create traffic source & sink.");  
  106.   Ptr<Node> appSource = NodeList::GetNode (0);  
  107.   Ptr<Sender> sender = CreateObject<Sender>();  
  108.   appSource->AddApplication (sender);  
  109.   sender->SetStartTime (Seconds (1));  
  110.   
  111.   Ptr<Node> appSink = NodeList::GetNode (1);  
  112.   Ptr<Receiver> receiver = CreateObject<Receiver>();  
  113.   appSink->AddApplication (receiver);  
  114.   receiver->SetStartTime (Seconds (0));  
  115.   
  116.   Config::Set ("/NodeList/*/ApplicationList/*/$Sender/Destination",  
  117.                Ipv4AddressValue ("192.168.0.2"));  
  118.   //------------------------------------------------------------  
  119.   //-- Setup stats and data collection  
  120.   //--------------------------------------------  
  121.   
  122.   // Create a DataCollector object to hold information about this run.  
  123.   DataCollector data;  
  124.   data.DescribeRun (experiment,//experiment 是对象  
  125.                     strategy,//strategy 是被检查的代码或者参数  
  126.                     input,//2个节点距离  
  127.                     runID);  
  128.   
  129.   // Add any information we wish to record about this run.  
  130.   data.AddMetadata ("author""tjkopena");  
  131.   
  132.   // Create a counter to track how many frames are generated.  Updates  
  133.   // are triggered by the trace signal generated by the WiFi MAC model  
  134.   // object.  Here we connect the counter to the signal via the simple  
  135.   // TxCallback() glue function defined above.  
  136.   Ptr<CounterCalculator<uint32_t> > totalTx =  
  137.     CreateObject<CounterCalculator<uint32_t> >();  
  138.   totalTx->SetKey ("wifi-tx-frames");  
  139.   totalTx->SetContext ("node[0]");  
  140.   Config::Connect ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Mac/MacTx",  
  141.                    MakeBoundCallback (&TxCallback, totalTx));  
  142.   data.AddDataCalculator (totalTx);  
  143.   
  144.   // This is similar, but creates a counter to track how many frames  
  145.   // are received.  Instead of our own glue function, this uses a  
  146.   // method of an adapter class to connect a counter directly to the  
  147.   // trace signal generated by the WiFi MAC.  
  148.   Ptr<PacketCounterCalculator> totalRx =  
  149.     CreateObject<PacketCounterCalculator>();  
  150.   totalRx->SetKey ("wifi-rx-frames");  
  151.   totalRx->SetContext ("node[1]");  
  152.   Config::Connect ("/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/Mac/MacRx",  
  153.                    MakeCallback (&PacketCounterCalculator::PacketUpdate,  
  154.                                  totalRx));  
  155.   data.AddDataCalculator (totalRx);  
  156.   // This counter tracks how many packets---as opposed to frames---are  
  157.   // generated.  This is connected directly to a trace signal provided  
  158.   // by our Sender class.  
  159.   Ptr<PacketCounterCalculator> appTx =  
  160.     CreateObject<PacketCounterCalculator>();  
  161.   appTx->SetKey ("sender-tx-packets");  
  162.   appTx->SetContext ("node[0]");  
  163.   Config::Connect ("/NodeList/0/ApplicationList/*/$Sender/Tx",  
  164.                    MakeCallback (&PacketCounterCalculator::PacketUpdate,  
  165.                                  appTx));  
  166.   data.AddDataCalculator (appTx);  
  167.   
  168.   // Here a counter for received packets is directly manipulated by  
  169.   // one of the custom objects in our simulation, the Receiver  
  170.   // Application.  The Receiver object is given a pointer to the  
  171.   // counter and calls its Update() method whenever a packet arrives.  
  172.   Ptr<CounterCalculator<> > appRx =  
  173.     CreateObject<CounterCalculator<> >();  
  174.   appRx->SetKey ("receiver-rx-packets");  
  175.   appRx->SetContext ("node[1]");  
  176.   receiver->SetCounter (appRx);  
  177.   data.AddDataCalculator (appRx);  
  178.   /**  
  179.    * Just to show this is here...  
  180.    Ptr<MinMaxAvgTotalCalculator<uint32_t> > test =   
  181.    CreateObject<MinMaxAvgTotalCalculator<uint32_t> >();  
  182.    test->SetKey("test-dc");  
  183.    data.AddDataCalculator(test);  
  184.   
  185.    test->Update(4);  
  186.    test->Update(8);  
  187.    test->Update(24);  
  188.    test->Update(12);  
  189.   **/  
  190.   
  191.   // This DataCalculator connects directly to the transmit trace  
  192.   // provided by our Sender Application.  It records some basic  
  193.   // statistics about the sizes of the packets received (min, max,  
  194.   // avg, total # bytes), although in this scenaro they're fixed.  
  195.   Ptr<PacketSizeMinMaxAvgTotalCalculator> appTxPkts =  
  196.     CreateObject<PacketSizeMinMaxAvgTotalCalculator>();  
  197.   appTxPkts->SetKey ("tx-pkt-size");  
  198.   appTxPkts->SetContext ("node[0]");  
  199.   Config::Connect ("/NodeList/0/ApplicationList/*/$Sender/Tx",  
  200.                    MakeCallback  
  201.                      (&PacketSizeMinMaxAvgTotalCalculator::PacketUpdate,  
  202.                      appTxPkts));  
  203.   data.AddDataCalculator (appTxPkts);  
  204.   // Here we directly manipulate another DataCollector tracking min,  
  205.   // max, total, and average propagation delays.  Check out the Sender  
  206.   // and Receiver classes to see how packets are tagged with  
  207.   // timestamps to do this.  
  208.   Ptr<TimeMinMaxAvgTotalCalculator> delayStat =  
  209.     CreateObject<TimeMinMaxAvgTotalCalculator>();  
  210.   delayStat->SetKey ("delay");  
  211.   delayStat->SetContext (".");  
  212.   receiver->SetDelayTracker (delayStat);  
  213.   data.AddDataCalculator (delayStat);  
  214.   
  215.   
  216.   //------------------------------------------------------------  
  217.   //-- Run the simulation  
  218.   //--------------------------------------------  
  219.   NS_LOG_INFO ("Run Simulation.");  
  220.   Simulator::Run ();  
  221.   //------------------------------------------------------------  
  222.   //-- Generate statistics output.  
  223.   //--------------------------------------------  
  224.   
  225.   // Pick an output writer based in the requested format.  
  226.   Ptr<DataOutputInterface> output = 0;  
  227.   if (format == "omnet") {  
  228.       NS_LOG_INFO ("Creating omnet formatted data output.");  
  229.       output = CreateObject<OmnetDataOutput>();  
  230.     } else if (format == "db") {  
  231.     #ifdef STATS_HAS_SQLITE3  
  232.       NS_LOG_INFO ("Creating sqlite formatted data output.");  
  233.       output = CreateObject<SqliteDataOutput>();  
  234.     #endif  
  235.     } else {  
  236.       NS_LOG_ERROR ("Unknown output format " << format);  
  237.     }  
  238.   
  239.   // Finally, have that writer interrogate the DataCollector and save  
  240.   // the results.  
  241.   if (output != 0)  
  242.     output->Output (data);  
  243.   
  244.   // Free any memory here at the end of this example.  
  245.   Simulator::Destroy ();  
  246.   
  247.   // end main  
  248. }  
一 给定本次仿真参数distance,format,experiment,strategy,runID在初始化的同时也可以通过命令行改变,这些参数用于从多次实验中快速区分和组合数据。

二 创建节点和网络模型

三 安装协议栈,并分配IP

四 设置移动模型,这里为静止,并给定初始位置

五 安装应用,这里安装Sender / Receiver,自定义的见examples/stats/wifi-example-apps.h|cc

数据统计与收集,这是本文重点,下面具体分析。

这里创建DataCollector对象来存储运行信息,并通过Trace机制记录收发端帧和分组传输情况。

1 记录发端帧传输(基WIFI MAC对界)

通过CounterCalculator(src/stats/model/basic-data-calculators.h )类实现计数,利用Trace机制,当节点0上wifiNetDevice/Mac/MacTx变化(source),通过Config::Connect关联,定义的TxCallback作为sink函数调用,导致CounterCalculator::update调用即m_count++从而起到计数功能;

2 记录收端帧传输(基WIFI MAC对界)

类似情况1,虽然这里的sink函数是PacketConterCalculator::PacketUpdate(src/network/utils/packet-data-calculators.cc),但是该函数仍然是通过CounterCalculator::update实现计数,即利用Trace机制,当节点1上wifiNetDevice/Mac/MacRx变化(source),通过Config::Connect关联;

3 记录发端分组传输

 也是通过PacketConterCalculator::PacketUpdate实现计数,利用Trace机制,当节点0上/Application/*/$Sender/Tx变化(source),通过通过Config::Connect关联,定义的PacketConterCalculator::PacketUpdate作为sink函数调用;

4 记录收端分组接收

由于收端应用Receiver没有定义traced source,故这里没有采用Trace机制,而是直接利用Receiver:;SetCounter直接操作,通过SetCounter显示类型转换,j将appRx赋值给Receiver内部计数器,从而实现计数

以上均是通过PacketConterCalculator(src/network/utils/packet-data-calculators.cc)或者CounterCalculator(src/stats/model/basic-data-calculators.h )实现传输单元的计数,\下一个将通过引入PacketSizeMinMaxAvgTotalCalculator (src/network/utils/packet-data-calculators.h|cc)和MinMaxAvgTotalCalculator(src/stats/model/basic-data-calculators.h)实现单元内大小的记录。

5 记录发端分组大小

这里采用Trace机制,节点0上/Application/*/$Sender/Tx变化(source),通过通过Config::Connect关联,定义的PacketSizeMinMaxAvgTotalCalculator::PacketUpdate作为sink函数调用,从而MinMaxAvgTotalCalculator::Update实现大小的记录。

6 记录端到端产生分组时的延迟

类似情况4,不采用Trace机制,直接利用Receiver:;SetDelayTracker记录传世时延最值/平均值等

七 运行程序命令

八 统计结果输出

对于输出要么OMNet++(纯文本输出格式)要么SQLite(数据库格式输出),这取决于程序头部定义的参数format,并最终DataCollector对象进行存储。

九 控制脚本实现最后运行

通过 一个简单的控制脚本实现该仿真程序在不同距离下大量重复(作为输入)实验后运行画图。可参考example/stats/wifi-example-db.sh(以后自己写多个不同输入下重复仿真项目时可参考这个)。该运行脚本每次都是基于一个不同的距离作为输入,收集每次仿真结果到SQLite数据库,其中对于每个距离输入,进行5次重复实验以减小波动。全部仿真完成只需几十秒,在完成存储到数据库后,可通过SQLite命令行进行SQL查询。并调用 wifi-example.gnuplot画图

进入该目录

  1. cd /NS3/ns-allinone-3.25/ns-3.25/examples/stats  
  2. ./wifi-example-db.sh   

产生data.db数据库,wifi-default.data和wifi-default.eps图

图是对应距离下的丢包率以表征WiFi模型性能。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值