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