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-aggregate-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-aggregate-tracer.h"
22 
23 #include "ns3/node.h"
24 #include "ns3/packet.h"
25 #include "ns3/config.h"
26 #include "ns3/callback.h"
27 #include "ns3/ndn-app.h"
28 #include "ns3/ndn-face.h"
29 #include "ns3/ndn-interest.h"
30 #include "ns3/ndn-data.h"
31 #include "ns3/ndn-pit-entry.h"
32 
33 #include "ns3/simulator.h"
34 #include "ns3/node-list.h"
35 #include "ns3/log.h"
36 
37 #include <fstream>
38 
39 NS_LOG_COMPONENT_DEFINE ("ndn.L3AggregateTracer");
40 
41 namespace ns3 {
42 namespace ndn {
43 
44 static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > > > g_tracers;
45 
46 template<class T>
47 static inline void
48 NullDeleter (T *ptr)
49 {
50 }
51 
52 void
54 {
55  g_tracers.clear ();
56 }
57 
58 void
59 L3AggregateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
60 {
61  using namespace boost;
62  using namespace std;
63 
64  std::list<Ptr<L3AggregateTracer> > 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<L3AggregateTracer> 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 L3AggregateTracer::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<L3AggregateTracer> > 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<L3AggregateTracer> 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 L3AggregateTracer::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<L3AggregateTracer> > 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<L3AggregateTracer> 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<L3AggregateTracer>
188  boost::shared_ptr<std::ostream> outputStream,
189  Time averagingPeriod/* = Seconds (0.5)*/)
190 {
191  NS_LOG_DEBUG ("Node: " << node->GetId ());
192 
193  Ptr<L3AggregateTracer> trace = Create<L3AggregateTracer> (outputStream, node);
194  trace->SetAveragingPeriod (averagingPeriod);
195 
196  return trace;
197 }
198 
199 L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
200  : L3Tracer (node)
201  , m_os (os)
202 {
203  Reset ();
204 }
205 
206 L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
207  : L3Tracer (node)
208  , m_os (os)
209 {
210  Reset ();
211 }
212 
214 {
215 };
216 
217 void
218 L3AggregateTracer::SetAveragingPeriod (const Time &period)
219 {
220  m_period = period;
221  m_printEvent.Cancel ();
222  m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
223 }
224 
225 void
226 L3AggregateTracer::PeriodicPrinter ()
227 {
228  Print (*m_os);
229  Reset ();
230 
231  m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
232 }
233 
234 void
235 L3AggregateTracer::PrintHeader (std::ostream &os) const
236 {
237  os << "Time" << "\t"
238 
239  << "Node" << "\t"
240  << "FaceId" << "\t"
241  << "FaceDescr" << "\t"
242 
243  << "Type" << "\t"
244  << "Packets" << "\t"
245  << "Kilobytes";
246 }
247 
248 void
249 L3AggregateTracer::Reset ()
250 {
251  for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
252  stats != m_stats.end ();
253  stats++)
254  {
255  stats->second.get<0> ().Reset ();
256  stats->second.get<1> ().Reset ();
257  }
258 }
259 
260 
261 #define STATS(INDEX) stats->second.get<INDEX> ()
262 
263 #define PRINTER(printName, fieldName) \
264  os << time.ToDouble (Time::S) << "\t" \
265  << m_node << "\t"; \
266  if (stats->first) \
267  { \
268  os \
269  << stats->first->GetId () << "\t" \
270  << *stats->first << "\t"; \
271  } \
272  else \
273  { \
274  os << "-1\tall\t"; \
275  } \
276  os \
277  << printName << "\t" \
278  << STATS(0).fieldName << "\t" \
279  << STATS(1).fieldName / 1024.0 << "\n";
280 
281 
282 void
283 L3AggregateTracer::Print (std::ostream &os) const
284 {
285  Time time = Simulator::Now ();
286 
287  for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
288  stats != m_stats.end ();
289  stats++)
290  {
291  if (!stats->first)
292  continue;
293 
294  PRINTER ("InInterests", m_inInterests);
295  PRINTER ("OutInterests", m_outInterests);
296  PRINTER ("DropInterests", m_dropInterests);
297 
298  PRINTER ("InNacks", m_inNacks);
299  PRINTER ("OutNacks", m_outNacks);
300  PRINTER ("DropNacks", m_dropNacks);
301 
302  PRINTER ("InData", m_inData);
303  PRINTER ("OutData", m_outData);
304  PRINTER ("DropData", m_dropData);
305  }
306 
307  {
308  std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.find (Ptr<const Face> (0));
309  if (stats != m_stats.end ())
310  {
311  PRINTER ("SatisfiedInterests", m_satisfiedInterests);
312  PRINTER ("TimedOutInterests", m_timedOutInterests);
313  }
314  }
315 }
316 
317 void
318 L3AggregateTracer::OutInterests (Ptr<const Interest> interest, Ptr<const Face> face)
319 {
320  m_stats[face].get<0> ().m_outInterests ++;
321  if (interest->GetWire ())
322  {
323  m_stats[face].get<1> ().m_outInterests += interest->GetWire ()->GetSize ();
324  }
325 }
326 
327 void
328 L3AggregateTracer::InInterests (Ptr<const Interest> interest, Ptr<const Face> face)
329 {
330  m_stats[face].get<0> ().m_inInterests ++;
331  if (interest->GetWire ())
332  {
333  m_stats[face].get<1> ().m_inInterests += interest->GetWire ()->GetSize ();
334  }
335 }
336 
337 void
338 L3AggregateTracer::DropInterests (Ptr<const Interest> interest, Ptr<const Face> face)
339 {
340  m_stats[face].get<0> ().m_dropInterests ++;
341  if (interest->GetWire ())
342  {
343  m_stats[face].get<1> ().m_dropInterests += interest->GetWire ()->GetSize ();
344  }
345 }
346 
347 void
348 L3AggregateTracer::OutNacks (Ptr<const Interest> nack, Ptr<const Face> face)
349 {
350  m_stats[face].get<0> ().m_outNacks ++;
351  if (nack->GetWire ())
352  {
353  m_stats[face].get<1> ().m_outNacks += nack->GetWire ()->GetSize ();
354  }
355 }
356 
357 void
358 L3AggregateTracer::InNacks (Ptr<const Interest> nack, Ptr<const Face> face)
359 {
360  m_stats[face].get<0> ().m_inNacks ++;
361  if (nack->GetWire ())
362  {
363  m_stats[face].get<1> ().m_inNacks += nack->GetWire ()->GetSize ();
364  }
365 }
366 
367 void
368 L3AggregateTracer::DropNacks (Ptr<const Interest> nack, Ptr<const Face> face)
369 {
370  m_stats[face].get<0> ().m_dropNacks ++;
371  if (nack->GetWire ())
372  {
373  m_stats[face].get<1> ().m_dropNacks += nack->GetWire ()->GetSize ();
374  }
375 }
376 
377 void
378 L3AggregateTracer::OutData (Ptr<const Data> data,
379  bool fromCache, Ptr<const Face> face)
380 {
381  m_stats[face].get<0> ().m_outData ++;
382  if (data->GetWire ())
383  {
384  m_stats[face].get<1> ().m_outData += data->GetWire ()->GetSize ();
385  }
386 }
387 
388 void
389 L3AggregateTracer::InData (Ptr<const Data> data,
390  Ptr<const Face> face)
391 {
392  m_stats[face].get<0> ().m_inData ++;
393  if (data->GetWire ())
394  {
395  m_stats[face].get<1> ().m_inData += data->GetWire ()->GetSize ();
396  }
397 }
398 
399 void
400 L3AggregateTracer::DropData (Ptr<const Data> data,
401  Ptr<const Face> face)
402 {
403  m_stats[face].get<0> ().m_dropData ++;
404  if (data->GetWire ())
405  {
406  m_stats[face].get<1> ().m_dropData += data->GetWire ()->GetSize ();
407  }
408 }
409 
410 void
411 L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>)
412 {
413  m_stats[0].get<0> ().m_satisfiedInterests ++;
414  // no "size" stats
415 }
416 
417 void
418 L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>)
419 {
420  m_stats[0].get<0> ().m_timedOutInterests ++;
421  // no "size" stats
422 }
423 
424 } // namespace ndn
425 } // namespace ns3
virtual void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)
L3AggregateTracer(boost::shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer.
static void Install(const NodeContainer &nodes, const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on the selected simulation nodes.
static void InstallAll(const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on all simulation nodes.
Base class for network-layer (incoming/outgoing Interests and Data) tracing of NDN stack...
Definition: ndn-l3-tracer.h:52
virtual ~L3AggregateTracer()
Destructor.
virtual void Print(std::ostream &os) const
Print current trace data.
static void Destroy()
Explicit request to remove all statically created tracers.