blob: 1c135a642574f97f93de1edf94933121e247bf17 [file] [log] [blame]
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
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 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
Alexander Afanasyev0c395372014-12-20 15:54:02 -080021#include "ndn-app-delay-tracer.hpp"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080022#include "ns3/node.h"
23#include "ns3/packet.h"
24#include "ns3/config.h"
25#include "ns3/names.h"
26#include "ns3/callback.h"
27
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080028#include "apps/ndn-app.hpp"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080029#include "ns3/simulator.h"
30#include "ns3/node-list.h"
31#include "ns3/log.h"
32
33#include <boost/lexical_cast.hpp>
34#include <boost/make_shared.hpp>
35
36#include <fstream>
37
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038NS_LOG_COMPONENT_DEFINE("ndn.AppDelayTracer");
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080039
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080040namespace ns3 {
41namespace ndn {
42
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080043static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer>>>>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070045
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070046void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047AppDelayTracer::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 -080053AppDelayTracer::InstallAll(const std::string& file)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080054{
55 using namespace boost;
56 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080057
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 std::list<Ptr<AppDelayTracer>> 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 Afanasyevdb64ff12013-01-18 16:37:31 -080063
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 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080068
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 Afanasyevdb64ff12013-01-18 16:37:31 -080074
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
76 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
77 tracers.push_back(trace);
78 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080079
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 Afanasyevdb64ff12013-01-18 16:37:31 -080087}
88
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070089void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090AppDelayTracer::Install(const NodeContainer& nodes, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070091{
92 using namespace boost;
93 using namespace std;
94
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 std::list<Ptr<AppDelayTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080096 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080098 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700100
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 if (!os->is_open()) {
102 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
103 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700104 }
105
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 outputStream = os;
107 }
108 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800109 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700111
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
113 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
114 tracers.push_back(trace);
115 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700116
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 if (tracers.size() > 0) {
118 // *m_l3RateTrace << "# "; // not necessary for R's read.table
119 tracers.front()->PrintHeader(*outputStream);
120 *outputStream << "\n";
121 }
122
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800123 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700124}
125
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700126void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127AppDelayTracer::Install(Ptr<Node> node, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700128{
129 using namespace boost;
130 using namespace std;
131
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 std::list<Ptr<AppDelayTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800133 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800135 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700137
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 if (!os->is_open()) {
139 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
140 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700141 }
142
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 outputStream = os;
144 }
145 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800146 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700148
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 Ptr<AppDelayTracer> trace = Install(node, outputStream);
150 tracers.push_back(trace);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700151
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 if (tracers.size() > 0) {
153 // *m_l3RateTrace << "# "; // not necessary for R's read.table
154 tracers.front()->PrintHeader(*outputStream);
155 *outputStream << "\n";
156 }
157
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800158 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700159}
160
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700161Ptr<AppDelayTracer>
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800162AppDelayTracer::Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700163{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164 NS_LOG_DEBUG("Node: " << node->GetId());
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700165
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166 Ptr<AppDelayTracer> trace = Create<AppDelayTracer>(outputStream, node);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700167
168 return trace;
169}
170
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800171//////////////////////////////////////////////////////////////////////////////
172//////////////////////////////////////////////////////////////////////////////
173//////////////////////////////////////////////////////////////////////////////
174
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800175AppDelayTracer::AppDelayTracer(shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 : m_nodePtr(node)
177 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800178{
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800179 m_node = boost::lexical_cast<std::string>(m_nodePtr->GetId());
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800180
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800181 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800182
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800183 std::string name = Names::FindName(node);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800184 if (!name.empty()) {
185 m_node = name;
186 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800187}
188
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800189AppDelayTracer::AppDelayTracer(shared_ptr<std::ostream> os, const std::string& node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800190 : m_node(node)
191 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800192{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800194}
195
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196AppDelayTracer::~AppDelayTracer(){};
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800197
198void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199AppDelayTracer::Connect()
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800200{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201 Config::ConnectWithoutContext("/NodeList/" + m_node
202 + "/ApplicationList/*/LastRetransmittedInterestDataDelay",
203 MakeCallback(&AppDelayTracer::LastRetransmittedInterestDataDelay,
204 this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800205
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206 Config::ConnectWithoutContext("/NodeList/" + m_node + "/ApplicationList/*/FirstInterestDataDelay",
207 MakeCallback(&AppDelayTracer::FirstInterestDataDelay, this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800208}
209
210void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211AppDelayTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800212{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 os << "Time"
214 << "\t"
215 << "Node"
216 << "\t"
217 << "AppId"
218 << "\t"
219 << "SeqNo"
220 << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800221
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800222 << "Type"
223 << "\t"
224 << "DelayS"
225 << "\t"
226 << "DelayUS"
227 << "\t"
228 << "RetxCount"
229 << "\t"
230 << "HopCount"
231 << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800232}
233
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800234void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800235AppDelayTracer::LastRetransmittedInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay,
236 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800237{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800238 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800239 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800240 << "LastDelay"
241 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << 1 << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800242 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800243}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800244
245void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800246AppDelayTracer::FirstInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount,
247 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800248{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800249 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800250 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800251 << "FullDelay"
252 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << retxCount
253 << "\t" << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800254}
255
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800256} // namespace ndn
257} // namespace ns3