blob: f470aae48ee743970118aab5e263223c7d508e40 [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"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080030#include "ns3/ndn-content-store.hpp"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080031#include "ns3/simulator.h"
32#include "ns3/node-list.h"
33#include "ns3/log.h"
34
35#include <boost/lexical_cast.hpp>
36
37#include <fstream>
38
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_LOG_COMPONENT_DEFINE("ndn.CsTracer");
Alexander Afanasyev86287992012-12-10 16:11:50 -080040
41using namespace std;
42
43namespace ns3 {
44namespace ndn {
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080045
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046static std::list<boost::tuple<boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer>>>> g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070047
Alexander Afanasyev5352af32013-07-15 09:51:28 -070048template<class T>
49static inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050NullDeleter(T* ptr)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070051{
52}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080053
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070054void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055CsTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070056{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070058}
59
60void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061CsTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070062{
63 using namespace boost;
64 using namespace std;
65
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 std::list<Ptr<CsTracer>> tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070067 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 if (file != "-") {
69 boost::shared_ptr<std::ofstream> os(new std::ofstream());
70 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -070071
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 if (!os->is_open()) {
73 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
74 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070075 }
76
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 outputStream = os;
78 }
79 else {
80 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
81 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070082
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
84 Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
85 tracers.push_back(trace);
86 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070087
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 if (tracers.size() > 0) {
89 // *m_l3RateTrace << "# "; // not necessary for R's read.table
90 tracers.front()->PrintHeader(*outputStream);
91 *outputStream << "\n";
92 }
93
94 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -070095}
96
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070097void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098CsTracer::Install(const NodeContainer& nodes, const std::string& file,
99 Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700100{
101 using namespace boost;
102 using namespace std;
103
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 std::list<Ptr<CsTracer>> tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700105 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 if (file != "-") {
107 boost::shared_ptr<std::ofstream> os(new std::ofstream());
108 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700109
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 if (!os->is_open()) {
111 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
112 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700113 }
114
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 outputStream = os;
116 }
117 else {
118 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
119 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700120
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
122 Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
123 tracers.push_back(trace);
124 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700125
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 if (tracers.size() > 0) {
127 // *m_l3RateTrace << "# "; // not necessary for R's read.table
128 tracers.front()->PrintHeader(*outputStream);
129 *outputStream << "\n";
130 }
131
132 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700133}
134
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135void
136CsTracer::Install(Ptr<Node> node, const std::string& file,
137 Time averagingPeriod /* = Seconds (0.5)*/)
138{
139 using namespace boost;
140 using namespace std;
141
142 std::list<Ptr<CsTracer>> tracers;
143 boost::shared_ptr<std::ostream> outputStream;
144 if (file != "-") {
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 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
150 return;
151 }
152
153 outputStream = os;
154 }
155 else {
156 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
157 }
158
159 Ptr<CsTracer> trace = Install(node, outputStream, averagingPeriod);
160 tracers.push_back(trace);
161
162 if (tracers.size() > 0) {
163 // *m_l3RateTrace << "# "; // not necessary for R's read.table
164 tracers.front()->PrintHeader(*outputStream);
165 *outputStream << "\n";
166 }
167
168 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
169}
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700170
171Ptr<CsTracer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800172CsTracer::Install(Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream,
173 Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700174{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 NS_LOG_DEBUG("Node: " << node->GetId());
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700176
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177 Ptr<CsTracer> trace = Create<CsTracer>(outputStream, node);
178 trace->SetAveragingPeriod(averagingPeriod);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700179
180 return trace;
181}
182
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800183//////////////////////////////////////////////////////////////////////////////
184//////////////////////////////////////////////////////////////////////////////
185//////////////////////////////////////////////////////////////////////////////
186
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800187CsTracer::CsTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node)
188 : m_nodePtr(node)
189 , m_os(os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800190{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800191 m_node = boost::lexical_cast<string>(m_nodePtr->GetId());
Alexander Afanasyev86287992012-12-10 16:11:50 -0800192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 Connect();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 string name = Names::FindName(node);
196 if (!name.empty()) {
197 m_node = name;
198 }
Alexander Afanasyev86287992012-12-10 16:11:50 -0800199}
200
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201CsTracer::CsTracer(boost::shared_ptr<std::ostream> os, const std::string& node)
202 : m_node(node)
203 , m_os(os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800204{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 Connect();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800206}
207
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800208CsTracer::~CsTracer(){};
Alexander Afanasyev86287992012-12-10 16:11:50 -0800209
210void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211CsTracer::Connect()
Alexander Afanasyev86287992012-12-10 16:11:50 -0800212{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 Ptr<ContentStore> cs = m_nodePtr->GetObject<ContentStore>();
214 cs->TraceConnectWithoutContext("CacheHits", MakeCallback(&CsTracer::CacheHits, this));
215 cs->TraceConnectWithoutContext("CacheMisses", MakeCallback(&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800216
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800217 Reset();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800218}
219
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800220void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800221CsTracer::SetAveragingPeriod(const Time& period)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800222{
223 m_period = period;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800224 m_printEvent.Cancel();
225 m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800226}
227
228void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800229CsTracer::PeriodicPrinter()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800230{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800231 Print(*m_os);
232 Reset();
233
234 m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800235}
236
237void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800238CsTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800239{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800240 os << "Time"
241 << "\t"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800242
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800243 << "Node"
244 << "\t"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800245
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800246 << "Type"
247 << "\t"
248 << "Packets"
249 << "\t";
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800250}
251
252void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800253CsTracer::Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800254{
255 m_stats.Reset();
256}
257
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800258#define PRINTER(printName, fieldName) \
259 os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << printName << "\t" << m_stats.fieldName \
260 << "\n";
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800261
262void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800263CsTracer::Print(std::ostream& os) const
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800264{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800265 Time time = Simulator::Now();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800266
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800267 PRINTER("CacheHits", m_cacheHits);
268 PRINTER("CacheMisses", m_cacheMisses);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800269}
270
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800271void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700272CsTracer::CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800273{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800274 m_stats.m_cacheHits++;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800275}
276
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800277void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700278CsTracer::CacheMisses(shared_ptr<const Interest>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800279{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800280 m_stats.m_cacheMisses++;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800281}
282
Alexander Afanasyev86287992012-12-10 16:11:50 -0800283} // namespace ndn
284} // namespace ns3