blob: aed46cfb36e6dc514dc1d384c7afd8f8810239b8 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
34// StatsCounters is an interface for plugging into external
35// counters for monitoring. Counters can be looked up and
36// manipulated by name.
37
38class StatsTable : public AllStatic {
39 public:
40 // Register an application-defined function where
41 // counters can be looked up.
42 static void SetCounterFunction(CounterLookupCallback f) {
43 lookup_function_ = f;
44 }
45
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000046 // Register an application-defined function to create
47 // a histogram for passing to the AddHistogramSample function
48 static void SetCreateHistogramFunction(CreateHistogramCallback f) {
49 create_histogram_function_ = f;
50 }
51
52 // Register an application-defined function to add a sample
53 // to a histogram created with CreateHistogram function
54 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
55 add_histogram_sample_function_ = f;
56 }
57
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 static bool HasCounterFunction() {
59 return lookup_function_ != NULL;
60 }
61
62 // Lookup the location of a counter by name. If the lookup
63 // is successful, returns a non-NULL pointer for writing the
64 // value of the counter. Each thread calling this function
65 // may receive a different location to store it's counter.
66 // The return value must not be cached and re-used across
67 // threads, although a single thread is free to cache it.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000068 static int* FindLocation(const char* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069 if (!lookup_function_) return NULL;
70 return lookup_function_(name);
71 }
72
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000073 // Create a histogram by name. If the create is successful,
74 // returns a non-NULL pointer for use with AddHistogramSample
75 // function. min and max define the expected minimum and maximum
76 // sample values. buckets is the maximum number of buckets
77 // that the samples will be grouped into.
kasperl@chromium.org31ca8882009-05-26 09:36:02 +000078 static void* CreateHistogram(const char* name,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000079 int min,
80 int max,
81 size_t buckets) {
82 if (!create_histogram_function_) return NULL;
83 return create_histogram_function_(name, min, max, buckets);
84 }
85
86 // Add a sample to a histogram created with the CreateHistogram
87 // function.
88 static void AddHistogramSample(void* histogram, int sample) {
89 if (!add_histogram_sample_function_) return;
90 return add_histogram_sample_function_(histogram, sample);
91 }
92
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 private:
94 static CounterLookupCallback lookup_function_;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095 static CreateHistogramCallback create_histogram_function_;
96 static AddHistogramSampleCallback add_histogram_sample_function_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097};
98
99// StatsCounters are dynamically created values which can be tracked in
100// the StatsTable. They are designed to be lightweight to create and
101// easy to use.
102//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103// Internally, a counter represents a value in a row of a StatsTable.
104// The row has a 32bit value for each process/thread in the table and also
105// a name (stored in the table metadata). Since the storage location can be
106// thread-specific, this class cannot be shared across threads.
107//
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108// This class is designed to be POD initialized. It will be registered with
109// the counter system on first use. For example:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000110// StatsCounter c = { "c:myctr", NULL, false };
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000111struct StatsCounter {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000112 const char* name_;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000113 int* ptr_;
114 bool lookup_done_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
116 // Sets the counter to a specific value.
117 void Set(int value) {
118 int* loc = GetPtr();
119 if (loc) *loc = value;
120 }
121
122 // Increments the counter.
123 void Increment() {
124 int* loc = GetPtr();
125 if (loc) (*loc)++;
126 }
127
128 void Increment(int value) {
129 int* loc = GetPtr();
130 if (loc)
131 (*loc) += value;
132 }
133
134 // Decrements the counter.
135 void Decrement() {
136 int* loc = GetPtr();
137 if (loc) (*loc)--;
138 }
139
140 void Decrement(int value) {
141 int* loc = GetPtr();
142 if (loc) (*loc) -= value;
143 }
144
145 // Is this counter enabled?
146 // Returns false if table is full.
147 bool Enabled() {
148 return GetPtr() != NULL;
149 }
150
151 // Get the internal pointer to the counter. This is used
152 // by the code generator to emit code that manipulates a
153 // given counter without calling the runtime system.
154 int* GetInternalPointer() {
155 int* loc = GetPtr();
156 ASSERT(loc != NULL);
157 return loc;
158 }
159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 protected:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 // Returns the cached address of this counter location.
162 int* GetPtr() {
163 if (lookup_done_)
164 return ptr_;
165 lookup_done_ = true;
166 ptr_ = StatsTable::FindLocation(name_);
167 return ptr_;
168 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169};
170
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000171// StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 };
172struct StatsCounterTimer {
173 StatsCounter counter_;
174
175 int64_t start_time_;
176 int64_t stop_time_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
178 // Start the timer.
179 void Start();
180
181 // Stop the timer and record the results.
182 void Stop();
183
184 // Returns true if the timer is running.
185 bool Running() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000186 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188};
189
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000190// A HistogramTimer allows distributions of results to be created
191// HistogramTimer t = { L"foo", NULL, false, 0, 0 };
192struct HistogramTimer {
193 const char* name_;
194 void* histogram_;
195 bool lookup_done_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000197 int64_t start_time_;
198 int64_t stop_time_;
199
200 // Start the timer.
201 void Start();
202
203 // Stop the timer and record the results.
204 void Stop();
205
206 // Returns true if the timer is running.
207 bool Running() {
208 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 }
210
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000211 protected:
212 // Returns the handle to the histogram.
213 void* GetHistogram() {
214 if (!lookup_done_) {
215 lookup_done_ = true;
216 histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000218 return histogram_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220};
221
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000222// Helper class for scoping a HistogramTimer.
223class HistogramTimerScope BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000225 explicit HistogramTimerScope(HistogramTimer* timer) :
226 timer_(timer) {
227 timer_->Start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000229 ~HistogramTimerScope() {
230 timer_->Stop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 }
232 private:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000233 HistogramTimer* timer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234};
235
236
237} } // namespace v8::internal
238
239#endif // V8_COUNTERS_H_