blob: 40aed8ff9f3a571c0b919c45d6c683243c2f3746 [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"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070031#include "ns3/ndn-data.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
Alexander Afanasyev5352af32013-07-15 09:51:28 -070047template<class T>
48static inline void
49NullDeleter (T *ptr)
50{
51}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080052
53boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
54CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
55{
56 using namespace boost;
57 using namespace std;
58
59 std::list<Ptr<CsTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070060 boost::shared_ptr<std::ostream> outputStream;
61 if (file != "-")
62 {
63 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
64 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080065
Alexander Afanasyev5352af32013-07-15 09:51:28 -070066 if (!os->is_open ())
67 return boost::make_tuple (outputStream, tracers);
68
69 outputStream = os;
70 }
71 else
72 {
73 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
74 }
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080075
76 for (NodeList::Iterator node = NodeList::Begin ();
77 node != NodeList::End ();
78 node++)
79 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070080 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080081 tracers.push_back (trace);
82 }
83
84 if (tracers.size () > 0)
85 {
86 // *m_l3RateTrace << "# "; // not necessary for R's read.table
87 tracers.front ()->PrintHeader (*outputStream);
88 *outputStream << "\n";
89 }
90
91 return boost::make_tuple (outputStream, tracers);
92}
93
Alexander Afanasyev5352af32013-07-15 09:51:28 -070094boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
95CsTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
96{
97 using namespace boost;
98 using namespace std;
99
100 std::list<Ptr<CsTracer> > tracers;
101 boost::shared_ptr<std::ostream> outputStream;
102 if (file != "-")
103 {
104 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
105 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
106
107 if (!os->is_open ())
108 return boost::make_tuple (outputStream, tracers);
109
110 outputStream = os;
111 }
112 else
113 {
114 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
115 }
116
117 for (NodeContainer::Iterator node = nodes.Begin ();
118 node != nodes.End ();
119 node++)
120 {
121 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
122 tracers.push_back (trace);
123 }
124
125 if (tracers.size () > 0)
126 {
127 // *m_l3RateTrace << "# "; // not necessary for R's read.table
128 tracers.front ()->PrintHeader (*outputStream);
129 *outputStream << "\n";
130 }
131
132 return boost::make_tuple (outputStream, tracers);
133}
134
135boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
136CsTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
137{
138 using namespace boost;
139 using namespace std;
140
141 std::list<Ptr<CsTracer> > tracers;
142 boost::shared_ptr<std::ostream> outputStream;
143 if (file != "-")
144 {
145 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
146 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
147
148 if (!os->is_open ())
149 return boost::make_tuple (outputStream, tracers);
150
151 outputStream = os;
152 }
153 else
154 {
155 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
156 }
157
158 Ptr<CsTracer> trace = Install (node, outputStream, averagingPeriod);
159 tracers.push_back (trace);
160
161 if (tracers.size () > 0)
162 {
163 // *m_l3RateTrace << "# "; // not necessary for R's read.table
164 tracers.front ()->PrintHeader (*outputStream);
165 *outputStream << "\n";
166 }
167
168 return boost::make_tuple (outputStream, tracers);
169}
170
171
172Ptr<CsTracer>
173CsTracer::Install (Ptr<Node> node,
174 boost::shared_ptr<std::ostream> outputStream,
175 Time averagingPeriod/* = Seconds (0.5)*/)
176{
177 NS_LOG_DEBUG ("Node: " << node->GetId ());
178
179 Ptr<CsTracer> trace = Create<CsTracer> (outputStream, node);
180 trace->SetAveragingPeriod (averagingPeriod);
181
182 return trace;
183}
184
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800185//////////////////////////////////////////////////////////////////////////////
186//////////////////////////////////////////////////////////////////////////////
187//////////////////////////////////////////////////////////////////////////////
188
189CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800190: m_nodePtr (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800191, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800192{
193 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
194
195 Connect ();
196
197 string name = Names::FindName (node);
198 if (!name.empty ())
199 {
200 m_node = name;
201 }
202}
203
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800204CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800205: m_node (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800206, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800207{
208 Connect ();
209}
210
211CsTracer::~CsTracer ()
212{
213};
214
215
216void
217CsTracer::Connect ()
218{
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800219 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits",
220 MakeCallback (&CsTracer::CacheHits, this));
221 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses",
222 MakeCallback (&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800223
224 Reset ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800225}
226
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800227
228void
229CsTracer::SetAveragingPeriod (const Time &period)
230{
231 m_period = period;
232 m_printEvent.Cancel ();
233 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
234}
235
236void
237CsTracer::PeriodicPrinter ()
238{
239 Print (*m_os);
240 Reset ();
241
242 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
243}
244
245void
246CsTracer::PrintHeader (std::ostream &os) const
247{
248 os << "Time" << "\t"
249
250 << "Node" << "\t"
251
252 << "Type" << "\t"
253 << "Packets" << "\t";
254}
255
256void
257CsTracer::Reset ()
258{
259 m_stats.Reset();
260}
261
262#define PRINTER(printName, fieldName) \
263 os << time.ToDouble (Time::S) << "\t" \
264 << m_node << "\t" \
265 << printName << "\t" \
266 << m_stats.fieldName << "\n";
267
268
269void
270CsTracer::Print (std::ostream &os) const
271{
272 Time time = Simulator::Now ();
273
274 PRINTER ("CacheHits", m_cacheHits);
275 PRINTER ("CacheMisses", m_cacheMisses);
276}
277
278void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700279CsTracer::CacheHits (Ptr<const Interest>, Ptr<const Data>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800280{
281 m_stats.m_cacheHits ++;
282}
283
284void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700285CsTracer::CacheMisses (Ptr<const Interest>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800286{
287 m_stats.m_cacheMisses ++;
288}
289
290
Alexander Afanasyev86287992012-12-10 16:11:50 -0800291} // namespace ndn
292} // namespace ns3