blob: 1cc79a55fafb73d00a2fd43f3560edc68a3cb142 [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 Afanasyev59314802012-11-26 14:56:04 -080031#include "ns3/simulator.h"
32#include "ns3/node-list.h"
33#include "ns3/log.h"
34
35#include <fstream>
36
37NS_LOG_COMPONENT_DEFINE ("ndn.L3AggregateTracer");
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080038
39namespace ns3 {
40namespace ndn {
41
Alexander Afanasyev59314802012-11-26 14:56:04 -080042boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L3AggregateTracer> > >
43L3AggregateTracer::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
78L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
79 : L3Tracer (node)
80 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080081{
82 Reset ();
83}
84
Alexander Afanasyev59314802012-11-26 14:56:04 -080085L3AggregateTracer::L3AggregateTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
86 : L3Tracer (node)
87 , m_os (os)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080088{
89 Reset ();
90}
91
92L3AggregateTracer::~L3AggregateTracer ()
93{
94};
95
96void
Alexander Afanasyev59314802012-11-26 14:56:04 -080097L3AggregateTracer::SetAveragingPeriod (const Time &period)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -080098{
Alexander Afanasyev59314802012-11-26 14:56:04 -080099 m_period = period;
100 m_printEvent.Cancel ();
101 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800102}
103
Alexander Afanasyev59314802012-11-26 14:56:04 -0800104void
105L3AggregateTracer::PeriodicPrinter ()
106{
107 Print (*m_os);
108 Reset ();
109
110 m_printEvent = Simulator::Schedule (m_period, &L3AggregateTracer::PeriodicPrinter, this);
111}
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800112
113void
114L3AggregateTracer::PrintHeader (std::ostream &os) const
115{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800116 os << "Time" << "\t"
117
118 << "Node" << "\t"
119 << "FaceId" << "\t"
120 << "FaceDescr" << "\t"
121
122 << "Type" << "\t"
123 << "Packets" << "\t"
124 << "Kilobytes";
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800125}
126
127void
Alexander Afanasyev59314802012-11-26 14:56:04 -0800128L3AggregateTracer::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
152void
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800153L3AggregateTracer::Print (std::ostream &os) const
154{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800155 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 Afanasyevc9d5c1a2012-11-21 18:00:26 -0800160
Alexander Afanasyev59314802012-11-26 14:56:04 -0800161 PRINTER ("InInterests", m_inInterests);
162 PRINTER ("OutInterests", m_outInterests);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600163 PRINTER ("DropInterests", m_dropInterests);
Alexander Afanasyev59314802012-11-26 14:56:04 -0800164
165 PRINTER ("InNacks", m_inNacks);
166 PRINTER ("OutNacks", m_outNacks);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600167 PRINTER ("DropNacks", m_dropNacks);
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800168
Alexander Afanasyev59314802012-11-26 14:56:04 -0800169 PRINTER ("InData", m_inData);
170 PRINTER ("OutData", m_outData);
Alexander Afanasyevf1a4f1e2013-03-18 08:37:49 -0600171 PRINTER ("DropData", m_dropData);
Alexander Afanasyev59314802012-11-26 14:56:04 -0800172 }
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800173}
174
175void
176L3AggregateTracer::OutInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700177 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800178{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800179 m_stats[face].get<0> ().m_outInterests ++;
180 m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800181}
182
183void
184L3AggregateTracer::InInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700185 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800186{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800187 m_stats[face].get<0> ().m_inInterests ++;
188 m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800189}
190
191void
192L3AggregateTracer::DropInterests (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700193 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800194{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800195 m_stats[face].get<0> ().m_dropInterests ++;
196 m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800197}
198
199void
200L3AggregateTracer::OutNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700201 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800202{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800203 m_stats[face].get<0> ().m_outNacks ++;
204 m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800205}
206
207void
208L3AggregateTracer::InNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700209 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800210{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800211 m_stats[face].get<0> ().m_inNacks ++;
212 m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800213}
214
215void
216L3AggregateTracer::DropNacks (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700217 Ptr<const Interest> header, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800218{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800219 m_stats[face].get<0> ().m_dropNacks ++;
220 m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800221}
222
223void
224L3AggregateTracer::OutData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700225 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800226 bool fromCache, Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800227{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800228 m_stats[face].get<0> ().m_outData ++;
229 m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800230}
231
232void
233L3AggregateTracer::InData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700234 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800235 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800236{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800237 m_stats[face].get<0> ().m_inData ++;
238 m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800239}
240
241void
242L3AggregateTracer::DropData (std::string context,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700243 Ptr<const ContentObject> header, Ptr<const Packet> payload,
Alexander Afanasyev59314802012-11-26 14:56:04 -0800244 Ptr<const Face> face)
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800245{
Alexander Afanasyev59314802012-11-26 14:56:04 -0800246 m_stats[face].get<0> ().m_dropData ++;
247 m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize ();
Alexander Afanasyevc9d5c1a2012-11-21 18:00:26 -0800248}
249
250} // namespace ndn
251} // namespace ns3