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