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> |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 29 | #include <ns3/node-container.h> |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 30 | |
| 31 | #include <boost/tuple/tuple.hpp> |
| 32 | #include <boost/shared_ptr.hpp> |
| 33 | #include <map> |
| 34 | #include <list> |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ns3 { |
| 37 | |
| 38 | class Node; |
| 39 | class Packet; |
| 40 | |
| 41 | namespace ndn { |
| 42 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 43 | class Interest; |
| 44 | class ContentObject; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 46 | typedef Interest InterestHeader; |
| 47 | typedef ContentObject ContentObjectHeader; |
| 48 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 49 | namespace cs { |
| 50 | |
| 51 | struct Stats |
| 52 | { |
| 53 | inline void Reset () |
| 54 | { |
| 55 | m_cacheHits = 0; |
| 56 | m_cacheMisses = 0; |
| 57 | } |
| 58 | double m_cacheHits; |
| 59 | double m_cacheMisses; |
| 60 | }; |
| 61 | |
| 62 | } |
| 63 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 64 | /** |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 65 | * @ingroup ndn |
| 66 | * @brief NDN tracer for cache performance (hits and misses) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 67 | */ |
| 68 | class CsTracer : public SimpleRefCount<CsTracer> |
| 69 | { |
| 70 | public: |
| 71 | /** |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 72 | * @brief Helper method to install tracers on all simulation nodes |
| 73 | * |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 74 | * @param file File to which traces will be written. If filename is -, then std::out is used |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 75 | * @param averagingPeriod How often data will be written into the trace file (default, every half second) |
| 76 | * |
| 77 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved |
| 78 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 79 | * |
| 80 | */ |
| 81 | static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 82 | InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5)); |
| 83 | |
| 84 | /** |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 85 | * @brief Helper method to install tracers on the selected simulation nodes |
| 86 | * |
| 87 | * @param nodes Nodes on which to install tracer |
| 88 | * @param file File to which traces will be written. If filename is -, then std::out is used |
| 89 | * @param averagingPeriod How often data will be written into the trace file (default, every half second) |
| 90 | * |
| 91 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved |
| 92 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 93 | * |
| 94 | */ |
| 95 | static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 96 | Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod = Seconds (0.5)); |
| 97 | |
| 98 | /** |
| 99 | * @brief Helper method to install tracers on a specific simulation node |
| 100 | * |
| 101 | * @param nodes Nodes on which to install tracer |
| 102 | * @param file File to which traces will be written. If filename is -, then std::out is used |
| 103 | * @param averagingPeriod How often data will be written into the trace file (default, every half second) |
| 104 | * |
| 105 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved |
| 106 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 107 | * |
| 108 | */ |
| 109 | static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > > |
| 110 | Install (Ptr<Node> node, const std::string &file, Time averagingPeriod = Seconds (0.5)); |
| 111 | |
| 112 | /** |
| 113 | * @brief Helper method to install tracers on a specific simulation node |
| 114 | * |
| 115 | * @param nodes Nodes on which to install tracer |
| 116 | * @param outputStream Smart pointer to a stream |
| 117 | * @param averagingPeriod How often data will be written into the trace file (default, every half second) |
| 118 | * |
| 119 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved |
| 120 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 121 | */ |
| 122 | static Ptr<CsTracer> |
| 123 | Install (Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream, Time averagingPeriod = Seconds (0.5)); |
| 124 | |
| 125 | /** |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 126 | * @brief Trace constructor that attaches to the node using node pointer |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 127 | * @param os reference to the output stream |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 128 | * @param node pointer to the node |
| 129 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 130 | CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Trace constructor that attaches to the node using node name |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 134 | * @param os reference to the output stream |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 135 | * @param nodeName name of the node registered using Names::Add |
| 136 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 137 | CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * @brief Destructor |
| 141 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 142 | ~CsTracer (); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * @brief Print head of the trace (e.g., for post-processing) |
| 146 | * |
| 147 | * @param os reference to output stream |
| 148 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 149 | void |
| 150 | PrintHeader (std::ostream &os) const; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * @brief Print current trace data |
| 154 | * |
| 155 | * @param os reference to output stream |
| 156 | */ |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 157 | void |
| 158 | Print (std::ostream &os) const; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 160 | private: |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 161 | void |
| 162 | Connect (); |
| 163 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 164 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 165 | CacheHits (Ptr<const Interest>, Ptr<const ContentObject>); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 167 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 168 | CacheMisses (Ptr<const Interest>); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 170 | private: |
| 171 | void |
| 172 | SetAveragingPeriod (const Time &period); |
| 173 | |
| 174 | void |
| 175 | Reset (); |
| 176 | |
| 177 | void |
| 178 | PeriodicPrinter (); |
| 179 | |
| 180 | private: |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 181 | std::string m_node; |
| 182 | Ptr<Node> m_nodePtr; |
| 183 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 184 | boost::shared_ptr<std::ostream> m_os; |
| 185 | |
| 186 | Time m_period; |
| 187 | EventId m_printEvent; |
| 188 | cs::Stats m_stats; |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | /** |
| 192 | * @brief Helper to dump the trace to an output stream |
| 193 | */ |
| 194 | inline std::ostream& |
| 195 | operator << (std::ostream &os, const CsTracer &tracer) |
| 196 | { |
| 197 | os << "# "; |
| 198 | tracer.PrintHeader (os); |
| 199 | os << "\n"; |
| 200 | tracer.Print (os); |
| 201 | return os; |
| 202 | } |
| 203 | |
| 204 | } // namespace ndn |
| 205 | } // namespace ns3 |
| 206 | |
| 207 | #endif // CCNX_CS_TRACER_H |