blob: 22006654418548c8fc2d6b22d331cad54ee2218e [file] [log] [blame]
Alexander Afanasyev86287992012-12-10 16:11:50 -08001/* -*- 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 *
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080018 * Author: Xiaoyan Hu <x......u@gmail.com>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev86287992012-12-10 16:11:50 -080020 */
21
22#include "ndn-cs-tracer.h"
23#include "ns3/node.h"
24#include "ns3/packet.h"
25#include "ns3/config.h"
26#include "ns3/names.h"
27#include "ns3/callback.h"
28
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080029#include "ns3/ndn-app.h"
Alexander Afanasyev86287992012-12-10 16:11:50 -080030#include "ns3/ndn-interest.h"
31#include "ns3/ndn-content-object.h"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080032#include "ns3/simulator.h"
33#include "ns3/node-list.h"
34#include "ns3/log.h"
35
36#include <boost/lexical_cast.hpp>
37
38#include <fstream>
39
40NS_LOG_COMPONENT_DEFINE ("ndn.CsTracer");
Alexander Afanasyev86287992012-12-10 16:11:50 -080041
42using namespace std;
43
44namespace ns3 {
45namespace ndn {
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080046
47
48boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
49CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
50{
51 using namespace boost;
52 using namespace std;
53
54 std::list<Ptr<CsTracer> > tracers;
55 boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
56 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
57
58 if (!outputStream->is_open ())
59 return boost::make_tuple (outputStream, tracers);
60
61 for (NodeList::Iterator node = NodeList::Begin ();
62 node != NodeList::End ();
63 node++)
64 {
65 NS_LOG_DEBUG ("Node: " << (*node)->GetId ());
66
67 Ptr<CsTracer> trace = Create<CsTracer> (outputStream, *node);
68 trace->SetAveragingPeriod (averagingPeriod);
69 tracers.push_back (trace);
70 }
71
72 if (tracers.size () > 0)
73 {
74 // *m_l3RateTrace << "# "; // not necessary for R's read.table
75 tracers.front ()->PrintHeader (*outputStream);
76 *outputStream << "\n";
77 }
78
79 return boost::make_tuple (outputStream, tracers);
80}
81
82//////////////////////////////////////////////////////////////////////////////
83//////////////////////////////////////////////////////////////////////////////
84//////////////////////////////////////////////////////////////////////////////
85
86CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyev86287992012-12-10 16:11:50 -080087: m_nodePtr (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080088, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -080089{
90 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
91
92 Connect ();
93
94 string name = Names::FindName (node);
95 if (!name.empty ())
96 {
97 m_node = name;
98 }
99}
100
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800101CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800102: m_node (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800103, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800104{
105 Connect ();
106}
107
108CsTracer::~CsTracer ()
109{
110};
111
112
113void
114CsTracer::Connect ()
115{
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800116 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits",
117 MakeCallback (&CsTracer::CacheHits, this));
118 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses",
119 MakeCallback (&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800120
121 Reset ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800122}
123
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800124
125void
126CsTracer::SetAveragingPeriod (const Time &period)
127{
128 m_period = period;
129 m_printEvent.Cancel ();
130 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
131}
132
133void
134CsTracer::PeriodicPrinter ()
135{
136 Print (*m_os);
137 Reset ();
138
139 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
140}
141
142void
143CsTracer::PrintHeader (std::ostream &os) const
144{
145 os << "Time" << "\t"
146
147 << "Node" << "\t"
148
149 << "Type" << "\t"
150 << "Packets" << "\t";
151}
152
153void
154CsTracer::Reset ()
155{
156 m_stats.Reset();
157}
158
159#define PRINTER(printName, fieldName) \
160 os << time.ToDouble (Time::S) << "\t" \
161 << m_node << "\t" \
162 << printName << "\t" \
163 << m_stats.fieldName << "\n";
164
165
166void
167CsTracer::Print (std::ostream &os) const
168{
169 Time time = Simulator::Now ();
170
171 PRINTER ("CacheHits", m_cacheHits);
172 PRINTER ("CacheMisses", m_cacheMisses);
173}
174
175void
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800176CsTracer::CacheHits (Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800177{
178 m_stats.m_cacheHits ++;
179}
180
181void
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800182CsTracer::CacheMisses (Ptr<const InterestHeader>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800183{
184 m_stats.m_cacheMisses ++;
185}
186
187
Alexander Afanasyev86287992012-12-10 16:11:50 -0800188} // namespace ndn
189} // namespace ns3