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