blob: eb6f0b39312068c24908165a3d030f0ff2e77fb8 [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>
29
30#include <boost/tuple/tuple.hpp>
31#include <boost/shared_ptr.hpp>
32#include <map>
33#include <list>
Alexander Afanasyev86287992012-12-10 16:11:50 -080034
35namespace ns3 {
36
37class Node;
38class Packet;
39
40namespace ndn {
41
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070042class Interest;
43class ContentObject;
Alexander Afanasyev86287992012-12-10 16:11:50 -080044
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070045typedef Interest InterestHeader;
46typedef ContentObject ContentObjectHeader;
47
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080048namespace cs {
49
50struct Stats
51{
52 inline void Reset ()
53 {
54 m_cacheHits = 0;
55 m_cacheMisses = 0;
56 }
57 double m_cacheHits;
58 double m_cacheMisses;
59};
60
61}
62
Alexander Afanasyev86287992012-12-10 16:11:50 -080063/**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080064 * @ingroup ndn
65 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080066 */
67class CsTracer : public SimpleRefCount<CsTracer>
68{
69public:
70 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080071 * @brief Helper method to install tracers on all simulation nodes
72 *
73 * @param file File to which traces will be written
74 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
75 *
76 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
77 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
78 *
79 */
80 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
81 InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
82
83 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -080084 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080085 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -080086 * @param node pointer to the node
87 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080088 CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -080089
90 /**
91 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080092 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -080093 * @param nodeName name of the node registered using Names::Add
94 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080095 CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node);
Alexander Afanasyev86287992012-12-10 16:11:50 -080096
97 /**
98 * @brief Destructor
99 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800100 ~CsTracer ();
Alexander Afanasyev86287992012-12-10 16:11:50 -0800101
102 /**
103 * @brief Print head of the trace (e.g., for post-processing)
104 *
105 * @param os reference to output stream
106 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800107 void
108 PrintHeader (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800109
110 /**
111 * @brief Print current trace data
112 *
113 * @param os reference to output stream
114 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800115 void
116 Print (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800117
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800118private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800119 void
120 Connect ();
121
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800122 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700123 CacheHits (Ptr<const Interest>, Ptr<const ContentObject>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800124
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800125 void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700126 CacheMisses (Ptr<const Interest>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800127
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800128private:
129 void
130 SetAveragingPeriod (const Time &period);
131
132 void
133 Reset ();
134
135 void
136 PeriodicPrinter ();
137
138private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800139 std::string m_node;
140 Ptr<Node> m_nodePtr;
141
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800142 boost::shared_ptr<std::ostream> m_os;
143
144 Time m_period;
145 EventId m_printEvent;
146 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800147};
148
149/**
150 * @brief Helper to dump the trace to an output stream
151 */
152inline std::ostream&
153operator << (std::ostream &os, const CsTracer &tracer)
154{
155 os << "# ";
156 tracer.PrintHeader (os);
157 os << "\n";
158 tracer.Print (os);
159 return os;
160}
161
162} // namespace ndn
163} // namespace ns3
164
165#endif // CCNX_CS_TRACER_H