OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 }; | 106 }; |
107 | 107 |
108 // StatsCounters are dynamically created values which can be tracked in | 108 // StatsCounters are dynamically created values which can be tracked in |
109 // the StatsTable. They are designed to be lightweight to create and | 109 // the StatsTable. They are designed to be lightweight to create and |
110 // easy to use. | 110 // easy to use. |
111 // | 111 // |
112 // Internally, a counter represents a value in a row of a StatsTable. | 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 | 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 | 114 // a name (stored in the table metadata). Since the storage location can be |
115 // thread-specific, this class cannot be shared across threads. | 115 // thread-specific, this class cannot be shared across threads. |
116 // | 116 class StatsCounter { |
117 // This class is designed to be POD initialized. It will be registered with | 117 public: |
118 // the counter system on first use. For example: | 118 StatsCounter() { } |
119 // StatsCounter c = { "c:myctr", NULL, false }; | 119 explicit StatsCounter(const char* name) |
120 struct StatsCounter { | 120 : name_(name), ptr_(NULL), lookup_done_(false) { } |
121 const char* name_; | |
122 int* ptr_; | |
123 bool lookup_done_; | |
124 | 121 |
125 // Sets the counter to a specific value. | 122 // Sets the counter to a specific value. |
126 void Set(int value) { | 123 void Set(int value) { |
127 int* loc = GetPtr(); | 124 int* loc = GetPtr(); |
128 if (loc) *loc = value; | 125 if (loc) *loc = value; |
129 } | 126 } |
130 | 127 |
131 // Increments the counter. | 128 // Increments the counter. |
132 void Increment() { | 129 void Increment() { |
133 int* loc = GetPtr(); | 130 int* loc = GetPtr(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // Returns the cached address of this counter location. | 167 // Returns the cached address of this counter location. |
171 int* GetPtr() { | 168 int* GetPtr() { |
172 if (lookup_done_) return ptr_; | 169 if (lookup_done_) return ptr_; |
173 lookup_done_ = true; | 170 lookup_done_ = true; |
174 ptr_ = FindLocationInStatsTable(); | 171 ptr_ = FindLocationInStatsTable(); |
175 return ptr_; | 172 return ptr_; |
176 } | 173 } |
177 | 174 |
178 private: | 175 private: |
179 int* FindLocationInStatsTable() const; | 176 int* FindLocationInStatsTable() const; |
180 }; | |
181 | 177 |
182 // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; | 178 const char* name_; |
183 struct StatsCounterTimer { | 179 int* ptr_; |
184 StatsCounter counter_; | 180 bool lookup_done_; |
185 | |
186 int64_t start_time_; | |
187 int64_t stop_time_; | |
188 | |
189 // Start the timer. | |
190 void Start(); | |
191 | |
192 // Stop the timer and record the results. | |
193 void Stop(); | |
194 | |
195 // Returns true if the timer is running. | |
196 bool Running() { | |
197 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; | |
198 } | |
199 }; | 181 }; |
200 | 182 |
201 // A Histogram represents a dynamically created histogram in the StatsTable. | 183 // A Histogram represents a dynamically created histogram in the StatsTable. |
202 // | 184 // It will be registered with the histogram system on first use. |
203 // This class is designed to be POD initialized. It will be registered with | 185 class Histogram { |
204 // the histogram system on first use. For example: | 186 public: |
205 // Histogram h = { "myhist", 0, 10000, 50, NULL, false }; | 187 Histogram() { } |
206 struct Histogram { | 188 Histogram(const char* name, |
207 const char* name_; | 189 int min, |
208 int min_; | 190 int max, |
209 int max_; | 191 int num_buckets, |
210 int num_buckets_; | 192 Isolate* isolate) |
211 void* histogram_; | 193 : name_(name), |
212 bool lookup_done_; | 194 min_(min), |
| 195 max_(max), |
| 196 num_buckets_(num_buckets), |
| 197 histogram_(NULL), |
| 198 lookup_done_(false), |
| 199 isolate_(isolate) { } |
213 | 200 |
214 // Add a single sample to this histogram. | 201 // Add a single sample to this histogram. |
215 void AddSample(int sample); | 202 void AddSample(int sample); |
216 | 203 |
217 // Returns true if this histogram is enabled. | 204 // Returns true if this histogram is enabled. |
218 bool Enabled() { | 205 bool Enabled() { |
219 return GetHistogram() != NULL; | 206 return GetHistogram() != NULL; |
220 } | 207 } |
221 | 208 |
222 // Reset the cached internal pointer. | 209 // Reset the cached internal pointer. |
223 void Reset() { | 210 void Reset() { |
224 lookup_done_ = false; | 211 lookup_done_ = false; |
225 } | 212 } |
226 | 213 |
227 protected: | 214 protected: |
228 // Returns the handle to the histogram. | 215 // Returns the handle to the histogram. |
229 void* GetHistogram() { | 216 void* GetHistogram() { |
230 if (!lookup_done_) { | 217 if (!lookup_done_) { |
231 lookup_done_ = true; | 218 lookup_done_ = true; |
232 histogram_ = CreateHistogram(); | 219 histogram_ = CreateHistogram(); |
233 } | 220 } |
234 return histogram_; | 221 return histogram_; |
235 } | 222 } |
236 | 223 |
| 224 const char* name() { return name_; } |
| 225 Isolate* isolate() const { return isolate_; } |
| 226 |
237 private: | 227 private: |
238 void* CreateHistogram() const; | 228 void* CreateHistogram() const; |
| 229 |
| 230 const char* name_; |
| 231 int min_; |
| 232 int max_; |
| 233 int num_buckets_; |
| 234 void* histogram_; |
| 235 bool lookup_done_; |
| 236 Isolate* isolate_; |
239 }; | 237 }; |
240 | 238 |
241 // A HistogramTimer allows distributions of results to be created | 239 // A HistogramTimer allows distributions of results to be created. |
242 // HistogramTimer t = { {L"foo", 0, 10000, 50, NULL, false}, 0, 0 }; | 240 class HistogramTimer : public Histogram { |
243 struct HistogramTimer { | 241 public: |
244 Histogram histogram_; | 242 HistogramTimer() { } |
245 | 243 HistogramTimer(const char* name, |
246 int64_t start_time_; | 244 int min, |
247 int64_t stop_time_; | 245 int max, |
| 246 int num_buckets, |
| 247 Isolate* isolate) |
| 248 : Histogram(name, min, max, num_buckets, isolate), |
| 249 start_time_(0), |
| 250 stop_time_(0) { } |
248 | 251 |
249 // Start the timer. | 252 // Start the timer. |
250 void Start(); | 253 void Start(); |
251 | 254 |
252 // Stop the timer and record the results. | 255 // Stop the timer and record the results. |
253 void Stop(); | 256 void Stop(); |
254 | 257 |
255 // Returns true if the timer is running. | 258 // Returns true if the timer is running. |
256 bool Running() { | 259 bool Running() { |
257 return histogram_.Enabled() && (start_time_ != 0) && (stop_time_ == 0); | 260 return Enabled() && (start_time_ != 0) && (stop_time_ == 0); |
258 } | 261 } |
259 | 262 |
260 void Reset() { | 263 private: |
261 histogram_.Reset(); | 264 int64_t start_time_; |
262 } | 265 int64_t stop_time_; |
263 }; | 266 }; |
264 | 267 |
265 // Helper class for scoping a HistogramTimer. | 268 // Helper class for scoping a HistogramTimer. |
266 class HistogramTimerScope BASE_EMBEDDED { | 269 class HistogramTimerScope BASE_EMBEDDED { |
267 public: | 270 public: |
268 explicit HistogramTimerScope(HistogramTimer* timer) : | 271 explicit HistogramTimerScope(HistogramTimer* timer) : |
269 timer_(timer) { | 272 timer_(timer) { |
270 timer_->Start(); | 273 timer_->Start(); |
271 } | 274 } |
272 ~HistogramTimerScope() { | 275 ~HistogramTimerScope() { |
273 timer_->Stop(); | 276 timer_->Stop(); |
274 } | 277 } |
275 private: | 278 private: |
276 HistogramTimer* timer_; | 279 HistogramTimer* timer_; |
277 }; | 280 }; |
278 | 281 |
279 | 282 |
280 } } // namespace v8::internal | 283 } } // namespace v8::internal |
281 | 284 |
282 #endif // V8_COUNTERS_H_ | 285 #endif // V8_COUNTERS_H_ |
OLD | NEW |