1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
6 #define BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
7
8 #include <string>
9 #include <type_traits>
10
11 #include "base/base_export.h"
12 #include "base/check_op.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/histogram_base.h"
15 #include "base/time/time.h"
16
17 // TODO(crbug/1265443): Update this file's function comments to provide more
18 // detail, like histogram_macros.h.
19 //
20 // Functions for recording metrics.
21 //
22 // For best practices on deciding when to emit to a histogram and what form
23 // the histogram should take, see
24 // https://chromium.googlesource.com/chromium/src.git/+/HEAD/tools/metrics/histograms/README.md
25 //
26 // For deciding whether to use the function or macro APIs, see
27 // https://chromium.googlesource.com/chromium/src/+/HEAD/tools/metrics/histograms/README.md#coding-emitting-to-histograms"
28 //
29 // Every function is duplicated to take both std::string and char* for the name.
30 // This avoids ctor/dtor instantiation for constant strings to std::string,
31 // which makes the call be larger than caching macros (which do accept char*)
32 // in those cases.
33 namespace base {
34
35 // For numeric measurements where you want exact integer values up to
36 // |exclusive_max|. |exclusive_max| itself is included in the overflow bucket.
37 // Therefore, if you want an accurate measure up to kMax, then |exclusive_max|
38 // should be set to kMax + 1.
39 //
40 // |exclusive_max| should be 101 or less. If you need to capture a larger range,
41 // we recommend the use of the COUNT histograms below.
42 //
43 // Sample usage:
44 // base::UmaHistogramExactLinear("Histogram.Linear", sample, kMax + 1);
45 // In this case, buckets are 1, 2, .., kMax, kMax+1, where the kMax+1 bucket
46 // captures everything kMax+1 and above.
47 BASE_EXPORT void UmaHistogramExactLinear(const std::string& name,
48 int sample,
49 int exclusive_max);
50 BASE_EXPORT void UmaHistogramExactLinear(const char* name,
51 int sample,
52 int exclusive_max);
53
54 // For adding a sample to an enumerated histogram.
55 // Sample usage:
56 // // These values are persisted to logs. Entries should not be renumbered and
57 // // numeric values should never be reused.
58 // enum class NewTabPageAction {
59 // kUseOmnibox = 0,
60 // kClickTitle = 1,
61 // // kUseSearchbox = 2, // no longer used, combined into omnibox
62 // kOpenBookmark = 3,
63 // kMaxValue = kOpenBookmark,
64 // };
65 // base::UmaHistogramEnumeration("My.Enumeration",
66 // NewTabPageAction::kClickTitle);
67 //
68 // Note that there are code that refer implementation details of this function.
69 // Keep them synchronized.
70 template <typename T>
UmaHistogramEnumeration(const std::string & name,T sample)71 void UmaHistogramEnumeration(const std::string& name, T sample) {
72 static_assert(std::is_enum_v<T>, "T is not an enum.");
73 // This also ensures that an enumeration that doesn't define kMaxValue fails
74 // with a semi-useful error ("no member named 'kMaxValue' in ...").
75 static_assert(static_cast<uintmax_t>(T::kMaxValue) <=
76 static_cast<uintmax_t>(INT_MAX) - 1,
77 "Enumeration's kMaxValue is out of range of INT_MAX!");
78 DCHECK_LE(static_cast<uintmax_t>(sample),
79 static_cast<uintmax_t>(T::kMaxValue));
80 return UmaHistogramExactLinear(name, static_cast<int>(sample),
81 static_cast<int>(T::kMaxValue) + 1);
82 }
83
84 template <typename T>
UmaHistogramEnumeration(const char * name,T sample)85 void UmaHistogramEnumeration(const char* name, T sample) {
86 static_assert(std::is_enum_v<T>, "T is not an enum.");
87 // This also ensures that an enumeration that doesn't define kMaxValue fails
88 // with a semi-useful error ("no member named 'kMaxValue' in ...").
89 static_assert(static_cast<uintmax_t>(T::kMaxValue) <=
90 static_cast<uintmax_t>(INT_MAX) - 1,
91 "Enumeration's kMaxValue is out of range of INT_MAX!");
92 DCHECK_LE(static_cast<uintmax_t>(sample),
93 static_cast<uintmax_t>(T::kMaxValue));
94 return UmaHistogramExactLinear(name, static_cast<int>(sample),
95 static_cast<int>(T::kMaxValue) + 1);
96 }
97
98 // Some legacy histograms may manually specify the enum size, with a kCount,
99 // COUNT, kMaxValue, or MAX_VALUE sentinel like so:
100 // // These values are persisted to logs. Entries should not be renumbered and
101 // // numeric values should never be reused.
102 // enum class NewTabPageAction {
103 // kUseOmnibox = 0,
104 // kClickTitle = 1,
105 // // kUseSearchbox = 2, // no longer used, combined into omnibox
106 // kOpenBookmark = 3,
107 // kCount,
108 // };
109 // base::UmaHistogramEnumeration("My.Enumeration",
110 // NewTabPageAction::kClickTitle,
111 // kCount);
112 // Note: The value in |sample| must be strictly less than |enum_size|. This is
113 // otherwise functionally equivalent to the above.
114 template <typename T>
UmaHistogramEnumeration(const std::string & name,T sample,T enum_size)115 void UmaHistogramEnumeration(const std::string& name, T sample, T enum_size) {
116 static_assert(std::is_enum_v<T>, "T is not an enum.");
117 DCHECK_LE(static_cast<uintmax_t>(enum_size), static_cast<uintmax_t>(INT_MAX));
118 DCHECK_LT(static_cast<uintmax_t>(sample), static_cast<uintmax_t>(enum_size));
119 return UmaHistogramExactLinear(name, static_cast<int>(sample),
120 static_cast<int>(enum_size));
121 }
122
123 template <typename T>
UmaHistogramEnumeration(const char * name,T sample,T enum_size)124 void UmaHistogramEnumeration(const char* name, T sample, T enum_size) {
125 static_assert(std::is_enum_v<T>, "T is not an enum.");
126 DCHECK_LE(static_cast<uintmax_t>(enum_size), static_cast<uintmax_t>(INT_MAX));
127 DCHECK_LT(static_cast<uintmax_t>(sample), static_cast<uintmax_t>(enum_size));
128 return UmaHistogramExactLinear(name, static_cast<int>(sample),
129 static_cast<int>(enum_size));
130 }
131
132 // For adding boolean sample to histogram.
133 // Sample usage:
134 // base::UmaHistogramBoolean("My.Boolean", true)
135 BASE_EXPORT void UmaHistogramBoolean(const std::string& name, bool sample);
136 BASE_EXPORT void UmaHistogramBoolean(const char* name, bool sample);
137
138 // For adding histogram sample denoting a percentage.
139 // Percents are integers between 1 and 100, inclusively.
140 // Sample usage:
141 // base::UmaHistogramPercentage("My.Percent", 69)
142 BASE_EXPORT void UmaHistogramPercentage(const std::string& name, int percent);
143 BASE_EXPORT void UmaHistogramPercentage(const char* name, int percent);
144
145 // Obsolete. Use |UmaHistogramPercentage| instead. See crbug/1121318.
146 BASE_EXPORT void UmaHistogramPercentageObsoleteDoNotUse(const std::string& name,
147 int percent);
148 BASE_EXPORT void UmaHistogramPercentageObsoleteDoNotUse(const char* name,
149 int percent);
150
151 // For adding counts histogram.
152 // Sample usage:
153 // base::UmaHistogramCustomCounts("My.Counts", some_value, 1, 600, 30)
154 BASE_EXPORT void UmaHistogramCustomCounts(const std::string& name,
155 int sample,
156 int min,
157 int exclusive_max,
158 size_t buckets);
159 BASE_EXPORT void UmaHistogramCustomCounts(const char* name,
160 int sample,
161 int min,
162 int exclusive_max,
163 size_t buckets);
164
165 // Counts specialization for maximum counts 100, 1000, 10k, 100k, 1M and 10M.
166 BASE_EXPORT void UmaHistogramCounts100(const std::string& name, int sample);
167 BASE_EXPORT void UmaHistogramCounts100(const char* name, int sample);
168 BASE_EXPORT void UmaHistogramCounts1000(const std::string& name, int sample);
169 BASE_EXPORT void UmaHistogramCounts1000(const char* name, int sample);
170 BASE_EXPORT void UmaHistogramCounts10000(const std::string& name, int sample);
171 BASE_EXPORT void UmaHistogramCounts10000(const char* name, int sample);
172 BASE_EXPORT void UmaHistogramCounts100000(const std::string& name, int sample);
173 BASE_EXPORT void UmaHistogramCounts100000(const char* name, int sample);
174 BASE_EXPORT void UmaHistogramCounts1M(const std::string& name, int sample);
175 BASE_EXPORT void UmaHistogramCounts1M(const char* name, int sample);
176 BASE_EXPORT void UmaHistogramCounts10M(const std::string& name, int sample);
177 BASE_EXPORT void UmaHistogramCounts10M(const char* name, int sample);
178
179 // For histograms storing times. It uses milliseconds granularity.
180 BASE_EXPORT void UmaHistogramCustomTimes(const std::string& name,
181 TimeDelta sample,
182 TimeDelta min,
183 TimeDelta max,
184 size_t buckets);
185 BASE_EXPORT void UmaHistogramCustomTimes(const char* name,
186 TimeDelta sample,
187 TimeDelta min,
188 TimeDelta max,
189 size_t buckets);
190 // For short timings from 1 ms up to 10 seconds (50 buckets).
191 BASE_EXPORT void UmaHistogramTimes(const std::string& name, TimeDelta sample);
192 BASE_EXPORT void UmaHistogramTimes(const char* name, TimeDelta sample);
193 // For medium timings up to 3 minutes (50 buckets).
194 BASE_EXPORT void UmaHistogramMediumTimes(const std::string& name,
195 TimeDelta sample);
196 BASE_EXPORT void UmaHistogramMediumTimes(const char* name, TimeDelta sample);
197 // For time intervals up to 1 hr (50 buckets).
198 BASE_EXPORT void UmaHistogramLongTimes(const std::string& name,
199 TimeDelta sample);
200 BASE_EXPORT void UmaHistogramLongTimes(const char* name, TimeDelta sample);
201
202 // For time intervals up to 1 hr (100 buckets).
203 BASE_EXPORT void UmaHistogramLongTimes100(const std::string& name,
204 TimeDelta sample);
205 BASE_EXPORT void UmaHistogramLongTimes100(const char* name, TimeDelta sample);
206
207 // For histograms storing times with microseconds granularity.
208 BASE_EXPORT void UmaHistogramCustomMicrosecondsTimes(const std::string& name,
209 TimeDelta sample,
210 TimeDelta min,
211 TimeDelta max,
212 size_t buckets);
213 BASE_EXPORT void UmaHistogramCustomMicrosecondsTimes(const char* name,
214 TimeDelta sample,
215 TimeDelta min,
216 TimeDelta max,
217 size_t buckets);
218
219 // For microseconds timings from 1 microsecond up to 10 seconds (50 buckets).
220 BASE_EXPORT void UmaHistogramMicrosecondsTimes(const std::string& name,
221 TimeDelta sample);
222 BASE_EXPORT void UmaHistogramMicrosecondsTimes(const char* name,
223 TimeDelta sample);
224
225 // For recording memory related histograms.
226 // Used to measure common KB-granularity memory stats. Range is up to 500M.
227 BASE_EXPORT void UmaHistogramMemoryKB(const std::string& name, int sample);
228 BASE_EXPORT void UmaHistogramMemoryKB(const char* name, int sample);
229 // Used to measure common MB-granularity memory stats. Range is up to ~1G.
230 BASE_EXPORT void UmaHistogramMemoryMB(const std::string& name, int sample);
231 BASE_EXPORT void UmaHistogramMemoryMB(const char* name, int sample);
232 // Used to measure common MB-granularity memory stats. Range is up to ~64G.
233 BASE_EXPORT void UmaHistogramMemoryLargeMB(const std::string& name, int sample);
234 BASE_EXPORT void UmaHistogramMemoryLargeMB(const char* name, int sample);
235
236 // For recording sparse histograms.
237 // The |sample| can be a negative or non-negative number.
238 //
239 // Sparse histograms are well suited for recording counts of exact sample values
240 // that are sparsely distributed over a relatively large range, in cases where
241 // ultra-fast performance is not critical. For instance, Sqlite.Version.* are
242 // sparse because for any given database, there's going to be exactly one
243 // version logged.
244 //
245 // Performance:
246 // ------------
247 // Sparse histograms are typically more memory-efficient but less time-efficient
248 // than other histograms. Essentially, they sparse histograms use a map rather
249 // than a vector for their backing storage; they also require lock acquisition
250 // to increment a sample, whereas other histogram do not. Hence, each increment
251 // operation is a bit slower than for other histograms. But, if the data is
252 // sparse, then they use less memory client-side, because they allocate buckets
253 // on demand rather than preallocating.
254 //
255 // Data size:
256 // ----------
257 // Note that server-side, we still need to load all buckets, across all users,
258 // at once. Thus, please avoid exploding such histograms, i.e. uploading many
259 // many distinct values to the server (across all users). Concretely, keep the
260 // number of distinct values <= 100 ideally, definitely <= 1000. If you have no
261 // guarantees on the range of your data, use clamping, e.g.:
262 // UmaHistogramSparse("My.Histogram", std::clamp(value, 0, 200));
263 BASE_EXPORT void UmaHistogramSparse(const std::string& name, int sample);
264 BASE_EXPORT void UmaHistogramSparse(const char* name, int sample);
265
266 } // namespace base
267
268 #endif // BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
269