A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
flow-monitor.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2009 INESC Porto
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
19 //
20 
21 #ifndef FLOW_MONITOR_H
22 #define FLOW_MONITOR_H
23 
24 #include <vector>
25 #include <map>
26 
27 #include "ns3/ptr.h"
28 #include "ns3/object.h"
29 #include "ns3/flow-probe.h"
30 #include "ns3/flow-classifier.h"
31 #include "ns3/histogram.h"
32 #include "ns3/nstime.h"
33 #include "ns3/event-id.h"
34 
35 namespace ns3 {
36 
41 class FlowMonitor : public Object
42 {
43 public:
44 
46  struct FlowStats
47  {
52 
57 
62 
66 
69  Time delaySum; // delayCount == rxPackets
70 
78  Time jitterSum; // jitterCount == rxPackets - 1
79 
81 
83  uint64_t txBytes;
85  uint64_t rxBytes;
87  uint32_t txPackets;
89  uint32_t rxPackets;
90 
96  uint32_t lostPackets;
97 
100  uint32_t timesForwarded;
101 
108 
121  std::vector<uint32_t> packetsDropped; // packetsDropped[reasonCode] => number of dropped packets
122 
125  std::vector<uint64_t> bytesDropped; // bytesDropped[reasonCode] => number of dropped bytes
126  Histogram flowInterruptionsHistogram; // histogram of durations of flow interruptions
127  };
128 
129  // --- basic methods ---
130  static TypeId GetTypeId ();
131  TypeId GetInstanceTypeId () const;
132  FlowMonitor ();
133 
135  void SetFlowClassifier (Ptr<FlowClassifier> classifier);
136 
138  void Start (const Time &time);
140  void Stop (const Time &time);
142  void StartRightNow ();
144  void StopRightNow ();
145 
146  // --- methods to be used by the FlowMonitorProbe's only ---
150  void AddProbe (Ptr<FlowProbe> probe);
151 
156  void ReportFirstTx (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
159  void ReportForwarding (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
162  void ReportLastRx (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
165  void ReportDrop (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId,
166  uint32_t packetSize, uint32_t reasonCode);
167 
169  void CheckForLostPackets ();
170 
174  void CheckForLostPackets (Time maxDelay);
175 
176  // --- methods to get the results ---
181  std::map<FlowId, FlowStats> GetFlowStats () const;
182 
184  std::vector< Ptr<FlowProbe> > GetAllProbes () const;
185 
191  void SerializeToXmlStream (std::ostream &os, int indent, bool enableHistograms, bool enableProbes);
197  std::string SerializeToXmlString (int indent, bool enableHistograms, bool enableProbes);
202  void SerializeToXmlFile (std::string fileName, bool enableHistograms, bool enableProbes);
203 
204 
205 protected:
206 
207  virtual void NotifyConstructionCompleted ();
208  virtual void DoDispose (void);
209 
210 private:
211 
213  {
214  Time firstSeenTime; // absolute time when the packet was first seen by a probe
215  Time lastSeenTime; // absolute time when the packet was last seen by a probe
216  uint32_t timesForwarded; // number of times the packet was reportedly forwarded
217  };
218 
219  // FlowId --> FlowStats
220  std::map<FlowId, FlowStats> m_flowStats;
221 
222  // (FlowId,PacketId) --> TrackedPacket
223  typedef std::map< std::pair<FlowId, FlowPacketId>, TrackedPacket> TrackedPacketMap;
226  std::vector< Ptr<FlowProbe> > m_flowProbes;
227 
228  // note: this is needed only for serialization
230 
233  bool m_enabled;
239 
240  FlowStats& GetStatsForFlow (FlowId flowId);
242 };
243 
244 
245 } // namespace ns3
246 
247 #endif /* FLOW_MONITOR_H */
248