21 #include "ipv4-rate-l3-tracer.h"
23 #include "ns3/packet.h"
24 #include "ns3/config.h"
25 #include "ns3/callback.h"
26 #include "ns3/simulator.h"
27 #include "ns3/node-list.h"
31 #include "ns3/ipv4-header.h"
33 #include <boost/lexical_cast.hpp>
35 using namespace boost;
38 NS_LOG_COMPONENT_DEFINE (
"Ipv4RateL3Tracer");
48 boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<Ipv4RateL3Tracer> > >
49 Ipv4RateL3Tracer::InstallAll (
const std::string &file, Time averagingPeriod)
51 std::list<Ptr<Ipv4RateL3Tracer> > tracers;
52 boost::shared_ptr<std::ostream> outputStream;
55 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
56 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
59 return boost::make_tuple (outputStream, tracers);
65 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
68 for (NodeList::Iterator node = NodeList::Begin ();
69 node != NodeList::End ();
72 Ptr<Ipv4RateL3Tracer> trace = Install (*node, outputStream, averagingPeriod);
73 tracers.push_back (trace);
76 if (tracers.size () > 0)
79 tracers.front ()->PrintHeader (*outputStream);
80 *outputStream <<
"\n";
83 return boost::make_tuple (outputStream, tracers);
86 boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<Ipv4RateL3Tracer> > >
87 Ipv4RateL3Tracer::Install (
const NodeContainer &nodes,
const std::string &file, Time averagingPeriod)
89 using namespace boost;
92 std::list<Ptr<Ipv4RateL3Tracer> > tracers;
93 boost::shared_ptr<std::ostream> outputStream;
96 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
97 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
100 return boost::make_tuple (outputStream, tracers);
106 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
109 for (NodeContainer::Iterator node = nodes.Begin ();
110 node != nodes.End ();
113 Ptr<Ipv4RateL3Tracer> trace = Install (*node, outputStream, averagingPeriod);
114 tracers.push_back (trace);
117 if (tracers.size () > 0)
120 tracers.front ()->PrintHeader (*outputStream);
121 *outputStream <<
"\n";
124 return boost::make_tuple (outputStream, tracers);
127 boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<Ipv4RateL3Tracer> > >
128 Ipv4RateL3Tracer::Install (Ptr<Node> node,
const std::string &file, Time averagingPeriod)
130 using namespace boost;
133 std::list<Ptr<Ipv4RateL3Tracer> > tracers;
134 boost::shared_ptr<std::ostream> outputStream;
137 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
138 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
141 return boost::make_tuple (outputStream, tracers);
147 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
150 Ptr<Ipv4RateL3Tracer> trace = Install (node, outputStream, averagingPeriod);
151 tracers.push_back (trace);
153 if (tracers.size () > 0)
156 tracers.front ()->PrintHeader (*outputStream);
157 *outputStream <<
"\n";
160 return boost::make_tuple (outputStream, tracers);
164 Ptr<Ipv4RateL3Tracer>
165 Ipv4RateL3Tracer::Install (Ptr<Node> node,
166 boost::shared_ptr<std::ostream> outputStream,
167 Time averagingPeriod)
169 NS_LOG_DEBUG (
"Node: " << node->GetId ());
171 Ptr<Ipv4RateL3Tracer> trace = Create<Ipv4RateL3Tracer> (outputStream, node);
172 trace->SetAveragingPeriod (averagingPeriod);
178 Ipv4RateL3Tracer::Ipv4RateL3Tracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
182 SetAveragingPeriod (Seconds (1.0));
185 Ipv4RateL3Tracer::~Ipv4RateL3Tracer ()
187 m_printEvent.Cancel ();
191 Ipv4RateL3Tracer::SetAveragingPeriod (
const Time &period)
194 m_printEvent.Cancel ();
195 m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter,
this);
199 Ipv4RateL3Tracer::PeriodicPrinter ()
204 m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter,
this);
208 Ipv4RateL3Tracer::PrintHeader (std::ostream &os)
const
213 <<
"Interface" <<
"\t"
221 Ipv4RateL3Tracer::Reset ()
223 for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
224 stats != m_stats.end ();
227 stats->second.get<0> ().Reset ();
228 stats->second.get<1> ().Reset ();
232 const double alpha = 0.8;
234 #define STATS(INDEX) stats->second.get<INDEX> ()
235 #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
237 #define PRINTER(printName, fieldName) \
238 STATS(2).fieldName = alpha * RATE(0, fieldName) + (1-alpha) * STATS(2).fieldName; \
239 STATS(3).fieldName = alpha * RATE(1, fieldName) / 1024.0 + (1-alpha) * STATS(3).fieldName; \
241 os << time.ToDouble (Time::S) << "\t" \
243 << stats->first << "\t" \
244 << printName << "\t" \
245 << STATS(2).fieldName << "\t" \
246 << STATS(3).fieldName << "\n";
249 Ipv4RateL3Tracer::Print (std::ostream &os)
const
251 for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
252 stats != m_stats.end ();
255 Time time = Simulator::Now ();
257 PRINTER (
"In", m_in);
258 PRINTER (
"Out", m_out);
259 PRINTER (
"Drop", m_drop);
264 Ipv4RateL3Tracer::Rx (Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface)
266 m_stats[iface].get<0> ().m_in ++;
267 m_stats[iface].get<1> ().m_in += packet->GetSize ();
271 Ipv4RateL3Tracer::Tx (Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface)
273 m_stats[iface].get<0> ().m_out ++;
274 m_stats[iface].get<1> ().m_out += packet->GetSize ();
278 Ipv4RateL3Tracer::Drop (
const Ipv4Header &header, Ptr<const Packet> packet, Ipv4L3Protocol::DropReason, Ptr<Ipv4> ipv4, uint32_t iface)
280 m_stats[iface].get<0> ().m_drop ++;
281 m_stats[iface].get<1> ().m_drop += packet->GetSize ();
Base class for IPv4 network-layer tracers.