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