blob: f00ecf2622aefd817717086b63ab4eb4d9b33929 [file] [log] [blame]
asapersson@webrtc.org580d3672014-10-23 12:57:56 +00001//
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
12#define SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000013
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020014#include <map>
15#include <memory>
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000016#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/atomicops.h"
19#include "rtc_base/checks.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000020
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//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020035// By default WebRTC provides implementations of the aforementioned methods
36// that can be found in system_wrappers/source/metrics.cc. If clients want to
37// provide a custom version, they will have to:
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000038//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020039// 1. Compile WebRTC defining the preprocessor macro
40// WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
41// by setting the GN arg rtc_exclude_metrics_default to true).
42// 2. Provide implementations of:
43// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
44// const std::string& name, int sample, int min, int max,
45// int bucket_count);
46// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
47// const std::string& name, int sample, int boundary);
48// void webrtc::metrics::HistogramAdd(
49// Histogram* histogram_pointer, const std::string& name, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000050//
51// Example usage:
52//
53// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
54//
55// enum Types {
56// kTypeX,
57// kTypeY,
58// kBoundary,
59// };
60//
61// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 03:58:58 -080062//
63// NOTE: It is recommended to do the Chromium review for modifications to
64// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000065
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000066// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000067
asapersson1fe48a52016-01-07 01:02:42 -080068// 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
asapersson5265fed2016-04-18 02:58:47 -070075#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
76 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
77
asapersson1fe48a52016-01-07 01:02:42 -080078#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
Karl Wiberg79eb1d92017-11-08 12:26:07 +010087#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
88 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
89 webrtc::metrics::HistogramFactoryGetCounts( \
90 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 01:02:42 -080091
henrik.lundinf29e05d2016-12-01 09:58:45 -080092#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
93 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
94 webrtc::metrics::HistogramFactoryGetCountsLinear( \
95 name, min, max, bucket_count))
96
ilnik6d5b4d62017-08-30 03:32:14 -070097// Slow metrics: pointer to metric is acquired at each call and is not cached.
98//
asapersson53805322015-12-21 01:46:20 -080099#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
100 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000101
ilnik6d5b4d62017-08-30 03:32:14 -0700102#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
103 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
104
105#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
106 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
107
108#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
109 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
110
111#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
112 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
113
114#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
115 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
116
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100117#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
118 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
119 webrtc::metrics::HistogramFactoryGetCounts( \
120 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000121
asapersson1fe48a52016-01-07 01:02:42 -0800122// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 03:32:14 -0700123#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
124 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
125
126// Histogram for booleans.
127#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
128 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
129
130// Histogram for enumerators (evenly spaced buckets).
131// |boundary| should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700132//
133// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
134// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 03:32:14 -0700135#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700136 RTC_HISTOGRAM_COMMON_BLOCK( \
ilnik6d5b4d62017-08-30 03:32:14 -0700137 name, sample, \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700138 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 03:32:14 -0700139
140// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 01:02:42 -0800141#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
142 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
143
Max Morin84cab202016-07-01 13:35:19 +0200144// Histogram for booleans.
145#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
146 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
147
asapersson1fe48a52016-01-07 01:02:42 -0800148// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000149// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800150#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100151 RTC_HISTOGRAM_COMMON_BLOCK( \
152 name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800153 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
154
asapersson1fe48a52016-01-07 01:02:42 -0800155// The name of the histogram should not vary.
156// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700157#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
158 factory_get_invocation) \
159 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800160 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700161 webrtc::metrics::Histogram* histogram_pointer = \
162 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
163 if (!histogram_pointer) { \
164 histogram_pointer = factory_get_invocation; \
165 webrtc::metrics::Histogram* prev_pointer = \
166 rtc::AtomicOps::CompareAndSwapPtr( \
167 &atomic_histogram_pointer, \
168 static_cast<webrtc::metrics::Histogram*>(nullptr), \
169 histogram_pointer); \
170 RTC_DCHECK(prev_pointer == nullptr || \
171 prev_pointer == histogram_pointer); \
172 } \
173 if (histogram_pointer) { \
sakal71b83932016-09-16 06:56:15 -0700174 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
175 } \
asapersson1fe48a52016-01-07 01:02:42 -0800176 } while (0)
177
178// Deprecated.
179// The histogram is constructed/found for each call.
sakal71b83932016-09-16 06:56:15 -0700180// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 01:02:42 -0800181#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 06:56:15 -0700182 do { \
183 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
184 if (histogram_pointer) { \
185 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
186 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000187 } while (0)
188
asapersson040b79f2016-02-02 07:13:01 -0800189// Helper macros.
190// Macros for calling a histogram with varying name (e.g. when using a metric
ilnik6d5b4d62017-08-30 03:32:14 -0700191// in different modes such as real-time vs screenshare). Fast, because pointer
192// is cached. |index| should be different for different names. Allowed |index|
193// values are 0, 1, and 2.
asapersson040b79f2016-02-02 07:13:01 -0800194#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100195 RTC_HISTOGRAMS_COMMON(index, name, sample, \
196 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
asapersson040b79f2016-02-02 07:13:01 -0800197
198#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100199 RTC_HISTOGRAMS_COMMON(index, name, sample, \
200 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
asapersson040b79f2016-02-02 07:13:01 -0800201
asapersson5265fed2016-04-18 02:58:47 -0700202#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100203 RTC_HISTOGRAMS_COMMON(index, name, sample, \
204 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
asapersson5265fed2016-04-18 02:58:47 -0700205
asapersson040b79f2016-02-02 07:13:01 -0800206#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100207 RTC_HISTOGRAMS_COMMON(index, name, sample, \
208 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800209
210#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100211 RTC_HISTOGRAMS_COMMON(index, name, sample, \
212 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800213
214#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100215 RTC_HISTOGRAMS_COMMON(index, name, sample, \
216 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800217
218#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100219 RTC_HISTOGRAMS_COMMON(index, name, sample, \
220 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
asapersson040b79f2016-02-02 07:13:01 -0800221
222#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100223 RTC_HISTOGRAMS_COMMON(index, name, sample, \
224 RTC_HISTOGRAM_PERCENTAGE(name, sample))
asapersson040b79f2016-02-02 07:13:01 -0800225
226#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
ilnik6d5b4d62017-08-30 03:32:14 -0700227 do { \
228 switch (index) { \
229 case 0: \
230 macro_invocation; \
231 break; \
232 case 1: \
233 macro_invocation; \
234 break; \
235 case 2: \
236 macro_invocation; \
237 break; \
238 default: \
239 RTC_NOTREACHED(); \
240 } \
asapersson040b79f2016-02-02 07:13:01 -0800241 } while (0)
242
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000243namespace webrtc {
244namespace metrics {
245
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000246// Time that should have elapsed for stats that are gathered once per call.
247enum { kMinRunTimeInSeconds = 10 };
248
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000249class Histogram;
250
251// Functions for getting pointer to histogram (constructs or finds the named
252// histogram).
253
254// Get histogram for counters.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100255Histogram* HistogramFactoryGetCounts(const std::string& name,
256 int min,
257 int max,
258 int bucket_count);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000259
henrik.lundinf29e05d2016-12-01 09:58:45 -0800260// Get histogram for counters with linear bucket spacing.
261Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
262 int min,
263 int max,
264 int bucket_count);
265
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000266// Get histogram for enumerators.
267// |boundary| should be above the max enumerator sample.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100268Histogram* HistogramFactoryGetEnumeration(const std::string& name,
269 int boundary);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000270
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700271// Get sparse histogram for enumerators.
272// |boundary| should be above the max enumerator sample.
273Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
274 int boundary);
275
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000276// Function for adding a |sample| to a histogram.
sakal71b83932016-09-16 06:56:15 -0700277void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000278
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200279struct SampleInfo {
280 SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
281 ~SampleInfo();
282
283 const std::string name;
284 const int min;
285 const int max;
286 const size_t bucket_count;
287 std::map<int, int> samples; // <value, # of events>
288};
289
290// Enables collection of samples.
291// This method should be called before any other call into webrtc.
292void Enable();
293
294// Gets histograms and clears all samples.
295void GetAndReset(
296 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
297
298// Functions below are mainly for testing.
299
300// Clears all samples.
301void Reset();
302
303// Returns the number of times the |sample| has been added to the histogram.
304int NumEvents(const std::string& name, int sample);
305
306// Returns the total number of added samples to the histogram.
307int NumSamples(const std::string& name);
308
309// Returns the minimum sample value (or -1 if the histogram has no samples).
310int MinSample(const std::string& name);
311
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000312} // namespace metrics
313} // namespace webrtc
314
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200315#endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_