blob: 048fdaabf2996faa9e895c994a3daa8996713468 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_COUNTERS_H_
29#define V8_COUNTERS_H_
30
Ben Murdochb0fe1622011-05-05 13:52:32 +010031#include "../include/v8.h"
32#include "allocation.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
37// StatsCounters is an interface for plugging into external
38// counters for monitoring. Counters can be looked up and
39// manipulated by name.
40
41class StatsTable : public AllStatic {
42 public:
43 // Register an application-defined function where
44 // counters can be looked up.
45 static void SetCounterFunction(CounterLookupCallback f) {
46 lookup_function_ = f;
47 }
48
49 // Register an application-defined function to create
50 // a histogram for passing to the AddHistogramSample function
51 static void SetCreateHistogramFunction(CreateHistogramCallback f) {
52 create_histogram_function_ = f;
53 }
54
55 // Register an application-defined function to add a sample
56 // to a histogram created with CreateHistogram function
57 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
58 add_histogram_sample_function_ = f;
59 }
60
61 static bool HasCounterFunction() {
62 return lookup_function_ != NULL;
63 }
64
65 // Lookup the location of a counter by name. If the lookup
66 // is successful, returns a non-NULL pointer for writing the
67 // value of the counter. Each thread calling this function
68 // may receive a different location to store it's counter.
69 // The return value must not be cached and re-used across
70 // threads, although a single thread is free to cache it.
Steve Block6ded16b2010-05-10 14:33:55 +010071 static int* FindLocation(const char* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +000072 if (!lookup_function_) return NULL;
73 return lookup_function_(name);
74 }
75
76 // Create a histogram by name. If the create is successful,
77 // returns a non-NULL pointer for use with AddHistogramSample
78 // function. min and max define the expected minimum and maximum
79 // sample values. buckets is the maximum number of buckets
80 // that the samples will be grouped into.
81 static void* CreateHistogram(const char* name,
82 int min,
83 int max,
84 size_t buckets) {
85 if (!create_histogram_function_) return NULL;
86 return create_histogram_function_(name, min, max, buckets);
87 }
88
89 // Add a sample to a histogram created with the CreateHistogram
90 // function.
91 static void AddHistogramSample(void* histogram, int sample) {
92 if (!add_histogram_sample_function_) return;
93 return add_histogram_sample_function_(histogram, sample);
94 }
95
96 private:
97 static CounterLookupCallback lookup_function_;
98 static CreateHistogramCallback create_histogram_function_;
99 static AddHistogramSampleCallback add_histogram_sample_function_;
100};
101
102// StatsCounters are dynamically created values which can be tracked in
103// the StatsTable. They are designed to be lightweight to create and
104// easy to use.
105//
106// Internally, a counter represents a value in a row of a StatsTable.
107// The row has a 32bit value for each process/thread in the table and also
108// a name (stored in the table metadata). Since the storage location can be
109// thread-specific, this class cannot be shared across threads.
110//
111// This class is designed to be POD initialized. It will be registered with
112// the counter system on first use. For example:
113// StatsCounter c = { "c:myctr", NULL, false };
114struct StatsCounter {
115 const char* name_;
116 int* ptr_;
117 bool lookup_done_;
118
119 // Sets the counter to a specific value.
120 void Set(int value) {
121 int* loc = GetPtr();
122 if (loc) *loc = value;
123 }
124
125 // Increments the counter.
126 void Increment() {
127 int* loc = GetPtr();
128 if (loc) (*loc)++;
129 }
130
131 void Increment(int value) {
132 int* loc = GetPtr();
133 if (loc)
134 (*loc) += value;
135 }
136
137 // Decrements the counter.
138 void Decrement() {
139 int* loc = GetPtr();
140 if (loc) (*loc)--;
141 }
142
143 void Decrement(int value) {
144 int* loc = GetPtr();
145 if (loc) (*loc) -= value;
146 }
147
148 // Is this counter enabled?
149 // Returns false if table is full.
150 bool Enabled() {
151 return GetPtr() != NULL;
152 }
153
154 // Get the internal pointer to the counter. This is used
155 // by the code generator to emit code that manipulates a
156 // given counter without calling the runtime system.
157 int* GetInternalPointer() {
158 int* loc = GetPtr();
159 ASSERT(loc != NULL);
160 return loc;
161 }
162
163 protected:
164 // Returns the cached address of this counter location.
165 int* GetPtr() {
166 if (lookup_done_)
167 return ptr_;
168 lookup_done_ = true;
169 ptr_ = StatsTable::FindLocation(name_);
170 return ptr_;
171 }
172};
173
174// StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 };
175struct StatsCounterTimer {
176 StatsCounter counter_;
177
178 int64_t start_time_;
179 int64_t stop_time_;
180
181 // Start the timer.
182 void Start();
183
184 // Stop the timer and record the results.
185 void Stop();
186
187 // Returns true if the timer is running.
188 bool Running() {
189 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0;
190 }
191};
192
193// A HistogramTimer allows distributions of results to be created
194// HistogramTimer t = { L"foo", NULL, false, 0, 0 };
195struct HistogramTimer {
196 const char* name_;
197 void* histogram_;
198 bool lookup_done_;
199
200 int64_t start_time_;
201 int64_t stop_time_;
202
203 // Start the timer.
204 void Start();
205
206 // Stop the timer and record the results.
207 void Stop();
208
209 // Returns true if the timer is running.
210 bool Running() {
211 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0);
212 }
213
214 protected:
215 // Returns the handle to the histogram.
216 void* GetHistogram() {
217 if (!lookup_done_) {
218 lookup_done_ = true;
219 histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50);
220 }
221 return histogram_;
222 }
223};
224
225// Helper class for scoping a HistogramTimer.
226class HistogramTimerScope BASE_EMBEDDED {
227 public:
228 explicit HistogramTimerScope(HistogramTimer* timer) :
229 timer_(timer) {
230 timer_->Start();
231 }
232 ~HistogramTimerScope() {
233 timer_->Stop();
234 }
235 private:
236 HistogramTimer* timer_;
237};
238
239
240} } // namespace v8::internal
241
242#endif // V8_COUNTERS_H_