A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
athstats-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include "athstats-helper.h"
10
11#include "ns3/config.h"
12#include "ns3/log.h"
13#include "ns3/net-device-container.h"
14#include "ns3/node-container.h"
15#include "ns3/simulator.h"
16#include "ns3/wifi-mode.h"
17#include "ns3/wifi-phy-common.h"
18#include "ns3/wifi-phy-state.h"
19
20#include <fstream>
21#include <iomanip>
22#include <sstream>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("Athstats");
28
30 : m_interval(Seconds(1.0))
31{
32}
33
34void
35AthstatsHelper::EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
36{
38 std::ostringstream oss;
39 oss << filename << "_" << std::setfill('0') << std::setw(3) << std::right << nodeid << "_"
40 << std::setfill('0') << std::setw(3) << std::right << deviceid;
41 athstats->Open(oss.str());
42
43 oss.str("");
44 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
45 std::string devicepath = oss.str();
46
47 Config::Connect(devicepath + "/Mac/MacTx",
49 Config::Connect(devicepath + "/Mac/MacRx",
51
52 Config::Connect(devicepath + "/RemoteStationManager/MacTxRtsFailed",
54 Config::Connect(devicepath + "/RemoteStationManager/MacTxDataFailed",
56 Config::Connect(devicepath + "/RemoteStationManager/MacTxFinalRtsFailed",
58 Config::Connect(devicepath + "/RemoteStationManager/MacTxFinalDataFailed",
60
61 Config::Connect(devicepath + "/Phy/State/RxOk",
63 Config::Connect(devicepath + "/Phy/State/RxError",
65 Config::Connect(devicepath + "/Phy/State/Tx",
67 Config::Connect(devicepath + "/Phy/State/State",
69}
70
71void
73{
74 EnableAthstats(filename, nd->GetNode()->GetId(), nd->GetIfIndex());
75}
76
77void
79{
80 for (auto i = d.Begin(); i != d.End(); ++i)
81 {
82 Ptr<NetDevice> dev = *i;
83 EnableAthstats(filename, dev->GetNode()->GetId(), dev->GetIfIndex());
84 }
85}
86
87void
89{
91 for (auto i = n.Begin(); i != n.End(); ++i)
92 {
93 Ptr<Node> node = *i;
94 for (std::size_t j = 0; j < node->GetNDevices(); ++j)
95 {
96 devs.Add(node->GetDevice(j));
97 }
98 }
99 EnableAthstats(filename, devs);
100}
101
103
104TypeId
106{
107 static TypeId tid = TypeId("ns3::AthstatsWifiTraceSink")
108 .SetParent<Object>()
109 .SetGroupName("Wifi")
110 .AddConstructor<AthstatsWifiTraceSink>()
111 .AddAttribute("Interval",
112 "Time interval between reports",
113 TimeValue(Seconds(1.0)),
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(nullptr)
129{
131}
132
134{
135 NS_LOG_FUNCTION(this);
136
137 if (m_writer != nullptr)
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 = nullptr;
151 }
152 else
153 {
154 NS_LOG_LOGIC("m_writer == 0");
155 }
156}
157
158void
170
171void
173{
174 NS_LOG_FUNCTION(this << context << p);
175 ++m_txCount;
176}
177
178void
180{
181 NS_LOG_FUNCTION(this << context << p);
182 ++m_rxCount;
183}
184
185void
187{
188 NS_LOG_FUNCTION(this << context << address);
190}
191
192void
194{
195 NS_LOG_FUNCTION(this << context << address);
197}
198
199void
201{
202 NS_LOG_FUNCTION(this << context << address);
204}
205
206void
208{
209 NS_LOG_FUNCTION(this << context << address);
211}
212
213void
215 Ptr<const Packet> packet,
216 double snr,
217 WifiMode mode,
218 WifiPreamble preamble)
219{
220 NS_LOG_FUNCTION(this << context << packet << " mode=" << mode << " snr=" << snr
221 << "preamble=" << preamble);
223}
224
225void
226AthstatsWifiTraceSink::PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
227{
228 NS_LOG_FUNCTION(this << context << packet << " snr=" << snr);
230}
231
232void
234 Ptr<const Packet> packet,
235 WifiMode mode,
236 WifiPreamble preamble,
237 uint8_t txPower)
238{
239 NS_LOG_FUNCTION(this << context << packet << "PHYTX mode=" << mode << "Preamble=" << preamble
240 << "Power=" << txPower);
241 ++m_phyTxCount;
242}
243
244void
246 Time start,
247 Time duration,
248 WifiPhyState state)
249{
250 NS_LOG_FUNCTION(this << context << start << duration << state);
251}
252
253void
254AthstatsWifiTraceSink::Open(const std::string& name)
255{
256 NS_LOG_FUNCTION(this << name);
258 m_writer == nullptr,
259 "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
260
261 m_writer = new std::ofstream();
262 NS_ABORT_MSG_UNLESS(m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
263
264 NS_LOG_LOGIC("Created writer " << m_writer);
265
266 m_writer->open(name, std::ios_base::binary | std::ios_base::out);
268 "AthstatsWifiTraceSink::Open (): m_writer->open (" << name << ") failed");
269
270 NS_ASSERT_MSG(m_writer->is_open(), "AthstatsWifiTraceSink::Open (): m_writer not open");
271
272 NS_LOG_LOGIC("Writer opened successfully");
273}
274
275void
277{
278 NS_LOG_FUNCTION(this);
279
280 if (!m_writer)
281 {
282 return;
283 }
284
285 // The comments below refer to how each value maps to madwifi's athstats.
286 // Format: "%8lu %8lu %7u %7u %7u %6u %6u %6u %7u %4u %3uM"
287 std::stringstream ss;
288
289 // /proc/net/dev transmitted packets to which we should subtract management frames
290 ss << std::setw(8) << m_txCount << " ";
291
292 // /proc/net/dev received packets but subtracts management frames from it
293 ss << std::setw(8) << m_rxCount << " ";
294
295 ss << std::setw(7) << 0 << " "; // ast_tx_altrate
296 ss << std::setw(7) << m_shortRetryCount << " "; // ast_tx_shortretry
297 ss << std::setw(7) << m_longRetryCount << " "; // ast_tx_longretry
298 ss << std::setw(6) << m_exceededRetryCount << " "; // ast_tx_xretries
299 ss << std::setw(6) << m_phyRxErrorCount << " "; // ast_rx_crcerr
300 ss << std::setw(6) << 0 << " "; // ast_rx_badcrypt
301 ss << std::setw(7) << 0 << " "; // ast_rx_phyerr
302 ss << std::setw(4) << 0 << " "; // ast_rx_rssi
303 ss << std::setw(3) << 0 << "M"; // rate
304
305 *m_writer << ss.str() << std::endl;
306
309}
310
311} // 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 Open(const std::string &name)
Open a file for output.
static TypeId GetTypeId()
Get the type ID.
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...
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.
uint32_t m_exceededRetryCount
exceeded retry count
an EUI-48 address
holds a vector of ns3::NetDevice pointers
Iterator Begin() 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.
Iterator End() const
Get an iterator which indicates past-the-last NetDevice in the container.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
represent a single transmission mode
Definition wifi-mode.h:40
#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:75
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition abort.h:133
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
WifiPhyState
The state of the PHY layer.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416