A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
flow-monitor.h
Go to the documentation of this file.
1//
2// Copyright (c) 2009 INESC Porto
3//
4// SPDX-License-Identifier: GPL-2.0-only
5//
6// Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
7//
8
9#ifndef FLOW_MONITOR_H
10#define FLOW_MONITOR_H
11
12#include "flow-classifier.h"
13#include "flow-probe.h"
14
15#include "ns3/event-id.h"
16#include "ns3/histogram.h"
17#include "ns3/nstime.h"
18#include "ns3/object.h"
19#include "ns3/ptr.h"
20
21#include <map>
22#include <vector>
23
24namespace ns3
25{
26
27/**
28 * @defgroup flow-monitor Flow Monitor
29 * @brief Collect and store performance data from a simulation
30 */
31
32/**
33 * @ingroup flow-monitor
34 * @brief An object that monitors and reports back packet flows observed during a simulation
35 *
36 * The FlowMonitor class is responsible for coordinating efforts
37 * regarding probes, and collects end-to-end flow statistics.
38 *
39 */
40class FlowMonitor : public Object
41{
42 public:
43 /// @brief Structure that represents the measured metrics of an individual packet flow
44 struct FlowStats
45 {
46 /// Contains the absolute time when the first packet in the flow
47 /// was transmitted, i.e. the time when the flow transmission
48 /// starts
50
51 /// Contains the absolute time when the first packet in the flow
52 /// was received by an end node, i.e. the time when the flow
53 /// reception starts
55
56 /// Contains the absolute time when the last packet in the flow
57 /// was transmitted, i.e. the time when the flow transmission
58 /// ends
60
61 /// Contains the absolute time when the last packet in the flow
62 /// was received, i.e. the time when the flow reception ends
64
65 /// Contains the sum of all end-to-end delays for all received
66 /// packets of the flow.
67 Time delaySum; // delayCount == rxPackets
68
69 /// Contains the sum of all end-to-end delay jitter (delay
70 /// variation) values for all received packets of the flow. Here
71 /// we define _jitter_ of a packet as the delay variation
72 /// relatively to the last packet of the stream,
73 /// i.e. \f$Jitter\left\{P_N\right\} = \left|Delay\left\{P_N\right\} -
74 /// Delay\left\{P_{N-1}\right\}\right|\f$. This definition is in accordance with the
75 /// Type-P-One-way-ipdv as defined in IETF \RFC{3393}.
76 Time jitterSum; // jitterCount == rxPackets - 1
77
78 /// Contains the last measured delay of a packet
79 /// It is stored to measure the packet's Jitter
81 /// Contains the largest measured delay of a received packet
83 /// Contains the smallest measured delay of a received packet
85
86 /// Total number of transmitted bytes for the flow
87 uint64_t txBytes;
88 /// Total number of received bytes for the flow
89 uint64_t rxBytes;
90 /// Total number of transmitted packets for the flow
92 /// Total number of received packets for the flow
94
95 /// Total number of packets that are assumed to be lost,
96 /// i.e. those that were transmitted but have not been reportedly
97 /// received or forwarded for a long time. By default, packets
98 /// missing for a period of over 10 seconds are assumed to be
99 /// lost, although this value can be easily configured in runtime
101
102 /// Contains the number of times a packet has been reportedly
103 /// forwarded, summed for all received packets in the flow
105
106 /// Histogram of the packet delays
108 /// Histogram of the packet jitters
110 /// Histogram of the packet sizes
112
113 /// This attribute also tracks the number of lost packets and
114 /// bytes, but discriminates the losses by a _reason code_. This
115 /// reason code is usually an enumeration defined by the concrete
116 /// FlowProbe class, and for each reason code there may be a
117 /// vector entry indexed by that code and whose value is the
118 /// number of packets or bytes lost due to this reason. For
119 /// instance, in the Ipv4FlowProbe case the following reasons are
120 /// currently defined: DROP_NO_ROUTE (no IPv4 route found for a
121 /// packet), DROP_TTL_EXPIRE (a packet was dropped due to an IPv4
122 /// TTL field decremented and reaching zero), and
123 /// DROP_BAD_CHECKSUM (a packet had bad IPv4 header checksum and
124 /// had to be dropped).
125 std::vector<uint32_t>
126 packetsDropped; // packetsDropped[reasonCode] => number of dropped packets
127
128 /// This attribute also tracks the number of lost bytes. See also
129 /// comment in attribute packetsDropped.
130 std::vector<uint64_t> bytesDropped; // bytesDropped[reasonCode] => number of dropped bytes
131 Histogram flowInterruptionsHistogram; //!< histogram of durations of flow interruptions
132 };
133
134 // --- basic methods ---
135 /**
136 * @brief Get the type ID.
137 * @return the object TypeId
138 */
139 static TypeId GetTypeId();
140 FlowMonitor();
141
142 /// Add a FlowClassifier to be used by the flow monitor.
143 /// @param classifier the FlowClassifier
144 void AddFlowClassifier(Ptr<FlowClassifier> classifier);
145
146 /// Set the time, counting from the current time, from which to start monitoring flows.
147 /// This method overwrites any previous calls to Start()
148 /// @param time delta time to start
149 void Start(const Time& time);
150 /// Set the time, counting from the current time, from which to stop monitoring flows.
151 /// This method overwrites any previous calls to Stop()
152 /// @param time delta time to stop
153 void Stop(const Time& time);
154 /// Begin monitoring flows *right now*
155 void StartRightNow();
156 /// End monitoring flows *right now*
157 void StopRightNow();
158
159 // --- methods to be used by the FlowMonitorProbe's only ---
160 /// Register a new FlowProbe that will begin monitoring and report
161 /// events to this monitor. This method is normally only used by
162 /// FlowProbe implementations.
163 /// @param probe the probe to add
164 void AddProbe(Ptr<FlowProbe> probe);
165
166 /// FlowProbe implementations are supposed to call this method to
167 /// report that a new packet was transmitted (but keep in mind the
168 /// distinction between a new packet entering the system and a
169 /// packet that is already known and is only being forwarded).
170 /// @param probe the reporting probe
171 /// @param flowId flow identification
172 /// @param packetId Packet ID
173 /// @param packetSize packet size
174 void ReportFirstTx(Ptr<FlowProbe> probe,
175 FlowId flowId,
176 FlowPacketId packetId,
178 /// FlowProbe implementations are supposed to call this method to
179 /// report that a known packet is being forwarded.
180 /// @param probe the reporting probe
181 /// @param flowId flow identification
182 /// @param packetId Packet ID
183 /// @param packetSize packet size
185 FlowId flowId,
186 FlowPacketId packetId,
188 /// FlowProbe implementations are supposed to call this method to
189 /// report that a known packet is being received.
190 /// @param probe the reporting probe
191 /// @param flowId flow identification
192 /// @param packetId Packet ID
193 /// @param packetSize packet size
194 void ReportLastRx(Ptr<FlowProbe> probe,
195 FlowId flowId,
196 FlowPacketId packetId,
198 /// FlowProbe implementations are supposed to call this method to
199 /// report that a known packet is being dropped due to some reason.
200 /// @param probe the reporting probe
201 /// @param flowId flow identification
202 /// @param packetId Packet ID
203 /// @param packetSize packet size
204 /// @param reasonCode drop reason code
205 void ReportDrop(Ptr<FlowProbe> probe,
206 FlowId flowId,
207 FlowPacketId packetId,
209 uint32_t reasonCode);
210
211 /// Check right now for packets that appear to be lost
212 void CheckForLostPackets();
213
214 /// Check right now for packets that appear to be lost, considering
215 /// packets as lost if not seen in the network for a time larger
216 /// than maxDelay
217 /// @param maxDelay the max delay for a packet
218 void CheckForLostPackets(Time maxDelay);
219
220 // --- methods to get the results ---
221
222 /// Container: FlowId, FlowStats
223 typedef std::map<FlowId, FlowStats> FlowStatsContainer;
224 /// Container Iterator: FlowId, FlowStats
225 typedef std::map<FlowId, FlowStats>::iterator FlowStatsContainerI;
226 /// Container Const Iterator: FlowId, FlowStats
227 typedef std::map<FlowId, FlowStats>::const_iterator FlowStatsContainerCI;
228 /// Container: FlowProbe
229 typedef std::vector<Ptr<FlowProbe>> FlowProbeContainer;
230 /// Container Iterator: FlowProbe
231 typedef std::vector<Ptr<FlowProbe>>::iterator FlowProbeContainerI;
232 /// Container Const Iterator: FlowProbe
233 typedef std::vector<Ptr<FlowProbe>>::const_iterator FlowProbeContainerCI;
234
235 /// Retrieve all collected the flow statistics. Note, if the
236 /// FlowMonitor has not stopped monitoring yet, you should call
237 /// CheckForLostPackets() to make sure all possibly lost packets are
238 /// accounted for.
239 /// @returns the flows statistics
240 const FlowStatsContainer& GetFlowStats() const;
241
242 /// Get a list of all FlowProbe's associated with this FlowMonitor
243 /// @returns a list of all the probes
244 const FlowProbeContainer& GetAllProbes() const;
245
246 /// Serializes the results to an std::ostream in XML format
247 /// @param os the output stream
248 /// @param indent number of spaces to use as base indentation level
249 /// @param enableHistograms if true, include also the histograms in the output
250 /// @param enableProbes if true, include also the per-probe/flow pair statistics in the output
251 void SerializeToXmlStream(std::ostream& os,
252 uint16_t indent,
253 bool enableHistograms,
254 bool enableProbes);
255
256 /// Same as SerializeToXmlStream, but returns the output as a std::string
257 /// @param indent number of spaces to use as base indentation level
258 /// @param enableHistograms if true, include also the histograms in the output
259 /// @param enableProbes if true, include also the per-probe/flow pair statistics in the output
260 /// @return the XML output as string
261 std::string SerializeToXmlString(uint16_t indent, bool enableHistograms, bool enableProbes);
262
263 /// Same as SerializeToXmlStream, but writes to a file instead
264 /// @param fileName name or path of the output file that will be created
265 /// @param enableHistograms if true, include also the histograms in the output
266 /// @param enableProbes if true, include also the per-probe/flow pair statistics in the output
267 void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes);
268
269 /// Reset all the statistics
270 void ResetAllStats();
271
272 protected:
273 void NotifyConstructionCompleted() override;
274 void DoDispose() override;
275
276 private:
277 /// Structure to represent a single tracked packet data
279 {
280 Time firstSeenTime; //!< absolute time when the packet was first seen by a probe
281 Time lastSeenTime; //!< absolute time when the packet was last seen by a probe
282 uint32_t timesForwarded; //!< number of times the packet was reportedly forwarded
283 };
284
285 /// FlowId --> FlowStats
287
288 /// (FlowId,PacketId) --> TrackedPacket
289 typedef std::map<std::pair<FlowId, FlowPacketId>, TrackedPacket> TrackedPacketMap;
290 TrackedPacketMap m_trackedPackets; //!< Tracked packets
291 Time m_maxPerHopDelay; //!< Minimum per-hop delay
292 FlowProbeContainer m_flowProbes; //!< all the FlowProbes
293
294 // note: this is needed only for serialization
295 std::list<Ptr<FlowClassifier>> m_classifiers; //!< the FlowClassifiers
296
297 EventId m_startEvent; //!< Start event
298 EventId m_stopEvent; //!< Stop event
299 bool m_enabled; //!< FlowMon is enabled
300 double m_delayBinWidth; //!< Delay bin width (for histograms)
301 double m_jitterBinWidth; //!< Jitter bin width (for histograms)
302 double m_packetSizeBinWidth; //!< packet size bin width (for histograms)
303 double m_flowInterruptionsBinWidth; //!< Flow interruptions bin width (for histograms)
304 Time m_flowInterruptionsMinTime; //!< Flow interruptions minimum time
305
306 /// Get the stats for a given flow
307 /// @param flowId the Flow identification
308 /// @returns the stats of the flow
310
311 /// Periodic function to check for lost packets and prune statistics
313};
314
315} // namespace ns3
316
317#endif /* FLOW_MONITOR_H */
An identifier for simulation events.
Definition event-id.h:45
An object that monitors and reports back packet flows observed during a simulation.
FlowStats & GetStatsForFlow(FlowId flowId)
Get the stats for a given flow.
FlowProbeContainer m_flowProbes
all the FlowProbes
void StopRightNow()
End monitoring flows right now
void ResetAllStats()
Reset all the statistics.
const FlowProbeContainer & GetAllProbes() const
Get a list of all FlowProbe's associated with this FlowMonitor.
std::vector< Ptr< FlowProbe > > FlowProbeContainer
Container: FlowProbe.
FlowStatsContainer m_flowStats
FlowId --> FlowStats.
bool m_enabled
FlowMon is enabled.
void CheckForLostPackets()
Check right now for packets that appear to be lost.
void Start(const Time &time)
Set the time, counting from the current time, from which to start monitoring flows.
std::map< std::pair< FlowId, FlowPacketId >, TrackedPacket > TrackedPacketMap
(FlowId,PacketId) --> TrackedPacket
double m_flowInterruptionsBinWidth
Flow interruptions bin width (for histograms)
void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but writes to a file instead.
void AddFlowClassifier(Ptr< FlowClassifier > classifier)
Add a FlowClassifier to be used by the flow monitor.
void ReportLastRx(Ptr< FlowProbe > probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize)
FlowProbe implementations are supposed to call this method to report that a known packet is being rec...
EventId m_startEvent
Start event.
std::list< Ptr< FlowClassifier > > m_classifiers
the FlowClassifiers
void AddProbe(Ptr< FlowProbe > probe)
Register a new FlowProbe that will begin monitoring and report events to this monitor.
void ReportForwarding(Ptr< FlowProbe > probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize)
FlowProbe implementations are supposed to call this method to report that a known packet is being for...
Time m_flowInterruptionsMinTime
Flow interruptions minimum time.
std::map< FlowId, FlowStats >::iterator FlowStatsContainerI
Container Iterator: FlowId, FlowStats.
std::map< FlowId, FlowStats > FlowStatsContainer
Container: FlowId, FlowStats.
double m_jitterBinWidth
Jitter bin width (for histograms)
std::string SerializeToXmlString(uint16_t indent, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but returns the output as a std::string.
void Stop(const Time &time)
Set the time, counting from the current time, from which to stop monitoring flows.
void PeriodicCheckForLostPackets()
Periodic function to check for lost packets and prune statistics.
double m_packetSizeBinWidth
packet size bin width (for histograms)
Time m_maxPerHopDelay
Minimum per-hop delay.
void DoDispose() override
Destructor implementation.
std::map< FlowId, FlowStats >::const_iterator FlowStatsContainerCI
Container Const Iterator: FlowId, FlowStats.
void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
const FlowStatsContainer & GetFlowStats() const
Retrieve all collected the flow statistics.
TrackedPacketMap m_trackedPackets
Tracked packets.
double m_delayBinWidth
Delay bin width (for histograms)
void StartRightNow()
Begin monitoring flows right now
void ReportFirstTx(Ptr< FlowProbe > probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize)
FlowProbe implementations are supposed to call this method to report that a new packet was transmitte...
void SerializeToXmlStream(std::ostream &os, uint16_t indent, bool enableHistograms, bool enableProbes)
Serializes the results to an std::ostream in XML format.
EventId m_stopEvent
Stop event.
std::vector< Ptr< FlowProbe > >::const_iterator FlowProbeContainerCI
Container Const Iterator: FlowProbe.
static TypeId GetTypeId()
Get the type ID.
void ReportDrop(Ptr< FlowProbe > probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize, uint32_t reasonCode)
FlowProbe implementations are supposed to call this method to report that a known packet is being dro...
std::vector< Ptr< FlowProbe > >::iterator FlowProbeContainerI
Container Iterator: FlowProbe.
Class used to store data and make an histogram of the data frequency.
Definition histogram.h:35
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Structure that represents the measured metrics of an individual packet flow.
uint32_t rxPackets
Total number of received packets for the flow.
Histogram packetSizeHistogram
Histogram of the packet sizes.
Time timeLastTxPacket
Contains the absolute time when the last packet in the flow was transmitted, i.e.
uint32_t lostPackets
Total number of packets that are assumed to be lost, i.e.
Histogram jitterHistogram
Histogram of the packet jitters.
Time lastDelay
Contains the last measured delay of a packet It is stored to measure the packet's Jitter.
Time timeLastRxPacket
Contains the absolute time when the last packet in the flow was received, i.e.
Histogram flowInterruptionsHistogram
histogram of durations of flow interruptions
uint64_t rxBytes
Total number of received bytes for the flow.
Time maxDelay
Contains the largest measured delay of a received packet.
Time minDelay
Contains the smallest measured delay of a received packet.
Time delaySum
Contains the sum of all end-to-end delays for all received packets of the flow.
Time timeFirstRxPacket
Contains the absolute time when the first packet in the flow was received by an end node,...
uint32_t timesForwarded
Contains the number of times a packet has been reportedly forwarded, summed for all received packets ...
uint32_t txPackets
Total number of transmitted packets for the flow.
uint64_t txBytes
Total number of transmitted bytes for the flow.
Histogram delayHistogram
Histogram of the packet delays.
Time jitterSum
Contains the sum of all end-to-end delay jitter (delay variation) values for all received packets of ...
std::vector< uint64_t > bytesDropped
This attribute also tracks the number of lost bytes.
Time timeFirstTxPacket
Contains the absolute time when the first packet in the flow was transmitted, i.e.
std::vector< uint32_t > packetsDropped
This attribute also tracks the number of lost packets and bytes, but discriminates the losses by a re...
Structure to represent a single tracked packet data.
Time lastSeenTime
absolute time when the packet was last seen by a probe
Time firstSeenTime
absolute time when the packet was first seen by a probe
uint32_t timesForwarded
number of times the packet was reportedly forwarded
static const uint32_t packetSize
Packet size generated at the AP.