blob: 939df9bc705ba79f796a292f9ce5e422013b5cea [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
25#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
31#include <boost/tuple/tuple.hpp>
32#include <boost/shared_ptr.hpp>
33#include <map>
34#include <list>
Alexander Afanasyev86287992012-12-10 16:11:50 -080035
36namespace ns3 {
37
38class Node;
39class Packet;
40
41namespace ndn {
42
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070043class Interest;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070044class Data;
Alexander Afanasyev86287992012-12-10 16:11:50 -080045
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070046typedef Interest InterestHeader;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070047typedef Data DataHeader;
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070048
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080049namespace cs {
50
Alexander Afanasyev79206512013-07-27 16:49:12 -070051/// @cond include_hidden
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052struct Stats {
53 inline void
54 Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080055 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 m_cacheHits = 0;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080057 m_cacheMisses = 0;
58 }
59 double m_cacheHits;
60 double m_cacheMisses;
61};
Alexander Afanasyev79206512013-07-27 16:49:12 -070062/// @endcond
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080064
Alexander Afanasyev86287992012-12-10 16:11:50 -080065/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070066 * @ingroup ndn-tracers
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080067 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080068 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069class CsTracer : public SimpleRefCount<CsTracer> {
Alexander Afanasyev86287992012-12-10 16:11:50 -080070public:
71 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080072 * @brief Helper method to install tracers on all simulation nodes
73 *
Alexander Afanasyev5352af32013-07-15 09:51:28 -070074 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 * @param averagingPeriod How often data will be written into the trace file (default, every half
76 *second)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080077 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
79 *tuple needs to be preserved
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080080 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 *
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080082 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070083 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 InstallAll(const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080085
86 /**
Alexander Afanasyev5352af32013-07-15 09:51:28 -070087 * @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 Afanasyevbe55cf62014-12-20 17:51:09 -080091 * @param averagingPeriod How often data will be written into the trace file (default, every half
92 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070093 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
95 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -070096 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
97 *
98 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070099 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 Install(const NodeContainer& nodes, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700101
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800107 * @param averagingPeriod How often data will be written into the trace file (default, every half
108 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700109 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
111 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700112 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
113 *
114 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700115 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 Install(Ptr<Node> node, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700117
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800123 * @param averagingPeriod How often data will be written into the trace file (default, every half
124 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700125 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
127 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700128 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
129 */
130 static Ptr<CsTracer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 Install(Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream,
132 Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -0700133
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 Afanasyevbe55cf62014-12-20 17:51:09 -0800141 Destroy();
142
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700143 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800144 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800145 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800146 * @param node pointer to the node
147 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 CsTracer(boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800149
150 /**
151 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800152 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800153 * @param nodeName name of the node registered using Names::Add
154 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800155 CsTracer(boost::shared_ptr<std::ostream> os, const std::string& node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800156
157 /**
158 * @brief Destructor
159 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 ~CsTracer();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800161
162 /**
163 * @brief Print head of the trace (e.g., for post-processing)
164 *
165 * @param os reference to output stream
166 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800167 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 PrintHeader(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800169
170 /**
171 * @brief Print current trace data
172 *
173 * @param os reference to output stream
174 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800175 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 Print(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800177
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800178private:
179 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180 Connect();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800181
182 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183 CacheHits(Ptr<const Interest>, Ptr<const Data>);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800184
185 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186 CacheMisses(Ptr<const Interest>);
187
188private:
189 void
190 SetAveragingPeriod(const Time& period);
191
192 void
193 Reset();
194
195 void
196 PeriodicPrinter();
197
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800198private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800199 std::string m_node;
200 Ptr<Node> m_nodePtr;
201
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800202 boost::shared_ptr<std::ostream> m_os;
203
204 Time m_period;
205 EventId m_printEvent;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800207};
208
209/**
210 * @brief Helper to dump the trace to an output stream
211 */
212inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213operator<<(std::ostream& os, const CsTracer& tracer)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800214{
215 os << "# ";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800216 tracer.PrintHeader(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800217 os << "\n";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800218 tracer.Print(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800219 return os;
220}
221
222} // namespace ndn
223} // namespace ns3
224
225#endif // CCNX_CS_TRACER_H