• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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_INTERNAL_OVERLOADS_H_
6 #define BASE_METRICS_HISTOGRAM_FUNCTIONS_INTERNAL_OVERLOADS_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <string_view>
12 #include <type_traits>
13 
14 #include "base/base_export.h"
15 #include "base/check_op.h"
16 #include "base/metrics/histogram.h"
17 #include "base/metrics/histogram_base.h"
18 #include "base/time/time.h"
19 
20 // This file provides overloads for the functions defined in
21 // histogram_functions.h. These functions are duplicated to also support both
22 // std::string and char* for the name. This avoids ctor/dtor instantiation for
23 // constant strings to std::string, which makes the call be larger than caching
24 // macros (which do accept char*) in those cases. These overloads are in a
25 // separate header for readability.
26 
27 // See the main header for documentation:
28 // https://chromium.googlesource.com/chromium/src/+/HEAD/base/metrics/histogram_functions.h.
29 
30 namespace base {
31 // LINT.IfChange(UmaHistogramExactLinear)
32 BASE_EXPORT void UmaHistogramExactLinear(const std::string& name,
33                                          int sample,
34                                          int exclusive_max);
35 BASE_EXPORT void UmaHistogramExactLinear(const char* name,
36                                          int sample,
37                                          int exclusive_max);
38 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramExactLinear)
39 
40 // LINT.IfChange(UmaHistogramEnumeration)
41 template <typename T>
UmaHistogramEnumeration(const std::string & name,T sample)42 void UmaHistogramEnumeration(const std::string& name, T sample) {
43   static_assert(std::is_enum_v<T>, "T is not an enum.");
44   // This also ensures that an enumeration that doesn't define kMaxValue fails
45   // with a semi-useful error ("no member named 'kMaxValue' in ...").
46   static_assert(static_cast<uintmax_t>(T::kMaxValue) <=
47                     static_cast<uintmax_t>(INT_MAX) - 1,
48                 "Enumeration's kMaxValue is out of range of INT_MAX!");
49   DCHECK_LE(static_cast<uintmax_t>(sample),
50             static_cast<uintmax_t>(T::kMaxValue));
51   return UmaHistogramExactLinear(name, static_cast<int>(sample),
52                                  static_cast<int>(T::kMaxValue) + 1);
53 }
54 
55 template <typename T>
UmaHistogramEnumeration(const char * name,T sample)56 void UmaHistogramEnumeration(const char* name, T sample) {
57   static_assert(std::is_enum_v<T>, "T is not an enum.");
58   // This also ensures that an enumeration that doesn't define kMaxValue fails
59   // with a semi-useful error ("no member named 'kMaxValue' in ...").
60   static_assert(static_cast<uintmax_t>(T::kMaxValue) <=
61                     static_cast<uintmax_t>(INT_MAX) - 1,
62                 "Enumeration's kMaxValue is out of range of INT_MAX!");
63   DCHECK_LE(static_cast<uintmax_t>(sample),
64             static_cast<uintmax_t>(T::kMaxValue));
65   return UmaHistogramExactLinear(name, static_cast<int>(sample),
66                                  static_cast<int>(T::kMaxValue) + 1);
67 }
68 
69 template <typename T>
UmaHistogramEnumeration(const std::string & name,T sample,T enum_size)70 void UmaHistogramEnumeration(const std::string& name, T sample, T enum_size) {
71   static_assert(std::is_enum_v<T>, "T is not an enum.");
72   DCHECK_LE(static_cast<uintmax_t>(enum_size), static_cast<uintmax_t>(INT_MAX));
73   DCHECK_LT(static_cast<uintmax_t>(sample), static_cast<uintmax_t>(enum_size));
74   return UmaHistogramExactLinear(name, static_cast<int>(sample),
75                                  static_cast<int>(enum_size));
76 }
77 
78 template <typename T>
UmaHistogramEnumeration(const char * name,T sample,T enum_size)79 void UmaHistogramEnumeration(const char* name, T sample, T enum_size) {
80   static_assert(std::is_enum_v<T>, "T is not an enum.");
81   DCHECK_LE(static_cast<uintmax_t>(enum_size), static_cast<uintmax_t>(INT_MAX));
82   DCHECK_LT(static_cast<uintmax_t>(sample), static_cast<uintmax_t>(enum_size));
83   return UmaHistogramExactLinear(name, static_cast<int>(sample),
84                                  static_cast<int>(enum_size));
85 }
86 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramEnumeration)
87 
88 // LINT.IfChange(UmaHistogramBoolean)
89 BASE_EXPORT void UmaHistogramBoolean(const std::string& name, bool sample);
90 BASE_EXPORT void UmaHistogramBoolean(const char* name, bool sample);
91 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramBoolean)
92 
93 // LINT.IfChange(UmaHistogramPercentage)
94 BASE_EXPORT void UmaHistogramPercentage(const std::string& name, int percent);
95 BASE_EXPORT void UmaHistogramPercentage(const char* name, int percent);
96 
97 BASE_EXPORT void UmaHistogramPercentageObsoleteDoNotUse(const std::string& name,
98                                                         int percent);
99 BASE_EXPORT void UmaHistogramPercentageObsoleteDoNotUse(const char* name,
100                                                         int percent);
101 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramPercentage)
102 
103 // LINT.IfChange(UmaHistogramCounts)
104 BASE_EXPORT void UmaHistogramCustomCounts(const std::string& name,
105                                           int sample,
106                                           int min,
107                                           int exclusive_max,
108                                           size_t buckets);
109 BASE_EXPORT void UmaHistogramCustomCounts(const char* name,
110                                           int sample,
111                                           int min,
112                                           int exclusive_max,
113                                           size_t buckets);
114 
115 BASE_EXPORT void UmaHistogramCounts100(const std::string& name, int sample);
116 BASE_EXPORT void UmaHistogramCounts100(const char* name, int sample);
117 BASE_EXPORT void UmaHistogramCounts1000(const std::string& name, int sample);
118 BASE_EXPORT void UmaHistogramCounts1000(const char* name, int sample);
119 BASE_EXPORT void UmaHistogramCounts10000(const std::string& name, int sample);
120 BASE_EXPORT void UmaHistogramCounts10000(const char* name, int sample);
121 BASE_EXPORT void UmaHistogramCounts100000(const std::string& name, int sample);
122 BASE_EXPORT void UmaHistogramCounts100000(const char* name, int sample);
123 BASE_EXPORT void UmaHistogramCounts1M(const std::string& name, int sample);
124 BASE_EXPORT void UmaHistogramCounts1M(const char* name, int sample);
125 BASE_EXPORT void UmaHistogramCounts10M(const std::string& name, int sample);
126 BASE_EXPORT void UmaHistogramCounts10M(const char* name, int sample);
127 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramCounts)
128 
129 // LINT.IfChange(UmaHistogramTimes)
130 BASE_EXPORT void UmaHistogramCustomTimes(const std::string& name,
131                                          TimeDelta sample,
132                                          TimeDelta min,
133                                          TimeDelta max,
134                                          size_t buckets);
135 BASE_EXPORT void UmaHistogramCustomTimes(const char* name,
136                                          TimeDelta sample,
137                                          TimeDelta min,
138                                          TimeDelta max,
139                                          size_t buckets);
140 
141 BASE_EXPORT void UmaHistogramTimes(const std::string& name, TimeDelta sample);
142 BASE_EXPORT void UmaHistogramTimes(const char* name, TimeDelta sample);
143 
144 BASE_EXPORT void UmaHistogramMediumTimes(const std::string& name,
145                                          TimeDelta sample);
146 BASE_EXPORT void UmaHistogramMediumTimes(const char* name, TimeDelta sample);
147 
148 BASE_EXPORT void UmaHistogramLongTimes(const std::string& name,
149                                        TimeDelta sample);
150 BASE_EXPORT void UmaHistogramLongTimes(const char* name, TimeDelta sample);
151 
152 BASE_EXPORT void UmaHistogramLongTimes100(const std::string& name,
153                                           TimeDelta sample);
154 BASE_EXPORT void UmaHistogramLongTimes100(const char* name, TimeDelta sample);
155 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramTimes)
156 
157 // LINT.IfChange(UmaHistogramMicrosecondsTimes)
158 BASE_EXPORT void UmaHistogramCustomMicrosecondsTimes(const std::string& name,
159                                                      TimeDelta sample,
160                                                      TimeDelta min,
161                                                      TimeDelta max,
162                                                      size_t buckets);
163 BASE_EXPORT void UmaHistogramCustomMicrosecondsTimes(const char* name,
164                                                      TimeDelta sample,
165                                                      TimeDelta min,
166                                                      TimeDelta max,
167                                                      size_t buckets);
168 
169 BASE_EXPORT void UmaHistogramMicrosecondsTimes(const std::string& name,
170                                                TimeDelta sample);
171 BASE_EXPORT void UmaHistogramMicrosecondsTimes(const char* name,
172                                                TimeDelta sample);
173 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramMicrosecondsTimes)
174 
175 // LINT.IfChange(UmaHistogramMemory)
176 BASE_EXPORT void UmaHistogramMemoryKB(const std::string& name, int sample);
177 BASE_EXPORT void UmaHistogramMemoryKB(const char* name, int sample);
178 
179 BASE_EXPORT void UmaHistogramMemoryMB(const std::string& name, int sample);
180 BASE_EXPORT void UmaHistogramMemoryMB(const char* name, int sample);
181 
182 BASE_EXPORT void UmaHistogramMemoryLargeMB(const std::string& name, int sample);
183 BASE_EXPORT void UmaHistogramMemoryLargeMB(const char* name, int sample);
184 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramMemory)
185 
186 // LINT.IfChange(UmaHistogramSparse)
187 BASE_EXPORT void UmaHistogramSparse(const std::string& name, int sample);
188 BASE_EXPORT void UmaHistogramSparse(const char* name, int sample);
189 // LINT.ThenChange(/base/metrics/histogram_functions.h:UmaHistogramSparse)
190 
191 }  // namespace base
192 
193 #endif  // BASE_METRICS_HISTOGRAM_FUNCTIONS_INTERNAL_OVERLOADS_H_
194