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 | |
| 47 | |
| 48 | boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 49 | CsTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/) |
| 50 | { |
| 51 | using namespace boost; |
| 52 | using namespace std; |
| 53 | |
| 54 | std::list<Ptr<CsTracer> > tracers; |
| 55 | boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ()); |
| 56 | outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc); |
| 57 | |
| 58 | if (!outputStream->is_open ()) |
| 59 | return boost::make_tuple (outputStream, tracers); |
| 60 | |
| 61 | for (NodeList::Iterator node = NodeList::Begin (); |
| 62 | node != NodeList::End (); |
| 63 | node++) |
| 64 | { |
| 65 | NS_LOG_DEBUG ("Node: " << (*node)->GetId ()); |
| 66 | |
| 67 | Ptr<CsTracer> trace = Create<CsTracer> (outputStream, *node); |
| 68 | trace->SetAveragingPeriod (averagingPeriod); |
| 69 | tracers.push_back (trace); |
| 70 | } |
| 71 | |
| 72 | if (tracers.size () > 0) |
| 73 | { |
| 74 | // *m_l3RateTrace << "# "; // not necessary for R's read.table |
| 75 | tracers.front ()->PrintHeader (*outputStream); |
| 76 | *outputStream << "\n"; |
| 77 | } |
| 78 | |
| 79 | return boost::make_tuple (outputStream, tracers); |
| 80 | } |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | ////////////////////////////////////////////////////////////////////////////// |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 87 | : m_nodePtr (node) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 88 | , m_os (os) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 89 | { |
| 90 | m_node = boost::lexical_cast<string> (m_nodePtr->GetId ()); |
| 91 | |
| 92 | Connect (); |
| 93 | |
| 94 | string name = Names::FindName (node); |
| 95 | if (!name.empty ()) |
| 96 | { |
| 97 | m_node = name; |
| 98 | } |
| 99 | } |
| 100 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 101 | CsTracer::CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 102 | : m_node (node) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 103 | , m_os (os) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 104 | { |
| 105 | Connect (); |
| 106 | } |
| 107 | |
| 108 | CsTracer::~CsTracer () |
| 109 | { |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | void |
| 114 | CsTracer::Connect () |
| 115 | { |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame^] | 116 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits", |
| 117 | MakeCallback (&CsTracer::CacheHits, this)); |
| 118 | Config::ConnectWithoutContext ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses", |
| 119 | MakeCallback (&CsTracer::CacheMisses, this)); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 120 | |
| 121 | Reset (); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 124 | |
| 125 | void |
| 126 | CsTracer::SetAveragingPeriod (const Time &period) |
| 127 | { |
| 128 | m_period = period; |
| 129 | m_printEvent.Cancel (); |
| 130 | m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | CsTracer::PeriodicPrinter () |
| 135 | { |
| 136 | Print (*m_os); |
| 137 | Reset (); |
| 138 | |
| 139 | m_printEvent = Simulator::Schedule (m_period, &CsTracer::PeriodicPrinter, this); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | CsTracer::PrintHeader (std::ostream &os) const |
| 144 | { |
| 145 | os << "Time" << "\t" |
| 146 | |
| 147 | << "Node" << "\t" |
| 148 | |
| 149 | << "Type" << "\t" |
| 150 | << "Packets" << "\t"; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | CsTracer::Reset () |
| 155 | { |
| 156 | m_stats.Reset(); |
| 157 | } |
| 158 | |
| 159 | #define PRINTER(printName, fieldName) \ |
| 160 | os << time.ToDouble (Time::S) << "\t" \ |
| 161 | << m_node << "\t" \ |
| 162 | << printName << "\t" \ |
| 163 | << m_stats.fieldName << "\n"; |
| 164 | |
| 165 | |
| 166 | void |
| 167 | CsTracer::Print (std::ostream &os) const |
| 168 | { |
| 169 | Time time = Simulator::Now (); |
| 170 | |
| 171 | PRINTER ("CacheHits", m_cacheHits); |
| 172 | PRINTER ("CacheMisses", m_cacheMisses); |
| 173 | } |
| 174 | |
| 175 | void |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame^] | 176 | CsTracer::CacheHits (Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 177 | { |
| 178 | m_stats.m_cacheHits ++; |
| 179 | } |
| 180 | |
| 181 | void |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame^] | 182 | CsTracer::CacheMisses (Ptr<const InterestHeader>) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 183 | { |
| 184 | m_stats.m_cacheMisses ++; |
| 185 | } |
| 186 | |
| 187 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 188 | } // namespace ndn |
| 189 | } // namespace ns3 |