blob: 906e85a2a420af577d84d9e451b6e376a101f672 [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;
44class ContentObject;
Alexander Afanasyev86287992012-12-10 16:11:50 -080045
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070046typedef Interest InterestHeader;
47typedef ContentObject ContentObjectHeader;
48
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 */
83 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
84 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 */
97 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
98 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 */
111 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
112 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));
126
127 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800128 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800129 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800130 * @param node pointer to the node
131 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800132 CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800133
134 /**
135 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800136 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800137 * @param nodeName name of the node registered using Names::Add
138 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800139 CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800140
141 /**
142 * @brief Destructor
143 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800144 ~CsTracer ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800145
146 /**
147 * @brief Print head of the trace (e.g., for post-processing)
148 *
149 * @param os reference to output stream
150 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800151 void
152 PrintHeader (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800153
154 /**
155 * @brief Print current trace data
156 *
157 * @param os reference to output stream
158 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800159 void
160 Print (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800161
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800162private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800163 void
164 Connect ();
165
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800166 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700167 CacheHits (Ptr<const Interest>, Ptr<const ContentObject>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800168
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800169 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700170 CacheMisses (Ptr<const Interest>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800171
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800172private:
173 void
174 SetAveragingPeriod (const Time &period);
175
176 void
177 Reset ();
178
179 void
180 PeriodicPrinter ();
181
182private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800183 std::string m_node;
184 Ptr<Node> m_nodePtr;
185
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800186 boost::shared_ptr<std::ostream> m_os;
187
188 Time m_period;
189 EventId m_printEvent;
190 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800191};
192
193/**
194 * @brief Helper to dump the trace to an output stream
195 */
196inline std::ostream&
197operator << (std::ostream &os, const CsTracer &tracer)
198{
199 os << "# ";
200 tracer.PrintHeader (os);
201 os << "\n";
202 tracer.Print (os);
203 return os;
204}
205
206} // namespace ndn
207} // namespace ns3
208
209#endif // CCNX_CS_TRACER_H