Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "l2-rate-tracer.hpp" |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 34 | NS_LOG_COMPONENT_DEFINE("L2RateTracer"); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ns3 { |
| 37 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 38 | static std::list<std::tuple<std::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer>>>> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | g_tracers; |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 41 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | L2RateTracer::Destroy() |
Alexander Afanasyev | db5f3b6 | 2013-08-09 17:42:12 -0700 | [diff] [blame] | 43 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | g_tracers.clear(); |
Alexander Afanasyev | db5f3b6 | 2013-08-09 17:42:12 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 48 | L2RateTracer::InstallAll(const std::string& file, Time averagingPeriod /* = Seconds (0.5)*/) |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | std::list<Ptr<L2RateTracer>> tracers; |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 51 | std::shared_ptr<std::ostream> outputStream; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | if (file != "-") { |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 53 | std::shared_ptr<std::ofstream> os(new std::ofstream()); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | os->open(file.c_str(), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | if (!os->is_open()) { |
| 57 | NS_LOG_ERROR("File " << file << " cannot be opened for writing. Tracing disabled"); |
| 58 | return; |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 59 | } |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | outputStream = os; |
| 62 | } |
| 63 | else { |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 64 | outputStream = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{})); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | } |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 68 | NS_LOG_DEBUG("Node: " << boost::lexical_cast<std::string>((*node)->GetId())); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 70 | Ptr<L2RateTracer> trace = Create<L2RateTracer>(outputStream, *node); |
| 71 | trace->SetAveragingPeriod(averagingPeriod); |
| 72 | tracers.push_back(trace); |
| 73 | } |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | 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 Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 81 | g_tracers.push_back(std::make_tuple(outputStream, tracers)); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 84 | L2RateTracer::L2RateTracer(std::shared_ptr<std::ostream> os, Ptr<Node> node) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | : L2Tracer(node) |
| 86 | , m_os(os) |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | SetAveragingPeriod(Seconds(1.0)); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | L2RateTracer::~L2RateTracer() |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | m_printEvent.Cancel(); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 97 | L2RateTracer::SetAveragingPeriod(const Time& period) |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 98 | { |
| 99 | m_period = period; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | m_printEvent.Cancel(); |
| 101 | m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | L2RateTracer::PeriodicPrinter() |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 106 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 107 | Print(*m_os); |
| 108 | Reset(); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 110 | m_printEvent = Simulator::Schedule(m_period, &L2RateTracer::PeriodicPrinter, this); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | L2RateTracer::PrintHeader(std::ostream& os) const |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 115 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | os << "Time" |
| 117 | << "\t" |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | << "Node" |
| 120 | << "\t" |
| 121 | << "Interface" |
| 122 | << "\t" |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 124 | << "Type" |
| 125 | << "\t" |
| 126 | << "Packets" |
| 127 | << "\t" |
| 128 | << "Kilobytes" |
| 129 | << "\t" |
| 130 | << "PacketsRaw" |
| 131 | << "\t" |
Alexander Afanasyev | dca0f37 | 2013-03-18 11:53:03 -0600 | [diff] [blame] | 132 | << "KilobytesRaw"; |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 136 | L2RateTracer::Reset() |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 137 | { |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 138 | std::get<0>(m_stats).Reset(); |
| 139 | std::get<1>(m_stats).Reset(); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | const double alpha = 0.8; |
| 143 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 144 | #define STATS(INDEX) std::get<INDEX>(m_stats) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble(Time::S) |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | #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 Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 156 | |
| 157 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 158 | L2RateTracer::Print(std::ostream& os) const |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 159 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | Time time = Simulator::Now(); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | PRINTER("Drop", m_drop, "combined"); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 166 | L2RateTracer::Drop(Ptr<const Packet> packet) |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 167 | { |
| 168 | // no interface information... this should be part of this L2Tracer object data |
| 169 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 170 | std::get<0>(m_stats).m_drop++; |
| 171 | std::get<1>(m_stats).m_drop += packet->GetSize(); |
Alexander Afanasyev | c759762 | 2013-02-28 10:58:52 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } // namespace ns3 |