Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 19 | |
| 20 | #ifndef CCNX_CS_TRACER_H |
| 21 | #define CCNX_CS_TRACER_H |
| 22 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 23 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
| 24 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 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 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 31 | #include <tuple> |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 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 | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 42 | namespace cs { |
| 43 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 44 | /// @cond include_hidden |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | struct Stats { |
| 46 | inline void |
| 47 | Reset() |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 49 | m_cacheHits = 0; |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 50 | m_cacheMisses = 0; |
| 51 | } |
| 52 | double m_cacheHits; |
| 53 | double m_cacheMisses; |
| 54 | }; |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 55 | /// @endcond |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | } |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 58 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 59 | * @ingroup ndn-tracers |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 60 | * @brief NDN tracer for cache performance (hits and misses) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 61 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 62 | class CsTracer : public SimpleRefCount<CsTracer> { |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 63 | public: |
| 64 | /** |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 65 | * @brief Helper method to install tracers on all simulation nodes |
| 66 | * |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 67 | * @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] | 68 | * @param averagingPeriod How often data will be written into the trace file (default, every half |
| 69 | *second) |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 70 | * |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This |
| 72 | *tuple needs to be preserved |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 73 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | * |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 75 | */ |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 76 | static void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 77 | InstallAll(const std::string& file, Time averagingPeriod = Seconds(0.5)); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 80 | * @brief Helper method to install tracers on the selected simulation nodes |
| 81 | * |
| 82 | * @param nodes Nodes on which to install tracer |
| 83 | * @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] | 84 | * @param averagingPeriod How often data will be written into the trace file (default, every half |
| 85 | *second) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 86 | * |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This |
| 88 | *tuple needs to be preserved |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 89 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 90 | * |
| 91 | */ |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 92 | static void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | 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] | 94 | |
| 95 | /** |
| 96 | * @brief Helper method to install tracers on a specific simulation node |
| 97 | * |
| 98 | * @param nodes Nodes on which to install tracer |
| 99 | * @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] | 100 | * @param averagingPeriod How often data will be written into the trace file (default, every half |
| 101 | *second) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 102 | * |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This |
| 104 | *tuple needs to be preserved |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 105 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 106 | * |
| 107 | */ |
Alexander Afanasyev | 3fe94dc | 2013-08-09 17:12:12 -0700 | [diff] [blame] | 108 | static void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | 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] | 110 | |
| 111 | /** |
| 112 | * @brief Helper method to install tracers on a specific simulation node |
| 113 | * |
| 114 | * @param nodes Nodes on which to install tracer |
| 115 | * @param outputStream Smart pointer to a stream |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | * @param averagingPeriod How often data will be written into the trace file (default, every half |
| 117 | *second) |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 118 | * |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This |
| 120 | *tuple needs to be preserved |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 121 | * for the lifetime of simulation, otherwise SEGFAULTs are inevitable |
| 122 | */ |
| 123 | static Ptr<CsTracer> |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 124 | Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream, |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | 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 |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 134 | Destroy(); |
| 135 | |
Alexander Afanasyev | 5352af3 | 2013-07-15 09:51:28 -0700 | [diff] [blame] | 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 | */ |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 141 | CsTracer(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 | */ |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 148 | CsTracer(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 | be55cf6 | 2014-12-20 17:51:09 -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 |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 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: |
| 172 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 173 | Connect(); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 174 | |
| 175 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 176 | CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>); |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 177 | |
| 178 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 179 | CacheMisses(shared_ptr<const Interest>); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | void |
| 183 | SetAveragingPeriod(const Time& period); |
| 184 | |
| 185 | void |
| 186 | Reset(); |
| 187 | |
| 188 | void |
| 189 | PeriodicPrinter(); |
| 190 | |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 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 | |
Spyridon Mastorakis | da904f2 | 2014-11-05 16:23:33 -0800 | [diff] [blame] | 195 | shared_ptr<std::ostream> m_os; |
Alexander Afanasyev | b1d6b03 | 2013-01-18 14:11:11 -0800 | [diff] [blame] | 196 | |
| 197 | Time m_period; |
| 198 | EventId m_printEvent; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 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& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 206 | operator<<(std::ostream& os, const CsTracer& tracer) |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 207 | { |
| 208 | os << "# "; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 209 | tracer.PrintHeader(os); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 210 | os << "\n"; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 211 | tracer.Print(os); |
Alexander Afanasyev | 8628799 | 2012-12-10 16:11:50 -0800 | [diff] [blame] | 212 | return os; |
| 213 | } |
| 214 | |
| 215 | } // namespace ndn |
| 216 | } // namespace ns3 |
| 217 | |
| 218 | #endif // CCNX_CS_TRACER_H |