histogram_functions.h 13 KB

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