blob: 25a51807dec0d6968950bb2e3ce13c8a5283ea33 [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
kjellanderd56d68c2015-11-02 02:12:41 -080011#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000013
14#include <string>
15
asapersson1fe48a52016-01-07 01:02:42 -080016#include "webrtc/base/atomicops.h"
17#include "webrtc/base/checks.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000018#include "webrtc/common_types.h"
asapersson9c246c42016-03-29 04:16:24 -070019#include "webrtc/system_wrappers/include/logging.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000020
21// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
22// statistics.
23//
24// Histogram for counters.
25// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
26//
27// Histogram for enumerators.
28// The boundary should be above the max enumerator sample.
29// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
30//
31//
32// The macros use the methods HistogramFactoryGetCounts,
33// HistogramFactoryGetEnumeration and HistogramAdd.
34//
35// Therefore, WebRTC clients must either:
36//
37// - provide implementations of
38// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
39// const std::string& name, int sample, int min, int max,
40// int bucket_count);
41// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
42// const std::string& name, int sample, int boundary);
43// void webrtc::metrics::HistogramAdd(
44// Histogram* histogram_pointer, const std::string& name, int sample);
45//
46// - or link with the default implementations (i.e.
andresp@webrtc.org86e1e482015-01-14 09:30:52 +000047// system_wrappers/system_wrappers.gyp:metrics_default).
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000048//
49//
50// Example usage:
51//
52// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
53//
54// enum Types {
55// kTypeX,
56// kTypeY,
57// kBoundary,
58// };
59//
60// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
61
62
63// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000064
asapersson1fe48a52016-01-07 01:02:42 -080065// Histogram for counters (exponentially spaced buckets).
66#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
67 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
68
69#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
70 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
71
asapersson5265fed2016-04-18 02:58:47 -070072#define RTC_HISTOGRAM_COUNTS_500(name, sample) \
73 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
74
asapersson1fe48a52016-01-07 01:02:42 -080075#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
76 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
77
78#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
79 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
80
81#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
82 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
83
84#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
asapersson1d02d3e2016-09-09 22:40:25 -070085 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -080086 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
87
henrik.lundinf29e05d2016-12-01 09:58:45 -080088#define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
89 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
90 webrtc::metrics::HistogramFactoryGetCountsLinear( \
91 name, min, max, bucket_count))
92
asapersson1fe48a52016-01-07 01:02:42 -080093// Deprecated.
94// TODO(asapersson): Remove.
asapersson53805322015-12-21 01:46:20 -080095#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
96 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000097
asapersson53805322015-12-21 01:46:20 -080098#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
99 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800100 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000101
asapersson1fe48a52016-01-07 01:02:42 -0800102// Histogram for percentage (evenly spaced buckets).
103#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
104 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
105
Max Morin84cab202016-07-01 13:35:19 +0200106// Histogram for booleans.
107#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
108 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
109
asapersson1fe48a52016-01-07 01:02:42 -0800110// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000111// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800112#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
asapersson1d02d3e2016-09-09 22:40:25 -0700113 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800114 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
115
asapersson1fe48a52016-01-07 01:02:42 -0800116// The name of the histogram should not vary.
117// TODO(asapersson): Consider changing string to const char*.
sakal71b83932016-09-16 06:56:15 -0700118#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
119 factory_get_invocation) \
120 do { \
asapersson1fe48a52016-01-07 01:02:42 -0800121 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
sakal71b83932016-09-16 06:56:15 -0700122 webrtc::metrics::Histogram* histogram_pointer = \
123 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
124 if (!histogram_pointer) { \
125 histogram_pointer = factory_get_invocation; \
126 webrtc::metrics::Histogram* prev_pointer = \
127 rtc::AtomicOps::CompareAndSwapPtr( \
128 &atomic_histogram_pointer, \
129 static_cast<webrtc::metrics::Histogram*>(nullptr), \
130 histogram_pointer); \
131 RTC_DCHECK(prev_pointer == nullptr || \
132 prev_pointer == histogram_pointer); \
133 } \
134 if (histogram_pointer) { \
135 RTC_DCHECK_EQ(constant_name, \
136 webrtc::metrics::GetHistogramName(histogram_pointer)) \
137 << "The name should not vary."; \
138 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
139 } \
asapersson1fe48a52016-01-07 01:02:42 -0800140 } while (0)
141
142// Deprecated.
143// The histogram is constructed/found for each call.
sakal71b83932016-09-16 06:56:15 -0700144// May be used for histograms with infrequent updates.`
asapersson1fe48a52016-01-07 01:02:42 -0800145#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
sakal71b83932016-09-16 06:56:15 -0700146 do { \
147 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
148 if (histogram_pointer) { \
149 webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
150 } \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000151 } while (0)
152
asapersson040b79f2016-02-02 07:13:01 -0800153// Helper macros.
154// Macros for calling a histogram with varying name (e.g. when using a metric
155// in different modes such as real-time vs screenshare).
156#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
157 RTC_HISTOGRAMS_COMMON(index, name, sample, \
158 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
159
160#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
161 RTC_HISTOGRAMS_COMMON(index, name, sample, \
162 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
163
asapersson5265fed2016-04-18 02:58:47 -0700164#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
165 RTC_HISTOGRAMS_COMMON(index, name, sample, \
166 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
167
asapersson040b79f2016-02-02 07:13:01 -0800168#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
169 RTC_HISTOGRAMS_COMMON(index, name, sample, \
170 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
171
172#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
173 RTC_HISTOGRAMS_COMMON(index, name, sample, \
174 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
175
176#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
177 RTC_HISTOGRAMS_COMMON(index, name, sample, \
178 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
179
180#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
181 RTC_HISTOGRAMS_COMMON(index, name, sample, \
182 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
183
184#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
185 RTC_HISTOGRAMS_COMMON(index, name, sample, \
186 RTC_HISTOGRAM_PERCENTAGE(name, sample))
187
188#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
189 do { \
asapersson040b79f2016-02-02 07:13:01 -0800190 switch (index) { \
191 case 0: \
192 macro_invocation; \
193 break; \
194 case 1: \
195 macro_invocation; \
196 break; \
197 case 2: \
198 macro_invocation; \
199 break; \
200 default: \
201 RTC_NOTREACHED(); \
202 } \
203 } while (0)
204
205
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000206namespace webrtc {
207namespace metrics {
208
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000209// Time that should have elapsed for stats that are gathered once per call.
210enum { kMinRunTimeInSeconds = 10 };
211
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000212class Histogram;
213
214// Functions for getting pointer to histogram (constructs or finds the named
215// histogram).
216
217// Get histogram for counters.
218Histogram* HistogramFactoryGetCounts(
219 const std::string& name, int min, int max, int bucket_count);
220
henrik.lundinf29e05d2016-12-01 09:58:45 -0800221// Get histogram for counters with linear bucket spacing.
222Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
223 int min,
224 int max,
225 int bucket_count);
226
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000227// Get histogram for enumerators.
228// |boundary| should be above the max enumerator sample.
229Histogram* HistogramFactoryGetEnumeration(
230 const std::string& name, int boundary);
231
sakal71b83932016-09-16 06:56:15 -0700232// Returns name of the histogram.
233const std::string& GetHistogramName(Histogram* histogram_pointer);
234
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000235// Function for adding a |sample| to a histogram.
sakal71b83932016-09-16 06:56:15 -0700236void HistogramAdd(Histogram* histogram_pointer, int sample);
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000237
238} // namespace metrics
239} // namespace webrtc
240
kjellanderd56d68c2015-11-02 02:12:41 -0800241#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000242