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 "athstats-helper.h"
25 #include <iomanip>
26 #include <fstream>
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("Athstats");
31 
33  : m_interval (Seconds (1.0))
34 {
35 }
36 
37 void
38 AthstatsHelper::EnableAthstats (std::string filename, uint32_t nodeid, uint32_t deviceid)
39 {
40  Ptr<AthstatsWifiTraceSink> athstats = CreateObject<AthstatsWifiTraceSink> ();
41  std::ostringstream oss;
42  oss << filename
43  << "_" << std::setfill ('0') << std::setw (3) << std::right << nodeid
44  << "_" << std::setfill ('0') << std::setw (3) << std::right << deviceid;
45  athstats->Open (oss.str ());
46 
47  oss.str ("");
48  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
49  std::string devicepath = oss.str ();
50 
51  Config::Connect (devicepath + "/Mac/MacTx", MakeCallback (&AthstatsWifiTraceSink::DevTxTrace, athstats));
52  Config::Connect (devicepath + "/Mac/MacRx", MakeCallback (&AthstatsWifiTraceSink::DevRxTrace, athstats));
53 
54  Config::Connect (devicepath + "/RemoteStationManager/TxRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxRtsFailedTrace, athstats));
55  Config::Connect (devicepath + "/RemoteStationManager/MacTxDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxDataFailedTrace, athstats));
56  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalRtsFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalRtsFailedTrace, athstats));
57  Config::Connect (devicepath + "/RemoteStationManager/MacTxFinalDataFailed", MakeCallback (&AthstatsWifiTraceSink::TxFinalDataFailedTrace, athstats));
58 
59  Config::Connect (devicepath + "/Phy/State/RxOk", MakeCallback (&AthstatsWifiTraceSink::PhyRxOkTrace, athstats));
60  Config::Connect (devicepath + "/Phy/State/RxError", MakeCallback (&AthstatsWifiTraceSink::PhyRxErrorTrace, athstats));
61  Config::Connect (devicepath + "/Phy/State/Tx", MakeCallback (&AthstatsWifiTraceSink::PhyTxTrace, athstats));
62  Config::Connect (devicepath + "/Phy/State/State", MakeCallback (&AthstatsWifiTraceSink::PhyStateTrace, athstats));
63 }
64 
65 void
67 {
68  EnableAthstats (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
69 }
70 
71 void
73 {
74  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
75  {
76  Ptr<NetDevice> dev = *i;
77  EnableAthstats (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
78  }
79 }
80 
81 void
83 {
84  NetDeviceContainer devs;
85  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
86  {
87  Ptr<Node> node = *i;
88  for (uint32_t j = 0; j < node->GetNDevices (); ++j)
89  {
90  devs.Add (node->GetDevice (j));
91  }
92  }
93  EnableAthstats (filename, devs);
94 }
95 
97 
98 TypeId
100 {
101  static TypeId tid = TypeId ("ns3::AthstatsWifiTraceSink")
102  .SetParent<Object> ()
103  .SetGroupName ("Wifi")
104  .AddConstructor<AthstatsWifiTraceSink> ()
105  .AddAttribute ("Interval",
106  "Time interval between reports",
107  TimeValue (Seconds (1.0)),
109  MakeTimeChecker ())
110  ;
111  return tid;
112 }
113 
115  : m_txCount (0),
116  m_rxCount (0),
117  m_shortRetryCount (0),
118  m_longRetryCount (0),
119  m_exceededRetryCount (0),
120  m_phyRxOkCount (0),
121  m_phyRxErrorCount (0),
122  m_phyTxCount (0),
123  m_writer (0)
124 {
126 }
127 
129 {
130  NS_LOG_FUNCTION (this);
131 
132  if (m_writer != 0)
133  {
134  NS_LOG_LOGIC ("m_writer nonzero " << m_writer);
135  if (m_writer->is_open ())
136  {
137  NS_LOG_LOGIC ("m_writer open. Closing " << m_writer);
138  m_writer->close ();
139  }
140 
141  NS_LOG_LOGIC ("Deleting writer " << m_writer);
142  delete m_writer;
143 
144  NS_LOG_LOGIC ("m_writer = 0");
145  m_writer = 0;
146  }
147  else
148  {
149  NS_LOG_LOGIC ("m_writer == 0");
150  }
151 }
152 
153 void
155 {
156  m_txCount = 0;
157  m_rxCount = 0;
158  m_shortRetryCount = 0;
159  m_longRetryCount = 0;
161  m_phyRxOkCount = 0;
162  m_phyRxErrorCount = 0;
163  m_phyTxCount = 0;
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION (this << context << p);
170  ++m_txCount;
171 }
172 
173 void
175 {
176  NS_LOG_FUNCTION (this << context << p);
177  ++m_rxCount;
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this << context << address);
185 }
186 
187 void
189 {
190  NS_LOG_FUNCTION (this << context << address);
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this << context << address);
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION (this << context << address);
206 }
207 
208 void
209 AthstatsWifiTraceSink::PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
210 {
211  NS_LOG_FUNCTION (this << context << packet << " mode=" << mode << " snr=" << snr );
212  ++m_phyRxOkCount;
213 }
214 
215 void
216 AthstatsWifiTraceSink::PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
217 {
218  NS_LOG_FUNCTION (this << context << packet << " snr=" << snr );
220 }
221 
222 void
223 AthstatsWifiTraceSink::PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
224 {
225  NS_LOG_FUNCTION (this << context << packet << "PHYTX mode=" << mode );
226  ++m_phyTxCount;
227 }
228 
229 void
230 AthstatsWifiTraceSink::PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
231 {
232  NS_LOG_FUNCTION (this << context << start << duration << state);
233 
234 }
235 
236 void
237 AthstatsWifiTraceSink::Open (std::string const &name)
238 {
239  NS_LOG_FUNCTION (this << name);
240  NS_ABORT_MSG_UNLESS (m_writer == 0, "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
241 
242  m_writer = new std::ofstream ();
243  NS_ABORT_MSG_UNLESS (m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
244 
245  NS_LOG_LOGIC ("Created writer " << m_writer);
246 
247  m_writer->open (name.c_str (), std::ios_base::binary | std::ios_base::out);
248  NS_ABORT_MSG_IF (m_writer->fail (), "AthstatsWifiTraceSink::Open (): m_writer->open (" << name.c_str () << ") failed");
249 
250  NS_ASSERT_MSG (m_writer->is_open (), "AthstatsWifiTraceSink::Open (): m_writer not open");
251 
252  NS_LOG_LOGIC ("Writer opened successfully");
253 }
254 
255 void
257 {
258  NS_LOG_FUNCTION (this);
259  //The comments below refer to how each value maps to madwifi's athstats
260  //I know C strings are ugly but that's the quickest way to use exactly the same format as in madwifi
261  char str[200];
262  snprintf (str, 200, "%8u %8u %7u %7u %7u %6u %6u %6u %7u %4u %3uM\n",
263  (unsigned int) m_txCount, // /proc/net/dev transmitted packets to which we should subract mgmt frames
264  (unsigned int) m_rxCount, // /proc/net/dev received packets but subracts mgmt frames from it
265  (unsigned int) 0, // ast_tx_altrate
266  (unsigned int) m_shortRetryCount, // ast_tx_shortretry
267  (unsigned int) m_longRetryCount, // ast_tx_longretry
268  (unsigned int) m_exceededRetryCount, // ast_tx_xretries
269  (unsigned int) m_phyRxErrorCount, // ast_rx_crcerr
270  (unsigned int) 0, // ast_rx_badcrypt
271  (unsigned int) 0, // ast_rx_phyerr
272  (unsigned int) 0, // ast_rx_rssi
273  (unsigned int) 0 // rate
274  );
275 
276  if (m_writer)
277  {
278 
279  *m_writer << str;
280 
281  ResetCounters ();
283  }
284 }
285 
286 } //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
uint32_t m_rxCount
receive count
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:45
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
def start()
Definition: core.py:1790
uint32_t m_phyRxOkCount
phy receive ok count
#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.
uint32_t m_shortRetryCount
short retry count
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:97
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
std::ofstream * m_writer
output stream
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
AttributeValue implementation for Time.
Definition: nstime.h:1055
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
uint32_t m_txCount
transmit count
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
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
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.
uint32_t m_exceededRetryCount
exceeded 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...
an EUI-48 address
Definition: mac48-address.h:43
uint32_t m_longRetryCount
long retry count
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1564
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:1056
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
Enable athstats.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t m_phyRxErrorCount
phy receive error count
#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
uint32_t m_phyTxCount
phy transmit count
#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
void WriteStats()
Write status function.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
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...
void ResetCounters()
Reset counters function.
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:171
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
static TypeId GetTypeId(void)
Get the type ID.