blob: 99b8194f1aa684efb3fbf3263b6b492c9a8ddfb7 [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
14#include <string>
15
Mirko Bonadei71207422017-09-15 13:58:09 +020016#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/atomicops.h"
18#include "rtc_base/checks.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000019
20// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
21// statistics.
22//
23// Histogram for counters.
24// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
25//
26// Histogram for enumerators.
27// The boundary should be above the max enumerator sample.
28// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
29//
30//
31// The macros use the methods HistogramFactoryGetCounts,
32// HistogramFactoryGetEnumeration and HistogramAdd.
33//
34// Therefore, WebRTC clients must either:
35//
36// - provide implementations of
37// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
38// const std::string& name, int sample, int min, int max,
39// int bucket_count);
40// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
41// const std::string& name, int sample, int boundary);
42// void webrtc::metrics::HistogramAdd(
43// Histogram* histogram_pointer, const std::string& name, int sample);
44//
45// - or link with the default implementations (i.e.
mbonadei1e5b0262017-02-22 00:55:32 -080046// system_wrappers:metrics_default).
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000047//
48//
49// Example usage:
50//
51// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
52//
53// enum Types {
54// kTypeX,
55// kTypeY,
56// kBoundary,
57// };
58//
59// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
henrik.lundinc9badd52016-12-06 03:58:58 -080060//
61// NOTE: It is recommended to do the Chromium review for modifications to
62// histograms.xml before new metrics are committed to WebRTC.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000063
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000064// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000065
asapersson1fe48a52016-01-07 01:02:42 -080066// Histogram for counters (exponentially spaced buckets).
67#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
68 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
69
70#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
71 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
72
asapersson5265fed2016-04-18 02:58:47 -070073#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
74 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
75
asapersson1fe48a52016-01-07 01:02:42 -080076#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
77 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
78
79#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
80 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
81
82#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
83 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
84
Karl Wiberg79eb1d92017-11-08 12:26:07 +010085#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
86 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
87 webrtc::metrics::HistogramFactoryGetCounts( \
88 name, min, max, bucket_count))
asapersson1fe48a52016-01-07 01:02:42 -080089
henrik.lundinf29e05d2016-12-01 09:58:45 -080090#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
91 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
92 webrtc::metrics::HistogramFactoryGetCountsLinear( \
93 name, min, max, bucket_count))
94
ilnik6d5b4d62017-08-30 03:32:14 -070095// Slow metrics: pointer to metric is acquired at each call and is not cached.
96//
asapersson53805322015-12-21 01:46:20 -080097#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
98 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000099
ilnik6d5b4d62017-08-30 03:32:14 -0700100#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
101 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
102
103#define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
104 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
105
106#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
107 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
108
109#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
110 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
111
112#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
113 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
114
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100115#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
116 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
117 webrtc::metrics::HistogramFactoryGetCounts( \
118 name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000119
asapersson1fe48a52016-01-07 01:02:42 -0800120// Histogram for percentage (evenly spaced buckets).
ilnik6d5b4d62017-08-30 03:32:14 -0700121#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
122 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
123
124// Histogram for booleans.
125#define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
126 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
127
128// Histogram for enumerators (evenly spaced buckets).
129// |boundary| should be above the max enumerator sample.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700130//
131// TODO(qingsi): Refactor the default implementation given by RtcHistogram,
132// which is already sparse, and remove the boundary argument from the macro.
ilnik6d5b4d62017-08-30 03:32:14 -0700133#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700134 RTC_HISTOGRAM_COMMON_BLOCK( \
ilnik6d5b4d62017-08-30 03:32:14 -0700135 name, sample, \
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700136 webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
ilnik6d5b4d62017-08-30 03:32:14 -0700137
138// Histogram for percentage (evenly spaced buckets).
asapersson1fe48a52016-01-07 01:02:42 -0800139#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
140 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
141
Max Morin84cab202016-07-01 13:35:19 +0200142// Histogram for booleans.
143#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
144 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
145
asapersson1fe48a52016-01-07 01:02:42 -0800146// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000147// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800148#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100149 RTC_HISTOGRAM_COMMON_BLOCK( \
150 name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800151 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
152
asapersson1fe48a52016-01-07 01:02:42 -0800153// The name of the histogram should not vary.
154// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700155#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
156 factory_get_invocation) \
157 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800158 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700159 webrtc::metrics::Histogram* histogram_pointer = \
160 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
161 if (!histogram_pointer) { \
162 histogram_pointer = factory_get_invocation; \
163 webrtc::metrics::Histogram* prev_pointer = \
164 rtc::AtomicOps::CompareAndSwapPtr( \
165 &atomic_histogram_pointer, \
166 static_cast<webrtc::metrics::Histogram*>(nullptr), \
167 histogram_pointer); \
168 RTC_DCHECK(prev_pointer == nullptr || \
169 prev_pointer == histogram_pointer); \
170 } \
171 if (histogram_pointer) { \
sakal71b83932016-09-16 06:56:15 -0700172 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
173 } \
asapersson1fe48a52016-01-07 01:02:42 -0800174 } while (0)
175
176// Deprecated.
177// The histogram is constructed/found for each call.
sakal71b83932016-09-16 06:56:15 -0700178// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 01:02:42 -0800179#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 06:56:15 -0700180 do { \
181 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
182 if (histogram_pointer) { \
183 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
184 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000185 } while (0)
186
asapersson040b79f2016-02-02 07:13:01 -0800187// Helper macros.
188// Macros for calling a histogram with varying name (e.g. when using a metric
ilnik6d5b4d62017-08-30 03:32:14 -0700189// in different modes such as real-time vs screenshare). Fast, because pointer
190// is cached. |index| should be different for different names. Allowed |index|
191// values are 0, 1, and 2.
asapersson040b79f2016-02-02 07:13:01 -0800192#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100193 RTC_HISTOGRAMS_COMMON(index, name, sample, \
194 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
asapersson040b79f2016-02-02 07:13:01 -0800195
196#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100197 RTC_HISTOGRAMS_COMMON(index, name, sample, \
198 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
asapersson040b79f2016-02-02 07:13:01 -0800199
asapersson5265fed2016-04-18 02:58:47 -0700200#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100201 RTC_HISTOGRAMS_COMMON(index, name, sample, \
202 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
asapersson5265fed2016-04-18 02:58:47 -0700203
asapersson040b79f2016-02-02 07:13:01 -0800204#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100205 RTC_HISTOGRAMS_COMMON(index, name, sample, \
206 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800207
208#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100209 RTC_HISTOGRAMS_COMMON(index, name, sample, \
210 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800211
212#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100213 RTC_HISTOGRAMS_COMMON(index, name, sample, \
214 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
asapersson040b79f2016-02-02 07:13:01 -0800215
216#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100217 RTC_HISTOGRAMS_COMMON(index, name, sample, \
218 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
asapersson040b79f2016-02-02 07:13:01 -0800219
220#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100221 RTC_HISTOGRAMS_COMMON(index, name, sample, \
222 RTC_HISTOGRAM_PERCENTAGE(name, sample))
asapersson040b79f2016-02-02 07:13:01 -0800223
224#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
ilnik6d5b4d62017-08-30 03:32:14 -0700225 do { \
226 switch (index) { \
227 case 0: \
228 macro_invocation; \
229 break; \
230 case 1: \
231 macro_invocation; \
232 break; \
233 case 2: \
234 macro_invocation; \
235 break; \
236 default: \
237 RTC_NOTREACHED(); \
238 } \
asapersson040b79f2016-02-02 07:13:01 -0800239 } while (0)
240
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000241namespace webrtc {
242namespace metrics {
243
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000244// Time that should have elapsed for stats that are gathered once per call.
245enum { kMinRunTimeInSeconds = 10 };
246
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000247class Histogram;
248
249// Functions for getting pointer to histogram (constructs or finds the named
250// histogram).
251
252// Get histogram for counters.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100253Histogram* HistogramFactoryGetCounts(const std::string& name,
254 int min,
255 int max,
256 int bucket_count);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000257
henrik.lundinf29e05d2016-12-01 09:58:45 -0800258// Get histogram for counters with linear bucket spacing.
259Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
260 int min,
261 int max,
262 int bucket_count);
263
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000264// Get histogram for enumerators.
265// |boundary| should be above the max enumerator sample.
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100266Histogram* HistogramFactoryGetEnumeration(const std::string& name,
267 int boundary);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000268
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700269// Get sparse histogram for enumerators.
270// |boundary| should be above the max enumerator sample.
271Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
272 int boundary);
273
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000274// Function for adding a |sample| to a histogram.
sakal71b83932016-09-16 06:56:15 -0700275void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000276
277} // namespace metrics
278} // namespace webrtc
279
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200280#endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_