1 // Copyright 2007-2008 the V8 project authors. All rights reserved. 2 // 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 31 namespace v8 { 32 namespace internal { 33 34 // StatsCounters is an interface for plugging into external 35 // counters for monitoring. Counters can be looked up and 36 // manipulated by name. 37 38 class StatsTable : public AllStatic { 39 public: 40 // Register an application-defined function where 41 // counters can be looked up. SetCounterFunction(CounterLookupCallback f)42 static void SetCounterFunction(CounterLookupCallback f) { 43 lookup_function_ = f; 44 } 45 46 // Register an application-defined function to create 47 // a histogram for passing to the AddHistogramSample function SetCreateHistogramFunction(CreateHistogramCallback f)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 SetAddHistogramSampleFunction(AddHistogramSampleCallback f)54 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { 55 add_histogram_sample_function_ = f; 56 } 57 HasCounterFunction()58 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. FindLocation(const char * name)68 static int *FindLocation(const char* name) { 69 if (!lookup_function_) return NULL; 70 return lookup_function_(name); 71 } 72 73 // 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. CreateHistogram(const char * name,int min,int max,size_t buckets)78 static void* CreateHistogram(const char* name, 79 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. AddHistogramSample(void * histogram,int sample)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 93 private: 94 static CounterLookupCallback lookup_function_; 95 static CreateHistogramCallback create_histogram_function_; 96 static AddHistogramSampleCallback add_histogram_sample_function_; 97 }; 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 // 103 // 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 // 108 // This class is designed to be POD initialized. It will be registered with 109 // the counter system on first use. For example: 110 // StatsCounter c = { "c:myctr", NULL, false }; 111 struct StatsCounter { 112 const char* name_; 113 int* ptr_; 114 bool lookup_done_; 115 116 // Sets the counter to a specific value. SetStatsCounter117 void Set(int value) { 118 int* loc = GetPtr(); 119 if (loc) *loc = value; 120 } 121 122 // Increments the counter. IncrementStatsCounter123 void Increment() { 124 int* loc = GetPtr(); 125 if (loc) (*loc)++; 126 } 127 IncrementStatsCounter128 void Increment(int value) { 129 int* loc = GetPtr(); 130 if (loc) 131 (*loc) += value; 132 } 133 134 // Decrements the counter. DecrementStatsCounter135 void Decrement() { 136 int* loc = GetPtr(); 137 if (loc) (*loc)--; 138 } 139 DecrementStatsCounter140 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. EnabledStatsCounter147 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. GetInternalPointerStatsCounter154 int* GetInternalPointer() { 155 int* loc = GetPtr(); 156 ASSERT(loc != NULL); 157 return loc; 158 } 159 160 protected: 161 // Returns the cached address of this counter location. GetPtrStatsCounter162 int* GetPtr() { 163 if (lookup_done_) 164 return ptr_; 165 lookup_done_ = true; 166 ptr_ = StatsTable::FindLocation(name_); 167 return ptr_; 168 } 169 }; 170 171 // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; 172 struct StatsCounterTimer { 173 StatsCounter counter_; 174 175 int64_t start_time_; 176 int64_t stop_time_; 177 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. RunningStatsCounterTimer185 bool Running() { 186 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; 187 } 188 }; 189 190 // A HistogramTimer allows distributions of results to be created 191 // HistogramTimer t = { L"foo", NULL, false, 0, 0 }; 192 struct HistogramTimer { 193 const char* name_; 194 void* histogram_; 195 bool lookup_done_; 196 197 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. RunningHistogramTimer207 bool Running() { 208 return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0); 209 } 210 211 protected: 212 // Returns the handle to the histogram. GetHistogramHistogramTimer213 void* GetHistogram() { 214 if (!lookup_done_) { 215 lookup_done_ = true; 216 histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50); 217 } 218 return histogram_; 219 } 220 }; 221 222 // Helper class for scoping a HistogramTimer. 223 class HistogramTimerScope BASE_EMBEDDED { 224 public: HistogramTimerScope(HistogramTimer * timer)225 explicit HistogramTimerScope(HistogramTimer* timer) : 226 timer_(timer) { 227 timer_->Start(); 228 } ~HistogramTimerScope()229 ~HistogramTimerScope() { 230 timer_->Stop(); 231 } 232 private: 233 HistogramTimer* timer_; 234 }; 235 236 237 } } // namespace v8::internal 238 239 #endif // V8_COUNTERS_H_ 240