blob: c0ccdc060795b70fb21a5271dab04ea20de7aaa4 [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"
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080022
Alexander Afanasyevc7597622013-02-28 10:58:52 -080023#include "ns3/node.h"
24#include "ns3/packet.h"
25#include "ns3/config.h"
26#include "ns3/callback.h"
27#include "ns3/simulator.h"
28#include "ns3/node-list.h"
29#include "ns3/node.h"
30#include "ns3/log.h"
31
32#include <boost/lexical_cast.hpp>
33#include <fstream>
34
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035NS_LOG_COMPONENT_DEFINE("L2RateTracer");
Alexander Afanasyevc7597622013-02-28 10:58:52 -080036
37namespace ns3 {
38
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080039static std::list<std::tuple<std::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer>>>>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070041
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070042void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043L2RateTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070044{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070046}
47
48void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049L2RateTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080050{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 std::list<Ptr<L2RateTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080052 std::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080054 std::shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevc7597622013-02-28 10:58:52 -080056
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 if (!os->is_open()) {
58 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
59 return;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070060 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080061
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 outputStream = os;
63 }
64 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080065 outputStream = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080067
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080069 NS_LOG_DEBUG("Node: " << boost::lexical_cast<std::string>((*node)->GetId()));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080070
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 Ptr<L2RateTracer> trace = Create<L2RateTracer>(outputStream, *node);
72 trace->SetAveragingPeriod(averagingPeriod);
73 tracers.push_back(trace);
74 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080075
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 if (tracers.size() > 0) {
77 // *m_l3RateTrace << "# "; // not necessary for R's read.table
78 tracers.front()->PrintHeader(*outputStream);
79 *outputStream << "\n";
80 }
81
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080082 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080083}
84
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080085L2RateTracer::L2RateTracer(std::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 : L2Tracer(node)
87 , m_os(os)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080088{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 SetAveragingPeriod(Seconds(1.0));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080090}
91
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092L2RateTracer::~L2RateTracer()
Alexander Afanasyevc7597622013-02-28 10:58:52 -080093{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 m_printEvent.Cancel();
Alexander Afanasyevc7597622013-02-28 10:58:52 -080095}
96
97void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098L2RateTracer::SetAveragingPeriod(const Time& period)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080099{
100 m_period = period;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 m_printEvent.Cancel();
102 m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800103}
104
105void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106L2RateTracer::PeriodicPrinter()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800107{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 Print(*m_os);
109 Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800110
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800112}
113
114void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115L2RateTracer::PrintHeader(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800116{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 os << "Time"
118 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800119
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 << "Node"
121 << "\t"
122 << "Interface"
123 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800124
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 << "Type"
126 << "\t"
127 << "Packets"
128 << "\t"
129 << "Kilobytes"
130 << "\t"
131 << "PacketsRaw"
132 << "\t"
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600133 << "KilobytesRaw";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800134}
135
136void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137L2RateTracer::Reset()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800138{
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800139 std::get<0>(m_stats).Reset();
140 std::get<1>(m_stats).Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800141}
142
143const double alpha = 0.8;
144
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800145#define STATS(INDEX) std::get<INDEX>(m_stats)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800147
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148#define PRINTER(printName, fieldName, interface) \
149 STATS(2).fieldName = \
150 /*new value*/ alpha * RATE(0, fieldName) + /*old value*/ (1 - alpha) * STATS(2).fieldName; \
151 STATS(3).fieldName = /*new value*/ alpha * RATE(1, fieldName) / 1024.0 \
152 + /*old value*/ (1 - alpha) * STATS(3).fieldName; \
153 \
154 os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << interface << "\t" << printName << "\t" \
155 << STATS(2).fieldName << "\t" << STATS(3).fieldName << "\t" << STATS(0).fieldName << "\t" \
156 << STATS(1).fieldName / 1024.0 << "\n";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800157
158void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159L2RateTracer::Print(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800160{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 Time time = Simulator::Now();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800162
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 PRINTER("Drop", m_drop, "combined");
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800164}
165
166void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167L2RateTracer::Drop(Ptr<const Packet> packet)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800168{
169 // no interface information... this should be part of this L2Tracer object data
170
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800171 std::get<0>(m_stats).m_drop++;
172 std::get<1>(m_stats).m_drop += packet->GetSize();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800173}
174
175} // namespace ns3