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