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 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | |
| 16 | #include "webrtc/common_types.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/atomicops.h" |
| 18 | #include "webrtc/rtc_base/checks.h" |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 19 | |
| 20 | // Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate |
| 21 | // statistics. |
| 22 | // |
| 23 | // Histogram for counters. |
| 24 | // RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count); |
| 25 | // |
| 26 | // Histogram for enumerators. |
| 27 | // The boundary should be above the max enumerator sample. |
| 28 | // RTC_HISTOGRAM_ENUMERATION(name, sample, boundary); |
| 29 | // |
| 30 | // |
| 31 | // The macros use the methods HistogramFactoryGetCounts, |
| 32 | // HistogramFactoryGetEnumeration and HistogramAdd. |
| 33 | // |
| 34 | // Therefore, WebRTC clients must either: |
| 35 | // |
| 36 | // - provide implementations of |
| 37 | // Histogram* webrtc::metrics::HistogramFactoryGetCounts( |
| 38 | // const std::string& name, int sample, int min, int max, |
| 39 | // int bucket_count); |
| 40 | // Histogram* webrtc::metrics::HistogramFactoryGetEnumeration( |
| 41 | // const std::string& name, int sample, int boundary); |
| 42 | // void webrtc::metrics::HistogramAdd( |
| 43 | // Histogram* histogram_pointer, const std::string& name, int sample); |
| 44 | // |
| 45 | // - or link with the default implementations (i.e. |
mbonadei | 1e5b026 | 2017-02-22 00:55:32 -0800 | [diff] [blame] | 46 | // system_wrappers:metrics_default). |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 47 | // |
| 48 | // |
| 49 | // Example usage: |
| 50 | // |
| 51 | // RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100); |
| 52 | // |
| 53 | // enum Types { |
| 54 | // kTypeX, |
| 55 | // kTypeY, |
| 56 | // kBoundary, |
| 57 | // }; |
| 58 | // |
| 59 | // RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary); |
henrik.lundin | c9badd5 | 2016-12-06 03:58:58 -0800 | [diff] [blame] | 60 | // |
| 61 | // NOTE: It is recommended to do the Chromium review for modifications to |
| 62 | // histograms.xml before new metrics are committed to WebRTC. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | // Macros for adding samples to a named histogram. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 66 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 67 | // Histogram for counters (exponentially spaced buckets). |
| 68 | #define RTC_HISTOGRAM_COUNTS_100(name, sample) \ |
| 69 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50) |
| 70 | |
| 71 | #define RTC_HISTOGRAM_COUNTS_200(name, sample) \ |
| 72 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50) |
| 73 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 74 | #define RTC_HISTOGRAM_COUNTS_500(name, sample) \ |
| 75 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50) |
| 76 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 77 | #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 78 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50) |
| 79 | |
| 80 | #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 81 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50) |
| 82 | |
| 83 | #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 84 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50) |
| 85 | |
| 86 | #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 87 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 88 | webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count)) |
| 89 | |
henrik.lundin | f29e05d | 2016-12-01 09:58:45 -0800 | [diff] [blame] | 90 | #define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \ |
| 91 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
| 92 | webrtc::metrics::HistogramFactoryGetCountsLinear( \ |
| 93 | name, min, max, bucket_count)) |
| 94 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 95 | // Deprecated. |
| 96 | // TODO(asapersson): Remove. |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 97 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \ |
| 98 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50) |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 99 | |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 100 | #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \ |
| 101 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 102 | webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count)) |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 103 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 104 | // Histogram for percentage (evenly spaced buckets). |
| 105 | #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 106 | RTC_HISTOGRAM_ENUMERATION(name, sample, 101) |
| 107 | |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 108 | // Histogram for booleans. |
| 109 | #define RTC_HISTOGRAM_BOOLEAN(name, sample) \ |
| 110 | RTC_HISTOGRAM_ENUMERATION(name, sample, 2) |
| 111 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 112 | // Histogram for enumerators (evenly spaced buckets). |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 113 | // |boundary| should be above the max enumerator sample. |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 114 | #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 115 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 116 | webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary)) |
| 117 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 118 | // The name of the histogram should not vary. |
| 119 | // TODO(asapersson): Consider changing string to const char*. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 120 | #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \ |
| 121 | factory_get_invocation) \ |
| 122 | do { \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 123 | static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \ |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 124 | webrtc::metrics::Histogram* histogram_pointer = \ |
| 125 | rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \ |
| 126 | if (!histogram_pointer) { \ |
| 127 | histogram_pointer = factory_get_invocation; \ |
| 128 | webrtc::metrics::Histogram* prev_pointer = \ |
| 129 | rtc::AtomicOps::CompareAndSwapPtr( \ |
| 130 | &atomic_histogram_pointer, \ |
| 131 | static_cast<webrtc::metrics::Histogram*>(nullptr), \ |
| 132 | histogram_pointer); \ |
| 133 | RTC_DCHECK(prev_pointer == nullptr || \ |
| 134 | prev_pointer == histogram_pointer); \ |
| 135 | } \ |
| 136 | if (histogram_pointer) { \ |
| 137 | RTC_DCHECK_EQ(constant_name, \ |
| 138 | webrtc::metrics::GetHistogramName(histogram_pointer)) \ |
| 139 | << "The name should not vary."; \ |
| 140 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 141 | } \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 142 | } while (0) |
| 143 | |
| 144 | // Deprecated. |
| 145 | // The histogram is constructed/found for each call. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 146 | // May be used for histograms with infrequent updates.` |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 147 | #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \ |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 148 | do { \ |
| 149 | webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \ |
| 150 | if (histogram_pointer) { \ |
| 151 | webrtc::metrics::HistogramAdd(histogram_pointer, sample); \ |
| 152 | } \ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 153 | } while (0) |
| 154 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 155 | // Helper macros. |
| 156 | // Macros for calling a histogram with varying name (e.g. when using a metric |
| 157 | // in different modes such as real-time vs screenshare). |
| 158 | #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
| 159 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 160 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)) |
| 161 | |
| 162 | #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
| 163 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 164 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)) |
| 165 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 166 | #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
| 167 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 168 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)) |
| 169 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 170 | #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
| 171 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 172 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)) |
| 173 | |
| 174 | #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
| 175 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 176 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)) |
| 177 | |
| 178 | #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
| 179 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 180 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)) |
| 181 | |
| 182 | #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
| 183 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 184 | RTC_HISTOGRAM_ENUMERATION(name, sample, boundary)) |
| 185 | |
| 186 | #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
| 187 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 188 | RTC_HISTOGRAM_PERCENTAGE(name, sample)) |
| 189 | |
| 190 | #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \ |
| 191 | do { \ |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 192 | switch (index) { \ |
| 193 | case 0: \ |
| 194 | macro_invocation; \ |
| 195 | break; \ |
| 196 | case 1: \ |
| 197 | macro_invocation; \ |
| 198 | break; \ |
| 199 | case 2: \ |
| 200 | macro_invocation; \ |
| 201 | break; \ |
| 202 | default: \ |
| 203 | RTC_NOTREACHED(); \ |
| 204 | } \ |
| 205 | } while (0) |
| 206 | |
| 207 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 208 | namespace webrtc { |
| 209 | namespace metrics { |
| 210 | |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 +0000 | [diff] [blame] | 211 | // Time that should have elapsed for stats that are gathered once per call. |
| 212 | enum { kMinRunTimeInSeconds = 10 }; |
| 213 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 214 | class Histogram; |
| 215 | |
| 216 | // Functions for getting pointer to histogram (constructs or finds the named |
| 217 | // histogram). |
| 218 | |
| 219 | // Get histogram for counters. |
| 220 | Histogram* HistogramFactoryGetCounts( |
| 221 | const std::string& name, int min, int max, int bucket_count); |
| 222 | |
henrik.lundin | f29e05d | 2016-12-01 09:58:45 -0800 | [diff] [blame] | 223 | // Get histogram for counters with linear bucket spacing. |
| 224 | Histogram* HistogramFactoryGetCountsLinear(const std::string& name, |
| 225 | int min, |
| 226 | int max, |
| 227 | int bucket_count); |
| 228 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 229 | // Get histogram for enumerators. |
| 230 | // |boundary| should be above the max enumerator sample. |
| 231 | Histogram* HistogramFactoryGetEnumeration( |
| 232 | const std::string& name, int boundary); |
| 233 | |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 234 | // Returns name of the histogram. |
| 235 | const std::string& GetHistogramName(Histogram* histogram_pointer); |
| 236 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 237 | // Function for adding a |sample| to a histogram. |
sakal | 71b8393 | 2016-09-16 06:56:15 -0700 | [diff] [blame] | 238 | void HistogramAdd(Histogram* histogram_pointer, int sample); |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 239 | |
| 240 | } // namespace metrics |
| 241 | } // namespace webrtc |
| 242 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 243 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |