22 #include "ns3/packet.h" 23 #include "ns3/config.h" 24 #include "ns3/callback.h" 25 #include "ns3/simulator.h" 27 #include "ns3/node-list.h" 32 #include <boost/lexical_cast.hpp> 34 NS_LOG_COMPONENT_DEFINE(
"ndn.L3RateTracer");
39 static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer>>>>
51 std::list<Ptr<L3RateTracer>> tracers;
52 shared_ptr<std::ostream> outputStream;
54 shared_ptr<std::ofstream> os(
new std::ofstream());
55 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
58 NS_LOG_ERROR(
"File " << file <<
" cannot be opened for writing. Tracing disabled");
65 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
68 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
69 Ptr<L3RateTracer> trace =
Install(*node, outputStream, averagingPeriod);
70 tracers.push_back(trace);
73 if (tracers.size() > 0) {
75 tracers.front()->PrintHeader(*outputStream);
76 *outputStream <<
"\n";
79 g_tracers.push_back(std::make_tuple(outputStream, tracers));
84 Time averagingPeriod )
86 using namespace boost;
89 std::list<Ptr<L3RateTracer>> tracers;
90 shared_ptr<std::ostream> outputStream;
92 shared_ptr<std::ofstream> os(
new std::ofstream());
93 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
96 NS_LOG_ERROR(
"File " << file <<
" cannot be opened for writing. Tracing disabled");
103 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
106 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
107 Ptr<L3RateTracer> trace =
Install(*node, outputStream, averagingPeriod);
108 tracers.push_back(trace);
111 if (tracers.size() > 0) {
113 tracers.front()->PrintHeader(*outputStream);
114 *outputStream <<
"\n";
117 g_tracers.push_back(std::make_tuple(outputStream, tracers));
122 Time averagingPeriod )
124 using namespace boost;
127 std::list<Ptr<L3RateTracer>> tracers;
128 shared_ptr<std::ostream> outputStream;
130 shared_ptr<std::ofstream> os(
new std::ofstream());
131 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
133 if (!os->is_open()) {
134 NS_LOG_ERROR(
"File " << file <<
" cannot be opened for writing. Tracing disabled");
141 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
144 Ptr<L3RateTracer> trace =
Install(node, outputStream, averagingPeriod);
145 tracers.push_back(trace);
147 if (tracers.size() > 0) {
149 tracers.front()->PrintHeader(*outputStream);
150 *outputStream <<
"\n";
153 g_tracers.push_back(std::make_tuple(outputStream, tracers));
158 Time averagingPeriod )
160 NS_LOG_DEBUG(
"Node: " << node->GetId());
162 Ptr<L3RateTracer> trace = Create<L3RateTracer>(outputStream, node);
163 trace->SetAveragingPeriod(averagingPeriod);
172 SetAveragingPeriod(Seconds(1.0));
179 SetAveragingPeriod(Seconds(1.0));
184 m_printEvent.Cancel();
188 L3RateTracer::SetAveragingPeriod(
const Time& period)
191 m_printEvent.Cancel();
192 m_printEvent = Simulator::Schedule(m_period, &L3RateTracer::PeriodicPrinter,
this);
196 L3RateTracer::PeriodicPrinter()
201 m_printEvent = Simulator::Schedule(m_period, &L3RateTracer::PeriodicPrinter,
this);
229 L3RateTracer::Reset()
231 for (
auto& stats : m_stats) {
232 std::get<0>(stats.second).Reset();
233 std::get<1>(stats.second).Reset();
239 #define STATS(INDEX) std::get<INDEX>(stats.second) 240 #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S) 242 #define PRINTER(printName, fieldName) \ 243 STATS(2).fieldName = \ 244 alpha * RATE(0, fieldName) + (1 - alpha) * STATS(2).fieldName; \ 245 STATS(3).fieldName = alpha * RATE(1, fieldName) / 1024.0 \ 246 + (1 - alpha) * STATS(3).fieldName; \ 248 os << time.ToDouble(Time::S) << "\t" << m_node << "\t"; \ 249 if (stats.first != nullptr) { \ 250 os << stats.first->getId() << "\t" << stats.first->getLocalUri() << "\t"; \ 255 os << printName << "\t" << STATS(2).fieldName << "\t" << STATS(3).fieldName << "\t" \ 256 << STATS(0).fieldName << "\t" << STATS(1).fieldName / 1024.0 << "\n"; 261 Time
time = Simulator::Now();
263 for (
auto& stats : m_stats) {
264 if (stats.first ==
nullptr)
267 PRINTER(
"InInterests", m_inInterests);
268 PRINTER(
"OutInterests", m_outInterests);
273 PRINTER(
"InSatisfiedInterests", m_satisfiedInterests);
274 PRINTER(
"InTimedOutInterests", m_timedOutInterests);
276 PRINTER(
"OutSatisfiedInterests", m_outSatisfiedInterests);
277 PRINTER(
"OutTimedOutInterests", m_outTimedOutInterests);
281 auto i = m_stats.find(
nullptr);
282 if (i != m_stats.end()) {
284 PRINTER(
"SatisfiedInterests", m_satisfiedInterests);
285 PRINTER(
"TimedOutInterests", m_timedOutInterests);
293 std::get<0>(m_stats[face.shared_from_this()]).m_outInterests++;
294 if (interest.hasWire()) {
295 std::get<1>(m_stats[face.shared_from_this()]).m_outInterests +=
296 interest.wireEncode().size();
303 std::get<0>(m_stats[face.shared_from_this()]).m_inInterests++;
304 if (interest.hasWire()) {
305 std::get<1>(m_stats[face.shared_from_this()]).m_inInterests +=
306 interest.wireEncode().size();
313 std::get<0>(m_stats[face.shared_from_this()]).m_outData++;
314 if (data.hasWire()) {
315 std::get<1>(m_stats[face.shared_from_this()]).m_outData +=
316 data.wireEncode().size();
323 std::get<0>(m_stats[face.shared_from_this()]).m_inData++;
324 if (data.hasWire()) {
325 std::get<1>(m_stats[face.shared_from_this()]).m_inData +=
326 data.wireEncode().size();
333 std::get<0>(m_stats[
nullptr]).m_satisfiedInterests++;
337 std::get<0>(m_stats[in.getFace()]).m_satisfiedInterests ++;
341 std::get<0>(m_stats[out.getFace()]).m_outSatisfiedInterests ++;
348 std::get<0>(m_stats[
nullptr]).m_timedOutInterests++;
352 std::get<0>(m_stats[in.getFace()]).m_timedOutInterests++;
356 std::get<0>(m_stats[out.getFace()]).m_outTimedOutInterests++;
virtual ~L3RateTracer()
Destructor.
Copyright (c) 2011-2015 Regents of the University of California.
static void Destroy()
Explicit request to remove all statically created tracers.
Copyright (c) 2013-2015 Regents of the University of California.
virtual void SatisfiedInterests(const nfd::pit::Entry &, const Face &, const Data &)
Base class for network-layer (incoming/outgoing Interests and Data) tracing of NDN stack...
const InRecordCollection & getInRecords() const
static std::list< std::tuple< shared_ptr< std::ostream >, std::list< Ptr< AppDelayTracer > > > > g_tracers
Copyright (c) 2011-2015 Regents of the University of California.
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.
virtual void Print(std::ostream &os) const
Print current trace data.
virtual void InInterests(const Interest &interest, const Face &face)
const OutRecordCollection & getOutRecords() const
virtual void OutData(const Data &data, const Face &face)
virtual void TimedOutInterests(const nfd::pit::Entry &)
#define PRINTER(printName, fieldName)
L3RateTracer(shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer.
static void InstallAll(const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on all simulation nodes.
virtual void PrintHeader(std::ostream &os) const
Print head of the trace (e.g., for post-processing)
virtual void OutInterests(const Interest &interest, const Face &face)
virtual void InData(const Data &data, const Face &face)