Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 UCLA |
| 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-l3-rate-tracer.h" |
| 22 | #include "ns3/node.h" |
| 23 | #include "ns3/packet.h" |
| 24 | #include "ns3/config.h" |
| 25 | #include "ns3/callback.h" |
| 26 | #include "ns3/simulator.h" |
| 27 | #include "ns3/log.h" |
| 28 | #include "ns3/node-list.h" |
| 29 | |
| 30 | #include "ns3/ndn-app.h" |
| 31 | #include "ns3/ndn-face.h" |
| 32 | #include "ns3/ndn-interest.h" |
| 33 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 34 | #include "ns3/ndn-pit-entry.h" |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 35 | |
| 36 | #include <fstream> |
| 37 | #include <boost/lexical_cast.hpp> |
| 38 | |
| 39 | using namespace boost; |
| 40 | using namespace std; |
| 41 | |
| 42 | NS_LOG_COMPONENT_DEFINE ("ndn.L3RateTracer"); |
| 43 | |
| 44 | namespace ns3 { |
| 45 | namespace ndn { |
| 46 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 47 | template<class T> |
| 48 | static inline void |
| 49 | NullDeleter (T *ptr) |
| 50 | { |
| 51 | } |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 52 | |
| 53 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer> > > |
| 54 | L3RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 55 | { |
| 56 | std::list<Ptr<L3RateTracer> > tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 57 | boost::shared_ptr<std::ostream> outputStream; |
| 58 | if (file != "-") |
| 59 | { |
| 60 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 61 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 63 | if (!os->is_open ()) |
| 64 | return boost::make_tuple (outputStream, tracers); |
| 65 | |
| 66 | outputStream = os; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 71 | } |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 72 | |
| 73 | for (NodeList::Iterator node = NodeList::Begin (); |
| 74 | node != NodeList::End (); |
| 75 | node++) |
| 76 | { |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 77 | Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 78 | tracers.push_back (trace); |
| 79 | } |
| 80 | |
| 81 | if (tracers.size () > 0) |
| 82 | { |
| 83 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 84 | tracers.front ()->PrintHeader (*outputStream); |
| 85 | *outputStream << "\n"; |
| 86 | } |
| 87 | |
| 88 | return boost::make_tuple (outputStream, tracers); |
| 89 | } |
| 90 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 91 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer> > > |
| 92 | L3RateTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 93 | { |
| 94 | using namespace boost; |
| 95 | using namespace std; |
| 96 | |
| 97 | std::list<Ptr<L3RateTracer> > tracers; |
| 98 | boost::shared_ptr<std::ostream> outputStream; |
| 99 | if (file != "-") |
| 100 | { |
| 101 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 102 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 103 | |
| 104 | if (!os->is_open ()) |
| 105 | return boost::make_tuple (outputStream, tracers); |
| 106 | |
| 107 | outputStream = os; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 112 | } |
| 113 | |
| 114 | for (NodeContainer::Iterator node = nodes.Begin (); |
| 115 | node != nodes.End (); |
| 116 | node++) |
| 117 | { |
| 118 | Ptr<L3RateTracer> trace = Install (*node, outputStream, averagingPeriod); |
| 119 | tracers.push_back (trace); |
| 120 | } |
| 121 | |
| 122 | if (tracers.size () > 0) |
| 123 | { |
| 124 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 125 | tracers.front ()->PrintHeader (*outputStream); |
| 126 | *outputStream << "\n"; |
| 127 | } |
| 128 | |
| 129 | return boost::make_tuple (outputStream, tracers); |
| 130 | } |
| 131 | |
| 132 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3RateTracer> > > |
| 133 | L3RateTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 134 | { |
| 135 | using namespace boost; |
| 136 | using namespace std; |
| 137 | |
| 138 | std::list<Ptr<L3RateTracer> > tracers; |
| 139 | boost::shared_ptr<std::ostream> outputStream; |
| 140 | if (file != "-") |
| 141 | { |
| 142 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 143 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 144 | |
| 145 | if (!os->is_open ()) |
| 146 | return boost::make_tuple (outputStream, tracers); |
| 147 | |
| 148 | outputStream = os; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 153 | } |
| 154 | |
| 155 | Ptr<L3RateTracer> trace = Install (node, outputStream, averagingPeriod); |
| 156 | tracers.push_back (trace); |
| 157 | |
| 158 | if (tracers.size () > 0) |
| 159 | { |
| 160 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 161 | tracers.front ()->PrintHeader (*outputStream); |
| 162 | *outputStream << "\n"; |
| 163 | } |
| 164 | |
| 165 | return boost::make_tuple (outputStream, tracers); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | Ptr<L3RateTracer> |
| 170 | L3RateTracer::Install (Ptr<Node> node, |
| 171 | boost::shared_ptr<std::ostream> outputStream, |
| 172 | Time averagingPeriod/* = Seconds (0.5)*/) |
| 173 | { |
| 174 | NS_LOG_DEBUG ("Node: " << node->GetId ()); |
| 175 | |
| 176 | Ptr<L3RateTracer> trace = Create<L3RateTracer> (outputStream, node); |
| 177 | trace->SetAveragingPeriod (averagingPeriod); |
| 178 | |
| 179 | return trace; |
| 180 | } |
| 181 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 182 | |
| 183 | L3RateTracer::L3RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
| 184 | : L3Tracer (node) |
| 185 | , m_os (os) |
| 186 | { |
| 187 | SetAveragingPeriod (Seconds (1.0)); |
| 188 | } |
| 189 | |
| 190 | L3RateTracer::L3RateTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
| 191 | : L3Tracer (node) |
| 192 | , m_os (os) |
| 193 | { |
| 194 | SetAveragingPeriod (Seconds (1.0)); |
| 195 | } |
| 196 | |
| 197 | L3RateTracer::~L3RateTracer () |
| 198 | { |
| 199 | m_printEvent.Cancel (); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | L3RateTracer::SetAveragingPeriod (const Time &period) |
| 204 | { |
| 205 | m_period = period; |
| 206 | m_printEvent.Cancel (); |
| 207 | m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter, this); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | L3RateTracer::PeriodicPrinter () |
| 212 | { |
| 213 | Print (*m_os); |
| 214 | Reset (); |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 215 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 216 | m_printEvent = Simulator::Schedule (m_period, &L3RateTracer::PeriodicPrinter, this); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | L3RateTracer::PrintHeader (std::ostream &os) const |
| 221 | { |
| 222 | os << "Time" << "\t" |
| 223 | |
| 224 | << "Node" << "\t" |
| 225 | << "FaceId" << "\t" |
| 226 | << "FaceDescr" << "\t" |
| 227 | |
| 228 | << "Type" << "\t" |
| 229 | << "Packets" << "\t" |
| 230 | << "Kilobytes" << "\t" |
| 231 | << "PacketRaw" << "\t" |
| 232 | << "KilobytesRaw"; |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | L3RateTracer::Reset () |
| 237 | { |
| 238 | for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 239 | stats != m_stats.end (); |
| 240 | stats++) |
| 241 | { |
| 242 | stats->second.get<0> ().Reset (); |
| 243 | stats->second.get<1> ().Reset (); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | const double alpha = 0.8; |
| 248 | |
| 249 | #define STATS(INDEX) stats->second.get<INDEX> () |
| 250 | #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S) |
| 251 | |
| 252 | #define PRINTER(printName, fieldName) \ |
| 253 | STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \ |
| 254 | STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \ |
| 255 | \ |
| 256 | os << time.ToDouble (Time::S) << "\t" \ |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 257 | << m_node << "\t"; \ |
| 258 | if (stats->first) \ |
| 259 | { \ |
| 260 | os \ |
| 261 | << stats->first->GetId () << "\t" \ |
| 262 | << *stats->first << "\t"; \ |
| 263 | } \ |
| 264 | else \ |
| 265 | { \ |
| 266 | os << "-1\tall\t"; \ |
| 267 | } \ |
| 268 | os \ |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 269 | << printName << "\t" \ |
| 270 | << STATS(2).fieldName << "\t" \ |
| 271 | << STATS(3).fieldName << "\t" \ |
| 272 | << STATS(0).fieldName << "\t" \ |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 273 | << STATS(1).fieldName / 1024.0 << "\n"; |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 274 | |
| 275 | void |
| 276 | L3RateTracer::Print (std::ostream &os) const |
| 277 | { |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 278 | Time time = Simulator::Now (); |
| 279 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 280 | for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 281 | stats != m_stats.end (); |
| 282 | stats++) |
| 283 | { |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 284 | if (!stats->first) |
| 285 | continue; |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 286 | |
| 287 | PRINTER ("InInterests", m_inInterests); |
| 288 | PRINTER ("OutInterests", m_outInterests); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 289 | PRINTER ("DropInterests", m_dropInterests); |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 290 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 291 | PRINTER ("InNacks", m_inNacks); |
| 292 | PRINTER ("OutNacks", m_outNacks); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 293 | PRINTER ("DropNacks", m_dropNacks); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 294 | |
| 295 | PRINTER ("InData", m_inData); |
| 296 | PRINTER ("OutData", m_outData); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 297 | PRINTER ("DropData", m_dropData); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 298 | } |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 299 | |
| 300 | { |
| 301 | std::map<Ptr<const Face>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.find (Ptr<const Face> (0)); |
Alexander Afanasyev | 6692e0b | 2013-05-06 10:38:50 -0700 | [diff] [blame] | 302 | if (stats != m_stats.end ()) |
| 303 | { |
| 304 | PRINTER ("SatisfiedInterests", m_satisfiedInterests); |
| 305 | PRINTER ("TimedOutInterests", m_timedOutInterests); |
| 306 | } |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 307 | } |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
| 311 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 312 | L3RateTracer::OutInterests (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 313 | { |
| 314 | m_stats[face].get<0> ().m_outInterests ++; |
| 315 | m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize (); |
| 316 | } |
| 317 | |
| 318 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 319 | L3RateTracer::InInterests (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 320 | { |
| 321 | m_stats[face].get<0> ().m_inInterests ++; |
| 322 | m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize (); |
| 323 | } |
| 324 | |
| 325 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 326 | L3RateTracer::DropInterests (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 327 | { |
| 328 | m_stats[face].get<0> ().m_dropInterests ++; |
| 329 | m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize (); |
| 330 | } |
| 331 | |
| 332 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 333 | L3RateTracer::OutNacks (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 334 | { |
| 335 | m_stats[face].get<0> ().m_outNacks ++; |
| 336 | m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize (); |
| 337 | } |
| 338 | |
| 339 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 340 | L3RateTracer::InNacks (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 341 | { |
| 342 | m_stats[face].get<0> ().m_inNacks ++; |
| 343 | m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize (); |
| 344 | } |
| 345 | |
| 346 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 347 | L3RateTracer::DropNacks (Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 348 | { |
| 349 | m_stats[face].get<0> ().m_dropNacks ++; |
| 350 | m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize (); |
| 351 | } |
| 352 | |
| 353 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 354 | L3RateTracer::OutData (Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 355 | bool fromCache, Ptr<const Face> face) |
| 356 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 357 | m_stats[face].get<0> ().m_outData ++; |
| 358 | m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 362 | L3RateTracer::InData (Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 363 | Ptr<const Face> face) |
| 364 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 365 | m_stats[face].get<0> ().m_inData ++; |
| 366 | m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame^] | 370 | L3RateTracer::DropData (Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 371 | Ptr<const Face> face) |
| 372 | { |
| 373 | m_stats[face].get<0> ().m_dropData ++; |
| 374 | m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize (); |
| 375 | } |
| 376 | |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 377 | void |
| 378 | L3RateTracer::SatisfiedInterests (Ptr<const pit::Entry>) |
| 379 | { |
| 380 | m_stats[0].get<0> ().m_satisfiedInterests ++; |
| 381 | // no "size" stats |
| 382 | } |
| 383 | |
| 384 | void |
| 385 | L3RateTracer::TimedOutInterests (Ptr<const pit::Entry>) |
| 386 | { |
| 387 | m_stats[0].get<0> ().m_timedOutInterests ++; |
| 388 | // no "size" stats |
| 389 | } |
| 390 | |
| 391 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 392 | } // namespace ndn |
| 393 | } // namespace ns3 |