A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-tx-stats-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Huazhong University of Science and Technology
3 * Copyright (c) 2024 University of Washington
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Authors: Muyuan Shen <muyuan@uw.edu>
8 * Hao Yin <haoyin@uw.edu>
9 */
10
11#ifndef WIFI_TX_STATS_HELPER_H
12#define WIFI_TX_STATS_HELPER_H
13
14#include "ns3/nstime.h"
15#include "ns3/object.h"
16#include "ns3/qos-utils.h"
17#include "ns3/type-id.h"
18#include "ns3/wifi-export.h"
19#include "ns3/wifi-mac.h"
20#include "ns3/wifi-types.h"
21#include "ns3/wifi-utils.h"
22
23#include <cstdint>
24#include <functional>
25#include <map>
26#include <string>
27#include <tuple>
28#include <vector>
29
30namespace ns3
31{
32
34class NodeContainer;
35class Time;
36class Packet;
37class WifiMpdu;
38
39/**
40 * @brief Statistics helper for tracking outcomes of data MPDU transmissions.
41 *
42 * This helper may be used to track statistics of all data MPDU transmissions on a given Node,
43 * WifiNetDevice, or even link granularity (for MLO operation), including counts of the
44 * number of successfully acknowledged MPDUs, the number of retransmissions (if any) of
45 * those successfully acknowledged MPDUs, and the number of failed MPDUs (by drop reason).
46 * The helper can also be used to access timestamped data about how long the MPDU was enqueued
47 * and how long it took to complete acknowledgement (or failure) once the first transmission
48 * of it was attempted. For failed MPDUs, their WifiMacDropReason can be accessed.
49 */
50class WIFI_EXPORT WifiTxStatsHelper
51{
52 public:
53 /**
54 * When Multi-Link Operation (MLO) is used, it is possible for MPDUs to be sent on
55 * multiple links concurrently. When such an MPDU is acked, a question arises as to how
56 * to count the number of successes (only one success, or success on each link). The
57 * ACK does not convey which transmission on which link triggered the event. Therefore,
58 * two policies are defined for how to count a successful event.
59 * 1) FIRST_LINK_IN_SET: Count the success on the first link of the set of in-flight links
60 * 2) ALL_LINKS: Count the success on all links in the in-flight link set
61 */
67
68 /**
69 * Structure for recording information about an individual data MPDU transmission.
70 */
71 struct WIFI_EXPORT MpduRecord
72 {
73 uint32_t m_nodeId{std::numeric_limits<uint32_t>::max()}; //!< The sending node ID
74 uint32_t m_deviceId{std::numeric_limits<uint32_t>::max()}; //!< The sending device ID
75 Time m_enqueueTime; //!< The enqueue time (time that the packet was added to a WifiMacQueue)
76 Time m_txStartTime; //!< The time at which the MPDU was first transmitted
77 std::optional<Time> m_dropTime; //!< The time of removal from the WifiMacQueue, if failed
78 Time m_ackTime; //!< The time at which the MPDU was acknowledged
79 uint64_t m_mpduSeqNum{0}; //!< The MPDU sequence number
80 uint32_t m_retransmissions{0}; //!< A count of the number of retransmissions of the MPDU
81 uint8_t m_tid{WIFI_TID_UNDEFINED}; //!< The TID for the MPDU
82 std::set<uint8_t> m_successLinkIdSet; //!< If acked, the set of in-flight link ID(s)
83 std::optional<WifiMacDropReason> m_dropReason; //!< If failed, the drop reason
84 };
85
86 /**
87 * Default constructor; start time initialized to zero and stop time to Time::Max()
88 */
90 /**
91 * Construct a helper with start and stop times. Only those MPDUs
92 * enqueued between the start and stop times will be counted.
93 *
94 * @param startTime The measurement start time
95 * @param stopTime The measurement stop time
96 */
98
99 /**
100 * Enables trace collection for all nodes and WifiNetDevices in the specified NodeContainer.
101 * @param nodes The NodeContainer to which traces are to be connected.
102 */
103 void Enable(const NodeContainer& nodes);
104 /**
105 * Enables trace collection for all WifiNetDevices in the specified NetDeviceContainer.
106 * @param devices The NetDeviceContainer to which traces are to be connected.
107 */
108 void Enable(const NetDeviceContainer& devices);
109 /**
110 * @brief Set the start time for statistics collection
111 *
112 * No MPDUs enqueued before startTime will be included
113 * in the statistics.
114 *
115 * @param startTime The measurement start time
116 */
117 void Start(Time startTime);
118 /**
119 * @brief Set the stop time for statistics collection
120 *
121 * No MPDUs enqueued after stopTime elapses will be included in
122 * statistics. However, MPDUs that were enqueued before stopTime
123 * (and after startTime) will be included in the statistics if
124 * they are dequeued before the statistics are queried by the user.
125 *
126 * @param stopTime The measurement stop time
127 */
128 void Stop(Time stopTime);
129 /**
130 * @brief Clear the data structures for all completed successful and failed MpduRecords
131 */
132 void Reset();
133
134 //
135 // Hash function and unordered maps
136 //
137
138 /**
139 * Hash function for the tuples used in unordered_maps
140 */
141 struct WIFI_EXPORT TupleHash
142 {
143 /**
144 * Combines two hash values into one (similar to boost::hash_combine)
145 * @param seed the starting hash point
146 * @param value the value to combine to the hash
147 */
148 template <typename T>
149 void hash_combine(size_t& seed, const T& value) const
150 {
151 seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
152 }
153
154 /**
155 * Hash function for tuples of arbitrary size
156 * @param tuple the input Tuple
157 * @return the hash of the tuple elements
158 */
159 template <typename Tuple, size_t Index = 0>
160 size_t tuple_hash(const Tuple& tuple) const
161 {
162 if constexpr (Index < std::tuple_size_v<Tuple>)
163 {
164 size_t seed = tuple_hash<Tuple, Index + 1>(tuple);
165 hash_combine(seed, std::get<Index>(tuple));
166 return seed;
167 }
168 else
169 {
170 return 0; // Base case
171 }
172 }
173
174 /**
175 * Operator overload for calculating hash of tuple
176 * @param tuple the input Tuple
177 * @return the hash of the tuple elements
178 */
179 template <typename... Args>
180 size_t operator()(const std::tuple<Args...>& tuple) const
181 {
182 return tuple_hash(tuple);
183 }
184 };
185
187 std::unordered_map<std::tuple<uint32_t, uint32_t, uint8_t>,
188 uint64_t,
189 TupleHash>; //!< std::unordered_map of {nodeId, deviceId, linkId} tuple
190 //!< to counter value
192 std::unordered_map<std::tuple<uint32_t, uint32_t>,
193 uint64_t,
194 TupleHash>; //!< std::unordered_map of {nodeId, deviceId} tuple to
195 //!< counter value
197 std::unordered_map<std::tuple<uint32_t, uint32_t, uint8_t>,
198 std::list<MpduRecord>,
199 TupleHash>; //!< std::unordered_map of {nodeId, deviceId, linkId} tuple
200 //!< to a list of MPDU records
202 std::unordered_map<std::tuple<uint32_t, uint32_t>,
203 std::list<MpduRecord>,
204 TupleHash>; //!< std::unordered_map of {nodeId, deviceId} tuple to a list
205 //!< of MPDU records
206
207 //
208 // Methods to retrieve the counts tabulated by this helper
209 //
210
211 /**
212 * @brief Return the counts of successful MPDU transmissions in a hash map
213 *
214 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
215 * device ID}. The value returned by indexing the tuple is the count of
216 * successful MPDU transmissions.
217 *
218 * If Multi-Link Operation (MLO) is configured, a variant of this method is available
219 * that returns an unordered_map indexed by a tuple that includes the Link ID, for
220 * more granular statistics reporting.
221 * @see GetSuccessesByNodeDeviceLink
222 *
223 * If Multi-Link Operation (MLO) is configured, and MPDUs are sent on multiple
224 * links in parallel, this method will count one success for any such acknowledged
225 * MPDU (even if it was successfully received on more than one link).
226 *
227 * @return A hash map with counts of MPDU successful transmissions
228 */
230 /**
231 * @brief Return the counts of successful MPDU transmissions in a hash map
232 * @param type how the statistics handle a successful MPDU transmitted on multiple links
233 *
234 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
235 * device ID, link ID}. The value returned by indexing the tuple is the count of
236 * successful MPDU transmissions.
237 *
238 * The link ID corresponds to the link for which the MPDU was successfully
239 * acknowledged, regardless of the links that may have been used in
240 * earlier transmissions of the MPDU.
241 *
242 * For a similar method that does not use Link IDs in the key,
243 * @see GetSuccessesByNodeDevice
244 *
245 * @return A hash map with counts of MPDU successful transmissions
246 */
249 /**
250 * @brief Return the counts of failed MPDU transmissions in a hash map
251 *
252 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
253 * device ID}. The value returned by indexing the tuple is the count of
254 * failed MPDU transmissions.
255 *
256 * The number of failures are only stored with the granularity
257 * of per-device, since in a multi-link scenario, some retransmissions
258 * (that lead to eventual failure) may occur on different links.
259 *
260 * @return A hash map with counts of MPDU failed transmissions
261 */
263 /**
264 * @brief Return the counts of failed MPDU transmissions due to given drop reason in a hash map
265 * @param reason Reason for dropping the MPDU
266 *
267 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
268 * device ID}. The value returned by indexing the tuple is the count of
269 * failed MPDU transmissions due to the reason given.
270 *
271 * The number of failures are only stored with the granularity
272 * of per-device, since in a multi-link scenario, some retransmissions
273 * (that lead to eventual failure) may occur on different links.
274 *
275 * @return A hash map with counts of MPDU failed transmissions
276 */
278 /**
279 * @brief Return the counts of MPDU retransmissions in a hash map
280 *
281 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
282 * device ID}. The value returned by indexing the tuple is the count of
283 * MPDU retransmissions of those MPDUs that were successfully acked.
284 *
285 * The number of retransmissions are only stored with the granularity
286 * of per-device, since in a multi-link scenario, some retransmissions
287 * may be sent on different links.
288 *
289 * @return A hash map with counts of MPDU retransmissions for successful MPDUs
290 */
292 /**
293 * @brief Return the count of successful MPDU transmissions across all enabled devices
294 *
295 * @return The count of all successful MPDU transmissions
296 */
297 uint64_t GetSuccesses() const;
298 /**
299 * @brief Return the count of failed MPDU transmissions across all enabled devices
300 *
301 * @return The count of all failed MPDU transmissions
302 */
303 uint64_t GetFailures() const;
304 /**
305 * @brief Return the count of failed MPDU transmissions due to given drop reason across
306 * all enabled devices
307 * @param reason Reason for dropping the MPDU
308 *
309 * @return The count of all failed MPDU transmissions for the specified reason
310 */
311 uint64_t GetFailures(WifiMacDropReason reason) const;
312 /**
313 * @brief Return the count of MPDU retransmissions across all enabled devices
314 *
315 * @return The count of all MPDU retransmissions for acked MPDUs
316 */
317 uint64_t GetRetransmissions() const;
318 /**
319 * @brief Return the duration since the helper was started or reset
320 *
321 * @return The duration since the helper was started or reset
322 */
323 Time GetDuration() const;
324 /**
325 * @brief Return a hash map of successful MPDU records
326 * @param type how the statistics handle a successful MPDU transmitted on multiple links
327 *
328 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
329 * device ID, link ID}. The value returned by indexing the tuple is the list of MPDU
330 * records of the MPDUs that were successfully acked.
331 *
332 * @return A const reference to the hash map of successful MPDU records
333 */
336 /**
337 * @brief Return a hash map of MPDU records for failed transmissions
338 *
339 * The return type is an std::unordered_map, and the keys are tuples of {node ID,
340 * device ID}. The value returned by indexing the tuple is the list of MPDU
341 * records of the MPDUs that failed.
342 *
343 * @return A const reference to the hash map of MPDU records corresponding to failure
344 */
346
347 private:
348 /**
349 * @brief Callback for the WifiMacQueue::Enqueue trace
350 * @param nodeId the Node ID triggering the trace
351 * @param deviceId the Node ID triggering the trace
352 * @param mpdu The MPDU enqueued
353 */
354 void NotifyMacEnqueue(uint32_t nodeId, uint32_t deviceId, Ptr<const WifiMpdu> mpdu);
355 /**
356 * @brief Callback for the WifiPhy::PhyTxBegin trace
357 * @param nodeId the Node ID triggering the trace
358 * @param deviceId the Node ID triggering the trace
359 * @param pkt The frame being sent
360 */
361 void NotifyTxStart(uint32_t nodeId, uint32_t deviceId, Ptr<const Packet> pkt, Watt_u);
362 /**
363 * @brief Callback for the WifiMac::AckedMpdu trace
364 * @param nodeId the Node ID triggering the trace
365 * @param deviceId the Node ID triggering the trace
366 * @param mpdu The MPDU acked
367 */
368 void NotifyAcked(uint32_t nodeId, uint32_t deviceId, Ptr<const WifiMpdu> mpdu);
369 /**
370 * @brief Callback for the WifiMac::DroppedMpdu trace
371 * @param nodeId the Node ID triggering the trace
372 * @param deviceId the Node ID triggering the trace
373 * @param reason Reason for dropping the MPDU
374 * @param mpdu The MPDU dropped
375 */
376 void NotifyMacDropped(uint32_t nodeId,
377 uint32_t deviceId,
378 WifiMacDropReason reason,
380
381 MpduRecordsPerNodeDevice_t m_successMap; //!< The nested map of successful MPDUs
382 MpduRecordsPerNodeDevice_t m_failureMap; //!< The nested map of failed MPDUs
383 std::map<uint64_t, MpduRecord> m_inflightMap; //!< In-flight MPDUs; key is a Packet UID
384 Time m_startTime; //!< The start time for recording statistics
385 Time m_stopTime{Time::Max()}; //!< The stop time for recording statistics
386};
387
388} // namespace ns3
389
390#endif // WIFI_TX_STATS_HELPER_H
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
network packets
Definition packet.h:228
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition nstime.h:287
WifiMpdu stores a (const) packet along with a MAC header.
Definition wifi-mpdu.h:52
Time m_stopTime
The stop time for recording statistics.
MpduRecordsPerNodeDevice_t m_successMap
The nested map of successful MPDUs.
void Reset()
Clear the data structures for all completed successful and failed MpduRecords.
std::unordered_map< std::tuple< uint32_t, uint32_t >, std::list< MpduRecord >, TupleHash > MpduRecordsPerNodeDevice_t
std::unordered_map of {nodeId, deviceId} tuple to a list of MPDU records
std::unordered_map< std::tuple< uint32_t, uint32_t, uint8_t >, std::list< MpduRecord >, TupleHash > MpduRecordsPerNodeDeviceLink_t
std::unordered_map of {nodeId, deviceId, linkId} tuple to a list of MPDU records
std::map< uint64_t, MpduRecord > m_inflightMap
In-flight MPDUs; key is a Packet UID.
void NotifyAcked(uint32_t nodeId, uint32_t deviceId, Ptr< const WifiMpdu > mpdu)
Callback for the WifiMac::AckedMpdu trace.
CountPerNodeDeviceLink_t GetSuccessesByNodeDeviceLink(WifiTxStatsHelper::MultiLinkSuccessType type=FIRST_LINK_IN_SET) const
Return the counts of successful MPDU transmissions in a hash map.
MultiLinkSuccessType
When Multi-Link Operation (MLO) is used, it is possible for MPDUs to be sent on multiple links concur...
void Stop(Time stopTime)
Set the stop time for statistics collection.
void NotifyTxStart(uint32_t nodeId, uint32_t deviceId, Ptr< const Packet > pkt, Watt_u)
Callback for the WifiPhy::PhyTxBegin trace.
uint64_t GetFailures() const
Return the count of failed MPDU transmissions across all enabled devices.
WifiTxStatsHelper()
Default constructor; start time initialized to zero and stop time to Time::Max().
Time m_startTime
The start time for recording statistics.
void NotifyMacEnqueue(uint32_t nodeId, uint32_t deviceId, Ptr< const WifiMpdu > mpdu)
Callback for the WifiMacQueue::Enqueue trace.
CountPerNodeDevice_t GetFailuresByNodeDevice() const
Return the counts of failed MPDU transmissions in a hash map.
std::unordered_map< std::tuple< uint32_t, uint32_t >, uint64_t, TupleHash > CountPerNodeDevice_t
std::unordered_map of {nodeId, deviceId} tuple to counter value
const MpduRecordsPerNodeDeviceLink_t GetSuccessRecords(WifiTxStatsHelper::MultiLinkSuccessType type=FIRST_LINK_IN_SET) const
Return a hash map of successful MPDU records.
const MpduRecordsPerNodeDevice_t & GetFailureRecords() const
Return a hash map of MPDU records for failed transmissions.
CountPerNodeDevice_t GetSuccessesByNodeDevice() const
Return the counts of successful MPDU transmissions in a hash map.
MpduRecordsPerNodeDevice_t m_failureMap
The nested map of failed MPDUs.
uint64_t GetSuccesses() const
Return the count of successful MPDU transmissions across all enabled devices.
void NotifyMacDropped(uint32_t nodeId, uint32_t deviceId, WifiMacDropReason reason, Ptr< const WifiMpdu > mpdu)
Callback for the WifiMac::DroppedMpdu trace.
void Enable(const NodeContainer &nodes)
Enables trace collection for all nodes and WifiNetDevices in the specified NodeContainer.
CountPerNodeDevice_t GetRetransmissionsByNodeDevice() const
Return the counts of MPDU retransmissions in a hash map.
uint64_t GetRetransmissions() const
Return the count of MPDU retransmissions across all enabled devices.
void Start(Time startTime)
Set the start time for statistics collection.
Time GetDuration() const
Return the duration since the helper was started or reset.
std::unordered_map< std::tuple< uint32_t, uint32_t, uint8_t >, uint64_t, TupleHash > CountPerNodeDeviceLink_t
std::unordered_map of {nodeId, deviceId, linkId} tuple to counter value
Time stopTime
WifiMacDropReason
The reason why an MPDU was dropped.
Definition wifi-mac.h:72
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static constexpr uint8_t WIFI_TID_UNDEFINED
Invalid TID identifier.
Definition wifi-utils.h:313
double Watt_u
Watt weak type.
Definition wifi-units.h:25
Structure for recording information about an individual data MPDU transmission.
uint32_t m_deviceId
The sending device ID.
uint32_t m_nodeId
The sending node ID.
uint64_t m_mpduSeqNum
The MPDU sequence number.
Time m_ackTime
The time at which the MPDU was acknowledged.
uint32_t m_retransmissions
A count of the number of retransmissions of the MPDU.
Time m_enqueueTime
The enqueue time (time that the packet was added to a WifiMacQueue).
std::set< uint8_t > m_successLinkIdSet
If acked, the set of in-flight link ID(s).
std::optional< WifiMacDropReason > m_dropReason
If failed, the drop reason.
std::optional< Time > m_dropTime
The time of removal from the WifiMacQueue, if failed.
Time m_txStartTime
The time at which the MPDU was first transmitted.
uint8_t m_tid
The TID for the MPDU.
Hash function for the tuples used in unordered_maps.
size_t operator()(const std::tuple< Args... > &tuple) const
Operator overload for calculating hash of tuple.
size_t tuple_hash(const Tuple &tuple) const
Hash function for tuples of arbitrary size.
void hash_combine(size_t &seed, const T &value) const
Combines two hash values into one (similar to boost::hash_combine).