blob: 77ae3285338884bf7341cdf4e5831a229fca9773 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevc7597622013-02-28 10:58:52 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevc7597622013-02-28 10:58:52 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevc7597622013-02-28 10:58:52 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevc7597622013-02-28 10:58:52 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevc7597622013-02-28 10:58:52 -080019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "l2-rate-tracer.hpp"
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080021
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
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("L2RateTracer");
Alexander Afanasyevc7597622013-02-28 10:58:52 -080035
36namespace ns3 {
37
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080038static std::list<std::tuple<std::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer>>>>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 g_tracers;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070040
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070041void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042L2RateTracer::Destroy()
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 g_tracers.clear();
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -070045}
46
47void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048L2RateTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080049{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 std::list<Ptr<L2RateTracer>> tracers;
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080051 std::shared_ptr<std::ostream> outputStream;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 if (file != "-") {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080053 std::shared_ptr<std::ofstream> os(new std::ofstream());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
Alexander Afanasyevc7597622013-02-28 10:58:52 -080055
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 if (!os->is_open()) {
57 NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled");
58 return;
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070059 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080060
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 outputStream = os;
62 }
63 else {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080064 outputStream = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080066
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080068 NS_LOG_DEBUG("Node: " << boost::lexical_cast<std::string>((*node)->GetId()));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080069
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 Ptr<L2RateTracer> trace = Create<L2RateTracer>(outputStream, *node);
71 trace->SetAveragingPeriod(averagingPeriod);
72 tracers.push_back(trace);
73 }
Alexander Afanasyevc7597622013-02-28 10:58:52 -080074
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 if (tracers.size() > 0) {
76 // *m_l3RateTrace << "# "; // not necessary for R's read.table
77 tracers.front()->PrintHeader(*outputStream);
78 *outputStream << "\n";
79 }
80
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080081 g_tracers.push_back(std::make_tuple(outputStream, tracers));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080082}
83
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080084L2RateTracer::L2RateTracer(std::shared_ptr<std::ostream> os, Ptr<Node> node)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 : L2Tracer(node)
86 , m_os(os)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080087{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 SetAveragingPeriod(Seconds(1.0));
Alexander Afanasyevc7597622013-02-28 10:58:52 -080089}
90
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091L2RateTracer::~L2RateTracer()
Alexander Afanasyevc7597622013-02-28 10:58:52 -080092{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 m_printEvent.Cancel();
Alexander Afanasyevc7597622013-02-28 10:58:52 -080094}
95
96void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097L2RateTracer::SetAveragingPeriod(const Time& period)
Alexander Afanasyevc7597622013-02-28 10:58:52 -080098{
99 m_period = period;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 m_printEvent.Cancel();
101 m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this);
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800102}
103
104void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105L2RateTracer::PeriodicPrinter()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800106{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 Print(*m_os);
108 Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800109
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 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::PrintHeader(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800115{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 os << "Time"
117 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 << "Node"
120 << "\t"
121 << "Interface"
122 << "\t"
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800123
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 << "Type"
125 << "\t"
126 << "Packets"
127 << "\t"
128 << "Kilobytes"
129 << "\t"
130 << "PacketsRaw"
131 << "\t"
Alexander Afanasyevdca0f372013-03-18 11:53:03 -0600132 << "KilobytesRaw";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800133}
134
135void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136L2RateTracer::Reset()
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800137{
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800138 std::get<0>(m_stats).Reset();
139 std::get<1>(m_stats).Reset();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800140}
141
142const double alpha = 0.8;
143
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800144#define STATS(INDEX) std::get<INDEX>(m_stats)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800146
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147#define PRINTER(printName, fieldName, interface) \
148 STATS(2).fieldName = \
149 /*new value*/ alpha * RATE(0, fieldName) + /*old value*/ (1 - alpha) * STATS(2).fieldName; \
150 STATS(3).fieldName = /*new value*/ alpha * RATE(1, fieldName) / 1024.0 \
151 + /*old value*/ (1 - alpha) * STATS(3).fieldName; \
152 \
153 os << time.ToDouble(Time::S) << "\t" << m_node << "\t" << interface << "\t" << printName << "\t" \
154 << STATS(2).fieldName << "\t" << STATS(3).fieldName << "\t" << STATS(0).fieldName << "\t" \
155 << STATS(1).fieldName / 1024.0 << "\n";
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800156
157void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158L2RateTracer::Print(std::ostream& os) const
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800159{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 Time time = Simulator::Now();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800161
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 PRINTER("Drop", m_drop, "combined");
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800163}
164
165void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166L2RateTracer::Drop(Ptr<const Packet> packet)
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800167{
168 // no interface information... this should be part of this L2Tracer object data
169
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800170 std::get<0>(m_stats).m_drop++;
171 std::get<1>(m_stats).m_drop += packet->GetSize();
Alexander Afanasyevc7597622013-02-28 10:58:52 -0800172}
173
174} // namespace ns3