Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(590)

Side by Side Diff: src/counters.h

Issue 2906063002: Move StatsTable into the Counters class. (Closed)
Patch Set: Fix nits. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/api.cc ('k') | src/counters.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_COUNTERS_H_ 5 #ifndef V8_COUNTERS_H_
6 #define V8_COUNTERS_H_ 6 #define V8_COUNTERS_H_
7 7
8 #include "include/v8.h" 8 #include "include/v8.h"
9 #include "src/allocation.h" 9 #include "src/allocation.h"
10 #include "src/base/atomic-utils.h" 10 #include "src/base/atomic-utils.h"
11 #include "src/base/platform/elapsed-timer.h" 11 #include "src/base/platform/elapsed-timer.h"
12 #include "src/base/platform/time.h" 12 #include "src/base/platform/time.h"
13 #include "src/globals.h" 13 #include "src/globals.h"
14 #include "src/isolate.h" 14 #include "src/isolate.h"
15 #include "src/objects.h" 15 #include "src/objects.h"
16 #include "src/runtime/runtime.h" 16 #include "src/runtime/runtime.h"
17 #include "src/tracing/trace-event.h" 17 #include "src/tracing/trace-event.h"
18 #include "src/tracing/traced-value.h" 18 #include "src/tracing/traced-value.h"
19 #include "src/tracing/tracing-category-observer.h" 19 #include "src/tracing/tracing-category-observer.h"
20 20
21 namespace v8 { 21 namespace v8 {
22 namespace internal { 22 namespace internal {
23 23
24 // StatsCounters is an interface for plugging into external 24 // StatsCounters is an interface for plugging into external
25 // counters for monitoring. Counters can be looked up and 25 // counters for monitoring. Counters can be looked up and
26 // manipulated by name. 26 // manipulated by name.
27 27
28 class Counters;
29
28 class StatsTable { 30 class StatsTable {
29 public: 31 public:
30 // Register an application-defined function where 32 // Register an application-defined function where
31 // counters can be looked up. Note: Must be called on main thread, 33 // counters can be looked up. Note: Must be called on main thread,
32 // so that threaded stats counters can be created now. 34 // so that threaded stats counters can be created now.
33 void SetCounterFunction(CounterLookupCallback f, Isolate* isolate); 35 void SetCounterFunction(CounterLookupCallback f);
34 36
35 // Register an application-defined function to create 37 // Register an application-defined function to create
36 // a histogram for passing to the AddHistogramSample function 38 // a histogram for passing to the AddHistogramSample function
37 void SetCreateHistogramFunction(CreateHistogramCallback f) { 39 void SetCreateHistogramFunction(CreateHistogramCallback f) {
38 create_histogram_function_ = f; 40 create_histogram_function_ = f;
39 } 41 }
40 42
41 // Register an application-defined function to add a sample 43 // Register an application-defined function to add a sample
42 // to a histogram created with CreateHistogram function 44 // to a histogram created with CreateHistogram function
43 void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { 45 void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
(...skipping 29 matching lines...) Expand all
73 } 75 }
74 76
75 // Add a sample to a histogram created with the CreateHistogram 77 // Add a sample to a histogram created with the CreateHistogram
76 // function. 78 // function.
77 void AddHistogramSample(void* histogram, int sample) { 79 void AddHistogramSample(void* histogram, int sample) {
78 if (!add_histogram_sample_function_) return; 80 if (!add_histogram_sample_function_) return;
79 return add_histogram_sample_function_(histogram, sample); 81 return add_histogram_sample_function_(histogram, sample);
80 } 82 }
81 83
82 private: 84 private:
83 StatsTable(); 85 explicit StatsTable(Counters* counters);
84 86
87 Counters* counters_;
85 CounterLookupCallback lookup_function_; 88 CounterLookupCallback lookup_function_;
86 CreateHistogramCallback create_histogram_function_; 89 CreateHistogramCallback create_histogram_function_;
87 AddHistogramSampleCallback add_histogram_sample_function_; 90 AddHistogramSampleCallback add_histogram_sample_function_;
88 91
89 friend class Isolate; 92 friend class Counters;
90 93
91 DISALLOW_COPY_AND_ASSIGN(StatsTable); 94 DISALLOW_COPY_AND_ASSIGN(StatsTable);
92 }; 95 };
93 96
94 // Base class for stats counters. 97 // Base class for stats counters.
95 class StatsCounterBase { 98 class StatsCounterBase {
96 public:
97 StatsCounterBase() {}
98 StatsCounterBase(Isolate* isolate, const char* name)
99 : isolate_(isolate), name_(name), ptr_(nullptr) {}
100
101 protected: 99 protected:
102 Isolate* isolate_; 100 Counters* counters_;
103 const char* name_; 101 const char* name_;
104 int* ptr_; 102 int* ptr_;
105 103
104 StatsCounterBase() {}
105 StatsCounterBase(Counters* counters, const char* name)
106 : counters_(counters), name_(name), ptr_(nullptr) {}
107
106 void SetLoc(int* loc, int value) { *loc = value; } 108 void SetLoc(int* loc, int value) { *loc = value; }
107 void IncrementLoc(int* loc) { (*loc)++; } 109 void IncrementLoc(int* loc) { (*loc)++; }
108 void IncrementLoc(int* loc, int value) { (*loc) += value; } 110 void IncrementLoc(int* loc, int value) { (*loc) += value; }
109 void DecrementLoc(int* loc) { (*loc)--; } 111 void DecrementLoc(int* loc) { (*loc)--; }
110 void DecrementLoc(int* loc, int value) { (*loc) -= value; } 112 void DecrementLoc(int* loc, int value) { (*loc) -= value; }
111 113
112 int* FindLocationInStatsTable() const; 114 int* FindLocationInStatsTable() const;
113 }; 115 };
114 116
115 // StatsCounters are dynamically created values which can be tracked in 117 // StatsCounters are dynamically created values which can be tracked in
116 // the StatsTable. They are designed to be lightweight to create and 118 // the StatsTable. They are designed to be lightweight to create and
117 // easy to use. 119 // easy to use.
118 // 120 //
119 // Internally, a counter represents a value in a row of a StatsTable. 121 // Internally, a counter represents a value in a row of a StatsTable.
120 // The row has a 32bit value for each process/thread in the table and also 122 // The row has a 32bit value for each process/thread in the table and also
121 // a name (stored in the table metadata). Since the storage location can be 123 // a name (stored in the table metadata). Since the storage location can be
122 // thread-specific, this class cannot be shared across threads. 124 // thread-specific, this class cannot be shared across threads.
123 class StatsCounter : public StatsCounterBase { 125 class StatsCounter : public StatsCounterBase {
124 public: 126 public:
125 StatsCounter() { } 127 StatsCounter() { }
126 StatsCounter(Isolate* isolate, const char* name) 128 StatsCounter(Counters* counters, const char* name)
127 : StatsCounterBase(isolate, name), lookup_done_(false) {} 129 : StatsCounterBase(counters, name), lookup_done_(false) {}
128 130
129 // Sets the counter to a specific value. 131 // Sets the counter to a specific value.
130 void Set(int value) { 132 void Set(int value) {
131 if (int* loc = GetPtr()) SetLoc(loc, value); 133 if (int* loc = GetPtr()) SetLoc(loc, value);
132 } 134 }
133 135
134 // Increments the counter. 136 // Increments the counter.
135 void Increment() { 137 void Increment() {
136 if (int* loc = GetPtr()) IncrementLoc(loc); 138 if (int* loc = GetPtr()) IncrementLoc(loc);
137 } 139 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 180
179 bool lookup_done_; 181 bool lookup_done_;
180 }; 182 };
181 183
182 // Thread safe version of StatsCounter. WARNING: Unlike StatsCounter, 184 // Thread safe version of StatsCounter. WARNING: Unlike StatsCounter,
183 // StatsCounterThreadSafe's constructor and method Reset() actually do 185 // StatsCounterThreadSafe's constructor and method Reset() actually do
184 // the table lookup, and should be called from the main thread 186 // the table lookup, and should be called from the main thread
185 // (i.e. not workers). 187 // (i.e. not workers).
186 class StatsCounterThreadSafe : public StatsCounterBase { 188 class StatsCounterThreadSafe : public StatsCounterBase {
187 public: 189 public:
188 StatsCounterThreadSafe(Isolate* isolate, const char* name); 190 StatsCounterThreadSafe(Counters* counters, const char* name);
189 191
190 void Set(int Value); 192 void Set(int Value);
191 void Increment(); 193 void Increment();
192 void Increment(int value); 194 void Increment(int value);
193 void Decrement(); 195 void Decrement();
194 void Decrement(int value); 196 void Decrement(int value);
195 bool Enabled() { return ptr_ != NULL; } 197 bool Enabled() { return ptr_ != NULL; }
196 int* GetInternalPointer() { 198 int* GetInternalPointer() {
197 DCHECK(ptr_ != NULL); 199 DCHECK(ptr_ != NULL);
198 return ptr_; 200 return ptr_;
199 } 201 }
200 void Reset() { GetPtr(); } 202 void Reset() { GetPtr(); }
201 203
202 private: 204 private:
203 int* GetPtr(); 205 int* GetPtr();
204 206
205 base::Mutex mutex_; 207 base::Mutex mutex_;
206 208
207 private: 209 private:
208 DISALLOW_IMPLICIT_CONSTRUCTORS(StatsCounterThreadSafe); 210 DISALLOW_IMPLICIT_CONSTRUCTORS(StatsCounterThreadSafe);
209 }; 211 };
210 212
211 // A Histogram represents a dynamically created histogram in the StatsTable. 213 // A Histogram represents a dynamically created histogram in the StatsTable.
212 // It will be registered with the histogram system on first use. 214 // It will be registered with the histogram system on first use.
213 class Histogram { 215 class Histogram {
214 public: 216 public:
215 Histogram() { } 217 Histogram() { }
216 Histogram(const char* name, 218 Histogram(const char* name, int min, int max, int num_buckets,
217 int min, 219 Counters* counters)
218 int max,
219 int num_buckets,
220 Isolate* isolate)
221 : name_(name), 220 : name_(name),
222 min_(min), 221 min_(min),
223 max_(max), 222 max_(max),
224 num_buckets_(num_buckets), 223 num_buckets_(num_buckets),
225 histogram_(NULL), 224 histogram_(NULL),
226 lookup_done_(false), 225 lookup_done_(false),
227 isolate_(isolate) { } 226 counters_(counters) {}
228 227
229 // Add a single sample to this histogram. 228 // Add a single sample to this histogram.
230 void AddSample(int sample); 229 void AddSample(int sample);
231 230
232 // Returns true if this histogram is enabled. 231 // Returns true if this histogram is enabled.
233 bool Enabled() { 232 bool Enabled() {
234 return GetHistogram() != NULL; 233 return GetHistogram() != NULL;
235 } 234 }
236 235
237 // Reset the cached internal pointer. 236 // Reset the cached internal pointer.
238 void Reset() { 237 void Reset() {
239 lookup_done_ = false; 238 lookup_done_ = false;
240 } 239 }
241 240
242 const char* name() { return name_; } 241 const char* name() { return name_; }
243 242
244 protected: 243 protected:
245 // Returns the handle to the histogram. 244 // Returns the handle to the histogram.
246 void* GetHistogram() { 245 void* GetHistogram() {
247 if (!lookup_done_) { 246 if (!lookup_done_) {
248 lookup_done_ = true; 247 lookup_done_ = true;
249 histogram_ = CreateHistogram(); 248 histogram_ = CreateHistogram();
250 } 249 }
251 return histogram_; 250 return histogram_;
252 } 251 }
253 252
254 Isolate* isolate() const { return isolate_; } 253 Counters* counters() const { return counters_; }
255 254
256 private: 255 private:
257 void* CreateHistogram() const; 256 void* CreateHistogram() const;
258 257
259 const char* name_; 258 const char* name_;
260 int min_; 259 int min_;
261 int max_; 260 int max_;
262 int num_buckets_; 261 int num_buckets_;
263 void* histogram_; 262 void* histogram_;
264 bool lookup_done_; 263 bool lookup_done_;
265 Isolate* isolate_; 264 Counters* counters_;
266 }; 265 };
267 266
268 // A HistogramTimer allows distributions of results to be created. 267 // A HistogramTimer allows distributions of results to be created.
269 class HistogramTimer : public Histogram { 268 class HistogramTimer : public Histogram {
270 public: 269 public:
271 enum Resolution { 270 enum Resolution {
272 MILLISECOND, 271 MILLISECOND,
273 MICROSECOND 272 MICROSECOND
274 }; 273 };
275 274
276 HistogramTimer() {} 275 HistogramTimer() {}
277 HistogramTimer(const char* name, int min, int max, Resolution resolution, 276 HistogramTimer(const char* name, int min, int max, Resolution resolution,
278 int num_buckets, Isolate* isolate) 277 int num_buckets, Counters* counters)
279 : Histogram(name, min, max, num_buckets, isolate), 278 : Histogram(name, min, max, num_buckets, counters),
280 resolution_(resolution) {} 279 resolution_(resolution) {}
281 280
282 // Start the timer. 281 // Start the timer.
283 void Start(); 282 void Start();
284 283
285 // Stop the timer and record the results. 284 // Stop the timer and record the results.
286 void Stop(); 285 void Stop();
287 286
288 // Returns true if the timer is running. 287 // Returns true if the timer is running.
289 bool Running() { 288 bool Running() {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 // 350 //
352 // Helpers: 351 // Helpers:
353 // - AggregatingHistogramTimerScope, the "outer" scope within which 352 // - AggregatingHistogramTimerScope, the "outer" scope within which
354 // times will be summed up. 353 // times will be summed up.
355 // - AggregatedHistogramTimerScope, the "inner" scope which defines the 354 // - AggregatedHistogramTimerScope, the "inner" scope which defines the
356 // events to be timed. 355 // events to be timed.
357 class AggregatableHistogramTimer : public Histogram { 356 class AggregatableHistogramTimer : public Histogram {
358 public: 357 public:
359 AggregatableHistogramTimer() {} 358 AggregatableHistogramTimer() {}
360 AggregatableHistogramTimer(const char* name, int min, int max, 359 AggregatableHistogramTimer(const char* name, int min, int max,
361 int num_buckets, Isolate* isolate) 360 int num_buckets, Counters* counters)
362 : Histogram(name, min, max, num_buckets, isolate) {} 361 : Histogram(name, min, max, num_buckets, counters) {}
363 362
364 // Start/stop the "outer" scope. 363 // Start/stop the "outer" scope.
365 void Start() { time_ = base::TimeDelta(); } 364 void Start() { time_ = base::TimeDelta(); }
366 void Stop() { AddSample(static_cast<int>(time_.InMicroseconds())); } 365 void Stop() { AddSample(static_cast<int>(time_.InMicroseconds())); }
367 366
368 // Add a time value ("inner" scope). 367 // Add a time value ("inner" scope).
369 void Add(base::TimeDelta other) { time_ += other; } 368 void Add(base::TimeDelta other) { time_ += other; }
370 369
371 private: 370 private:
372 base::TimeDelta time_; 371 base::TimeDelta time_;
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 stats_counter_count 1327 stats_counter_count
1329 }; 1328 };
1330 // clang-format on 1329 // clang-format on
1331 1330
1332 void ResetCounters(); 1331 void ResetCounters();
1333 void ResetHistograms(); 1332 void ResetHistograms();
1334 void InitializeHistograms(); 1333 void InitializeHistograms();
1335 1334
1336 RuntimeCallStats* runtime_call_stats() { return &runtime_call_stats_; } 1335 RuntimeCallStats* runtime_call_stats() { return &runtime_call_stats_; }
1337 1336
1337 StatsTable* stats_table() { return &stats_table_; }
1338
1339 Isolate* isolate() { return isolate_; }
1340
1338 private: 1341 private:
1342 Isolate* isolate_;
1343 StatsTable stats_table_;
1344
1339 #define HR(name, caption, min, max, num_buckets) Histogram name##_; 1345 #define HR(name, caption, min, max, num_buckets) Histogram name##_;
1340 HISTOGRAM_RANGE_LIST(HR) 1346 HISTOGRAM_RANGE_LIST(HR)
1341 #undef HR 1347 #undef HR
1342 1348
1343 #define HT(name, caption, max, res) HistogramTimer name##_; 1349 #define HT(name, caption, max, res) HistogramTimer name##_;
1344 HISTOGRAM_TIMER_LIST(HT) 1350 HISTOGRAM_TIMER_LIST(HT)
1345 #undef HT 1351 #undef HT
1346 1352
1347 #define AHT(name, caption) \ 1353 #define AHT(name, caption) \
1348 AggregatableHistogramTimer name##_; 1354 AggregatableHistogramTimer name##_;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1394 #undef SC 1400 #undef SC
1395 1401
1396 #define SC(name) \ 1402 #define SC(name) \
1397 StatsCounter size_of_CODE_AGE_##name##_; \ 1403 StatsCounter size_of_CODE_AGE_##name##_; \
1398 StatsCounter count_of_CODE_AGE_##name##_; 1404 StatsCounter count_of_CODE_AGE_##name##_;
1399 CODE_AGE_LIST_COMPLETE(SC) 1405 CODE_AGE_LIST_COMPLETE(SC)
1400 #undef SC 1406 #undef SC
1401 1407
1402 RuntimeCallStats runtime_call_stats_; 1408 RuntimeCallStats runtime_call_stats_;
1403 1409
1404 friend class Isolate;
1405
1406 DISALLOW_IMPLICIT_CONSTRUCTORS(Counters); 1410 DISALLOW_IMPLICIT_CONSTRUCTORS(Counters);
1407 }; 1411 };
1408 1412
1409 } // namespace internal 1413 } // namespace internal
1410 } // namespace v8 1414 } // namespace v8
1411 1415
1412 #endif // V8_COUNTERS_H_ 1416 #endif // V8_COUNTERS_H_
OLDNEW
« no previous file with comments | « src/api.cc ('k') | src/counters.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698