Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -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 | * |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 18 | * Author: Xiaoyan Hu <x......u@gmail.com> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "ndn-cs-tracer.h" |
| 23 | #include "ns3/node.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/config.h" |
| 26 | #include "ns3/names.h" |
| 27 | #include "ns3/callback.h" |
| 28 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 29 | #include "ns3/ndn-app.h" |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 30 | #include "ns3/ndn-interest.h" |
| 31 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 32 | #include "ns3/simulator.h" |
| 33 | #include "ns3/node-list.h" |
| 34 | #include "ns3/log.h" |
| 35 | |
| 36 | #include <boost/lexical_cast.hpp> |
| 37 | |
| 38 | #include <fstream> |
| 39 | |
| 40 | NS_LOG_COMPONENT_DEFINE ("ndn.CsTracer"); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | namespace ns3 { |
| 45 | namespace ndn { |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 47 | template<class T> |
| 48 | static inline void |
| 49 | NullDeleter (T *ptr) |
| 50 | { |
| 51 | } |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 52 | |
| 53 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 54 | CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 55 | { |
| 56 | using namespace boost; |
| 57 | using namespace std; |
| 58 | |
| 59 | std::list<Ptr<CsTracer> > tracers; |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 60 | boost::shared_ptr<std::ostream> outputStream; |
| 61 | if (file != "-") |
| 62 | { |
| 63 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 64 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 66 | if (!os->is_open ()) |
| 67 | return boost::make_tuple (outputStream, tracers); |
| 68 | |
| 69 | outputStream = os; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 74 | } |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 75 | |
| 76 | for (NodeList::Iterator node = NodeList::Begin (); |
| 77 | node != NodeList::End (); |
| 78 | node++) |
| 79 | { |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 80 | Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 81 | tracers.push_back (trace); |
| 82 | } |
| 83 | |
| 84 | if (tracers.size () > 0) |
| 85 | { |
| 86 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 87 | tracers.front ()->PrintHeader (*outputStream); |
| 88 | *outputStream << "\n"; |
| 89 | } |
| 90 | |
| 91 | return boost::make_tuple (outputStream, tracers); |
| 92 | } |
| 93 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 94 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 95 | CsTracer::Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 96 | { |
| 97 | using namespace boost; |
| 98 | using namespace std; |
| 99 | |
| 100 | std::list<Ptr<CsTracer> > tracers; |
| 101 | boost::shared_ptr<std::ostream> outputStream; |
| 102 | if (file != "-") |
| 103 | { |
| 104 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 105 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 106 | |
| 107 | if (!os->is_open ()) |
| 108 | return boost::make_tuple (outputStream, tracers); |
| 109 | |
| 110 | outputStream = os; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 115 | } |
| 116 | |
| 117 | for (NodeContainer::Iterator node = nodes.Begin (); |
| 118 | node != nodes.End (); |
| 119 | node++) |
| 120 | { |
| 121 | Ptr<CsTracer> trace = Install (*node, outputStream, averagingPeriod); |
| 122 | tracers.push_back (trace); |
| 123 | } |
| 124 | |
| 125 | if (tracers.size () > 0) |
| 126 | { |
| 127 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 128 | tracers.front ()->PrintHeader (*outputStream); |
| 129 | *outputStream << "\n"; |
| 130 | } |
| 131 | |
| 132 | return boost::make_tuple (outputStream, tracers); |
| 133 | } |
| 134 | |
| 135 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 136 | CsTracer::Install (Ptr<Node> node, const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 137 | { |
| 138 | using namespace boost; |
| 139 | using namespace std; |
| 140 | |
| 141 | std::list<Ptr<CsTracer> > tracers; |
| 142 | boost::shared_ptr<std::ostream> outputStream; |
| 143 | if (file != "-") |
| 144 | { |
| 145 | boost::shared_ptr<std::ofstream> os (new std::ofstream ()); |
| 146 | os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 147 | |
| 148 | if (!os->is_open ()) |
| 149 | return boost::make_tuple (outputStream, tracers); |
| 150 | |
| 151 | outputStream = os; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>); |
| 156 | } |
| 157 | |
| 158 | Ptr<CsTracer> trace = Install (node, outputStream, averagingPeriod); |
| 159 | tracers.push_back (trace); |
| 160 | |
| 161 | if (tracers.size () > 0) |
| 162 | { |
| 163 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 164 | tracers.front ()->PrintHeader (*outputStream); |
| 165 | *outputStream << "\n"; |
| 166 | } |
| 167 | |
| 168 | return boost::make_tuple (outputStream, tracers); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | Ptr<CsTracer> |
| 173 | CsTracer::Install (Ptr<Node> node, |
| 174 | boost::shared_ptr<std::ostream> outputStream, |
| 175 | Time averagingPeriod/* = Seconds (0.5)*/) |
| 176 | { |
| 177 | NS_LOG_DEBUG ("Node: " << node->GetId ()); |
| 178 | |
| 179 | Ptr<CsTracer> trace = Create<CsTracer> (outputStream, node); |
| 180 | trace->SetAveragingPeriod (averagingPeriod); |
| 181 | |
| 182 | return trace; |
| 183 | } |
| 184 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 185 | ////////////////////////////////////////////////////////////////////////////// |
| 186 | ////////////////////////////////////////////////////////////////////////////// |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
| 189 | CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 190 | : m_nodePtr (node) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 191 | , m_os (os) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 192 | { |
| 193 | m_node = boost::lexical_cast<string> (m_nodePtr->GetId ()); |
| 194 | |
| 195 | Connect (); |
| 196 | |
| 197 | string name = Names::FindName (node); |
| 198 | if (!name.empty ()) |
| 199 | { |
| 200 | m_node = name; |
| 201 | } |
| 202 | } |
| 203 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 204 | CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 205 | : m_node (node) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 206 | , m_os (os) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 207 | { |
| 208 | Connect (); |
| 209 | } |
| 210 | |
| 211 | CsTracer::~CsTracer () |
| 212 | { |
| 213 | }; |
| 214 | |
| 215 | |
| 216 | void |
| 217 | CsTracer::Connect () |
| 218 | { |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 219 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits", |
| 220 | MakeCallback (&CsTracer::CacheHits, this)); |
| 221 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses", |
| 222 | MakeCallback (&CsTracer::CacheMisses, this)); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 223 | |
| 224 | Reset (); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 227 | |
| 228 | void |
| 229 | CsTracer::SetAveragingPeriod (const Time &period) |
| 230 | { |
| 231 | m_period = period; |
| 232 | m_printEvent.Cancel (); |
| 233 | m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this); |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | CsTracer::PeriodicPrinter () |
| 238 | { |
| 239 | Print (*m_os); |
| 240 | Reset (); |
| 241 | |
| 242 | m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this); |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | CsTracer::PrintHeader (std::ostream &os) const |
| 247 | { |
| 248 | os << "Time" << "\t" |
| 249 | |
| 250 | << "Node" << "\t" |
| 251 | |
| 252 | << "Type" << "\t" |
| 253 | << "Packets" << "\t"; |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | CsTracer::Reset () |
| 258 | { |
| 259 | m_stats.Reset(); |
| 260 | } |
| 261 | |
| 262 | #define PRINTER(printName, fieldName) \ |
| 263 | os << time.ToDouble (Time::S) << "\t" \ |
| 264 | << m_node << "\t" \ |
| 265 | << printName << "\t" \ |
| 266 | << m_stats.fieldName << "\n"; |
| 267 | |
| 268 | |
| 269 | void |
| 270 | CsTracer::Print (std::ostream &os) const |
| 271 | { |
| 272 | Time time = Simulator::Now (); |
| 273 | |
| 274 | PRINTER ("CacheHits", m_cacheHits); |
| 275 | PRINTER ("CacheMisses", m_cacheMisses); |
| 276 | } |
| 277 | |
| 278 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame^] | 279 | CsTracer::CacheHits (Ptr<const Interest>, Ptr<const Data>) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 280 | { |
| 281 | m_stats.m_cacheHits ++; |
| 282 | } |
| 283 | |
| 284 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 285 | CsTracer::CacheMisses (Ptr<const Interest>) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 286 | { |
| 287 | m_stats.m_cacheMisses ++; |
| 288 | } |
| 289 | |
| 290 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 291 | } // namespace ndn |
| 292 | } // namespace ns3 |