Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-app-delay-tracer.hpp" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 22 | #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 Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 28 | #include "ns3/ndn-app.hpp" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 29 | #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 Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | NS_LOG_COMPONENT_DEFINE("ndn.AppDelayTracer"); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | namespace ns3 { |
| 43 | namespace ndn { |
| 44 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | static std::list<boost::tuple<boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer>>>> |
| 46 | g_tracers; |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 48 | template<class T> |
| 49 | static inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | NullDeleter(T* ptr) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 54 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | AppDelayTracer::Destroy() |
Alexander Afanasyev | db5f3b6 | 2013-08-09 17:42:12 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | g_tracers.clear(); |
Alexander Afanasyev | db5f3b6 | 2013-08-09 17:42:12 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | AppDelayTracer::InstallAll(const std::string& file) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 62 | { |
| 63 | using namespace boost; |
| 64 | using namespace std; |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | std::list<Ptr<AppDelayTracer>> tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 67 | boost::shared_ptr<std::ostream> outputStream; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | 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 Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | if (!os->is_open()) { |
| 73 | NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled"); |
| 74 | return; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 75 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 77 | outputStream = os; |
| 78 | } |
| 79 | else { |
| 80 | outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>); |
| 81 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 84 | Ptr<AppDelayTracer> trace = Install(*node, outputStream); |
| 85 | tracers.push_back(trace); |
| 86 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | 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 Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 97 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | AppDelayTracer::Install(const NodeContainer& nodes, const std::string& file) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 99 | { |
| 100 | using namespace boost; |
| 101 | using namespace std; |
| 102 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | std::list<Ptr<AppDelayTracer>> tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 104 | boost::shared_ptr<std::ostream> outputStream; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | if (file != "-") { |
| 106 | boost::shared_ptr<std::ofstream> os(new std::ofstream()); |
| 107 | os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | if (!os->is_open()) { |
| 110 | NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled"); |
| 111 | return; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | outputStream = os; |
| 115 | } |
| 116 | else { |
| 117 | outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>); |
| 118 | } |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 120 | for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) { |
| 121 | Ptr<AppDelayTracer> trace = Install(*node, outputStream); |
| 122 | tracers.push_back(trace); |
| 123 | } |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | if (tracers.size() > 0) { |
| 126 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 127 | tracers.front()->PrintHeader(*outputStream); |
| 128 | *outputStream << "\n"; |
| 129 | } |
| 130 | |
| 131 | g_tracers.push_back(boost::make_tuple(outputStream, tracers)); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 134 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | AppDelayTracer::Install(Ptr<Node> node, const std::string& file) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 136 | { |
| 137 | using namespace boost; |
| 138 | using namespace std; |
| 139 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | std::list<Ptr<AppDelayTracer>> tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 141 | boost::shared_ptr<std::ostream> outputStream; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 142 | if (file != "-") { |
| 143 | boost::shared_ptr<std::ofstream> os(new std::ofstream()); |
| 144 | os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | if (!os->is_open()) { |
| 147 | NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled"); |
| 148 | return; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | outputStream = os; |
| 152 | } |
| 153 | else { |
| 154 | outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>); |
| 155 | } |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 157 | Ptr<AppDelayTracer> trace = Install(node, outputStream); |
| 158 | tracers.push_back(trace); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | if (tracers.size() > 0) { |
| 161 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 162 | tracers.front()->PrintHeader(*outputStream); |
| 163 | *outputStream << "\n"; |
| 164 | } |
| 165 | |
| 166 | g_tracers.push_back(boost::make_tuple(outputStream, tracers)); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 169 | Ptr<AppDelayTracer> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 170 | AppDelayTracer::Install(Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 171 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 172 | NS_LOG_DEBUG("Node: " << node->GetId()); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 174 | Ptr<AppDelayTracer> trace = Create<AppDelayTracer>(outputStream, node); |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 175 | |
| 176 | return trace; |
| 177 | } |
| 178 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 179 | ////////////////////////////////////////////////////////////////////////////// |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 183 | AppDelayTracer::AppDelayTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
| 184 | : m_nodePtr(node) |
| 185 | , m_os(os) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 186 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | m_node = boost::lexical_cast<string>(m_nodePtr->GetId()); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 189 | Connect(); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 191 | string name = Names::FindName(node); |
| 192 | if (!name.empty()) { |
| 193 | m_node = name; |
| 194 | } |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 197 | AppDelayTracer::AppDelayTracer(boost::shared_ptr<std::ostream> os, const std::string& node) |
| 198 | : m_node(node) |
| 199 | , m_os(os) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 200 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 201 | Connect(); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 204 | AppDelayTracer::~AppDelayTracer(){}; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 205 | |
| 206 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 207 | AppDelayTracer::Connect() |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 209 | Config::ConnectWithoutContext("/NodeList/" + m_node |
| 210 | + "/ApplicationList/*/LastRetransmittedInterestDataDelay", |
| 211 | MakeCallback(&AppDelayTracer::LastRetransmittedInterestDataDelay, |
| 212 | this)); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 214 | Config::ConnectWithoutContext("/NodeList/" + m_node + "/ApplicationList/*/FirstInterestDataDelay", |
| 215 | MakeCallback(&AppDelayTracer::FirstInterestDataDelay, this)); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 219 | AppDelayTracer::PrintHeader(std::ostream& os) const |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 220 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 221 | os << "Time" |
| 222 | << "\t" |
| 223 | << "Node" |
| 224 | << "\t" |
| 225 | << "AppId" |
| 226 | << "\t" |
| 227 | << "SeqNo" |
| 228 | << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 230 | << "Type" |
| 231 | << "\t" |
| 232 | << "DelayS" |
| 233 | << "\t" |
| 234 | << "DelayUS" |
| 235 | << "\t" |
| 236 | << "RetxCount" |
| 237 | << "\t" |
| 238 | << "HopCount" |
| 239 | << ""; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 242 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 243 | AppDelayTracer::LastRetransmittedInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay, |
| 244 | int32_t hopCount) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 245 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 246 | *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 247 | << seqno << "\t" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 248 | << "LastDelay" |
| 249 | << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << 1 << "\t" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 250 | << hopCount << "\n"; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 251 | } |
Alexander Afanasyev | fc8425c | 2013-01-31 13:33:49 -0800 | [diff] [blame] | 252 | |
| 253 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 254 | AppDelayTracer::FirstInterestDataDelay(Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, |
| 255 | int32_t hopCount) |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 256 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 257 | *m_os << Simulator::Now().ToDouble(Time::S) << "\t" << m_node << "\t" << app->GetId() << "\t" |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 258 | << seqno << "\t" |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 259 | << "FullDelay" |
| 260 | << "\t" << delay.ToDouble(Time::S) << "\t" << delay.ToDouble(Time::US) << "\t" << retxCount |
| 261 | << "\t" << hopCount << "\n"; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 264 | } // namespace ndn |
| 265 | } // namespace ns3 |