asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | // |
| 4 | // Use of this source code is governed by a BSD-style license |
| 5 | // that can be found in the LICENSE file in the root of the source |
| 6 | // tree. An additional intellectual property rights grant can be found |
| 7 | // in the file PATENTS. All contributing project authors may |
| 8 | // be found in the AUTHORS file in the root of the source tree. |
| 9 | // |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |
| 12 | #define SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 16 | #include <atomic> |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <memory> |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 21 | #include "absl/strings/string_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 23 | #include "rtc_base/string_utils.h" |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 24 | |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 25 | #if defined(RTC_DISABLE_METRICS) |
| 26 | #define RTC_METRICS_ENABLED 0 |
| 27 | #else |
| 28 | #define RTC_METRICS_ENABLED 1 |
| 29 | #endif |
| 30 | |
| 31 | namespace webrtc { |
| 32 | namespace metrics_impl { |
| 33 | template <typename... Ts> |
| 34 | void NoOp(const Ts&...) {} |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 35 | } // namespace metrics_impl |
| 36 | } // namespace webrtc |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 37 | |
| 38 | #if RTC_METRICS_ENABLED |
| 39 | #define EXPECT_METRIC_EQ(val1, val2) EXPECT_EQ(val1, val2) |
| 40 | #define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) \ |
| 41 | EXPECT_EQ_WAIT(val1, val2, timeout) |
| 42 | #define EXPECT_METRIC_GT(val1, val2) EXPECT_GT(val1, val2) |
| 43 | #define EXPECT_METRIC_LE(val1, val2) EXPECT_LE(val1, val2) |
| 44 | #define EXPECT_METRIC_TRUE(conditon) EXPECT_TRUE(conditon) |
| 45 | #define EXPECT_METRIC_FALSE(conditon) EXPECT_FALSE(conditon) |
| 46 | #define EXPECT_METRIC_THAT(value, matcher) EXPECT_THAT(value, matcher) |
| 47 | #else |
| 48 | #define EXPECT_METRIC_EQ(val1, val2) webrtc::metrics_impl::NoOp(val1, val2) |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 49 | #define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) \ |
| 50 | webrtc::metrics_impl::NoOp(val1, val2, timeout) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 51 | #define EXPECT_METRIC_GT(val1, val2) webrtc::metrics_impl::NoOp(val1, val2) |
| 52 | #define EXPECT_METRIC_LE(val1, val2) webrtc::metrics_impl::NoOp(val1, val2) |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 53 | #define EXPECT_METRIC_TRUE(condition) \ |
| 54 | webrtc::metrics_impl::NoOp(condition || true) |
| 55 | #define EXPECT_METRIC_FALSE(condition) \ |
| 56 | webrtc::metrics_impl::NoOp(condition && false) |
| 57 | #define EXPECT_METRIC_THAT(value, matcher) \ |
| 58 | webrtc::metrics_impl::NoOp(value, testing::_) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | #if RTC_METRICS_ENABLED |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 62 | // Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate |
| 63 | // statistics. |
| 64 | // |
| 65 | // Histogram for counters. |
| 66 | // RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count); |
| 67 | // |
| 68 | // Histogram for enumerators. |
| 69 | // The boundary should be above the max enumerator sample. |
| 70 | // RTC_HISTOGRAM_ENUMERATION(name, sample, boundary); |
| 71 | // |
| 72 | // |
| 73 | // The macros use the methods HistogramFactoryGetCounts, |
| 74 | // HistogramFactoryGetEnumeration and HistogramAdd. |
| 75 | // |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 76 | // By default WebRTC provides implementations of the aforementioned methods |
| 77 | // that can be found in system_wrappers/source/metrics.cc. If clients want to |
| 78 | // provide a custom version, they will have to: |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 79 | // |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 80 | // 1. Compile WebRTC defining the preprocessor macro |
| 81 | // WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved |
| 82 | // by setting the GN arg rtc_exclude_metrics_default to true). |
| 83 | // 2. Provide implementations of: |
| 84 | // Histogram* webrtc::metrics::HistogramFactoryGetCounts( |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 85 | // absl::string_view name, int sample, int min, int max, |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 86 | // int bucket_count); |
| 87 | // Histogram* webrtc::metrics::HistogramFactoryGetEnumeration( |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 88 | // absl::string_view name, int sample, int boundary); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 89 | // void webrtc::metrics::HistogramAdd( |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 90 | // Histogram* histogram_pointer, absl::string_view name, int sample); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 91 | // |
| 92 | // Example usage: |
| 93 | // |
| 94 | // RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100); |
| 95 | // |
| 96 | // enum Types { |
| 97 | // kTypeX, |
| 98 | // kTypeY, |
| 99 | // kBoundary, |
| 100 | // }; |
| 101 | // |
| 102 | // RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary); |
henrik.lundin | c9badd5 | 2016-12-06 11:58:58 | [diff] [blame] | 103 | // |
| 104 | // NOTE: It is recommended to do the Chromium review for modifications to |
| 105 | // histograms.xml before new metrics are committed to WebRTC. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 106 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 107 | // Macros for adding samples to a named histogram. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 108 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 109 | // Histogram for counters (exponentially spaced buckets). |
| 110 | #define RTC_HISTOGRAM_COUNTS_100(name, sample) \ |
| 111 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50) |
| 112 | |
| 113 | #define RTC_HISTOGRAM_COUNTS_200(name, sample) \ |
| 114 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50) |
| 115 | |
asapersson | 5265fed | 2016-04-18 09:58:47 | [diff] [blame] | 116 | #define RTC_HISTOGRAM_COUNTS_500(name, sample) \ |
| 117 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50) |
| 118 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 119 | #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 120 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50) |
| 121 | |
| 122 | #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 123 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50) |
| 124 | |
| 125 | #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 126 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50) |
| 127 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 128 | #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
| 129 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
| 130 | webrtc::metrics::HistogramFactoryGetCounts( \ |
| 131 | name, min, max, bucket_count)) |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 132 | |
henrik.lundin | f29e05d | 2016-12-01 17:58:45 | [diff] [blame] | 133 | #define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \ |
| 134 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
| 135 | webrtc::metrics::HistogramFactoryGetCountsLinear( \ |
| 136 | name, min, max, bucket_count)) |
| 137 | |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 138 | // Slow metrics: pointer to metric is acquired at each call and is not cached. |
| 139 | // |
asapersson | 5380532 | 2015-12-21 09:46:20 | [diff] [blame] | 140 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \ |
| 141 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50) |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 | [diff] [blame] | 142 | |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 143 | #define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \ |
| 144 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50) |
| 145 | |
| 146 | #define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \ |
| 147 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50) |
| 148 | |
| 149 | #define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \ |
| 150 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50) |
| 151 | |
| 152 | #define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \ |
| 153 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50) |
| 154 | |
| 155 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \ |
| 156 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50) |
| 157 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 158 | #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \ |
| 159 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \ |
| 160 | webrtc::metrics::HistogramFactoryGetCounts( \ |
| 161 | name, min, max, bucket_count)) |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 162 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 163 | // Histogram for percentage (evenly spaced buckets). |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 164 | #define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \ |
| 165 | RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101) |
| 166 | |
| 167 | // Histogram for booleans. |
| 168 | #define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \ |
| 169 | RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2) |
| 170 | |
| 171 | // Histogram for enumerators (evenly spaced buckets). |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 172 | // `boundary` should be above the max enumerator sample. |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 173 | // |
| 174 | // TODO(qingsi): Refactor the default implementation given by RtcHistogram, |
| 175 | // which is already sparse, and remove the boundary argument from the macro. |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 176 | #define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \ |
Qingsi Wang | 36e0f57 | 2019-01-17 02:37:21 | [diff] [blame] | 177 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \ |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 178 | name, sample, \ |
Qingsi Wang | d6eb71e | 2018-06-26 19:30:04 | [diff] [blame] | 179 | webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary)) |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 180 | |
| 181 | // Histogram for percentage (evenly spaced buckets). |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 182 | #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 183 | RTC_HISTOGRAM_ENUMERATION(name, sample, 101) |
| 184 | |
Max Morin | 84cab20 | 2016-07-01 11:35:19 | [diff] [blame] | 185 | // Histogram for booleans. |
| 186 | #define RTC_HISTOGRAM_BOOLEAN(name, sample) \ |
| 187 | RTC_HISTOGRAM_ENUMERATION(name, sample, 2) |
| 188 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 189 | // Histogram for enumerators (evenly spaced buckets). |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 190 | // `boundary` should be above the max enumerator sample. |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 191 | #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
Qingsi Wang | 36e0f57 | 2019-01-17 02:37:21 | [diff] [blame] | 192 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 193 | name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 194 | webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary)) |
| 195 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 196 | // The name of the histogram should not vary. |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 197 | #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \ |
| 198 | factory_get_invocation) \ |
| 199 | do { \ |
| 200 | static std::atomic<webrtc::metrics::Histogram*> atomic_histogram_pointer( \ |
| 201 | nullptr); \ |
| 202 | webrtc::metrics::Histogram* histogram_pointer = \ |
| 203 | atomic_histogram_pointer.load(std::memory_order_acquire); \ |
| 204 | if (!histogram_pointer) { \ |
| 205 | histogram_pointer = factory_get_invocation; \ |
| 206 | webrtc::metrics::Histogram* null_histogram = nullptr; \ |
| 207 | atomic_histogram_pointer.compare_exchange_strong(null_histogram, \ |
| 208 | histogram_pointer); \ |
| 209 | } \ |
| 210 | if (histogram_pointer) { \ |
| 211 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 212 | } \ |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 213 | } while (0) |
| 214 | |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 215 | // The histogram is constructed/found for each call. |
sakal | 71b8393 | 2016-09-16 13:56:15 | [diff] [blame] | 216 | // May be used for histograms with infrequent updates.` |
asapersson | 1fe48a5 | 2016-01-07 09:02:42 | [diff] [blame] | 217 | #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \ |
sakal | 71b8393 | 2016-09-16 13:56:15 | [diff] [blame] | 218 | do { \ |
| 219 | webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \ |
| 220 | if (histogram_pointer) { \ |
| 221 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 222 | } \ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 223 | } while (0) |
| 224 | |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 225 | // Helper macros. |
| 226 | // Macros for calling a histogram with varying name (e.g. when using a metric |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 227 | // in different modes such as real-time vs screenshare). Fast, because pointer |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 228 | // is cached. `index` should be different for different names. Allowed `index` |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 229 | // values are 0, 1, and 2. |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 230 | #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 231 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 232 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 233 | |
| 234 | #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 235 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 236 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 237 | |
asapersson | 5265fed | 2016-04-18 09:58:47 | [diff] [blame] | 238 | #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 239 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 240 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)) |
asapersson | 5265fed | 2016-04-18 09:58:47 | [diff] [blame] | 241 | |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 242 | #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 243 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 244 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 245 | |
| 246 | #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 247 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 248 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 249 | |
| 250 | #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 251 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 252 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 253 | |
| 254 | #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 255 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 256 | RTC_HISTOGRAM_ENUMERATION(name, sample, boundary)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 257 | |
| 258 | #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 259 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 260 | RTC_HISTOGRAM_PERCENTAGE(name, sample)) |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 261 | |
| 262 | #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \ |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 263 | do { \ |
| 264 | switch (index) { \ |
| 265 | case 0: \ |
| 266 | macro_invocation; \ |
| 267 | break; \ |
| 268 | case 1: \ |
| 269 | macro_invocation; \ |
| 270 | break; \ |
| 271 | case 2: \ |
| 272 | macro_invocation; \ |
| 273 | break; \ |
| 274 | default: \ |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 275 | RTC_DCHECK_NOTREACHED(); \ |
ilnik | 6d5b4d6 | 2017-08-30 10:32:14 | [diff] [blame] | 276 | } \ |
asapersson | 040b79f | 2016-02-02 15:13:01 | [diff] [blame] | 277 | } while (0) |
| 278 | |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 279 | #else |
| 280 | |
| 281 | //////////////////////////////////////////////////////////////////////////////// |
| 282 | // This section defines no-op alternatives to the metrics macros when |
| 283 | // RTC_METRICS_ENABLED is defined. |
| 284 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 285 | #define RTC_HISTOGRAM_COUNTS_100(name, sample) \ |
| 286 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 287 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 288 | #define RTC_HISTOGRAM_COUNTS_200(name, sample) \ |
| 289 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 290 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 291 | #define RTC_HISTOGRAM_COUNTS_500(name, sample) \ |
| 292 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 293 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 294 | #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 295 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 296 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 297 | #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 298 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 299 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 300 | #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 301 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 302 | |
| 303 | #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
| 304 | webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count) |
| 305 | |
| 306 | #define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \ |
| 307 | webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count) |
| 308 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 309 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \ |
| 310 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 311 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 312 | #define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \ |
| 313 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 314 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 315 | #define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \ |
| 316 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 317 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 318 | #define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \ |
| 319 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 320 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 321 | #define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \ |
| 322 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 323 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 324 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \ |
| 325 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 326 | |
| 327 | #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \ |
| 328 | webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count) |
| 329 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 330 | #define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \ |
| 331 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 332 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 333 | #define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \ |
| 334 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 335 | |
| 336 | #define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \ |
| 337 | webrtc::metrics_impl::NoOp(name, sample, boundary) |
| 338 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 339 | #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 340 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 341 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 342 | #define RTC_HISTOGRAM_BOOLEAN(name, sample) \ |
| 343 | webrtc::metrics_impl::NoOp(name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 344 | |
| 345 | #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
| 346 | webrtc::metrics_impl::NoOp(name, sample, boundary) |
| 347 | |
| 348 | #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \ |
| 349 | factory_get_invocation) \ |
| 350 | webrtc::metrics_impl::NoOp(constant_name, sample, factory_get_invocation) |
| 351 | |
| 352 | #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \ |
| 353 | webrtc::metrics_impl::NoOp(name, sample, factory_get_invocation) |
| 354 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 355 | #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
| 356 | webrtc::metrics_impl::NoOp(index, name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 357 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 358 | #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
| 359 | webrtc::metrics_impl::NoOp(index, name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 360 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 361 | #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
| 362 | webrtc::metrics_impl::NoOp(index, name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 363 | |
| 364 | #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
| 365 | webrtc::metrics_impl::NoOp(index, name, sample) |
| 366 | |
| 367 | #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
| 368 | webrtc::metrics_impl::NoOp(index, name, sample) |
| 369 | |
| 370 | #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
| 371 | webrtc::metrics_impl::NoOp(index, name, sample) |
| 372 | |
| 373 | #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
| 374 | webrtc::metrics_impl::NoOp(index, name, sample, boundary) |
| 375 | |
Jared Siskin | 7220ee9 | 2023-05-03 09:06:14 | [diff] [blame] | 376 | #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
| 377 | webrtc::metrics_impl::NoOp(index, name, sample) |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 378 | |
| 379 | #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \ |
| 380 | webrtc::metrics_impl::NoOp(index, name, sample, macro_invocation) |
| 381 | |
| 382 | #endif // RTC_METRICS_ENABLED |
| 383 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 384 | namespace webrtc { |
| 385 | namespace metrics { |
| 386 | |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 | [diff] [blame] | 387 | // Time that should have elapsed for stats that are gathered once per call. |
Peter Kasting | 662d7f1 | 2022-05-04 19:57:00 | [diff] [blame] | 388 | constexpr int kMinRunTimeInSeconds = 10; |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 | [diff] [blame] | 389 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 390 | class Histogram; |
| 391 | |
| 392 | // Functions for getting pointer to histogram (constructs or finds the named |
| 393 | // histogram). |
| 394 | |
| 395 | // Get histogram for counters. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 396 | Histogram* HistogramFactoryGetCounts(absl::string_view name, |
Mirko Bonadei | 51868f5 | 2019-11-23 15:10:32 | [diff] [blame] | 397 | int min, |
| 398 | int max, |
| 399 | int bucket_count); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 400 | |
henrik.lundin | f29e05d | 2016-12-01 17:58:45 | [diff] [blame] | 401 | // Get histogram for counters with linear bucket spacing. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 402 | Histogram* HistogramFactoryGetCountsLinear(absl::string_view name, |
Mirko Bonadei | 51868f5 | 2019-11-23 15:10:32 | [diff] [blame] | 403 | int min, |
| 404 | int max, |
| 405 | int bucket_count); |
henrik.lundin | f29e05d | 2016-12-01 17:58:45 | [diff] [blame] | 406 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 407 | // Get histogram for enumerators. |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 408 | // `boundary` should be above the max enumerator sample. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 409 | Histogram* HistogramFactoryGetEnumeration(absl::string_view name, int boundary); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 410 | |
Qingsi Wang | d6eb71e | 2018-06-26 19:30:04 | [diff] [blame] | 411 | // Get sparse histogram for enumerators. |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 412 | // `boundary` should be above the max enumerator sample. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 413 | Histogram* SparseHistogramFactoryGetEnumeration(absl::string_view name, |
Mirko Bonadei | 51868f5 | 2019-11-23 15:10:32 | [diff] [blame] | 414 | int boundary); |
Qingsi Wang | d6eb71e | 2018-06-26 19:30:04 | [diff] [blame] | 415 | |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 416 | // Function for adding a `sample` to a histogram. |
Mirko Bonadei | 51868f5 | 2019-11-23 15:10:32 | [diff] [blame] | 417 | void HistogramAdd(Histogram* histogram_pointer, int sample); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 418 | |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 419 | struct SampleInfo { |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 420 | SampleInfo(absl::string_view name, int min, int max, size_t bucket_count); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 421 | ~SampleInfo(); |
| 422 | |
| 423 | const std::string name; |
| 424 | const int min; |
| 425 | const int max; |
| 426 | const size_t bucket_count; |
| 427 | std::map<int, int> samples; // <value, # of events> |
| 428 | }; |
| 429 | |
| 430 | // Enables collection of samples. |
| 431 | // This method should be called before any other call into webrtc. |
| 432 | void Enable(); |
| 433 | |
| 434 | // Gets histograms and clears all samples. |
| 435 | void GetAndReset( |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 436 | std::map<std::string, std::unique_ptr<SampleInfo>, rtc::AbslStringViewCmp>* |
| 437 | histograms); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 438 | |
| 439 | // Functions below are mainly for testing. |
| 440 | |
| 441 | // Clears all samples. |
| 442 | void Reset(); |
| 443 | |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 444 | // Returns the number of times the `sample` has been added to the histogram. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 445 | int NumEvents(absl::string_view name, int sample); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 446 | |
| 447 | // Returns the total number of added samples to the histogram. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 448 | int NumSamples(absl::string_view name); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 449 | |
| 450 | // Returns the minimum sample value (or -1 if the histogram has no samples). |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 451 | int MinSample(absl::string_view name); |
Mirko Bonadei | c1c2a88 | 2018-09-06 11:34:51 | [diff] [blame] | 452 | |
Steve Anton | c1e6e86 | 2019-03-04 22:43:44 | [diff] [blame] | 453 | // Returns a map with keys the samples with at least one event and values the |
| 454 | // number of events for that sample. |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 455 | std::map<int, int> Samples(absl::string_view name); |
Steve Anton | c1e6e86 | 2019-03-04 22:43:44 | [diff] [blame] | 456 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 | [diff] [blame] | 457 | } // namespace metrics |
| 458 | } // namespace webrtc |
| 459 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 460 | #endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |