OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 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 24 matching lines...) Expand all Loading... |
35 // manipulated by name. | 35 // manipulated by name. |
36 | 36 |
37 class StatsTable : public AllStatic { | 37 class StatsTable : public AllStatic { |
38 public: | 38 public: |
39 // Register an application-defined function where | 39 // Register an application-defined function where |
40 // counters can be looked up. | 40 // counters can be looked up. |
41 static void SetCounterFunction(CounterLookupCallback f) { | 41 static void SetCounterFunction(CounterLookupCallback f) { |
42 lookup_function_ = f; | 42 lookup_function_ = f; |
43 } | 43 } |
44 | 44 |
| 45 // Register an application-defined function to create |
| 46 // a histogram for passing to the AddHistogramSample function |
| 47 static void SetCreateHistogramFunction(CreateHistogramCallback f) { |
| 48 create_histogram_function_ = f; |
| 49 } |
| 50 |
| 51 // Register an application-defined function to add a sample |
| 52 // to a histogram created with CreateHistogram function |
| 53 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { |
| 54 add_histogram_sample_function_ = f; |
| 55 } |
| 56 |
45 static bool HasCounterFunction() { | 57 static bool HasCounterFunction() { |
46 return lookup_function_ != NULL; | 58 return lookup_function_ != NULL; |
47 } | 59 } |
48 | 60 |
49 // Lookup the location of a counter by name. If the lookup | 61 // Lookup the location of a counter by name. If the lookup |
50 // is successful, returns a non-NULL pointer for writing the | 62 // is successful, returns a non-NULL pointer for writing the |
51 // value of the counter. Each thread calling this function | 63 // value of the counter. Each thread calling this function |
52 // may receive a different location to store it's counter. | 64 // may receive a different location to store it's counter. |
53 // The return value must not be cached and re-used across | 65 // The return value must not be cached and re-used across |
54 // threads, although a single thread is free to cache it. | 66 // threads, although a single thread is free to cache it. |
55 static int *FindLocation(const char* name) { | 67 static int *FindLocation(const char* name) { |
56 if (!lookup_function_) return NULL; | 68 if (!lookup_function_) return NULL; |
57 return lookup_function_(name); | 69 return lookup_function_(name); |
58 } | 70 } |
59 | 71 |
| 72 // Create a histogram by name. If the create is successful, |
| 73 // returns a non-NULL pointer for use with AddHistogramSample |
| 74 // function. min and max define the expected minimum and maximum |
| 75 // sample values. buckets is the maximum number of buckets |
| 76 // that the samples will be grouped into. |
| 77 static void *CreateHistogram(const char* name, |
| 78 int min, |
| 79 int max, |
| 80 size_t buckets) { |
| 81 if (!create_histogram_function_) return NULL; |
| 82 return create_histogram_function_(name, min, max, buckets); |
| 83 } |
| 84 |
| 85 // Add a sample to a histogram created with the CreateHistogram |
| 86 // function. |
| 87 static void AddHistogramSample(void* histogram, int sample) { |
| 88 if (!add_histogram_sample_function_) return; |
| 89 return add_histogram_sample_function_(histogram, sample); |
| 90 } |
| 91 |
60 private: | 92 private: |
61 static CounterLookupCallback lookup_function_; | 93 static CounterLookupCallback lookup_function_; |
| 94 static CreateHistogramCallback create_histogram_function_; |
| 95 static AddHistogramSampleCallback add_histogram_sample_function_; |
62 }; | 96 }; |
63 | 97 |
64 // StatsCounters are dynamically created values which can be tracked in | 98 // StatsCounters are dynamically created values which can be tracked in |
65 // the StatsTable. They are designed to be lightweight to create and | 99 // the StatsTable. They are designed to be lightweight to create and |
66 // easy to use. | 100 // easy to use. |
67 // | 101 // |
68 // Internally, a counter represents a value in a row of a StatsTable. | 102 // Internally, a counter represents a value in a row of a StatsTable. |
69 // The row has a 32bit value for each process/thread in the table and also | 103 // The row has a 32bit value for each process/thread in the table and also |
70 // a name (stored in the table metadata). Since the storage location can be | 104 // a name (stored in the table metadata). Since the storage location can be |
71 // thread-specific, this class cannot be shared across threads. | 105 // thread-specific, this class cannot be shared across threads. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 179 |
146 // Stop the timer and record the results. | 180 // Stop the timer and record the results. |
147 void Stop(); | 181 void Stop(); |
148 | 182 |
149 // Returns true if the timer is running. | 183 // Returns true if the timer is running. |
150 bool Running() { | 184 bool Running() { |
151 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; | 185 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; |
152 } | 186 } |
153 }; | 187 }; |
154 | 188 |
155 // A StatsRate is a combination of both a timer and a counter so that | 189 // A HistogramTimer allows distributions of results to be created |
156 // several statistics can be produced: | 190 // HistogramTimer t = { L"foo", NULL, false, 0, 0 }; |
157 // min, max, avg, count, total | 191 struct HistogramTimer { |
158 // | 192 const char* name_; |
159 // For example: | 193 void* histogram_; |
160 // StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 }, | 194 bool lookup_done_; |
161 // { L"c:myrate", NULL, false } }; | |
162 struct StatsRate { | |
163 StatsCounterTimer timer_; | |
164 StatsCounter counter_; | |
165 | 195 |
166 // Starts the rate timer. | 196 int64_t start_time_; |
167 void Start() { | 197 int64_t stop_time_; |
168 timer_.Start(); | 198 |
| 199 // Start the timer. |
| 200 void Start(); |
| 201 |
| 202 // Stop the timer and record the results. |
| 203 void Stop(); |
| 204 |
| 205 // Returns true if the timer is running. |
| 206 bool Running() { |
| 207 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0); |
169 } | 208 } |
170 | 209 |
171 // Stops the rate and records the time. | 210 protected: |
172 void Stop() { | 211 // Returns the handle to the histogram. |
173 if (timer_.Running()) { | 212 void* GetHistogram() { |
174 timer_.Stop(); | 213 if (!lookup_done_) { |
175 counter_.Increment(); | 214 lookup_done_ = true; |
| 215 histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50); |
176 } | 216 } |
| 217 return histogram_; |
177 } | 218 } |
178 }; | 219 }; |
179 | 220 |
180 | 221 // Helper class for scoping a HistogramTimer. |
181 // Helper class for scoping a rate. | 222 class HistogramTimerScope BASE_EMBEDDED { |
182 class StatsRateScope BASE_EMBEDDED { | |
183 public: | 223 public: |
184 explicit StatsRateScope(StatsRate* rate) : | 224 explicit HistogramTimerScope(HistogramTimer* timer) : |
185 rate_(rate) { | 225 timer_(timer) { |
186 rate_->Start(); | 226 timer_->Start(); |
187 } | 227 } |
188 ~StatsRateScope() { | 228 ~HistogramTimerScope() { |
189 rate_->Stop(); | 229 timer_->Stop(); |
190 } | 230 } |
191 private: | 231 private: |
192 StatsRate* rate_; | 232 HistogramTimer* timer_; |
193 }; | 233 }; |
194 | 234 |
195 | 235 |
196 } } // namespace v8::internal | 236 } } // namespace v8::internal |
197 | 237 |
198 #endif // V8_COUNTERS_H_ | 238 #endif // V8_COUNTERS_H_ |
OLD | NEW |