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