blob: c898b16a0d225390d89d2bb3788d564e52a81a70 [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
Spyridon Mastorakisda904f22014-11-05 16:23:33 -080033#include <tuple>
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080034#include <map>
35#include <list>
Alexander Afanasyev86287992012-12-10 16:11:50 -080036
37namespace ns3 {
38
39class Node;
40class Packet;
41
42namespace ndn {
43
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080044namespace cs {
45
Alexander Afanasyev79206512013-07-27 16:49:12 -070046/// @cond include_hidden
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047struct Stats {
48 inline void
49 Reset()
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080050 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 m_cacheHits = 0;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080052 m_cacheMisses = 0;
53 }
54 double m_cacheHits;
55 double m_cacheMisses;
56};
Alexander Afanasyev79206512013-07-27 16:49:12 -070057/// @endcond
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058}
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080059
Alexander Afanasyev86287992012-12-10 16:11:50 -080060/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070061 * @ingroup ndn-tracers
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080062 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080063 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064class CsTracer : public SimpleRefCount<CsTracer> {
Alexander Afanasyev86287992012-12-10 16:11:50 -080065public:
66 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080067 * @brief Helper method to install tracers on all simulation nodes
68 *
Alexander Afanasyev5352af32013-07-15 09:51:28 -070069 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 * @param averagingPeriod How often data will be written into the trace file (default, every half
71 *second)
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080072 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
74 *tuple needs to be preserved
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080075 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 *
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080077 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070078 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 InstallAll(const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080080
81 /**
Alexander Afanasyev5352af32013-07-15 09:51:28 -070082 * @brief Helper method to install tracers on the selected simulation nodes
83 *
84 * @param nodes Nodes on which to install tracer
85 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 * @param averagingPeriod How often data will be written into the trace file (default, every half
87 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -070088 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
90 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -070091 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
92 *
93 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070094 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 Install(const NodeContainer& nodes, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -070096
97 /**
98 * @brief Helper method to install tracers on a specific simulation node
99 *
100 * @param nodes Nodes on which to install tracer
101 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 * @param averagingPeriod How often data will be written into the trace file (default, every half
103 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700104 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
106 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700107 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
108 *
109 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700110 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 Install(Ptr<Node> node, const std::string& file, Time averagingPeriod = Seconds(0.5));
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700112
113 /**
114 * @brief Helper method to install tracers on a specific simulation node
115 *
116 * @param nodes Nodes on which to install tracer
117 * @param outputStream Smart pointer to a stream
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 * @param averagingPeriod How often data will be written into the trace file (default, every half
119 *second)
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700120 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This
122 *tuple needs to be preserved
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700123 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
124 */
125 static Ptr<CsTracer>
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800126 Install(Ptr<Node> node, shared_ptr<std::ostream> outputStream,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 Time averagingPeriod = Seconds(0.5));
Alexander Afanasyevdb5f3b62013-08-09 17:42:12 -0700128
129 /**
130 * @brief Explicit request to remove all statically created tracers
131 *
132 * This method can be helpful if simulation scenario contains several independent run,
133 * or if it is desired to do a postprocessing of the resulting data
134 */
135 static void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 Destroy();
137
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700138 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800139 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800140 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800141 * @param node pointer to the node
142 */
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800143 CsTracer(shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800144
145 /**
146 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800147 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800148 * @param nodeName name of the node registered using Names::Add
149 */
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800150 CsTracer(shared_ptr<std::ostream> os, const std::string& node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800151
152 /**
153 * @brief Destructor
154 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800155 ~CsTracer();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800156
157 /**
158 * @brief Print head of the trace (e.g., for post-processing)
159 *
160 * @param os reference to output stream
161 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800162 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 PrintHeader(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800164
165 /**
166 * @brief Print current trace data
167 *
168 * @param os reference to output stream
169 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800170 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171 Print(std::ostream& os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800172
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800173private:
174 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 Connect();
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800176
177 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700178 CacheHits(shared_ptr<const Interest>, shared_ptr<const Data>);
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800179
180 void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700181 CacheMisses(shared_ptr<const Interest>);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800182
183private:
184 void
185 SetAveragingPeriod(const Time& period);
186
187 void
188 Reset();
189
190 void
191 PeriodicPrinter();
192
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800193private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800194 std::string m_node;
195 Ptr<Node> m_nodePtr;
196
Spyridon Mastorakisda904f22014-11-05 16:23:33 -0800197 shared_ptr<std::ostream> m_os;
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800198
199 Time m_period;
200 EventId m_printEvent;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800202};
203
204/**
205 * @brief Helper to dump the trace to an output stream
206 */
207inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800208operator<<(std::ostream& os, const CsTracer& tracer)
Alexander Afanasyev86287992012-12-10 16:11:50 -0800209{
210 os << "# ";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211 tracer.PrintHeader(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800212 os << "\n";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 tracer.Print(os);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800214 return os;
215}
216
217} // namespace ndn
218} // namespace ns3
219
220#endif // CCNX_CS_TRACER_H