Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Side by Side Diff: base/metrics/histogram_macros_internal.h

Issue 2776853002: Make UMA_HISTOGRAM_ENUMERATION work with scoped enums. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/histogram_macros.h ('k') | base/metrics/histogram_macros_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ 5 #ifndef BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_
6 #define BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ 6 #define BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_
7 7
8 #include <stdint.h>
9
10 #include <limits>
11 #include <type_traits>
12
8 #include "base/atomicops.h" 13 #include "base/atomicops.h"
9 #include "base/logging.h" 14 #include "base/logging.h"
10 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
11 #include "base/metrics/sparse_histogram.h" 16 #include "base/metrics/sparse_histogram.h"
12 #include "base/time/time.h" 17 #include "base/time/time.h"
13 18
14 // This is for macros internal to base/metrics. They should not be used outside 19 // This is for macros internal to base/metrics. They should not be used outside
15 // of this directory. For writing to UMA histograms, see histogram_macros.h. 20 // of this directory. For writing to UMA histograms, see histogram_macros.h.
16 21
17 // TODO(rkaplow): Improve commenting of these methods. 22 // TODO(rkaplow): Improve commenting of these methods.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } while (0) 94 } while (0)
90 95
91 // This is a helper macro used by other macros and shouldn't be used directly. 96 // This is a helper macro used by other macros and shouldn't be used directly.
92 #define INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(name, sample, min, max, \ 97 #define INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(name, sample, min, max, \
93 bucket_count, flag) \ 98 bucket_count, flag) \
94 STATIC_HISTOGRAM_POINTER_BLOCK( \ 99 STATIC_HISTOGRAM_POINTER_BLOCK( \
95 name, Add(sample), \ 100 name, Add(sample), \
96 base::Histogram::FactoryGet(name, min, max, bucket_count, flag)) 101 base::Histogram::FactoryGet(name, min, max, bucket_count, flag))
97 102
98 // This is a helper macro used by other macros and shouldn't be used directly. 103 // This is a helper macro used by other macros and shouldn't be used directly.
99 // For an enumeration with N items, recording values in the range [0, N - 1], 104 // The bucketing scheme is linear with a bucket size of 1. For N items,
100 // this macro creates a linear histogram with N + 1 buckets: 105 // recording values in the range [0, N - 1] creates a linear histogram with N +
101 // [0, 1), [1, 2), ..., [N - 1, N), and an overflow bucket [N, infinity). 106 // 1 buckets:
107 // [0, 1), [1, 2), ..., [N - 1, N)
108 // and an overflow bucket [N, infinity).
109 //
102 // Code should never emit to the overflow bucket; only to the other N buckets. 110 // Code should never emit to the overflow bucket; only to the other N buckets.
103 // This allows future versions of Chrome to safely append new entries to the 111 // This allows future versions of Chrome to safely increase the boundary size.
104 // enumeration. Otherwise, the histogram would have [N - 1, infinity) as its 112 // Otherwise, the histogram would have [N - 1, infinity) as its overflow bucket,
105 // overflow bucket, and so the maximal value (N - 1) would be emitted to this 113 // and so the maximal value (N - 1) would be emitted to this overflow bucket.
106 // overflow bucket. But, if an additional enumerated value were later added, the 114 // But, if an additional value were later added, the bucket label for
107 // bucket label for the value (N - 1) would change to [N - 1, N), which would 115 // the value (N - 1) would change to [N - 1, N), which would result in different
108 // result in different versions of Chrome using different bucket labels for 116 // versions of Chrome using different bucket labels for identical data.
109 // identical data. 117 #define INTERNAL_HISTOGRAM_EXACT_LINEAR_WITH_FLAG(name, sample, boundary, \
118 flag) \
119 do { \
120 static_assert(!std::is_enum<decltype(sample)>::value, \
121 "|sample| should not be an enum type!"); \
122 static_assert(!std::is_enum<decltype(boundary)>::value, \
123 "|boundary| should not be an enum type!"); \
124 STATIC_HISTOGRAM_POINTER_BLOCK( \
125 name, Add(sample), \
126 base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, \
127 flag)); \
128 } while (0)
129
130 // Similar to the previous macro but intended for enumerations. This delegates
131 // the work to the previous macro, but supports scoped enumerations as well by
132 // forcing an explicit cast to the HistogramBase::Sample integral type.
133 //
134 // Note the range checks verify two separate issues:
135 // - that the declared enum max isn't out of range of HistogramBase::Sample
136 // - that the declared enum max is > 0
137 //
138 // TODO(dcheng): This should assert that the passed in types are actually enum
139 // types.
Ilya Sherman 2017/03/31 00:28:14 It looks to me like the current code allows, say,
dcheng 2017/03/31 00:46:00 I'll be addressing this TODO shortly. I expect to
Ilya Sherman 2017/03/31 00:47:15 Okay, lovely -- thanks!
110 #define INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \ 140 #define INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \
111 do { \ 141 do { \
112 static_assert( \ 142 static_assert( \
113 !std::is_enum<decltype(sample)>::value || \ 143 !std::is_enum<decltype(sample)>::value || \
114 !std::is_enum<decltype(boundary)>::value || \ 144 !std::is_enum<decltype(boundary)>::value || \
115 std::is_same<std::remove_const<decltype(sample)>::type, \ 145 std::is_same<std::remove_const<decltype(sample)>::type, \
116 std::remove_const<decltype(boundary)>::type>::value, \ 146 std::remove_const<decltype(boundary)>::type>::value, \
117 "|sample| and |boundary| shouldn't be of different enums"); \ 147 "|sample| and |boundary| shouldn't be of different enums"); \
118 STATIC_HISTOGRAM_POINTER_BLOCK( \ 148 static_assert( \
119 name, Add(sample), base::LinearHistogram::FactoryGet( \ 149 static_cast<uintmax_t>(boundary) < \
120 name, 1, boundary, boundary + 1, flag)); \ 150 static_cast<uintmax_t>( \
151 std::numeric_limits<base::HistogramBase::Sample>::max()), \
152 "|boundary| is out of range of HistogramBase::Sample"); \
153 INTERNAL_HISTOGRAM_EXACT_LINEAR_WITH_FLAG( \
154 name, static_cast<base::HistogramBase::Sample>(sample), \
155 static_cast<base::HistogramBase::Sample>(boundary), flag); \
121 } while (0) 156 } while (0)
122 157
123 // This is a helper macro used by other macros and shouldn't be used directly. 158 // This is a helper macro used by other macros and shouldn't be used directly.
124 // This is necessary to expand __COUNTER__ to an actual value. 159 // This is necessary to expand __COUNTER__ to an actual value.
125 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, is_long, key) \ 160 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, is_long, key) \
126 INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) 161 INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key)
127 162
128 // This is a helper macro used by other macros and shouldn't be used directly. 163 // This is a helper macro used by other macros and shouldn't be used directly.
129 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) \ 164 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) \
130 class ScopedHistogramTimer##key { \ 165 class ScopedHistogramTimer##key { \
(...skipping 17 matching lines...) Expand all
148 // may be more efficient in memory if the total number of sample values is small 183 // may be more efficient in memory if the total number of sample values is small
149 // compared to the range of their values. 184 // compared to the range of their values.
150 #define INTERNAL_HISTOGRAM_SPARSE_SLOWLY(name, sample) \ 185 #define INTERNAL_HISTOGRAM_SPARSE_SLOWLY(name, sample) \
151 do { \ 186 do { \
152 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \ 187 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \
153 name, base::HistogramBase::kUmaTargetedHistogramFlag); \ 188 name, base::HistogramBase::kUmaTargetedHistogramFlag); \
154 histogram->Add(sample); \ 189 histogram->Add(sample); \
155 } while (0) 190 } while (0)
156 191
157 #endif // BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ 192 #endif // BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_
OLDNEW
« no previous file with comments | « base/metrics/histogram_macros.h ('k') | base/metrics/histogram_macros_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698