1
// Copyright 2012 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
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.
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.
28
#ifndef V8_COUNTERS_H_
29
#define V8_COUNTERS_H_
31
#include "../include/v8.h"
32
#include "allocation.h"
37
// StatsCounters is an interface for plugging into external
38
// counters for monitoring. Counters can be looked up and
39
// manipulated by name.
43
// Register an application-defined function where
44
// counters can be looked up.
45
void SetCounterFunction(CounterLookupCallback f) {
49
// Register an application-defined function to create
50
// a histogram for passing to the AddHistogramSample function
51
void SetCreateHistogramFunction(CreateHistogramCallback f) {
52
create_histogram_function_ = f;
55
// Register an application-defined function to add a sample
56
// to a histogram created with CreateHistogram function
57
void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
58
add_histogram_sample_function_ = f;
61
bool HasCounterFunction() const {
62
return lookup_function_ != NULL;
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.
71
int* FindLocation(const char* name) {
72
if (!lookup_function_) return NULL;
73
return lookup_function_(name);
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
void* CreateHistogram(const char* name,
85
if (!create_histogram_function_) return NULL;
86
return create_histogram_function_(name, min, max, buckets);
89
// Add a sample to a histogram created with the CreateHistogram
91
void AddHistogramSample(void* histogram, int sample) {
92
if (!add_histogram_sample_function_) return;
93
return add_histogram_sample_function_(histogram, sample);
99
CounterLookupCallback lookup_function_;
100
CreateHistogramCallback create_histogram_function_;
101
AddHistogramSampleCallback add_histogram_sample_function_;
103
friend class Isolate;
105
DISALLOW_COPY_AND_ASSIGN(StatsTable);
108
// StatsCounters are dynamically created values which can be tracked in
109
// the StatsTable. They are designed to be lightweight to create and
112
// Internally, a counter represents a value in a row of a StatsTable.
113
// The row has a 32bit value for each process/thread in the table and also
114
// a name (stored in the table metadata). Since the storage location can be
115
// thread-specific, this class cannot be shared across threads.
117
// This class is designed to be POD initialized. It will be registered with
118
// the counter system on first use. For example:
119
// StatsCounter c = { "c:myctr", NULL, false };
120
struct StatsCounter {
125
// Sets the counter to a specific value.
126
void Set(int value) {
128
if (loc) *loc = value;
131
// Increments the counter.
137
void Increment(int value) {
143
// Decrements the counter.
149
void Decrement(int value) {
151
if (loc) (*loc) -= value;
154
// Is this counter enabled?
155
// Returns false if table is full.
157
return GetPtr() != NULL;
160
// Get the internal pointer to the counter. This is used
161
// by the code generator to emit code that manipulates a
162
// given counter without calling the runtime system.
163
int* GetInternalPointer() {
170
// Returns the cached address of this counter location.
172
if (lookup_done_) return ptr_;
174
ptr_ = FindLocationInStatsTable();
179
int* FindLocationInStatsTable() const;
182
// StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 };
183
struct StatsCounterTimer {
184
StatsCounter counter_;
192
// Stop the timer and record the results.
195
// Returns true if the timer is running.
197
return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0;
201
// A Histogram represents a dynamically created histogram in the StatsTable.
203
// This class is designed to be POD initialized. It will be registered with
204
// the histogram system on first use. For example:
205
// Histogram h = { "myhist", 0, 10000, 50, NULL, false };
214
// Add a single sample to this histogram.
215
void AddSample(int sample);
217
// Returns true if this histogram is enabled.
219
return GetHistogram() != NULL;
223
// Returns the handle to the histogram.
224
void* GetHistogram() {
227
histogram_ = CreateHistogram();
233
void* CreateHistogram() const;
236
// A HistogramTimer allows distributions of results to be created
237
// HistogramTimer t = { {L"foo", 0, 10000, 50, NULL, false}, 0, 0 };
238
struct HistogramTimer {
239
Histogram histogram_;
247
// Stop the timer and record the results.
250
// Returns true if the timer is running.
252
return histogram_.Enabled() && (start_time_ != 0) && (stop_time_ == 0);
256
// Helper class for scoping a HistogramTimer.
257
class HistogramTimerScope BASE_EMBEDDED {
259
explicit HistogramTimerScope(HistogramTimer* timer) :
263
~HistogramTimerScope() {
267
HistogramTimer* timer_;
271
} } // namespace v8::internal
273
#endif // V8_COUNTERS_H_