NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-l3-rate-tracer.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2011 UCLA
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19  */
20 
21 #include "ndn-l3-rate-tracer.h"
22 #include "ns3/node.h"
23 #include "ns3/packet.h"
24 #include "ns3/config.h"
25 #include "ns3/callback.h"
26 #include "ns3/simulator.h"
27 #include "ns3/log.h"
28 #include "ns3/node-list.h"
29 
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"
35 
36 #include <fstream>
37 #include <boost/lexical_cast.hpp>
38 
39 using namespace boost;
40 using namespace std;
41 
42 NS_LOG_COMPONENT_DEFINE ("ndn.L3RateTracer");
43 
44 namespace ns3 {
45 namespace ndn {
46 
47 static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer> > > > g_tracers;
48 
49 template<class T>
50 static inline void
51 NullDeleter (T *ptr)
52 {
53 }
54 
55 void
56 L3RateTracer::Destroy ()
57 {
58  g_tracers.clear ();
59 }
60 
61 void
62 L3RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
63 {
64  std::list<Ptr<L3RateTracer> > tracers;
65  boost::shared_ptr<std::ostream> outputStream;
66  if (file != "-")
67  {
68  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
69  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
70 
71  if (!os->is_open ())
72  {
73  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
74  return;
75  }
76 
77  outputStream = os;
78  }
79  else
80  {
81  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
82  }
83 
84  for (NodeList::Iterator node = NodeList::Begin ();
85  node != NodeList::End ();
86  node++)
87  {
88  Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod);
89  tracers.push_back (trace);
90  }
91 
92  if (tracers.size () > 0)
93  {
94  // *m_l3RateTrace << "# "; // not necessary for R's read.table
95  tracers.front ()->PrintHeader (*outputStream);
96  *outputStream << "\n";
97  }
98 
99  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
100 }
101 
102 void
103 L3RateTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
104 {
105  using namespace boost;
106  using namespace std;
107 
108  std::list<Ptr<L3RateTracer> > tracers;
109  boost::shared_ptr<std::ostream> outputStream;
110  if (file != "-")
111  {
112  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
113  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
114 
115  if (!os->is_open ())
116  {
117  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
118  return;
119  }
120 
121  outputStream = os;
122  }
123  else
124  {
125  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
126  }
127 
128  for (NodeContainer::Iterator node = nodes.Begin ();
129  node != nodes.End ();
130  node++)
131  {
132  Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod);
133  tracers.push_back (trace);
134  }
135 
136  if (tracers.size () > 0)
137  {
138  // *m_l3RateTrace << "# "; // not necessary for R's read.table
139  tracers.front ()->PrintHeader (*outputStream);
140  *outputStream << "\n";
141  }
142 
143  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
144 }
145 
146 void
147 L3RateTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
148 {
149  using namespace boost;
150  using namespace std;
151 
152  std::list<Ptr<L3RateTracer> > tracers;
153  boost::shared_ptr<std::ostream> outputStream;
154  if (file != "-")
155  {
156  boost::shared_ptr<std::ofstream> os (new std::ofstream ());
157  os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
158 
159  if (!os->is_open ())
160  {
161  NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
162  return;
163  }
164 
165  outputStream = os;
166  }
167  else
168  {
169  outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
170  }
171 
172  Ptr<L3RateTracer> trace = Install (node, outputStream, averagingPeriod);
173  tracers.push_back (trace);
174 
175  if (tracers.size () > 0)
176  {
177  // *m_l3RateTrace << "# "; // not necessary for R's read.table
178  tracers.front ()->PrintHeader (*outputStream);
179  *outputStream << "\n";
180  }
181 
182  g_tracers.push_back (boost::make_tuple (outputStream, tracers));
183 }
184 
185 
186 Ptr<L3RateTracer>
187 L3RateTracer::Install (Ptr<Node> node,
188  boost::shared_ptr<std::ostream> outputStream,
189  Time averagingPeriod/* = Seconds (0.5)*/)
190 {
191  NS_LOG_DEBUG ("Node: " << node->GetId ());
192 
193  Ptr<L3RateTracer> trace = Create<L3RateTracer> (outputStream, node);
194  trace->SetAveragingPeriod (averagingPeriod);
195 
196  return trace;
197 }
198 
199 
200 L3RateTracer::L3RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
201  : L3Tracer (node)
202  , m_os (os)
203 {
204  SetAveragingPeriod (Seconds (1.0));
205 }
206 
207 L3RateTracer::L3RateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
208  : L3Tracer (node)
209  , m_os (os)
210 {
211  SetAveragingPeriod (Seconds (1.0));
212 }
213 
215 {
216  m_printEvent.Cancel ();
217 }
218 
219 void
220 L3RateTracer::SetAveragingPeriod (const Time &period)
221 {
222  m_period = period;
223  m_printEvent.Cancel ();
224  m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter, this);
225 }
226 
227 void
228 L3RateTracer::PeriodicPrinter ()
229 {
230  Print (*m_os);
231  Reset ();
232 
233  m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter, this);
234 }
235 
236 void
237 L3RateTracer::PrintHeader (std::ostream &os) const
238 {
239  os << "Time" << "\t"
240 
241  << "Node" << "\t"
242  << "FaceId" << "\t"
243  << "FaceDescr" << "\t"
244 
245  << "Type" << "\t"
246  << "Packets" << "\t"
247  << "Kilobytes" << "\t"
248  << "PacketRaw" << "\t"
249  << "KilobytesRaw";
250 }
251 
252 void
253 L3RateTracer::Reset ()
254 {
255  for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
256  stats != m_stats.end ();
257  stats++)
258  {
259  stats->second.get<0> ().Reset ();
260  stats->second.get<1> ().Reset ();
261  }
262 }
263 
264 const double alpha = 0.8;
265 
266 #define STATS(INDEX) stats->second.get<INDEX> ()
267 #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
268 
269 #define PRINTER(printName, fieldName) \
270  STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
271  STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
272  \
273  os << time.ToDouble (Time::S) << "\t" \
274  << m_node << "\t"; \
275  if (stats->first) \
276  { \
277  os \
278  << stats->first->GetId () << "\t" \
279  << *stats->first << "\t"; \
280  } \
281  else \
282  { \
283  os << "-1\tall\t"; \
284  } \
285  os \
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";
291 
292 void
293 L3RateTracer::Print (std::ostream &os) const
294 {
295  Time time = Simulator::Now ();
296 
297  for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
298  stats != m_stats.end ();
299  stats++)
300  {
301  if (!stats->first)
302  continue;
303 
304  PRINTER ("InInterests", m_inInterests);
305  PRINTER ("OutInterests", m_outInterests);
306  PRINTER ("DropInterests", m_dropInterests);
307 
308  PRINTER ("InNacks", m_inNacks);
309  PRINTER ("OutNacks", m_outNacks);
310  PRINTER ("DropNacks", m_dropNacks);
311 
312  PRINTER ("InData", m_inData);
313  PRINTER ("OutData", m_outData);
314  PRINTER ("DropData", m_dropData);
315 
316  PRINTER ("InSatisfiedInterests", m_satisfiedInterests);
317  PRINTER ("InTimedOutInterests", m_timedOutInterests);
318 
319  PRINTER ("OutSatisfiedInterests", m_outSatisfiedInterests);
320  PRINTER ("OutTimedOutInterests", m_outTimedOutInterests);
321  }
322 
323  {
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 ())
326  {
327  PRINTER ("SatisfiedInterests", m_satisfiedInterests);
328  PRINTER ("TimedOutInterests", m_timedOutInterests);
329  }
330  }
331 }
332 
333 
334 void
335 L3RateTracer::OutInterests (Ptr<const Interest> interest, Ptr<const Face> face)
336 {
337  m_stats[face].get<0> ().m_outInterests ++;
338  if (interest->GetWire ())
339  {
340  m_stats[face].get<1> ().m_outInterests += interest->GetWire ()->GetSize ();
341  }
342 }
343 
344 void
345 L3RateTracer::InInterests (Ptr<const Interest> interest, Ptr<const Face> face)
346 {
347  m_stats[face].get<0> ().m_inInterests ++;
348  if (interest->GetWire ())
349  {
350  m_stats[face].get<1> ().m_inInterests += interest->GetWire ()->GetSize ();
351  }
352 }
353 
354 void
355 L3RateTracer::DropInterests (Ptr<const Interest> interest, Ptr<const Face> face)
356 {
357  m_stats[face].get<0> ().m_dropInterests ++;
358  if (interest->GetWire ())
359  {
360  m_stats[face].get<1> ().m_dropInterests += interest->GetWire ()->GetSize ();
361  }
362 }
363 
364 void
365 L3RateTracer::OutNacks (Ptr<const Interest> interest, Ptr<const Face> face)
366 {
367  m_stats[face].get<0> ().m_outNacks ++;
368  if (interest->GetWire ())
369  {
370  m_stats[face].get<1> ().m_outNacks += interest->GetWire ()->GetSize ();
371  }
372 }
373 
374 void
375 L3RateTracer::InNacks (Ptr<const Interest> interest, Ptr<const Face> face)
376 {
377  m_stats[face].get<0> ().m_inNacks ++;
378  if (interest->GetWire ())
379  {
380  m_stats[face].get<1> ().m_inNacks += interest->GetWire ()->GetSize ();
381  }
382 }
383 
384 void
385 L3RateTracer::DropNacks (Ptr<const Interest> interest, Ptr<const Face> face)
386 {
387  m_stats[face].get<0> ().m_dropNacks ++;
388  if (interest->GetWire ())
389  {
390  m_stats[face].get<1> ().m_dropNacks += interest->GetWire ()->GetSize ();
391  }
392 }
393 
394 void
395 L3RateTracer::OutData (Ptr<const Data> data,
396  bool fromCache, Ptr<const Face> face)
397 {
398  m_stats[face].get<0> ().m_outData ++;
399  if (data->GetWire ())
400  {
401  m_stats[face].get<1> ().m_outData += data->GetWire ()->GetSize ();
402  }
403 }
404 
405 void
406 L3RateTracer::InData (Ptr<const Data> data,
407  Ptr<const Face> face)
408 {
409  m_stats[face].get<0> ().m_inData ++;
410  if (data->GetWire ())
411  {
412  m_stats[face].get<1> ().m_inData += data->GetWire ()->GetSize ();
413  }
414 }
415 
416 void
417 L3RateTracer::DropData (Ptr<const Data> data,
418  Ptr<const Face> face)
419 {
420  m_stats[face].get<0> ().m_dropData ++;
421  if (data->GetWire ())
422  {
423  m_stats[face].get<1> ().m_dropData += data->GetWire ()->GetSize ();
424  }
425 }
426 
427 void
428 L3RateTracer::SatisfiedInterests (Ptr<const pit::Entry> entry)
429 {
430  m_stats[0].get<0> ().m_satisfiedInterests ++;
431  // no "size" stats
432 
433  for (pit::Entry::in_container::const_iterator i = entry->GetIncoming ().begin ();
434  i != entry->GetIncoming ().end ();
435  i++)
436  {
437  m_stats[i->m_face].get<0> ().m_satisfiedInterests ++;
438 }
439 
440  for (pit::Entry::out_container::const_iterator i = entry->GetOutgoing ().begin ();
441  i != entry->GetOutgoing ().end ();
442  i++)
443  {
444  m_stats[i->m_face].get<0> ().m_outSatisfiedInterests ++;
445  }
446 }
447 
448 void
449 L3RateTracer::TimedOutInterests (Ptr<const pit::Entry> entry)
450 {
451  m_stats[0].get<0> ().m_timedOutInterests ++;
452  // no "size" stats
453 
454  for (pit::Entry::in_container::const_iterator i = entry->GetIncoming ().begin ();
455  i != entry->GetIncoming ().end ();
456  i++)
457  {
458  m_stats[i->m_face].get<0> ().m_timedOutInterests ++;
459 }
460 
461  for (pit::Entry::out_container::const_iterator i = entry->GetOutgoing ().begin ();
462  i != entry->GetOutgoing ().end ();
463  i++)
464  {
465  m_stats[i->m_face].get<0> ().m_outTimedOutInterests ++;
466  }
467 }
468 
469 
470 } // namespace ndn
471 } // namespace ns3
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...
Definition: ndn-l3-tracer.h:52
virtual void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)