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-2012 University of California, Los Angeles |
| 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: Xiaoyan Hu <x......u@gmail.com> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef CCNX_CS_TRACER_H |
| 23 | #define CCNX_CS_TRACER_H |
| 24 | |
| 25 | #include "ns3/ptr.h" |
| 26 | #include "ns3/simple-ref-count.h" |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 27 | #include <ns3/nstime.h> |
| 28 | #include <ns3/event-id.h> |
| 29 | |
| 30 | #include <boost/tuple/tuple.hpp> |
| 31 | #include <boost/shared_ptr.hpp> |
| 32 | #include <map> |
| 33 | #include <list> |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 34 | |
| 35 | namespace ns3 { |
| 36 | |
| 37 | class Node; |
| 38 | class Packet; |
| 39 | |
| 40 | namespace ndn { |
| 41 | |
| 42 | class InterestHeader; |
| 43 | class ContentObjectHeader; |
| 44 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 45 | namespace cs { |
| 46 | |
| 47 | struct Stats |
| 48 | { |
| 49 | inline void Reset () |
| 50 | { |
| 51 | m_cacheHits = 0; |
| 52 | m_cacheMisses = 0; |
| 53 | } |
| 54 | double m_cacheHits; |
| 55 | double m_cacheMisses; |
| 56 | }; |
| 57 | |
| 58 | } |
| 59 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 60 | /** |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 61 | * @ingroup ndn |
| 62 | * @brief NDN tracer for cache performance (hits and misses) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 63 | */ |
| 64 | class CsTracer : public SimpleRefCount<CsTracer> |
| 65 | { |
| 66 | public: |
| 67 | /** |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 68 | * @brief Helper method to install tracers on all simulation nodes |
| 69 | * |
| 70 | * @param file File to which traces will be written |
| 71 | * @param averagingPeriod How often data will be written into the trace file (default, every half second) |
| 72 | * |
| 73 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved |
| 74 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 75 | * |
| 76 | */ |
| 77 | static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 78 | InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5)); |
| 79 | |
| 80 | /** |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 81 | * @brief Trace constructor that attaches to the node using node pointer |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 82 | * @param os reference to the output stream |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 83 | * @param node pointer to the node |
| 84 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 85 | CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @brief Trace constructor that attaches to the node using node name |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 89 | * @param os reference to the output stream |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 90 | * @param nodeName name of the node registered using Names::Add |
| 91 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 92 | CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * @brief Destructor |
| 96 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 97 | ~CsTracer (); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * @brief Print head of the trace (e.g., for post-processing) |
| 101 | * |
| 102 | * @param os reference to output stream |
| 103 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 104 | void |
| 105 | PrintHeader (std::ostream &os) const; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * @brief Print current trace data |
| 109 | * |
| 110 | * @param os reference to output stream |
| 111 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 112 | void |
| 113 | Print (std::ostream &os) const; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 115 | private: |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 116 | void |
| 117 | Connect (); |
| 118 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 119 | void |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 120 | CacheHits (Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 122 | void |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 123 | CacheMisses (Ptr<const InterestHeader>); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 125 | private: |
| 126 | void |
| 127 | SetAveragingPeriod (const Time &period); |
| 128 | |
| 129 | void |
| 130 | Reset (); |
| 131 | |
| 132 | void |
| 133 | PeriodicPrinter (); |
| 134 | |
| 135 | private: |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 136 | std::string m_node; |
| 137 | Ptr<Node> m_nodePtr; |
| 138 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 139 | boost::shared_ptr<std::ostream> m_os; |
| 140 | |
| 141 | Time m_period; |
| 142 | EventId m_printEvent; |
| 143 | cs::Stats m_stats; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * @brief Helper to dump the trace to an output stream |
| 148 | */ |
| 149 | inline std::ostream& |
| 150 | operator << (std::ostream &os, const CsTracer &tracer) |
| 151 | { |
| 152 | os << "# "; |
| 153 | tracer.PrintHeader (os); |
| 154 | os << "\n"; |
| 155 | tracer.Print (os); |
| 156 | return os; |
| 157 | } |
| 158 | |
| 159 | } // namespace ndn |
| 160 | } // namespace ns3 |
| 161 | |
| 162 | #endif // CCNX_CS_TRACER_H |