blob: 11fd8033fd83ebccf6ada2dff63945160395bb6b [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
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080029#include "apps/ndn-app.hpp"
30#include "model/cs/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
Alexander Afanasyev86287992012-12-10 16:11:50 -080041namespace ns3 {
42namespace ndn {
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080043
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080044static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<CsTracer>>>> g_tracers;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080045
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070046void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047CsTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070048{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070050}
51
52void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053CsTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070054{
55 using namespace boost;
56 using namespace std;
57
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 std::list<Ptr<CsTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080059 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080061 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -070063
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 if (!os->is_open()) {
65 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
66 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070067 }
68
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 outputStream = os;
70 }
71 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080072 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070074
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
76 Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
77 tracers.push_back(trace);
78 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -070079
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 if (tracers.size() > 0) {
81 // *m_l3RateTrace << "# "; // not necessary for R's read.table
82 tracers.front()->PrintHeader(*outputStream);
83 *outputStream << "\n";
84 }
85
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080086 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -070087}
88
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070089void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090CsTracer::Install(const NodeContainer& nodes, const std::string& file,
91 Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070092{
93 using namespace boost;
94 using namespace std;
95
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 std::list<Ptr<CsTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080097 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080099 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700101
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 if (!os->is_open()) {
103 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
104 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700105 }
106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 outputStream = os;
108 }
109 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800110 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700112
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
114 Ptr<CsTracer> trace = Install(*node, outputStream, averagingPeriod);
115 tracers.push_back(trace);
116 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700117
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 if (tracers.size() > 0) {
119 // *m_l3RateTrace << "# "; // not necessary for R's read.table
120 tracers.front()->PrintHeader(*outputStream);
121 *outputStream << "\n";
122 }
123
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800124 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700125}
126
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127void
128CsTracer::Install(Ptr<Node> node, const std::string& file,
129 Time averagingPeriod /* = Seconds (0.5)*/)
130{
131 using namespace boost;
132 using namespace std;
133
134 std::list<Ptr<CsTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800135 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800137 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
139
140 if (!os->is_open()) {
141 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
142 return;
143 }
144
145 outputStream = os;
146 }
147 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800148 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 }
150
151 Ptr<CsTracer> trace = Install(node, outputStream, averagingPeriod);
152 tracers.push_back(trace);
153
154 if (tracers.size() > 0) {
155 // *m_l3RateTrace << "# "; // not necessary for R's read.table
156 tracers.front()->PrintHeader(*outputStream);
157 *outputStream << "\n";
158 }
159
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800160 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161}
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700162
163Ptr<CsTracer>
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800164CsTracer::Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700166{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 NS_LOG_DEBUG("Node: " << node->GetId());
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700168
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 Ptr<CsTracer> trace = Create<CsTracer>(outputStream, node);
170 trace->SetAveragingPeriod(averagingPeriod);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700171
172 return trace;
173}
174
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800175//////////////////////////////////////////////////////////////////////////////
176//////////////////////////////////////////////////////////////////////////////
177//////////////////////////////////////////////////////////////////////////////
178
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800179CsTracer::CsTracer(shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180 : m_nodePtr(node)
181 , m_os(os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800182{
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800183 m_node = boost::lexical_cast<std::string>(m_nodePtr->GetId());
Alexander Afanasyev86287992012-12-10 16:11:50 -0800184
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185 Connect();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800186
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800187 std::string name = Names::FindName(node);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800188 if (!name.empty()) {
189 m_node = name;
190 }
Alexander Afanasyev86287992012-12-10 16:11:50 -0800191}
192
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800193CsTracer::CsTracer(shared_ptr<std::ostream> os, const std::string& node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800194 : m_node(node)
195 , m_os(os)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800196{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 Connect();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800198}
199
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200CsTracer::~CsTracer(){};
Alexander Afanasyev86287992012-12-10 16:11:50 -0800201
202void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203CsTracer::Connect()
Alexander Afanasyev86287992012-12-10 16:11:50 -0800204{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 Ptr<ContentStore> cs = m_nodePtr->GetObject<ContentStore>();
206 cs->TraceConnectWithoutContext("CacheHits", MakeCallback(&CsTracer::CacheHits, this));
207 cs->TraceConnectWithoutContext("CacheMisses", MakeCallback(&CsTracer::CacheMisses, this));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800208
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209 Reset();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800210}
211
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800212void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213CsTracer::SetAveragingPeriod(const Time& period)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800214{
215 m_period = period;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800216 m_printEvent.Cancel();
217 m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800218}
219
220void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800221CsTracer::PeriodicPrinter()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800222{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800223 Print(*m_os);
224 Reset();
225
226 m_printEvent = Simulator::Schedule(m_period, &CsTracer::PeriodicPrinter, this);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800227}
228
229void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800230CsTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800231{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800232 os << "Time"
233 << "\t"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800234
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800235 << "Node"
236 << "\t"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800237
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800238 << "Type"
239 << "\t"
240 << "Packets"
241 << "\t";
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800242}
243
244void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800245CsTracer::Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800246{
247 m_stats.Reset();
248}
249
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800250#define PRINTER(printName, fieldName) \
251 os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << printName << "\t" << m_stats.fieldName \
252 << "\n";
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800253
254void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800255CsTracer::Print(std::ostream& os) const
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800256{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800257 Time time = Simulator::Now();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800258
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800259 PRINTER("CacheHits", m_cacheHits);
260 PRINTER("CacheMisses", m_cacheMisses);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800261}
262
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800263void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700264CsTracer::CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800265{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800266 m_stats.m_cacheHits++;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800267}
268
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800269void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700270CsTracer::CacheMisses(shared_ptr<const Interest>)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800271{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800272 m_stats.m_cacheMisses++;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800273}
274
Alexander Afanasyev86287992012-12-10 16:11:50 -0800275} // namespace ndn
276} // namespace ns3