blob: 18c97574b1cf4f71376ba61339527b06997726d9 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020016#include <map>
17#include <memory>
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000018#include <string>
19
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000022
23// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
24// statistics.
25//
26// Histogram for counters.
27// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
28//
29// Histogram for enumerators.
30// The boundary should be above the max enumerator sample.
31// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
32//
33//
34// The macros use the methods HistogramFactoryGetCounts,
35// HistogramFactoryGetEnumeration and HistogramAdd.
36//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020037// By default WebRTC provides implementations of the aforementioned methods
38// that can be found in system_wrappers/source/metrics.cc. If clients want to
39// provide a custom version, they will have to:
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000040//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020041// 1. Compile WebRTC defining the preprocessor macro
42// WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
43// by setting the GN arg rtc_exclude_metrics_default to true).
44// 2. Provide implementations of:
45// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
46// const std::string& name, int sample, int min, int max,
47// int bucket_count);
48// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
49// const std::string& name, int sample, int boundary);
50// void webrtc::metrics::HistogramAdd(
51// Histogram* histogram_pointer, const std::string& name, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000052//
53// Example usage:
54//
55// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
56//
57// enum Types {
58// kTypeX,
59// kTypeY,
60// kBoundary,
61// };
62//
63// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 03:58:58 -080064//
65// NOTE: It is recommended to do the Chromium review for modifications to
66// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000067
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000068// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000069
asapersson1fe48a52016-01-07 01:02:42 -080070// Histogram for counters (exponentially spaced buckets).
71#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
72 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
73
74#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
75 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
76
asapersson5265fed2016-04-18 02:58:47 -070077#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
78 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
79
asapersson1fe48a52016-01-07 01:02:42 -080080#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
81 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
82
83#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
84 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
85
86#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
87 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
88
Karl Wiberg79eb1d92017-11-08 12:26:07 +010089#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
90 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
91 webrtc::metrics::HistogramFactoryGetCounts( \
92 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 01:02:42 -080093
henrik.lundinf29e05d2016-12-01 09:58:45 -080094#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
95 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
96 webrtc::metrics::HistogramFactoryGetCountsLinear( \
97 name, min, max, bucket_count))
98
ilnik6d5b4d62017-08-30 03:32:14 -070099// Slow metrics: pointer to metric is acquired at each call and is not cached.
100//
asapersson53805322015-12-21 01:46:20 -0800101#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
102 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000103
ilnik6d5b4d62017-08-30 03:32:14 -0700104#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
105 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
106
107#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
108 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
109
110#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
111 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
112
113#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
114 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
115
116#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
117 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
118
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100119#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
120 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
121 webrtc::metrics::HistogramFactoryGetCounts( \
122 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000123
asapersson1fe48a52016-01-07 01:02:42 -0800124// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 03:32:14 -0700125#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
126 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
127
128// Histogram for booleans.
129#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
130 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
131
132// Histogram for enumerators (evenly spaced buckets).
133// |boundary| should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700134//
135// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
136// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 03:32:14 -0700137#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800138 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
ilnik6d5b4d62017-08-30 03:32:14 -0700139 name, sample, \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700140 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 03:32:14 -0700141
142// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 01:02:42 -0800143#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
144 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
145
Max Morin84cab202016-07-01 13:35:19 +0200146// Histogram for booleans.
147#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
148 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
149
asapersson1fe48a52016-01-07 01:02:42 -0800150// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000151// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800152#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800153 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100154 name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800155 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
156
asapersson1fe48a52016-01-07 01:02:42 -0800157// The name of the histogram should not vary.
158// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700159#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
160 factory_get_invocation) \
161 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800162 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700163 webrtc::metrics::Histogram* histogram_pointer = \
164 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
165 if (!histogram_pointer) { \
166 histogram_pointer = factory_get_invocation; \
167 webrtc::metrics::Histogram* prev_pointer = \
168 rtc::AtomicOps::CompareAndSwapPtr( \
169 &atomic_histogram_pointer, \
170 static_cast<webrtc::metrics::Histogram*>(nullptr), \
171 histogram_pointer); \
172 RTC_DCHECK(prev_pointer == nullptr || \
173 prev_pointer == histogram_pointer); \
174 } \
175 if (histogram_pointer) { \
sakal71b83932016-09-16 06:56:15 -0700176 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
177 } \
asapersson1fe48a52016-01-07 01:02:42 -0800178 } while (0)
179
asapersson1fe48a52016-01-07 01:02:42 -0800180// The histogram is constructed/found for each call.
sakal71b83932016-09-16 06:56:15 -0700181// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 01:02:42 -0800182#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 06:56:15 -0700183 do { \
184 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
185 if (histogram_pointer) { \
186 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
187 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000188 } while (0)
189
asapersson040b79f2016-02-02 07:13:01 -0800190// Helper macros.
191// Macros for calling a histogram with varying name (e.g. when using a metric
ilnik6d5b4d62017-08-30 03:32:14 -0700192// in different modes such as real-time vs screenshare). Fast, because pointer
193// is cached. |index| should be different for different names. Allowed |index|
194// values are 0, 1, and 2.
asapersson040b79f2016-02-02 07:13:01 -0800195#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100196 RTC_HISTOGRAMS_COMMON(index, name, sample, \
197 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
asapersson040b79f2016-02-02 07:13:01 -0800198
199#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100200 RTC_HISTOGRAMS_COMMON(index, name, sample, \
201 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
asapersson040b79f2016-02-02 07:13:01 -0800202
asapersson5265fed2016-04-18 02:58:47 -0700203#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100204 RTC_HISTOGRAMS_COMMON(index, name, sample, \
205 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
asapersson5265fed2016-04-18 02:58:47 -0700206
asapersson040b79f2016-02-02 07:13:01 -0800207#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100208 RTC_HISTOGRAMS_COMMON(index, name, sample, \
209 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800210
211#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100212 RTC_HISTOGRAMS_COMMON(index, name, sample, \
213 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800214
215#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100216 RTC_HISTOGRAMS_COMMON(index, name, sample, \
217 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800218
219#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100220 RTC_HISTOGRAMS_COMMON(index, name, sample, \
221 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
asapersson040b79f2016-02-02 07:13:01 -0800222
223#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100224 RTC_HISTOGRAMS_COMMON(index, name, sample, \
225 RTC_HISTOGRAM_PERCENTAGE(name, sample))
asapersson040b79f2016-02-02 07:13:01 -0800226
227#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
ilnik6d5b4d62017-08-30 03:32:14 -0700228 do { \
229 switch (index) { \
230 case 0: \
231 macro_invocation; \
232 break; \
233 case 1: \
234 macro_invocation; \
235 break; \
236 case 2: \
237 macro_invocation; \
238 break; \
239 default: \
240 RTC_NOTREACHED(); \
241 } \
asapersson040b79f2016-02-02 07:13:01 -0800242 } while (0)
243
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000244namespace webrtc {
245namespace metrics {
246
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000247// Time that should have elapsed for stats that are gathered once per call.
248enum { kMinRunTimeInSeconds = 10 };
249
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000250class Histogram;
251
252// Functions for getting pointer to histogram (constructs or finds the named
253// histogram).
254
255// Get histogram for counters.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100256Histogram* HistogramFactoryGetCounts(const std::string& name,
257 int min,
258 int max,
259 int bucket_count);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000260
henrik.lundinf29e05d2016-12-01 09:58:45 -0800261// Get histogram for counters with linear bucket spacing.
262Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
263 int min,
264 int max,
265 int bucket_count);
266
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000267// Get histogram for enumerators.
268// |boundary| should be above the max enumerator sample.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100269Histogram* HistogramFactoryGetEnumeration(const std::string& name,
270 int boundary);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000271
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700272// Get sparse histogram for enumerators.
273// |boundary| should be above the max enumerator sample.
274Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
275 int boundary);
276
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000277// Function for adding a |sample| to a histogram.
sakal71b83932016-09-16 06:56:15 -0700278void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000279
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200280struct SampleInfo {
281 SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
282 ~SampleInfo();
283
284 const std::string name;
285 const int min;
286 const int max;
287 const size_t bucket_count;
288 std::map<int, int> samples; // <value, # of events>
289};
290
291// Enables collection of samples.
292// This method should be called before any other call into webrtc.
293void Enable();
294
295// Gets histograms and clears all samples.
296void GetAndReset(
297 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
298
299// Functions below are mainly for testing.
300
301// Clears all samples.
302void Reset();
303
304// Returns the number of times the |sample| has been added to the histogram.
305int NumEvents(const std::string& name, int sample);
306
307// Returns the total number of added samples to the histogram.
308int NumSamples(const std::string& name);
309
310// Returns the minimum sample value (or -1 if the histogram has no samples).
311int MinSample(const std::string& name);
312
Steve Antonc1e6e862019-03-04 14:43:44 -0800313// Returns a map with keys the samples with at least one event and values the
314// number of events for that sample.
315std::map<int, int> Samples(const std::string& name);
316
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000317} // namespace metrics
318} // namespace webrtc
319
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200320#endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_