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