Rate recorder trace helper

Also a small change in CCNx trace API
diff --git a/examples/synthetic-topology.cc b/examples/synthetic-topology.cc
index 0b03c7c..88662df 100644
--- a/examples/synthetic-topology.cc
+++ b/examples/synthetic-topology.cc
@@ -26,6 +26,7 @@
 #include "ns3/point-to-point-grid.h"
 #include "ns3/ipv4-global-routing-helper.h"
 #include "../utils/spring-mobility-helper.h"
+#include "../helper/ccnx-trace-helper.h"
 
 #include <sstream>
 #include "ns3/annotated-topology-reader.h"
@@ -99,11 +100,11 @@
   ApplicationContainer consumers2 = consumerHelper.Install(Names::Find<Node> ("/synthetic", "c2"));
 
   consumerHelper.SetPrefix ("/8");
-  consumerHelper.SetAttribute ("MeanRate", StringValue ("3Mbps"));
+  consumerHelper.SetAttribute ("MeanRate", StringValue ("4Mbps"));
   ApplicationContainer consumers3 = consumerHelper.Install(Names::Find<Node> ("/synthetic", "c3"));
   
   consumerHelper.SetPrefix ("/10");
-  consumerHelper.SetAttribute ("MeanRate", StringValue ("10Mbps"));
+  consumerHelper.SetAttribute ("MeanRate", StringValue ("8Mbps"));
   ApplicationContainer consumers4 = consumerHelper.Install(Names::Find<Node> ("/synthetic", "c4"));
 
   consumers.Start (Seconds (0));
@@ -139,6 +140,14 @@
 
   Simulator::Stop (finishTime);
 
+  CcnxTraceHelper traceHelper;
+  // traceHelper.EnableAggregateAppAll ("ns3::CcnxConsumer");
+  // traceHelper.EnableAggregateAppAll ("ns3::CcnxProducer");
+  // traceHelper.EnableAggregateL3All ();
+  // traceHelper.SetL3TraceFile ("trace-l3.log");
+  // traceHelper.SetAppTraceFile ("trace-app.log");
+  traceHelper.EnableRateL3All ("rate-trace.log");
+
   NS_LOG_INFO ("Run Simulation.");
   Simulator::Run ();
   Simulator::Destroy ();
diff --git a/helper/ccnx-trace-helper.cc b/helper/ccnx-trace-helper.cc
index 3e006e4..1248198 100644
--- a/helper/ccnx-trace-helper.cc
+++ b/helper/ccnx-trace-helper.cc
@@ -28,11 +28,17 @@
 #include "ns3/ccnx-face.h"
 #include "ns3/packet.h"
 #include "ns3/log.h"
+#include "ns3/assert.h"
 #include "ns3/node-list.h"
 #include "ns3/object-vector.h"
+#include "ns3/simulator.h"
+#include "ns3/names.h"
+
+#include <boost/ref.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/foreach.hpp>
 
 #include <fstream>
-#include <boost/lexical_cast.hpp>
 
 using namespace std;
 using namespace boost;
@@ -41,24 +47,46 @@
 
 namespace ns3 {
     
+CcnxAppTracer::CcnxAppTracer (const std::string &app, Ptr<Node> node, const std::string &appId)
+  : m_app (app)
+  , m_appId (appId)
+  , m_nodePtr (node)
+{
+  m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
+
+  Connect ();
+
+  string name = Names::FindName (node);
+  if (!name.empty ())
+    {
+      m_node = name;
+    }
+}
+
 CcnxAppTracer::CcnxAppTracer (const std::string &app, const std::string &node, const std::string &appId)
   : m_app (app)
   , m_appId (appId)
   , m_node (node)
 {
-  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/TransmittedInterests",
+  Connect ();
+}
+
+void
+CcnxAppTracer::Connect ()
+{
+  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+m_appId+"/$"+m_app+"/TransmittedInterests",
                    MakeCallback (&CcnxAppTracer::OutInterests, this));
 
-  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedNacks",
+  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+m_appId+"/$"+m_app+"/ReceivedNacks",
                    MakeCallback (&CcnxAppTracer::InNacks, this));
 
-  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedInterests",
+  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+m_appId+"/$"+m_app+"/ReceivedInterests",
                    MakeCallback (&CcnxAppTracer::InInterests, this));
   
-  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/TransmittedContentObjects",
+  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+m_appId+"/$"+m_app+"/TransmittedContentObjects",
                    MakeCallback (&CcnxAppTracer::OutData, this));
 
-  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+appId+"/$"+m_app+"/ReceivedContentObjects",
+  Config::Connect ("/NodeList/"+m_node+"/ApplicationList/"+m_appId+"/$"+m_app+"/ReceivedContentObjects",
                    MakeCallback (&CcnxAppTracer::InData, this));
 }
 
@@ -66,34 +94,69 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
+CcnxL3Tracer::CcnxL3Tracer (Ptr<Node> node)
+: m_nodePtr (node)
+{
+  m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
+
+  Connect ();
+
+  string name = Names::FindName (node);
+  if (!name.empty ())
+    {
+      m_node = name;
+    }
+}
+
 CcnxL3Tracer::CcnxL3Tracer (const std::string &node)
 : m_node (node)
 {
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/ForwardingStrategy/OutInterests",
+  Connect ();
+}
+
+void
+CcnxL3Tracer::Connect ()
+{
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/ForwardingStrategy/OutInterests",
                    MakeCallback (&CcnxL3Tracer::OutInterests, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InInterests",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/InInterests",
                    MakeCallback (&CcnxL3Tracer::InInterests, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropInterests",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/DropInterests",
                    MakeCallback (&CcnxL3Tracer::DropInterests, this));
 
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/OutNacks",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/OutNacks",
                    MakeCallback (&CcnxL3Tracer::OutNacks, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InNacks",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/InNacks",
                    MakeCallback (&CcnxL3Tracer::InNacks, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropNacks",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/DropNacks",
                    MakeCallback (&CcnxL3Tracer::DropNacks, this));
 
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/OutData",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/OutData",
                    MakeCallback (&CcnxL3Tracer::OutData, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/InData",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/InData",
                    MakeCallback (&CcnxL3Tracer::InData, this));
-  Config::Connect ("/NodeList/"+node+"/$ns3::CcnxL3Protocol/DropData",
+  Config::Connect ("/NodeList/"+m_node+"/$ns3::CcnxL3Protocol/DropData",
                    MakeCallback (&CcnxL3Tracer::DropData, this));
 }
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
+CcnxAggregateAppTracer::CcnxAggregateAppTracer (const std::string &app, Ptr<Node> node, const std::string &appId)
+  : CcnxAppTracer (app, node, appId)
+  , m_inInterests (0)
+  , m_outInterests (0)
+  , m_inNacks (0)
+  , m_inData (0)
+  , m_outData (0)
+
+  , m_inInterestsBytes (0)
+  , m_outInterestsBytes (0)
+  , m_inNacksBytes (0)
+  , m_inDataBytes (0)
+  , m_outDataBytes (0)
+{
+}
 
 CcnxAggregateAppTracer::CcnxAggregateAppTracer (const std::string &app, const std::string &node, const std::string &appId)
   : CcnxAppTracer (app, node, appId)
@@ -102,10 +165,32 @@
   , m_inNacks (0)
   , m_inData (0)
   , m_outData (0)
+
+  , m_inInterestsBytes (0)
+  , m_outInterestsBytes (0)
+  , m_inNacksBytes (0)
+  , m_inDataBytes (0)
+  , m_outDataBytes (0)
 {
 }
 
 void
+CcnxAggregateAppTracer::Reset ()
+{
+  m_inInterests = 0;
+  m_outInterests = 0;
+  m_inNacks = 0;
+  m_inData = 0;
+  m_outData = 0;
+
+  m_inInterestsBytes = 0;
+  m_outInterestsBytes = 0;
+  m_inNacksBytes = 0;
+  m_inDataBytes = 0;
+  m_outDataBytes = 0;
+}
+
+void
 CcnxAggregateAppTracer::PrintHeader (std::ostream &os) const
 {
   os << "NodeId" << "\t"
@@ -117,7 +202,15 @@
      << "InNacks" << "\t"
     
      << "InData" << "\t"
-     << "OutData";
+     << "OutData" << "\t"
+
+     << "InInterestsBytes" << "\t"
+     << "OutInterestsBytes" << "\t"
+    
+     << "InNacksBytes" << "\t"
+    
+     << "InDataBytes" << "\t"
+     << "OutDataBytes";
 }
 
 void
@@ -126,52 +219,66 @@
   os << m_node << "\t"
      << m_app << "\t"
      << m_appId << "\t"
+
      << m_inInterests << "\t"
      << m_outInterests << "\t"
     
      << m_inNacks << "\t"
     
      << m_inData << "\t"
-     << m_outData;
+     << m_outData << "\t"
+
+     << m_inInterestsBytes << "\t"
+     << m_outInterestsBytes << "\t"
+    
+     << m_inNacksBytes << "\t"
+    
+     << m_inDataBytes << "\t"
+     << m_outDataBytes;
 }
 
 void
 CcnxAggregateAppTracer::OutInterests (std::string context,
-                                      Ptr<const CcnxInterestHeader>, Ptr<CcnxApp>, Ptr<CcnxFace>)
+                                      Ptr<const CcnxInterestHeader> header, Ptr<CcnxApp>, Ptr<CcnxFace>)
 {
   m_outInterests++;
+  m_outInterestsBytes += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateAppTracer::OutData (std::string context,
-                                 Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
+                                 Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
                                  Ptr<CcnxApp>, Ptr<CcnxFace>)
 {
   m_outData++;
+  m_outDataBytes += header->GetSerializedSize () + payload->GetSerializedSize ();
 }
 
 void
 CcnxAggregateAppTracer::InInterests (std::string context,
-                                     Ptr<const CcnxInterestHeader>,
+                                     Ptr<const CcnxInterestHeader> header,
                                      Ptr<CcnxApp>, Ptr<CcnxFace>)
 {
   m_inInterests++;
+  m_inInterestsBytes += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateAppTracer::InNacks (std::string context,
-                                 Ptr<const CcnxInterestHeader>,
+                                 Ptr<const CcnxInterestHeader> header,
                                  Ptr<CcnxApp>, Ptr<CcnxFace>)
 {
   m_inNacks++;
+  m_inNacksBytes += header->GetSerializedSize ();
 }
   
 void
 CcnxAggregateAppTracer::InData (std::string context,
-                                Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
+                                Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
                                 Ptr<CcnxApp>, Ptr<CcnxFace>)
 {
   m_inData++;
+  m_inDataBytes += header->GetSerializedSize () + payload->GetSerializedSize ();
 }
 
 
@@ -179,22 +286,42 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
+CcnxAggregateL3Tracer::CcnxAggregateL3Tracer (Ptr<Node> node)
+  : CcnxL3Tracer (node)
+{
+  Reset ();
+}
 
 CcnxAggregateL3Tracer::CcnxAggregateL3Tracer (const std::string &node)
   : CcnxL3Tracer (node)
-  , m_inInterests (0)
-  , m_outInterests (0)
-  , m_dropInterests (0)
-  , m_inNacks (0)
-  , m_outNacks (0)
-  , m_dropNacks (0)
-  , m_inData (0)
-  , m_outData (0)
-  , m_dropData (0)
 {
+  Reset ();
 }
 
 void
+CcnxAggregateL3Tracer::Stats::Reset ()
+{
+  m_inInterests = 0;
+  m_outInterests = 0;
+  m_dropInterests = 0;
+  m_inNacks = 0;
+  m_outNacks = 0;
+  m_dropNacks = 0;
+  m_inData = 0;
+  m_outData = 0;
+  m_dropData = 0;
+}
+
+
+void
+CcnxAggregateL3Tracer::Reset ()
+{
+  m_packets.Reset ();
+  m_bytes.Reset ();
+}
+
+
+void
 CcnxAggregateL3Tracer::PrintHeader (std::ostream &os) const
 {
   os << "Node" << "\t"
@@ -208,87 +335,123 @@
     
      << "InData" << "\t"
      << "OutData" << "\t"
-     << "DropData";
+     << "DropData" << "\t"
+    
+     << "InInterestsBytes" << "\t"
+     << "OutInterestsBytes" << "\t"
+     << "DropInterestsBytes" << "\t"
+    
+     << "InNacksBytes" << "\t"
+     << "OutNacksBytes" << "\t"
+     << "DropNacksBytes" << "\t"
+    
+     << "InDataBytes" << "\t"
+     << "OutDataBytes" << "\t"
+     << "DropDataBytes";
 }
 
 void
 CcnxAggregateL3Tracer::Print (std::ostream &os) const
 {
   os << m_node << "\t"
-     << m_inInterests   << "\t"
-     << m_outInterests  << "\t"
-     << m_dropInterests << "\t"
-  
-     << m_inNacks   << "\t"
-     << m_outNacks  << "\t"
-     << m_dropNacks << "\t"
-  
-     << m_inData   << "\t"
-     << m_outData  << "\t"
-     << m_dropData;
+     << m_packets.m_inInterests   << "\t"
+     << m_packets.m_outInterests  << "\t"
+     << m_packets.m_dropInterests << "\t"
+
+     << m_packets.m_inNacks   << "\t"
+     << m_packets.m_outNacks  << "\t"
+     << m_packets.m_dropNacks << "\t"
+
+     << m_packets.m_inData   << "\t"
+     << m_packets.m_outData  << "\t"
+     << m_packets.m_dropData << "\t"
+
+     << m_bytes.m_inInterests   << "\t"
+     << m_bytes.m_outInterests  << "\t"
+     << m_bytes.m_dropInterests << "\t"
+
+     << m_bytes.m_inNacks   << "\t"
+     << m_bytes.m_outNacks  << "\t"
+     << m_bytes.m_dropNacks << "\t"
+
+     << m_bytes.m_inData   << "\t"
+     << m_bytes.m_outData  << "\t"
+     << m_bytes.m_dropData;
 }
 
 void
 CcnxAggregateL3Tracer::OutInterests  (std::string context,
-                                      Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
+                                      Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace>)
 {
-  m_outInterests++;
+  m_packets.m_outInterests++;
+  m_bytes.m_outInterests += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::InInterests   (std::string context,
-                                      Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
+                                      Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace>)
 {
-  m_inInterests++;
+  m_packets.m_inInterests++;
+  m_bytes.m_inInterests += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::DropInterests (std::string context,
-                                      Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
+                                      Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace>)
 {
-  m_dropInterests++;
+  m_packets.m_dropInterests++;
+  m_bytes.m_dropInterests += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::OutNacks  (std::string context,
-                                  Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
+                                  Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace>)
 {
-  m_outNacks++;
+  m_packets.m_outNacks++;
+  m_bytes.m_outNacks += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::InNacks   (std::string context,
-                                  Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>)
+                                  Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace>)
 {
-  m_inNacks++;
+  m_packets.m_inNacks++;
+  m_bytes.m_inNacks += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::DropNacks (std::string context,
-                                  Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
+                                  Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace>)
 {
-  m_dropNacks++;
+  m_packets.m_dropNacks++;
+  m_bytes.m_dropNacks += header->GetSerializedSize ();
 }
 
 void
 CcnxAggregateL3Tracer::OutData  (std::string context,
-                                 Ptr<const CcnxContentObjectHeader>, bool fromCache, Ptr<const CcnxFace>)
+                                 Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                                 bool fromCache, Ptr<const CcnxFace>)
 {
-  m_inData++;
+  m_packets.m_outData++;
+  m_bytes.m_outData += header->GetSerializedSize () + payload->GetSize ();
 }
 
 void
 CcnxAggregateL3Tracer::InData   (std::string context,
-                                 Ptr<const CcnxContentObjectHeader>, Ptr<const CcnxFace>)
+                                 Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                                 Ptr<const CcnxFace>)
 {
-  m_outData++;
+  m_packets.m_inData++;
+  m_bytes.m_inData += header->GetSerializedSize () + payload->GetSize ();
 }
 
 void
 CcnxAggregateL3Tracer::DropData (std::string context,
-                                 Ptr<const CcnxContentObjectHeader>, Ccnx::DropReason, Ptr<const CcnxFace>)
+                                 Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                                 Ccnx::DropReason, Ptr<const CcnxFace>)
 {
-  m_dropData++;
+  m_packets.m_dropData++;
+  m_bytes.m_dropData += header->GetSerializedSize () + payload->GetSize ();
 }
 
 
@@ -296,9 +459,228 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
+CcnxRateL3Tracer::CcnxRateL3Tracer (std::ostream &os, Ptr<Node> node)
+  : CcnxL3Tracer (node)
+  , m_os (os)
+{
+  SetAveragingPeriod (Seconds (1.0));
+}
+
+CcnxRateL3Tracer::CcnxRateL3Tracer (std::ostream &os, const std::string &node)
+  : CcnxL3Tracer (node)
+  , m_os (os)
+{
+  SetAveragingPeriod (Seconds (1.0));
+}
+
+CcnxRateL3Tracer::~CcnxRateL3Tracer ()
+{
+  m_printEvent.Cancel ();
+}
+
+void
+CcnxRateL3Tracer::SetAveragingPeriod (const Time &period)
+{
+  m_period = period;
+  m_printEvent.Cancel ();
+  m_printEvent = Simulator::Schedule (m_period, &CcnxRateL3Tracer::PeriodicPrinter, this);
+}
+
+void
+CcnxRateL3Tracer::PeriodicPrinter ()
+{
+  Print (m_os);
+  Reset ();
+  
+  m_printEvent = Simulator::Schedule (m_period, &CcnxRateL3Tracer::PeriodicPrinter, this);
+}
+
+void
+CcnxRateL3Tracer::PrintHeader (std::ostream &os) const
+{
+  os << "Time" << "\t"
+
+     << "Node" << "\t"
+     << "FaceId" << "\t"
+     << "FaceDescr" << "\t"
+
+     << "Type" << "\t"
+     << "Packets" << "\t"
+     << "Kilobytes";
+}
+
+void
+CcnxRateL3Tracer::Reset ()
+{
+  for (std::map<Ptr<const CcnxFace>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
+       stats != m_stats.end ();
+       stats++)
+    {
+      stats->second.get<0> ().Reset ();
+      stats->second.get<1> ().Reset ();
+    }
+}
+
+const double alpha = 0.8;
+
+#define STATS(INDEX) stats->second.get<INDEX> ()
+#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
+
+#define PRINTER(printName, fieldName) \
+STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
+ STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
+                                                                        \
+os << time.ToDouble (Time::S) << "\t"                                   \
+ << m_node << "\t"                                                      \
+ << stats->first->GetId () << "\t"                                      \
+ << *stats->first << "\t"                                               \
+ << printName << "\t"                                                   \
+ << STATS(2).fieldName  << "\t"                                        \
+ << STATS(3).fieldName << "\n";
+
+void
+CcnxRateL3Tracer::Print (std::ostream &os) const
+{
+  for (std::map<Ptr<const CcnxFace>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin ();
+       stats != m_stats.end ();
+       stats++)
+    {
+      Time time = Simulator::Now ();
+
+      PRINTER ("InInterests",   m_inInterests);
+      PRINTER ("OutInterests",  m_outInterests);
+      PRINTER ("DropInterests", m_dropInterests);
+      
+      PRINTER ("InNacks",   m_inNacks);
+      PRINTER ("OutNacks",  m_outNacks);
+      PRINTER ("DropNacks", m_dropNacks);
+
+      PRINTER ("InData",   m_inData);
+      PRINTER ("OutData",  m_outData);
+      PRINTER ("DropData", m_dropData);
+
+      // os << Simulator::Now ().ToDouble (Time::S) << "\t"
+
+      //    << m_node << "\t"
+      //    << stats->first->GetId () << "\t"
+      //    << *stats->first << "\t"
+      //    << "InInterests" << "\t" << 
+      //    << stats->second.get<0> ().m_inInterests / m_period.ToDouble (Time::S)  << "\t"
+      //    << stats->second.get<1> ().m_inInterests / m_period.ToDouble (Time::S) / 1024.0;
+
+      //    << stats->second.get<0> ().m_outInterests / m_period.ToDouble (Time::S)  << "\t"
+      //    << stats->second.get<0> ().m_dropInterests / m_period.ToDouble (Time::S) << "\t"
+
+      //    << stats->second.get<0> ().m_inNacks / m_period.ToDouble (Time::S)   << "\t"
+      //    << stats->second.get<0> ().m_outNacks / m_period.ToDouble (Time::S)  << "\t"
+      //    << stats->second.get<0> ().m_dropNacks / m_period.ToDouble (Time::S) << "\t"
+
+      //    << stats->second.get<0> ().m_inData / m_period.ToDouble (Time::S)   << "\t"
+      //    << stats->second.get<0> ().m_outData / m_period.ToDouble (Time::S)  << "\t"
+      //    << stats->second.get<0> ().m_dropData / m_period.ToDouble (Time::S) << "\t"
+
+      //    << stats->second.get<1> ().m_outInterests / m_period.ToDouble (Time::S) / 1024.0  << "\t"
+      //    << stats->second.get<1> ().m_dropInterests / m_period.ToDouble (Time::S) / 1024.0 << "\t"
+
+      //    << stats->second.get<1> ().m_inNacks / m_period.ToDouble (Time::S) / 1024.0   << "\t"
+      //    << stats->second.get<1> ().m_outNacks / m_period.ToDouble (Time::S) / 1024.0  << "\t"
+      //    << stats->second.get<1> ().m_dropNacks / m_period.ToDouble (Time::S) / 1024.0 << "\t"
+
+      //    << stats->second.get<1> ().m_inData / m_period.ToDouble (Time::S) / 1024.0   << "\t"
+      //    << stats->second.get<1> ().m_outData / m_period.ToDouble (Time::S) / 1024.0  << "\t"
+      //    << stats->second.get<1> ().m_dropData / m_period.ToDouble (Time::S) / 1024.0;
+    }
+}
+
+
+void
+CcnxRateL3Tracer::OutInterests  (std::string context,
+                                      Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_outInterests ++;
+  m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::InInterests   (std::string context,
+                                 Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_inInterests ++;
+  m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::DropInterests (std::string context,
+                                      Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_dropInterests ++;
+  m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::OutNacks  (std::string context,
+                                  Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_outNacks ++;
+  m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::InNacks   (std::string context,
+                                  Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_inNacks ++;
+  m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::DropNacks (std::string context,
+                             Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_dropNacks ++;
+  m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize ();
+}
+
+void
+CcnxRateL3Tracer::OutData  (std::string context,
+                            Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                            bool fromCache, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_inData ++;
+  m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize ();
+}
+
+void
+CcnxRateL3Tracer::InData   (std::string context,
+                            Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                            Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_outData ++;
+  m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize ();
+}
+
+void
+CcnxRateL3Tracer::DropData (std::string context,
+                            Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload,
+                            Ccnx::DropReason, Ptr<const CcnxFace> face)
+{
+  m_stats[face].get<0> ().m_dropData ++;
+  m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize ();
+}
+
+////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////
+
+CcnxTraceHelper::CcnxTraceHelper ()
+  : m_l3RateTrace (0)
+{
+}
+
 CcnxTraceHelper::~CcnxTraceHelper ()
 {
   NS_LOG_FUNCTION (this);
+  if (m_l3RateTrace != 0) delete m_l3RateTrace;
   
   if (m_apps.size () > 0)
     {
@@ -391,7 +773,7 @@
           if ((*app)->GetInstanceTypeId ().GetName () == appName)
             {
               m_apps.push_back (Create<CcnxAggregateAppTracer> (appName,
-                                                                lexical_cast<string> ((*node)->GetId ()),
+                                                                *node,
                                                                 lexical_cast<string> (appId)));
             }
         }
@@ -409,9 +791,33 @@
     {
       NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
       
-      m_l3s.push_back (Create<CcnxAggregateL3Tracer> (lexical_cast<string> ((*node)->GetId ())));
+      m_l3s.push_back (Create<CcnxAggregateL3Tracer> (*node));
     }
 }
 
+void
+CcnxTraceHelper::EnableRateL3All (const std::string &l3RateTrace)
+{
+  NS_LOG_FUNCTION (this);
+  m_l3RateTrace = new ofstream (l3RateTrace.c_str (), ios::trunc);
+
+  for (NodeList::Iterator node = NodeList::Begin ();
+       node != NodeList::End ();
+       node++)
+    {
+      NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
+
+      Ptr<CcnxRateL3Tracer> trace = Create<CcnxRateL3Tracer> (boost::ref(*m_l3RateTrace), *node);
+      trace->SetAveragingPeriod (Seconds (0.5));
+      m_l3Rates.push_back (trace);
+    }
+
+  if (m_l3Rates.size () > 0)
+    {
+      // *m_l3RateTrace << "# "; // not necessary for R's read.table
+      m_l3Rates.front ()->PrintHeader (*m_l3RateTrace);
+      *m_l3RateTrace << "\n";
+    }
+}
 
 } // namespace ns3
diff --git a/helper/ccnx-trace-helper.h b/helper/ccnx-trace-helper.h
index 0a0906d..7668b1b 100644
--- a/helper/ccnx-trace-helper.h
+++ b/helper/ccnx-trace-helper.h
@@ -24,6 +24,11 @@
 #include "ns3/ptr.h"
 #include "ns3/simple-ref-count.h"
 #include "ns3/ccnx.h"
+#include "ns3/nstime.h"
+#include "ns3/event-id.h"
+
+#include <boost/tuple/tuple.hpp>
+#include <map>
 
 namespace ns3 {
 
@@ -32,9 +37,13 @@
 class CcnxAppTracer : public SimpleRefCount<CcnxAppTracer>
 {
 public:
-  CcnxAppTracer (const std::string &app, const std::string &node = "*", const std::string &appId = "*");
+  CcnxAppTracer (const std::string &app, Ptr<Node> node, const std::string &appId = "*");
+  CcnxAppTracer (const std::string &app, const std::string &node, const std::string &appId = "*");
   virtual ~CcnxAppTracer ()  { };
 
+  void
+  Connect ();
+
   virtual void
   PrintHeader (std::ostream &os) const = 0;
   
@@ -65,6 +74,7 @@
   std::string m_app;
   std::string m_appId;
   std::string m_node;
+  Ptr<Node> m_nodePtr;
 };
 
 std::ostream&
@@ -80,9 +90,13 @@
 class CcnxL3Tracer : public SimpleRefCount<CcnxL3Tracer>
 {
 public:
-  CcnxL3Tracer (const std::string &node = "*");
+  CcnxL3Tracer (Ptr<Node> node);
+  CcnxL3Tracer (const std::string &node);
   virtual ~CcnxL3Tracer () { };
 
+  void
+  Connect ();
+  
   virtual void
   PrintHeader (std::ostream &os) const = 0;
 
@@ -116,18 +130,34 @@
   
   virtual void
   OutData  (std::string context,
-            Ptr<const CcnxContentObjectHeader>, bool fromCache, Ptr<const CcnxFace>) = 0;
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, bool fromCache, Ptr<const CcnxFace>) = 0;
 
   virtual void
   InData   (std::string context,
-            Ptr<const CcnxContentObjectHeader>, Ptr<const CcnxFace>) = 0;
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ptr<const CcnxFace>) = 0;
 
   virtual void
   DropData (std::string context,
-            Ptr<const CcnxContentObjectHeader>, Ccnx::DropReason, Ptr<const CcnxFace>) = 0;
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ccnx::DropReason, Ptr<const CcnxFace>) = 0;
 
 protected:
   std::string m_node;
+  Ptr<Node> m_nodePtr;
+
+  struct Stats
+  {
+    void Reset ();
+    
+    uint64_t m_inInterests;
+    uint64_t m_outInterests;
+    uint64_t m_dropInterests;
+    uint64_t m_inNacks;
+    uint64_t m_outNacks;
+    uint64_t m_dropNacks;
+    uint64_t m_inData;
+    uint64_t m_outData;
+    uint64_t m_dropData;
+  };
 };
 
 std::ostream&
@@ -147,7 +177,8 @@
 class CcnxAggregateAppTracer : public CcnxAppTracer
 {
 public:
-  CcnxAggregateAppTracer (const std::string &app, const std::string &node = "*", const std::string &appId = "*");
+  CcnxAggregateAppTracer (const std::string &app, Ptr<Node> node, const std::string &appId = "*");
+  CcnxAggregateAppTracer (const std::string &app, const std::string &node, const std::string &appId = "*");
   virtual ~CcnxAggregateAppTracer () { };
 
   virtual void
@@ -176,18 +207,29 @@
   InData  (std::string context,
            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ptr<CcnxApp>, Ptr<CcnxFace>);
 
-private:
+protected:
+  void
+  Reset ();
+
+protected:
   uint64_t m_inInterests;
   uint64_t m_outInterests;
   uint64_t m_inNacks;
   uint64_t m_inData; 
   uint64_t m_outData;
+
+  uint64_t m_inInterestsBytes;
+  uint64_t m_outInterestsBytes;
+  uint64_t m_inNacksBytes;
+  uint64_t m_inDataBytes;
+  uint64_t m_outDataBytes;
 };
 
 class CcnxAggregateL3Tracer : public CcnxL3Tracer
 {
 public:
-  CcnxAggregateL3Tracer (const std::string &node = "*");
+  CcnxAggregateL3Tracer (Ptr<Node> node);
+  CcnxAggregateL3Tracer (const std::string &node);
   virtual ~CcnxAggregateL3Tracer () { };
   
   virtual void
@@ -222,31 +264,105 @@
   
   virtual void
   OutData  (std::string context,
-            Ptr<const CcnxContentObjectHeader>, bool fromCache, Ptr<const CcnxFace>);
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, bool fromCache, Ptr<const CcnxFace>);
 
   virtual void
   InData   (std::string context,
-            Ptr<const CcnxContentObjectHeader>, Ptr<const CcnxFace>);
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ptr<const CcnxFace>);
 
   virtual void
   DropData (std::string context,
-            Ptr<const CcnxContentObjectHeader>, Ccnx::DropReason, Ptr<const CcnxFace>);
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ccnx::DropReason, Ptr<const CcnxFace>);
+
+protected:
+  void
+  Reset ();
+  
+protected:
+  Stats m_packets;
+  Stats m_bytes;
+};
+
+/**
+ * @ingroup ccnx
+ * @brief CCNx network-layer rate tracer
+ */
+class CcnxRateL3Tracer : public CcnxL3Tracer
+{
+public:
+  /**
+   * @brief Network layer tracer constructor
+   */
+  CcnxRateL3Tracer (std::ostream &os, Ptr<Node> node);
+  CcnxRateL3Tracer (std::ostream &os, const std::string &node);
+  virtual ~CcnxRateL3Tracer ();
+
+  void
+  SetAveragingPeriod (const Time &period);
+  
+  virtual void
+  PrintHeader (std::ostream &os) const;
+
+  virtual void
+  Print (std::ostream &os) const;
+
+  virtual void
+  OutInterests  (std::string context,
+                 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>);
+
+  virtual void
+  InInterests   (std::string context,
+                 Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>);
+
+  virtual void
+  DropInterests (std::string context,
+                 Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>);
+  
+  virtual void
+  OutNacks  (std::string context,
+             Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>);
+
+  virtual void
+  InNacks   (std::string context,
+             Ptr<const CcnxInterestHeader>, Ptr<const CcnxFace>);
+
+  virtual void
+  DropNacks (std::string context,
+             Ptr<const CcnxInterestHeader>, Ccnx::DropReason, Ptr<const CcnxFace>);
+  
+  virtual void
+  OutData  (std::string context,
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, bool fromCache, Ptr<const CcnxFace>);
+
+  virtual void
+  InData   (std::string context,
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ptr<const CcnxFace>);
+
+  virtual void
+  DropData (std::string context,
+            Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>, Ccnx::DropReason, Ptr<const CcnxFace>);
 
 private:
-  uint64_t m_inInterests;
-  uint64_t m_outInterests;
-  uint64_t m_dropInterests;
-  uint64_t m_inNacks;
-  uint64_t m_outNacks;
-  uint64_t m_dropNacks;
-  uint64_t m_inData;
-  uint64_t m_outData;
-  uint64_t m_dropData;
+  void
+  PeriodicPrinter ();
+  
+  void
+  Reset ();
+
+private:
+  std::ostream& m_os;
+  Time m_period;
+  EventId m_printEvent;
+
+  mutable std::map<Ptr<const CcnxFace>, boost::tuple<Stats, Stats, Stats, Stats> > m_stats;
 };
 
+
 class CcnxTraceHelper
 {
 public:
+  CcnxTraceHelper ();
+  
   /**
    * @brief Destructor that invokes trace output procedures
    */
@@ -271,9 +387,9 @@
    */
   void
   SetL3TraceFile (const std::string &l3Trace = "l3.log");
-  
+
   /**
-   * @brief Enable aggregate app-level CCNx tracing on all CCNx applications (individual file per application)
+   * @brief Enable aggregate app-level CCNx tracing on all CCNx applications
    *
    * @param app  Class name of the application of interest
    */
@@ -281,17 +397,26 @@
   EnableAggregateAppAll (const std::string &app);
 
   /**
-   * @brief Enable aggregate network-level CCNx tracing on all CCNx nodes (individual file per node)
+   * @brief Enable aggregate network-level CCNx tracing on all CCNx node
    */
   void
   EnableAggregateL3All ();
 
+  /**
+   * @brief Enable network-level CCNx rate tracing on all CCNx nodes (individual file per node)
+   */
+  void
+  EnableRateL3All (const std::string &l3RateTrace = "l3-rate.log");
+  
 private:
   std::string m_appTrace;
   std::list<Ptr<CcnxAggregateAppTracer> > m_apps;
 
   std::string m_l3Trace;
   std::list<Ptr<CcnxAggregateL3Tracer> > m_l3s;
+
+  std::list<Ptr<CcnxRateL3Tracer> > m_l3Rates;
+  std::ostream *m_l3RateTrace;
 };
 
 
diff --git a/model/ccnx-content-store.cc b/model/ccnx-content-store.cc
index 4875386..b0c4858 100644
--- a/model/ccnx-content-store.cc
+++ b/model/ccnx-content-store.cc
@@ -150,7 +150,7 @@
 }
 
 
-boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader> >
+boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
 CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest)
 {
   NS_LOG_FUNCTION_NOARGS ();
@@ -162,9 +162,9 @@
                                            m_contentStore.project<i_mru> (it));
 
       // return fully formed CCNx packet
-      return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader ());
+      return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader (), it->GetPacket ());
     }
-  return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader> > (0, 0);
+  return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
 }   
     
 void 
diff --git a/model/ccnx-content-store.h b/model/ccnx-content-store.h
index dd0f884..9f07a47 100644
--- a/model/ccnx-content-store.h
+++ b/model/ccnx-content-store.h
@@ -174,7 +174,7 @@
    * If an entry is found, it is promoted to the top of most recent
    * used entries index, \see m_contentStore
    */
-  boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader> >
+  boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
   Lookup (Ptr<const CcnxInterestHeader> interest);
             
   /**
@@ -215,22 +215,6 @@
    * Release build dumps everything in MRU order
    */
   void Print () const;
-            
-protected:
-  // /**
-  //  * \brief Move the given CS entry to the head of the list
-  //  *
-  //  * \param entry Content Store entry
-  //  */
-  // void Promote( CsEntry &entry );
-
-  /**
-   * \todo Alex: DoDispose and NotifyNewAggregate are seem to be very
-   * important, but I'm not yet sure what exactly they are supposed to
-   * do
-   */
-  // virtual void DoDispose ();
-  // virtual void NotifyNewAggregate ();
 
 private:
   CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor
@@ -244,7 +228,7 @@
    * \brief Content store implemented as a Boost.MultiIndex container
    * \internal
    */
-    CcnxContentStoreContainer::type m_contentStore;
+  CcnxContentStoreContainer::type m_contentStore;
 };
 
 inline std::ostream&
diff --git a/model/ccnx-l3-protocol.cc b/model/ccnx-l3-protocol.cc
index 7f5f195..2e55164 100644
--- a/model/ccnx-l3-protocol.cc
+++ b/model/ccnx-l3-protocol.cc
@@ -423,8 +423,9 @@
     }
 
   Ptr<Packet> contentObject;
-  Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
-  tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
+  Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
+  Ptr<const Packet> payload; // used for tracing
+  tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
   if (contentObject != 0)
     {
       NS_ASSERT (contentObjectHeader != 0);
@@ -432,7 +433,7 @@
       NS_LOG_LOGIC("Found in cache");
         
       incomingFace->Send (contentObject);
-      m_outData (contentObjectHeader, true, incomingFace);
+      m_outData (contentObjectHeader, payload, true, incomingFace);
 
       // Set pruning timout on PIT entry (instead of deleting the record)
       m_pit->modify (m_pit->iterator_to (pitEntry),
@@ -533,7 +534,7 @@
 {
     
   NS_LOG_FUNCTION (incomingFace << header << payload << packet);
-  m_inData (header, incomingFace);
+  m_inData (header, payload, incomingFace);
 
   // 1. Lookup PIT entry
   try
@@ -580,7 +581,7 @@
           if (incoming.m_face != incomingFace)
             {
               incoming.m_face->Send (packet->Copy ());
-              m_outData (header, false, incoming.m_face);
+              m_outData (header, payload, false, incoming.m_face);
             }
 
           // successfull forwarded data trace
@@ -600,7 +601,7 @@
       //    (unsolicited data packets should not "poison" content store)
       
       //drop dulicated or not requested data packet
-      m_dropData (header, UNSOLICITED, incomingFace);
+      m_dropData (header, payload, UNSOLICITED, incomingFace);
       return; // do not process unsoliced data packets
     }
 }
diff --git a/model/ccnx.h b/model/ccnx.h
index f87da94..8d565c1 100644
--- a/model/ccnx.h
+++ b/model/ccnx.h
@@ -197,14 +197,14 @@
   ////////////////////////////////////////////////////////////////////
   ////////////////////////////////////////////////////////////////////
 
-  TracedCallback<Ptr<const CcnxContentObjectHeader>,
+  TracedCallback<Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
                  bool /*from cache*/,
                  Ptr<const CcnxFace> > m_outData;
 
-  TracedCallback<Ptr<const CcnxContentObjectHeader>,
+  TracedCallback<Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
                  Ptr<const CcnxFace> > m_inData;
 
-  TracedCallback<Ptr<const CcnxContentObjectHeader>,
+  TracedCallback<Ptr<const CcnxContentObjectHeader>, Ptr<const Packet>,
                   DropReason,
                   Ptr<const CcnxFace> > m_dropData;  
 };