blob: eb075851ae10b5d55cae089e4f72357d68d148ce [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 Afanasyevb1d6b032013-01-18 14:11:11 -080052struct Stats
53{
54 inline void Reset ()
55 {
56 m_cacheHits = 0;
57 m_cacheMisses = 0;
58 }
59 double m_cacheHits;
60 double m_cacheMisses;
61};
Alexander Afanasyev79206512013-07-27 16:49:12 -070062/// @endcond
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080063
64}
65
Alexander Afanasyev86287992012-12-10 16:11:50 -080066/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070067 * @ingroup ndn-tracers
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080068 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080069 */
70class CsTracer : public SimpleRefCount<CsTracer>
71{
72public:
73 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080074 * @brief Helper method to install tracers on all simulation nodes
75 *
Alexander Afanasyev5352af32013-07-15 09:51:28 -070076 * @param file File to which traces will be written. If filename is -, then std::out is used
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080077 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
78 *
79 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
80 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
81 *
82 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070083 static void
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080084 InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
85
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
91 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
92 *
93 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
94 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
95 *
96 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -070097 static void
Alexander Afanasyev5352af32013-07-15 09:51:28 -070098 Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod = Seconds (0.5));
99
100 /**
101 * @brief Helper method to install tracers on a specific simulation node
102 *
103 * @param nodes Nodes on which to install tracer
104 * @param file File to which traces will be written. If filename is -, then std::out is used
105 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
106 *
107 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
108 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
109 *
110 */
Alexander Afanasyev3fe94dc2013-08-09 17:12:12 -0700111 static void
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700112 Install (Ptr<Node> node, const std::string &file, Time averagingPeriod = Seconds (0.5));
113
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
119 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
120 *
121 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
122 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
123 */
124 static Ptr<CsTracer>
125 Install (Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream, 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
134 Destroy ();
Alexander Afanasyev5352af32013-07-15 09:51:28 -0700135
136 /**
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 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800141 CsTracer (boost::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 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800148 CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800149
150 /**
151 * @brief Destructor
152 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -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
161 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
169 Print (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800170
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800171private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800172 void
173 Connect ();
174
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800175 void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700176 CacheHits (Ptr<const Interest>, Ptr<const Data>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800177
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800178 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700179 CacheMisses (Ptr<const Interest>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800180
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800181private:
182 void
183 SetAveragingPeriod (const Time &period);
184
185 void
186 Reset ();
187
188 void
189 PeriodicPrinter ();
190
191private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800192 std::string m_node;
193 Ptr<Node> m_nodePtr;
194
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800195 boost::shared_ptr<std::ostream> m_os;
196
197 Time m_period;
198 EventId m_printEvent;
199 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&
206operator << (std::ostream &os, const CsTracer &tracer)
207{
208 os << "# ";
209 tracer.PrintHeader (os);
210 os << "\n";
211 tracer.Print (os);
212 return os;
213}
214
215} // namespace ndn
216} // namespace ns3
217
218#endif // CCNX_CS_TRACER_H