blob: bad0f6592f7e91e33fb5dfab419a6b887e1d20c1 [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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020015#include <map>
16#include <memory>
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000017#include <string>
18
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000021
22// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
23// statistics.
24//
25// Histogram for counters.
26// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
27//
28// Histogram for enumerators.
29// The boundary should be above the max enumerator sample.
30// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
31//
32//
33// The macros use the methods HistogramFactoryGetCounts,
34// HistogramFactoryGetEnumeration and HistogramAdd.
35//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020036// By default WebRTC provides implementations of the aforementioned methods
37// that can be found in system_wrappers/source/metrics.cc. If clients want to
38// provide a custom version, they will have to:
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000039//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020040// 1. Compile WebRTC defining the preprocessor macro
41// WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
42// by setting the GN arg rtc_exclude_metrics_default to true).
43// 2. Provide implementations of:
44// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
45// const std::string& name, int sample, int min, int max,
46// int bucket_count);
47// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
48// const std::string& name, int sample, int boundary);
49// void webrtc::metrics::HistogramAdd(
50// Histogram* histogram_pointer, const std::string& name, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000051//
52// Example usage:
53//
54// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
55//
56// enum Types {
57// kTypeX,
58// kTypeY,
59// kBoundary,
60// };
61//
62// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 03:58:58 -080063//
64// NOTE: It is recommended to do the Chromium review for modifications to
65// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000066
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000067// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000068
asapersson1fe48a52016-01-07 01:02:42 -080069// Histogram for counters (exponentially spaced buckets).
70#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
71 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
72
73#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
74 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
75
asapersson5265fed2016-04-18 02:58:47 -070076#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
77 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
78
asapersson1fe48a52016-01-07 01:02:42 -080079#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
80 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
81
82#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
83 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
84
85#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
86 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
87
Karl Wiberg79eb1d92017-11-08 12:26:07 +010088#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
89 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
90 webrtc::metrics::HistogramFactoryGetCounts( \
91 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 01:02:42 -080092
henrik.lundinf29e05d2016-12-01 09:58:45 -080093#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
94 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
95 webrtc::metrics::HistogramFactoryGetCountsLinear( \
96 name, min, max, bucket_count))
97
ilnik6d5b4d62017-08-30 03:32:14 -070098// Slow metrics: pointer to metric is acquired at each call and is not cached.
99//
asapersson53805322015-12-21 01:46:20 -0800100#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
101 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000102
ilnik6d5b4d62017-08-30 03:32:14 -0700103#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
104 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
105
106#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
107 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
108
109#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
110 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
111
112#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
113 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
114
115#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
116 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
117
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100118#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
119 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
120 webrtc::metrics::HistogramFactoryGetCounts( \
121 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000122
asapersson1fe48a52016-01-07 01:02:42 -0800123// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 03:32:14 -0700124#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
125 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
126
127// Histogram for booleans.
128#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
129 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
130
131// Histogram for enumerators (evenly spaced buckets).
132// |boundary| should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700133//
134// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
135// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 03:32:14 -0700136#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800137 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
ilnik6d5b4d62017-08-30 03:32:14 -0700138 name, sample, \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700139 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 03:32:14 -0700140
141// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 01:02:42 -0800142#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
143 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
144
Max Morin84cab202016-07-01 13:35:19 +0200145// Histogram for booleans.
146#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
147 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
148
asapersson1fe48a52016-01-07 01:02:42 -0800149// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000150// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800151#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800152 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100153 name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800154 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
155
asapersson1fe48a52016-01-07 01:02:42 -0800156// The name of the histogram should not vary.
157// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700158#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
159 factory_get_invocation) \
160 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800161 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700162 webrtc::metrics::Histogram* histogram_pointer = \
163 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
164 if (!histogram_pointer) { \
165 histogram_pointer = factory_get_invocation; \
166 webrtc::metrics::Histogram* prev_pointer = \
167 rtc::AtomicOps::CompareAndSwapPtr( \
168 &atomic_histogram_pointer, \
169 static_cast<webrtc::metrics::Histogram*>(nullptr), \
170 histogram_pointer); \
171 RTC_DCHECK(prev_pointer == nullptr || \
172 prev_pointer == histogram_pointer); \
173 } \
174 if (histogram_pointer) { \
sakal71b83932016-09-16 06:56:15 -0700175 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
176 } \
asapersson1fe48a52016-01-07 01:02:42 -0800177 } while (0)
178
asapersson1fe48a52016-01-07 01:02:42 -0800179// 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_