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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include "athstats-helper.h"
21
22#include "ns3/config.h"
23#include "ns3/log.h"
24#include "ns3/net-device-container.h"
25#include "ns3/node-container.h"
26#include "ns3/simulator.h"
27#include "ns3/wifi-mode.h"
28#include "ns3/wifi-phy-common.h"
29#include "ns3/wifi-phy-state.h"
30
31#include <fstream>
32#include <iomanip>
33#include <sstream>
34
35namespace ns3
36{
37
38NS_LOG_COMPONENT_DEFINE("Athstats");
39
41 : m_interval(Seconds(1.0))
42{
43}
44
45void
46AthstatsHelper::EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
47{
48 Ptr<AthstatsWifiTraceSink> athstats = CreateObject<AthstatsWifiTraceSink>();
49 std::ostringstream oss;
50 oss << filename << "_" << std::setfill('0') << std::setw(3) << std::right << nodeid << "_"
51 << std::setfill('0') << std::setw(3) << std::right << deviceid;
52 athstats->Open(oss.str());
53
54 oss.str("");
55 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
56 std::string devicepath = oss.str();
57
58 Config::Connect(devicepath + "/Mac/MacTx",
60 Config::Connect(devicepath + "/Mac/MacRx",
62
63 Config::Connect(devicepath + "/RemoteStationManager/MacTxRtsFailed",
65 Config::Connect(devicepath + "/RemoteStationManager/MacTxDataFailed",
67 Config::Connect(devicepath + "/RemoteStationManager/MacTxFinalRtsFailed",
69 Config::Connect(devicepath + "/RemoteStationManager/MacTxFinalDataFailed",
71
72 Config::Connect(devicepath + "/Phy/State/RxOk",
74 Config::Connect(devicepath + "/Phy/State/RxError",
76 Config::Connect(devicepath + "/Phy/State/Tx",
78 Config::Connect(devicepath + "/Phy/State/State",
80}
81
82void
84{
85 EnableAthstats(filename, nd->GetNode()->GetId(), nd->GetIfIndex());
86}
87
88void
90{
91 for (auto i = d.Begin(); i != d.End(); ++i)
92 {
93 Ptr<NetDevice> dev = *i;
94 EnableAthstats(filename, dev->GetNode()->GetId(), dev->GetIfIndex());
95 }
96}
97
98void
100{
102 for (auto i = n.Begin(); i != n.End(); ++i)
103 {
104 Ptr<Node> node = *i;
105 for (std::size_t j = 0; j < node->GetNDevices(); ++j)
106 {
107 devs.Add(node->GetDevice(j));
108 }
109 }
110 EnableAthstats(filename, devs);
111}
112
114
115TypeId
117{
118 static TypeId tid = TypeId("ns3::AthstatsWifiTraceSink")
119 .SetParent<Object>()
120 .SetGroupName("Wifi")
121 .AddConstructor<AthstatsWifiTraceSink>()
122 .AddAttribute("Interval",
123 "Time interval between reports",
124 TimeValue(Seconds(1.0)),
127 return tid;
128}
129
131 : m_txCount(0),
132 m_rxCount(0),
133 m_shortRetryCount(0),
134 m_longRetryCount(0),
135 m_exceededRetryCount(0),
136 m_phyRxOkCount(0),
137 m_phyRxErrorCount(0),
138 m_phyTxCount(0),
139 m_writer(nullptr)
140{
142}
143
145{
146 NS_LOG_FUNCTION(this);
147
148 if (m_writer != nullptr)
149 {
150 NS_LOG_LOGIC("m_writer nonzero " << m_writer);
151 if (m_writer->is_open())
152 {
153 NS_LOG_LOGIC("m_writer open. Closing " << m_writer);
154 m_writer->close();
155 }
156
157 NS_LOG_LOGIC("Deleting writer " << m_writer);
158 delete m_writer;
159
160 NS_LOG_LOGIC("m_writer = 0");
161 m_writer = nullptr;
162 }
163 else
164 {
165 NS_LOG_LOGIC("m_writer == 0");
166 }
167}
168
169void
171{
172 m_txCount = 0;
173 m_rxCount = 0;
177 m_phyRxOkCount = 0;
179 m_phyTxCount = 0;
180}
181
182void
184{
185 NS_LOG_FUNCTION(this << context << p);
186 ++m_txCount;
187}
188
189void
191{
192 NS_LOG_FUNCTION(this << context << p);
193 ++m_rxCount;
194}
195
196void
198{
199 NS_LOG_FUNCTION(this << context << address);
201}
202
203void
205{
206 NS_LOG_FUNCTION(this << context << address);
208}
209
210void
212{
213 NS_LOG_FUNCTION(this << context << address);
215}
216
217void
219{
220 NS_LOG_FUNCTION(this << context << address);
222}
223
224void
226 Ptr<const Packet> packet,
227 double snr,
228 WifiMode mode,
229 WifiPreamble preamble)
230{
231 NS_LOG_FUNCTION(this << context << packet << " mode=" << mode << " snr=" << snr
232 << "preamble=" << preamble);
234}
235
236void
237AthstatsWifiTraceSink::PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
238{
239 NS_LOG_FUNCTION(this << context << packet << " snr=" << snr);
241}
242
243void
245 Ptr<const Packet> packet,
246 WifiMode mode,
247 WifiPreamble preamble,
248 uint8_t txPower)
249{
250 NS_LOG_FUNCTION(this << context << packet << "PHYTX mode=" << mode << "Preamble=" << preamble
251 << "Power=" << txPower);
252 ++m_phyTxCount;
253}
254
255void
257 Time start,
258 Time duration,
259 WifiPhyState state)
260{
261 NS_LOG_FUNCTION(this << context << start << duration << state);
262}
263
264void
265AthstatsWifiTraceSink::Open(const std::string& name)
266{
267 NS_LOG_FUNCTION(this << name);
269 m_writer == nullptr,
270 "AthstatsWifiTraceSink::Open (): m_writer already allocated (std::ofstream leak detected)");
271
272 m_writer = new std::ofstream();
273 NS_ABORT_MSG_UNLESS(m_writer, "AthstatsWifiTraceSink::Open (): Cannot allocate m_writer");
274
275 NS_LOG_LOGIC("Created writer " << m_writer);
276
277 m_writer->open(name, std::ios_base::binary | std::ios_base::out);
279 "AthstatsWifiTraceSink::Open (): m_writer->open (" << name << ") failed");
280
281 NS_ASSERT_MSG(m_writer->is_open(), "AthstatsWifiTraceSink::Open (): m_writer not open");
282
283 NS_LOG_LOGIC("Writer opened successfully");
284}
285
286void
288{
289 NS_LOG_FUNCTION(this);
290
291 if (!m_writer)
292 {
293 return;
294 }
295
296 // The comments below refer to how each value maps to madwifi's athstats.
297 // Format: "%8lu %8lu %7u %7u %7u %6u %6u %6u %7u %4u %3uM"
298 std::stringstream ss;
299
300 // /proc/net/dev transmitted packets to which we should subtract management frames
301 ss << std::setw(8) << m_txCount << " ";
302
303 // /proc/net/dev received packets but subtracts management frames from it
304 ss << std::setw(8) << m_rxCount << " ";
305
306 ss << std::setw(7) << 0 << " "; // ast_tx_altrate
307 ss << std::setw(7) << m_shortRetryCount << " "; // ast_tx_shortretry
308 ss << std::setw(7) << m_longRetryCount << " "; // ast_tx_longretry
309 ss << std::setw(6) << m_exceededRetryCount << " "; // ast_tx_xretries
310 ss << std::setw(6) << m_phyRxErrorCount << " "; // ast_rx_crcerr
311 ss << std::setw(6) << 0 << " "; // ast_rx_badcrypt
312 ss << std::setw(7) << 0 << " "; // ast_rx_phyerr
313 ss << std::setw(4) << 0 << " "; // ast_rx_rssi
314 ss << std::setw(3) << 0 << "M"; // rate
315
316 *m_writer << ss.str() << std::endl;
317
320}
321
322} // 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
Definition: mac48-address.h:46
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:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
represent a single transmission mode
Definition: wifi-mode.h:51
#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:86
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#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:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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.
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:704