A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-co-trace-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Indraprastha Institute of Information Technology Delhi
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#ifndef WIFI_CO_HELPER_H
8#define WIFI_CO_HELPER_H
9
10#include "ns3/callback.h"
11#include "ns3/config.h"
12#include "ns3/nstime.h"
13#include "ns3/ptr.h"
14#include "ns3/simulator.h"
15#include "ns3/wifi-phy-state.h"
16
17#include <iostream>
18#include <map>
19#include <string>
20#include <vector>
21
22namespace ns3
23{
24
25class WifiNetDevice;
26class NodeContainer;
27class NetDeviceContainer;
28
29/**
30 * @class WifiCoTraceHelper
31 * @brief Track channel occupancy durations for WifiNetDevice
32 *
33 * The WifiCoTraceHelper class tracks the duration that a particular WifiNetDevice is in
34 * different states. The states are defined by the ns-3 WifiPhyStateHelper and include states such
35 * as IDLE, CCA_BUSY, TX, and RX. The helper tracks these durations between a user-configured start
36 * and end time. At the end of a simulation, this helper can print out statistics on channel
37 * occupancy, and permits the export of an internal data structure to allow for custom printing or
38 * statistics handling.
39 *
40 * This helper supports both single-link devices and multi-link devices (MLD).
41 */
43{
44 public:
45 /**
46 * @struct DeviceRecord
47 * @brief Keeps track of channel occupancy statistics observed at a WifiNetDevice.
48 *
49 * Data structure to track durations of each WifiPhy state. Elements in m_linkStateDurations are
50 * indexed by "linkId".
51 */
53 {
54 uint32_t m_nodeId; ///< Id of Node on which the WifiNetDevice is installed.
55 std::string m_nodeName; ///< Name of Node on which the WifiNetDevice is installed. It's
56 ///< empty if the name isn't configured.
57 uint32_t m_ifIndex; ///< Device Id of WifiNetDevice.
58 std::string m_deviceName; ///< Device name. It's empty if the name isn't configured.
59
60 /**
61 * Duration statistics by link and state. LinkId is the key in the
62 * first map, and the WifiPhyState is the key to the second.
63 */
64 std::map<uint8_t, std::map<WifiPhyState, Time>> m_linkStateDurations;
65
66 /**
67 * Constructor.
68 *
69 * @param device The WifiNetDevice whose links will be monitored to collect statistics.
70 */
72
73 /**
74 * Update the duration statistics for the provided linkId and state
75 *
76 * @param linkId Id of Link whose statistics is updated.
77 * @param start Time at which link switched its WifiPhyState to state.
78 * @param duration Duration of time that the link stayed in this state.
79 * @param state The state of the link from start to (start + duration).
80 */
81 void AddLinkMeasurement(size_t linkId, Time start, Time duration, WifiPhyState state);
82 };
83
84 /**
85 * Default Constructor. StartTime is Seconds(0) and stopTime is Time::Max()
86 */
88
89 /**
90 * Construct a helper object measuring between two simulation time points [startTime, stopTime]
91 *
92 * @param startTime The measurement start time
93 * @param stopTime The measurement stop time
94 */
96
97 /**
98 * Enables trace collection for all nodes and WifiNetDevices in the specified NodeContainer.
99 * @param nodes The NodeContainer containing WifiNetDevices to which traces are to be connected.
100 */
102 /**
103 * Enables trace collection for all nodes corresponding to the devices in the specified
104 * NetDeviceContainer.
105 * @param devices The NetDeviceContainer to which traces are to be connected.
106 */
107 void Enable(NetDeviceContainer devices);
108
109 /**
110 * Starts the collection of statistics from a specified start time.
111 * @param startTime The time to start collecting statistics.
112 */
113 void Start(Time startTime);
114
115 /**
116 * Stops the collection of statistics at a specified time.
117 * @param stopTime The time to stop collecting statistics.
118 */
119 void Stop(Time stopTime);
120
121 /**
122 * Resets the current statistics, clearing all links and their durations. It does not disconnect
123 * traced callbacks. It does not clear DeviceRecords. Only the statistics collected prior to
124 * invoking this method is cleared.
125 */
126 void Reset();
127
128 /**
129 * Print measurement results on an output stream
130 *
131 * @param os The output stream to print to
132 * @param unit The unit of time in which the durations should be printed. Defaults to AUTO.
133 */
134 void PrintStatistics(std::ostream& os, Time::Unit unit = Time::Unit::AUTO) const;
135
136 /**
137 * Returns measurement results on each installed device.
138 *
139 * @return Statistics of each device traced by this helper.
140 */
141 const std::vector<DeviceRecord>& GetDeviceRecords() const;
142
143 private:
144 uint32_t m_numDevices{0}; ///< Count the number of devices traced by this helper.
145 Time m_startTime; ///< Instant at which statistics collection should start.
146 Time m_stopTime{Time::Max()}; ///< Instant at which statistics collection should stop.
147
148 std::vector<DeviceRecord> m_deviceRecords; ///< Stores the collected statistics.
149
150 /**
151 * A callback used to update statistics.
152 *
153 * @param idx Index of the device in m_deviceRecords vector.
154 * @param phyId Id of Phy whose state is switched.
155 * @param start Instant at which link switched its WifiPhyState to state.
156 * @param duration Duration of time the link stays in this state.
157 * @param state The state of the link from start to (start + duration).
158 */
159 void NotifyWifiPhyState(std::size_t idx,
160 std::size_t phyId,
161 Time start,
162 Time duration,
163 WifiPhyState state);
164
165 /**
166 * Compute overlapping time-duration between two intervals. It is a helper function used by
167 * NotifyWifiPhyState function.
168 *
169 * @param start1 Beginning of first interval
170 * @param stop1 End of first interval
171 * @param start2 Beginning of second interval
172 * @param stop2 End of second interval
173 * @return Return the time duration common between the two intervals.
174 */
175 Time ComputeOverlappingDuration(Time start1, Time stop1, Time start2, Time stop2);
176 /**
177 * A helper function used by PrintStatistics method. It converts absolute statistics to
178 * percentages.
179 *
180 * @param linkStates Statistics of a link.
181 * @return Statistics in percentages.
182 */
183 std::map<WifiPhyState, double> ComputePercentage(
184 const std::map<WifiPhyState, Time>& linkStates) const;
185
186 /**
187 * A helper function used by PrintStatistics method. It prints statistics of a link.
188 *
189 * @param os The output stream to print to
190 * @param linkStates Statistics of a link.
191 * @param unit The unit of time in which the durations should be printed.
192 * @return The output stream after IO operations.
193 */
194 std::ostream& PrintLinkStates(std::ostream& os,
195 const std::map<WifiPhyState, Time>& linkStates,
196 Time::Unit unit) const;
197
198 /**
199 * A helper function used by PrintLinkStates method to format the output. It pads each string at
200 * left with space characters such that the decimal points are at the same position.
201 *
202 * @param column A column of strings. Each string must contain a decimal point.
203 */
204 void AlignDecimal(std::vector<std::string>& column) const;
205
206 /**
207 * A helper function used by PrintLinkStates method to format the output. It pads each string at
208 * right with space characters such that all strings have same width.
209 *
210 * @param column A column of strings.
211 */
212 void AlignWidth(std::vector<std::string>& column) const;
213};
214
215} // namespace ns3
216
217#endif
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Unit
The unit to use to interpret a number representing time.
Definition nstime.h:100
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition nstime.h:286
Track channel occupancy durations for WifiNetDevice.
std::map< WifiPhyState, double > ComputePercentage(const std::map< WifiPhyState, Time > &linkStates) const
A helper function used by PrintStatistics method.
void AlignWidth(std::vector< std::string > &column) const
A helper function used by PrintLinkStates method to format the output.
Time ComputeOverlappingDuration(Time start1, Time stop1, Time start2, Time stop2)
Compute overlapping time-duration between two intervals.
void Stop(Time stopTime)
Stops the collection of statistics at a specified time.
void Enable(NodeContainer nodes)
Enables trace collection for all nodes and WifiNetDevices in the specified NodeContainer.
void PrintStatistics(std::ostream &os, Time::Unit unit=Time::Unit::AUTO) const
Print measurement results on an output stream.
Time m_stopTime
Instant at which statistics collection should stop.
void Start(Time startTime)
Starts the collection of statistics from a specified start time.
Time m_startTime
Instant at which statistics collection should start.
std::ostream & PrintLinkStates(std::ostream &os, const std::map< WifiPhyState, Time > &linkStates, Time::Unit unit) const
A helper function used by PrintStatistics method.
void NotifyWifiPhyState(std::size_t idx, std::size_t phyId, Time start, Time duration, WifiPhyState state)
A callback used to update statistics.
std::vector< DeviceRecord > m_deviceRecords
Stores the collected statistics.
const std::vector< DeviceRecord > & GetDeviceRecords() const
Returns measurement results on each installed device.
void AlignDecimal(std::vector< std::string > &column) const
A helper function used by PrintLinkStates method to format the output.
uint32_t m_numDevices
Count the number of devices traced by this helper.
void Reset()
Resets the current statistics, clearing all links and their durations.
WifiCoTraceHelper()
Default Constructor.
Time stopTime
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
WifiPhyState
The state of the PHY layer.
Keeps track of channel occupancy statistics observed at a WifiNetDevice.
std::map< uint8_t, std::map< WifiPhyState, Time > > m_linkStateDurations
Duration statistics by link and state.
uint32_t m_nodeId
Id of Node on which the WifiNetDevice is installed.
uint32_t m_ifIndex
Device Id of WifiNetDevice.
void AddLinkMeasurement(size_t linkId, Time start, Time duration, WifiPhyState state)
Update the duration statistics for the provided linkId and state.
std::string m_deviceName
Device name. It's empty if the name isn't configured.
DeviceRecord(Ptr< WifiNetDevice > device)
Constructor.
std::string m_nodeName
Name of Node on which the WifiNetDevice is installed.