ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2007-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | namespace v8 { namespace internal { |
| 32 | |
| 33 | // StatsCounters is an interface for plugging into external |
| 34 | // counters for monitoring. Counters can be looked up and |
| 35 | // manipulated by name. |
| 36 | |
| 37 | class StatsTable : public AllStatic { |
| 38 | public: |
| 39 | // Register an application-defined function where |
| 40 | // counters can be looked up. |
| 41 | static void SetCounterFunction(CounterLookupCallback f) { |
| 42 | lookup_function_ = f; |
| 43 | } |
| 44 | |
| 45 | static bool HasCounterFunction() { |
| 46 | return lookup_function_ != NULL; |
| 47 | } |
| 48 | |
| 49 | // Lookup the location of a counter by name. If the lookup |
| 50 | // is successful, returns a non-NULL pointer for writing the |
| 51 | // value of the counter. Each thread calling this function |
| 52 | // may receive a different location to store it's counter. |
| 53 | // The return value must not be cached and re-used across |
| 54 | // threads, although a single thread is free to cache it. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 55 | static int *FindLocation(const char* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | if (!lookup_function_) return NULL; |
| 57 | return lookup_function_(name); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | static CounterLookupCallback lookup_function_; |
| 62 | }; |
| 63 | |
| 64 | // StatsCounters are dynamically created values which can be tracked in |
| 65 | // the StatsTable. They are designed to be lightweight to create and |
| 66 | // easy to use. |
| 67 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | // 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 |
| 70 | // a name (stored in the table metadata). Since the storage location can be |
| 71 | // thread-specific, this class cannot be shared across threads. |
| 72 | // |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 73 | // This class is designed to be POD initialized. It will be registered with |
| 74 | // the counter system on first use. For example: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 75 | // StatsCounter c = { "c:myctr", NULL, false }; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 76 | struct StatsCounter { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 77 | const char* name_; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 78 | int* ptr_; |
| 79 | bool lookup_done_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
| 81 | // Sets the counter to a specific value. |
| 82 | void Set(int value) { |
| 83 | int* loc = GetPtr(); |
| 84 | if (loc) *loc = value; |
| 85 | } |
| 86 | |
| 87 | // Increments the counter. |
| 88 | void Increment() { |
| 89 | int* loc = GetPtr(); |
| 90 | if (loc) (*loc)++; |
| 91 | } |
| 92 | |
| 93 | void Increment(int value) { |
| 94 | int* loc = GetPtr(); |
| 95 | if (loc) |
| 96 | (*loc) += value; |
| 97 | } |
| 98 | |
| 99 | // Decrements the counter. |
| 100 | void Decrement() { |
| 101 | int* loc = GetPtr(); |
| 102 | if (loc) (*loc)--; |
| 103 | } |
| 104 | |
| 105 | void Decrement(int value) { |
| 106 | int* loc = GetPtr(); |
| 107 | if (loc) (*loc) -= value; |
| 108 | } |
| 109 | |
| 110 | // Is this counter enabled? |
| 111 | // Returns false if table is full. |
| 112 | bool Enabled() { |
| 113 | return GetPtr() != NULL; |
| 114 | } |
| 115 | |
| 116 | // Get the internal pointer to the counter. This is used |
| 117 | // by the code generator to emit code that manipulates a |
| 118 | // given counter without calling the runtime system. |
| 119 | int* GetInternalPointer() { |
| 120 | int* loc = GetPtr(); |
| 121 | ASSERT(loc != NULL); |
| 122 | return loc; |
| 123 | } |
| 124 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | protected: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | // Returns the cached address of this counter location. |
| 127 | int* GetPtr() { |
| 128 | if (lookup_done_) |
| 129 | return ptr_; |
| 130 | lookup_done_ = true; |
| 131 | ptr_ = StatsTable::FindLocation(name_); |
| 132 | return ptr_; |
| 133 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 136 | // StatsCounterTimer t = { { L"t:foo", NULL, false }, 0, 0 }; |
| 137 | struct StatsCounterTimer { |
| 138 | StatsCounter counter_; |
| 139 | |
| 140 | int64_t start_time_; |
| 141 | int64_t stop_time_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
| 143 | // Start the timer. |
| 144 | void Start(); |
| 145 | |
| 146 | // Stop the timer and record the results. |
| 147 | void Stop(); |
| 148 | |
| 149 | // Returns true if the timer is running. |
| 150 | bool Running() { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 151 | return counter_.Enabled() && start_time_ != 0 && stop_time_ == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | // A StatsRate is a combination of both a timer and a counter so that |
| 156 | // several statistics can be produced: |
| 157 | // min, max, avg, count, total |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 158 | // |
| 159 | // For example: |
| 160 | // StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 }, |
| 161 | // { L"c:myrate", NULL, false } }; |
| 162 | struct StatsRate { |
| 163 | StatsCounterTimer timer_; |
| 164 | StatsCounter counter_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | |
| 166 | // Starts the rate timer. |
| 167 | void Start() { |
| 168 | timer_.Start(); |
| 169 | } |
| 170 | |
| 171 | // Stops the rate and records the time. |
| 172 | void Stop() { |
| 173 | if (timer_.Running()) { |
| 174 | timer_.Stop(); |
| 175 | counter_.Increment(); |
| 176 | } |
| 177 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | // Helper class for scoping a rate. |
| 182 | class StatsRateScope BASE_EMBEDDED { |
| 183 | public: |
| 184 | explicit StatsRateScope(StatsRate* rate) : |
| 185 | rate_(rate) { |
| 186 | rate_->Start(); |
| 187 | } |
| 188 | ~StatsRateScope() { |
| 189 | rate_->Stop(); |
| 190 | } |
| 191 | private: |
| 192 | StatsRate* rate_; |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | } } // namespace v8::internal |
| 197 | |
| 198 | #endif // V8_COUNTERS_H_ |