blob: 0fee344aa407ac295861ece924f56486b5fff553 [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) \
asapersson58d992e2016-03-29 02:15:06 -070085 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, false, \
86 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
87
88// RTC_HISTOGRAM_COUNTS with logging.
89#define RTC_LOGGED_HISTOGRAM_COUNTS_100(name, sample) \
90 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
91
92#define RTC_LOGGED_HISTOGRAM_COUNTS_200(name, sample) \
93 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
94
asapersson5265fed2016-04-18 02:58:47 -070095#define RTC_LOGGED_HISTOGRAM_COUNTS_500(name, sample) \
96 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
97
asapersson58d992e2016-03-29 02:15:06 -070098#define RTC_LOGGED_HISTOGRAM_COUNTS_1000(name, sample) \
99 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
100
101#define RTC_LOGGED_HISTOGRAM_COUNTS_10000(name, sample) \
102 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
103
104#define RTC_LOGGED_HISTOGRAM_COUNTS_100000(name, sample) \
105 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
106
107#define RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
108 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, true, \
asapersson1fe48a52016-01-07 01:02:42 -0800109 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
110
111// Deprecated.
112// TODO(asapersson): Remove.
asapersson53805322015-12-21 01:46:20 -0800113#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
114 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000115
asapersson53805322015-12-21 01:46:20 -0800116#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
117 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800118 webrtc::metrics::HistogramFactoryGetCounts(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).
121#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
122 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
123
asapersson58d992e2016-03-29 02:15:06 -0700124// RTC_HISTOGRAM_PERCENTAGE with logging.
125#define RTC_LOGGED_HISTOGRAM_PERCENTAGE(name, sample) \
126 RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, 101)
127
Max Morin84cab202016-07-01 13:35:19 +0200128// Histogram for booleans.
129#define RTC_HISTOGRAM_BOOLEAN(name, sample) \
130 RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
131
132// RTC_HISTOGRAM_BOOLEAN with logging.
133#define RTC_LOGGED_HISTOGRAM_BOOLEAN(name, sample) \
134 RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, 2)
135
asapersson1fe48a52016-01-07 01:02:42 -0800136// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000137// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800138#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
asapersson58d992e2016-03-29 02:15:06 -0700139 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, false, \
140 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
141
142// RTC_HISTOGRAM_ENUMERATION with logging.
143#define RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, boundary) \
144 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, true, \
asapersson1fe48a52016-01-07 01:02:42 -0800145 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
146
asapersson1fe48a52016-01-07 01:02:42 -0800147// The name of the histogram should not vary.
148// TODO(asapersson): Consider changing string to const char*.
asapersson58d992e2016-03-29 02:15:06 -0700149#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, log, \
asapersson1fe48a52016-01-07 01:02:42 -0800150 factory_get_invocation) \
151 do { \
asapersson58d992e2016-03-29 02:15:06 -0700152 if (log) { \
153 LOG(LS_INFO) << constant_name << " " << sample; \
154 } \
asapersson1fe48a52016-01-07 01:02:42 -0800155 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
156 webrtc::metrics::Histogram* histogram_pointer = \
157 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
158 if (!histogram_pointer) { \
159 histogram_pointer = factory_get_invocation; \
160 webrtc::metrics::Histogram* prev_pointer = \
161 rtc::AtomicOps::CompareAndSwapPtr( \
162 &atomic_histogram_pointer, \
163 static_cast<webrtc::metrics::Histogram*>(nullptr), \
164 histogram_pointer); \
165 RTC_DCHECK(prev_pointer == nullptr || \
166 prev_pointer == histogram_pointer); \
167 } \
168 webrtc::metrics::HistogramAdd(histogram_pointer, constant_name, sample); \
169 } while (0)
170
171// Deprecated.
172// The histogram is constructed/found for each call.
173// May be used for histograms with infrequent updates.
174#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000175 do { \
176 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
asapersson1fe48a52016-01-07 01:02:42 -0800177 webrtc::metrics::HistogramAdd(histogram_pointer, name, sample); \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000178 } while (0)
179
asapersson040b79f2016-02-02 07:13:01 -0800180
181// Helper macros.
182// Macros for calling a histogram with varying name (e.g. when using a metric
183// in different modes such as real-time vs screenshare).
184#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
185 RTC_HISTOGRAMS_COMMON(index, name, sample, \
186 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
187
188#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
189 RTC_HISTOGRAMS_COMMON(index, name, sample, \
190 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
191
asapersson5265fed2016-04-18 02:58:47 -0700192#define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
193 RTC_HISTOGRAMS_COMMON(index, name, sample, \
194 RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
195
asapersson040b79f2016-02-02 07:13:01 -0800196#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
197 RTC_HISTOGRAMS_COMMON(index, name, sample, \
198 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
199
200#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
201 RTC_HISTOGRAMS_COMMON(index, name, sample, \
202 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
203
204#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
205 RTC_HISTOGRAMS_COMMON(index, name, sample, \
206 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
207
208#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
209 RTC_HISTOGRAMS_COMMON(index, name, sample, \
210 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
211
212#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
213 RTC_HISTOGRAMS_COMMON(index, name, sample, \
214 RTC_HISTOGRAM_PERCENTAGE(name, sample))
215
asapersson58d992e2016-03-29 02:15:06 -0700216// RTC_HISTOGRAMS_COUNTS with logging.
217#define RTC_LOGGED_HISTOGRAMS_COUNTS_100(index, name, sample) \
218 RTC_HISTOGRAMS_COMMON(index, name, sample, \
219 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
220
221#define RTC_LOGGED_HISTOGRAMS_COUNTS_200(index, name, sample) \
222 RTC_HISTOGRAMS_COMMON(index, name, sample, \
223 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
224
asapersson5265fed2016-04-18 02:58:47 -0700225#define RTC_LOGGED_HISTOGRAMS_COUNTS_500(index, name, sample) \
226 RTC_HISTOGRAMS_COMMON(index, name, sample, \
227 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
228
asapersson58d992e2016-03-29 02:15:06 -0700229#define RTC_LOGGED_HISTOGRAMS_COUNTS_1000(index, name, sample) \
230 RTC_HISTOGRAMS_COMMON(index, name, sample, \
231 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
232
233#define RTC_LOGGED_HISTOGRAMS_COUNTS_10000(index, name, sample) \
234 RTC_HISTOGRAMS_COMMON(index, name, sample, \
235 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
236
237#define RTC_LOGGED_HISTOGRAMS_COUNTS_100000(index, name, sample) \
238 RTC_HISTOGRAMS_COMMON(index, name, sample, \
239 RTC_LOGGED_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
240
241#define RTC_LOGGED_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
242 RTC_HISTOGRAMS_COMMON(index, name, sample, \
243 RTC_LOGGED_HISTOGRAM_ENUMERATION(name, sample, boundary))
244
245#define RTC_LOGGED_HISTOGRAMS_PERCENTAGE(index, name, sample) \
246 RTC_HISTOGRAMS_COMMON(index, name, sample, \
247 RTC_LOGGED_HISTOGRAM_PERCENTAGE(name, sample))
248
asapersson040b79f2016-02-02 07:13:01 -0800249#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
250 do { \
asapersson040b79f2016-02-02 07:13:01 -0800251 switch (index) { \
252 case 0: \
253 macro_invocation; \
254 break; \
255 case 1: \
256 macro_invocation; \
257 break; \
258 case 2: \
259 macro_invocation; \
260 break; \
261 default: \
262 RTC_NOTREACHED(); \
263 } \
264 } while (0)
265
266
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000267namespace webrtc {
268namespace metrics {
269
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000270// Time that should have elapsed for stats that are gathered once per call.
271enum { kMinRunTimeInSeconds = 10 };
272
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000273class Histogram;
274
275// Functions for getting pointer to histogram (constructs or finds the named
276// histogram).
277
278// Get histogram for counters.
279Histogram* HistogramFactoryGetCounts(
280 const std::string& name, int min, int max, int bucket_count);
281
282// Get histogram for enumerators.
283// |boundary| should be above the max enumerator sample.
284Histogram* HistogramFactoryGetEnumeration(
285 const std::string& name, int boundary);
286
287// Function for adding a |sample| to a histogram.
288// |name| can be used to verify that it matches the histogram name.
289void HistogramAdd(
290 Histogram* histogram_pointer, const std::string& name, int sample);
291
292} // namespace metrics
293} // namespace webrtc
294
kjellanderd56d68c2015-11-02 02:12:41 -0800295#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000296