blob: 0c1becb4c284af3c927b27327eb4e1adb5c6cfd2 [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 Afanasyev3fe94dc2013-08-09 17:12:12 -070047static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > > g_tracers;
48
Alexander Afanasyev5352af32013-07-15 09:51:28 -070049template<class T>
50static inline void
51NullDeleter (T *ptr)
52{
53}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080054
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070055void
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080056CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
57{
58 using namespace boost;
59 using namespace std;
60
61 std::list<Ptr<CsTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070062 boost::shared_ptr<std::ostream> outputStream;
63 if (file != "-")
64 {
65 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
66 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080067
Alexander Afanasyev5352af32013-07-15 09:51:28 -070068 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070069 {
70 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
71 return;
72 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070073
74 outputStream = os;
75 }
76 else
77 {
78 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
79 }
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080080
81 for (NodeList::Iterator node = NodeList::Begin ();
82 node != NodeList::End ();
83 node++)
84 {
Alexander Afanasyev5352af32013-07-15 09:51:28 -070085 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080086 tracers.push_back (trace);
87 }
88
89 if (tracers.size () > 0)
90 {
91 // *m_l3RateTrace << "# "; // not necessary for R's read.table
92 tracers.front ()->PrintHeader (*outputStream);
93 *outputStream << "\n";
94 }
95
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070096 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080097}
98
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070099void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700100CsTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
101{
102 using namespace boost;
103 using namespace std;
104
105 std::list<Ptr<CsTracer> > tracers;
106 boost::shared_ptr<std::ostream> outputStream;
107 if (file != "-")
108 {
109 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
110 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
111
112 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700113 {
114 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
115 return;
116 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700117
118 outputStream = os;
119 }
120 else
121 {
122 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
123 }
124
125 for (NodeContainer::Iterator node = nodes.Begin ();
126 node != nodes.End ();
127 node++)
128 {
129 Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod);
130 tracers.push_back (trace);
131 }
132
133 if (tracers.size () > 0)
134 {
135 // *m_l3RateTrace << "# "; // not necessary for R's read.table
136 tracers.front ()->PrintHeader (*outputStream);
137 *outputStream << "\n";
138 }
139
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700140 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700141}
142
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700143void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700144CsTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
145{
146 using namespace boost;
147 using namespace std;
148
149 std::list<Ptr<CsTracer> > tracers;
150 boost::shared_ptr<std::ostream> outputStream;
151 if (file != "-")
152 {
153 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
154 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
155
156 if (!os->is_open ())
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700157 {
158 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
159 return;
160 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700161
162 outputStream = os;
163 }
164 else
165 {
166 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
167 }
168
169 Ptr<CsTracer> trace = Install (node, outputStream, averagingPeriod);
170 tracers.push_back (trace);
171
172 if (tracers.size () > 0)
173 {
174 // *m_l3RateTrace << "# "; // not necessary for R's read.table
175 tracers.front ()->PrintHeader (*outputStream);
176 *outputStream << "\n";
177 }
178
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700179 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700180}
181
182
183Ptr<CsTracer>
184CsTracer::Install (Ptr<Node> node,
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700185 boost::shared_ptr<std::ostream> outputStream,
186 Time averagingPeriod/* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700187{
188 NS_LOG_DEBUG ("Node: " << node->GetId ());
189
190 Ptr<CsTracer> trace = Create<CsTracer> (outputStream, node);
191 trace->SetAveragingPeriod (averagingPeriod);
192
193 return trace;
194}
195
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800196//////////////////////////////////////////////////////////////////////////////
197//////////////////////////////////////////////////////////////////////////////
198//////////////////////////////////////////////////////////////////////////////
199
200CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800201: m_nodePtr (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800202, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800203{
204 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
205
206 Connect ();
207
208 string name = Names::FindName (node);
209 if (!name.empty ())
210 {
211 m_node = name;
212 }
213}
214
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800215CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800216: m_node (node)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800217, m_os (os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800218{
219 Connect ();
220}
221
222CsTracer::~CsTracer ()
223{
224};
225
226
227void
228CsTracer::Connect ()
229{
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800230 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits",
231 MakeCallback (&CsTracer::CacheHits, this));
232 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses",
233 MakeCallback (&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800234
235 Reset ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800236}
237
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800238
239void
240CsTracer::SetAveragingPeriod (const Time &period)
241{
242 m_period = period;
243 m_printEvent.Cancel ();
244 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
245}
246
247void
248CsTracer::PeriodicPrinter ()
249{
250 Print (*m_os);
251 Reset ();
252
253 m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this);
254}
255
256void
257CsTracer::PrintHeader (std::ostream &os) const
258{
259 os << "Time" << "\t"
260
261 << "Node" << "\t"
262
263 << "Type" << "\t"
264 << "Packets" << "\t";
265}
266
267void
268CsTracer::Reset ()
269{
270 m_stats.Reset();
271}
272
273#define PRINTER(printName, fieldName) \
274 os << time.ToDouble (Time::S) << "\t" \
275 << m_node << "\t" \
276 << printName << "\t" \
277 << m_stats.fieldName << "\n";
278
279
280void
281CsTracer::Print (std::ostream &os) const
282{
283 Time time = Simulator::Now ();
284
285 PRINTER ("CacheHits", m_cacheHits);
286 PRINTER ("CacheMisses", m_cacheMisses);
287}
288
289void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700290CsTracer::CacheHits (Ptr<const Interest>, Ptr<const Data>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800291{
292 m_stats.m_cacheHits ++;
293}
294
295void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700296CsTracer::CacheMisses (Ptr<const Interest>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800297{
298 m_stats.m_cacheMisses ++;
299}
300
301
Alexander Afanasyev86287992012-12-10 16:11:50 -0800302} // namespace ndn
303} // namespace ns3