A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("Athstats");
34 
35 namespace ns3 {
36 
37 
39  : m_interval (Seconds (1.0))
40 {
41 }
42 
43 void
44 AthstatsHelper::EnableAthstats (std::string filename, uint32_t nodeid, uint32_t deviceid)
45 {
46  Ptr<AthstatsWifiTraceSink> athstats = CreateObject<AthstatsWifiTraceSink> ();
47  std::ostringstream oss;
48  oss << filename
49  << "_" << std::setfill ('0') << std::setw (3) << std::right << nodeid
50  << "_" << std::setfill ('0') << std::setw (3) << std::right << deviceid;
51  athstats->Open (oss.str ());
52 
53  oss.str ("");
54  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
55  std::string devicepath = oss.str ();
56 
57  Config::Connect (devicepath + "/Mac/MacTx", MakeCallback (&AthstatsWifiTraceSink::DevTxTrace, athstats));
58  Config::Connect (devicepath + "/Mac/MacRx", MakeCallback (&AthstatsWifiTraceSink::DevRxTrace, athstats));
59 
60  Config::Connect (devicepath + "/RemoteStationManager/TxRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxRtsFailedTrace, athstats));
61  Config::Connect (devicepath + "/RemoteStationManager/MacTxDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxDataFailedTrace, athstats));
62  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalRtsFailedTrace, athstats));
63  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalDataFailedTrace, athstats));
64 
65  Config::Connect (devicepath + "/Phy/State/RxOk", MakeCallback (&AthstatsWifiTraceSink::PhyRxOkTrace, athstats));
66  Config::Connect (devicepath + "/Phy/State/RxError", MakeCallback (&AthstatsWifiTraceSink::PhyRxErrorTrace, athstats));
67  Config::Connect (devicepath + "/Phy/State/Tx", MakeCallback (&AthstatsWifiTraceSink::PhyTxTrace, athstats));
68  Config::Connect (devicepath + "/Phy/State/State", MakeCallback (&AthstatsWifiTraceSink::PhyStateTrace, athstats));
69 }
70 
71 void
73 {
74  EnableAthstats (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
75 }
76 
77 
78 void
80 {
81  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
82  {
83  Ptr<NetDevice> dev = *i;
84  EnableAthstats (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
85  }
86 }
87 
88 
89 void
91 {
92  NetDeviceContainer devs;
93  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
94  {
95  Ptr<Node> node = *i;
96  for (uint32_t j = 0; j < node->GetNDevices (); ++j)
97  {
98  devs.Add (node->GetDevice (j));
99  }
100  }
101  EnableAthstats (filename, devs);
102 }
103 
104 
105 
106 
107 
109  ;
110 
111 TypeId
113 {
114  static TypeId tid = TypeId ("ns3::AthstatsWifiTraceSink")
115  .SetParent<Object> ()
116  .AddConstructor<AthstatsWifiTraceSink> ()
117  .AddAttribute ("Interval",
118  "Time interval between reports",
119  TimeValue (Seconds (1.0)),
120  MakeTimeAccessor (&AthstatsWifiTraceSink::m_interval),
121  MakeTimeChecker ())
122  ;
123  return tid;
124 }
125 
127  : m_txCount (0),
128  m_rxCount (0),
129  m_shortRetryCount (0),
130  m_longRetryCount (0),
131  m_exceededRetryCount (0),
132  m_phyRxOkCount (0),
133  m_phyRxErrorCount (0),
134  m_phyTxCount (0),
135  m_writer (0)
136 {
138 }
139 
141 {
142  NS_LOG_FUNCTION (this);
143 
144  if (m_writer != 0)
145  {
146  NS_LOG_LOGIC ("m_writer nonzero " << m_writer);
147  if (m_writer->is_open ())
148  {
149  NS_LOG_LOGIC ("m_writer open. Closing " << m_writer);
150  m_writer->close ();
151  }
152 
153  NS_LOG_LOGIC ("Deleting writer " << m_writer);
154  delete m_writer;
155 
156  NS_LOG_LOGIC ("m_writer = 0");
157  m_writer = 0;
158  }
159  else
160  {
161  NS_LOG_LOGIC ("m_writer == 0");
162  }
163 }
164 
165 void
167 {
168  m_txCount = 0;
169  m_rxCount = 0;
170  m_shortRetryCount = 0;
171  m_longRetryCount = 0;
173  m_phyRxOkCount = 0;
174  m_phyRxErrorCount = 0;
175  m_phyTxCount = 0;
176 }
177 
178 void
180 {
181  NS_LOG_FUNCTION (this << context << p);
182  ++m_txCount;
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION (this << context << p);
189  ++m_rxCount;
190 }
191 
192 
193 void
195 {
196  NS_LOG_FUNCTION (this << context << address);
198 }
199 
200 void
202 {
203  NS_LOG_FUNCTION (this << context << address);
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION (this << context << address);
212 }
213 
214 void
216 {
217  NS_LOG_FUNCTION (this << context << address);
219 }
220 
221 
222 
223 void
224 AthstatsWifiTraceSink::PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
225 {
226  NS_LOG_FUNCTION (this << context << packet << " mode=" << mode << " snr=" << snr );
227  ++m_phyRxOkCount;
228 }
229 
230 void
231 AthstatsWifiTraceSink::PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
232 {
233  NS_LOG_FUNCTION (this << context << packet << " snr=" << snr );
235 }
236 
237 void
238 AthstatsWifiTraceSink::PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
239 {
240  NS_LOG_FUNCTION (this << context << packet << "PHYTX mode=" << mode );
241  ++m_phyTxCount;
242 }
243 
244 
245 void
246 AthstatsWifiTraceSink::PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
247 {
248  NS_LOG_FUNCTION (this << context << start << duration << state);
249 
250 }
251 
252 
253 
254 void
255 AthstatsWifiTraceSink::Open (std::string const &name)
256 {
257  NS_LOG_FUNCTION (this << name);
258  NS_ABORT_MSG_UNLESS (m_writer == 0, "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
259 
260  m_writer = new std::ofstream ();
261  NS_ABORT_MSG_UNLESS (m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
262 
263  NS_LOG_LOGIC ("Created writer " << m_writer);
264 
265  m_writer->open (name.c_str (), std::ios_base::binary | std::ios_base::out);
266  NS_ABORT_MSG_IF (m_writer->fail (), "AthstatsWifiTraceSink::Open (): m_writer->open (" << name.c_str () << ") failed");
267 
268  NS_ASSERT_MSG (m_writer->is_open (), "AthstatsWifiTraceSink::Open (): m_writer not open");
269 
270  NS_LOG_LOGIC ("Writer opened successfully");
271 }
272 
273 
274 void
276 {
277  NS_ABORT_MSG_UNLESS (this, "function called with null this pointer, now=" << Now () );
278  // the comments below refer to how each value maps to madwifi's athstats
279  // I know C strings are ugly but that's the quickest way to use exactly the same format as in madwifi
280  char str[200];
281  snprintf (str, 200, "%8u %8u %7u %7u %7u %6u %6u %6u %7u %4u %3uM\n",
282  (unsigned int) m_txCount, // /proc/net/dev transmitted packets to which we should subract mgmt frames
283  (unsigned int) m_rxCount, // /proc/net/dev received packets but subracts mgmt frames from it
284  (unsigned int) 0, // ast_tx_altrate,
285  (unsigned int) m_shortRetryCount, // ast_tx_shortretry,
286  (unsigned int) m_longRetryCount, // ast_tx_longretry,
287  (unsigned int) m_exceededRetryCount, // ast_tx_xretries,
288  (unsigned int) m_phyRxErrorCount, // ast_rx_crcerr,
289  (unsigned int) 0, // ast_rx_badcrypt,
290  (unsigned int) 0, // ast_rx_phyerr,
291  (unsigned int) 0, // ast_rx_rssi,
292  (unsigned int) 0 // rate
293  );
294 
295  if (m_writer)
296  {
297 
298  *m_writer << str;
299 
300  ResetCounters ();
302  }
303 }
304 
305 
306 
307 
308 } // namespace ns3
309 
310 
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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...
std::vector< Ptr< Node > >::const_iterator Iterator
State
The state of the PHY layer.
Definition: wifi-phy.h:123
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:824
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:91
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
Definition: abort.h:131
trace sink for wifi device that mimics madwifi's athstats tool.
NS_LOG_COMPONENT_DEFINE("Athstats")
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:29
hold objects of type ns3::Time
Definition: nstime.h:961
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
Definition: node.cc:132
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
uint32_t GetNDevices(void) const
Definition: node.cc:140
void Open(std::string const &name)
Open a file for output.
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:41
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
std::vector< Ptr< NetDevice > >::const_iterator Iterator
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
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:63
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...
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
static TypeId GetTypeId(void)