Alexander Afanasyev | 7e71c75 | 2012-01-25 21:40:39 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 UCLA |
| 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 "ipv4-rate-l3-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 | |
| 28 | #include "ns3/ipv4-header.h" |
| 29 | |
| 30 | namespace ns3 { |
| 31 | |
| 32 | Ipv4RateL3Tracer::Ipv4RateL3Tracer (std::ostream &os, Ptr<Node> node) |
| 33 | : Ipv4L3Tracer (node) |
| 34 | , m_os (os) |
| 35 | { |
| 36 | SetAveragingPeriod (Seconds (1.0)); |
| 37 | } |
| 38 | |
| 39 | Ipv4RateL3Tracer::~Ipv4RateL3Tracer () |
| 40 | { |
| 41 | m_printEvent.Cancel (); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | Ipv4RateL3Tracer::SetAveragingPeriod (const Time &period) |
| 46 | { |
| 47 | m_period = period; |
| 48 | m_printEvent.Cancel (); |
| 49 | m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter, this); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | Ipv4RateL3Tracer::PeriodicPrinter () |
| 54 | { |
| 55 | Print (m_os); |
| 56 | Reset (); |
| 57 | |
| 58 | m_printEvent = Simulator::Schedule (m_period, &Ipv4RateL3Tracer::PeriodicPrinter, this); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | Ipv4RateL3Tracer::PrintHeader (std::ostream &os) const |
| 63 | { |
| 64 | os << "Time" << "\t" |
| 65 | |
| 66 | << "Node" << "\t" |
| 67 | << "Interface" << "\t" |
| 68 | |
| 69 | << "Type" << "\t" |
| 70 | << "Packets" << "\t" |
| 71 | << "Kilobytes"; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | Ipv4RateL3Tracer::Reset () |
| 76 | { |
| 77 | for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 78 | stats != m_stats.end (); |
| 79 | stats++) |
| 80 | { |
| 81 | stats->second.get<0> ().Reset (); |
| 82 | stats->second.get<1> ().Reset (); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const double alpha = 0.8; |
| 87 | |
| 88 | #define STATS(INDEX) stats->second.get<INDEX> () |
| 89 | #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S) |
| 90 | |
| 91 | #define PRINTER(printName, fieldName) \ |
| 92 | STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \ |
| 93 | STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \ |
| 94 | \ |
| 95 | os << time.ToDouble (Time::S) << "\t" \ |
| 96 | << m_node << "\t" \ |
| 97 | << stats->first << "\t" \ |
| 98 | << printName << "\t" \ |
| 99 | << STATS(2).fieldName << "\t" \ |
| 100 | << STATS(3).fieldName << "\n"; |
| 101 | |
| 102 | void |
| 103 | Ipv4RateL3Tracer::Print (std::ostream &os) const |
| 104 | { |
| 105 | for (std::map<uint32_t, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 106 | stats != m_stats.end (); |
| 107 | stats++) |
| 108 | { |
| 109 | Time time = Simulator::Now (); |
| 110 | |
| 111 | PRINTER ("In", m_in); |
| 112 | PRINTER ("Out", m_out); |
| 113 | PRINTER ("Drop", m_drop); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Ipv4RateL3Tracer::Rx (std::string context, |
| 119 | Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface) |
| 120 | { |
| 121 | m_stats[iface].get<0> ().m_in ++; |
| 122 | m_stats[iface].get<1> ().m_in += packet->GetSize (); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Ipv4RateL3Tracer::Tx (std::string context, |
| 127 | Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t iface) |
| 128 | { |
| 129 | m_stats[iface].get<0> ().m_out ++; |
| 130 | m_stats[iface].get<1> ().m_out += packet->GetSize (); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | Ipv4RateL3Tracer::Drop (std::string context, |
| 135 | const Ipv4Header &header, Ptr<const Packet> packet, Ipv4L3Protocol::DropReason, Ptr<Ipv4> ipv4, uint32_t iface) |
| 136 | { |
| 137 | m_stats[iface].get<0> ().m_drop ++; |
| 138 | m_stats[iface].get<1> ().m_drop += packet->GetSize (); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | } // namespace ns3 |