blob: e977285d97be502570ab5b0f3469049a5446404a [file] [log] [blame]
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -08001/* -*- 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"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070030#include "ns3/ndn-data.h"
Alexander Afanasyev37b84c52013-04-26 13:38:52 -070031#include "ns3/ndn-pit-entry.h"
32
Alexander Afanasyev59314802012-11-26 14:56:04 -080033#include "ns3/simulator.h"
34#include "ns3/node-list.h"
35#include "ns3/log.h"
36
37#include <fstream>
38
39NS_LOG_COMPONENT_DEFINE ("ndn.L3AggregateTracer");
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080040
41namespace ns3 {
42namespace ndn {
43
Alexander Afanasyev5352af32013-07-15 09:51:28 -070044template<class T>
45static inline void
46NullDeleter (T *ptr)
47{
48}
49
Alexander Afanasyev59314802012-11-26 14:56:04 -080050boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
51L3AggregateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
52{
53 using namespace boost;
54 using namespace std;
Alexander Afanasyev37b84c52013-04-26 13:38:52 -070055
Alexander Afanasyev59314802012-11-26 14:56:04 -080056 std::list<Ptr<L3AggregateTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070057 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 Afanasyev59314802012-11-26 14:56:04 -080062
Alexander Afanasyev5352af32013-07-15 09:51:28 -070063 if (!os->is_open ())
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070064 return boost::make_tuple (outputStream, tracers);
Alexander Afanasyev5352af32013-07-15 09:51:28 -070065
66 outputStream = os;
67 }
68 else
69 {
70 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
71 }
Alexander Afanasyev59314802012-11-26 14:56:04 -080072
73 for (NodeList::Iterator node = NodeList::Begin ();
74 node != NodeList::End ();
75 node++)
76 {
Alexander Afanasyeva68783a2013-06-27 13:39:04 -070077 Ptr<L3AggregateTracer> trace = Install (*node, outputStream, averagingPeriod);
Alexander Afanasyev59314802012-11-26 14:56:04 -080078 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 Afanasyeva68783a2013-06-27 13:39:04 -070091boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
92L3AggregateTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
93{
94 using namespace boost;
95 using namespace std;
Alexander Afanasyev59314802012-11-26 14:56:04 -080096
Alexander Afanasyeva68783a2013-06-27 13:39:04 -070097 std::list<Ptr<L3AggregateTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -070098 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);
Alexander Afanasyeva68783a2013-06-27 13:39:04 -0700103
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700104 if (!os->is_open ())
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700105 return boost::make_tuple (outputStream, tracers);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700106
107 outputStream = os;
108 }
109 else
110 {
111 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
112 }
Alexander Afanasyeva68783a2013-06-27 13:39:04 -0700113
114 for (NodeContainer::Iterator node = nodes.Begin ();
115 node != nodes.End ();
116 node++)
117 {
118 Ptr<L3AggregateTracer> 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
132boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
133L3AggregateTracer::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<L3AggregateTracer> > tracers;
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700139 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);
Alexander Afanasyeva68783a2013-06-27 13:39:04 -0700144
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700145 if (!os->is_open ())
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700146 return boost::make_tuple (outputStream, tracers);
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700147
148 outputStream = os;
149 }
150 else
151 {
152 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
153 }
Alexander Afanasyeva68783a2013-06-27 13:39:04 -0700154
155 Ptr<L3AggregateTracer> 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
169Ptr<L3AggregateTracer>
170L3AggregateTracer::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<L3AggregateTracer> trace = Create<L3AggregateTracer> (outputStream, node);
177 trace->SetAveragingPeriod (averagingPeriod);
178
179 return trace;
180}
Alexander Afanasyev59314802012-11-26 14:56:04 -0800181
182L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
183 : L3Tracer (node)
184 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800185{
186 Reset ();
187}
188
Alexander Afanasyev59314802012-11-26 14:56:04 -0800189L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
190 : L3Tracer (node)
191 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800192{
193 Reset ();
194}
195
196L3AggregateTracer::~L3AggregateTracer ()
197{
198};
199
200void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800201L3AggregateTracer::SetAveragingPeriod (const Time &period)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800202{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800203 m_period = period;
204 m_printEvent.Cancel ();
205 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800206}
207
Alexander Afanasyev59314802012-11-26 14:56:04 -0800208void
209L3AggregateTracer::PeriodicPrinter ()
210{
211 Print (*m_os);
212 Reset ();
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700213
Alexander Afanasyev59314802012-11-26 14:56:04 -0800214 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
215}
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800216
217void
218L3AggregateTracer::PrintHeader (std::ostream &os) const
219{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800220 os << "Time" << "\t"
221
222 << "Node" << "\t"
223 << "FaceId" << "\t"
224 << "FaceDescr" << "\t"
225
226 << "Type" << "\t"
227 << "Packets" << "\t"
228 << "Kilobytes";
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800229}
230
231void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800232L3AggregateTracer::Reset ()
233{
234 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
235 stats != m_stats.end ();
236 stats++)
237 {
238 stats->second.get<0> ().Reset ();
239 stats->second.get<1> ().Reset ();
240 }
241}
242
243
244#define STATS(INDEX) stats->second.get<INDEX> ()
245
246#define PRINTER(printName, fieldName) \
247 os << time.ToDouble (Time::S) << "\t" \
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700248 << m_node << "\t"; \
249 if (stats->first) \
250 { \
251 os \
252 << stats->first->GetId () << "\t" \
253 << *stats->first << "\t"; \
254 } \
255 else \
256 { \
257 os << "-1\tall\t"; \
258 } \
259 os \
Alexander Afanasyev59314802012-11-26 14:56:04 -0800260 << printName << "\t" \
261 << STATS(0).fieldName << "\t" \
262 << STATS(1).fieldName / 1024.0 << "\n";
263
264
265void
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800266L3AggregateTracer::Print (std::ostream &os) const
267{
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700268 Time time = Simulator::Now ();
269
Alexander Afanasyev59314802012-11-26 14:56:04 -0800270 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
271 stats != m_stats.end ();
272 stats++)
273 {
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700274 if (!stats->first)
275 continue;
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800276
Alexander Afanasyev59314802012-11-26 14:56:04 -0800277 PRINTER ("InInterests", m_inInterests);
278 PRINTER ("OutInterests", m_outInterests);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600279 PRINTER ("DropInterests", m_dropInterests);
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700280
Alexander Afanasyev59314802012-11-26 14:56:04 -0800281 PRINTER ("InNacks", m_inNacks);
282 PRINTER ("OutNacks", m_outNacks);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600283 PRINTER ("DropNacks", m_dropNacks);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800284
Alexander Afanasyev59314802012-11-26 14:56:04 -0800285 PRINTER ("InData", m_inData);
286 PRINTER ("OutData", m_outData);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600287 PRINTER ("DropData", m_dropData);
Alexander Afanasyev59314802012-11-26 14:56:04 -0800288 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700289
290 {
291 std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.find (Ptr<const Face> (0));
Alexander Afanasyev6692e0b2013-05-06 10:38:50 -0700292 if (stats != m_stats.end ())
293 {
294 PRINTER ("SatisfiedInterests", m_satisfiedInterests);
295 PRINTER ("TimedOutInterests", m_timedOutInterests);
296 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700297 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800298}
299
300void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700301L3AggregateTracer::OutInterests (Ptr<const Interest> interest, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800302{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800303 m_stats[face].get<0> ().m_outInterests ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700304 if (interest->GetWire ())
305 {
306 m_stats[face].get<1> ().m_outInterests += interest->GetWire ()->GetSize ();
307 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800308}
309
310void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700311L3AggregateTracer::InInterests (Ptr<const Interest> interest, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800312{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800313 m_stats[face].get<0> ().m_inInterests ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700314 if (interest->GetWire ())
315 {
316 m_stats[face].get<1> ().m_inInterests += interest->GetWire ()->GetSize ();
317 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800318}
319
320void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700321L3AggregateTracer::DropInterests (Ptr<const Interest> interest, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800322{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800323 m_stats[face].get<0> ().m_dropInterests ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700324 if (interest->GetWire ())
325 {
326 m_stats[face].get<1> ().m_dropInterests += interest->GetWire ()->GetSize ();
327 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800328}
329
330void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700331L3AggregateTracer::OutNacks (Ptr<const Interest> nack, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800332{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800333 m_stats[face].get<0> ().m_outNacks ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700334 if (nack->GetWire ())
335 {
336 m_stats[face].get<1> ().m_outNacks += nack->GetWire ()->GetSize ();
337 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800338}
339
340void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700341L3AggregateTracer::InNacks (Ptr<const Interest> nack, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800342{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800343 m_stats[face].get<0> ().m_inNacks ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700344 if (nack->GetWire ())
345 {
346 m_stats[face].get<1> ().m_inNacks += nack->GetWire ()->GetSize ();
347 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800348}
349
350void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700351L3AggregateTracer::DropNacks (Ptr<const Interest> nack, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800352{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800353 m_stats[face].get<0> ().m_dropNacks ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700354 if (nack->GetWire ())
355 {
356 m_stats[face].get<1> ().m_dropNacks += nack->GetWire ()->GetSize ();
357 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800358}
359
360void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700361L3AggregateTracer::OutData (Ptr<const Data> data,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800362 bool fromCache, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800363{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800364 m_stats[face].get<0> ().m_outData ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700365 if (data->GetWire ())
366 {
367 m_stats[face].get<1> ().m_outData += data->GetWire ()->GetSize ();
368 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800369}
370
371void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700372L3AggregateTracer::InData (Ptr<const Data> data,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800373 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800374{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800375 m_stats[face].get<0> ().m_inData ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700376 if (data->GetWire ())
377 {
378 m_stats[face].get<1> ().m_inData += data->GetWire ()->GetSize ();
379 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800380}
381
382void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700383L3AggregateTracer::DropData (Ptr<const Data> data,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800384 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800385{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800386 m_stats[face].get<0> ().m_dropData ++;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700387 if (data->GetWire ())
388 {
389 m_stats[face].get<1> ().m_dropData += data->GetWire ()->GetSize ();
390 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800391}
392
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700393void
394L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>)
395{
396 m_stats[0].get<0> ().m_satisfiedInterests ++;
397 // no "size" stats
398}
399
400void
401L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>)
402{
403 m_stats[0].get<0> ().m_timedOutInterests ++;
404 // no "size" stats
405}
406
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800407} // namespace ndn
408} // namespace ns3