blob: b52de9c30c48b5eb7c87bf107b985fccbe3fca48 [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
42class InterestHeader;
43class ContentObjectHeader;
44
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080045namespace cs {
46
47struct Stats
48{
49 inline void Reset ()
50 {
51 m_cacheHits = 0;
52 m_cacheMisses = 0;
53 }
54 double m_cacheHits;
55 double m_cacheMisses;
56};
57
58}
59
Alexander Afanasyev86287992012-12-10 16:11:50 -080060/**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080061 * @ingroup ndn
62 * @brief NDN tracer for cache performance (hits and misses)
Alexander Afanasyev86287992012-12-10 16:11:50 -080063 */
64class CsTracer : public SimpleRefCount<CsTracer>
65{
66public:
67 /**
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080068 * @brief Helper method to install tracers on all simulation nodes
69 *
70 * @param file File to which traces will be written
71 * @param averagingPeriod How often data will be written into the trace file (default, every half second)
72 *
73 * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
74 * for the lifetime of simulation, otherwise SEGFAULTs are inevitable
75 *
76 */
77 static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsTracer> > >
78 InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
79
80 /**
Alexander Afanasyev86287992012-12-10 16:11:50 -080081 * @brief Trace constructor that attaches to the node using node pointer
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080082 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -080083 * @param node pointer to the node
84 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080085 CsTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
Alexander Afanasyev86287992012-12-10 16:11:50 -080086
87 /**
88 * @brief Trace constructor that attaches to the node using node name
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080089 * @param os reference to the output stream
Alexander Afanasyev86287992012-12-10 16:11:50 -080090 * @param nodeName name of the node registered using Names::Add
91 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080092 CsTracer (boost::shared_ptr<std::ostream> os, const std::string &node);
Alexander Afanasyev86287992012-12-10 16:11:50 -080093
94 /**
95 * @brief Destructor
96 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -080097 ~CsTracer ();
Alexander Afanasyev86287992012-12-10 16:11:50 -080098
99 /**
100 * @brief Print head of the trace (e.g., for post-processing)
101 *
102 * @param os reference to output stream
103 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800104 void
105 PrintHeader (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800106
107 /**
108 * @brief Print current trace data
109 *
110 * @param os reference to output stream
111 */
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800112 void
113 Print (std::ostream &os) const;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800114
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800115private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800116 void
117 Connect ();
118
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800119 void
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800120 CacheHits (Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800121
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800122 void
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800123 CacheMisses (Ptr<const InterestHeader>);
Alexander Afanasyev86287992012-12-10 16:11:50 -0800124
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800125private:
126 void
127 SetAveragingPeriod (const Time &period);
128
129 void
130 Reset ();
131
132 void
133 PeriodicPrinter ();
134
135private:
Alexander Afanasyev86287992012-12-10 16:11:50 -0800136 std::string m_node;
137 Ptr<Node> m_nodePtr;
138
Alexander Afanasyevb1d6b032013-01-18 14:11:11 -0800139 boost::shared_ptr<std::ostream> m_os;
140
141 Time m_period;
142 EventId m_printEvent;
143 cs::Stats m_stats;
Alexander Afanasyev86287992012-12-10 16:11:50 -0800144};
145
146/**
147 * @brief Helper to dump the trace to an output stream
148 */
149inline std::ostream&
150operator << (std::ostream &os, const CsTracer &tracer)
151{
152 os << "# ";
153 tracer.PrintHeader (os);
154 os << "\n";
155 tracer.Print (os);
156 return os;
157}
158
159} // namespace ndn
160} // namespace ns3
161
162#endif // CCNX_CS_TRACER_H