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