blob: 666f9457887f6a76a61623a219fdc122ea19b06f [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080028#include "ns3/ndn-app.hpp"
29#include "ns3/ndn-interest.hpp"
30#include "ns3/ndn-data.hpp"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080031#include "ns3/simulator.h"
32#include "ns3/node-list.h"
33#include "ns3/log.h"
34
35#include <boost/lexical_cast.hpp>
36#include <boost/make_shared.hpp>
37
38#include <fstream>
39
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040NS_LOG_COMPONENT_DEFINE("ndn.AppDelayTracer");
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080041
42using namespace std;
43
44namespace ns3 {
45namespace ndn {
46
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047static std::list<boost::tuple<boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer>>>>
48 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070049
Alexander Afanasyev5352af32013-07-15 09:51:28 -070050template<class T>
51static inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052NullDeleter(T* ptr)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070053{
54}
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080055
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070056void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057AppDelayTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070058{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070060}
61
62void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063AppDelayTracer::InstallAll(const std::string& file)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080064{
65 using namespace boost;
66 using namespace std;
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -080067
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 std::list<Ptr<AppDelayTracer>> tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070069 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 if (file != "-") {
71 boost::shared_ptr<std::ofstream> os(new std::ofstream());
72 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080073
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 if (!os->is_open()) {
75 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
76 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070077 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 outputStream = os;
80 }
81 else {
82 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
83 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080084
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
86 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
87 tracers.push_back(trace);
88 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080089
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 if (tracers.size() > 0) {
91 // *m_l3RateTrace << "# "; // not necessary for R's read.table
92 tracers.front()->PrintHeader(*outputStream);
93 *outputStream << "\n";
94 }
95
96 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080097}
98
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070099void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100AppDelayTracer::Install(const NodeContainer& nodes, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700101{
102 using namespace boost;
103 using namespace std;
104
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 std::list<Ptr<AppDelayTracer>> tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700106 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 if (file != "-") {
108 boost::shared_ptr<std::ofstream> os(new std::ofstream());
109 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700110
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 if (!os->is_open()) {
112 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
113 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700114 }
115
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 outputStream = os;
117 }
118 else {
119 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
120 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700121
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) {
123 Ptr<AppDelayTracer> trace = Install(*node, outputStream);
124 tracers.push_back(trace);
125 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700126
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 if (tracers.size() > 0) {
128 // *m_l3RateTrace << "# "; // not necessary for R's read.table
129 tracers.front()->PrintHeader(*outputStream);
130 *outputStream << "\n";
131 }
132
133 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700134}
135
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700136void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137AppDelayTracer::Install(Ptr<Node> node, const std::string& file)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700138{
139 using namespace boost;
140 using namespace std;
141
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 std::list<Ptr<AppDelayTracer>> tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700143 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 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);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700147
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 if (!os->is_open()) {
149 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
150 return;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700151 }
152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 outputStream = os;
154 }
155 else {
156 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
157 }
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700158
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159 Ptr<AppDelayTracer> trace = Install(node, outputStream);
160 tracers.push_back(trace);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700161
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 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));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700169}
170
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700171Ptr<AppDelayTracer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800172AppDelayTracer::Install(Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700173{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800174 NS_LOG_DEBUG("Node: " << node->GetId());
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700175
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 Ptr<AppDelayTracer> trace = Create<AppDelayTracer>(outputStream, node);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700177
178 return trace;
179}
180
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800181//////////////////////////////////////////////////////////////////////////////
182//////////////////////////////////////////////////////////////////////////////
183//////////////////////////////////////////////////////////////////////////////
184
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185AppDelayTracer::AppDelayTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node)
186 : m_nodePtr(node)
187 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800188{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 m_node = boost::lexical_cast<string>(m_nodePtr->GetId());
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800190
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800191 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 string name = Names::FindName(node);
194 if (!name.empty()) {
195 m_node = name;
196 }
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800197}
198
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199AppDelayTracer::AppDelayTracer(boost::shared_ptr<std::ostream> os, const std::string& node)
200 : m_node(node)
201 , m_os(os)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800202{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 Connect();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800204}
205
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206AppDelayTracer::~AppDelayTracer(){};
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800207
208void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209AppDelayTracer::Connect()
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800210{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211 Config::ConnectWithoutContext("/NodeList/" + m_node
212 + "/ApplicationList/*/LastRetransmittedInterestDataDelay",
213 MakeCallback(&AppDelayTracer::LastRetransmittedInterestDataDelay,
214 this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800215
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800216 Config::ConnectWithoutContext("/NodeList/" + m_node + "/ApplicationList/*/FirstInterestDataDelay",
217 MakeCallback(&AppDelayTracer::FirstInterestDataDelay, this));
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800218}
219
220void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800221AppDelayTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800222{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800223 os << "Time"
224 << "\t"
225 << "Node"
226 << "\t"
227 << "AppId"
228 << "\t"
229 << "SeqNo"
230 << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800231
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800232 << "Type"
233 << "\t"
234 << "DelayS"
235 << "\t"
236 << "DelayUS"
237 << "\t"
238 << "RetxCount"
239 << "\t"
240 << "HopCount"
241 << "";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800242}
243
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800244void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800245AppDelayTracer::LastRetransmittedInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay,
246 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800247{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800248 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800249 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800250 << "LastDelay"
251 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << 1 << "\t"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800252 << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800253}
Alexander Afanasyevfc8425c2013-01-31 13:33:49 -0800254
255void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800256AppDelayTracer::FirstInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount,
257 int32_t hopCount)
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800258{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800259 *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t"
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800260 << seqno << "\t"
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800261 << "FullDelay"
262 << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << retxCount
263 << "\t" << hopCount << "\n";
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800264}
265
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800266} // namespace ndn
267} // namespace ns3