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