blob: ce193bdc39bc897bc10431ffff2ba27075267718 [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"
19
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.
andresp@webrtc.org86e1e482015-01-14 09:30:52 +000046// system_wrappers/system_wrappers.gyp: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);
60
61
62// Macros for adding samples to a named histogram.
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000063
asapersson1fe48a52016-01-07 01:02:42 -080064// Histogram for counters (exponentially spaced buckets).
65#define RTC_HISTOGRAM_COUNTS_100(name, sample) \
66 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
67
68#define RTC_HISTOGRAM_COUNTS_200(name, sample) \
69 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
70
71#define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
72 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
73
74#define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
75 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
76
77#define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
78 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
79
80#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
81 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
82 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
83
84// Deprecated.
85// TODO(asapersson): Remove.
asapersson53805322015-12-21 01:46:20 -080086#define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
87 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000088
asapersson53805322015-12-21 01:46:20 -080089#define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
90 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
asapersson86b01602015-10-20 23:55:26 -070091
asapersson53805322015-12-21 01:46:20 -080092#define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
93 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000094
asapersson53805322015-12-21 01:46:20 -080095#define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
96 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000097
asapersson53805322015-12-21 01:46:20 -080098#define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
99 RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000100
asapersson53805322015-12-21 01:46:20 -0800101#define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
102 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -0800103 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000104
asapersson1fe48a52016-01-07 01:02:42 -0800105// Histogram for percentage (evenly spaced buckets).
106#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
107 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
108
109// Deprecated.
110// TODO(asapersson): Remove.
asapersson53805322015-12-21 01:46:20 -0800111#define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
112 RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000113
asapersson1fe48a52016-01-07 01:02:42 -0800114// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000115// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -0800116#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
117 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
118 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
119
120// Deprecated.
121// TODO(asapersson): Remove.
asapersson53805322015-12-21 01:46:20 -0800122#define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
123 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
124 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000125
asapersson1fe48a52016-01-07 01:02:42 -0800126// The name of the histogram should not vary.
127// TODO(asapersson): Consider changing string to const char*.
128#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
129 factory_get_invocation) \
130 do { \
131 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
132 webrtc::metrics::Histogram* histogram_pointer = \
133 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
134 if (!histogram_pointer) { \
135 histogram_pointer = factory_get_invocation; \
136 webrtc::metrics::Histogram* prev_pointer = \
137 rtc::AtomicOps::CompareAndSwapPtr( \
138 &atomic_histogram_pointer, \
139 static_cast<webrtc::metrics::Histogram*>(nullptr), \
140 histogram_pointer); \
141 RTC_DCHECK(prev_pointer == nullptr || \
142 prev_pointer == histogram_pointer); \
143 } \
144 webrtc::metrics::HistogramAdd(histogram_pointer, constant_name, sample); \
145 } while (0)
146
147// Deprecated.
148// The histogram is constructed/found for each call.
149// May be used for histograms with infrequent updates.
150#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000151 do { \
152 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
asapersson1fe48a52016-01-07 01:02:42 -0800153 webrtc::metrics::HistogramAdd(histogram_pointer, name, sample); \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000154 } while (0)
155
asapersson040b79f2016-02-02 07:13:01 -0800156
157// Helper macros.
158// Macros for calling a histogram with varying name (e.g. when using a metric
159// in different modes such as real-time vs screenshare).
160#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
161 RTC_HISTOGRAMS_COMMON(index, name, sample, \
162 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
163
164#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
165 RTC_HISTOGRAMS_COMMON(index, name, sample, \
166 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
167
168#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 { \
190 RTC_DCHECK(index >= 0); \
191 RTC_DCHECK(index <= 2); \
192 switch (index) { \
193 case 0: \
194 macro_invocation; \
195 break; \
196 case 1: \
197 macro_invocation; \
198 break; \
199 case 2: \
200 macro_invocation; \
201 break; \
202 default: \
203 RTC_NOTREACHED(); \
204 } \
205 } while (0)
206
207
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000208namespace webrtc {
209namespace metrics {
210
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000211// Time that should have elapsed for stats that are gathered once per call.
212enum { kMinRunTimeInSeconds = 10 };
213
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000214class Histogram;
215
216// Functions for getting pointer to histogram (constructs or finds the named
217// histogram).
218
219// Get histogram for counters.
220Histogram* HistogramFactoryGetCounts(
221 const std::string& name, int min, int max, int bucket_count);
222
223// Get histogram for enumerators.
224// |boundary| should be above the max enumerator sample.
225Histogram* HistogramFactoryGetEnumeration(
226 const std::string& name, int boundary);
227
228// Function for adding a |sample| to a histogram.
229// |name| can be used to verify that it matches the histogram name.
230void HistogramAdd(
231 Histogram* histogram_pointer, const std::string& name, int sample);
232
233} // namespace metrics
234} // namespace webrtc
235
kjellanderd56d68c2015-11-02 02:12:41 -0800236#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000237