blob: 646c516cfa8b8d92522a7e143ad7e69fa5fd6589 [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 Afanasyev59314802012-11-26 14:56:04 -080044boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
45L3AggregateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
46{
47 using namespace boost;
48 using namespace std;
Alexander Afanasyev37b84c52013-04-26 13:38:52 -070049
Alexander Afanasyev59314802012-11-26 14:56:04 -080050 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 {
Alexander Afanasyeva68783a2013-06-27 13:39:04 -070061 Ptr<L3AggregateTracer> trace = Install (*node, outputStream, averagingPeriod);
Alexander Afanasyev59314802012-11-26 14:56:04 -080062 tracers.push_back (trace);
63 }
64
65 if (tracers.size () > 0)
66 {
67 // *m_l3RateTrace << "# "; // not necessary for R's read.table
68 tracers.front ()->PrintHeader (*outputStream);
69 *outputStream << "\n";
70 }
71
72 return boost::make_tuple (outputStream, tracers);
73}
74
Alexander Afanasyeva68783a2013-06-27 13:39:04 -070075boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
76L3AggregateTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
77{
78 using namespace boost;
79 using namespace std;
Alexander Afanasyev59314802012-11-26 14:56:04 -080080
Alexander Afanasyeva68783a2013-06-27 13:39:04 -070081 std::list<Ptr<L3AggregateTracer> > tracers;
82 boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
83 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
84
85 if (!outputStream->is_open ())
86 return boost::make_tuple (outputStream, tracers);
87
88 for (NodeContainer::Iterator node = nodes.Begin ();
89 node != nodes.End ();
90 node++)
91 {
92 Ptr<L3AggregateTracer> trace = Install (*node, outputStream, averagingPeriod);
93 tracers.push_back (trace);
94 }
95
96 if (tracers.size () > 0)
97 {
98 // *m_l3RateTrace << "# "; // not necessary for R's read.table
99 tracers.front ()->PrintHeader (*outputStream);
100 *outputStream << "\n";
101 }
102
103 return boost::make_tuple (outputStream, tracers);
104}
105
106boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
107L3AggregateTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
108{
109 using namespace boost;
110 using namespace std;
111
112 std::list<Ptr<L3AggregateTracer> > tracers;
113 boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
114 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
115
116 if (!outputStream->is_open ())
117 return boost::make_tuple (outputStream, tracers);
118
119 Ptr<L3AggregateTracer> trace = Install (node, outputStream, averagingPeriod);
120 tracers.push_back (trace);
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
133Ptr<L3AggregateTracer>
134L3AggregateTracer::Install (Ptr<Node> node,
135 boost::shared_ptr<std::ostream> outputStream,
136 Time averagingPeriod/* = Seconds (0.5)*/)
137{
138 NS_LOG_DEBUG ("Node: " << node->GetId ());
139
140 Ptr<L3AggregateTracer> trace = Create<L3AggregateTracer> (outputStream, node);
141 trace->SetAveragingPeriod (averagingPeriod);
142
143 return trace;
144}
Alexander Afanasyev59314802012-11-26 14:56:04 -0800145
146L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
147 : L3Tracer (node)
148 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800149{
150 Reset ();
151}
152
Alexander Afanasyev59314802012-11-26 14:56:04 -0800153L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
154 : L3Tracer (node)
155 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800156{
157 Reset ();
158}
159
160L3AggregateTracer::~L3AggregateTracer ()
161{
162};
163
164void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800165L3AggregateTracer::SetAveragingPeriod (const Time &period)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800166{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800167 m_period = period;
168 m_printEvent.Cancel ();
169 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800170}
171
Alexander Afanasyev59314802012-11-26 14:56:04 -0800172void
173L3AggregateTracer::PeriodicPrinter ()
174{
175 Print (*m_os);
176 Reset ();
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700177
Alexander Afanasyev59314802012-11-26 14:56:04 -0800178 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
179}
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800180
181void
182L3AggregateTracer::PrintHeader (std::ostream &os) const
183{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800184 os << "Time" << "\t"
185
186 << "Node" << "\t"
187 << "FaceId" << "\t"
188 << "FaceDescr" << "\t"
189
190 << "Type" << "\t"
191 << "Packets" << "\t"
192 << "Kilobytes";
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800193}
194
195void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800196L3AggregateTracer::Reset ()
197{
198 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
199 stats != m_stats.end ();
200 stats++)
201 {
202 stats->second.get<0> ().Reset ();
203 stats->second.get<1> ().Reset ();
204 }
205}
206
207
208#define STATS(INDEX) stats->second.get<INDEX> ()
209
210#define PRINTER(printName, fieldName) \
211 os << time.ToDouble (Time::S) << "\t" \
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700212 << m_node << "\t"; \
213 if (stats->first) \
214 { \
215 os \
216 << stats->first->GetId () << "\t" \
217 << *stats->first << "\t"; \
218 } \
219 else \
220 { \
221 os << "-1\tall\t"; \
222 } \
223 os \
Alexander Afanasyev59314802012-11-26 14:56:04 -0800224 << printName << "\t" \
225 << STATS(0).fieldName << "\t" \
226 << STATS(1).fieldName / 1024.0 << "\n";
227
228
229void
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800230L3AggregateTracer::Print (std::ostream &os) const
231{
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700232 Time time = Simulator::Now ();
233
Alexander Afanasyev59314802012-11-26 14:56:04 -0800234 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
235 stats != m_stats.end ();
236 stats++)
237 {
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700238 if (!stats->first)
239 continue;
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800240
Alexander Afanasyev59314802012-11-26 14:56:04 -0800241 PRINTER ("InInterests", m_inInterests);
242 PRINTER ("OutInterests", m_outInterests);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600243 PRINTER ("DropInterests", m_dropInterests);
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700244
Alexander Afanasyev59314802012-11-26 14:56:04 -0800245 PRINTER ("InNacks", m_inNacks);
246 PRINTER ("OutNacks", m_outNacks);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600247 PRINTER ("DropNacks", m_dropNacks);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800248
Alexander Afanasyev59314802012-11-26 14:56:04 -0800249 PRINTER ("InData", m_inData);
250 PRINTER ("OutData", m_outData);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600251 PRINTER ("DropData", m_dropData);
Alexander Afanasyev59314802012-11-26 14:56:04 -0800252 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700253
254 {
255 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 -0700256 if (stats != m_stats.end ())
257 {
258 PRINTER ("SatisfiedInterests", m_satisfiedInterests);
259 PRINTER ("TimedOutInterests", m_timedOutInterests);
260 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700261 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800262}
263
264void
265L3AggregateTracer::OutInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700266 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800267{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800268 m_stats[face].get<0> ().m_outInterests ++;
269 m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800270}
271
272void
273L3AggregateTracer::InInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700274 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800275{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800276 m_stats[face].get<0> ().m_inInterests ++;
277 m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800278}
279
280void
281L3AggregateTracer::DropInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700282 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800283{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800284 m_stats[face].get<0> ().m_dropInterests ++;
285 m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800286}
287
288void
289L3AggregateTracer::OutNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700290 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800291{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800292 m_stats[face].get<0> ().m_outNacks ++;
293 m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800294}
295
296void
297L3AggregateTracer::InNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700298 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800299{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800300 m_stats[face].get<0> ().m_inNacks ++;
301 m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800302}
303
304void
305L3AggregateTracer::DropNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700306 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800307{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800308 m_stats[face].get<0> ().m_dropNacks ++;
309 m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800310}
311
312void
313L3AggregateTracer::OutData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700314 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800315 bool fromCache, 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_outData ++;
318 m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800319}
320
321void
322L3AggregateTracer::InData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700323 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800324 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800325{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800326 m_stats[face].get<0> ().m_inData ++;
327 m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800328}
329
330void
331L3AggregateTracer::DropData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700332 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800333 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800334{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800335 m_stats[face].get<0> ().m_dropData ++;
336 m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800337}
338
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700339void
340L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>)
341{
342 m_stats[0].get<0> ().m_satisfiedInterests ++;
343 // no "size" stats
344}
345
346void
347L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>)
348{
349 m_stats[0].get<0> ().m_timedOutInterests ++;
350 // no "size" stats
351}
352
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800353} // namespace ndn
354} // namespace ns3