blob: c29a727df9967c29e76ede232784e314128a0145 [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
Ying Wangef3998f2019-12-09 13:06:53 +010023#if defined(RTC_DISABLE_METRICS)
24#define RTC_METRICS_ENABLED 0
25#else
26#define RTC_METRICS_ENABLED 1
27#endif
28
29namespace webrtc {
30namespace metrics_impl {
31template <typename... Ts>
32void NoOp(const Ts&...) {}
33}
34}
35
36#if RTC_METRICS_ENABLED
37#define EXPECT_METRIC_EQ(val1, val2) EXPECT_EQ(val1, val2)
38#define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) \
39 EXPECT_EQ_WAIT(val1, val2, timeout)
40#define EXPECT_METRIC_GT(val1, val2) EXPECT_GT(val1, val2)
41#define EXPECT_METRIC_LE(val1, val2) EXPECT_LE(val1, val2)
42#define EXPECT_METRIC_TRUE(conditon) EXPECT_TRUE(conditon)
43#define EXPECT_METRIC_FALSE(conditon) EXPECT_FALSE(conditon)
44#define EXPECT_METRIC_THAT(value, matcher) EXPECT_THAT(value, matcher)
45#else
46#define EXPECT_METRIC_EQ(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
47#define EXPECT_METRIC_EQ_WAIT(val1, val2, timeout) webrtc::metrics_impl::NoOp(val1, val2, timeout)
48#define EXPECT_METRIC_GT(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
49#define EXPECT_METRIC_LE(val1, val2) webrtc::metrics_impl::NoOp(val1, val2)
50#define EXPECT_METRIC_TRUE(condition) webrtc::metrics_impl::NoOp(condition || true)
51#define EXPECT_METRIC_FALSE(condition) webrtc::metrics_impl::NoOp(condition && false)
52#define EXPECT_METRIC_THAT(value, matcher) webrtc::metrics_impl::NoOp(value, testing::_)
53#endif
54
55#if RTC_METRICS_ENABLED
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000056// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
57// statistics.
58//
59// Histogram for counters.
60// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
61//
62// Histogram for enumerators.
63// The boundary should be above the max enumerator sample.
64// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
65//
66//
67// The macros use the methods HistogramFactoryGetCounts,
68// HistogramFactoryGetEnumeration and HistogramAdd.
69//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020070// By default WebRTC provides implementations of the aforementioned methods
71// that can be found in system_wrappers/source/metrics.cc. If clients want to
72// provide a custom version, they will have to:
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000073//
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020074// 1. Compile WebRTC defining the preprocessor macro
75// WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
76// by setting the GN arg rtc_exclude_metrics_default to true).
77// 2. Provide implementations of:
78// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
79// const std::string& name, int sample, int min, int max,
80// int bucket_count);
81// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
82// const std::string& name, int sample, int boundary);
83// void webrtc::metrics::HistogramAdd(
84// Histogram* histogram_pointer, const std::string& name, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000085//
86// Example usage:
87//
88// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
89//
90// enum Types {
91// kTypeX,
92// kTypeY,
93// kBoundary,
94// };
95//
96// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 03:58:58 -080097//
98// NOTE: It is recommended to do the Chromium review for modifications to
99// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000100
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000101// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000102
asapersson1fe48a52016-01-07 01:02:42 -0800103// Histogram for counters (exponentially spaced buckets).
104#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
105 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
106
107#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
108 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
109
asapersson5265fed2016-04-18 02:58:47 -0700110#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
111 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
112
asapersson1fe48a52016-01-07 01:02:42 -0800113#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
114 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
115
116#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
117 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
118
119#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
120 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
121
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100122#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
123 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
124 webrtc::metrics::HistogramFactoryGetCounts( \
125 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 01:02:42 -0800126
henrik.lundinf29e05d2016-12-01 09:58:45 -0800127#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
128 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
129 webrtc::metrics::HistogramFactoryGetCountsLinear( \
130 name, min, max, bucket_count))
131
ilnik6d5b4d62017-08-30 03:32:14 -0700132// Slow metrics: pointer to metric is acquired at each call and is not cached.
133//
asapersson53805322015-12-21 01:46:20 -0800134#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
135 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000136
ilnik6d5b4d62017-08-30 03:32:14 -0700137#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
138 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
139
140#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
141 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
142
143#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
144 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
145
146#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
147 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
148
149#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
150 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
151
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100152#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
153 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
154 webrtc::metrics::HistogramFactoryGetCounts( \
155 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000156
asapersson1fe48a52016-01-07 01:02:42 -0800157// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 03:32:14 -0700158#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
159 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
160
161// Histogram for booleans.
162#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
163 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
164
165// Histogram for enumerators (evenly spaced buckets).
Artem Titovf0671922021-07-27 12:40:17 +0200166// `boundary` should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700167//
168// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
169// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 03:32:14 -0700170#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800171 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
ilnik6d5b4d62017-08-30 03:32:14 -0700172 name, sample, \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700173 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 03:32:14 -0700174
175// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 01:02:42 -0800176#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
177 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
178
Max Morin84cab202016-07-01 13:35:19 +0200179// Histogram for booleans.
180#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
181 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
182
asapersson1fe48a52016-01-07 01:02:42 -0800183// Histogram for enumerators (evenly spaced buckets).
Artem Titovf0671922021-07-27 12:40:17 +0200184// `boundary` should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800185#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Qingsi Wang36e0f572019-01-16 18:37:21 -0800186 RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100187 name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800188 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
189
asapersson1fe48a52016-01-07 01:02:42 -0800190// The name of the histogram should not vary.
191// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700192#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
193 factory_get_invocation) \
194 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800195 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700196 webrtc::metrics::Histogram* histogram_pointer = \
197 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
198 if (!histogram_pointer) { \
199 histogram_pointer = factory_get_invocation; \
200 webrtc::metrics::Histogram* prev_pointer = \
201 rtc::AtomicOps::CompareAndSwapPtr( \
202 &atomic_histogram_pointer, \
203 static_cast<webrtc::metrics::Histogram*>(nullptr), \
204 histogram_pointer); \
205 RTC_DCHECK(prev_pointer == nullptr || \
206 prev_pointer == histogram_pointer); \
207 } \
208 if (histogram_pointer) { \
sakal71b83932016-09-16 06:56:15 -0700209 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
210 } \
asapersson1fe48a52016-01-07 01:02:42 -0800211 } while (0)
212
asapersson1fe48a52016-01-07 01:02:42 -0800213// The histogram is constructed/found for each call.
sakal71b83932016-09-16 06:56:15 -0700214// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 01:02:42 -0800215#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 06:56:15 -0700216 do { \
217 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
218 if (histogram_pointer) { \
219 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
220 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000221 } while (0)
222
asapersson040b79f2016-02-02 07:13:01 -0800223// Helper macros.
224// Macros for calling a histogram with varying name (e.g. when using a metric
ilnik6d5b4d62017-08-30 03:32:14 -0700225// in different modes such as real-time vs screenshare). Fast, because pointer
Artem Titovf0671922021-07-27 12:40:17 +0200226// is cached. `index` should be different for different names. Allowed `index`
ilnik6d5b4d62017-08-30 03:32:14 -0700227// values are 0, 1, and 2.
asapersson040b79f2016-02-02 07:13:01 -0800228#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100229 RTC_HISTOGRAMS_COMMON(index, name, sample, \
230 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
asapersson040b79f2016-02-02 07:13:01 -0800231
232#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100233 RTC_HISTOGRAMS_COMMON(index, name, sample, \
234 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
asapersson040b79f2016-02-02 07:13:01 -0800235
asapersson5265fed2016-04-18 02:58:47 -0700236#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100237 RTC_HISTOGRAMS_COMMON(index, name, sample, \
238 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
asapersson5265fed2016-04-18 02:58:47 -0700239
asapersson040b79f2016-02-02 07:13:01 -0800240#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100241 RTC_HISTOGRAMS_COMMON(index, name, sample, \
242 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800243
244#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100245 RTC_HISTOGRAMS_COMMON(index, name, sample, \
246 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800247
248#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100249 RTC_HISTOGRAMS_COMMON(index, name, sample, \
250 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800251
252#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100253 RTC_HISTOGRAMS_COMMON(index, name, sample, \
254 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
asapersson040b79f2016-02-02 07:13:01 -0800255
256#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100257 RTC_HISTOGRAMS_COMMON(index, name, sample, \
258 RTC_HISTOGRAM_PERCENTAGE(name, sample))
asapersson040b79f2016-02-02 07:13:01 -0800259
260#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
ilnik6d5b4d62017-08-30 03:32:14 -0700261 do { \
262 switch (index) { \
263 case 0: \
264 macro_invocation; \
265 break; \
266 case 1: \
267 macro_invocation; \
268 break; \
269 case 2: \
270 macro_invocation; \
271 break; \
272 default: \
Artem Titovd3251962021-11-15 16:57:07 +0100273 RTC_DCHECK_NOTREACHED(); \
ilnik6d5b4d62017-08-30 03:32:14 -0700274 } \
asapersson040b79f2016-02-02 07:13:01 -0800275 } while (0)
276
Ying Wangef3998f2019-12-09 13:06:53 +0100277#else
278
279////////////////////////////////////////////////////////////////////////////////
280// This section defines no-op alternatives to the metrics macros when
281// RTC_METRICS_ENABLED is defined.
282
283#define RTC_HISTOGRAM_COUNTS_100(name, sample) webrtc::metrics_impl::NoOp(name, sample)
284
285#define RTC_HISTOGRAM_COUNTS_200(name, sample) webrtc::metrics_impl::NoOp(name, sample)
286
287#define RTC_HISTOGRAM_COUNTS_500(name, sample) webrtc::metrics_impl::NoOp(name, sample)
288
289#define RTC_HISTOGRAM_COUNTS_1000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
290
291#define RTC_HISTOGRAM_COUNTS_10000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
292
293#define RTC_HISTOGRAM_COUNTS_100000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
294
295#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
296 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
297
298#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
299 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
300
301#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) webrtc::metrics_impl::NoOp(name, sample)
302
303#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) webrtc::metrics_impl::NoOp(name, sample)
304
305#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) webrtc::metrics_impl::NoOp(name, sample)
306
307#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
308
309#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
310
311#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) webrtc::metrics_impl::NoOp(name, sample)
312
313#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
314 webrtc::metrics_impl::NoOp(name, sample, min, max, bucket_count)
315
316#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) webrtc::metrics_impl::NoOp(name, sample)
317
318#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) webrtc::metrics_impl::NoOp(name, sample)
319
320#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
321 webrtc::metrics_impl::NoOp(name, sample, boundary)
322
323#define RTC_HISTOGRAM_PERCENTAGE(name, sample) webrtc::metrics_impl::NoOp(name, sample)
324
325#define RTC_HISTOGRAM_BOOLEAN(name, sample) webrtc::metrics_impl::NoOp(name, sample)
326
327#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
328 webrtc::metrics_impl::NoOp(name, sample, boundary)
329
330#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
331 factory_get_invocation) \
332 webrtc::metrics_impl::NoOp(constant_name, sample, factory_get_invocation)
333
334#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
335 webrtc::metrics_impl::NoOp(name, sample, factory_get_invocation)
336
337#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) webrtc::metrics_impl::NoOp(index, name, sample)
338
339#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) webrtc::metrics_impl::NoOp(index, name, sample)
340
341#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) webrtc::metrics_impl::NoOp(index, name, sample)
342
343#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
344 webrtc::metrics_impl::NoOp(index, name, sample)
345
346#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
347 webrtc::metrics_impl::NoOp(index, name, sample)
348
349#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
350 webrtc::metrics_impl::NoOp(index, name, sample)
351
352#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
353 webrtc::metrics_impl::NoOp(index, name, sample, boundary)
354
355#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) webrtc::metrics_impl::NoOp(index, name, sample)
356
357#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
358 webrtc::metrics_impl::NoOp(index, name, sample, macro_invocation)
359
360#endif // RTC_METRICS_ENABLED
361
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000362namespace webrtc {
363namespace metrics {
364
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000365// Time that should have elapsed for stats that are gathered once per call.
Peter Kasting662d7f12022-05-04 12:57:00 -0700366constexpr int kMinRunTimeInSeconds = 10;
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000367
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000368class Histogram;
369
370// Functions for getting pointer to histogram (constructs or finds the named
371// histogram).
372
373// Get histogram for counters.
Mirko Bonadei51868f52019-11-23 15:10:32 +0000374Histogram* HistogramFactoryGetCounts(const std::string& name,
375 int min,
376 int max,
377 int bucket_count);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000378
henrik.lundinf29e05d2016-12-01 09:58:45 -0800379// Get histogram for counters with linear bucket spacing.
Mirko Bonadei51868f52019-11-23 15:10:32 +0000380Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
381 int min,
382 int max,
383 int bucket_count);
henrik.lundinf29e05d2016-12-01 09:58:45 -0800384
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000385// Get histogram for enumerators.
Artem Titovf0671922021-07-27 12:40:17 +0200386// `boundary` should be above the max enumerator sample.
Mirko Bonadei51868f52019-11-23 15:10:32 +0000387Histogram* HistogramFactoryGetEnumeration(const std::string& name,
388 int boundary);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000389
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700390// Get sparse histogram for enumerators.
Artem Titovf0671922021-07-27 12:40:17 +0200391// `boundary` should be above the max enumerator sample.
Mirko Bonadei51868f52019-11-23 15:10:32 +0000392Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
393 int boundary);
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700394
Artem Titovf0671922021-07-27 12:40:17 +0200395// Function for adding a `sample` to a histogram.
Mirko Bonadei51868f52019-11-23 15:10:32 +0000396void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000397
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200398struct SampleInfo {
399 SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
400 ~SampleInfo();
401
402 const std::string name;
403 const int min;
404 const int max;
405 const size_t bucket_count;
406 std::map<int, int> samples; // <value, # of events>
407};
408
409// Enables collection of samples.
410// This method should be called before any other call into webrtc.
411void Enable();
412
413// Gets histograms and clears all samples.
414void GetAndReset(
415 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
416
417// Functions below are mainly for testing.
418
419// Clears all samples.
420void Reset();
421
Artem Titovf0671922021-07-27 12:40:17 +0200422// Returns the number of times the `sample` has been added to the histogram.
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200423int NumEvents(const std::string& name, int sample);
424
425// Returns the total number of added samples to the histogram.
426int NumSamples(const std::string& name);
427
428// Returns the minimum sample value (or -1 if the histogram has no samples).
429int MinSample(const std::string& name);
430
Steve Antonc1e6e862019-03-04 14:43:44 -0800431// Returns a map with keys the samples with at least one event and values the
432// number of events for that sample.
433std::map<int, int> Samples(const std::string& name);
434
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000435} // namespace metrics
436} // namespace webrtc
437
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200438#endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_