Alexander Afanasyev | b3e4b85 | 2011-12-23 15:58:20 -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 "ccnx-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/ccnx-app.h" |
| 29 | #include "ns3/ccnx-face.h" |
| 30 | #include "ns3/ccnx-interest-header.h" |
| 31 | #include "ns3/ccnx-content-object-header.h" |
| 32 | |
| 33 | namespace ns3 { |
| 34 | |
| 35 | CcnxRateL3Tracer::CcnxRateL3Tracer (std::ostream &os, Ptr<Node> node) |
| 36 | : CcnxL3Tracer (node) |
| 37 | , m_os (os) |
| 38 | { |
| 39 | SetAveragingPeriod (Seconds (1.0)); |
| 40 | } |
| 41 | |
| 42 | CcnxRateL3Tracer::CcnxRateL3Tracer (std::ostream &os, const std::string &node) |
| 43 | : CcnxL3Tracer (node) |
| 44 | , m_os (os) |
| 45 | { |
| 46 | SetAveragingPeriod (Seconds (1.0)); |
| 47 | } |
| 48 | |
| 49 | CcnxRateL3Tracer::~CcnxRateL3Tracer () |
| 50 | { |
| 51 | m_printEvent.Cancel (); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | CcnxRateL3Tracer::SetAveragingPeriod (const Time &period) |
| 56 | { |
| 57 | m_period = period; |
| 58 | m_printEvent.Cancel (); |
| 59 | m_printEvent = Simulator::Schedule (m_period, &CcnxRateL3Tracer::PeriodicPrinter, this); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | CcnxRateL3Tracer::PeriodicPrinter () |
| 64 | { |
| 65 | Print (m_os); |
| 66 | Reset (); |
| 67 | |
| 68 | m_printEvent = Simulator::Schedule (m_period, &CcnxRateL3Tracer::PeriodicPrinter, this); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | CcnxRateL3Tracer::PrintHeader (std::ostream &os) const |
| 73 | { |
| 74 | os << "Time" << "\t" |
| 75 | |
| 76 | << "Node" << "\t" |
| 77 | << "FaceId" << "\t" |
| 78 | << "FaceDescr" << "\t" |
| 79 | |
| 80 | << "Type" << "\t" |
| 81 | << "Packets" << "\t" |
| 82 | << "Kilobytes"; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | CcnxRateL3Tracer::Reset () |
| 87 | { |
| 88 | for (std::map<Ptr<const CcnxFace>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 89 | stats != m_stats.end (); |
| 90 | stats++) |
| 91 | { |
| 92 | stats->second.get<0> ().Reset (); |
| 93 | stats->second.get<1> ().Reset (); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | const double alpha = 0.8; |
| 98 | |
| 99 | #define STATS(INDEX) stats->second.get<INDEX> () |
| 100 | #define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S) |
| 101 | |
| 102 | #define PRINTER(printName, fieldName) \ |
| 103 | STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \ |
| 104 | STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \ |
| 105 | \ |
| 106 | os << time.ToDouble (Time::S) << "\t" \ |
| 107 | << m_node << "\t" \ |
| 108 | << stats->first->GetId () << "\t" \ |
| 109 | << *stats->first << "\t" \ |
| 110 | << printName << "\t" \ |
| 111 | << STATS(2).fieldName << "\t" \ |
| 112 | << STATS(3).fieldName << "\n"; |
| 113 | |
| 114 | void |
| 115 | CcnxRateL3Tracer::Print (std::ostream &os) const |
| 116 | { |
| 117 | for (std::map<Ptr<const CcnxFace>, boost::tuple<Stats, Stats, Stats, Stats> >::iterator stats = m_stats.begin (); |
| 118 | stats != m_stats.end (); |
| 119 | stats++) |
| 120 | { |
| 121 | Time time = Simulator::Now (); |
| 122 | |
| 123 | PRINTER ("InInterests", m_inInterests); |
| 124 | PRINTER ("OutInterests", m_outInterests); |
| 125 | PRINTER ("DropInterests", m_dropInterests); |
| 126 | |
| 127 | PRINTER ("InNacks", m_inNacks); |
| 128 | PRINTER ("OutNacks", m_outNacks); |
| 129 | PRINTER ("DropNacks", m_dropNacks); |
| 130 | |
| 131 | PRINTER ("InData", m_inData); |
| 132 | PRINTER ("OutData", m_outData); |
| 133 | PRINTER ("DropData", m_dropData); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | CcnxRateL3Tracer::OutInterests (std::string context, |
| 140 | Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face) |
| 141 | { |
| 142 | m_stats[face].get<0> ().m_outInterests ++; |
| 143 | m_stats[face].get<1> ().m_outInterests += header->GetSerializedSize (); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | CcnxRateL3Tracer::InInterests (std::string context, |
| 148 | Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face) |
| 149 | { |
| 150 | m_stats[face].get<0> ().m_inInterests ++; |
| 151 | m_stats[face].get<1> ().m_inInterests += header->GetSerializedSize (); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | CcnxRateL3Tracer::DropInterests (std::string context, |
| 156 | Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace> face) |
| 157 | { |
| 158 | m_stats[face].get<0> ().m_dropInterests ++; |
| 159 | m_stats[face].get<1> ().m_dropInterests += header->GetSerializedSize (); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | CcnxRateL3Tracer::OutNacks (std::string context, |
| 164 | Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face) |
| 165 | { |
| 166 | m_stats[face].get<0> ().m_outNacks ++; |
| 167 | m_stats[face].get<1> ().m_outNacks += header->GetSerializedSize (); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | CcnxRateL3Tracer::InNacks (std::string context, |
| 172 | Ptr<const CcnxInterestHeader> header, Ptr<const CcnxFace> face) |
| 173 | { |
| 174 | m_stats[face].get<0> ().m_inNacks ++; |
| 175 | m_stats[face].get<1> ().m_inNacks += header->GetSerializedSize (); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | CcnxRateL3Tracer::DropNacks (std::string context, |
| 180 | Ptr<const CcnxInterestHeader> header, Ccnx::DropReason, Ptr<const CcnxFace> face) |
| 181 | { |
| 182 | m_stats[face].get<0> ().m_dropNacks ++; |
| 183 | m_stats[face].get<1> ().m_dropNacks += header->GetSerializedSize (); |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | CcnxRateL3Tracer::OutData (std::string context, |
| 188 | Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload, |
| 189 | bool fromCache, Ptr<const CcnxFace> face) |
| 190 | { |
| 191 | m_stats[face].get<0> ().m_inData ++; |
| 192 | m_stats[face].get<1> ().m_inData += header->GetSerializedSize () + payload->GetSize (); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | CcnxRateL3Tracer::InData (std::string context, |
| 197 | Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload, |
| 198 | Ptr<const CcnxFace> face) |
| 199 | { |
| 200 | m_stats[face].get<0> ().m_outData ++; |
| 201 | m_stats[face].get<1> ().m_outData += header->GetSerializedSize () + payload->GetSize (); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | CcnxRateL3Tracer::DropData (std::string context, |
| 206 | Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> payload, |
| 207 | Ccnx::DropReason, Ptr<const CcnxFace> face) |
| 208 | { |
| 209 | m_stats[face].get<0> ().m_dropData ++; |
| 210 | m_stats[face].get<1> ().m_dropData += header->GetSerializedSize () + payload->GetSize (); |
| 211 | } |
| 212 | |
| 213 | } // namespace ns3 |