blob: d14d005c963377a6a102e9d65ba5793c20039f08 [file] [log] [blame]
Alexander Afanasyev86287992012-12-10 16:11:50 -08001/* -*- 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
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070025#include "ns3/ndnSIM/model/ndn-common.hpp"
26
Alexander Afanasyev86287992012-12-10 16:11:50 -080027#include "ns3/ptr.h"
28#include "ns3/simple-ref-count.h"
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080029#include <ns3/nstime.h>
30#include <ns3/event-id.h>
Alexander Afanasyev5352af32013-07-15 09:51:28 -070031#include <ns3/node-container.h>
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080032
33#include <boost/tuple/tuple.hpp>
34#include <boost/shared_ptr.hpp>
35#include <map>
36#include <list>
Alexander Afanasyev86287992012-12-10 16:11:50 -080037
38namespace ns3 {
39
40class Node;
41class Packet;
42
43namespace ndn {
44
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080045namespace cs {
46
Alexander Afanasyev79206512013-07-27 16:49:12 -070047/// @cond include_hidden
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048struct Stats {
49 inline void
50 Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080051 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 m_cacheHits = 0;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080053 m_cacheMisses = 0;
54 }
55 double m_cacheHits;
56 double m_cacheMisses;
57};
Alexander Afanasyev79206512013-07-27 16:49:12 -070058/// @endcond
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080060
Alexander Afanasyev86287992012-12-10 16:11:50 -080061/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070062 * @ingroup ndn-tracers
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080063 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080064 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065class CsTracer : public SimpleRefCount<CsTracer> {
Alexander Afanasyev86287992012-12-10 16:11:50 -080066public:
67 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080068 * @brief Helper method to install tracers on all simulation nodes
69 *
Alexander Afanasyev5352af32013-07-15 09:51:28 -070070 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 * @param averagingPeriod How often data will be written into the trace file (default, every half
72 *second)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080073 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
75 *tuple needs to be preserved
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080076 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 *
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080078 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070079 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 InstallAll(const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080081
82 /**
Alexander Afanasyev5352af32013-07-15 09:51:28 -070083 * @brief Helper method to install tracers on the selected simulation nodes
84 *
85 * @param nodes Nodes on which to install tracer
86 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 * @param averagingPeriod How often data will be written into the trace file (default, every half
88 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070089 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
91 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -070092 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
93 *
94 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070095 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 Install(const NodeContainer& nodes, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -070097
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
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 * @param averagingPeriod How often data will be written into the trace file (default, every half
104 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700105 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
107 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700108 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
109 *
110 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700111 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 Install(Ptr<Node> node, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700113
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
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 * @param averagingPeriod How often data will be written into the trace file (default, every half
120 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700121 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
123 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700124 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
125 */
126 static Ptr<CsTracer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 Install(Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream,
128 Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -0700129
130 /**
131 * @brief Explicit request to remove all statically created tracers
132 *
133 * This method can be helpful if simulation scenario contains several independent run,
134 * or if it is desired to do a postprocessing of the resulting data
135 */
136 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 Destroy();
138
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700139 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800140 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800141 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800142 * @param node pointer to the node
143 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 CsTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800145
146 /**
147 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800148 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800149 * @param nodeName name of the node registered using Names::Add
150 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 CsTracer(boost::shared_ptr<std::ostream> os, const std::string& node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800152
153 /**
154 * @brief Destructor
155 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156 ~CsTracer();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800157
158 /**
159 * @brief Print head of the trace (e.g., for post-processing)
160 *
161 * @param os reference to output stream
162 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800163 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164 PrintHeader(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800165
166 /**
167 * @brief Print current trace data
168 *
169 * @param os reference to output stream
170 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800171 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800172 Print(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800173
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800174private:
175 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 Connect();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800177
178 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700179 CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800180
181 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700182 CacheMisses(shared_ptr<const Interest>);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183
184private:
185 void
186 SetAveragingPeriod(const Time& period);
187
188 void
189 Reset();
190
191 void
192 PeriodicPrinter();
193
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800194private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800195 std::string m_node;
196 Ptr<Node> m_nodePtr;
197
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800198 boost::shared_ptr<std::ostream> m_os;
199
200 Time m_period;
201 EventId m_printEvent;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800202 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800203};
204
205/**
206 * @brief Helper to dump the trace to an output stream
207 */
208inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209operator<<(std::ostream& os, const CsTracer& tracer)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800210{
211 os << "# ";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800212 tracer.PrintHeader(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800213 os << "\n";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800214 tracer.Print(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800215 return os;
216}
217
218} // namespace ndn
219} // namespace ns3
220
221#endif // CCNX_CS_TRACER_H