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