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
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::AthstatsWifiTraceSink::m_rxCount
uint32_t m_rxCount
receive count
Definition: athstats-helper.h:221
ns3::AthstatsWifiTraceSink
trace sink for wifi device that mimics madwifi's athstats tool.
Definition: athstats-helper.h:98
ns3::AthstatsWifiTraceSink::TxRtsFailedTrace
void TxRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when a RTS frame transmission by the considered device has failed.
Definition: athstats-helper.cc:186
ns3::AthstatsWifiTraceSink::TxDataFailedTrace
void TxDataFailedTrace(std::string context, Mac48Address address)
Function to be called when a data frame transmission by the considered device has failed.
Definition: athstats-helper.cc:193
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::AthstatsHelper::EnableAthstats
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
Enable athstats.
Definition: athstats-helper.cc:43
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::AthstatsWifiTraceSink::PhyTxTrace
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.
Definition: athstats-helper.cc:228
ns3::Node::GetId
uint32_t GetId(void) const
Definition: node.cc:109
ns3::AthstatsWifiTraceSink::m_phyRxErrorCount
uint32_t m_phyRxErrorCount
PHY receive error count.
Definition: athstats-helper.h:226
ns3::AthstatsWifiTraceSink::AthstatsWifiTraceSink
AthstatsWifiTraceSink()
Definition: athstats-helper.cc:119
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
athstats-helper.h
ns3::AthstatsWifiTraceSink::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: athstats-helper.cc:104
ns3::Mac48Address
an EUI-48 address
Definition: mac48-address.h:44
ns3::AthstatsWifiTraceSink::WriteStats
void WriteStats()
Write status function.
Definition: athstats-helper.cc:260
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::AthstatsWifiTraceSink::m_shortRetryCount
uint32_t m_shortRetryCount
short retry count
Definition: athstats-helper.h:222
ns3::AthstatsWifiTraceSink::m_longRetryCount
uint32_t m_longRetryCount
long retry count
Definition: athstats-helper.h:223
ns3::AthstatsWifiTraceSink::DevRxTrace
void DevRxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device receives a packet
Definition: athstats-helper.cc:179
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
visualizer.core.start
def start()
Definition: core.py:1855
ns3::NetDeviceContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
Definition: net-device-container.cc:46
ns3::WifiMode
represent a single transmission mode
Definition: wifi-mode.h:48
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
ns3::AthstatsWifiTraceSink::m_interval
Time m_interval
interval
Definition: athstats-helper.h:231
ns3::NetDevice::GetNode
virtual Ptr< Node > GetNode(void) const =0
ns3::AthstatsWifiTraceSink::m_txCount
uint32_t m_txCount
transmit count
Definition: athstats-helper.h:220
ns3::Node::GetDevice
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
ns3::AthstatsWifiTraceSink::TxFinalRtsFailedTrace
void TxFinalRtsFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a RTS frame has exceeded the retry limit.
Definition: athstats-helper.cc:200
first.address
address
Definition: first.py:44
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
NS_ASSERT_MSG
#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
ns3::NetDeviceContainer::End
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Definition: net-device-container.cc:51
ns3::MakeCallback
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:1642
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
WifiPhyState
WifiPhyState
The state of the PHY layer.
Definition: wifi-phy-state.h:32
ns3::WifiPreamble
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-phy-common.h:68
ns3::NetDeviceContainer::Iterator
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Definition: net-device-container.h:45
NS_ABORT_MSG_UNLESS
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
ns3::AthstatsWifiTraceSink::PhyStateTrace
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.
Definition: athstats-helper.cc:235
ns3::NodeContainer::Iterator
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-container.h:42
ns3::Config::Connect
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
ns3::AthstatsWifiTraceSink::m_exceededRetryCount
uint32_t m_exceededRetryCount
exceeded retry count
Definition: athstats-helper.h:224
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::AthstatsHelper::AthstatsHelper
AthstatsHelper()
Definition: athstats-helper.cc:37
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::AthstatsWifiTraceSink::PhyRxErrorTrace
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...
Definition: athstats-helper.cc:221
ns3::AthstatsWifiTraceSink::m_writer
std::ofstream * m_writer
output stream
Definition: athstats-helper.h:229
ns3::AthstatsWifiTraceSink::TxFinalDataFailedTrace
void TxFinalDataFailedTrace(std::string context, Mac48Address address)
Function to be called when the transmission of a data frame has exceeded the retry limit.
Definition: athstats-helper.cc:207
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::AthstatsWifiTraceSink::~AthstatsWifiTraceSink
virtual ~AthstatsWifiTraceSink()
Definition: athstats-helper.cc:133
ns3::AthstatsWifiTraceSink::PhyRxOkTrace
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.
Definition: athstats-helper.cc:214
ns3::AthstatsWifiTraceSink::ResetCounters
void ResetCounters()
Reset counters function.
Definition: athstats-helper.cc:159
ns3::Node::GetNDevices
uint32_t GetNDevices(void) const
Definition: node.cc:152
ns3::AthstatsWifiTraceSink::Open
void Open(std::string const &name)
Open a file for output.
Definition: athstats-helper.cc:241
ns3::AthstatsWifiTraceSink::DevTxTrace
void DevTxTrace(std::string context, Ptr< const Packet > p)
function to be called when the net device transmits a packet
Definition: athstats-helper.cc:172
ns3::NetDevice::GetIfIndex
virtual uint32_t GetIfIndex(void) const =0
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
ns3::AthstatsWifiTraceSink::m_phyTxCount
uint32_t m_phyTxCount
PHY transmit count.
Definition: athstats-helper.h:227
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
ns3::MakeTimeAccessor
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:1354
ns3::AthstatsWifiTraceSink::m_phyRxOkCount
uint32_t m_phyRxOkCount
PHY receive OK count.
Definition: athstats-helper.h:225