blob: 6e2da1b9aca90c6e563123408762597407119a67 [file] [log] [blame]
asapersson@webrtc.org580d3672014-10-23 12:57:561//
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 04:47:3111#ifndef SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
12#define SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:5613
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Niels Möller7a669002022-06-27 07:47:0216#include <atomic>
Mirko Bonadeic1c2a882018-09-06 11:34:5117#include <map>
18#include <memory>
asapersson@webrtc.org580d3672014-10-23 12:57:5619#include <string>
20
Ali Tofigh969c13562022-05-13 08:26:5821#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "rtc_base/checks.h"
Ali Tofigh969c13562022-05-13 08:26:5823#include "rtc_base/string_utils.h"
asapersson@webrtc.org580d3672014-10-23 12:57:5624
Ying Wangef3998f2019-12-09 12:06:5325#if defined(RTC_DISABLE_METRICS)
26#define RTC_METRICS_ENABLED 0
27#else
28#define RTC_METRICS_ENABLED 1
29#endif
30
31namespace webrtc {
32namespace metrics_impl {
33template <typename... Ts>
34void NoOp(const Ts&...) {}
Jared Siskin7220ee92023-05-03 09:06:1435} // namespace metrics_impl
36} // namespace webrtc
Ying Wangef3998f2019-12-09 12:06:5337
38#if RTC_METRICS_ENABLED
39#define EXPECT_METRIC_EQ(val1, val2) EXPECT_EQ(val1, val2)
40#define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) \
41 EXPECT_EQ_WAIT(val1, val2, timeout)
42#define EXPECT_METRIC_GT(val1, val2) EXPECT_GT(val1, val2)
43#define EXPECT_METRIC_LE(val1, val2) EXPECT_LE(val1, val2)
44#define EXPECT_METRIC_TRUE(conditon) EXPECT_TRUE(conditon)
45#define EXPECT_METRIC_FALSE(conditon) EXPECT_FALSE(conditon)
46#define EXPECT_METRIC_THAT(value, matcher) EXPECT_THAT(value, matcher)
47#else
48#define EXPECT_METRIC_EQ(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
Jared Siskin7220ee92023-05-03 09:06:1449#define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) \
50 webrtc::metrics_impl::NoOp(val1, val2, timeout)
Ying Wangef3998f2019-12-09 12:06:5351#define EXPECT_METRIC_GT(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
52#define EXPECT_METRIC_LE(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
Jared Siskin7220ee92023-05-03 09:06:1453#define EXPECT_METRIC_TRUE(condition) \
54 webrtc::metrics_impl::NoOp(condition || true)
55#define EXPECT_METRIC_FALSE(condition) \
56 webrtc::metrics_impl::NoOp(condition && false)
57#define EXPECT_METRIC_THAT(value, matcher) \
58 webrtc::metrics_impl::NoOp(value, testing::_)
Ying Wangef3998f2019-12-09 12:06:5359#endif
60
61#if RTC_METRICS_ENABLED
asapersson@webrtc.org580d3672014-10-23 12:57:5662// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
63// statistics.
64//
65// Histogram for counters.
66// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
67//
68// Histogram for enumerators.
69// The boundary should be above the max enumerator sample.
70// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
71//
72//
73// The macros use the methods HistogramFactoryGetCounts,
74// HistogramFactoryGetEnumeration and HistogramAdd.
75//
Mirko Bonadeic1c2a882018-09-06 11:34:5176// By default WebRTC provides implementations of the aforementioned methods
77// that can be found in system_wrappers/source/metrics.cc. If clients want to
78// provide a custom version, they will have to:
asapersson@webrtc.org580d3672014-10-23 12:57:5679//
Mirko Bonadeic1c2a882018-09-06 11:34:5180// 1. Compile WebRTC defining the preprocessor macro
81// WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
82// by setting the GN arg rtc_exclude_metrics_default to true).
83// 2. Provide implementations of:
84// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
Ali Tofigh969c13562022-05-13 08:26:5885// absl::string_view name, int sample, int min, int max,
Mirko Bonadeic1c2a882018-09-06 11:34:5186// int bucket_count);
87// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
Ali Tofigh969c13562022-05-13 08:26:5888// absl::string_view name, int sample, int boundary);
Mirko Bonadeic1c2a882018-09-06 11:34:5189// void webrtc::metrics::HistogramAdd(
Ali Tofigh969c13562022-05-13 08:26:5890// Histogram* histogram_pointer, absl::string_view name, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:5691//
92// Example usage:
93//
94// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
95//
96// enum Types {
97// kTypeX,
98// kTypeY,
99// kBoundary,
100// };
101//
102// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 11:58:58103//
104// NOTE: It is recommended to do the Chromium review for modifications to
105// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56106
asapersson@webrtc.org580d3672014-10-23 12:57:56107// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56108
asapersson1fe48a52016-01-07 09:02:42109// Histogram for counters (exponentially spaced buckets).
110#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
111 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
112
113#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
114 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
115
asapersson5265fed2016-04-18 09:58:47116#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
117 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
118
asapersson1fe48a52016-01-07 09:02:42119#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
120 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
121
122#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
123 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
124
125#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
126 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
127
Karl Wiberg79eb1d92017-11-08 11:26:07128#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
129 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
130 webrtc::metrics::HistogramFactoryGetCounts( \
131 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 09:02:42132
henrik.lundinf29e05d2016-12-01 17:58:45133#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
134 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
135 webrtc::metrics::HistogramFactoryGetCountsLinear( \
136 name, min, max, bucket_count))
137
ilnik6d5b4d62017-08-30 10:32:14138// Slow metrics: pointer to metric is acquired at each call and is not cached.
139//
asapersson53805322015-12-21 09:46:20140#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
141 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38142
ilnik6d5b4d62017-08-30 10:32:14143#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
144 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
145
146#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
147 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
148
149#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
150 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
151
152#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
153 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
154
155#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
156 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
157
Karl Wiberg79eb1d92017-11-08 11:26:07158#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
159 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
160 webrtc::metrics::HistogramFactoryGetCounts( \
161 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56162
asapersson1fe48a52016-01-07 09:02:42163// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 10:32:14164#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
165 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
166
167// Histogram for booleans.
168#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
169 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
170
171// Histogram for enumerators (evenly spaced buckets).
Artem Titovf0671922021-07-27 10:40:17172// `boundary` should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 19:54:53173//
174// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
175// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 10:32:14176#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wang36e0f572019-01-17 02:37:21177 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
ilnik6d5b4d62017-08-30 10:32:14178 name, sample, \
Qingsi Wangd6eb71e2018-06-26 19:30:04179 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 10:32:14180
181// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 09:02:42182#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
183 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
184
Max Morin84cab202016-07-01 11:35:19185// Histogram for booleans.
186#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
187 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
188
asapersson1fe48a52016-01-07 09:02:42189// Histogram for enumerators (evenly spaced buckets).
Artem Titovf0671922021-07-27 10:40:17190// `boundary` should be above the max enumerator sample.
asapersson1fe48a52016-01-07 09:02:42191#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Qingsi Wang36e0f572019-01-17 02:37:21192 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
Karl Wiberg79eb1d92017-11-08 11:26:07193 name, sample, \
asapersson1fe48a52016-01-07 09:02:42194 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
195
asapersson1fe48a52016-01-07 09:02:42196// The name of the histogram should not vary.
Niels Möller7a669002022-06-27 07:47:02197#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
198 factory_get_invocation) \
199 do { \
200 static std::atomic<webrtc::metrics::Histogram*> atomic_histogram_pointer( \
201 nullptr); \
202 webrtc::metrics::Histogram* histogram_pointer = \
203 atomic_histogram_pointer.load(std::memory_order_acquire); \
204 if (!histogram_pointer) { \
205 histogram_pointer = factory_get_invocation; \
206 webrtc::metrics::Histogram* null_histogram = nullptr; \
207 atomic_histogram_pointer.compare_exchange_strong(null_histogram, \
208 histogram_pointer); \
209 } \
210 if (histogram_pointer) { \
211 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
212 } \
asapersson1fe48a52016-01-07 09:02:42213 } while (0)
214
asapersson1fe48a52016-01-07 09:02:42215// The histogram is constructed/found for each call.
sakal71b83932016-09-16 13:56:15216// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 09:02:42217#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 13:56:15218 do { \
219 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
220 if (histogram_pointer) { \
221 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
222 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56223 } while (0)
224
asapersson040b79f2016-02-02 15:13:01225// Helper macros.
226// Macros for calling a histogram with varying name (e.g. when using a metric
ilnik6d5b4d62017-08-30 10:32:14227// in different modes such as real-time vs screenshare). Fast, because pointer
Artem Titovf0671922021-07-27 10:40:17228// is cached. `index` should be different for different names. Allowed `index`
ilnik6d5b4d62017-08-30 10:32:14229// values are 0, 1, and 2.
asapersson040b79f2016-02-02 15:13:01230#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07231 RTC_HISTOGRAMS_COMMON(index, name, sample, \
232 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
asapersson040b79f2016-02-02 15:13:01233
234#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07235 RTC_HISTOGRAMS_COMMON(index, name, sample, \
236 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
asapersson040b79f2016-02-02 15:13:01237
asapersson5265fed2016-04-18 09:58:47238#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07239 RTC_HISTOGRAMS_COMMON(index, name, sample, \
240 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
asapersson5265fed2016-04-18 09:58:47241
asapersson040b79f2016-02-02 15:13:01242#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07243 RTC_HISTOGRAMS_COMMON(index, name, sample, \
244 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
asapersson040b79f2016-02-02 15:13:01245
246#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07247 RTC_HISTOGRAMS_COMMON(index, name, sample, \
248 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
asapersson040b79f2016-02-02 15:13:01249
250#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07251 RTC_HISTOGRAMS_COMMON(index, name, sample, \
252 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
asapersson040b79f2016-02-02 15:13:01253
254#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 11:26:07255 RTC_HISTOGRAMS_COMMON(index, name, sample, \
256 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
asapersson040b79f2016-02-02 15:13:01257
258#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 11:26:07259 RTC_HISTOGRAMS_COMMON(index, name, sample, \
260 RTC_HISTOGRAM_PERCENTAGE(name, sample))
asapersson040b79f2016-02-02 15:13:01261
262#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
ilnik6d5b4d62017-08-30 10:32:14263 do { \
264 switch (index) { \
265 case 0: \
266 macro_invocation; \
267 break; \
268 case 1: \
269 macro_invocation; \
270 break; \
271 case 2: \
272 macro_invocation; \
273 break; \
274 default: \
Artem Titovd3251962021-11-15 15:57:07275 RTC_DCHECK_NOTREACHED(); \
ilnik6d5b4d62017-08-30 10:32:14276 } \
asapersson040b79f2016-02-02 15:13:01277 } while (0)
278
Ying Wangef3998f2019-12-09 12:06:53279#else
280
281////////////////////////////////////////////////////////////////////////////////
282// This section defines no-op alternatives to the metrics macros when
283// RTC_METRICS_ENABLED is defined.
284
Jared Siskin7220ee92023-05-03 09:06:14285#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
286 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53287
Jared Siskin7220ee92023-05-03 09:06:14288#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
289 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53290
Jared Siskin7220ee92023-05-03 09:06:14291#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
292 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53293
Jared Siskin7220ee92023-05-03 09:06:14294#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
295 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53296
Jared Siskin7220ee92023-05-03 09:06:14297#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
298 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53299
Jared Siskin7220ee92023-05-03 09:06:14300#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
301 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53302
303#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
304 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
305
306#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
307 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
308
Jared Siskin7220ee92023-05-03 09:06:14309#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
310 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53311
Jared Siskin7220ee92023-05-03 09:06:14312#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
313 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53314
Jared Siskin7220ee92023-05-03 09:06:14315#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
316 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53317
Jared Siskin7220ee92023-05-03 09:06:14318#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
319 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53320
Jared Siskin7220ee92023-05-03 09:06:14321#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
322 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53323
Jared Siskin7220ee92023-05-03 09:06:14324#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
325 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53326
327#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
328 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
329
Jared Siskin7220ee92023-05-03 09:06:14330#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
331 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53332
Jared Siskin7220ee92023-05-03 09:06:14333#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
334 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53335
336#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
337 webrtc::metrics_impl::NoOp(name, sample, boundary)
338
Jared Siskin7220ee92023-05-03 09:06:14339#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
340 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53341
Jared Siskin7220ee92023-05-03 09:06:14342#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
343 webrtc::metrics_impl::NoOp(name, sample)
Ying Wangef3998f2019-12-09 12:06:53344
345#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
346 webrtc::metrics_impl::NoOp(name, sample, boundary)
347
348#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
349 factory_get_invocation) \
350 webrtc::metrics_impl::NoOp(constant_name, sample, factory_get_invocation)
351
352#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
353 webrtc::metrics_impl::NoOp(name, sample, factory_get_invocation)
354
Jared Siskin7220ee92023-05-03 09:06:14355#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
356 webrtc::metrics_impl::NoOp(index, name, sample)
Ying Wangef3998f2019-12-09 12:06:53357
Jared Siskin7220ee92023-05-03 09:06:14358#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
359 webrtc::metrics_impl::NoOp(index, name, sample)
Ying Wangef3998f2019-12-09 12:06:53360
Jared Siskin7220ee92023-05-03 09:06:14361#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
362 webrtc::metrics_impl::NoOp(index, name, sample)
Ying Wangef3998f2019-12-09 12:06:53363
364#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
365 webrtc::metrics_impl::NoOp(index, name, sample)
366
367#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
368 webrtc::metrics_impl::NoOp(index, name, sample)
369
370#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
371 webrtc::metrics_impl::NoOp(index, name, sample)
372
373#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
374 webrtc::metrics_impl::NoOp(index, name, sample, boundary)
375
Jared Siskin7220ee92023-05-03 09:06:14376#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
377 webrtc::metrics_impl::NoOp(index, name, sample)
Ying Wangef3998f2019-12-09 12:06:53378
379#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
380 webrtc::metrics_impl::NoOp(index, name, sample, macro_invocation)
381
382#endif // RTC_METRICS_ENABLED
383
asapersson@webrtc.org580d3672014-10-23 12:57:56384namespace webrtc {
385namespace metrics {
386
asapersson@webrtc.org83b52002014-11-28 10:17:13387// Time that should have elapsed for stats that are gathered once per call.
Peter Kasting662d7f12022-05-04 19:57:00388constexpr int kMinRunTimeInSeconds = 10;
asapersson@webrtc.org83b52002014-11-28 10:17:13389
asapersson@webrtc.org580d3672014-10-23 12:57:56390class Histogram;
391
392// Functions for getting pointer to histogram (constructs or finds the named
393// histogram).
394
395// Get histogram for counters.
Ali Tofigh969c13562022-05-13 08:26:58396Histogram* HistogramFactoryGetCounts(absl::string_view name,
Mirko Bonadei51868f52019-11-23 15:10:32397 int min,
398 int max,
399 int bucket_count);
asapersson@webrtc.org580d3672014-10-23 12:57:56400
henrik.lundinf29e05d2016-12-01 17:58:45401// Get histogram for counters with linear bucket spacing.
Ali Tofigh969c13562022-05-13 08:26:58402Histogram* HistogramFactoryGetCountsLinear(absl::string_view name,
Mirko Bonadei51868f52019-11-23 15:10:32403 int min,
404 int max,
405 int bucket_count);
henrik.lundinf29e05d2016-12-01 17:58:45406
asapersson@webrtc.org580d3672014-10-23 12:57:56407// Get histogram for enumerators.
Artem Titovf0671922021-07-27 10:40:17408// `boundary` should be above the max enumerator sample.
Ali Tofigh969c13562022-05-13 08:26:58409Histogram* HistogramFactoryGetEnumeration(absl::string_view name, int boundary);
asapersson@webrtc.org580d3672014-10-23 12:57:56410
Qingsi Wangd6eb71e2018-06-26 19:30:04411// Get sparse histogram for enumerators.
Artem Titovf0671922021-07-27 10:40:17412// `boundary` should be above the max enumerator sample.
Ali Tofigh969c13562022-05-13 08:26:58413Histogram* SparseHistogramFactoryGetEnumeration(absl::string_view name,
Mirko Bonadei51868f52019-11-23 15:10:32414 int boundary);
Qingsi Wangd6eb71e2018-06-26 19:30:04415
Artem Titovf0671922021-07-27 10:40:17416// Function for adding a `sample` to a histogram.
Mirko Bonadei51868f52019-11-23 15:10:32417void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56418
Mirko Bonadeic1c2a882018-09-06 11:34:51419struct SampleInfo {
Ali Tofigh969c13562022-05-13 08:26:58420 SampleInfo(absl::string_view name, int min, int max, size_t bucket_count);
Mirko Bonadeic1c2a882018-09-06 11:34:51421 ~SampleInfo();
422
423 const std::string name;
424 const int min;
425 const int max;
426 const size_t bucket_count;
427 std::map<int, int> samples; // <value, # of events>
428};
429
430// Enables collection of samples.
431// This method should be called before any other call into webrtc.
432void Enable();
433
434// Gets histograms and clears all samples.
435void GetAndReset(
Ali Tofigh969c13562022-05-13 08:26:58436 std::map<std::string, std::unique_ptr<SampleInfo>, rtc::AbslStringViewCmp>*
437 histograms);
Mirko Bonadeic1c2a882018-09-06 11:34:51438
439// Functions below are mainly for testing.
440
441// Clears all samples.
442void Reset();
443
Artem Titovf0671922021-07-27 10:40:17444// Returns the number of times the `sample` has been added to the histogram.
Ali Tofigh969c13562022-05-13 08:26:58445int NumEvents(absl::string_view name, int sample);
Mirko Bonadeic1c2a882018-09-06 11:34:51446
447// Returns the total number of added samples to the histogram.
Ali Tofigh969c13562022-05-13 08:26:58448int NumSamples(absl::string_view name);
Mirko Bonadeic1c2a882018-09-06 11:34:51449
450// Returns the minimum sample value (or -1 if the histogram has no samples).
Ali Tofigh969c13562022-05-13 08:26:58451int MinSample(absl::string_view name);
Mirko Bonadeic1c2a882018-09-06 11:34:51452
Steve Antonc1e6e862019-03-04 22:43:44453// Returns a map with keys the samples with at least one event and values the
454// number of events for that sample.
Ali Tofigh969c13562022-05-13 08:26:58455std::map<int, int> Samples(absl::string_view name);
Steve Antonc1e6e862019-03-04 22:43:44456
asapersson@webrtc.org580d3672014-10-23 12:57:56457} // namespace metrics
458} // namespace webrtc
459
Mirko Bonadei92ea95e2017-09-15 04:47:31460#endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_