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-aggregate-tracer.h" |
| 22 | |
| 23 | #include "ns3/node.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/config.h" |
| 26 | #include "ns3/callback.h" |
| 27 | #include "ns3/ndn-app.h" |
| 28 | #include "ns3/ndn-face.h" |
| 29 | #include "ns3/ndn-interest.h" |
| 30 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 31 | #include "ns3/ndn-pit-entry.h" |
| 32 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 33 | #include "ns3/simulator.h" |
| 34 | #include "ns3/node-list.h" |
| 35 | #include "ns3/log.h" |
| 36 | |
| 37 | #include <fstream> |
| 38 | |
| 39 | NS_LOG_COMPONENT_DEFINE ("ndn.L3AggregateTracer"); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 40 | |
| 41 | namespace ns3 { |
| 42 | namespace ndn { |
| 43 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 44 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > > |
| 45 | L3AggregateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 46 | { |
| 47 | using namespace boost; |
| 48 | using namespace std; |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 50 | std::list<Ptr<L3AggregateTracer> > tracers; |
| 51 | boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ()); |
| 52 | outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 53 | |
| 54 | if (!outputStream->is_open ()) |
| 55 | return boost::make_tuple (outputStream, tracers); |
| 56 | |
| 57 | for (NodeList::Iterator node = NodeList::Begin (); |
| 58 | node != NodeList::End (); |
| 59 | node++) |
| 60 | { |
| 61 | NS_LOG_DEBUG ("Node: " << (*node)->GetId ()); |
| 62 | |
| 63 | Ptr<L3AggregateTracer> trace = Create<L3AggregateTracer> (outputStream, *node); |
| 64 | trace->SetAveragingPeriod (averagingPeriod); |
| 65 | tracers.push_back (trace); |
| 66 | } |
| 67 | |
| 68 | if (tracers.size () > 0) |
| 69 | { |
| 70 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 71 | tracers.front ()->PrintHeader (*outputStream); |
| 72 | *outputStream << "\n"; |
| 73 | } |
| 74 | |
| 75 | return boost::make_tuple (outputStream, tracers); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
| 81 | : L3Tracer (node) |
| 82 | , m_os (os) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 83 | { |
| 84 | Reset (); |
| 85 | } |
| 86 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 87 | L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
| 88 | : L3Tracer (node) |
| 89 | , m_os (os) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 90 | { |
| 91 | Reset (); |
| 92 | } |
| 93 | |
| 94 | L3AggregateTracer::~L3AggregateTracer () |
| 95 | { |
| 96 | }; |
| 97 | |
| 98 | void |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 99 | L3AggregateTracer::SetAveragingPeriod (const Time &period) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 101 | m_period = period; |
| 102 | m_printEvent.Cancel (); |
| 103 | m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 106 | void |
| 107 | L3AggregateTracer::PeriodicPrinter () |
| 108 | { |
| 109 | Print (*m_os); |
| 110 | Reset (); |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 112 | m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this); |
| 113 | } |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 114 | |
| 115 | void |
| 116 | L3AggregateTracer::PrintHeader (std::ostream &os) const |
| 117 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 118 | os << "Time" << "\t" |
| 119 | |
| 120 | << "Node" << "\t" |
| 121 | << "FaceId" << "\t" |
| 122 | << "FaceDescr" << "\t" |
| 123 | |
| 124 | << "Type" << "\t" |
| 125 | << "Packets" << "\t" |
| 126 | << "Kilobytes"; |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 130 | L3AggregateTracer::Reset () |
| 131 | { |
| 132 | for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin (); |
| 133 | stats != m_stats.end (); |
| 134 | stats++) |
| 135 | { |
| 136 | stats->second.get<0> ().Reset (); |
| 137 | stats->second.get<1> ().Reset (); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | #define STATS(INDEX) stats->second.get<INDEX> () |
| 143 | |
| 144 | #define PRINTER(printName, fieldName) \ |
| 145 | os << time.ToDouble (Time::S) << "\t" \ |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 146 | << m_node << "\t"; \ |
| 147 | if (stats->first) \ |
| 148 | { \ |
| 149 | os \ |
| 150 | << stats->first->GetId () << "\t" \ |
| 151 | << *stats->first << "\t"; \ |
| 152 | } \ |
| 153 | else \ |
| 154 | { \ |
| 155 | os << "-1\tall\t"; \ |
| 156 | } \ |
| 157 | os \ |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 158 | << printName << "\t" \ |
| 159 | << STATS(0).fieldName << "\t" \ |
| 160 | << STATS(1).fieldName / 1024.0 << "\n"; |
| 161 | |
| 162 | |
| 163 | void |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 164 | L3AggregateTracer::Print (std::ostream &os) const |
| 165 | { |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 166 | Time time = Simulator::Now (); |
| 167 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 168 | for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin (); |
| 169 | stats != m_stats.end (); |
| 170 | stats++) |
| 171 | { |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 172 | if (!stats->first) |
| 173 | continue; |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 175 | PRINTER ("InInterests", m_inInterests); |
| 176 | PRINTER ("OutInterests", m_outInterests); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 177 | PRINTER ("DropInterests", m_dropInterests); |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 179 | PRINTER ("InNacks", m_inNacks); |
| 180 | PRINTER ("OutNacks", m_outNacks); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 181 | PRINTER ("DropNacks", m_dropNacks); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 183 | PRINTER ("InData", m_inData); |
| 184 | PRINTER ("OutData", m_outData); |
Alexander Afanasyev | f1a4f1e | 2013-03-18 08:37:49 -0600 | [diff] [blame] | 185 | PRINTER ("DropData", m_dropData); |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 186 | } |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 187 | |
| 188 | { |
| 189 | std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.find (Ptr<const Face> (0)); |
Alexander Afanasyev | 6692e0b | 2013-05-06 10:38:50 -0700 | [diff] [blame] | 190 | if (stats != m_stats.end ()) |
| 191 | { |
| 192 | PRINTER ("SatisfiedInterests", m_satisfiedInterests); |
| 193 | PRINTER ("TimedOutInterests", m_timedOutInterests); |
| 194 | } |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 195 | } |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
| 199 | L3AggregateTracer::OutInterests (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 200 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 201 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 202 | m_stats[face].get<0> ().m_outInterests ++; |
| 203 | m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void |
| 207 | L3AggregateTracer::InInterests (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 208 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 209 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 210 | m_stats[face].get<0> ().m_inInterests ++; |
| 211 | m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | L3AggregateTracer::DropInterests (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 216 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 217 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 218 | m_stats[face].get<0> ().m_dropInterests ++; |
| 219 | m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
| 223 | L3AggregateTracer::OutNacks (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 224 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 226 | m_stats[face].get<0> ().m_outNacks ++; |
| 227 | m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
| 231 | L3AggregateTracer::InNacks (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 232 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 233 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 234 | m_stats[face].get<0> ().m_inNacks ++; |
| 235 | m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | L3AggregateTracer::DropNacks (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 240 | Ptr<const Interest> header, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 241 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 242 | m_stats[face].get<0> ().m_dropNacks ++; |
| 243 | m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | L3AggregateTracer::OutData (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 248 | Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 249 | bool fromCache, Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 250 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 251 | m_stats[face].get<0> ().m_outData ++; |
| 252 | m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
| 256 | L3AggregateTracer::InData (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 257 | Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 258 | Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 259 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 260 | m_stats[face].get<0> ().m_inData ++; |
| 261 | m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
| 265 | L3AggregateTracer::DropData (std::string context, |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 266 | Ptr<const ContentObject> header, Ptr<const Packet> payload, |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 267 | Ptr<const Face> face) |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 268 | { |
Alexander Afanasyev | 5931480 | 2012-11-26 14:56:04 -0800 | [diff] [blame] | 269 | m_stats[face].get<0> ().m_dropData ++; |
| 270 | m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize (); |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Alexander Afanasyev | 37b84c5 | 2013-04-26 13:38:52 -0700 | [diff] [blame] | 273 | void |
| 274 | L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>) |
| 275 | { |
| 276 | m_stats[0].get<0> ().m_satisfiedInterests ++; |
| 277 | // no "size" stats |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>) |
| 282 | { |
| 283 | m_stats[0].get<0> ().m_timedOutInterests ++; |
| 284 | // no "size" stats |
| 285 | } |
| 286 | |
Alexander Afanasyev | c9d5c1a | 2012-11-21 18:00:26 -0800 | [diff] [blame] | 287 | } // namespace ndn |
| 288 | } // namespace ns3 |