histogram_macros_internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_MACROS_INTERNAL_H_
  5. #define BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_
  6. #include <stdint.h>
  7. #include <atomic>
  8. #include <limits>
  9. #include <memory>
  10. #include <type_traits>
  11. #include "base/check_op.h"
  12. #include "base/metrics/histogram.h"
  13. #include "base/metrics/sparse_histogram.h"
  14. #include "base/time/time.h"
  15. // This is for macros and helpers internal to base/metrics. They should not be
  16. // used outside of this directory. For writing to UMA histograms, see
  17. // histogram_macros.h.
  18. namespace base {
  19. namespace internal {
  20. // Helper traits for deducing the boundary value for enums.
  21. template <typename Enum, typename SFINAE = void>
  22. struct EnumSizeTraits {
  23. static constexpr Enum Count() {
  24. static_assert(
  25. sizeof(Enum) == 0,
  26. "enumerator must define kMaxValue enumerator to use this macro!");
  27. return Enum();
  28. }
  29. };
  30. // Since the UMA histogram macros expect a value one larger than the max defined
  31. // enumerator value, add one.
  32. template <typename Enum>
  33. struct EnumSizeTraits<
  34. Enum,
  35. std::enable_if_t<std::is_enum<decltype(Enum::kMaxValue)>::value>> {
  36. static constexpr Enum Count() {
  37. return static_cast<Enum>(
  38. static_cast<std::underlying_type_t<Enum>>(Enum::kMaxValue) + 1);
  39. }
  40. };
  41. } // namespace internal
  42. } // namespace base
  43. // TODO(rkaplow): Improve commenting of these methods.
  44. //------------------------------------------------------------------------------
  45. // Histograms are often put in areas where they are called many many times, and
  46. // performance is critical. As a result, they are designed to have a very low
  47. // recurring cost of executing (adding additional samples). Toward that end,
  48. // the macros declare a static pointer to the histogram in question, and only
  49. // take a "slow path" to construct (or find) the histogram on the first run
  50. // through the macro. We leak the histograms at shutdown time so that we don't
  51. // have to validate using the pointers at any time during the running of the
  52. // process.
  53. // In some cases (integration into 3rd party code), it's useful to separate the
  54. // definition of |atomic_histogram_pointer| from its use. To achieve this we
  55. // define HISTOGRAM_POINTER_USE, which uses an |atomic_histogram_pointer|, and
  56. // STATIC_HISTOGRAM_POINTER_BLOCK, which defines an |atomic_histogram_pointer|
  57. // and forwards to HISTOGRAM_POINTER_USE.
  58. #define HISTOGRAM_POINTER_USE( \
  59. atomic_histogram_pointer, constant_histogram_name, \
  60. histogram_add_method_invocation, histogram_factory_get_invocation) \
  61. do { \
  62. base::HistogramBase* histogram_pointer( \
  63. reinterpret_cast<base::HistogramBase*>( \
  64. atomic_histogram_pointer->load(std::memory_order_acquire))); \
  65. if (!histogram_pointer) { \
  66. /* \
  67. * This is the slow path, which will construct OR find the \
  68. * matching histogram. |histogram_factory_get_invocation| includes \
  69. * locks on a global histogram name map and is completely thread \
  70. * safe. \
  71. */ \
  72. histogram_pointer = histogram_factory_get_invocation; \
  73. \
  74. /* \
  75. * We could do this without any barrier, since FactoryGet() \
  76. * entered and exited a lock after construction, but this barrier \
  77. * makes things clear. \
  78. */ \
  79. atomic_histogram_pointer->store( \
  80. reinterpret_cast<uintptr_t>(histogram_pointer), \
  81. std::memory_order_release); \
  82. } \
  83. if (DCHECK_IS_ON()) \
  84. histogram_pointer->CheckName(constant_histogram_name); \
  85. histogram_pointer->histogram_add_method_invocation; \
  86. } while (0)
  87. // This is a helper macro used by other macros and shouldn't be used directly.
  88. // Defines the static |atomic_histogram_pointer| and forwards to
  89. // HISTOGRAM_POINTER_USE.
  90. #define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \
  91. histogram_add_method_invocation, \
  92. histogram_factory_get_invocation) \
  93. do { \
  94. /* \
  95. * The pointer's presence indicates that the initialization is complete. \
  96. * Initialization is idempotent, so it can safely be atomically repeated. \
  97. */ \
  98. static std::atomic_uintptr_t atomic_histogram_pointer; \
  99. HISTOGRAM_POINTER_USE( \
  100. std::addressof(atomic_histogram_pointer), constant_histogram_name, \
  101. histogram_add_method_invocation, histogram_factory_get_invocation); \
  102. } while (0)
  103. // This is a helper macro used by other macros and shouldn't be used directly.
  104. #define INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(name, sample, min, max, \
  105. bucket_count, flag) \
  106. STATIC_HISTOGRAM_POINTER_BLOCK( \
  107. name, Add(sample), \
  108. base::Histogram::FactoryGet(name, min, max, bucket_count, flag))
  109. // This is a helper macro used by other macros and shouldn't be used directly.
  110. // The bucketing scheme is linear with a bucket size of 1. For N items,
  111. // recording values in the range [0, N - 1] creates a linear histogram with N +
  112. // 1 buckets:
  113. // [0, 1), [1, 2), ..., [N - 1, N)
  114. // and an overflow bucket [N, infinity).
  115. //
  116. // Code should never emit to the overflow bucket; only to the other N buckets.
  117. // This allows future versions of Chrome to safely increase the boundary size.
  118. // Otherwise, the histogram would have [N - 1, infinity) as its overflow bucket,
  119. // and so the maximal value (N - 1) would be emitted to this overflow bucket.
  120. // But, if an additional value were later added, the bucket label for
  121. // the value (N - 1) would change to [N - 1, N), which would result in different
  122. // versions of Chrome using different bucket labels for identical data.
  123. #define INTERNAL_HISTOGRAM_EXACT_LINEAR_WITH_FLAG(name, sample, boundary, \
  124. flag) \
  125. do { \
  126. static_assert(!std::is_enum<std::decay_t<decltype(sample)>>::value, \
  127. "|sample| should not be an enum type!"); \
  128. static_assert(!std::is_enum<std::decay_t<decltype(boundary)>>::value, \
  129. "|boundary| should not be an enum type!"); \
  130. STATIC_HISTOGRAM_POINTER_BLOCK( \
  131. name, Add(sample), \
  132. base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, \
  133. flag)); \
  134. } while (0)
  135. // While this behaves the same as the above macro, the wrapping of a linear
  136. // histogram with another object to do the scaling means the POINTER_BLOCK
  137. // macro can't be used as it is tied to HistogramBase
  138. #define INTERNAL_HISTOGRAM_SCALED_EXACT_LINEAR_WITH_FLAG( \
  139. name, sample, count, boundary, scale, flag) \
  140. do { \
  141. static_assert(!std::is_enum<std::decay_t<decltype(sample)>>::value, \
  142. "|sample| should not be an enum type!"); \
  143. static_assert(!std::is_enum<std::decay_t<decltype(boundary)>>::value, \
  144. "|boundary| should not be an enum type!"); \
  145. class ScaledLinearHistogramInstance : public base::ScaledLinearHistogram { \
  146. public: \
  147. ScaledLinearHistogramInstance() \
  148. : ScaledLinearHistogram(name, \
  149. 1, \
  150. boundary, \
  151. boundary + 1, \
  152. scale, \
  153. flag) {} \
  154. }; \
  155. static base::LazyInstance<ScaledLinearHistogramInstance>::Leaky scaled; \
  156. scaled.Get().AddScaledCount(sample, count); \
  157. } while (0)
  158. // Helper for 'overloading' UMA_HISTOGRAM_ENUMERATION with a variable number of
  159. // arguments.
  160. #define INTERNAL_UMA_HISTOGRAM_ENUMERATION_GET_MACRO(_1, _2, NAME, ...) NAME
  161. #define INTERNAL_UMA_HISTOGRAM_ENUMERATION_DEDUCE_BOUNDARY(name, sample, \
  162. flags) \
  163. INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG( \
  164. name, sample, \
  165. base::internal::EnumSizeTraits<std::decay_t<decltype(sample)>>::Count(), \
  166. flags)
  167. // Note: The value in |sample| must be strictly less than |enum_size|.
  168. #define INTERNAL_UMA_HISTOGRAM_ENUMERATION_SPECIFY_BOUNDARY(name, sample, \
  169. enum_size, flags) \
  170. INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, enum_size, flags)
  171. // Similar to the previous macro but intended for enumerations. This delegates
  172. // the work to the previous macro, but supports scoped enumerations as well by
  173. // forcing an explicit cast to the HistogramBase::Sample integral type.
  174. //
  175. // Note the range checks verify two separate issues:
  176. // - that the declared enum size isn't out of range of HistogramBase::Sample
  177. // - that the declared enum size is > 0
  178. //
  179. // TODO(dcheng): This should assert that the passed in types are actually enum
  180. // types.
  181. #define INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \
  182. do { \
  183. using decayed_sample = std::decay<decltype(sample)>::type; \
  184. using decayed_boundary = std::decay<decltype(boundary)>::type; \
  185. static_assert(!std::is_enum<decayed_boundary>::value || \
  186. std::is_enum<decayed_sample>::value, \
  187. "Unexpected: |boundary| is enum, but |sample| is not."); \
  188. static_assert(!std::is_enum<decayed_sample>::value || \
  189. !std::is_enum<decayed_boundary>::value || \
  190. std::is_same<decayed_sample, decayed_boundary>::value, \
  191. "|sample| and |boundary| shouldn't be of different enums"); \
  192. static_assert( \
  193. static_cast<uintmax_t>(boundary) < \
  194. static_cast<uintmax_t>( \
  195. std::numeric_limits<base::HistogramBase::Sample>::max()), \
  196. "|boundary| is out of range of HistogramBase::Sample"); \
  197. INTERNAL_HISTOGRAM_EXACT_LINEAR_WITH_FLAG( \
  198. name, static_cast<base::HistogramBase::Sample>(sample), \
  199. static_cast<base::HistogramBase::Sample>(boundary), flag); \
  200. } while (0)
  201. #define INTERNAL_HISTOGRAM_SCALED_ENUMERATION_WITH_FLAG(name, sample, count, \
  202. scale, flag) \
  203. do { \
  204. using decayed_sample = std::decay<decltype(sample)>::type; \
  205. static_assert(std::is_enum<decayed_sample>::value, \
  206. "Unexpected: |sample| is not at enum."); \
  207. constexpr auto boundary = base::internal::EnumSizeTraits< \
  208. std::decay_t<decltype(sample)>>::Count(); \
  209. static_assert( \
  210. static_cast<uintmax_t>(boundary) < \
  211. static_cast<uintmax_t>( \
  212. std::numeric_limits<base::HistogramBase::Sample>::max()), \
  213. "|boundary| is out of range of HistogramBase::Sample"); \
  214. INTERNAL_HISTOGRAM_SCALED_EXACT_LINEAR_WITH_FLAG( \
  215. name, static_cast<base::HistogramBase::Sample>(sample), count, \
  216. static_cast<base::HistogramBase::Sample>(boundary), scale, flag); \
  217. } while (0)
  218. // This is a helper macro used by other macros and shouldn't be used directly.
  219. // This is necessary to expand __COUNTER__ to an actual value.
  220. #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, timing, key) \
  221. INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, timing, key)
  222. // This is a helper macro used by other macros and shouldn't be used directly.
  223. #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, timing, key) \
  224. class ScopedHistogramTimer##key { \
  225. public: \
  226. ScopedHistogramTimer##key() : constructed_(base::TimeTicks::Now()) {} \
  227. ~ScopedHistogramTimer##key() { \
  228. base::TimeDelta elapsed = base::TimeTicks::Now() - constructed_; \
  229. switch (timing) { \
  230. case ScopedHistogramTiming::kMicrosecondTimes: \
  231. UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES( \
  232. name, elapsed, base::TimeDelta::FromMicroseconds(1), \
  233. base::TimeDelta::FromSeconds(1), 50); \
  234. break; \
  235. case ScopedHistogramTiming::kMediumTimes: \
  236. UMA_HISTOGRAM_TIMES(name, elapsed); \
  237. break; \
  238. case ScopedHistogramTiming::kLongTimes: \
  239. UMA_HISTOGRAM_LONG_TIMES_100(name, elapsed); \
  240. break; \
  241. } \
  242. } \
  243. \
  244. private: \
  245. base::TimeTicks constructed_; \
  246. } scoped_histogram_timer_##key
  247. #endif // BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_