asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [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 06:47:31 +0200 | [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 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Mirko Bonadei | c1c2a88 | 2018-09-06 13:34:51 +0200 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <memory> |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 22 | |
| 23 | // Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate |
| 24 | // statistics. |
| 25 | // |
| 26 | // Histogram for counters. |
| 27 | // RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count); |
| 28 | // |
| 29 | // Histogram for enumerators. |
| 30 | // The boundary should be above the max enumerator sample. |
| 31 | // RTC_HISTOGRAM_ENUMERATION(name, sample, boundary); |
| 32 | // |
| 33 | // |
| 34 | // The macros use the methods HistogramFactoryGetCounts, |
| 35 | // HistogramFactoryGetEnumeration and HistogramAdd. |
| 36 | // |
Mirko Bonadei | c1c2a88 | 2018-09-06 13:34:51 +0200 | [diff] [blame] | 37 | // By default WebRTC provides implementations of the aforementioned methods |
| 38 | // that can be found in system_wrappers/source/metrics.cc. If clients want to |
| 39 | // provide a custom version, they will have to: |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 40 | // |
Mirko Bonadei | c1c2a88 | 2018-09-06 13:34:51 +0200 | [diff] [blame] | 41 | // 1. Compile WebRTC defining the preprocessor macro |
| 42 | // WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved |
| 43 | // by setting the GN arg rtc_exclude_metrics_default to true). |
| 44 | // 2. Provide implementations of: |
| 45 | // Histogram* webrtc::metrics::HistogramFactoryGetCounts( |
| 46 | // const std::string& name, int sample, int min, int max, |
| 47 | // int bucket_count); |
| 48 | // Histogram* webrtc::metrics::HistogramFactoryGetEnumeration( |
| 49 | // const std::string& name, int sample, int boundary); |
| 50 | // void webrtc::metrics::HistogramAdd( |
| 51 | // Histogram* histogram_pointer, const std::string& name, int sample); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 52 | // |
| 53 | // Example usage: |
| 54 | // |
| 55 | // RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100); |
| 56 | // |
| 57 | // enum Types { |
| 58 | // kTypeX, |
| 59 | // kTypeY, |
| 60 | // kBoundary, |
| 61 | // }; |
| 62 | // |
| 63 | // RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary); |
henrik.lundin | c9badd5 | 2016-12-06 03:58:58 -0800 | [diff] [blame] | 64 | // |
| 65 | // NOTE: It is recommended to do the Chromium review for modifications to |
| 66 | // histograms.xml before new metrics are committed to WebRTC. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 67 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 68 | // Macros for adding samples to a named histogram. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 69 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 70 | // Histogram for counters (exponentially spaced buckets). |
| 71 | #define RTC_HISTOGRAM_COUNTS_100(name, sample) \ |
| 72 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50) |
| 73 | |
| 74 | #define RTC_HISTOGRAM_COUNTS_200(name, sample) \ |
| 75 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50) |
| 76 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 77 | #define RTC_HISTOGRAM_COUNTS_500(name, sample) \ |
| 78 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50) |
| 79 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 80 | #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 81 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50) |
| 82 | |
| 83 | #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 84 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50) |
| 85 | |
| 86 | #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 87 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50) |
| 88 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 89 | #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
| 90 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
| 91 | webrtc::metrics::HistogramFactoryGetCounts( \ |
| 92 | name, min, max, bucket_count)) |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 93 | |
henrik.lundin | f29e05d | 2016-12-01 09:58:45 -0800 | [diff] [blame] | 94 | #define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \ |
| 95 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
| 96 | webrtc::metrics::HistogramFactoryGetCountsLinear( \ |
| 97 | name, min, max, bucket_count)) |
| 98 | |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 99 | // Slow metrics: pointer to metric is acquired at each call and is not cached. |
| 100 | // |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 101 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \ |
| 102 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50) |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 103 | |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 104 | #define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \ |
| 105 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50) |
| 106 | |
| 107 | #define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \ |
| 108 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50) |
| 109 | |
| 110 | #define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \ |
| 111 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50) |
| 112 | |
| 113 | #define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \ |
| 114 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50) |
| 115 | |
| 116 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \ |
| 117 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50) |
| 118 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 119 | #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \ |
| 120 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \ |
| 121 | webrtc::metrics::HistogramFactoryGetCounts( \ |
| 122 | name, min, max, bucket_count)) |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 123 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 124 | // Histogram for percentage (evenly spaced buckets). |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 125 | #define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \ |
| 126 | RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101) |
| 127 | |
| 128 | // Histogram for booleans. |
| 129 | #define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \ |
| 130 | RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2) |
| 131 | |
| 132 | // Histogram for enumerators (evenly spaced buckets). |
| 133 | // |boundary| should be above the max enumerator sample. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 134 | // |
| 135 | // TODO(qingsi): Refactor the default implementation given by RtcHistogram, |
| 136 | // which is already sparse, and remove the boundary argument from the macro. |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 137 | #define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \ |
Qingsi Wang | 36e0f57 | 2019-01-16 18:37:21 -0800 | [diff] [blame] | 138 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \ |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 139 | name, sample, \ |
Qingsi Wang | d6eb71e | 2018-06-26 12:30:04 -0700 | [diff] [blame] | 140 | webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary)) |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 141 | |
| 142 | // Histogram for percentage (evenly spaced buckets). |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 143 | #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 144 | RTC_HISTOGRAM_ENUMERATION(name, sample, 101) |
| 145 | |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 146 | // Histogram for booleans. |
| 147 | #define RTC_HISTOGRAM_BOOLEAN(name, sample) \ |
| 148 | RTC_HISTOGRAM_ENUMERATION(name, sample, 2) |
| 149 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 150 | // Histogram for enumerators (evenly spaced buckets). |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 151 | // |boundary| should be above the max enumerator sample. |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 152 | #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
Qingsi Wang | 36e0f57 | 2019-01-16 18:37:21 -0800 | [diff] [blame] | 153 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 154 | name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 155 | webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary)) |
| 156 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 157 | // The name of the histogram should not vary. |
| 158 | // TODO(asapersson): Consider changing string to const char*. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 159 | #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \ |
| 160 | factory_get_invocation) \ |
| 161 | do { \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 162 | static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \ |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 163 | webrtc::metrics::Histogram* histogram_pointer = \ |
| 164 | rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \ |
| 165 | if (!histogram_pointer) { \ |
| 166 | histogram_pointer = factory_get_invocation; \ |
| 167 | webrtc::metrics::Histogram* prev_pointer = \ |
| 168 | rtc::AtomicOps::CompareAndSwapPtr( \ |
| 169 | &atomic_histogram_pointer, \ |
| 170 | static_cast<webrtc::metrics::Histogram*>(nullptr), \ |
| 171 | histogram_pointer); \ |
| 172 | RTC_DCHECK(prev_pointer == nullptr || \ |
| 173 | prev_pointer == histogram_pointer); \ |
| 174 | } \ |
| 175 | if (histogram_pointer) { \ |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 176 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 177 | } \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 178 | } while (0) |
| 179 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 180 | // The histogram is constructed/found for each call. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 181 | // May be used for histograms with infrequent updates.` |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 182 | #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \ |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 183 | do { \ |
| 184 | webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \ |
| 185 | if (histogram_pointer) { \ |
| 186 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 187 | } \ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 188 | } while (0) |
| 189 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 190 | // Helper macros. |
| 191 | // Macros for calling a histogram with varying name (e.g. when using a metric |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 192 | // in different modes such as real-time vs screenshare). Fast, because pointer |
| 193 | // is cached. |index| should be different for different names. Allowed |index| |
| 194 | // values are 0, 1, and 2. |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 195 | #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 196 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 197 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 198 | |
| 199 | #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 200 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 201 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 202 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 203 | #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 204 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 205 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)) |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 206 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 207 | #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 208 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 209 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 210 | |
| 211 | #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 212 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 213 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 214 | |
| 215 | #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 216 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 217 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 218 | |
| 219 | #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 220 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 221 | RTC_HISTOGRAM_ENUMERATION(name, sample, boundary)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 222 | |
| 223 | #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 224 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 225 | RTC_HISTOGRAM_PERCENTAGE(name, sample)) |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 226 | |
| 227 | #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \ |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 228 | do { \ |
| 229 | switch (index) { \ |
| 230 | case 0: \ |
| 231 | macro_invocation; \ |
| 232 | break; \ |
| 233 | case 1: \ |
| 234 | macro_invocation; \ |
| 235 | break; \ |
| 236 | case 2: \ |
| 237 | macro_invocation; \ |
| 238 | break; \ |
| 239 | default: \ |
| 240 | RTC_NOTREACHED(); \ |
| 241 | } \ |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 242 | } while (0) |
| 243 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 244 | namespace webrtc { |
| 245 | namespace metrics { |
| 246 | |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 +0000 | [diff] [blame] | 247 | // Time that should have elapsed for stats that are gathered once per call. |
| 248 | enum { kMinRunTimeInSeconds = 10 }; |
| 249 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 250 | class Histogram; |
| 251 | |
| 252 | // Functions for getting pointer to histogram (constructs or finds the named |
| 253 | // histogram). |
| 254 | |
| 255 | // Get histogram for counters. |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 256 | Histogram* HistogramFactoryGetCounts(const std::string& name, |
| 257 | int min, |
| 258 | int max, |
| 259 | int bucket_count); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 260 | |
henrik.lundin | f29e05d | 2016-12-01 09:58:45 -0800 | [diff] [blame] | 261 | // Get histogram for counters with linear bucket spacing. |
| 262 | Histogram* HistogramFactoryGetCountsLinear(const std::string& name, |
| 263 | int min, |
| 264 | int max, |
| 265 | int bucket_count); |
| 266 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 267 | // Get histogram for enumerators. |
| 268 | // |boundary| should be above the max enumerator sample. |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 269 | Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
| 270 | int boundary); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 271 | |
Qingsi Wang | d6eb71e | 2018-06-26 12:30:04 -0700 | [diff] [blame] | 272 | // Get sparse histogram for enumerators. |
| 273 | // |boundary| should be above the max enumerator sample. |
| 274 | Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name, |
| 275 | int boundary); |
| 276 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 277 | // Function for adding a |sample| to a histogram. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 278 | void HistogramAdd(Histogram* histogram_pointer, int sample); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 279 | |
Mirko Bonadei | c1c2a88 | 2018-09-06 13:34:51 +0200 | [diff] [blame] | 280 | struct SampleInfo { |
| 281 | SampleInfo(const std::string& name, int min, int max, size_t bucket_count); |
| 282 | ~SampleInfo(); |
| 283 | |
| 284 | const std::string name; |
| 285 | const int min; |
| 286 | const int max; |
| 287 | const size_t bucket_count; |
| 288 | std::map<int, int> samples; // <value, # of events> |
| 289 | }; |
| 290 | |
| 291 | // Enables collection of samples. |
| 292 | // This method should be called before any other call into webrtc. |
| 293 | void Enable(); |
| 294 | |
| 295 | // Gets histograms and clears all samples. |
| 296 | void GetAndReset( |
| 297 | std::map<std::string, std::unique_ptr<SampleInfo>>* histograms); |
| 298 | |
| 299 | // Functions below are mainly for testing. |
| 300 | |
| 301 | // Clears all samples. |
| 302 | void Reset(); |
| 303 | |
| 304 | // Returns the number of times the |sample| has been added to the histogram. |
| 305 | int NumEvents(const std::string& name, int sample); |
| 306 | |
| 307 | // Returns the total number of added samples to the histogram. |
| 308 | int NumSamples(const std::string& name); |
| 309 | |
| 310 | // Returns the minimum sample value (or -1 if the histogram has no samples). |
| 311 | int MinSample(const std::string& name); |
| 312 | |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 313 | // Returns a map with keys the samples with at least one event and values the |
| 314 | // number of events for that sample. |
| 315 | std::map<int, int> Samples(const std::string& name); |
| 316 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 317 | } // namespace metrics |
| 318 | } // namespace webrtc |
| 319 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 320 | #endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |