blob: 5ece34cbde39874e666c668298d9a71fd94e6a49 [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"
30#include "ns3/ndn-content-object.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 ())
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 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 ())
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 }
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 ())
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 }
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 Afanasyev5352af32013-07-15 09:51:28 -0700301L3AggregateTracer::OutInterests (Ptr<const Interest> header, 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 ++;
304 m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800305}
306
307void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700308L3AggregateTracer::InInterests (Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800309{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800310 m_stats[face].get<0> ().m_inInterests ++;
311 m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800312}
313
314void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700315L3AggregateTracer::DropInterests (Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800316{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800317 m_stats[face].get<0> ().m_dropInterests ++;
318 m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800319}
320
321void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700322L3AggregateTracer::OutNacks (Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800323{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800324 m_stats[face].get<0> ().m_outNacks ++;
325 m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800326}
327
328void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700329L3AggregateTracer::InNacks (Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800330{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800331 m_stats[face].get<0> ().m_inNacks ++;
332 m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800333}
334
335void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700336L3AggregateTracer::DropNacks (Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800337{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800338 m_stats[face].get<0> ().m_dropNacks ++;
339 m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800340}
341
342void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700343L3AggregateTracer::OutData (Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800344 bool fromCache, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800345{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800346 m_stats[face].get<0> ().m_outData ++;
347 m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800348}
349
350void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700351L3AggregateTracer::InData (Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800352 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800353{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800354 m_stats[face].get<0> ().m_inData ++;
355 m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800356}
357
358void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700359L3AggregateTracer::DropData (Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800360 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800361{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800362 m_stats[face].get<0> ().m_dropData ++;
363 m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800364}
365
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700366void
367L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>)
368{
369 m_stats[0].get<0> ().m_satisfiedInterests ++;
370 // no "size" stats
371}
372
373void
374L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>)
375{
376 m_stats[0].get<0> ().m_timedOutInterests ++;
377 // no "size" stats
378}
379
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800380} // namespace ndn
381} // namespace ns3