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