OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Histogram is an object that aggregates statistics, and can summarize them in | 5 // Histogram is an object that aggregates statistics, and can summarize them in |
6 // various forms, including ASCII graphical, HTML, and numerically (as a | 6 // various forms, including ASCII graphical, HTML, and numerically (as a |
7 // vector of numbers corresponding to each of the aggregating buckets). | 7 // vector of numbers corresponding to each of the aggregating buckets). |
8 | 8 |
9 // It supports calls to accumulate either time intervals (which are processed | 9 // It supports calls to accumulate either time intervals (which are processed |
10 // as integral number of milliseconds), or arbitrary integral units. | 10 // as integral number of milliseconds), or arbitrary integral units. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // The above example has an exponential ratio of 2 (doubling the bucket width | 43 // The above example has an exponential ratio of 2 (doubling the bucket width |
44 // in each consecutive bucket. The Histogram class automatically calculates | 44 // in each consecutive bucket. The Histogram class automatically calculates |
45 // the smallest ratio that it can use to construct the number of buckets | 45 // the smallest ratio that it can use to construct the number of buckets |
46 // selected in the constructor. An another example, if you had 50 buckets, | 46 // selected in the constructor. An another example, if you had 50 buckets, |
47 // and millisecond time values from 1 to 10000, then the ratio between | 47 // and millisecond time values from 1 to 10000, then the ratio between |
48 // consecutive bucket widths will be approximately somewhere around the 50th | 48 // consecutive bucket widths will be approximately somewhere around the 50th |
49 // root of 10000. This approach provides very fine grain (narrow) buckets | 49 // root of 10000. This approach provides very fine grain (narrow) buckets |
50 // at the low end of the histogram scale, but allows the histogram to cover a | 50 // at the low end of the histogram scale, but allows the histogram to cover a |
51 // gigantic range with the addition of very few buckets. | 51 // gigantic range with the addition of very few buckets. |
52 | 52 |
53 // Usually we use macros to define and use a histogram. These macros use a | 53 // Usually we use macros to define and use a histogram, which are defined in |
54 // pattern involving a function static variable, that is a pointer to a | 54 // base/metrics/histogram_macros.h. Note: Callers should include that header |
55 // histogram. This static is explicitly initialized on any thread | 55 // directly if they only access the histogram APIs through macros. |
| 56 // |
| 57 // Macros use a pattern involving a function static variable, that is a pointer |
| 58 // to a histogram. This static is explicitly initialized on any thread |
56 // that detects a uninitialized (NULL) pointer. The potentially racy | 59 // that detects a uninitialized (NULL) pointer. The potentially racy |
57 // initialization is not a problem as it is always set to point to the same | 60 // initialization is not a problem as it is always set to point to the same |
58 // value (i.e., the FactoryGet always returns the same value). FactoryGet | 61 // value (i.e., the FactoryGet always returns the same value). FactoryGet |
59 // is also completely thread safe, which results in a completely thread safe, | 62 // is also completely thread safe, which results in a completely thread safe, |
60 // and relatively fast, set of counters. To avoid races at shutdown, the static | 63 // and relatively fast, set of counters. To avoid races at shutdown, the static |
61 // pointer is NOT deleted, and we leak the histograms at process termination. | 64 // pointer is NOT deleted, and we leak the histograms at process termination. |
62 | 65 |
63 #ifndef BASE_METRICS_HISTOGRAM_H_ | 66 #ifndef BASE_METRICS_HISTOGRAM_H_ |
64 #define BASE_METRICS_HISTOGRAM_H_ | 67 #define BASE_METRICS_HISTOGRAM_H_ |
65 | 68 |
66 #include <map> | 69 #include <map> |
67 #include <string> | 70 #include <string> |
68 #include <vector> | 71 #include <vector> |
69 | 72 |
70 #include "base/atomicops.h" | |
71 #include "base/base_export.h" | 73 #include "base/base_export.h" |
72 #include "base/basictypes.h" | 74 #include "base/basictypes.h" |
73 #include "base/compiler_specific.h" | 75 #include "base/compiler_specific.h" |
74 #include "base/gtest_prod_util.h" | 76 #include "base/gtest_prod_util.h" |
75 #include "base/logging.h" | 77 #include "base/logging.h" |
76 #include "base/memory/scoped_ptr.h" | 78 #include "base/memory/scoped_ptr.h" |
77 #include "base/metrics/bucket_ranges.h" | 79 #include "base/metrics/bucket_ranges.h" |
78 #include "base/metrics/histogram_base.h" | 80 #include "base/metrics/histogram_base.h" |
| 81 // TODO(asvitkine): Migrate callers to to include this directly and remove this. |
| 82 #include "base/metrics/histogram_macros.h" |
79 #include "base/metrics/histogram_samples.h" | 83 #include "base/metrics/histogram_samples.h" |
80 #include "base/time/time.h" | 84 #include "base/time/time.h" |
81 | 85 |
82 class Pickle; | 86 class Pickle; |
83 class PickleIterator; | 87 class PickleIterator; |
84 | 88 |
85 namespace base { | 89 namespace base { |
86 | 90 |
87 class Lock; | |
88 //------------------------------------------------------------------------------ | |
89 // Histograms are often put in areas where they are called many many times, and | |
90 // performance is critical. As a result, they are designed to have a very low | |
91 // recurring cost of executing (adding additional samples). Toward that end, | |
92 // the macros declare a static pointer to the histogram in question, and only | |
93 // take a "slow path" to construct (or find) the histogram on the first run | |
94 // through the macro. We leak the histograms at shutdown time so that we don't | |
95 // have to validate using the pointers at any time during the running of the | |
96 // process. | |
97 | |
98 // The following code is generally what a thread-safe static pointer | |
99 // initialization looks like for a histogram (after a macro is expanded). This | |
100 // sample is an expansion (with comments) of the code for | |
101 // LOCAL_HISTOGRAM_CUSTOM_COUNTS(). | |
102 | |
103 /* | |
104 do { | |
105 // The pointer's presence indicates the initialization is complete. | |
106 // Initialization is idempotent, so it can safely be atomically repeated. | |
107 static base::subtle::AtomicWord atomic_histogram_pointer = 0; | |
108 | |
109 // Acquire_Load() ensures that we acquire visibility to the pointed-to data | |
110 // in the histogram. | |
111 base::Histogram* histogram_pointer(reinterpret_cast<base::Histogram*>( | |
112 base::subtle::Acquire_Load(&atomic_histogram_pointer))); | |
113 | |
114 if (!histogram_pointer) { | |
115 // This is the slow path, which will construct OR find the matching | |
116 // histogram. FactoryGet includes locks on a global histogram name map | |
117 // and is completely thread safe. | |
118 histogram_pointer = base::Histogram::FactoryGet( | |
119 name, min, max, bucket_count, base::HistogramBase::kNoFlags); | |
120 | |
121 // Use Release_Store to ensure that the histogram data is made available | |
122 // globally before we make the pointer visible. | |
123 // Several threads may perform this store, but the same value will be | |
124 // stored in all cases (for a given named/spec'ed histogram). | |
125 // We could do this without any barrier, since FactoryGet entered and | |
126 // exited a lock after construction, but this barrier makes things clear. | |
127 base::subtle::Release_Store(&atomic_histogram_pointer, | |
128 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); | |
129 } | |
130 | |
131 // Ensure calling contract is upheld, and the name does NOT vary. | |
132 DCHECK(histogram_pointer->histogram_name() == constant_histogram_name); | |
133 | |
134 histogram_pointer->Add(sample); | |
135 } while (0); | |
136 */ | |
137 | |
138 // The above pattern is repeated in several macros. The only elements that | |
139 // vary are the invocation of the Add(sample) vs AddTime(sample), and the choice | |
140 // of which FactoryGet method to use. The different FactoryGet methods have | |
141 // various argument lists, so the function with its argument list is provided as | |
142 // a macro argument here. The name is only used in a DCHECK, to assure that | |
143 // callers don't try to vary the name of the histogram (which would tend to be | |
144 // ignored by the one-time initialization of the histogtram_pointer). | |
145 #define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \ | |
146 histogram_add_method_invocation, \ | |
147 histogram_factory_get_invocation) \ | |
148 do { \ | |
149 static base::subtle::AtomicWord atomic_histogram_pointer = 0; \ | |
150 base::HistogramBase* histogram_pointer( \ | |
151 reinterpret_cast<base::HistogramBase*>( \ | |
152 base::subtle::Acquire_Load(&atomic_histogram_pointer))); \ | |
153 if (!histogram_pointer) { \ | |
154 histogram_pointer = histogram_factory_get_invocation; \ | |
155 base::subtle::Release_Store(&atomic_histogram_pointer, \ | |
156 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); \ | |
157 } \ | |
158 if (DCHECK_IS_ON) \ | |
159 histogram_pointer->CheckName(constant_histogram_name); \ | |
160 histogram_pointer->histogram_add_method_invocation; \ | |
161 } while (0) | |
162 | |
163 | |
164 //------------------------------------------------------------------------------ | |
165 // Provide easy general purpose histogram in a macro, just like stats counters. | |
166 // The first four macros use 50 buckets. | |
167 | |
168 #define LOCAL_HISTOGRAM_TIMES(name, sample) LOCAL_HISTOGRAM_CUSTOM_TIMES( \ | |
169 name, sample, base::TimeDelta::FromMilliseconds(1), \ | |
170 base::TimeDelta::FromSeconds(10), 50) | |
171 | |
172 // For folks that need real specific times, use this to select a precise range | |
173 // of times you want plotted, and the number of buckets you want used. | |
174 #define LOCAL_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ | |
175 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ | |
176 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ | |
177 base::HistogramBase::kNoFlags)) | |
178 | |
179 #define LOCAL_HISTOGRAM_COUNTS(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \ | |
180 name, sample, 1, 1000000, 50) | |
181 | |
182 #define LOCAL_HISTOGRAM_COUNTS_100(name, sample) \ | |
183 LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50) | |
184 | |
185 #define LOCAL_HISTOGRAM_COUNTS_10000(name, sample) \ | |
186 LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50) | |
187 | |
188 #define LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ | |
189 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
190 base::Histogram::FactoryGet(name, min, max, bucket_count, \ | |
191 base::HistogramBase::kNoFlags)) | |
192 | |
193 // This is a helper macro used by other macros and shouldn't be used directly. | |
194 #define HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \ | |
195 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
196 base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, \ | |
197 flag)) | |
198 | |
199 #define LOCAL_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ | |
200 LOCAL_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) | |
201 | |
202 #define LOCAL_HISTOGRAM_BOOLEAN(name, sample) \ | |
203 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ | |
204 base::BooleanHistogram::FactoryGet(name, base::Histogram::kNoFlags)) | |
205 | |
206 // Support histograming of an enumerated value. The samples should always be | |
207 // strictly less than |boundary_value| -- this prevents you from running into | |
208 // problems down the line if you add additional buckets to the histogram. Note | |
209 // also that, despite explicitly setting the minimum bucket value to |1| below, | |
210 // it is fine for enumerated histograms to be 0-indexed -- this is because | |
211 // enumerated histograms should never have underflow. | |
212 #define LOCAL_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ | |
213 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
214 base::LinearHistogram::FactoryGet(name, 1, boundary_value, \ | |
215 boundary_value + 1, base::HistogramBase::kNoFlags)) | |
216 | |
217 // Support histograming of an enumerated value. Samples should be one of the | |
218 // std::vector<int> list provided via |custom_ranges|. See comments above | |
219 // CustomRanges::FactoryGet about the requirement of |custom_ranges|. | |
220 // You can use the helper function CustomHistogram::ArrayToCustomRanges to | |
221 // transform a C-style array of valid sample values to a std::vector<int>. | |
222 #define LOCAL_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ | |
223 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
224 base::CustomHistogram::FactoryGet(name, custom_ranges, \ | |
225 base::HistogramBase::kNoFlags)) | |
226 | |
227 #define LOCAL_HISTOGRAM_MEMORY_KB(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \ | |
228 name, sample, 1000, 500000, 50) | |
229 | |
230 //------------------------------------------------------------------------------ | |
231 // The following macros provide typical usage scenarios for callers that wish | |
232 // to record histogram data, and have the data submitted/uploaded via UMA. | |
233 // Not all systems support such UMA, but if they do, the following macros | |
234 // should work with the service. | |
235 | |
236 #define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ | |
237 name, sample, base::TimeDelta::FromMilliseconds(1), \ | |
238 base::TimeDelta::FromSeconds(10), 50) | |
239 | |
240 #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ | |
241 name, sample, base::TimeDelta::FromMilliseconds(10), \ | |
242 base::TimeDelta::FromMinutes(3), 50) | |
243 | |
244 // Use this macro when times can routinely be much longer than 10 seconds. | |
245 #define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ | |
246 name, sample, base::TimeDelta::FromMilliseconds(1), \ | |
247 base::TimeDelta::FromHours(1), 50) | |
248 | |
249 // Use this macro when times can routinely be much longer than 10 seconds and | |
250 // you want 100 buckets. | |
251 #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ | |
252 name, sample, base::TimeDelta::FromMilliseconds(1), \ | |
253 base::TimeDelta::FromHours(1), 100) | |
254 | |
255 #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ | |
256 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ | |
257 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ | |
258 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
259 | |
260 #define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
261 name, sample, 1, 1000000, 50) | |
262 | |
263 #define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
264 name, sample, 1, 100, 50) | |
265 | |
266 #define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
267 name, sample, 1, 10000, 50) | |
268 | |
269 #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ | |
270 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
271 base::Histogram::FactoryGet(name, min, max, bucket_count, \ | |
272 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
273 | |
274 #define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
275 name, sample, 1000, 500000, 50) | |
276 | |
277 #define UMA_HISTOGRAM_MEMORY_MB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
278 name, sample, 1, 1000, 50) | |
279 | |
280 #define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ | |
281 UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) | |
282 | |
283 #define UMA_HISTOGRAM_BOOLEAN(name, sample) \ | |
284 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ | |
285 base::BooleanHistogram::FactoryGet(name, \ | |
286 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
287 | |
288 // The samples should always be strictly less than |boundary_value|. For more | |
289 // details, see the comment for the |LOCAL_HISTOGRAM_ENUMERATION| macro, above. | |
290 #define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ | |
291 HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \ | |
292 base::HistogramBase::kUmaTargetedHistogramFlag) | |
293 | |
294 // Similar to UMA_HISTOGRAM_ENUMERATION, but used for recording stability | |
295 // histograms. Use this if recording a histogram that should be part of the | |
296 // initial stability log. | |
297 #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ | |
298 HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \ | |
299 base::HistogramBase::kUmaStabilityHistogramFlag) | |
300 | |
301 #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ | |
302 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ | |
303 base::CustomHistogram::FactoryGet(name, custom_ranges, \ | |
304 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
305 | |
306 //------------------------------------------------------------------------------ | |
307 | |
308 class BucketRanges; | |
309 class SampleVector; | |
310 | |
311 class BooleanHistogram; | 91 class BooleanHistogram; |
312 class CustomHistogram; | 92 class CustomHistogram; |
313 class Histogram; | 93 class Histogram; |
314 class LinearHistogram; | 94 class LinearHistogram; |
| 95 class SampleVector; |
315 | 96 |
316 class BASE_EXPORT Histogram : public HistogramBase { | 97 class BASE_EXPORT Histogram : public HistogramBase { |
317 public: | 98 public: |
318 // Initialize maximum number of buckets in histograms as 16,384. | 99 // Initialize maximum number of buckets in histograms as 16,384. |
319 static const size_t kBucketCount_MAX; | 100 static const size_t kBucketCount_MAX; |
320 | 101 |
321 typedef std::vector<Count> Counts; | 102 typedef std::vector<Count> Counts; |
322 | 103 |
323 //---------------------------------------------------------------------------- | 104 //---------------------------------------------------------------------------- |
324 // For a valid histogram, input should follow these restrictions: | 105 // For a valid histogram, input should follow these restrictions: |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); | 393 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); |
613 static BucketRanges* CreateBucketRangesFromCustomRanges( | 394 static BucketRanges* CreateBucketRangesFromCustomRanges( |
614 const std::vector<Sample>& custom_ranges); | 395 const std::vector<Sample>& custom_ranges); |
615 | 396 |
616 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 397 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
617 }; | 398 }; |
618 | 399 |
619 } // namespace base | 400 } // namespace base |
620 | 401 |
621 #endif // BASE_METRICS_HISTOGRAM_H_ | 402 #endif // BASE_METRICS_HISTOGRAM_H_ |
OLD | NEW |