blob: 87c290ee514070cc32acc4b6722fbf45d9a5daf2 [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
51struct Stats
52{
53 inline void Reset ()
54 {
55 m_cacheHits = 0;
56 m_cacheMisses = 0;
57 }
58 double m_cacheHits;
59 double m_cacheMisses;
60};
61
62}
63
Alexander Afanasyev86287992012-12-10 16:11:50 -080064/**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080065 * @ingroup ndn
66 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080067 */
68class CsTracer : public SimpleRefCount<CsTracer>
69{
70public:
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 Afanasyevb1d6b032013-01-18 14:11:11 -080075 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
76 *
77 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
78 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
79 *
80 */
81 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
82 InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
83
84 /**
Alexander Afanasyev5352af32013-07-15 09:51:28 -070085 * @brief Helper method to install tracers on the selected simulation nodes
86 *
87 * @param nodes Nodes on which to install tracer
88 * @param file File to which traces will be written. If filename is -, then std::out is used
89 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
90 *
91 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
92 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
93 *
94 */
95 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
96 Install (const NodeContainer &nodes, const std::string &file, Time averagingPeriod = Seconds (0.5));
97
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
103 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
104 *
105 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
106 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
107 *
108 */
109 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
110 Install (Ptr<Node> node, const std::string &file, Time averagingPeriod = Seconds (0.5));
111
112 /**
113 * @brief Helper method to install tracers on a specific simulation node
114 *
115 * @param nodes Nodes on which to install tracer
116 * @param outputStream Smart pointer to a stream
117 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
118 *
119 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
120 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
121 */
122 static Ptr<CsTracer>
123 Install (Ptr<Node> node, boost::shared_ptr<std::ostream> outputStream, Time averagingPeriod = Seconds (0.5));
124
125 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -0800126 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800127 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800128 * @param node pointer to the node
129 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800130 CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800131
132 /**
133 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800134 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -0800135 * @param nodeName name of the node registered using Names::Add
136 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800137 CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800138
139 /**
140 * @brief Destructor
141 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800142 ~CsTracer ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800143
144 /**
145 * @brief Print head of the trace (e.g., for post-processing)
146 *
147 * @param os reference to output stream
148 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800149 void
150 PrintHeader (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800151
152 /**
153 * @brief Print current trace data
154 *
155 * @param os reference to output stream
156 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800157 void
158 Print (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800159
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800160private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800161 void
162 Connect ();
163
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800164 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700165 CacheHits (Ptr<const Interest>, Ptr<const ContentObject>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800166
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800167 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700168 CacheMisses (Ptr<const Interest>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800169
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800170private:
171 void
172 SetAveragingPeriod (const Time &period);
173
174 void
175 Reset ();
176
177 void
178 PeriodicPrinter ();
179
180private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800181 std::string m_node;
182 Ptr<Node> m_nodePtr;
183
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800184 boost::shared_ptr<std::ostream> m_os;
185
186 Time m_period;
187 EventId m_printEvent;
188 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800189};
190
191/**
192 * @brief Helper to dump the trace to an output stream
193 */
194inline std::ostream&
195operator << (std::ostream &os, const CsTracer &tracer)
196{
197 os << "# ";
198 tracer.PrintHeader (os);
199 os << "\n";
200 tracer.Print (os);
201 return os;
202}
203
204} // namespace ndn
205} // namespace ns3
206
207#endif // CCNX_CS_TRACER_H