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 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("Athstats");
36 
38  : m_interval (Seconds (1.0))
39 {
40 }
41 
42 void
43 AthstatsHelper::EnableAthstats (std::string filename, uint32_t nodeid, uint32_t deviceid)
44 {
45  Ptr<AthstatsWifiTraceSink> athstats = CreateObject<AthstatsWifiTraceSink> ();
46  std::ostringstream oss;
47  oss << filename
48  << "_" << std::setfill ('0') << std::setw (3) << std::right << nodeid
49  << "_" << std::setfill ('0') << std::setw (3) << std::right << deviceid;
50  athstats->Open (oss.str ());
51 
52  oss.str ("");
53  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
54  std::string devicepath = oss.str ();
55 
56  Config::Connect (devicepath + "/Mac/MacTx", MakeCallback (&AthstatsWifiTraceSink::DevTxTrace, athstats));
57  Config::Connect (devicepath + "/Mac/MacRx", MakeCallback (&AthstatsWifiTraceSink::DevRxTrace, athstats));
58 
59  Config::Connect (devicepath + "/RemoteStationManager/TxRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxRtsFailedTrace, athstats));
60  Config::Connect (devicepath + "/RemoteStationManager/MacTxDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxDataFailedTrace, athstats));
61  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalRtsFailedTrace, athstats));
62  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalDataFailedTrace, athstats));
63 
64  Config::Connect (devicepath + "/Phy/State/RxOk", MakeCallback (&AthstatsWifiTraceSink::PhyRxOkTrace, athstats));
65  Config::Connect (devicepath + "/Phy/State/RxError", MakeCallback (&AthstatsWifiTraceSink::PhyRxErrorTrace, athstats));
66  Config::Connect (devicepath + "/Phy/State/Tx", MakeCallback (&AthstatsWifiTraceSink::PhyTxTrace, athstats));
67  Config::Connect (devicepath + "/Phy/State/State", MakeCallback (&AthstatsWifiTraceSink::PhyStateTrace, athstats));
68 }
69 
70 void
72 {
73  EnableAthstats (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
74 }
75 
76 
77 void
79 {
80  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
81  {
82  Ptr<NetDevice> dev = *i;
83  EnableAthstats (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
84  }
85 }
86 
87 
88 void
90 {
91  NetDeviceContainer devs;
92  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
93  {
94  Ptr<Node> node = *i;
95  for (uint32_t j = 0; j < node->GetNDevices (); ++j)
96  {
97  devs.Add (node->GetDevice (j));
98  }
99  }
100  EnableAthstats (filename, devs);
101 }
102 
103 
104 
105 
106 
108 
109 TypeId
111 {
112  static TypeId tid = TypeId ("ns3::AthstatsWifiTraceSink")
113  .SetParent<Object> ()
114  .AddConstructor<AthstatsWifiTraceSink> ()
115  .AddAttribute ("Interval",
116  "Time interval between reports",
117  TimeValue (Seconds (1.0)),
119  MakeTimeChecker ())
120  ;
121  return tid;
122 }
123 
125  : m_txCount (0),
126  m_rxCount (0),
127  m_shortRetryCount (0),
128  m_longRetryCount (0),
129  m_exceededRetryCount (0),
130  m_phyRxOkCount (0),
131  m_phyRxErrorCount (0),
132  m_phyTxCount (0),
133  m_writer (0)
134 {
136 }
137 
139 {
140  NS_LOG_FUNCTION (this);
141 
142  if (m_writer != 0)
143  {
144  NS_LOG_LOGIC ("m_writer nonzero " << m_writer);
145  if (m_writer->is_open ())
146  {
147  NS_LOG_LOGIC ("m_writer open. Closing " << m_writer);
148  m_writer->close ();
149  }
150 
151  NS_LOG_LOGIC ("Deleting writer " << m_writer);
152  delete m_writer;
153 
154  NS_LOG_LOGIC ("m_writer = 0");
155  m_writer = 0;
156  }
157  else
158  {
159  NS_LOG_LOGIC ("m_writer == 0");
160  }
161 }
162 
163 void
165 {
166  m_txCount = 0;
167  m_rxCount = 0;
168  m_shortRetryCount = 0;
169  m_longRetryCount = 0;
171  m_phyRxOkCount = 0;
172  m_phyRxErrorCount = 0;
173  m_phyTxCount = 0;
174 }
175 
176 void
178 {
179  NS_LOG_FUNCTION (this << context << p);
180  ++m_txCount;
181 }
182 
183 void
185 {
186  NS_LOG_FUNCTION (this << context << p);
187  ++m_rxCount;
188 }
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
214 {
215  NS_LOG_FUNCTION (this << context << address);
217 }
218 
219 
220 
221 void
222 AthstatsWifiTraceSink::PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
223 {
224  NS_LOG_FUNCTION (this << context << packet << " mode=" << mode << " snr=" << snr );
225  ++m_phyRxOkCount;
226 }
227 
228 void
229 AthstatsWifiTraceSink::PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
230 {
231  NS_LOG_FUNCTION (this << context << packet << " snr=" << snr );
233 }
234 
235 void
236 AthstatsWifiTraceSink::PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
237 {
238  NS_LOG_FUNCTION (this << context << packet << "PHYTX mode=" << mode );
239  ++m_phyTxCount;
240 }
241 
242 
243 void
244 AthstatsWifiTraceSink::PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
245 {
246  NS_LOG_FUNCTION (this << context << start << duration << state);
247 
248 }
249 
250 
251 
252 void
253 AthstatsWifiTraceSink::Open (std::string const &name)
254 {
255  NS_LOG_FUNCTION (this << name);
256  NS_ABORT_MSG_UNLESS (m_writer == 0, "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
257 
258  m_writer = new std::ofstream ();
259  NS_ABORT_MSG_UNLESS (m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
260 
261  NS_LOG_LOGIC ("Created writer " << m_writer);
262 
263  m_writer->open (name.c_str (), std::ios_base::binary | std::ios_base::out);
264  NS_ABORT_MSG_IF (m_writer->fail (), "AthstatsWifiTraceSink::Open (): m_writer->open (" << name.c_str () << ") failed");
265 
266  NS_ASSERT_MSG (m_writer->is_open (), "AthstatsWifiTraceSink::Open (): m_writer not open");
267 
268  NS_LOG_LOGIC ("Writer opened successfully");
269 }
270 
271 
272 void
274 {
275  NS_ABORT_MSG_UNLESS (this, "function called with null this pointer, now=" << Now () );
276  // the comments below refer to how each value maps to madwifi's athstats
277  // I know C strings are ugly but that's the quickest way to use exactly the same format as in madwifi
278  char str[200];
279  snprintf (str, 200, "%8u %8u %7u %7u %7u %6u %6u %6u %7u %4u %3uM\n",
280  (unsigned int) m_txCount, // /proc/net/dev transmitted packets to which we should subract mgmt frames
281  (unsigned int) m_rxCount, // /proc/net/dev received packets but subracts mgmt frames from it
282  (unsigned int) 0, // ast_tx_altrate,
283  (unsigned int) m_shortRetryCount, // ast_tx_shortretry,
284  (unsigned int) m_longRetryCount, // ast_tx_longretry,
285  (unsigned int) m_exceededRetryCount, // ast_tx_xretries,
286  (unsigned int) m_phyRxErrorCount, // ast_rx_crcerr,
287  (unsigned int) 0, // ast_rx_badcrypt,
288  (unsigned int) 0, // ast_rx_phyerr,
289  (unsigned int) 0, // ast_rx_rssi,
290  (unsigned int) 0 // rate
291  );
292 
293  if (m_writer)
294  {
295 
296  *m_writer << str;
297 
298  ResetCounters ();
300  }
301 }
302 
303 
304 
305 
306 } // namespace ns3
307 
308 
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:95
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.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:738
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:93
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:439
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:29
AttributeValue implementation for Time.
Definition: nstime.h:921
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:134
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
uint32_t GetNDevices(void) const
Definition: node.cc:142
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:980
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:922
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:84
#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:859
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.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
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:132
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
static TypeId GetTypeId(void)