blob: f5c4d221f706e87c89a51d67d45520579e5683cb [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080021#include "l2-rate-tracer.hpp"
Alexander Afanasyevc7597622013-02-28 10:58:52 -080022#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
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_LOG_COMPONENT_DEFINE("L2RateTracer");
Alexander Afanasyevc7597622013-02-28 10:58:52 -080038
39namespace ns3 {
40
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041static std::list<boost::tuple<boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer>>>>
42 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070043
44template<class T>
45static inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046NullDeleter(T* ptr)
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070047{
48}
49
50void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051L2RateTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070052{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070054}
55
56void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057L2RateTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080058{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 std::list<Ptr<L2RateTracer>> tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070060 boost::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 if (file != "-") {
62 boost::shared_ptr<std::ofstream> os(new std::ofstream());
63 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevc7597622013-02-28 10:58:52 -080064
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 if (!os->is_open()) {
66 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
67 return;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070068 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080069
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 outputStream = os;
71 }
72 else {
73 outputStream = boost::shared_ptr<std::ostream>(&std::cout, NullDeleter<std::ostream>);
74 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080075
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
77 NS_LOG_DEBUG("Node: " << lexical_cast<string>((*node)->GetId()));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 Ptr<L2RateTracer> trace = Create<L2RateTracer>(outputStream, *node);
80 trace->SetAveragingPeriod(averagingPeriod);
81 tracers.push_back(trace);
82 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080083
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 if (tracers.size() > 0) {
85 // *m_l3RateTrace << "# "; // not necessary for R's read.table
86 tracers.front()->PrintHeader(*outputStream);
87 *outputStream << "\n";
88 }
89
90 g_tracers.push_back(boost::make_tuple(outputStream, tracers));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080091}
92
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093L2RateTracer::L2RateTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node)
94 : L2Tracer(node)
95 , m_os(os)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080096{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 SetAveragingPeriod(Seconds(1.0));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080098}
99
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100L2RateTracer::~L2RateTracer()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800101{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 m_printEvent.Cancel();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800103}
104
105void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106L2RateTracer::SetAveragingPeriod(const Time& period)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800107{
108 m_period = period;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 m_printEvent.Cancel();
110 m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800111}
112
113void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114L2RateTracer::PeriodicPrinter()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800115{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 Print(*m_os);
117 Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800120}
121
122void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123L2RateTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800124{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 os << "Time"
126 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800127
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 << "Node"
129 << "\t"
130 << "Interface"
131 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800132
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 << "Type"
134 << "\t"
135 << "Packets"
136 << "\t"
137 << "Kilobytes"
138 << "\t"
139 << "PacketsRaw"
140 << "\t"
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600141 << "KilobytesRaw";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800142}
143
144void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145L2RateTracer::Reset()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800146{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 m_stats.get<0>().Reset();
148 m_stats.get<1>().Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800149}
150
151const double alpha = 0.8;
152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153#define STATS(INDEX) m_stats.get<INDEX>()
154#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800155
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156#define PRINTER(printName, fieldName, interface) \
157 STATS(2).fieldName = \
158 /*new value*/ alpha * RATE(0, fieldName) + /*old value*/ (1 - alpha) * STATS(2).fieldName; \
159 STATS(3).fieldName = /*new value*/ alpha * RATE(1, fieldName) / 1024.0 \
160 + /*old value*/ (1 - alpha) * STATS(3).fieldName; \
161 \
162 os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << interface << "\t" << printName << "\t" \
163 << STATS(2).fieldName << "\t" << STATS(3).fieldName << "\t" << STATS(0).fieldName << "\t" \
164 << STATS(1).fieldName / 1024.0 << "\n";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800165
166void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167L2RateTracer::Print(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800168{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 Time time = Simulator::Now();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800170
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171 PRINTER("Drop", m_drop, "combined");
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800172}
173
174void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175L2RateTracer::Drop(Ptr<const Packet> packet)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800176{
177 // no interface information... this should be part of this L2Tracer object data
178
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 m_stats.get<0>().m_drop++;
180 m_stats.get<1>().m_drop += packet->GetSize();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800181}
182
183} // namespace ns3