22 #include "ns3/packet.h" 
   23 #include "ns3/config.h" 
   24 #include "ns3/names.h" 
   25 #include "ns3/callback.h" 
   29 #include "ns3/simulator.h" 
   30 #include "ns3/node-list.h" 
   33 #include <boost/lexical_cast.hpp> 
   37 NS_LOG_COMPONENT_DEFINE(
"ndn.CsTracer");
 
   42 static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<CsTracer>>>> 
g_tracers;
 
   53   using namespace boost;
 
   56   std::list<Ptr<CsTracer>> tracers;
 
   57   shared_ptr<std::ostream> outputStream;
 
   59     shared_ptr<std::ofstream> os(
new std::ofstream());
 
   60     os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
 
   63       NS_LOG_ERROR(
"File " << file << 
" cannot be opened for writing. Tracing disabled");
 
   70     outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
 
   73   for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
 
   74     Ptr<CsTracer> trace = 
Install(*node, outputStream, averagingPeriod);
 
   75     tracers.push_back(trace);
 
   78   if (tracers.size() > 0) {
 
   80     tracers.front()->PrintHeader(*outputStream);
 
   81     *outputStream << 
"\n";
 
   84   g_tracers.push_back(std::make_tuple(outputStream, tracers));
 
   89                   Time averagingPeriod )
 
   91   using namespace boost;
 
   94   std::list<Ptr<CsTracer>> tracers;
 
   95   shared_ptr<std::ostream> outputStream;
 
   97     shared_ptr<std::ofstream> os(
new std::ofstream());
 
   98     os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
 
  100     if (!os->is_open()) {
 
  101       NS_LOG_ERROR(
"File " << file << 
" cannot be opened for writing. Tracing disabled");
 
  108     outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
 
  111   for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
 
  112     Ptr<CsTracer> trace = 
Install(*node, outputStream, averagingPeriod);
 
  113     tracers.push_back(trace);
 
  116   if (tracers.size() > 0) {
 
  118     tracers.front()->PrintHeader(*outputStream);
 
  119     *outputStream << 
"\n";
 
  122   g_tracers.push_back(std::make_tuple(outputStream, tracers));
 
  127                   Time averagingPeriod )
 
  129   using namespace boost;
 
  132   std::list<Ptr<CsTracer>> tracers;
 
  133   shared_ptr<std::ostream> outputStream;
 
  135     shared_ptr<std::ofstream> os(
new std::ofstream());
 
  136     os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
 
  138     if (!os->is_open()) {
 
  139       NS_LOG_ERROR(
"File " << file << 
" cannot be opened for writing. Tracing disabled");
 
  146     outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
 
  149   Ptr<CsTracer> trace = 
Install(node, outputStream, averagingPeriod);
 
  150   tracers.push_back(trace);
 
  152   if (tracers.size() > 0) {
 
  154     tracers.front()->PrintHeader(*outputStream);
 
  155     *outputStream << 
"\n";
 
  158   g_tracers.push_back(std::make_tuple(outputStream, tracers));
 
  163                   Time averagingPeriod )
 
  165   NS_LOG_DEBUG(
"Node: " << node->GetId());
 
  167   Ptr<CsTracer> trace = Create<CsTracer>(outputStream, node);
 
  168   trace->SetAveragingPeriod(averagingPeriod);
 
  181   m_node = boost::lexical_cast<std::string>(m_nodePtr->GetId());
 
  185   std::string name = Names::FindName(node);
 
  203   Ptr<ContentStore> cs = m_nodePtr->GetObject<
ContentStore>();
 
  204   cs->TraceConnectWithoutContext(
"CacheHits", MakeCallback(&CsTracer::CacheHits, 
this));
 
  205   cs->TraceConnectWithoutContext(
"CacheMisses", MakeCallback(&CsTracer::CacheMisses, 
this));
 
  211 CsTracer::SetAveragingPeriod(
const Time& period)
 
  214   m_printEvent.Cancel();
 
  215   m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, 
this);
 
  219 CsTracer::PeriodicPrinter()
 
  224   m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, 
this);
 
  248 #define PRINTER(printName, fieldName)                                                              \ 
  249   os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << printName << "\t" << m_stats.fieldName \ 
  255   Time time = Simulator::Now();
 
  257   PRINTER(
"CacheHits", m_cacheHits);
 
  258   PRINTER(
"CacheMisses", m_cacheMisses);
 
  262 CsTracer::CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>)
 
  264   m_stats.m_cacheHits++;
 
  268 CsTracer::CacheMisses(shared_ptr<const Interest>)
 
  270   m_stats.m_cacheMisses++;
 
static void InstallAll(const std::string &file, Time averagingPeriod=Seconds(0.5))
Helper method to install tracers on all simulation nodes. 
 
void Print(std::ostream &os) const 
Print current trace data. 
 
#define PRINTER(printName, fieldName)
 
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 Destroy()
Explicit request to remove all statically created tracers. 
 
static std::list< std::tuple< shared_ptr< std::ostream >, std::list< Ptr< AppDelayTracer > > > > g_tracers
 
void PrintHeader(std::ostream &os) const 
Print head of the trace (e.g., for post-processing) 
 
CsTracer(shared_ptr< std::ostream > os, Ptr< Node > node)
Trace constructor that attaches to the node using node pointer. 
 
Base class for NDN content store.