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/simulator.h"
23 #include "ns3/config.h"
24 #include "ns3/wifi-mode.h"
25 #include "ns3/wifi-phy-common.h"
26 #include "ns3/wifi-phy-state.h"
27 #include "ns3/net-device-container.h"
28 #include "ns3/node-container.h"
29 #include "athstats-helper.h"
30 #include <iomanip>
31 #include <fstream>
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/MacTxRtsFailed", 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 void
78 {
79  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
80  {
81  Ptr<NetDevice> dev = *i;
82  EnableAthstats (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
83  }
84 }
85 
86 void
88 {
89  NetDeviceContainer devs;
90  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
91  {
92  Ptr<Node> node = *i;
93  for (std::size_t j = 0; j < node->GetNDevices (); ++j)
94  {
95  devs.Add (node->GetDevice (j));
96  }
97  }
98  EnableAthstats (filename, devs);
99 }
100 
102 
103 TypeId
105 {
106  static TypeId tid = TypeId ("ns3::AthstatsWifiTraceSink")
107  .SetParent<Object> ()
108  .SetGroupName ("Wifi")
109  .AddConstructor<AthstatsWifiTraceSink> ()
110  .AddAttribute ("Interval",
111  "Time interval between reports",
112  TimeValue (Seconds (1.0)),
114  MakeTimeChecker ())
115  ;
116  return tid;
117 }
118 
120  : m_txCount (0),
121  m_rxCount (0),
122  m_shortRetryCount (0),
123  m_longRetryCount (0),
124  m_exceededRetryCount (0),
125  m_phyRxOkCount (0),
126  m_phyRxErrorCount (0),
127  m_phyTxCount (0),
128  m_writer (0)
129 {
131 }
132 
134 {
135  NS_LOG_FUNCTION (this);
136 
137  if (m_writer != 0)
138  {
139  NS_LOG_LOGIC ("m_writer nonzero " << m_writer);
140  if (m_writer->is_open ())
141  {
142  NS_LOG_LOGIC ("m_writer open. Closing " << m_writer);
143  m_writer->close ();
144  }
145 
146  NS_LOG_LOGIC ("Deleting writer " << m_writer);
147  delete m_writer;
148 
149  NS_LOG_LOGIC ("m_writer = 0");
150  m_writer = 0;
151  }
152  else
153  {
154  NS_LOG_LOGIC ("m_writer == 0");
155  }
156 }
157 
158 void
160 {
161  m_txCount = 0;
162  m_rxCount = 0;
163  m_shortRetryCount = 0;
164  m_longRetryCount = 0;
166  m_phyRxOkCount = 0;
167  m_phyRxErrorCount = 0;
168  m_phyTxCount = 0;
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this << context << p);
175  ++m_txCount;
176 }
177 
178 void
180 {
181  NS_LOG_FUNCTION (this << context << p);
182  ++m_rxCount;
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION (this << context << address);
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << context << address);
197 }
198 
199 void
201 {
202  NS_LOG_FUNCTION (this << context << address);
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION (this << context << address);
211 }
212 
213 void
214 AthstatsWifiTraceSink::PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, WifiPreamble preamble)
215 {
216  NS_LOG_FUNCTION (this << context << packet << " mode=" << mode << " snr=" << snr << "preamble=" << preamble);
217  ++m_phyRxOkCount;
218 }
219 
220 void
221 AthstatsWifiTraceSink::PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
222 {
223  NS_LOG_FUNCTION (this << context << packet << " snr=" << snr );
225 }
226 
227 void
228 AthstatsWifiTraceSink::PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
229 {
230  NS_LOG_FUNCTION (this << context << packet << "PHYTX mode=" << mode << "Preamble=" << preamble << "Power=" << txPower);
231  ++m_phyTxCount;
232 }
233 
234 void
235 AthstatsWifiTraceSink::PhyStateTrace (std::string context, Time start, Time duration, WifiPhyState state)
236 {
237  NS_LOG_FUNCTION (this << context << start << duration << state);
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 subtract management frames
268  (unsigned int) m_rxCount, // /proc/net/dev received packets but subtracts management 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
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
Enable athstats.
trace sink for wifi device that mimics madwifi's athstats tool.
void TxFinalRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a RTS frame has exceeded the retry limit.
void TxFinalDataFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a data frame has exceeded the retry limit.
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
Function to be called when the PHY layer of the considered device receives a frame.
uint32_t m_txCount
transmit count
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
std::ofstream * m_writer
output stream
uint32_t m_rxCount
receive count
uint32_t m_shortRetryCount
short retry count
void WriteStats()
Write status function.
void TxRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when a RTS frame transmission by the considered device has failed.
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
Function to be called when the PHY layer of the considered device changes state.
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...
static TypeId GetTypeId(void)
Get the type ID.
void DevTxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device transmits a packet
uint32_t m_longRetryCount
long retry count
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.
uint32_t m_phyTxCount
PHY transmit count.
void TxDataFailedTrace(std::string context, Mac48Address address)
Function to be called when a data frame transmission by the considered device has failed.
uint32_t m_phyRxOkCount
PHY receive OK count.
uint32_t m_phyRxErrorCount
PHY receive error count.
void ResetCounters()
Reset counters function.
void Open(std::string const &name)
Open a file for output.
uint32_t m_exceededRetryCount
exceeded retry count
an EUI-48 address
Definition: mac48-address.h:44
holds a vector of ns3::NetDevice pointers
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
virtual Ptr< Node > GetNode(void) const =0
virtual uint32_t GetIfIndex(void) const =0
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t GetNDevices(void) const
Definition: node.cc:152
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
A base class which provides memory management and object aggregation.
Definition: object.h:88
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
represent a single transmission mode
Definition: wifi-mode.h:48
#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:88
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:1309
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:522
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
def start()
Definition: core.py:1853
WifiPhyState
The state of the PHY layer.