| 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 17 matching lines...) Expand all Loading... |
| 28 #ifndef V8_COUNTERS_H_ | 28 #ifndef V8_COUNTERS_H_ |
| 29 #define V8_COUNTERS_H_ | 29 #define V8_COUNTERS_H_ |
| 30 | 30 |
| 31 namespace v8 { | 31 namespace v8 { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 // StatsCounters is an interface for plugging into external | 34 // StatsCounters is an interface for plugging into external |
| 35 // counters for monitoring. Counters can be looked up and | 35 // counters for monitoring. Counters can be looked up and |
| 36 // manipulated by name. | 36 // manipulated by name. |
| 37 | 37 |
| 38 class StatsTable : public AllStatic { | 38 class StatsTable { |
| 39 public: | 39 public: |
| 40 // Register an application-defined function where | 40 // Register an application-defined function where |
| 41 // counters can be looked up. | 41 // counters can be looked up. |
| 42 static void SetCounterFunction(CounterLookupCallback f) { | 42 void SetCounterFunction(CounterLookupCallback f) { |
| 43 lookup_function_ = f; | 43 lookup_function_ = f; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Register an application-defined function to create | 46 // Register an application-defined function to create |
| 47 // a histogram for passing to the AddHistogramSample function | 47 // a histogram for passing to the AddHistogramSample function |
| 48 static void SetCreateHistogramFunction(CreateHistogramCallback f) { | 48 void SetCreateHistogramFunction(CreateHistogramCallback f) { |
| 49 create_histogram_function_ = f; | 49 create_histogram_function_ = f; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Register an application-defined function to add a sample | 52 // Register an application-defined function to add a sample |
| 53 // to a histogram created with CreateHistogram function | 53 // to a histogram created with CreateHistogram function |
| 54 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { | 54 void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { |
| 55 add_histogram_sample_function_ = f; | 55 add_histogram_sample_function_ = f; |
| 56 } | 56 } |
| 57 | 57 |
| 58 static bool HasCounterFunction() { | 58 bool HasCounterFunction() const { |
| 59 return lookup_function_ != NULL; | 59 return lookup_function_ != NULL; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Lookup the location of a counter by name. If the lookup | 62 // Lookup the location of a counter by name. If the lookup |
| 63 // is successful, returns a non-NULL pointer for writing the | 63 // is successful, returns a non-NULL pointer for writing the |
| 64 // value of the counter. Each thread calling this function | 64 // value of the counter. Each thread calling this function |
| 65 // may receive a different location to store it's counter. | 65 // may receive a different location to store it's counter. |
| 66 // The return value must not be cached and re-used across | 66 // The return value must not be cached and re-used across |
| 67 // threads, although a single thread is free to cache it. | 67 // threads, although a single thread is free to cache it. |
| 68 static int* FindLocation(const char* name) { | 68 int* FindLocation(const char* name) { |
| 69 if (!lookup_function_) return NULL; | 69 if (!lookup_function_) return NULL; |
| 70 return lookup_function_(name); | 70 return lookup_function_(name); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Create a histogram by name. If the create is successful, | 73 // Create a histogram by name. If the create is successful, |
| 74 // returns a non-NULL pointer for use with AddHistogramSample | 74 // returns a non-NULL pointer for use with AddHistogramSample |
| 75 // function. min and max define the expected minimum and maximum | 75 // function. min and max define the expected minimum and maximum |
| 76 // sample values. buckets is the maximum number of buckets | 76 // sample values. buckets is the maximum number of buckets |
| 77 // that the samples will be grouped into. | 77 // that the samples will be grouped into. |
| 78 static void* CreateHistogram(const char* name, | 78 void* CreateHistogram(const char* name, |
| 79 int min, | 79 int min, |
| 80 int max, | 80 int max, |
| 81 size_t buckets) { | 81 size_t buckets) { |
| 82 if (!create_histogram_function_) return NULL; | 82 if (!create_histogram_function_) return NULL; |
| 83 return create_histogram_function_(name, min, max, buckets); | 83 return create_histogram_function_(name, min, max, buckets); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Add a sample to a histogram created with the CreateHistogram | 86 // Add a sample to a histogram created with the CreateHistogram |
| 87 // function. | 87 // function. |
| 88 static void AddHistogramSample(void* histogram, int sample) { | 88 void AddHistogramSample(void* histogram, int sample) { |
| 89 if (!add_histogram_sample_function_) return; | 89 if (!add_histogram_sample_function_) return; |
| 90 return add_histogram_sample_function_(histogram, sample); | 90 return add_histogram_sample_function_(histogram, sample); |
| 91 } | 91 } |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 static CounterLookupCallback lookup_function_; | 94 StatsTable(); |
| 95 static CreateHistogramCallback create_histogram_function_; | 95 |
| 96 static AddHistogramSampleCallback add_histogram_sample_function_; | 96 CounterLookupCallback lookup_function_; |
| 97 CreateHistogramCallback create_histogram_function_; |
| 98 AddHistogramSampleCallback add_histogram_sample_function_; |
| 99 |
| 100 friend class Isolate; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(StatsTable); |
| 97 }; | 103 }; |
| 98 | 104 |
| 99 // StatsCounters are dynamically created values which can be tracked in | 105 // StatsCounters are dynamically created values which can be tracked in |
| 100 // the StatsTable. They are designed to be lightweight to create and | 106 // the StatsTable. They are designed to be lightweight to create and |
| 101 // easy to use. | 107 // easy to use. |
| 102 // | 108 // |
| 103 // Internally, a counter represents a value in a row of a StatsTable. | 109 // 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 | 110 // 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 | 111 // a name (stored in the table metadata). Since the storage location can be |
| 106 // thread-specific, this class cannot be shared across threads. | 112 // thread-specific, this class cannot be shared across threads. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 ASSERT(loc != NULL); | 162 ASSERT(loc != NULL); |
| 157 return loc; | 163 return loc; |
| 158 } | 164 } |
| 159 | 165 |
| 160 protected: | 166 protected: |
| 161 // Returns the cached address of this counter location. | 167 // Returns the cached address of this counter location. |
| 162 int* GetPtr() { | 168 int* GetPtr() { |
| 163 if (lookup_done_) | 169 if (lookup_done_) |
| 164 return ptr_; | 170 return ptr_; |
| 165 lookup_done_ = true; | 171 lookup_done_ = true; |
| 166 ptr_ = StatsTable::FindLocation(name_); | 172 ptr_ = FindLocationInStatsTable(); |
| 167 return ptr_; | 173 return ptr_; |
| 168 } | 174 } |
| 175 |
| 176 private: |
| 177 int* FindLocationInStatsTable() const; |
| 169 }; | 178 }; |
| 170 | 179 |
| 171 // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; | 180 // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; |
| 172 struct StatsCounterTimer { | 181 struct StatsCounterTimer { |
| 173 StatsCounter counter_; | 182 StatsCounter counter_; |
| 174 | 183 |
| 175 int64_t start_time_; | 184 int64_t start_time_; |
| 176 int64_t stop_time_; | 185 int64_t stop_time_; |
| 177 | 186 |
| 178 // Start the timer. | 187 // Start the timer. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 206 // Returns true if the timer is running. | 215 // Returns true if the timer is running. |
| 207 bool Running() { | 216 bool Running() { |
| 208 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0); | 217 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0); |
| 209 } | 218 } |
| 210 | 219 |
| 211 protected: | 220 protected: |
| 212 // Returns the handle to the histogram. | 221 // Returns the handle to the histogram. |
| 213 void* GetHistogram() { | 222 void* GetHistogram() { |
| 214 if (!lookup_done_) { | 223 if (!lookup_done_) { |
| 215 lookup_done_ = true; | 224 lookup_done_ = true; |
| 216 histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50); | 225 histogram_ = CreateHistogram(); |
| 217 } | 226 } |
| 218 return histogram_; | 227 return histogram_; |
| 219 } | 228 } |
| 229 |
| 230 private: |
| 231 void* CreateHistogram() const; |
| 220 }; | 232 }; |
| 221 | 233 |
| 222 // Helper class for scoping a HistogramTimer. | 234 // Helper class for scoping a HistogramTimer. |
| 223 class HistogramTimerScope BASE_EMBEDDED { | 235 class HistogramTimerScope BASE_EMBEDDED { |
| 224 public: | 236 public: |
| 225 explicit HistogramTimerScope(HistogramTimer* timer) : | 237 explicit HistogramTimerScope(HistogramTimer* timer) : |
| 226 timer_(timer) { | 238 timer_(timer) { |
| 227 timer_->Start(); | 239 timer_->Start(); |
| 228 } | 240 } |
| 229 ~HistogramTimerScope() { | 241 ~HistogramTimerScope() { |
| 230 timer_->Stop(); | 242 timer_->Stop(); |
| 231 } | 243 } |
| 232 private: | 244 private: |
| 233 HistogramTimer* timer_; | 245 HistogramTimer* timer_; |
| 234 }; | 246 }; |
| 235 | 247 |
| 236 | 248 |
| 237 } } // namespace v8::internal | 249 } } // namespace v8::internal |
| 238 | 250 |
| 239 #endif // V8_COUNTERS_H_ | 251 #endif // V8_COUNTERS_H_ |
| OLD | NEW |