blob: 74fa3dd1b91dc014267cb619031566c4d06bc838 [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-cs-tracer.hpp"
Alexander Afanasyev86287992012-12-10 16:11:50 -080023#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 Afanasyev0c395372014-12-20 15:54:02 -080029#include "ns3/ndn-app.hpp"
30#include "ns3/ndn-interest.hpp"
31#include "ns3/ndn-data.hpp"
32#include "ns3/ndn-content-store.hpp"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080033#include "ns3/simulator.h"
34#include "ns3/node-list.h"
35#include "ns3/log.h"
36
37#include <boost/lexical_cast.hpp>
38
39#include <fstream>
40
41NS_LOG_COMPONENT_DEFINE ("ndn.CsTracer");
Alexander Afanasyev86287992012-12-10 16:11:50 -080042
43using namespace std;
44
45namespace ns3 {
46namespace ndn {
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080047
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070048static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > > g_tracers;
49
Alexander Afanasyev5352af32013-07-15 09:51:28 -070050template<class T>
51static inline void
52NullDeleter (T *ptr)
53{
54}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080055
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070056void
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070057CsTracer::Destroy ()
58{
59 g_tracers.clear ();
60}
61
62void
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080063CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
64{
65 using namespace boost;
66 using namespace std;
67
68 std::list<Ptr<CsTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070069 boost::shared_ptr<std::ostream> outputStream;
70 if (file != "-")
71 {
72 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
73 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080074
Alexander Afanasyev5352af32013-07-15 09:51:28 -070075 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070076 {
77 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
78 return;
79 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070080
81 outputStream = os;
82 }
83 else
84 {
85 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
86 }
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080087
88 for (NodeList::Iterator node = NodeList::Begin ();
89 node != NodeList::End ();
90 node++)
91 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070092 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080093 tracers.push_back (trace);
94 }
95
96 if (tracers.size () > 0)
97 {
98 // *m_l3RateTrace << "# "; // not necessary for R's read.table
99 tracers.front ()->PrintHeader (*outputStream);
100 *outputStream << "\n";
101 }
102
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700103 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800104}
105
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700106void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700107CsTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
108{
109 using namespace boost;
110 using namespace std;
111
112 std::list<Ptr<CsTracer> > tracers;
113 boost::shared_ptr<std::ostream> outputStream;
114 if (file != "-")
115 {
116 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
117 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
118
119 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700120 {
121 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
122 return;
123 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700124
125 outputStream = os;
126 }
127 else
128 {
129 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
130 }
131
132 for (NodeContainer::Iterator node = nodes.Begin ();
133 node != nodes.End ();
134 node++)
135 {
136 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
137 tracers.push_back (trace);
138 }
139
140 if (tracers.size () > 0)
141 {
142 // *m_l3RateTrace << "# "; // not necessary for R's read.table
143 tracers.front ()->PrintHeader (*outputStream);
144 *outputStream << "\n";
145 }
146
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700147 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700148}
149
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700150void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700151CsTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
152{
153 using namespace boost;
154 using namespace std;
155
156 std::list<Ptr<CsTracer> > tracers;
157 boost::shared_ptr<std::ostream> outputStream;
158 if (file != "-")
159 {
160 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
161 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
162
163 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700164 {
165 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
166 return;
167 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700168
169 outputStream = os;
170 }
171 else
172 {
173 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
174 }
175
176 Ptr<CsTracer> trace = Install (node, outputStream, averagingPeriod);
177 tracers.push_back (trace);
178
179 if (tracers.size () > 0)
180 {
181 // *m_l3RateTrace << "# "; // not necessary for R's read.table
182 tracers.front ()->PrintHeader (*outputStream);
183 *outputStream << "\n";
184 }
185
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700186 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700187}
188
189
190Ptr<CsTracer>
191CsTracer::Install (Ptr<Node> node,
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700192 boost::shared_ptr<std::ostream> outputStream,
193 Time averagingPeriod/* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700194{
195 NS_LOG_DEBUG ("Node: " << node->GetId ());
196
197 Ptr<CsTracer> trace = Create<CsTracer> (outputStream, node);
198 trace->SetAveragingPeriod (averagingPeriod);
199
200 return trace;
201}
202
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800203//////////////////////////////////////////////////////////////////////////////
204//////////////////////////////////////////////////////////////////////////////
205//////////////////////////////////////////////////////////////////////////////
206
207CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800208: m_nodePtr (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800209, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800210{
211 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
212
213 Connect ();
214
215 string name = Names::FindName (node);
216 if (!name.empty ())
217 {
218 m_node = name;
219 }
220}
221
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800222CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800223: m_node (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800224, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800225{
226 Connect ();
227}
228
229CsTracer::~CsTracer ()
230{
231};
232
233
234void
235CsTracer::Connect ()
236{
Alexander Afanasyev260e4092013-08-11 17:12:46 -0700237 Ptr<ContentStore> cs = m_nodePtr->GetObject<ContentStore> ();
238 cs->TraceConnectWithoutContext ("CacheHits", MakeCallback (&CsTracer::CacheHits, this));
239 cs->TraceConnectWithoutContext ("CacheMisses", MakeCallback (&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800240
241 Reset ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800242}
243
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800244
245void
246CsTracer::SetAveragingPeriod (const Time &period)
247{
248 m_period = period;
249 m_printEvent.Cancel ();
250 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
251}
252
253void
254CsTracer::PeriodicPrinter ()
255{
256 Print (*m_os);
257 Reset ();
258
259 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
260}
261
262void
263CsTracer::PrintHeader (std::ostream &os) const
264{
265 os << "Time" << "\t"
266
267 << "Node" << "\t"
268
269 << "Type" << "\t"
270 << "Packets" << "\t";
271}
272
273void
274CsTracer::Reset ()
275{
276 m_stats.Reset();
277}
278
279#define PRINTER(printName, fieldName) \
280 os << time.ToDouble (Time::S) << "\t" \
281 << m_node << "\t" \
282 << printName << "\t" \
283 << m_stats.fieldName << "\n";
284
285
286void
287CsTracer::Print (std::ostream &os) const
288{
289 Time time = Simulator::Now ();
290
291 PRINTER ("CacheHits", m_cacheHits);
292 PRINTER ("CacheMisses", m_cacheMisses);
293}
294
295void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700296CsTracer::CacheHits (Ptr<const Interest>, Ptr<const Data>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800297{
298 m_stats.m_cacheHits ++;
299}
300
301void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700302CsTracer::CacheMisses (Ptr<const Interest>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800303{
304 m_stats.m_cacheMisses ++;
305}
306
307
Alexander Afanasyev86287992012-12-10 16:11:50 -0800308} // namespace ndn
309} // namespace ns3