00001 // Copyright 2007-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_COUNTERS_H_ 00029 #define V8_COUNTERS_H_ 00030 00031 #include <wchar.h> 00032 00033 namespace v8 { namespace internal { 00034 00035 // StatsCounters is an interface for plugging into external 00036 // counters for monitoring. Counters can be looked up and 00037 // manipulated by name. 00038 00039 class StatsTable : public AllStatic { 00040 public: 00041 // Register an application-defined function where 00042 // counters can be looked up. 00043 static void SetCounterFunction(CounterLookupCallback f) { 00044 lookup_function_ = f; 00045 } 00046 00047 static bool HasCounterFunction() { 00048 return lookup_function_ != NULL; 00049 } 00050 00051 // Lookup the location of a counter by name. If the lookup 00052 // is successful, returns a non-NULL pointer for writing the 00053 // value of the counter. Each thread calling this function 00054 // may receive a different location to store it's counter. 00055 // The return value must not be cached and re-used across 00056 // threads, although a single thread is free to cache it. 00057 static int *FindLocation(const wchar_t* name) { 00058 if (!lookup_function_) return NULL; 00059 return lookup_function_(name); 00060 } 00061 00062 private: 00063 static CounterLookupCallback lookup_function_; 00064 }; 00065 00066 // StatsCounters are dynamically created values which can be tracked in 00067 // the StatsTable. They are designed to be lightweight to create and 00068 // easy to use. 00069 // 00070 // Internally, a counter represents a value in a row of a StatsTable. 00071 // The row has a 32bit value for each process/thread in the table and also 00072 // a name (stored in the table metadata). Since the storage location can be 00073 // thread-specific, this class cannot be shared across threads. 00074 // 00075 // This class is designed to be POD initialized. It will be registered with 00076 // the counter system on first use. For example: 00077 // StatsCounter c = { L"c:myctr", NULL, false }; 00078 struct StatsCounter { 00079 const wchar_t* name_; 00080 int* ptr_; 00081 bool lookup_done_; 00082 00083 // Sets the counter to a specific value. 00084 void Set(int value) { 00085 int* loc = GetPtr(); 00086 if (loc) *loc = value; 00087 } 00088 00089 // Increments the counter. 00090 void Increment() { 00091 int* loc = GetPtr(); 00092 if (loc) (*loc)++; 00093 } 00094 00095 void Increment(int value) { 00096 int* loc = GetPtr(); 00097 if (loc) 00098 (*loc) += value; 00099 } 00100 00101 // Decrements the counter. 00102 void Decrement() { 00103 int* loc = GetPtr(); 00104 if (loc) (*loc)--; 00105 } 00106 00107 void Decrement(int value) { 00108 int* loc = GetPtr(); 00109 if (loc) (*loc) -= value; 00110 } 00111 00112 // Is this counter enabled? 00113 // Returns false if table is full. 00114 bool Enabled() { 00115 return GetPtr() != NULL; 00116 } 00117 00118 // Get the internal pointer to the counter. This is used 00119 // by the code generator to emit code that manipulates a 00120 // given counter without calling the runtime system. 00121 int* GetInternalPointer() { 00122 int* loc = GetPtr(); 00123 ASSERT(loc != NULL); 00124 return loc; 00125 } 00126 00127 protected: 00128 // Returns the cached address of this counter location. 00129 int* GetPtr() { 00130 if (lookup_done_) 00131 return ptr_; 00132 lookup_done_ = true; 00133 ptr_ = StatsTable::FindLocation(name_); 00134 return ptr_; 00135 } 00136 }; 00137 00138 // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; 00139 struct StatsCounterTimer { 00140 StatsCounter counter_; 00141 00142 int64_t start_time_; 00143 int64_t stop_time_; 00144 00145 // Start the timer. 00146 void Start(); 00147 00148 // Stop the timer and record the results. 00149 void Stop(); 00150 00151 // Returns true if the timer is running. 00152 bool Running() { 00153 return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; 00154 } 00155 }; 00156 00157 // A StatsRate is a combination of both a timer and a counter so that 00158 // several statistics can be produced: 00159 // min, max, avg, count, total 00160 // 00161 // For example: 00162 // StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 }, 00163 // { L"c:myrate", NULL, false } }; 00164 struct StatsRate { 00165 StatsCounterTimer timer_; 00166 StatsCounter counter_; 00167 00168 // Starts the rate timer. 00169 void Start() { 00170 timer_.Start(); 00171 } 00172 00173 // Stops the rate and records the time. 00174 void Stop() { 00175 if (timer_.Running()) { 00176 timer_.Stop(); 00177 counter_.Increment(); 00178 } 00179 } 00180 }; 00181 00182 00183 // Helper class for scoping a rate. 00184 class StatsRateScope BASE_EMBEDDED { 00185 public: 00186 explicit StatsRateScope(StatsRate* rate) : 00187 rate_(rate) { 00188 rate_->Start(); 00189 } 00190 ~StatsRateScope() { 00191 rate_->Stop(); 00192 } 00193 private: 00194 StatsRate* rate_; 00195 }; 00196 00197 00198 } } // namespace v8::internal 00199 00200 #endif // V8_COUNTERS_H_