xref: /aosp_15_r20/external/libchrome/base/metrics/histogram_macros.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_METRICS_HISTOGRAM_MACROS_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_METRICS_HISTOGRAM_MACROS_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_macros_internal.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_macros_local.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker // Macros for efficient use of histograms.
16*635a8641SAndroid Build Coastguard Worker //
17*635a8641SAndroid Build Coastguard Worker // For best practices on deciding when to emit to a histogram and what form
18*635a8641SAndroid Build Coastguard Worker // the histogram should take, see
19*635a8641SAndroid Build Coastguard Worker // https://chromium.googlesource.com/chromium/src.git/+/HEAD/tools/metrics/histograms/README.md
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker // TODO(rkaplow): Link to proper documentation on metric creation once we have
22*635a8641SAndroid Build Coastguard Worker // it in a good state.
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant - it
25*635a8641SAndroid Build Coastguard Worker // doesn't have to literally be a constant, but it must be the same string on
26*635a8641SAndroid Build Coastguard Worker // all calls from a particular call site. If this rule is violated, it is
27*635a8641SAndroid Build Coastguard Worker // possible the data will be written to the wrong histogram.
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
30*635a8641SAndroid Build Coastguard Worker // Enumeration histograms.
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker // These macros create histograms for enumerated data. Ideally, the data should
33*635a8641SAndroid Build Coastguard Worker // be of the form of "event occurs, log the result". We recommended not putting
34*635a8641SAndroid Build Coastguard Worker // related but not directly connected data as enums within the same histogram.
35*635a8641SAndroid Build Coastguard Worker // You should be defining an associated Enum, and the input sample should be
36*635a8641SAndroid Build Coastguard Worker // an element of the Enum.
37*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker // The first variant of UMA_HISTOGRAM_ENUMERATION accepts two arguments: the
40*635a8641SAndroid Build Coastguard Worker // histogram name and the enum sample. It deduces the correct boundary value to
41*635a8641SAndroid Build Coastguard Worker // use by looking for an enumerator with the name kMaxValue. kMaxValue should
42*635a8641SAndroid Build Coastguard Worker // share the value of the highest enumerator: this avoids switch statements
43*635a8641SAndroid Build Coastguard Worker // having to handle a sentinel no-op value.
44*635a8641SAndroid Build Coastguard Worker //
45*635a8641SAndroid Build Coastguard Worker // Sample usage:
46*635a8641SAndroid Build Coastguard Worker //   // These values are persisted to logs. Entries should not be renumbered and
47*635a8641SAndroid Build Coastguard Worker //   // numeric values should never be reused.
48*635a8641SAndroid Build Coastguard Worker //   enum class MyEnum {
49*635a8641SAndroid Build Coastguard Worker //     kFirstValue = 0,
50*635a8641SAndroid Build Coastguard Worker //     kSecondValue = 1,
51*635a8641SAndroid Build Coastguard Worker //     ...
52*635a8641SAndroid Build Coastguard Worker //     kFinalValue = N,
53*635a8641SAndroid Build Coastguard Worker //     kMaxValue = kFinalValue,
54*635a8641SAndroid Build Coastguard Worker //   };
55*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_ENUMERATION("My.Enumeration", MyEnum::kSomeValue);
56*635a8641SAndroid Build Coastguard Worker //
57*635a8641SAndroid Build Coastguard Worker // The second variant requires three arguments: the first two are the same as
58*635a8641SAndroid Build Coastguard Worker // before, and the third argument is the enum boundary: this must be strictly
59*635a8641SAndroid Build Coastguard Worker // greater than any other enumerator that will be sampled.
60*635a8641SAndroid Build Coastguard Worker //
61*635a8641SAndroid Build Coastguard Worker // Sample usage:
62*635a8641SAndroid Build Coastguard Worker //   // These values are persisted to logs. Entries should not be renumbered and
63*635a8641SAndroid Build Coastguard Worker //   // numeric values should never be reused.
64*635a8641SAndroid Build Coastguard Worker //   enum class MyEnum {
65*635a8641SAndroid Build Coastguard Worker //     FIRST_VALUE = 0,
66*635a8641SAndroid Build Coastguard Worker //     SECOND_VALUE = 1,
67*635a8641SAndroid Build Coastguard Worker //     ...
68*635a8641SAndroid Build Coastguard Worker //     FINAL_VALUE = N,
69*635a8641SAndroid Build Coastguard Worker //     COUNT
70*635a8641SAndroid Build Coastguard Worker //   };
71*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_ENUMERATION("My.Enumeration",
72*635a8641SAndroid Build Coastguard Worker //                             MyEnum::SOME_VALUE, MyEnum::COUNT);
73*635a8641SAndroid Build Coastguard Worker //
74*635a8641SAndroid Build Coastguard Worker // Note: If the enum is used in a switch, it is often desirable to avoid writing
75*635a8641SAndroid Build Coastguard Worker // a case statement to handle an unused sentinel value (i.e. COUNT in the above
76*635a8641SAndroid Build Coastguard Worker // example). For scoped enums, this is awkward since it requires casting the
77*635a8641SAndroid Build Coastguard Worker // enum to an arithmetic type and adding one. Instead, prefer the two argument
78*635a8641SAndroid Build Coastguard Worker // version of the macro which automatically deduces the boundary from kMaxValue.
79*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_ENUMERATION(name, ...)                            \
80*635a8641SAndroid Build Coastguard Worker   CR_EXPAND_ARG(INTERNAL_UMA_HISTOGRAM_ENUMERATION_GET_MACRO(           \
81*635a8641SAndroid Build Coastguard Worker       __VA_ARGS__, INTERNAL_UMA_HISTOGRAM_ENUMERATION_SPECIFY_BOUNDARY, \
82*635a8641SAndroid Build Coastguard Worker       INTERNAL_UMA_HISTOGRAM_ENUMERATION_DEDUCE_BOUNDARY)(              \
83*635a8641SAndroid Build Coastguard Worker       name, __VA_ARGS__, base::HistogramBase::kUmaTargetedHistogramFlag))
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker // As above but "scaled" count to avoid overflows caused by increments of
86*635a8641SAndroid Build Coastguard Worker // large amounts. See UMA_HISTOGRAM_SCALED_EXACT_LINEAR for more information.
87*635a8641SAndroid Build Coastguard Worker // Only the new format utilizing an internal kMaxValue is supported.
88*635a8641SAndroid Build Coastguard Worker // It'll be necessary to #include "base/lazy_instance.h" to use this macro.
89*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_SCALED_ENUMERATION(name, sample, count, scale) \
90*635a8641SAndroid Build Coastguard Worker   INTERNAL_HISTOGRAM_SCALED_ENUMERATION_WITH_FLAG(                   \
91*635a8641SAndroid Build Coastguard Worker       name, sample, count, scale,                                    \
92*635a8641SAndroid Build Coastguard Worker       base::HistogramBase::kUmaTargetedHistogramFlag)
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker // Histogram for boolean values.
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker // Sample usage:
97*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_BOOLEAN("Histogram.Boolean", bool);
98*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_BOOLEAN(name, sample)                                    \
99*635a8641SAndroid Build Coastguard Worker     STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample),                   \
100*635a8641SAndroid Build Coastguard Worker         base::BooleanHistogram::FactoryGet(name,                               \
101*635a8641SAndroid Build Coastguard Worker             base::HistogramBase::kUmaTargetedHistogramFlag))
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
104*635a8641SAndroid Build Coastguard Worker // Linear histograms.
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
107*635a8641SAndroid Build Coastguard Worker 
108*635a8641SAndroid Build Coastguard Worker // Used for capturing integer data with a linear bucketing scheme. This can be
109*635a8641SAndroid Build Coastguard Worker // used when you want the exact value of some small numeric count, with a max of
110*635a8641SAndroid Build Coastguard Worker // 100 or less. If you need to capture a range of greater than 100, we recommend
111*635a8641SAndroid Build Coastguard Worker // the use of the COUNT histograms below.
112*635a8641SAndroid Build Coastguard Worker 
113*635a8641SAndroid Build Coastguard Worker // Sample usage:
114*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_EXACT_LINEAR("Histogram.Linear", count, 10);
115*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_EXACT_LINEAR(name, sample, value_max) \
116*635a8641SAndroid Build Coastguard Worker   INTERNAL_HISTOGRAM_EXACT_LINEAR_WITH_FLAG(                \
117*635a8641SAndroid Build Coastguard Worker       name, sample, value_max, base::HistogramBase::kUmaTargetedHistogramFlag)
118*635a8641SAndroid Build Coastguard Worker 
119*635a8641SAndroid Build Coastguard Worker // Used for capturing basic percentages. This will be 100 buckets of size 1.
120*635a8641SAndroid Build Coastguard Worker 
121*635a8641SAndroid Build Coastguard Worker // Sample usage:
122*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_PERCENTAGE("Histogram.Percent", percent_as_int);
123*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_PERCENTAGE(name, percent_as_int) \
124*635a8641SAndroid Build Coastguard Worker   UMA_HISTOGRAM_EXACT_LINEAR(name, percent_as_int, 101)
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
127*635a8641SAndroid Build Coastguard Worker // Scaled Linear histograms.
128*635a8641SAndroid Build Coastguard Worker 
129*635a8641SAndroid Build Coastguard Worker // These take |count| and |scale| parameters to allow cumulative reporting of
130*635a8641SAndroid Build Coastguard Worker // large numbers. Only the scaled count is reported but the reminder is kept so
131*635a8641SAndroid Build Coastguard Worker // multiple calls will accumulate correctly.  Only "exact linear" is supported.
132*635a8641SAndroid Build Coastguard Worker // It'll be necessary to #include "base/lazy_instance.h" to use this macro.
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_SCALED_EXACT_LINEAR(name, sample, count, value_max, \
135*635a8641SAndroid Build Coastguard Worker                                           scale)                          \
136*635a8641SAndroid Build Coastguard Worker   INTERNAL_HISTOGRAM_SCALED_EXACT_LINEAR_WITH_FLAG(                       \
137*635a8641SAndroid Build Coastguard Worker       name, sample, count, value_max, scale,                              \
138*635a8641SAndroid Build Coastguard Worker       base::HistogramBase::kUmaTargetedHistogramFlag)
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
141*635a8641SAndroid Build Coastguard Worker // Count histograms. These are used for collecting numeric data. Note that we
142*635a8641SAndroid Build Coastguard Worker // have macros for more specialized use cases below (memory, time, percentages).
143*635a8641SAndroid Build Coastguard Worker 
144*635a8641SAndroid Build Coastguard Worker // The number suffixes here refer to the max size of the sample, i.e. COUNT_1000
145*635a8641SAndroid Build Coastguard Worker // will be able to collect samples of counts up to 1000. The default number of
146*635a8641SAndroid Build Coastguard Worker // buckets in all default macros is 50. We recommend erring on the side of too
147*635a8641SAndroid Build Coastguard Worker // large a range versus too short a range.
148*635a8641SAndroid Build Coastguard Worker // These macros default to exponential histograms - i.e. the lengths of the
149*635a8641SAndroid Build Coastguard Worker // bucket ranges exponentially increase as the sample range increases.
150*635a8641SAndroid Build Coastguard Worker // These should *not* be used if you are interested in exact counts, i.e. a
151*635a8641SAndroid Build Coastguard Worker // bucket range of 1. In these cases, you should use the ENUMERATION macros
152*635a8641SAndroid Build Coastguard Worker // defined later. These should also not be used to capture the number of some
153*635a8641SAndroid Build Coastguard Worker // event, i.e. "button X was clicked N times". In this cases, an enum should be
154*635a8641SAndroid Build Coastguard Worker // used, ideally with an appropriate baseline enum entry included.
155*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker // Sample usage:
158*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_COUNTS_1M("My.Histogram", sample);
159*635a8641SAndroid Build Coastguard Worker 
160*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(    \
161*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 100, 50)
162*635a8641SAndroid Build Coastguard Worker 
163*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_1000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(   \
164*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 1000, 50)
165*635a8641SAndroid Build Coastguard Worker 
166*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(  \
167*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 10000, 50)
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_100000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
170*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 100000, 50)
171*635a8641SAndroid Build Coastguard Worker 
172*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_1M(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(     \
173*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 1000000, 50)
174*635a8641SAndroid Build Coastguard Worker 
175*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS_10M(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(    \
176*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 10000000, 50)
177*635a8641SAndroid Build Coastguard Worker 
178*635a8641SAndroid Build Coastguard Worker // This can be used when the default ranges are not sufficient. This macro lets
179*635a8641SAndroid Build Coastguard Worker // the metric developer customize the min and max of the sampled range, as well
180*635a8641SAndroid Build Coastguard Worker // as the number of buckets recorded.
181*635a8641SAndroid Build Coastguard Worker // Any data outside the range here will be put in underflow and overflow
182*635a8641SAndroid Build Coastguard Worker // buckets. Min values should be >=1 as emitted 0s will still go into the
183*635a8641SAndroid Build Coastguard Worker // underflow bucket.
184*635a8641SAndroid Build Coastguard Worker 
185*635a8641SAndroid Build Coastguard Worker // Sample usage:
186*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_CUSTOM_COUNTS("My.Histogram", 1, 100000000, 100);
187*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count)      \
188*635a8641SAndroid Build Coastguard Worker     INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(                                \
189*635a8641SAndroid Build Coastguard Worker         name, sample, min, max, bucket_count,                                  \
190*635a8641SAndroid Build Coastguard Worker         base::HistogramBase::kUmaTargetedHistogramFlag)
191*635a8641SAndroid Build Coastguard Worker 
192*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
193*635a8641SAndroid Build Coastguard Worker // Timing histograms. These are used for collecting timing data (generally
194*635a8641SAndroid Build Coastguard Worker // latencies).
195*635a8641SAndroid Build Coastguard Worker 
196*635a8641SAndroid Build Coastguard Worker // These macros create exponentially sized histograms (lengths of the bucket
197*635a8641SAndroid Build Coastguard Worker // ranges exponentially increase as the sample range increases). The input
198*635a8641SAndroid Build Coastguard Worker // sample is a base::TimeDelta. The output data is measured in ms granularity.
199*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
200*635a8641SAndroid Build Coastguard Worker 
201*635a8641SAndroid Build Coastguard Worker // Sample usage:
202*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_TIMES("My.Timing.Histogram", time_delta);
203*635a8641SAndroid Build Coastguard Worker 
204*635a8641SAndroid Build Coastguard Worker // Short timings - up to 10 seconds. For high-resolution (microseconds) timings,
205*635a8641SAndroid Build Coastguard Worker // see UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES.
206*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES(          \
207*635a8641SAndroid Build Coastguard Worker     name, sample, base::TimeDelta::FromMilliseconds(1),                        \
208*635a8641SAndroid Build Coastguard Worker     base::TimeDelta::FromSeconds(10), 50)
209*635a8641SAndroid Build Coastguard Worker 
210*635a8641SAndroid Build Coastguard Worker // Medium timings - up to 3 minutes. Note this starts at 10ms (no good reason,
211*635a8641SAndroid Build Coastguard Worker // but not worth changing).
212*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES(   \
213*635a8641SAndroid Build Coastguard Worker     name, sample, base::TimeDelta::FromMilliseconds(10),                       \
214*635a8641SAndroid Build Coastguard Worker     base::TimeDelta::FromMinutes(3), 50)
215*635a8641SAndroid Build Coastguard Worker 
216*635a8641SAndroid Build Coastguard Worker // Long timings - up to an hour.
217*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES(     \
218*635a8641SAndroid Build Coastguard Worker     name, sample, base::TimeDelta::FromMilliseconds(1),                        \
219*635a8641SAndroid Build Coastguard Worker     base::TimeDelta::FromHours(1), 50)
220*635a8641SAndroid Build Coastguard Worker 
221*635a8641SAndroid Build Coastguard Worker // Long timings with higher granularity - up to an hour with 100 buckets.
222*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
223*635a8641SAndroid Build Coastguard Worker     name, sample, base::TimeDelta::FromMilliseconds(1),                        \
224*635a8641SAndroid Build Coastguard Worker     base::TimeDelta::FromHours(1), 100)
225*635a8641SAndroid Build Coastguard Worker 
226*635a8641SAndroid Build Coastguard Worker // This can be used when the default ranges are not sufficient. This macro lets
227*635a8641SAndroid Build Coastguard Worker // the metric developer customize the min and max of the sampled range, as well
228*635a8641SAndroid Build Coastguard Worker // as the number of buckets recorded.
229*635a8641SAndroid Build Coastguard Worker 
230*635a8641SAndroid Build Coastguard Worker // Sample usage:
231*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_CUSTOM_TIMES("Very.Long.Timing.Histogram", time_delta,
232*635a8641SAndroid Build Coastguard Worker //       base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);
233*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
234*635a8641SAndroid Build Coastguard Worker   STATIC_HISTOGRAM_POINTER_BLOCK(                                        \
235*635a8641SAndroid Build Coastguard Worker       name, AddTimeMillisecondsGranularity(sample),                      \
236*635a8641SAndroid Build Coastguard Worker       base::Histogram::FactoryTimeGet(                                   \
237*635a8641SAndroid Build Coastguard Worker           name, min, max, bucket_count,                                  \
238*635a8641SAndroid Build Coastguard Worker           base::HistogramBase::kUmaTargetedHistogramFlag))
239*635a8641SAndroid Build Coastguard Worker 
240*635a8641SAndroid Build Coastguard Worker // Same as UMA_HISTOGRAM_CUSTOM_TIMES but reports |sample| in microseconds,
241*635a8641SAndroid Build Coastguard Worker // dropping the report if this client doesn't have a high-resolution clock.
242*635a8641SAndroid Build Coastguard Worker //
243*635a8641SAndroid Build Coastguard Worker // Note: dropping reports on clients with low-resolution clocks means these
244*635a8641SAndroid Build Coastguard Worker // reports will be biased to a portion of the population on Windows. See
245*635a8641SAndroid Build Coastguard Worker // Windows.HasHighResolutionTimeTicks for the affected sample.
246*635a8641SAndroid Build Coastguard Worker //
247*635a8641SAndroid Build Coastguard Worker // Sample usage:
248*635a8641SAndroid Build Coastguard Worker //  UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
249*635a8641SAndroid Build Coastguard Worker //      "High.Resolution.TimingMicroseconds.Histogram", time_delta,
250*635a8641SAndroid Build Coastguard Worker //      base::TimeDelta::FromMicroseconds(1),
251*635a8641SAndroid Build Coastguard Worker //      base::TimeDelta::FromMilliseconds(10), 100);
252*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(name, sample, min, max, \
253*635a8641SAndroid Build Coastguard Worker                                                 bucket_count)           \
254*635a8641SAndroid Build Coastguard Worker   STATIC_HISTOGRAM_POINTER_BLOCK(                                       \
255*635a8641SAndroid Build Coastguard Worker       name, AddTimeMicrosecondsGranularity(sample),                     \
256*635a8641SAndroid Build Coastguard Worker       base::Histogram::FactoryMicrosecondsTimeGet(                      \
257*635a8641SAndroid Build Coastguard Worker           name, min, max, bucket_count,                                 \
258*635a8641SAndroid Build Coastguard Worker           base::HistogramBase::kUmaTargetedHistogramFlag))
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker // Scoped class which logs its time on this earth as a UMA statistic. This is
261*635a8641SAndroid Build Coastguard Worker // recommended for when you want a histogram which measures the time it takes
262*635a8641SAndroid Build Coastguard Worker // for a method to execute. This measures up to 10 seconds. It uses
263*635a8641SAndroid Build Coastguard Worker // UMA_HISTOGRAM_TIMES under the hood.
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker // Sample usage:
266*635a8641SAndroid Build Coastguard Worker //   void Function() {
267*635a8641SAndroid Build Coastguard Worker //     SCOPED_UMA_HISTOGRAM_TIMER("Component.FunctionTime");
268*635a8641SAndroid Build Coastguard Worker //     ...
269*635a8641SAndroid Build Coastguard Worker //   }
270*635a8641SAndroid Build Coastguard Worker #define SCOPED_UMA_HISTOGRAM_TIMER(name)                                       \
271*635a8641SAndroid Build Coastguard Worker   INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, false, __COUNTER__)
272*635a8641SAndroid Build Coastguard Worker 
273*635a8641SAndroid Build Coastguard Worker // Similar scoped histogram timer, but this uses UMA_HISTOGRAM_LONG_TIMES_100,
274*635a8641SAndroid Build Coastguard Worker // which measures up to an hour, and uses 100 buckets. This is more expensive
275*635a8641SAndroid Build Coastguard Worker // to store, so only use if this often takes >10 seconds.
276*635a8641SAndroid Build Coastguard Worker #define SCOPED_UMA_HISTOGRAM_LONG_TIMER(name)                                  \
277*635a8641SAndroid Build Coastguard Worker   INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, true, __COUNTER__)
278*635a8641SAndroid Build Coastguard Worker 
279*635a8641SAndroid Build Coastguard Worker 
280*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
281*635a8641SAndroid Build Coastguard Worker // Memory histograms.
282*635a8641SAndroid Build Coastguard Worker 
283*635a8641SAndroid Build Coastguard Worker // These macros create exponentially sized histograms (lengths of the bucket
284*635a8641SAndroid Build Coastguard Worker // ranges exponentially increase as the sample range increases). The input
285*635a8641SAndroid Build Coastguard Worker // sample must be a number measured in kilobytes.
286*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
287*635a8641SAndroid Build Coastguard Worker 
288*635a8641SAndroid Build Coastguard Worker // Sample usage:
289*635a8641SAndroid Build Coastguard Worker //   UMA_HISTOGRAM_MEMORY_KB("My.Memory.Histogram", memory_in_kb);
290*635a8641SAndroid Build Coastguard Worker 
291*635a8641SAndroid Build Coastguard Worker // Used to measure common KB-granularity memory stats. Range is up to 500000KB -
292*635a8641SAndroid Build Coastguard Worker // approximately 500M.
293*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_MEMORY_KB(name, sample)                                  \
294*635a8641SAndroid Build Coastguard Worker     UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1000, 500000, 50)
295*635a8641SAndroid Build Coastguard Worker 
296*635a8641SAndroid Build Coastguard Worker // Used to measure common MB-granularity memory stats. Range is up to ~64G.
297*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_MEMORY_LARGE_MB(name, sample)                            \
298*635a8641SAndroid Build Coastguard Worker     UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 64000, 100)
299*635a8641SAndroid Build Coastguard Worker 
300*635a8641SAndroid Build Coastguard Worker 
301*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
302*635a8641SAndroid Build Coastguard Worker // Stability-specific histograms.
303*635a8641SAndroid Build Coastguard Worker 
304*635a8641SAndroid Build Coastguard Worker // Histograms logged in as stability histograms will be included in the initial
305*635a8641SAndroid Build Coastguard Worker // stability log. See comments by declaration of
306*635a8641SAndroid Build Coastguard Worker // MetricsService::PrepareInitialStabilityLog().
307*635a8641SAndroid Build Coastguard Worker // All of these macros must be called with |name| as a runtime constant.
308*635a8641SAndroid Build Coastguard Worker 
309*635a8641SAndroid Build Coastguard Worker // For details on usage, see the documentation on the non-stability equivalents.
310*635a8641SAndroid Build Coastguard Worker 
311*635a8641SAndroid Build Coastguard Worker #define UMA_STABILITY_HISTOGRAM_COUNTS_100(name, sample)                       \
312*635a8641SAndroid Build Coastguard Worker     UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
313*635a8641SAndroid Build Coastguard Worker 
314*635a8641SAndroid Build Coastguard Worker #define UMA_STABILITY_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max,          \
315*635a8641SAndroid Build Coastguard Worker                                               bucket_count)                    \
316*635a8641SAndroid Build Coastguard Worker     INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(                                \
317*635a8641SAndroid Build Coastguard Worker         name, sample, min, max, bucket_count,                                  \
318*635a8641SAndroid Build Coastguard Worker         base::HistogramBase::kUmaStabilityHistogramFlag)
319*635a8641SAndroid Build Coastguard Worker 
320*635a8641SAndroid Build Coastguard Worker #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, enum_max)            \
321*635a8641SAndroid Build Coastguard Worker     INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(                                  \
322*635a8641SAndroid Build Coastguard Worker         name, sample, enum_max,                                                \
323*635a8641SAndroid Build Coastguard Worker         base::HistogramBase::kUmaStabilityHistogramFlag)
324*635a8641SAndroid Build Coastguard Worker 
325*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
326*635a8641SAndroid Build Coastguard Worker // Histogram instantiation helpers.
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker // Support a collection of histograms, perhaps one for each entry in an
329*635a8641SAndroid Build Coastguard Worker // enumeration. This macro manages a block of pointers, adding to a specific
330*635a8641SAndroid Build Coastguard Worker // one by its index.
331*635a8641SAndroid Build Coastguard Worker //
332*635a8641SAndroid Build Coastguard Worker // A typical instantiation looks something like this:
333*635a8641SAndroid Build Coastguard Worker //  STATIC_HISTOGRAM_POINTER_GROUP(
334*635a8641SAndroid Build Coastguard Worker //      GetHistogramNameForIndex(histogram_index),
335*635a8641SAndroid Build Coastguard Worker //      histogram_index, MAXIMUM_HISTOGRAM_INDEX, Add(some_delta),
336*635a8641SAndroid Build Coastguard Worker //      base::Histogram::FactoryGet(
337*635a8641SAndroid Build Coastguard Worker //          GetHistogramNameForIndex(histogram_index),
338*635a8641SAndroid Build Coastguard Worker //          MINIMUM_SAMPLE, MAXIMUM_SAMPLE, BUCKET_COUNT,
339*635a8641SAndroid Build Coastguard Worker //          base::HistogramBase::kUmaTargetedHistogramFlag));
340*635a8641SAndroid Build Coastguard Worker //
341*635a8641SAndroid Build Coastguard Worker // Though it seems inefficient to generate the name twice, the first
342*635a8641SAndroid Build Coastguard Worker // instance will be used only for DCHECK builds and the second will
343*635a8641SAndroid Build Coastguard Worker // execute only during the first access to the given index, after which
344*635a8641SAndroid Build Coastguard Worker // the pointer is cached and the name never needed again.
345*635a8641SAndroid Build Coastguard Worker #define STATIC_HISTOGRAM_POINTER_GROUP(constant_histogram_name, index,        \
346*635a8641SAndroid Build Coastguard Worker                                        constant_maximum,                      \
347*635a8641SAndroid Build Coastguard Worker                                        histogram_add_method_invocation,       \
348*635a8641SAndroid Build Coastguard Worker                                        histogram_factory_get_invocation)      \
349*635a8641SAndroid Build Coastguard Worker   do {                                                                        \
350*635a8641SAndroid Build Coastguard Worker     static base::subtle::AtomicWord atomic_histograms[constant_maximum];      \
351*635a8641SAndroid Build Coastguard Worker     DCHECK_LE(0, index);                                                      \
352*635a8641SAndroid Build Coastguard Worker     DCHECK_LT(index, constant_maximum);                                       \
353*635a8641SAndroid Build Coastguard Worker     HISTOGRAM_POINTER_USE(&atomic_histograms[index], constant_histogram_name, \
354*635a8641SAndroid Build Coastguard Worker                           histogram_add_method_invocation,                    \
355*635a8641SAndroid Build Coastguard Worker                           histogram_factory_get_invocation);                  \
356*635a8641SAndroid Build Coastguard Worker   } while (0)
357*635a8641SAndroid Build Coastguard Worker 
358*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
359*635a8641SAndroid Build Coastguard Worker // Deprecated histogram macros. Not recommended for current use.
360*635a8641SAndroid Build Coastguard Worker 
361*635a8641SAndroid Build Coastguard Worker // Legacy name for UMA_HISTOGRAM_COUNTS_1M. Suggest using explicit naming
362*635a8641SAndroid Build Coastguard Worker // and not using this macro going forward.
363*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS(        \
364*635a8641SAndroid Build Coastguard Worker     name, sample, 1, 1000000, 50)
365*635a8641SAndroid Build Coastguard Worker 
366*635a8641SAndroid Build Coastguard Worker // MB-granularity memory metric. This has a short max (1G).
367*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_MEMORY_MB(name, sample)                                  \
368*635a8641SAndroid Build Coastguard Worker     UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000, 50)
369*635a8641SAndroid Build Coastguard Worker 
370*635a8641SAndroid Build Coastguard Worker // For an enum with customized range. In general, sparse histograms should be
371*635a8641SAndroid Build Coastguard Worker // used instead.
372*635a8641SAndroid Build Coastguard Worker // Samples should be one of the std::vector<int> list provided via
373*635a8641SAndroid Build Coastguard Worker // |custom_ranges|. See comments above CustomRanges::FactoryGet about the
374*635a8641SAndroid Build Coastguard Worker // requirement of |custom_ranges|. You can use the helper function
375*635a8641SAndroid Build Coastguard Worker // CustomHistogram::ArrayToCustomEnumRanges to transform a C-style array of
376*635a8641SAndroid Build Coastguard Worker // valid sample values to a std::vector<int>.
377*635a8641SAndroid Build Coastguard Worker #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges)          \
378*635a8641SAndroid Build Coastguard Worker     STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample),                          \
379*635a8641SAndroid Build Coastguard Worker         base::CustomHistogram::FactoryGet(name, custom_ranges,                 \
380*635a8641SAndroid Build Coastguard Worker             base::HistogramBase::kUmaTargetedHistogramFlag))
381*635a8641SAndroid Build Coastguard Worker 
382*635a8641SAndroid Build Coastguard Worker #endif  // BASE_METRICS_HISTOGRAM_MACROS_H_
383