blob: b64fc6bfd034b9292edfc4e6d9a655f841745301 [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 {
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
80L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
81 : L3Tracer (node)
82 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080083{
84 Reset ();
85}
86
Alexander Afanasyev59314802012-11-26 14:56:04 -080087L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
88 : L3Tracer (node)
89 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080090{
91 Reset ();
92}
93
94L3AggregateTracer::~L3AggregateTracer ()
95{
96};
97
98void
Alexander Afanasyev59314802012-11-26 14:56:04 -080099L3AggregateTracer::SetAveragingPeriod (const Time &period)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800100{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800101 m_period = period;
102 m_printEvent.Cancel ();
103 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800104}
105
Alexander Afanasyev59314802012-11-26 14:56:04 -0800106void
107L3AggregateTracer::PeriodicPrinter ()
108{
109 Print (*m_os);
110 Reset ();
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700111
Alexander Afanasyev59314802012-11-26 14:56:04 -0800112 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
113}
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800114
115void
116L3AggregateTracer::PrintHeader (std::ostream &os) const
117{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800118 os << "Time" << "\t"
119
120 << "Node" << "\t"
121 << "FaceId" << "\t"
122 << "FaceDescr" << "\t"
123
124 << "Type" << "\t"
125 << "Packets" << "\t"
126 << "Kilobytes";
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800127}
128
129void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800130L3AggregateTracer::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 Afanasyev37b84c52013-04-26 13:38:52 -0700146 << 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 Afanasyev59314802012-11-26 14:56:04 -0800158 << printName << "\t" \
159 << STATS(0).fieldName << "\t" \
160 << STATS(1).fieldName / 1024.0 << "\n";
161
162
163void
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800164L3AggregateTracer::Print (std::ostream &os) const
165{
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700166 Time time = Simulator::Now ();
167
Alexander Afanasyev59314802012-11-26 14:56:04 -0800168 for (std::map<Ptr<const Face>, boost::tuple<Stats, Stats> >::iterator stats = m_stats.begin ();
169 stats != m_stats.end ();
170 stats++)
171 {
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700172 if (!stats->first)
173 continue;
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800174
Alexander Afanasyev59314802012-11-26 14:56:04 -0800175 PRINTER ("InInterests", m_inInterests);
176 PRINTER ("OutInterests", m_outInterests);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600177 PRINTER ("DropInterests", m_dropInterests);
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700178
Alexander Afanasyev59314802012-11-26 14:56:04 -0800179 PRINTER ("InNacks", m_inNacks);
180 PRINTER ("OutNacks", m_outNacks);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600181 PRINTER ("DropNacks", m_dropNacks);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800182
Alexander Afanasyev59314802012-11-26 14:56:04 -0800183 PRINTER ("InData", m_inData);
184 PRINTER ("OutData", m_outData);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600185 PRINTER ("DropData", m_dropData);
Alexander Afanasyev59314802012-11-26 14:56:04 -0800186 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700187
188 {
189 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 -0700190 if (stats != m_stats.end ())
191 {
192 PRINTER ("SatisfiedInterests", m_satisfiedInterests);
193 PRINTER ("TimedOutInterests", m_timedOutInterests);
194 }
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700195 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800196}
197
198void
199L3AggregateTracer::OutInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700200 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800201{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800202 m_stats[face].get<0> ().m_outInterests ++;
203 m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800204}
205
206void
207L3AggregateTracer::InInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700208 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800209{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800210 m_stats[face].get<0> ().m_inInterests ++;
211 m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800212}
213
214void
215L3AggregateTracer::DropInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700216 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800217{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800218 m_stats[face].get<0> ().m_dropInterests ++;
219 m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800220}
221
222void
223L3AggregateTracer::OutNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700224 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800225{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800226 m_stats[face].get<0> ().m_outNacks ++;
227 m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800228}
229
230void
231L3AggregateTracer::InNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700232 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800233{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800234 m_stats[face].get<0> ().m_inNacks ++;
235 m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800236}
237
238void
239L3AggregateTracer::DropNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700240 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800241{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800242 m_stats[face].get<0> ().m_dropNacks ++;
243 m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800244}
245
246void
247L3AggregateTracer::OutData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700248 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800249 bool fromCache, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800250{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800251 m_stats[face].get<0> ().m_outData ++;
252 m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800253}
254
255void
256L3AggregateTracer::InData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700257 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800258 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800259{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800260 m_stats[face].get<0> ().m_inData ++;
261 m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800262}
263
264void
265L3AggregateTracer::DropData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700266 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800267 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800268{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800269 m_stats[face].get<0> ().m_dropData ++;
270 m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800271}
272
Alexander Afanasyev37b84c52013-04-26 13:38:52 -0700273void
274L3AggregateTracer::SatisfiedInterests (Ptr<const pit::Entry>)
275{
276 m_stats[0].get<0> ().m_satisfiedInterests ++;
277 // no "size" stats
278}
279
280void
281L3AggregateTracer::TimedOutInterests (Ptr<const pit::Entry>)
282{
283 m_stats[0].get<0> ().m_timedOutInterests ++;
284 // no "size" stats
285}
286
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800287} // namespace ndn
288} // namespace ns3