blob: f4da1a7706a0a0e842a0dedb3d380337ad67bf10 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev86287992012-12-10 16:11:50 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev86287992012-12-10 16:11:50 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Afanasyev86287992012-12-10 16:11:50 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Afanasyev86287992012-12-10 16:11:50 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Afanasyev86287992012-12-10 16:11:50 -080019
20#ifndef CCNX_CS_TRACER_H
21#define CCNX_CS_TRACER_H
22
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Alexander Afanasyev86287992012-12-10 16:11:50 -080025#include "ns3/ptr.h"
26#include "ns3/simple-ref-count.h"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080027#include <ns3/nstime.h>
28#include <ns3/event-id.h>
Alexander Afanasyev5352af32013-07-15 09:51:28 -070029#include <ns3/node-container.h>
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080030
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080031#include <tuple>
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080032#include <map>
33#include <list>
Alexander Afanasyev86287992012-12-10 16:11:50 -080034
35namespace ns3 {
36
37class Node;
38class Packet;
39
40namespace ndn {
41
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080042namespace cs {
43
Alexander Afanasyev79206512013-07-27 16:49:12 -070044/// @cond include_hidden
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045struct Stats {
46 inline void
47 Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080048 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 m_cacheHits = 0;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080050 m_cacheMisses = 0;
51 }
52 double m_cacheHits;
53 double m_cacheMisses;
54};
Alexander Afanasyev79206512013-07-27 16:49:12 -070055/// @endcond
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080057
Alexander Afanasyev86287992012-12-10 16:11:50 -080058/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070059 * @ingroup ndn-tracers
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080060 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080061 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062class CsTracer : public SimpleRefCount<CsTracer> {
Alexander Afanasyev86287992012-12-10 16:11:50 -080063public:
64 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080065 * @brief Helper method to install tracers on all simulation nodes
66 *
Alexander Afanasyev5352af32013-07-15 09:51:28 -070067 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 * @param averagingPeriod How often data will be written into the trace file (default, every half
69 *second)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080070 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
72 *tuple needs to be preserved
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080073 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 *
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080075 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070076 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 InstallAll(const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080078
79 /**
Alexander Afanasyev5352af32013-07-15 09:51:28 -070080 * @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 Afanasyevbe55cf62014-12-20 17:51:09 -080084 * @param averagingPeriod How often data will be written into the trace file (default, every half
85 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070086 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
88 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -070089 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
90 *
91 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070092 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 Install(const NodeContainer& nodes, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -070094
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800100 * @param averagingPeriod How often data will be written into the trace file (default, every half
101 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700102 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
104 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700105 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
106 *
107 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700108 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 Install(Ptr<Node> node, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700110
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800116 * @param averagingPeriod How often data will be written into the trace file (default, every half
117 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700118 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
120 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700121 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
122 */
123 static Ptr<CsTracer>
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800124 Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -0700126
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800134 Destroy();
135
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700136 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800137 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800138 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800139 * @param node pointer to the node
140 */
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800141 CsTracer(shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800142
143 /**
144 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800145 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800146 * @param nodeName name of the node registered using Names::Add
147 */
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800148 CsTracer(shared_ptr<std::ostream> os, const std::string& node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800149
150 /**
151 * @brief Destructor
152 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 ~CsTracer();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800154
155 /**
156 * @brief Print head of the trace (e.g., for post-processing)
157 *
158 * @param os reference to output stream
159 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800160 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 PrintHeader(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800162
163 /**
164 * @brief Print current trace data
165 *
166 * @param os reference to output stream
167 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800168 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 Print(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800170
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800171private:
172 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800173 Connect();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800174
175 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700176 CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800177
178 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700179 CacheMisses(shared_ptr<const Interest>);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180
181private:
182 void
183 SetAveragingPeriod(const Time& period);
184
185 void
186 Reset();
187
188 void
189 PeriodicPrinter();
190
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800191private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800192 std::string m_node;
193 Ptr<Node> m_nodePtr;
194
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800195 shared_ptr<std::ostream> m_os;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800196
197 Time m_period;
198 EventId m_printEvent;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800200};
201
202/**
203 * @brief Helper to dump the trace to an output stream
204 */
205inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206operator<<(std::ostream& os, const CsTracer& tracer)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800207{
208 os << "# ";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209 tracer.PrintHeader(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800210 os << "\n";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211 tracer.Print(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800212 return os;
213}
214
215} // namespace ndn
216} // namespace ns3
217
218#endif // CCNX_CS_TRACER_H