Chromium Code Reviews

Side by Side Diff: base/stats_counters.h

Issue 3461019: FBTF: Move virtual methods to implementation files. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Win+chromeos+mac fixes Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 5
6 #ifndef BASE_STATS_COUNTERS_H__ 6 #ifndef BASE_STATS_COUNTERS_H__
7 #define BASE_STATS_COUNTERS_H__ 7 #define BASE_STATS_COUNTERS_H__
8 #pragma once 8 #pragma once
9 9
10 #include <string> 10 #include <string>
(...skipping 57 matching lines...)
68 #define DSIMPLE_STATS_COUNTER(name) do {} while (0) 68 #define DSIMPLE_STATS_COUNTER(name) do {} while (0)
69 #define DRATE_COUNTER(name, duration) do {} while (0) 69 #define DRATE_COUNTER(name, duration) do {} while (0)
70 70
71 #endif // NDEBUG 71 #endif // NDEBUG
72 72
73 //------------------------------------------------------------------------------ 73 //------------------------------------------------------------------------------
74 // StatsCounter represents a counter in the StatsTable class. 74 // StatsCounter represents a counter in the StatsTable class.
75 class StatsCounter { 75 class StatsCounter {
76 public: 76 public:
77 // Create a StatsCounter object. 77 // Create a StatsCounter object.
78 explicit StatsCounter(const std::string& name) 78 explicit StatsCounter(const std::string& name);
79 : counter_id_(-1) { 79 virtual ~StatsCounter();
80 // We prepend the name with 'c:' to indicate that it is a counter.
81 name_ = "c:";
82 name_.append(name);
83 };
84
85 virtual ~StatsCounter() {}
86 80
87 // Sets the counter to a specific value. 81 // Sets the counter to a specific value.
88 void Set(int value) { 82 void Set(int value);
89 int* loc = GetPtr();
90 if (loc) *loc = value;
91 }
92 83
93 // Increments the counter. 84 // Increments the counter.
94 void Increment() { 85 void Increment() {
95 Add(1); 86 Add(1);
96 } 87 }
97 88
98 virtual void Add(int value) { 89 virtual void Add(int value);
99 int* loc = GetPtr();
100 if (loc)
101 (*loc) += value;
102 }
103 90
104 // Decrements the counter. 91 // Decrements the counter.
105 void Decrement() { 92 void Decrement() {
106 Add(-1); 93 Add(-1);
107 } 94 }
108 95
109 void Subtract(int value) { 96 void Subtract(int value) {
110 Add(-value); 97 Add(-value);
111 } 98 }
112 99
113 // Is this counter enabled? 100 // Is this counter enabled?
114 // Returns false if table is full. 101 // Returns false if table is full.
115 bool Enabled() { 102 bool Enabled() {
116 return GetPtr() != NULL; 103 return GetPtr() != NULL;
117 } 104 }
118 105
119 int value() { 106 int value() {
120 int* loc = GetPtr(); 107 int* loc = GetPtr();
121 if (loc) return *loc; 108 if (loc) return *loc;
122 return 0; 109 return 0;
123 } 110 }
124 111
125 protected: 112 protected:
126 StatsCounter() 113 StatsCounter();
127 : counter_id_(-1) {
128 }
129 114
130 // Returns the cached address of this counter location. 115 // Returns the cached address of this counter location.
131 int* GetPtr() { 116 int* GetPtr();
132 StatsTable* table = StatsTable::current();
133 if (!table)
134 return NULL;
135
136 // If counter_id_ is -1, then we haven't looked it up yet.
137 if (counter_id_ == -1) {
138 counter_id_ = table->FindCounter(name_);
139 if (table->GetSlot() == 0) {
140 if (!table->RegisterThread("")) {
141 // There is no room for this thread. This thread
142 // cannot use counters.
143 counter_id_ = 0;
144 return NULL;
145 }
146 }
147 }
148
149 // If counter_id_ is > 0, then we have a valid counter.
150 if (counter_id_ > 0)
151 return table->GetLocation(counter_id_, table->GetSlot());
152
153 // counter_id_ was zero, which means the table is full.
154 return NULL;
155 }
156 117
157 std::string name_; 118 std::string name_;
158 // The counter id in the table. We initialize to -1 (an invalid value) 119 // The counter id in the table. We initialize to -1 (an invalid value)
159 // and then cache it once it has been looked up. The counter_id is 120 // and then cache it once it has been looked up. The counter_id is
160 // valid across all threads and processes. 121 // valid across all threads and processes.
161 int32 counter_id_; 122 int32 counter_id_;
162 }; 123 };
163 124
164 125
165 // A StatsCounterTimer is a StatsCounter which keeps a timer during 126 // A StatsCounterTimer is a StatsCounter which keeps a timer during
166 // the scope of the StatsCounterTimer. On destruction, it will record 127 // the scope of the StatsCounterTimer. On destruction, it will record
167 // its time measurement. 128 // its time measurement.
168 class StatsCounterTimer : protected StatsCounter { 129 class StatsCounterTimer : protected StatsCounter {
169 public: 130 public:
170 // Constructs and starts the timer. 131 // Constructs and starts the timer.
171 explicit StatsCounterTimer(const std::string& name) { 132 explicit StatsCounterTimer(const std::string& name);
172 // we prepend the name with 't:' to indicate that it is a timer. 133 virtual ~StatsCounterTimer();
173 name_ = "t:";
174 name_.append(name);
175 }
176 134
177 // Start the timer. 135 // Start the timer.
178 void Start() { 136 void Start();
179 if (!Enabled())
180 return;
181 start_time_ = base::TimeTicks::Now();
182 stop_time_ = base::TimeTicks();
183 }
184 137
185 // Stop the timer and record the results. 138 // Stop the timer and record the results.
186 void Stop() { 139 void Stop();
187 if (!Enabled() || !Running())
188 return;
189 stop_time_ = base::TimeTicks::Now();
190 Record();
191 }
192 140
193 // Returns true if the timer is running. 141 // Returns true if the timer is running.
194 bool Running() { 142 bool Running();
195 return Enabled() && !start_time_.is_null() && stop_time_.is_null();
196 }
197 143
198 // Accept a TimeDelta to increment. 144 // Accept a TimeDelta to increment.
199 virtual void AddTime(base::TimeDelta time) { 145 virtual void AddTime(base::TimeDelta time);
200 Add(static_cast<int>(time.InMilliseconds()));
201 }
202 146
203 protected: 147 protected:
204 // Compute the delta between start and stop, in milliseconds. 148 // Compute the delta between start and stop, in milliseconds.
205 void Record() { 149 void Record();
206 AddTime(stop_time_ - start_time_);
207 }
208 150
209 base::TimeTicks start_time_; 151 base::TimeTicks start_time_;
210 base::TimeTicks stop_time_; 152 base::TimeTicks stop_time_;
211 }; 153 };
212 154
213 // A StatsRate is a timer that keeps a count of the number of intervals added so 155 // A StatsRate is a timer that keeps a count of the number of intervals added so
214 // that several statistics can be produced: 156 // that several statistics can be produced:
215 // min, max, avg, count, total 157 // min, max, avg, count, total
216 class StatsRate : public StatsCounterTimer { 158 class StatsRate : public StatsCounterTimer {
217 public: 159 public:
218 // Constructs and starts the timer. 160 // Constructs and starts the timer.
219 explicit StatsRate(const char* name) 161 explicit StatsRate(const char* name);
220 : StatsCounterTimer(name), 162 virtual ~StatsRate();
221 counter_(name),
222 largest_add_(std::string(" ").append(name).append("MAX").c_str()) {
223 }
224 163
225 virtual void Add(int value) { 164 virtual void Add(int value);
226 counter_.Increment();
227 StatsCounterTimer::Add(value);
228 if (value > largest_add_.value())
229 largest_add_.Set(value);
230 }
231 165
232 private: 166 private:
233 StatsCounter counter_; 167 StatsCounter counter_;
234 StatsCounter largest_add_; 168 StatsCounter largest_add_;
235 }; 169 };
236 170
237 171
238 // Helper class for scoping a timer or rate. 172 // Helper class for scoping a timer or rate.
239 template<class T> class StatsScope { 173 template<class T> class StatsScope {
240 public: 174 public:
241 explicit StatsScope<T>(T& timer) 175 explicit StatsScope<T>(T& timer)
242 : timer_(timer) { 176 : timer_(timer) {
243 timer_.Start(); 177 timer_.Start();
244 } 178 }
245 179
246 ~StatsScope() { 180 ~StatsScope() {
247 timer_.Stop(); 181 timer_.Stop();
248 } 182 }
249 183
250 void Stop() { 184 void Stop() {
251 timer_.Stop(); 185 timer_.Stop();
252 } 186 }
253 187
254 private: 188 private:
255 T& timer_; 189 T& timer_;
256 }; 190 };
257 191
258 #endif // BASE_STATS_COUNTERS_H__ 192 #endif // BASE_STATS_COUNTERS_H__
OLDNEW

Powered by Google App Engine