blob: cf4af72c72c6c2b75d6c70ac2c53040f4649c01c [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
41boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > >
42L2RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
43{
44 std::list<Ptr<L2RateTracer> > tracers;
45 boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
46 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
47
48 if (!outputStream->is_open ())
49 return boost::make_tuple (outputStream, tracers);
50
51 for (NodeList::Iterator node = NodeList::Begin ();
52 node != NodeList::End ();
53 node++)
54 {
55 NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
56
57 Ptr<L2RateTracer> trace = Create<L2RateTracer> (outputStream, *node);
58 trace->SetAveragingPeriod (averagingPeriod);
59 tracers.push_back (trace);
60 }
61
62 if (tracers.size () > 0)
63 {
64 // *m_l3RateTrace << "# "; // not necessary for R's read.table
65 tracers.front ()->PrintHeader (*outputStream);
66 *outputStream << "\n";
67 }
68
69 return boost::make_tuple (outputStream, tracers);
70}
71
72
73L2RateTracer::L2RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
74 : L2Tracer (node)
75 , m_os (os)
76{
77 SetAveragingPeriod (Seconds (1.0));
78}
79
80L2RateTracer::~L2RateTracer ()
81{
82 m_printEvent.Cancel ();
83}
84
85void
86L2RateTracer::SetAveragingPeriod (const Time &period)
87{
88 m_period = period;
89 m_printEvent.Cancel ();
90 m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
91}
92
93void
94L2RateTracer::PeriodicPrinter ()
95{
96 Print (*m_os);
97 Reset ();
98
99 m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
100}
101
102void
103L2RateTracer::PrintHeader (std::ostream &os) const
104{
105 os << "Time" << "\t"
106
107 << "Node" << "\t"
108 << "Interface" << "\t"
109
110 << "Type" << "\t"
111 << "Packets" << "\t"
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600112 << "Kilobytes" << "\t"
113 << "PacketsRaw" << "\t"
114 << "KilobytesRaw";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800115}
116
117void
118L2RateTracer::Reset ()
119{
120 m_stats.get<0> ().Reset ();
121 m_stats.get<1> ().Reset ();
122}
123
124const double alpha = 0.8;
125
126#define STATS(INDEX) m_stats.get<INDEX> ()
127#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
128
129#define PRINTER(printName, fieldName, interface) \
130STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
131STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
132 \
133 os << time.ToDouble (Time::S) << "\t" \
134 << m_node << "\t" \
135 << interface << "\t" \
136 << printName << "\t" \
137 << STATS(2).fieldName << "\t" \
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600138 << STATS(3).fieldName << "\t" \
139 << STATS(0).fieldName << "\t" \
140 << STATS(1).fieldName / 1024.0 << "\n";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800141
142void
143L2RateTracer::Print (std::ostream &os) const
144{
145 Time time = Simulator::Now ();
146
147 PRINTER ("Drop", m_drop, "combined");
148}
149
150void
151L2RateTracer::Drop (Ptr<const Packet> packet)
152{
153 // no interface information... this should be part of this L2Tracer object data
154
155 m_stats.get<0> ().m_drop ++;
156 m_stats.get<1> ().m_drop += packet->GetSize ();
157}
158
159} // namespace ns3