blob: 48d2589ae31116e89274a6dd6c0bdd7e88e2d143 [file] [log] [blame]
Alexander Afanasyevc7597622013-02-28 10:58:52 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
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 "l2-rate-tracer.h"
22#include "ns3/node.h"
23#include "ns3/packet.h"
24#include "ns3/config.h"
25#include "ns3/callback.h"
26#include "ns3/simulator.h"
27#include "ns3/node-list.h"
28#include "ns3/node.h"
29#include "ns3/log.h"
30
31#include <boost/lexical_cast.hpp>
32#include <fstream>
33
34using namespace boost;
35using namespace std;
36
37NS_LOG_COMPONENT_DEFINE ("L2RateTracer");
38
39namespace ns3 {
40
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070041static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > > > g_tracers;
42
43template<class T>
44static inline void
45NullDeleter (T *ptr)
46{
47}
48
49void
Alexander Afanasyevc7597622013-02-28 10:58:52 -080050L2RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
51{
52 std::list<Ptr<L2RateTracer> > tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070053 boost::shared_ptr<std::ostream> outputStream;
54 if (file != "-")
55 {
56 boost::shared_ptr<std::ofstream> os (new std::ofstream ());
57 os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevc7597622013-02-28 10:58:52 -080058
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070059 if (!os->is_open ())
60 {
61 NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
62 return;
63 }
64
65 outputStream = os;
66 }
67 else
68 {
69 outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
70 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080071
72 for (NodeList::Iterator node = NodeList::Begin ();
73 node != NodeList::End ();
74 node++)
75 {
76 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
77
78 Ptr<L2RateTracer> trace = Create<L2RateTracer> (outputStream, *node);
79 trace->SetAveragingPeriod (averagingPeriod);
80 tracers.push_back (trace);
81 }
82
83 if (tracers.size () > 0)
84 {
85 // *m_l3RateTrace << "# "; // not necessary for R's read.table
86 tracers.front ()->PrintHeader (*outputStream);
87 *outputStream << "\n";
88 }
89
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070090 g_tracers.push_back (boost::make_tuple (outputStream, tracers));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080091}
92
93
94L2RateTracer::L2RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
95 : L2Tracer (node)
96 , m_os (os)
97{
98 SetAveragingPeriod (Seconds (1.0));
99}
100
101L2RateTracer::~L2RateTracer ()
102{
103 m_printEvent.Cancel ();
104}
105
106void
107L2RateTracer::SetAveragingPeriod (const Time &period)
108{
109 m_period = period;
110 m_printEvent.Cancel ();
111 m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
112}
113
114void
115L2RateTracer::PeriodicPrinter ()
116{
117 Print (*m_os);
118 Reset ();
119
120 m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
121}
122
123void
124L2RateTracer::PrintHeader (std::ostream &os) const
125{
126 os << "Time" << "\t"
127
128 << "Node" << "\t"
129 << "Interface" << "\t"
130
131 << "Type" << "\t"
132 << "Packets" << "\t"
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600133 << "Kilobytes" << "\t"
134 << "PacketsRaw" << "\t"
135 << "KilobytesRaw";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800136}
137
138void
139L2RateTracer::Reset ()
140{
141 m_stats.get<0> ().Reset ();
142 m_stats.get<1> ().Reset ();
143}
144
145const double alpha = 0.8;
146
147#define STATS(INDEX) m_stats.get<INDEX> ()
148#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
149
150#define PRINTER(printName, fieldName, interface) \
151STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
152STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
153 \
154 os << time.ToDouble (Time::S) << "\t" \
155 << m_node << "\t" \
156 << interface << "\t" \
157 << printName << "\t" \
158 << STATS(2).fieldName << "\t" \
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600159 << STATS(3).fieldName << "\t" \
160 << STATS(0).fieldName << "\t" \
161 << STATS(1).fieldName / 1024.0 << "\n";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800162
163void
164L2RateTracer::Print (std::ostream &os) const
165{
166 Time time = Simulator::Now ();
167
168 PRINTER ("Drop", m_drop, "combined");
169}
170
171void
172L2RateTracer::Drop (Ptr<const Packet> packet)
173{
174 // no interface information... this should be part of this L2Tracer object data
175
176 m_stats.get<0> ().m_drop ++;
177 m_stats.get<1> ().m_drop += packet->GetSize ();
178}
179
180} // namespace ns3