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. |
andresp@webrtc.org | 86e1e48 | 2015-01-14 09:30:52 +0000 | [diff] [blame] | 47 | // system_wrappers/system_wrappers.gyp: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); |
| 61 | |
| 62 | |
| 63 | // Macros for adding samples to a named histogram. |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 64 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 65 | // Histogram for counters (exponentially spaced buckets). |
| 66 | #define RTC_HISTOGRAM_COUNTS_100(name, sample) \ |
| 67 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50) |
| 68 | |
| 69 | #define RTC_HISTOGRAM_COUNTS_200(name, sample) \ |
| 70 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50) |
| 71 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 72 | #define RTC_HISTOGRAM_COUNTS_500(name, sample) \ |
| 73 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50) |
| 74 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 75 | #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 76 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50) |
| 77 | |
| 78 | #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 79 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50) |
| 80 | |
| 81 | #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 82 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50) |
| 83 | |
| 84 | #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 85 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, false, \ |
| 86 | webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count)) |
| 87 | |
| 88 | // RTC_HISTOGRAM_COUNTS with logging. |
| 89 | #define RTC_LOGGED_HISTOGRAM_COUNTS_100(name, sample) \ |
| 90 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100, 50) |
| 91 | |
| 92 | #define RTC_LOGGED_HISTOGRAM_COUNTS_200(name, sample) \ |
| 93 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 200, 50) |
| 94 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 95 | #define RTC_LOGGED_HISTOGRAM_COUNTS_500(name, sample) \ |
| 96 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 500, 50) |
| 97 | |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 98 | #define RTC_LOGGED_HISTOGRAM_COUNTS_1000(name, sample) \ |
| 99 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50) |
| 100 | |
| 101 | #define RTC_LOGGED_HISTOGRAM_COUNTS_10000(name, sample) \ |
| 102 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50) |
| 103 | |
| 104 | #define RTC_LOGGED_HISTOGRAM_COUNTS_100000(name, sample) \ |
| 105 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50) |
| 106 | |
| 107 | #define RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \ |
| 108 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, true, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 109 | webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count)) |
| 110 | |
| 111 | // Deprecated. |
| 112 | // TODO(asapersson): Remove. |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 113 | #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \ |
| 114 | RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50) |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 115 | |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 116 | #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \ |
| 117 | RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 118 | webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count)) |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 119 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 120 | // Histogram for percentage (evenly spaced buckets). |
| 121 | #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 122 | RTC_HISTOGRAM_ENUMERATION(name, sample, 101) |
| 123 | |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 124 | // RTC_HISTOGRAM_PERCENTAGE with logging. |
| 125 | #define RTC_LOGGED_HISTOGRAM_PERCENTAGE(name, sample) \ |
| 126 | RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, 101) |
| 127 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 128 | // Histogram for enumerators (evenly spaced buckets). |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 129 | // |boundary| should be above the max enumerator sample. |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 130 | #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 131 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, false, \ |
| 132 | webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary)) |
| 133 | |
| 134 | // RTC_HISTOGRAM_ENUMERATION with logging. |
| 135 | #define RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, boundary) \ |
| 136 | RTC_HISTOGRAM_COMMON_BLOCK(name, sample, true, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 137 | webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary)) |
| 138 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 139 | // The name of the histogram should not vary. |
| 140 | // TODO(asapersson): Consider changing string to const char*. |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 141 | #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, log, \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 142 | factory_get_invocation) \ |
| 143 | do { \ |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 144 | if (log) { \ |
| 145 | LOG(LS_INFO) << constant_name << " " << sample; \ |
| 146 | } \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 147 | static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \ |
| 148 | webrtc::metrics::Histogram* histogram_pointer = \ |
| 149 | rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \ |
| 150 | if (!histogram_pointer) { \ |
| 151 | histogram_pointer = factory_get_invocation; \ |
| 152 | webrtc::metrics::Histogram* prev_pointer = \ |
| 153 | rtc::AtomicOps::CompareAndSwapPtr( \ |
| 154 | &atomic_histogram_pointer, \ |
| 155 | static_cast<webrtc::metrics::Histogram*>(nullptr), \ |
| 156 | histogram_pointer); \ |
| 157 | RTC_DCHECK(prev_pointer == nullptr || \ |
| 158 | prev_pointer == histogram_pointer); \ |
| 159 | } \ |
| 160 | webrtc::metrics::HistogramAdd(histogram_pointer, constant_name, sample); \ |
| 161 | } while (0) |
| 162 | |
| 163 | // Deprecated. |
| 164 | // The histogram is constructed/found for each call. |
| 165 | // May be used for histograms with infrequent updates. |
| 166 | #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 167 | do { \ |
| 168 | webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \ |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 169 | webrtc::metrics::HistogramAdd(histogram_pointer, name, sample); \ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 170 | } while (0) |
| 171 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 172 | |
| 173 | // Helper macros. |
| 174 | // Macros for calling a histogram with varying name (e.g. when using a metric |
| 175 | // in different modes such as real-time vs screenshare). |
| 176 | #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
| 177 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 178 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)) |
| 179 | |
| 180 | #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
| 181 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 182 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)) |
| 183 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 184 | #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
| 185 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 186 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)) |
| 187 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 188 | #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
| 189 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 190 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)) |
| 191 | |
| 192 | #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
| 193 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 194 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)) |
| 195 | |
| 196 | #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
| 197 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 198 | RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)) |
| 199 | |
| 200 | #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
| 201 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 202 | RTC_HISTOGRAM_ENUMERATION(name, sample, boundary)) |
| 203 | |
| 204 | #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
| 205 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 206 | RTC_HISTOGRAM_PERCENTAGE(name, sample)) |
| 207 | |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 208 | // RTC_HISTOGRAMS_COUNTS with logging. |
| 209 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_100(index, name, sample) \ |
| 210 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 211 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)) |
| 212 | |
| 213 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_200(index, name, sample) \ |
| 214 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 215 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)) |
| 216 | |
asapersson | 5265fed | 2016-04-18 02:58:47 -0700 | [diff] [blame] | 217 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_500(index, name, sample) \ |
| 218 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 219 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)) |
| 220 | |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 221 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_1000(index, name, sample) \ |
| 222 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 223 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)) |
| 224 | |
| 225 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_10000(index, name, sample) \ |
| 226 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 227 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)) |
| 228 | |
| 229 | #define RTC_LOGGED_HISTOGRAMS_COUNTS_100000(index, name, sample) \ |
| 230 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 231 | RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)) |
| 232 | |
| 233 | #define RTC_LOGGED_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \ |
| 234 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 235 | RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, boundary)) |
| 236 | |
| 237 | #define RTC_LOGGED_HISTOGRAMS_PERCENTAGE(index, name, sample) \ |
| 238 | RTC_HISTOGRAMS_COMMON(index, name, sample, \ |
| 239 | RTC_LOGGED_HISTOGRAM_PERCENTAGE(name, sample)) |
| 240 | |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 241 | #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \ |
| 242 | do { \ |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 243 | switch (index) { \ |
| 244 | case 0: \ |
| 245 | macro_invocation; \ |
| 246 | break; \ |
| 247 | case 1: \ |
| 248 | macro_invocation; \ |
| 249 | break; \ |
| 250 | case 2: \ |
| 251 | macro_invocation; \ |
| 252 | break; \ |
| 253 | default: \ |
| 254 | RTC_NOTREACHED(); \ |
| 255 | } \ |
| 256 | } while (0) |
| 257 | |
| 258 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 259 | namespace webrtc { |
| 260 | namespace metrics { |
| 261 | |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 +0000 | [diff] [blame] | 262 | // Time that should have elapsed for stats that are gathered once per call. |
| 263 | enum { kMinRunTimeInSeconds = 10 }; |
| 264 | |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 265 | class Histogram; |
| 266 | |
| 267 | // Functions for getting pointer to histogram (constructs or finds the named |
| 268 | // histogram). |
| 269 | |
| 270 | // Get histogram for counters. |
| 271 | Histogram* HistogramFactoryGetCounts( |
| 272 | const std::string& name, int min, int max, int bucket_count); |
| 273 | |
| 274 | // Get histogram for enumerators. |
| 275 | // |boundary| should be above the max enumerator sample. |
| 276 | Histogram* HistogramFactoryGetEnumeration( |
| 277 | const std::string& name, int boundary); |
| 278 | |
| 279 | // Function for adding a |sample| to a histogram. |
| 280 | // |name| can be used to verify that it matches the histogram name. |
| 281 | void HistogramAdd( |
| 282 | Histogram* histogram_pointer, const std::string& name, int sample); |
| 283 | |
| 284 | } // namespace metrics |
| 285 | } // namespace webrtc |
| 286 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 287 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_ |
asapersson@webrtc.org | 580d367 | 2014-10-23 12:57:56 +0000 | [diff] [blame] | 288 | |