Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1169)

Side by Side Diff: base/metrics/histogram_macros.h

Issue 2361933002: Refactor histogram_macros.h. This improves documentation for the macros in the file, moves LOCAL_* … (Closed)
Patch Set: reword Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef BASE_METRICS_HISTOGRAM_MACROS_H_ 5 #ifndef BASE_METRICS_HISTOGRAM_MACROS_H_
6 #define BASE_METRICS_HISTOGRAM_MACROS_H_ 6 #define BASE_METRICS_HISTOGRAM_MACROS_H_
7 7
8 #include "base/atomicops.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_macros_internal.h"
10 #include "base/metrics/histogram_macros_local.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 12
13 // Macros for efficient use of histograms. See documentation in histogram.h. 13 // TODO(nikunjb): Move sparse macros to this file.
14 //
15 // UMA_HISTOGRAM_SPARSE_SLOWLY is defined in sparse_histogram.h as it has 14 // UMA_HISTOGRAM_SPARSE_SLOWLY is defined in sparse_histogram.h as it has
16 // different #include dependencies. 15 // different #include dependencies.
17 16
18 //------------------------------------------------------------------------------ 17 // TODO(rkaplow): Link to proper documentation on metric creation once we have
19 // Histograms are often put in areas where they are called many many times, and 18 // it in a good state.
20 // performance is critical. As a result, they are designed to have a very low 19
21 // recurring cost of executing (adding additional samples). Toward that end, 20 // All of these macros must be called with |name| as a runtime constant - it
22 // the macros declare a static pointer to the histogram in question, and only
23 // take a "slow path" to construct (or find) the histogram on the first run
24 // through the macro. We leak the histograms at shutdown time so that we don't
25 // have to validate using the pointers at any time during the running of the
26 // process.
27
28 // The following code is generally what a thread-safe static pointer
29 // initialization looks like for a histogram (after a macro is expanded). This
30 // sample is an expansion (with comments) of the code for
31 // LOCAL_HISTOGRAM_CUSTOM_COUNTS().
32
33 /*
34 do {
35 // The pointer's presence indicates the initialization is complete.
36 // Initialization is idempotent, so it can safely be atomically repeated.
37 static base::subtle::AtomicWord atomic_histogram_pointer = 0;
38
39 // Acquire_Load() ensures that we acquire visibility to the pointed-to data
40 // in the histogram.
41 base::Histogram* histogram_pointer(reinterpret_cast<base::Histogram*>(
42 base::subtle::Acquire_Load(&atomic_histogram_pointer)));
43
44 if (!histogram_pointer) {
45 // This is the slow path, which will construct OR find the matching
46 // histogram. FactoryGet includes locks on a global histogram name map
47 // and is completely thread safe.
48 histogram_pointer = base::Histogram::FactoryGet(
49 name, min, max, bucket_count, base::HistogramBase::kNoFlags);
50
51 // Use Release_Store to ensure that the histogram data is made available
52 // globally before we make the pointer visible.
53 // Several threads may perform this store, but the same value will be
54 // stored in all cases (for a given named/spec'ed histogram).
55 // We could do this without any barrier, since FactoryGet entered and
56 // exited a lock after construction, but this barrier makes things clear.
57 base::subtle::Release_Store(&atomic_histogram_pointer,
58 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer));
59 }
60
61 // Ensure calling contract is upheld, and the name does NOT vary.
62 DCHECK(histogram_pointer->histogram_name() == constant_histogram_name);
63
64 histogram_pointer->Add(sample);
65 } while (0);
66 */
67
68 // The above pattern is repeated in several macros. The only elements that
69 // vary are the invocation of the Add(sample) vs AddTime(sample), and the choice
70 // of which FactoryGet method to use. The different FactoryGet methods have
71 // various argument lists, so the function with its argument list is provided as
72 // a macro argument here. The name is only used in a DCHECK, to assure that
73 // callers don't try to vary the name of the histogram (which would tend to be
74 // ignored by the one-time initialization of the histogtram_pointer).
75
76 // In some cases (integration into 3rd party code), it's useful to seperate the
77 // definition of |atomic_histogram_poiner| from its use. To achieve this we
78 // define HISTOGRAM_POINTER_USE, which uses an |atomic_histogram_pointer|, and
79 // STATIC_HISTOGRAM_POINTER_BLOCK, which defines an |atomic_histogram_pointer|
80 // and forwards to HISTOGRAM_POINTER_USE.
81 #define HISTOGRAM_POINTER_USE(atomic_histogram_pointer, \
82 constant_histogram_name, \
83 histogram_add_method_invocation, \
84 histogram_factory_get_invocation) \
85 do { \
86 base::HistogramBase* histogram_pointer( \
87 reinterpret_cast<base::HistogramBase*>( \
88 base::subtle::Acquire_Load(atomic_histogram_pointer))); \
89 if (!histogram_pointer) { \
90 histogram_pointer = histogram_factory_get_invocation; \
91 base::subtle::Release_Store( \
92 atomic_histogram_pointer, \
93 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); \
94 } \
95 if (DCHECK_IS_ON()) \
96 histogram_pointer->CheckName(constant_histogram_name); \
97 histogram_pointer->histogram_add_method_invocation; \
98 } while (0)
99
100 // Defines the static |atomic_histogram_pointer| and forwards to
101 // HISTOGRAM_POINTER_USE.
102 #define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \
103 histogram_add_method_invocation, \
104 histogram_factory_get_invocation) \
105 do { \
106 static base::subtle::AtomicWord atomic_histogram_pointer = 0; \
107 HISTOGRAM_POINTER_USE(&atomic_histogram_pointer, constant_histogram_name, \
108 histogram_add_method_invocation, \
109 histogram_factory_get_invocation); \
110 } while (0)
111
112 //------------------------------------------------------------------------------
113 // Provide easy general purpose histogram in a macro, just like stats counters.
114 // Most of these macros use 50 buckets, but check the definition for details.
115 //
116 // All of these macros must be called with |name| as a runtime constant --- it
117 // doesn't have to literally be a constant, but it must be the same string on 21 // doesn't have to literally be a constant, but it must be the same string on
118 // all calls from a particular call site. If this rule is violated, 22 // all calls from a particular call site. If this rule is violated, it is
119 // STATIC_HISTOGRAM_POINTER_BLOCK will DCHECK, and if DCHECKS are disabled, the 23 // possible the data will be written to the wrong histogram.
120 // data will be written to the wrong histogram. 24
121 25 //------------------------------------------------------------------------------
122 #define LOCAL_HISTOGRAM_TIMES(name, sample) LOCAL_HISTOGRAM_CUSTOM_TIMES( \ 26 // Enumeration histograms.
123 name, sample, base::TimeDelta::FromMilliseconds(1), \ 27
28 // These macros create histograms for enumerated data. Ideally, the data should
29 // be of the form of "event occurs, log the result". We recommended not putting
30 // related but not directly connected data as enums within the same histogram.
31 // You should be defining an associated Enum, and the input sample should be
32 // an element of the Enum.
33 // All of these macros must be called with |name| as a runtime constant.
34
35 // Sample usage:
36 // UMA_HISTOGRAM_ENUMERATION("My.Enumeration", VALUE, EVENT_MAX_VALUE);
37 // New Enum values can be added, but existing enums must never be renumbered or
38 // delete and reused. The value in |sample| must be strictly less than
39 // |enum_max|.
40
41 #define UMA_HISTOGRAM_ENUMERATION(name, sample, enum_max) \
42 INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG( \
43 name, sample, enum_max, \
44 base::HistogramBase::kUmaTargetedHistogramFlag)
45
46 // Histogram for boolean values.
47
48 // Sample usage:
49 // UMA_HISTOGRAM_BOOLEAN("Histogram.Boolean", bool);
50 #define UMA_HISTOGRAM_BOOLEAN(name, sample) \
51 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
52 base::BooleanHistogram::FactoryGet(name, \
53 base::HistogramBase::kUmaTargetedHistogramFlag))
54
55 //------------------------------------------------------------------------------
56 // Percentage histograms.
57
58 // Used for capturing basic percentages. This will be 100 buckets of size 1.
59
60 // Sample usage:
61 // UMA_HISTOGRAM_PERCENTAGE("Histogram.Percent", percent_as_int);
62 #define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
63 UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
64
65
66 //------------------------------------------------------------------------------
67 // Count histograms. These are used for collecting numeric data. Note that we
68 // have macros for more specialized use cases below (memory, time, percentages).
69
70 // The number suffixes here refer to the max size of the sample, i.e. COUNT_1000
71 // will be able to collect samples of counts up to 1000. The default number of
72 // buckets in all default macros is 50. We recommend erring on the side of too
73 // large a range versus too short a range.
74 // These macros default to exponential histograms - i.e. the lengths of the
75 // bucket ranges exponentially increase as the sample range increases.
76 // These should *not* be used if you are interested in exact counts, i.e. a
77 // bucket range of 1. In these cases, you should use the ENUMERATION macros
78 // defined later. These should also not be used to capture the number of some
79 // event, i.e. "button X was clicked N times". In this cases, an enum should be
80 // used, ideally with an appropriate baseline enum entry included.
81
82 // Sample usage:
83 // UMA_HISTOGRAM_COUNTS_1M("My.Histogram", sample);
84
85 #define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
86 name, sample, 1, 100, 50)
87
88 #define UMA_HISTOGRAM_COUNTS_1000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
89 name, sample, 1, 1000, 50)
90
91 #define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
92 name, sample, 1, 10000, 50)
93
94 #define UMA_HISTOGRAM_COUNTS_100000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
95 name, sample, 1, 100000, 50)
96
97 #define UMA_HISTOGRAM_COUNTS_1M(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
98 name, sample, 1, 1000000, 50)
99
100 #define UMA_HISTOGRAM_COUNTS_10M(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
101 name, sample, 1, 10000000, 50)
102
103 // This can be used when the default ranges are not sufficient. This macro lets
104 // the metric developer customize the min and max of the sampled range, as well
105 // as the number of buckets recorded.
106 // Any data outside the range here will be put in underflow and overflow
107 // buckets.
Mark P 2016/09/26 23:06:47 I think you should say explicitly that therefore m
rkaplow 2016/09/27 15:08:26 Done.
108
109 // Sample usage:
110 // UMA_HISTOGRAM_CUSTOM_COUNTS("My.Histogram", 1, 100000000, 100);
111 #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
112 INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG( \
113 name, sample, min, max, bucket_count, \
114 base::HistogramBase::kUmaTargetedHistogramFlag)
115
116
117 //------------------------------------------------------------------------------
118 // Timing histograms. These are used for collecting timing data (generally
119 // latencies).
120
121 // These macros create exponentially sized histograms (lengths of the bucket
122 // ranges exponentially increase as the sample range increases). The input
123 // sample is a base::TimeDelta. The output data is measured in ms granularity.
124 // All of these macros must be called with |name| as a runtime constant.
125
126 // Sample usage:
127 // UMA_HISTOGRAM_TIMES("My.Timing.Histogram", time_delta);
128
129 // Short timings - up to 10 seconds.
130 #define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
131 name, sample, base::TimeDelta::FromMilliseconds(1), \
124 base::TimeDelta::FromSeconds(10), 50) 132 base::TimeDelta::FromSeconds(10), 50)
125 133
126 // For folks that need real specific times, use this to select a precise range 134 // Medium timings - up to 3 minutes. Note this starts at 10ms (no good reason,
127 // of times you want plotted, and the number of buckets you want used. 135 // but not worth changing).
128 #define LOCAL_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 136 #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
129 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ 137 name, sample, base::TimeDelta::FromMilliseconds(10), \
130 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \
131 base::HistogramBase::kNoFlags))
132
133 #define LOCAL_HISTOGRAM_COUNTS(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \
134 name, sample, 1, 1000000, 50)
135
136 #define LOCAL_HISTOGRAM_COUNTS_100(name, sample) \
137 LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
138
139 #define LOCAL_HISTOGRAM_COUNTS_10000(name, sample) \
140 LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
141
142 #define LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
143 INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG( \
144 name, sample, min, max, bucket_count, base::HistogramBase::kNoFlags)
145
146 // This is a helper macro used by other macros and shouldn't be used directly.
147 #define INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(name, sample, min, max, \
148 bucket_count, flag) \
149 STATIC_HISTOGRAM_POINTER_BLOCK( \
150 name, Add(sample), \
151 base::Histogram::FactoryGet(name, min, max, bucket_count, flag))
152
153 // This is a helper macro used by other macros and shouldn't be used directly.
154 // One additional bucket is created in the LinearHistogram for the illegal
155 // values >= boundary_value so that mistakes in calling the UMA enumeration
156 // macros can be detected.
157 #define INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \
158 STATIC_HISTOGRAM_POINTER_BLOCK( \
159 name, Add(sample), \
160 base::LinearHistogram::FactoryGet( \
161 name, 1, boundary, boundary + 1, flag))
162
163 #define LOCAL_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
164 LOCAL_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
165
166 #define LOCAL_HISTOGRAM_BOOLEAN(name, sample) \
167 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
168 base::BooleanHistogram::FactoryGet(name, base::Histogram::kNoFlags))
169
170 // Support histograming of an enumerated value. The samples should always be
171 // strictly less than |boundary_value| -- this prevents you from running into
172 // problems down the line if you add additional buckets to the histogram. Note
173 // also that, despite explicitly setting the minimum bucket value to |1| below,
174 // it is fine for enumerated histograms to be 0-indexed -- this is because
175 // enumerated histograms should never have underflow. One additional bucket is
176 // created in the LinearHistogram for the illegal values >= boundary_value so
177 // that mistakes in calling this macro can be detected.
178 #define LOCAL_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
179 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
180 base::LinearHistogram::FactoryGet(name, 1, boundary_value, \
181 boundary_value + 1, base::HistogramBase::kNoFlags))
182
183 // Support histograming of an enumerated value. Samples should be one of the
184 // std::vector<int> list provided via |custom_ranges|. See comments above
185 // CustomRanges::FactoryGet about the requirement of |custom_ranges|.
186 // You can use the helper function CustomHistogram::ArrayToCustomRanges to
187 // transform a C-style array of valid sample values to a std::vector<int>.
188 #define LOCAL_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
189 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
190 base::CustomHistogram::FactoryGet(name, custom_ranges, \
191 base::HistogramBase::kNoFlags))
192
193 #define LOCAL_HISTOGRAM_MEMORY_KB(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \
194 name, sample, 1000, 500000, 50)
195
196 //------------------------------------------------------------------------------
197 // The following macros provide typical usage scenarios for callers that wish
198 // to record histogram data, and have the data submitted/uploaded via UMA.
199 // Not all systems support such UMA, but if they do, the following macros
200 // should work with the service.
201
202 #define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
203 name, sample, base::TimeDelta::FromMilliseconds(1), \
204 base::TimeDelta::FromSeconds(10), 50)
205
206 #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
207 name, sample, base::TimeDelta::FromMilliseconds(10), \
208 base::TimeDelta::FromMinutes(3), 50) 138 base::TimeDelta::FromMinutes(3), 50)
209 139
210 // Use this macro when times can routinely be much longer than 10 seconds. 140 // Long timings - up to an hour.
211 #define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 141 #define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
212 name, sample, base::TimeDelta::FromMilliseconds(1), \ 142 name, sample, base::TimeDelta::FromMilliseconds(1), \
213 base::TimeDelta::FromHours(1), 50) 143 base::TimeDelta::FromHours(1), 50)
214 144
215 // Use this macro when times can routinely be much longer than 10 seconds and 145 // Long timings with higher granularity - up to an hour with 100 buckets.
216 // you want 100 buckets.
217 #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 146 #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
218 name, sample, base::TimeDelta::FromMilliseconds(1), \ 147 name, sample, base::TimeDelta::FromMilliseconds(1), \
219 base::TimeDelta::FromHours(1), 100) 148 base::TimeDelta::FromHours(1), 100)
220 149
221 #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 150 // This can be used when the default ranges are not sufficient. This macro lets
222 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ 151 // the metric developer customize the min and max of the sampled range, as well
223 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ 152 // as the number of buckets recorded.
224 base::HistogramBase::kUmaTargetedHistogramFlag)) 153
225 154 // Sample usage:
226 #define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 155 // UMA_HISTOGRAM_CUSTOM_TIMES("Very.Long.Timing.Histogram", duration_in_ms,
227 name, sample, 1, 1000000, 50) 156 // base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);
228 157 #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
229 #define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 158 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \
230 name, sample, 1, 100, 50) 159 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \
231
232 #define UMA_HISTOGRAM_COUNTS_1000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
233 name, sample, 1, 1000, 50)
234
235 #define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
236 name, sample, 1, 10000, 50)
237
238 #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
239 INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG( \
240 name, sample, min, max, bucket_count, \
241 base::HistogramBase::kUmaTargetedHistogramFlag)
242
243 #define UMA_STABILITY_HISTOGRAM_COUNTS_100(name, sample) \
244 UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
245
246 #define UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, \
247 bucket_count) \
248 INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG( \
249 name, sample, min, max, bucket_count, \
250 base::HistogramBase::kUmaStabilityHistogramFlag)
251
252 #define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
253 name, sample, 1000, 500000, 50)
254
255 #define UMA_HISTOGRAM_MEMORY_MB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
256 name, sample, 1, 1000, 50)
257
258 #define UMA_HISTOGRAM_MEMORY_LARGE_MB(name, sample) \
259 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 64000, 100)
260
261 #define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
262 UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
263
264 #define UMA_HISTOGRAM_BOOLEAN(name, sample) \
265 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
266 base::BooleanHistogram::FactoryGet(name, \
267 base::HistogramBase::kUmaTargetedHistogramFlag))
268
269 // The samples should always be strictly less than |boundary_value|. For more
270 // details, see the comment for the |LOCAL_HISTOGRAM_ENUMERATION| macro, above.
271 #define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
272 INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG( \
273 name, sample, boundary_value, \
274 base::HistogramBase::kUmaTargetedHistogramFlag)
275
276 // Similar to UMA_HISTOGRAM_ENUMERATION, but used for recording stability
277 // histograms. Use this if recording a histogram that should be part of the
278 // initial stability log.
279 #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
280 INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG( \
281 name, sample, boundary_value, \
282 base::HistogramBase::kUmaStabilityHistogramFlag)
283
284 #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
285 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
286 base::CustomHistogram::FactoryGet(name, custom_ranges, \
287 base::HistogramBase::kUmaTargetedHistogramFlag)) 160 base::HistogramBase::kUmaTargetedHistogramFlag))
288 161
289 // Scoped class which logs its time on this earth as a UMA statistic. This is 162 // Scoped class which logs its time on this earth as a UMA statistic. This is
290 // recommended for when you want a histogram which measures the time it takes 163 // recommended for when you want a histogram which measures the time it takes
291 // for a method to execute. This measures up to 10 seconds. 164 // for a method to execute. This measures up to 10 seconds. This uses
Mark P 2016/09/26 23:06:47 nit: second This could be "It"; the sentence will
rkaplow 2016/09/27 15:08:26 Done.
292 #define SCOPED_UMA_HISTOGRAM_TIMER(name) \ 165 // UMA_HISTOGRAM_TIMES under the hood.
293 SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, false, __COUNTER__) 166
167 // Sample usage:
168 // void Function() {
169 // SCOPED_UMA_HISTOGRAM_TIMER("Component.FunctionTime");
170 // ...
171 // }
172 #define SCOPED_UMA_HISTOGRAM_TIMER(name) \
173 INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, false, __COUNTER__)
294 174
295 // Similar scoped histogram timer, but this uses UMA_HISTOGRAM_LONG_TIMES_100, 175 // Similar scoped histogram timer, but this uses UMA_HISTOGRAM_LONG_TIMES_100,
296 // which measures up to an hour, and uses 100 buckets. This is more expensive 176 // which measures up to an hour, and uses 100 buckets. This is more expensive
297 // to store, so only use if this often takes >10 seconds. 177 // to store, so only use if this often takes >10 seconds.
298 #define SCOPED_UMA_HISTOGRAM_LONG_TIMER(name) \ 178 #define SCOPED_UMA_HISTOGRAM_LONG_TIMER(name) \
299 SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, true, __COUNTER__) 179 INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, true, __COUNTER__)
300 180
301 // This nested macro is necessary to expand __COUNTER__ to an actual value. 181
302 #define SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, is_long, key) \ 182 //------------------------------------------------------------------------------
303 SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) 183 // Memory histograms.
304 184
305 #define SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) \ 185 // These macros create exponentially sized histograms (lengths of the bucket
306 class ScopedHistogramTimer##key { \ 186 // ranges exponentially increase as the sample range increases). The input
307 public: \ 187 // sample must be a number measured in kilobytes.
308 ScopedHistogramTimer##key() : constructed_(base::TimeTicks::Now()) {} \ 188 // All of these macros must be called with |name| as a runtime constant.
Mark P 2016/09/26 23:06:47 You forgot this warning in the section on count hi
rkaplow 2016/09/27 15:08:26 Done.
309 ~ScopedHistogramTimer##key() { \ 189
310 base::TimeDelta elapsed = base::TimeTicks::Now() - constructed_; \ 190 // Sample usage:
311 if (is_long) { \ 191 // UMA_HISTOGRAM_MEMORY_KB("My.Memory.Histogram", memory_in_kb);
312 UMA_HISTOGRAM_LONG_TIMES_100(name, elapsed); \ 192
313 } else { \ 193 // Used to measure common KB-granularity memory stats. Range is up to 500000KB -
314 UMA_HISTOGRAM_TIMES(name, elapsed); \ 194 // approximately 500M.
315 } \ 195 #define UMA_HISTOGRAM_MEMORY_KB(name, sample) \
316 } \ 196 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1000, 500000, 50)
317 private: \ 197
318 base::TimeTicks constructed_; \ 198 // Used to measure common MB-granularity memory stats. Range is up to ~64G.
319 } scoped_histogram_timer_##key 199 #define UMA_HISTOGRAM_MEMORY_LARGE_MB(name, sample) \
200 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 64000, 100)
201
202
203 //------------------------------------------------------------------------------
204 // Stability-specific histograms.
205
206 // Histograms logged in as stability histograms will be included in the initial
207 // stability log. See comments by declaration of
208 // MetricsService::PrepareInitialStabilityLog().
209 // All of these macros must be called with |name| as a runtime constant.
210
211 // For details on usage, see the documentation on the non-stability equivalents.
212
213 #define UMA_STABILITY_HISTOGRAM_COUNTS_100(name, sample) \
214 UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
215
216 #define UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, \
217 bucket_count) \
218 INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG( \
219 name, sample, min, max, bucket_count, \
220 base::HistogramBase::kUmaStabilityHistogramFlag)
221
222 #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, enum_max) \
223 INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG( \
224 name, sample, enum_max, \
225 base::HistogramBase::kUmaStabilityHistogramFlag)
226
227
228 //------------------------------------------------------------------------------
229 // Deprecated histogram macros. Not recommended for current use.
230
231 // Legacy name for UMA_HISTOGRAM_COUNTS_1M. Suggest using explicit naming
232 // and not using this macro going forward.
233 #define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
234 name, sample, 1, 1000000, 50)
235
236 // MB-granularity memory metric. This has a short max (1G).
237 #define UMA_HISTOGRAM_MEMORY_MB(name, sample) \
238 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000, 50)
239
240 // For an enum with customized range. In general, sparse histograms should be
241 // used instead.
242 // Samples should be one of the std::vector<int> list provided via
243 // |custom_ranges|. See comments above CustomRanges::FactoryGet about the
244 // requirement of |custom_ranges|. You can use the helper function
245 // CustomHistogram::ArrayToCustomRanges to transform a C-style array of valid
246 // sample values to a std::vector<int>.
247 #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
248 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
249 base::CustomHistogram::FactoryGet(name, custom_ranges, \
250 base::HistogramBase::kUmaTargetedHistogramFlag))
320 251
321 #endif // BASE_METRICS_HISTOGRAM_MACROS_H_ 252 #endif // BASE_METRICS_HISTOGRAM_MACROS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698