blob: 823977b1ca70b966edfe5e93a164a478032f455e [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"
112 << "Kilobytes";
113}
114
115void
116L2RateTracer::Reset ()
117{
118 m_stats.get<0> ().Reset ();
119 m_stats.get<1> ().Reset ();
120}
121
122const double alpha = 0.8;
123
124#define STATS(INDEX) m_stats.get<INDEX> ()
125#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
126
127#define PRINTER(printName, fieldName, interface) \
128STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
129STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
130 \
131 os << time.ToDouble (Time::S) << "\t" \
132 << m_node << "\t" \
133 << interface << "\t" \
134 << printName << "\t" \
135 << STATS(2).fieldName << "\t" \
136 << STATS(3).fieldName << "\n";
137
138void
139L2RateTracer::Print (std::ostream &os) const
140{
141 Time time = Simulator::Now ();
142
143 PRINTER ("Drop", m_drop, "combined");
144}
145
146void
147L2RateTracer::Drop (Ptr<const Packet> packet)
148{
149 // no interface information... this should be part of this L2Tracer object data
150
151 m_stats.get<0> ().m_drop ++;
152 m_stats.get<1> ().m_drop += packet->GetSize ();
153}
154
155} // namespace ns3