NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
l2-rate-tracer.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "l2-rate-tracer.hpp"
21 
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/node-list.h"
28 #include "ns3/node.h"
29 #include "ns3/log.h"
30 
31 #include <boost/lexical_cast.hpp>
32 #include <fstream>
33 
34 NS_LOG_COMPONENT_DEFINE("L2RateTracer");
35 
36 namespace ns3 {
37 
38 static std::list<std::tuple<std::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer>>>>
40 
41 void
43 {
44  g_tracers.clear();
45 }
46 
47 void
48 L2RateTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
49 {
50  std::list<Ptr<L2RateTracer>> tracers;
51  std::shared_ptr<std::ostream> outputStream;
52  if (file != "-") {
53  std::shared_ptr<std::ofstream> os(new std::ofstream());
54  os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
55 
56  if (!os->is_open()) {
57  NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
58  return;
59  }
60 
61  outputStream = os;
62  }
63  else {
64  outputStream = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
65  }
66 
67  for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
68  NS_LOG_DEBUG("Node: " << boost::lexical_cast<std::string>((*node)->GetId()));
69 
70  Ptr<L2RateTracer> trace = Create<L2RateTracer>(outputStream, *node);
71  trace->SetAveragingPeriod(averagingPeriod);
72  tracers.push_back(trace);
73  }
74 
75  if (tracers.size() > 0) {
76  // *m_l3RateTrace << "# "; // not necessary for R's read.table
77  tracers.front()->PrintHeader(*outputStream);
78  *outputStream << "\n";
79  }
80 
81  g_tracers.push_back(std::make_tuple(outputStream, tracers));
82 }
83 
84 L2RateTracer::L2RateTracer(std::shared_ptr<std::ostream> os, Ptr<Node> node)
85  : L2Tracer(node)
86  , m_os(os)
87 {
88  SetAveragingPeriod(Seconds(1.0));
89 }
90 
92 {
93  m_printEvent.Cancel();
94 }
95 
96 void
98 {
99  m_period = period;
100  m_printEvent.Cancel();
101  m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
102 }
103 
104 void
105 L2RateTracer::PeriodicPrinter()
106 {
107  Print(*m_os);
108  Reset();
109 
110  m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
111 }
112 
113 void
114 L2RateTracer::PrintHeader(std::ostream& os) const
115 {
116  os << "Time"
117  << "\t"
118 
119  << "Node"
120  << "\t"
121  << "Interface"
122  << "\t"
123 
124  << "Type"
125  << "\t"
126  << "Packets"
127  << "\t"
128  << "Kilobytes"
129  << "\t"
130  << "PacketsRaw"
131  << "\t"
132  << "KilobytesRaw";
133 }
134 
135 void
136 L2RateTracer::Reset()
137 {
138  std::get<0>(m_stats).Reset();
139  std::get<1>(m_stats).Reset();
140 }
141 
142 const double alpha = 0.8;
143 
144 #define STATS(INDEX) std::get<INDEX>(m_stats)
145 #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S)
146 
147 #define PRINTER(printName, fieldName, interface) \
148  STATS(2).fieldName = \
149  /*new value*/ alpha * RATE(0, fieldName) + /*old value*/ (1 - alpha) * STATS(2).fieldName; \
150  STATS(3).fieldName = /*new value*/ alpha * RATE(1, fieldName) / 1024.0 \
151  + /*old value*/ (1 - alpha) * STATS(3).fieldName; \
152  \
153  os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << interface << "\t" << printName << "\t" \
154  << STATS(2).fieldName << "\t" << STATS(3).fieldName << "\t" << STATS(0).fieldName << "\t" \
155  << STATS(1).fieldName / 1024.0 << "\n";
156 
157 void
158 L2RateTracer::Print(std::ostream& os) const
159 {
160  Time time = Simulator::Now();
161 
162  PRINTER("Drop", m_drop, "combined");
163 }
164 
165 void
166 L2RateTracer::Drop(Ptr<const Packet> packet)
167 {
168  // no interface information... this should be part of this L2Tracer object data
169 
170  std::get<0>(m_stats).m_drop++;
171  std::get<1>(m_stats).m_drop += packet->GetSize();
172 }
173 
174 } // namespace ns3
virtual void PrintHeader(std::ostream &os) const
Link-layer tracer.
Definition: l2-tracer.hpp:39
#define PRINTER(printName, fieldName, interface)
const double alpha
static std::list< std::tuple< std::shared_ptr< std::ostream >, std::list< Ptr< L2RateTracer > > > > g_tracers
static void InstallAll(const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on all simulation nodes.
Copyright (c) 2011-2015 Regents of the University of California.
static void Destroy()
Explicit request to remove all statically created tracers.
void SetAveragingPeriod(const Time &period)
virtual void Drop(Ptr< const Packet >)
virtual void Print(std::ostream &os) const
L2RateTracer(std::shared_ptr< std::ostream > os, Ptr< Node > node)
Network layer tracer constructor.