A Discrete-Event Network Simulator
API
athstats-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/assert.h"
23 #include "ns3/abort.h"
24 #include "ns3/simulator.h"
25 #include "ns3/nstime.h"
26 #include "ns3/config.h"
27 #include "athstats-helper.h"
28 #include <iomanip>
29 #include <iostream>
30 #include <fstream>
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Athstats");
35 
37  : m_interval (Seconds (1.0))
38 {
39 }
40 
41 void
42 AthstatsHelper::EnableAthstats (std::string filename, uint32_t nodeid, uint32_t deviceid)
43 {
44  Ptr<AthstatsWifiTraceSink> athstats = CreateObject<AthstatsWifiTraceSink> ();
45  std::ostringstream oss;
46  oss << filename
47  << "_" << std::setfill ('0') << std::setw (3) << std::right << nodeid
48  << "_" << std::setfill ('0') << std::setw (3) << std::right << deviceid;
49  athstats->Open (oss.str ());
50 
51  oss.str ("");
52  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
53  std::string devicepath = oss.str ();
54 
55  Config::Connect (devicepath + "/Mac/MacTx", MakeCallback (&AthstatsWifiTraceSink::DevTxTrace, athstats));
56  Config::Connect (devicepath + "/Mac/MacRx", MakeCallback (&AthstatsWifiTraceSink::DevRxTrace, athstats));
57 
58  Config::Connect (devicepath + "/RemoteStationManager/TxRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxRtsFailedTrace, athstats));
59  Config::Connect (devicepath + "/RemoteStationManager/MacTxDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxDataFailedTrace, athstats));
60  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalRtsFailedTrace, athstats));
61  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalDataFailedTrace, athstats));
62 
63  Config::Connect (devicepath + "/Phy/State/RxOk", MakeCallback (&AthstatsWifiTraceSink::PhyRxOkTrace, athstats));
64  Config::Connect (devicepath + "/Phy/State/RxError", MakeCallback (&AthstatsWifiTraceSink::PhyRxErrorTrace, athstats));
65  Config::Connect (devicepath + "/Phy/State/Tx", MakeCallback (&AthstatsWifiTraceSink::PhyTxTrace, athstats));
66  Config::Connect (devicepath + "/Phy/State/State", MakeCallback (&AthstatsWifiTraceSink::PhyStateTrace, athstats));
67 }
68 
69 void
71 {
72  EnableAthstats (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
73 }
74 
75 void
77 {
78  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
79  {
80  Ptr<NetDevice> dev = *i;
81  EnableAthstats (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
82  }
83 }
84 
85 void
87 {
88  NetDeviceContainer devs;
89  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
90  {
91  Ptr<Node> node = *i;
92  for (uint32_t j = 0; j < node->GetNDevices (); ++j)
93  {
94  devs.Add (node->GetDevice (j));
95  }
96  }
97  EnableAthstats (filename, devs);
98 }
99 
101 
102 TypeId
104 {
105  static TypeId tid = TypeId ("ns3::AthstatsWifiTraceSink")
106  .SetParent<Object> ()
107  .SetGroupName ("Wifi")
108  .AddConstructor<AthstatsWifiTraceSink> ()
109  .AddAttribute ("Interval",
110  "Time interval between reports",
111  TimeValue (Seconds (1.0)),
113  MakeTimeChecker ())
114  ;
115  return tid;
116 }
117 
119  : m_txCount (0),
120  m_rxCount (0),
121  m_shortRetryCount (0),
122  m_longRetryCount (0),
123  m_exceededRetryCount (0),
124  m_phyRxOkCount (0),
125  m_phyRxErrorCount (0),
126  m_phyTxCount (0),
127  m_writer (0)
128 {
130 }
131 
133 {
134  NS_LOG_FUNCTION (this);
135 
136  if (m_writer != 0)
137  {
138  NS_LOG_LOGIC ("m_writer nonzero " << m_writer);
139  if (m_writer->is_open ())
140  {
141  NS_LOG_LOGIC ("m_writer open. Closing " << m_writer);
142  m_writer->close ();
143  }
144 
145  NS_LOG_LOGIC ("Deleting writer " << m_writer);
146  delete m_writer;
147 
148  NS_LOG_LOGIC ("m_writer = 0");
149  m_writer = 0;
150  }
151  else
152  {
153  NS_LOG_LOGIC ("m_writer == 0");
154  }
155 }
156 
157 void
159 {
160  m_txCount = 0;
161  m_rxCount = 0;
162  m_shortRetryCount = 0;
163  m_longRetryCount = 0;
165  m_phyRxOkCount = 0;
166  m_phyRxErrorCount = 0;
167  m_phyTxCount = 0;
168 }
169 
170 void
172 {
173  NS_LOG_FUNCTION (this << context << p);
174  ++m_txCount;
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION (this << context << p);
181  ++m_rxCount;
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this << context << address);
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this << context << address);
196 }
197 
198 void
200 {
201  NS_LOG_FUNCTION (this << context << address);
203 }
204 
205 void
207 {
208  NS_LOG_FUNCTION (this << context << address);
210 }
211 
212 void
213 AthstatsWifiTraceSink::PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
214 {
215  NS_LOG_FUNCTION (this << context << packet << " mode=" << mode << " snr=" << snr );
216  ++m_phyRxOkCount;
217 }
218 
219 void
220 AthstatsWifiTraceSink::PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
221 {
222  NS_LOG_FUNCTION (this << context << packet << " snr=" << snr );
224 }
225 
226 void
227 AthstatsWifiTraceSink::PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
228 {
229  NS_LOG_FUNCTION (this << context << packet << "PHYTX mode=" << mode );
230  ++m_phyTxCount;
231 }
232 
233 void
234 AthstatsWifiTraceSink::PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
235 {
236  NS_LOG_FUNCTION (this << context << start << duration << state);
237 
238 }
239 
240 void
241 AthstatsWifiTraceSink::Open (std::string const &name)
242 {
243  NS_LOG_FUNCTION (this << name);
244  NS_ABORT_MSG_UNLESS (m_writer == 0, "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
245 
246  m_writer = new std::ofstream ();
247  NS_ABORT_MSG_UNLESS (m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
248 
249  NS_LOG_LOGIC ("Created writer " << m_writer);
250 
251  m_writer->open (name.c_str (), std::ios_base::binary | std::ios_base::out);
252  NS_ABORT_MSG_IF (m_writer->fail (), "AthstatsWifiTraceSink::Open (): m_writer->open (" << name.c_str () << ") failed");
253 
254  NS_ASSERT_MSG (m_writer->is_open (), "AthstatsWifiTraceSink::Open (): m_writer not open");
255 
256  NS_LOG_LOGIC ("Writer opened successfully");
257 }
258 
259 void
261 {
262  NS_LOG_FUNCTION (this);
263  //The comments below refer to how each value maps to madwifi's athstats
264  //I know C strings are ugly but that's the quickest way to use exactly the same format as in madwifi
265  char str[200];
266  snprintf (str, 200, "%8u %8u %7u %7u %7u %6u %6u %6u %7u %4u %3uM\n",
267  (unsigned int) m_txCount, // /proc/net/dev transmitted packets to which we should subract mgmt frames
268  (unsigned int) m_rxCount, // /proc/net/dev received packets but subracts mgmt frames from it
269  (unsigned int) 0, // ast_tx_altrate
270  (unsigned int) m_shortRetryCount, // ast_tx_shortretry
271  (unsigned int) m_longRetryCount, // ast_tx_longretry
272  (unsigned int) m_exceededRetryCount, // ast_tx_xretries
273  (unsigned int) m_phyRxErrorCount, // ast_rx_crcerr
274  (unsigned int) 0, // ast_rx_badcrypt
275  (unsigned int) 0, // ast_rx_phyerr
276  (unsigned int) 0, // ast_rx_rssi
277  (unsigned int) 0 // rate
278  );
279 
280  if (m_writer)
281  {
282 
283  *m_writer << str;
284 
285  ResetCounters ();
287  }
288 }
289 
290 } //namespace ns3
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
Function to be called when a frame reception by the PHY layer of the considered device resulted in an...
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void DevTxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device transmits a packet
void PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
Function to be called when the PHY layer of the considered device changes state.
void TxFinalRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a RTS frame has exceeded the retry limit...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
def start()
Definition: core.py:1482
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
void TxRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when a RTS frame transmission by the considered device has failed.
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:99
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
trace sink for wifi device that mimics madwifi's athstats tool.
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:30
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
AttributeValue implementation for Time.
Definition: nstime.h:957
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:835
uint32_t GetNDevices(void) const
Definition: node.cc:150
void Open(std::string const &name)
Open a file for output.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
Function to be called when a frame is being transmitted by the PHY layer of the considered device...
an EUI-48 address
Definition: mac48-address.h:43
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1401
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:958
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, enum WifiPreamble preamble)
Function to be called when the PHY layer of the considered device receives a frame.
void TxFinalDataFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a data frame has exceeded the retry limit...
A base class which provides memory management and object aggregation.
Definition: object.h:87
tuple address
Definition: first.py:37
void TxDataFailedTrace(std::string context, Mac48Address address)
Function to be called when a data frame transmission by the considered device has failed...
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
State
The state of the PHY layer.
Definition: wifi-phy.h:168
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
static TypeId GetTypeId(void)