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