• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/metrics/histogram_functions.h"
6 
7 #include <string_view>
8 
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/histogram_base.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/time/time.h"
13 
14 namespace base {
15 // LINT.IfChange
16 
UmaHistogramBoolean(std::string_view name,bool sample)17 void UmaHistogramBoolean(std::string_view name, bool sample) {
18   HistogramBase* histogram = BooleanHistogram::FactoryGet(
19       name, HistogramBase::kUmaTargetedHistogramFlag);
20   histogram->Add(sample);
21 }
22 
UmaHistogramBoolean(const std::string & name,bool sample)23 void UmaHistogramBoolean(const std::string& name, bool sample) {
24   HistogramBase* histogram = BooleanHistogram::FactoryGet(
25       name, HistogramBase::kUmaTargetedHistogramFlag);
26   histogram->Add(sample);
27 }
28 
UmaHistogramBoolean(const char * name,bool sample)29 void UmaHistogramBoolean(const char* name, bool sample) {
30   HistogramBase* histogram = BooleanHistogram::FactoryGet(
31       name, HistogramBase::kUmaTargetedHistogramFlag);
32   histogram->Add(sample);
33 }
34 
UmaHistogramExactLinear(std::string_view name,int sample,int exclusive_max)35 void UmaHistogramExactLinear(std::string_view name,
36                              int sample,
37                              int exclusive_max) {
38   HistogramBase* histogram = LinearHistogram::FactoryGet(
39       name, 1, exclusive_max, static_cast<size_t>(exclusive_max + 1),
40       HistogramBase::kUmaTargetedHistogramFlag);
41   histogram->Add(sample);
42 }
43 
UmaHistogramExactLinear(const std::string & name,int sample,int exclusive_max)44 void UmaHistogramExactLinear(const std::string& name,
45                              int sample,
46                              int exclusive_max) {
47   HistogramBase* histogram = LinearHistogram::FactoryGet(
48       name, 1, exclusive_max, static_cast<size_t>(exclusive_max + 1),
49       HistogramBase::kUmaTargetedHistogramFlag);
50   histogram->Add(sample);
51 }
52 
UmaHistogramExactLinear(const char * name,int sample,int exclusive_max)53 void UmaHistogramExactLinear(const char* name, int sample, int exclusive_max) {
54   HistogramBase* histogram = LinearHistogram::FactoryGet(
55       name, 1, exclusive_max, static_cast<size_t>(exclusive_max + 1),
56       HistogramBase::kUmaTargetedHistogramFlag);
57   histogram->Add(sample);
58 }
59 
UmaHistogramPercentage(std::string_view name,int percent)60 void UmaHistogramPercentage(std::string_view name, int percent) {
61   UmaHistogramExactLinear(name, percent, 101);
62 }
63 
UmaHistogramPercentage(const std::string & name,int percent)64 void UmaHistogramPercentage(const std::string& name, int percent) {
65   UmaHistogramExactLinear(name, percent, 101);
66 }
67 
UmaHistogramPercentage(const char * name,int percent)68 void UmaHistogramPercentage(const char* name, int percent) {
69   UmaHistogramExactLinear(name, percent, 101);
70 }
71 
UmaHistogramPercentageObsoleteDoNotUse(std::string_view name,int percent)72 void UmaHistogramPercentageObsoleteDoNotUse(std::string_view name,
73                                             int percent) {
74   UmaHistogramExactLinear(name, percent, 100);
75 }
76 
UmaHistogramPercentageObsoleteDoNotUse(const std::string & name,int percent)77 void UmaHistogramPercentageObsoleteDoNotUse(const std::string& name,
78                                             int percent) {
79   UmaHistogramExactLinear(name, percent, 100);
80 }
81 
UmaHistogramPercentageObsoleteDoNotUse(const char * name,int percent)82 void UmaHistogramPercentageObsoleteDoNotUse(const char* name, int percent) {
83   UmaHistogramExactLinear(name, percent, 100);
84 }
85 
UmaHistogramCustomCounts(std::string_view name,int sample,int min,int exclusive_max,size_t buckets)86 void UmaHistogramCustomCounts(std::string_view name,
87                               int sample,
88                               int min,
89                               int exclusive_max,
90                               size_t buckets) {
91   HistogramBase* histogram =
92       Histogram::FactoryGet(name, min, exclusive_max, buckets,
93                             HistogramBase::kUmaTargetedHistogramFlag);
94   histogram->Add(sample);
95 }
96 
UmaHistogramCustomCounts(const std::string & name,int sample,int min,int exclusive_max,size_t buckets)97 void UmaHistogramCustomCounts(const std::string& name,
98                               int sample,
99                               int min,
100                               int exclusive_max,
101                               size_t buckets) {
102   HistogramBase* histogram =
103       Histogram::FactoryGet(name, min, exclusive_max, buckets,
104                             HistogramBase::kUmaTargetedHistogramFlag);
105   histogram->Add(sample);
106 }
107 
UmaHistogramCustomCounts(const char * name,int sample,int min,int exclusive_max,size_t buckets)108 void UmaHistogramCustomCounts(const char* name,
109                               int sample,
110                               int min,
111                               int exclusive_max,
112                               size_t buckets) {
113   HistogramBase* histogram =
114       Histogram::FactoryGet(name, min, exclusive_max, buckets,
115                             HistogramBase::kUmaTargetedHistogramFlag);
116   histogram->Add(sample);
117 }
118 
UmaHistogramCounts100(std::string_view name,int sample)119 void UmaHistogramCounts100(std::string_view name, int sample) {
120   UmaHistogramCustomCounts(name, sample, 1, 100, 50);
121 }
122 
UmaHistogramCounts100(const std::string & name,int sample)123 void UmaHistogramCounts100(const std::string& name, int sample) {
124   UmaHistogramCustomCounts(name, sample, 1, 100, 50);
125 }
126 
UmaHistogramCounts100(const char * name,int sample)127 void UmaHistogramCounts100(const char* name, int sample) {
128   UmaHistogramCustomCounts(name, sample, 1, 100, 50);
129 }
130 
UmaHistogramCounts1000(std::string_view name,int sample)131 void UmaHistogramCounts1000(std::string_view name, int sample) {
132   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
133 }
134 
UmaHistogramCounts1000(const std::string & name,int sample)135 void UmaHistogramCounts1000(const std::string& name, int sample) {
136   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
137 }
138 
UmaHistogramCounts1000(const char * name,int sample)139 void UmaHistogramCounts1000(const char* name, int sample) {
140   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
141 }
142 
UmaHistogramCounts10000(std::string_view name,int sample)143 void UmaHistogramCounts10000(std::string_view name, int sample) {
144   UmaHistogramCustomCounts(name, sample, 1, 10000, 50);
145 }
146 
UmaHistogramCounts10000(const std::string & name,int sample)147 void UmaHistogramCounts10000(const std::string& name, int sample) {
148   UmaHistogramCustomCounts(name, sample, 1, 10000, 50);
149 }
150 
UmaHistogramCounts10000(const char * name,int sample)151 void UmaHistogramCounts10000(const char* name, int sample) {
152   UmaHistogramCustomCounts(name, sample, 1, 10000, 50);
153 }
154 
UmaHistogramCounts100000(std::string_view name,int sample)155 void UmaHistogramCounts100000(std::string_view name, int sample) {
156   UmaHistogramCustomCounts(name, sample, 1, 100000, 50);
157 }
158 
UmaHistogramCounts100000(const std::string & name,int sample)159 void UmaHistogramCounts100000(const std::string& name, int sample) {
160   UmaHistogramCustomCounts(name, sample, 1, 100000, 50);
161 }
162 
UmaHistogramCounts100000(const char * name,int sample)163 void UmaHistogramCounts100000(const char* name, int sample) {
164   UmaHistogramCustomCounts(name, sample, 1, 100000, 50);
165 }
166 
UmaHistogramCounts1M(std::string_view name,int sample)167 void UmaHistogramCounts1M(std::string_view name, int sample) {
168   UmaHistogramCustomCounts(name, sample, 1, 1000000, 50);
169 }
170 
UmaHistogramCounts1M(const std::string & name,int sample)171 void UmaHistogramCounts1M(const std::string& name, int sample) {
172   UmaHistogramCustomCounts(name, sample, 1, 1000000, 50);
173 }
174 
UmaHistogramCounts1M(const char * name,int sample)175 void UmaHistogramCounts1M(const char* name, int sample) {
176   UmaHistogramCustomCounts(name, sample, 1, 1000000, 50);
177 }
178 
UmaHistogramCounts10M(std::string_view name,int sample)179 void UmaHistogramCounts10M(std::string_view name, int sample) {
180   UmaHistogramCustomCounts(name, sample, 1, 10000000, 50);
181 }
182 
UmaHistogramCounts10M(const std::string & name,int sample)183 void UmaHistogramCounts10M(const std::string& name, int sample) {
184   UmaHistogramCustomCounts(name, sample, 1, 10000000, 50);
185 }
186 
UmaHistogramCounts10M(const char * name,int sample)187 void UmaHistogramCounts10M(const char* name, int sample) {
188   UmaHistogramCustomCounts(name, sample, 1, 10000000, 50);
189 }
190 
UmaHistogramCustomTimes(std::string_view name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)191 void UmaHistogramCustomTimes(std::string_view name,
192                              TimeDelta sample,
193                              TimeDelta min,
194                              TimeDelta max,
195                              size_t buckets) {
196   HistogramBase* histogram = Histogram::FactoryTimeGet(
197       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
198   histogram->AddTimeMillisecondsGranularity(sample);
199 }
200 
UmaHistogramCustomTimes(const std::string & name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)201 void UmaHistogramCustomTimes(const std::string& name,
202                              TimeDelta sample,
203                              TimeDelta min,
204                              TimeDelta max,
205                              size_t buckets) {
206   HistogramBase* histogram = Histogram::FactoryTimeGet(
207       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
208   histogram->AddTimeMillisecondsGranularity(sample);
209 }
210 
UmaHistogramCustomTimes(const char * name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)211 void UmaHistogramCustomTimes(const char* name,
212                              TimeDelta sample,
213                              TimeDelta min,
214                              TimeDelta max,
215                              size_t buckets) {
216   HistogramBase* histogram = Histogram::FactoryTimeGet(
217       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
218   histogram->AddTimeMillisecondsGranularity(sample);
219 }
220 
UmaHistogramTimes(std::string_view name,TimeDelta sample)221 void UmaHistogramTimes(std::string_view name, TimeDelta sample) {
222   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Seconds(10), 50);
223 }
224 
UmaHistogramTimes(const std::string & name,TimeDelta sample)225 void UmaHistogramTimes(const std::string& name, TimeDelta sample) {
226   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Seconds(10), 50);
227 }
228 
UmaHistogramTimes(const char * name,TimeDelta sample)229 void UmaHistogramTimes(const char* name, TimeDelta sample) {
230   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Seconds(10), 50);
231 }
232 
UmaHistogramMediumTimes(std::string_view name,TimeDelta sample)233 void UmaHistogramMediumTimes(std::string_view name, TimeDelta sample) {
234   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Minutes(3), 50);
235 }
236 
UmaHistogramMediumTimes(const std::string & name,TimeDelta sample)237 void UmaHistogramMediumTimes(const std::string& name, TimeDelta sample) {
238   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Minutes(3), 50);
239 }
240 
UmaHistogramMediumTimes(const char * name,TimeDelta sample)241 void UmaHistogramMediumTimes(const char* name, TimeDelta sample) {
242   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Minutes(3), 50);
243 }
244 
UmaHistogramLongTimes(std::string_view name,TimeDelta sample)245 void UmaHistogramLongTimes(std::string_view name, TimeDelta sample) {
246   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 50);
247 }
248 
UmaHistogramLongTimes(const std::string & name,TimeDelta sample)249 void UmaHistogramLongTimes(const std::string& name, TimeDelta sample) {
250   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 50);
251 }
252 
UmaHistogramLongTimes(const char * name,TimeDelta sample)253 void UmaHistogramLongTimes(const char* name, TimeDelta sample) {
254   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 50);
255 }
256 
UmaHistogramLongTimes100(std::string_view name,TimeDelta sample)257 void UmaHistogramLongTimes100(std::string_view name, TimeDelta sample) {
258   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 100);
259 }
260 
UmaHistogramLongTimes100(const std::string & name,TimeDelta sample)261 void UmaHistogramLongTimes100(const std::string& name, TimeDelta sample) {
262   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 100);
263 }
264 
UmaHistogramLongTimes100(const char * name,TimeDelta sample)265 void UmaHistogramLongTimes100(const char* name, TimeDelta sample) {
266   UmaHistogramCustomTimes(name, sample, Milliseconds(1), Hours(1), 100);
267 }
268 
UmaHistogramCustomMicrosecondsTimes(std::string_view name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)269 void UmaHistogramCustomMicrosecondsTimes(std::string_view name,
270                                          TimeDelta sample,
271                                          TimeDelta min,
272                                          TimeDelta max,
273                                          size_t buckets) {
274   HistogramBase* histogram = Histogram::FactoryMicrosecondsTimeGet(
275       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
276   histogram->AddTimeMicrosecondsGranularity(sample);
277 }
278 
UmaHistogramCustomMicrosecondsTimes(const std::string & name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)279 void UmaHistogramCustomMicrosecondsTimes(const std::string& name,
280                                          TimeDelta sample,
281                                          TimeDelta min,
282                                          TimeDelta max,
283                                          size_t buckets) {
284   HistogramBase* histogram = Histogram::FactoryMicrosecondsTimeGet(
285       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
286   histogram->AddTimeMicrosecondsGranularity(sample);
287 }
288 
UmaHistogramCustomMicrosecondsTimes(const char * name,TimeDelta sample,TimeDelta min,TimeDelta max,size_t buckets)289 void UmaHistogramCustomMicrosecondsTimes(const char* name,
290                                          TimeDelta sample,
291                                          TimeDelta min,
292                                          TimeDelta max,
293                                          size_t buckets) {
294   HistogramBase* histogram = Histogram::FactoryMicrosecondsTimeGet(
295       name, min, max, buckets, HistogramBase::kUmaTargetedHistogramFlag);
296   histogram->AddTimeMicrosecondsGranularity(sample);
297 }
298 
UmaHistogramMicrosecondsTimes(std::string_view name,TimeDelta sample)299 void UmaHistogramMicrosecondsTimes(std::string_view name, TimeDelta sample) {
300   UmaHistogramCustomMicrosecondsTimes(name, sample, Microseconds(1),
301                                       Seconds(10), 50);
302 }
303 
UmaHistogramMicrosecondsTimes(const std::string & name,TimeDelta sample)304 void UmaHistogramMicrosecondsTimes(const std::string& name, TimeDelta sample) {
305   UmaHistogramCustomMicrosecondsTimes(name, sample, Microseconds(1),
306                                       Seconds(10), 50);
307 }
308 
UmaHistogramMicrosecondsTimes(const char * name,TimeDelta sample)309 void UmaHistogramMicrosecondsTimes(const char* name, TimeDelta sample) {
310   UmaHistogramCustomMicrosecondsTimes(name, sample, Microseconds(1),
311                                       Seconds(10), 50);
312 }
313 
UmaHistogramMemoryKB(std::string_view name,int sample)314 void UmaHistogramMemoryKB(std::string_view name, int sample) {
315   UmaHistogramCustomCounts(name, sample, 1000, 500000, 50);
316 }
317 
UmaHistogramMemoryKB(const std::string & name,int sample)318 void UmaHistogramMemoryKB(const std::string& name, int sample) {
319   UmaHistogramCustomCounts(name, sample, 1000, 500000, 50);
320 }
321 
UmaHistogramMemoryKB(const char * name,int sample)322 void UmaHistogramMemoryKB(const char* name, int sample) {
323   UmaHistogramCustomCounts(name, sample, 1000, 500000, 50);
324 }
325 
UmaHistogramMemoryMB(std::string_view name,int sample)326 void UmaHistogramMemoryMB(std::string_view name, int sample) {
327   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
328 }
329 
UmaHistogramMemoryMB(const std::string & name,int sample)330 void UmaHistogramMemoryMB(const std::string& name, int sample) {
331   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
332 }
333 
UmaHistogramMemoryMB(const char * name,int sample)334 void UmaHistogramMemoryMB(const char* name, int sample) {
335   UmaHistogramCustomCounts(name, sample, 1, 1000, 50);
336 }
337 
UmaHistogramMemoryLargeMB(std::string_view name,int sample)338 void UmaHistogramMemoryLargeMB(std::string_view name, int sample) {
339   UmaHistogramCustomCounts(name, sample, 1, 64000, 100);
340 }
341 
UmaHistogramMemoryLargeMB(const std::string & name,int sample)342 void UmaHistogramMemoryLargeMB(const std::string& name, int sample) {
343   UmaHistogramCustomCounts(name, sample, 1, 64000, 100);
344 }
345 
UmaHistogramMemoryLargeMB(const char * name,int sample)346 void UmaHistogramMemoryLargeMB(const char* name, int sample) {
347   UmaHistogramCustomCounts(name, sample, 1, 64000, 100);
348 }
349 
UmaHistogramSparse(std::string_view name,int sample)350 void UmaHistogramSparse(std::string_view name, int sample) {
351   HistogramBase* histogram = SparseHistogram::FactoryGet(
352       name, HistogramBase::kUmaTargetedHistogramFlag);
353   histogram->Add(sample);
354 }
355 
UmaHistogramSparse(const std::string & name,int sample)356 void UmaHistogramSparse(const std::string& name, int sample) {
357   HistogramBase* histogram = SparseHistogram::FactoryGet(
358       name, HistogramBase::kUmaTargetedHistogramFlag);
359   histogram->Add(sample);
360 }
361 
UmaHistogramSparse(const char * name,int sample)362 void UmaHistogramSparse(const char* name, int sample) {
363   HistogramBase* histogram = SparseHistogram::FactoryGet(
364       name, HistogramBase::kUmaTargetedHistogramFlag);
365   histogram->Add(sample);
366 }
367 
ScopedUmaHistogramTimer(std::string_view name,ScopedHistogramTiming timing)368 ScopedUmaHistogramTimer::ScopedUmaHistogramTimer(std::string_view name,
369                                                  ScopedHistogramTiming timing)
370     : constructed_(base::TimeTicks::Now()), timing_(timing), name_(name) {}
371 
~ScopedUmaHistogramTimer()372 ScopedUmaHistogramTimer::~ScopedUmaHistogramTimer() {
373   base::TimeDelta elapsed = base::TimeTicks::Now() - constructed_;
374   switch (timing_) {
375     case ScopedHistogramTiming::kMicrosecondTimes:
376       UmaHistogramMicrosecondsTimes(name_, elapsed);
377       break;
378     case ScopedHistogramTiming::kShortTimes:
379       UmaHistogramTimes(name_, elapsed);
380       break;
381     case ScopedHistogramTiming::kMediumTimes:
382       UmaHistogramMediumTimes(name_, elapsed);
383       break;
384     case ScopedHistogramTiming::kLongTimes:
385       UmaHistogramLongTimes(name_, elapsed);
386       break;
387   }
388 }
389 // LINT.ThenChange(//base/metrics/histogram_macros.h)
390 }  // namespace base
391