21 #include "ndn-l3-rate-tracer.h"
23 #include "ns3/packet.h"
24 #include "ns3/config.h"
25 #include "ns3/callback.h"
26 #include "ns3/simulator.h"
28 #include "ns3/node-list.h"
30 #include "ns3/ndn-app.h"
31 #include "ns3/ndn-face.h"
32 #include "ns3/ndn-interest.h"
33 #include "ns3/ndn-data.h"
34 #include "ns3/ndn-pit-entry.h"
37 #include <boost/lexical_cast.hpp>
39 using namespace boost;
42 NS_LOG_COMPONENT_DEFINE (
"ndn.L3RateTracer");
47 static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer> > > > g_tracers;
56 L3RateTracer::Destroy ()
62 L3RateTracer::InstallAll (
const std::string &file, Time averagingPeriod)
64 std::list<Ptr<L3RateTracer> > tracers;
65 boost::shared_ptr<std::ostream> outputStream;
68 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
69 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
73 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
81 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
84 for (NodeList::Iterator node = NodeList::Begin ();
85 node != NodeList::End ();
88 Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod);
89 tracers.push_back (trace);
92 if (tracers.size () > 0)
95 tracers.front ()->PrintHeader (*outputStream);
96 *outputStream <<
"\n";
99 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
103 L3RateTracer::Install (
const NodeContainer &nodes,
const std::string &file, Time averagingPeriod)
105 using namespace boost;
108 std::list<Ptr<L3RateTracer> > tracers;
109 boost::shared_ptr<std::ostream> outputStream;
112 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
113 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
117 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
125 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
128 for (NodeContainer::Iterator node = nodes.Begin ();
129 node != nodes.End ();
132 Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod);
133 tracers.push_back (trace);
136 if (tracers.size () > 0)
139 tracers.front ()->PrintHeader (*outputStream);
140 *outputStream <<
"\n";
143 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
147 L3RateTracer::Install (Ptr<Node> node,
const std::string &file, Time averagingPeriod)
149 using namespace boost;
152 std::list<Ptr<L3RateTracer> > tracers;
153 boost::shared_ptr<std::ostream> outputStream;
156 boost::shared_ptr<std::ofstream> os (
new std::ofstream ());
157 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
161 NS_LOG_ERROR (
"File " << file <<
" cannot be opened for writing. Tracing disabled");
169 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
172 Ptr<L3RateTracer> trace = Install (node, outputStream, averagingPeriod);
173 tracers.push_back (trace);
175 if (tracers.size () > 0)
178 tracers.front ()->PrintHeader (*outputStream);
179 *outputStream <<
"\n";
182 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
187 L3RateTracer::Install (Ptr<Node> node,
188 boost::shared_ptr<std::ostream> outputStream,
189 Time averagingPeriod)
191 NS_LOG_DEBUG (
"Node: " << node->GetId ());
193 Ptr<L3RateTracer> trace = Create<L3RateTracer> (outputStream, node);
194 trace->SetAveragingPeriod (averagingPeriod);
200 L3RateTracer::L3RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
204 SetAveragingPeriod (Seconds (1.0));
211 SetAveragingPeriod (Seconds (1.0));
216 m_printEvent.Cancel ();
220 L3RateTracer::SetAveragingPeriod (
const Time &period)
223 m_printEvent.Cancel ();
224 m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter,
this);
228 L3RateTracer::PeriodicPrinter ()
233 m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter,
this);
243 <<
"FaceDescr" <<
"\t"
247 <<
"Kilobytes" <<
"\t"
248 <<
"PacketRaw" <<
"\t"
253 L3RateTracer::Reset ()
255 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
256 stats != m_stats.end ();
259 stats->second.get<0> ().Reset ();
260 stats->second.get<1> ().Reset ();
264 const double alpha = 0.8;
266 #define STATS(INDEX) stats->second.get<INDEX> ()
267 #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
269 #define PRINTER(printName, fieldName) \
270 STATS(2).fieldName = alpha * RATE(0, fieldName) + (1-alpha) * STATS(2).fieldName; \
271 STATS(3).fieldName = alpha * RATE(1, fieldName) / 1024.0 + (1-alpha) * STATS(3).fieldName; \
273 os << time.ToDouble (Time::S) << "\t" \
278 << stats->first->GetId () << "\t" \
279 << *stats->first << "\t"; \
286 << printName << "\t" \
287 << STATS(2).fieldName << "\t" \
288 << STATS(3).fieldName << "\t" \
289 << STATS(0).fieldName << "\t" \
290 << STATS(1).fieldName / 1024.0 << "\n";
295 Time time = Simulator::Now ();
297 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
298 stats != m_stats.end ();
304 PRINTER (
"InInterests", m_inInterests);
305 PRINTER (
"OutInterests", m_outInterests);
306 PRINTER (
"DropInterests", m_dropInterests);
308 PRINTER (
"InNacks", m_inNacks);
309 PRINTER (
"OutNacks", m_outNacks);
310 PRINTER (
"DropNacks", m_dropNacks);
312 PRINTER (
"InData", m_inData);
313 PRINTER (
"OutData", m_outData);
314 PRINTER (
"DropData", m_dropData);
316 PRINTER (
"InSatisfiedInterests", m_satisfiedInterests);
317 PRINTER (
"InTimedOutInterests", m_timedOutInterests);
319 PRINTER (
"OutSatisfiedInterests", m_outSatisfiedInterests);
320 PRINTER (
"OutTimedOutInterests", m_outTimedOutInterests);
324 std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.find (Ptr<const Face> (0));
325 if (stats != m_stats.end ())
327 PRINTER (
"SatisfiedInterests", m_satisfiedInterests);
328 PRINTER (
"TimedOutInterests", m_timedOutInterests);
335 L3RateTracer::OutInterests (Ptr<const Interest> interest, Ptr<const Face> face)
337 m_stats[face].get<0> ().m_outInterests ++;
338 if (interest->GetWire ())
340 m_stats[face].get<1> ().m_outInterests += interest->GetWire ()->GetSize ();
345 L3RateTracer::InInterests (Ptr<const Interest> interest, Ptr<const Face> face)
347 m_stats[face].get<0> ().m_inInterests ++;
348 if (interest->GetWire ())
350 m_stats[face].get<1> ().m_inInterests += interest->GetWire ()->GetSize ();
355 L3RateTracer::DropInterests (Ptr<const Interest> interest, Ptr<const Face> face)
357 m_stats[face].get<0> ().m_dropInterests ++;
358 if (interest->GetWire ())
360 m_stats[face].get<1> ().m_dropInterests += interest->GetWire ()->GetSize ();
365 L3RateTracer::OutNacks (Ptr<const Interest> interest, Ptr<const Face> face)
367 m_stats[face].get<0> ().m_outNacks ++;
368 if (interest->GetWire ())
370 m_stats[face].get<1> ().m_outNacks += interest->GetWire ()->GetSize ();
375 L3RateTracer::InNacks (Ptr<const Interest> interest, Ptr<const Face> face)
377 m_stats[face].get<0> ().m_inNacks ++;
378 if (interest->GetWire ())
380 m_stats[face].get<1> ().m_inNacks += interest->GetWire ()->GetSize ();
385 L3RateTracer::DropNacks (Ptr<const Interest> interest, Ptr<const Face> face)
387 m_stats[face].get<0> ().m_dropNacks ++;
388 if (interest->GetWire ())
390 m_stats[face].get<1> ().m_dropNacks += interest->GetWire ()->GetSize ();
395 L3RateTracer::OutData (Ptr<const Data> data,
396 bool fromCache, Ptr<const Face> face)
398 m_stats[face].get<0> ().m_outData ++;
399 if (data->GetWire ())
401 m_stats[face].get<1> ().m_outData += data->GetWire ()->GetSize ();
406 L3RateTracer::InData (Ptr<const Data> data,
407 Ptr<const Face> face)
409 m_stats[face].get<0> ().m_inData ++;
410 if (data->GetWire ())
412 m_stats[face].get<1> ().m_inData += data->GetWire ()->GetSize ();
417 L3RateTracer::DropData (Ptr<const Data> data,
418 Ptr<const Face> face)
420 m_stats[face].get<0> ().m_dropData ++;
421 if (data->GetWire ())
423 m_stats[face].get<1> ().m_dropData += data->GetWire ()->GetSize ();
428 L3RateTracer::SatisfiedInterests (Ptr<const pit::Entry> entry)
430 m_stats[0].get<0> ().m_satisfiedInterests ++;
433 for (pit::Entry::in_container::const_iterator i = entry->GetIncoming ().begin ();
434 i != entry->GetIncoming ().end ();
437 m_stats[i->m_face].get<0> ().m_satisfiedInterests ++;
440 for (pit::Entry::out_container::const_iterator i = entry->GetOutgoing ().begin ();
441 i != entry->GetOutgoing ().end ();
444 m_stats[i->m_face].get<0> ().m_outSatisfiedInterests ++;
449 L3RateTracer::TimedOutInterests (Ptr<const pit::Entry> entry)
451 m_stats[0].get<0> ().m_timedOutInterests ++;
454 for (pit::Entry::in_container::const_iterator i = entry->GetIncoming ().begin ();
455 i != entry->GetIncoming ().end ();
458 m_stats[i->m_face].get<0> ().m_timedOutInterests ++;
461 for (pit::Entry::out_container::const_iterator i = entry->GetOutgoing ().begin ();
462 i != entry->GetOutgoing ().end ();
465 m_stats[i->m_face].get<0> ().m_outTimedOutInterests ++;
virtual void Print(std::ostream &os) const
Print current trace data.
virtual ~L3RateTracer()
Destructor.
L3RateTracer(boost::shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer.
Base class for network-layer (incoming/outgoing Interests and Data) tracing of NDN stack...
virtual void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)