blob: 63fdf733b709636f8e63a4e410b7d2ea3792b477 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-app-delay-tracer.hpp"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080021#include "ns3/node.h"
22#include "ns3/packet.h"
23#include "ns3/config.h"
24#include "ns3/names.h"
25#include "ns3/callback.h"
26
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080027#include "apps/ndn-app.hpp"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080028#include "ns3/simulator.h"
29#include "ns3/node-list.h"
30#include "ns3/log.h"
31
32#include <boost/lexical_cast.hpp>
33#include <boost/make_shared.hpp>
34
35#include <fstream>
36
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_LOG_COMPONENT_DEFINE("ndn.AppDelayTracer");
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080038
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080039namespace ns3 {
40namespace ndn {
41
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080042static std::list<std::tuple<shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer>>>>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070044
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070045void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046AppDelayTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070047{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070049}
50
51void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052AppDelayTracer::InstallAll(const std::string& file)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080053{
54 using namespace boost;
55 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080056
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 std::list<Ptr<AppDelayTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080058 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080060 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080062
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 if (!os->is_open()) {
64 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
65 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070066 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080067
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 outputStream = os;
69 }
70 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080071 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080073
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
75 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
76 tracers.push_back(trace);
77 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 if (tracers.size() > 0) {
80 // *m_l3RateTrace << "# "; // not necessary for R's read.table
81 tracers.front()->PrintHeader(*outputStream);
82 *outputStream << "\n";
83 }
84
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080085 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080086}
87
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070088void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089AppDelayTracer::Install(const NodeContainer& nodes, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070090{
91 using namespace boost;
92 using namespace std;
93
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 std::list<Ptr<AppDelayTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080095 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080097 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -070099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 if (!os->is_open()) {
101 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
102 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700103 }
104
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 outputStream = os;
106 }
107 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800108 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700110
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
112 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
113 tracers.push_back(trace);
114 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700115
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 if (tracers.size() > 0) {
117 // *m_l3RateTrace << "# "; // not necessary for R's read.table
118 tracers.front()->PrintHeader(*outputStream);
119 *outputStream << "\n";
120 }
121
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800122 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700123}
124
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700125void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126AppDelayTracer::Install(Ptr<Node> node, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700127{
128 using namespace boost;
129 using namespace std;
130
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 std::list<Ptr<AppDelayTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800132 shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800134 shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 if (!os->is_open()) {
138 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
139 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700140 }
141
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 outputStream = os;
143 }
144 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800145 outputStream = shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700147
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 Ptr<AppDelayTracer> trace = Install(node, outputStream);
149 tracers.push_back(trace);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700150
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 if (tracers.size() > 0) {
152 // *m_l3RateTrace << "# "; // not necessary for R's read.table
153 tracers.front()->PrintHeader(*outputStream);
154 *outputStream << "\n";
155 }
156
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800157 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700158}
159
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700160Ptr<AppDelayTracer>
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800161AppDelayTracer::Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700162{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 NS_LOG_DEBUG("Node: " << node->GetId());
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700164
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 Ptr<AppDelayTracer> trace = Create<AppDelayTracer>(outputStream, node);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700166
167 return trace;
168}
169
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800170//////////////////////////////////////////////////////////////////////////////
171//////////////////////////////////////////////////////////////////////////////
172//////////////////////////////////////////////////////////////////////////////
173
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800174AppDelayTracer::AppDelayTracer(shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 : m_nodePtr(node)
176 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800177{
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800178 m_node = boost::lexical_cast<std::string>(m_nodePtr->GetId());
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800179
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800181
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800182 std::string name = Names::FindName(node);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183 if (!name.empty()) {
184 m_node = name;
185 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800186}
187
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800188AppDelayTracer::AppDelayTracer(shared_ptr<std::ostream> os, const std::string& node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 : m_node(node)
190 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800191{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800192 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800193}
194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195AppDelayTracer::~AppDelayTracer(){};
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800196
197void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198AppDelayTracer::Connect()
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800199{
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400200 Config::ConnectWithoutContextFailSafe("/NodeList/" + m_node + "/ApplicationList/*/LastRetransmittedInterestDataDelay",
201 MakeCallback(&AppDelayTracer::LastRetransmittedInterestDataDelay,
202 this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800203
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400204 Config::ConnectWithoutContextFailSafe("/NodeList/" + m_node + "/ApplicationList/*/FirstInterestDataDelay",
205 MakeCallback(&AppDelayTracer::FirstInterestDataDelay, this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800206}
207
208void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209AppDelayTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800210{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211 os << "Time"
212 << "\t"
213 << "Node"
214 << "\t"
215 << "AppId"
216 << "\t"
217 << "SeqNo"
218 << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800219
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800220 << "Type"
221 << "\t"
222 << "DelayS"
223 << "\t"
224 << "DelayUS"
225 << "\t"
226 << "RetxCount"
227 << "\t"
228 << "HopCount"
229 << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800230}
231
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800232void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800233AppDelayTracer::LastRetransmittedInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay,
234 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800235{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800236 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800237 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800238 << "LastDelay"
239 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << 1 << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800240 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800241}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800242
243void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800244AppDelayTracer::FirstInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount,
245 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800246{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800247 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800248 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800249 << "FullDelay"
250 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << retxCount
251 << "\t" << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800252}
253
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800254} // namespace ndn
255} // namespace ns3