blob: b0a77c8711a7c33b247f91f7c3750697b108af75 [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(name, sample, min, max, bucket_count) \
90 RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
asapersson1fe48a52016-01-07 01:02:42 -080091 webrtc::metrics::HistogramFactoryGetCounts(name, min, max, bucket_count))
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000092
asapersson1fe48a52016-01-07 01:02:42 -080093// Histogram for percentage (evenly spaced buckets).
94#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
95 RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
96
asapersson1fe48a52016-01-07 01:02:42 -080097// Histogram for enumerators (evenly spaced buckets).
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000098// |boundary| should be above the max enumerator sample.
asapersson1fe48a52016-01-07 01:02:42 -080099#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
100 RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
101 webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
102
asapersson1fe48a52016-01-07 01:02:42 -0800103// The name of the histogram should not vary.
104// TODO(asapersson): Consider changing string to const char*.
105#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
106 factory_get_invocation) \
107 do { \
108 static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
109 webrtc::metrics::Histogram* histogram_pointer = \
110 rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
111 if (!histogram_pointer) { \
112 histogram_pointer = factory_get_invocation; \
113 webrtc::metrics::Histogram* prev_pointer = \
114 rtc::AtomicOps::CompareAndSwapPtr( \
115 &atomic_histogram_pointer, \
116 static_cast<webrtc::metrics::Histogram*>(nullptr), \
117 histogram_pointer); \
118 RTC_DCHECK(prev_pointer == nullptr || \
119 prev_pointer == histogram_pointer); \
120 } \
121 webrtc::metrics::HistogramAdd(histogram_pointer, constant_name, sample); \
122 } while (0)
123
124// Deprecated.
125// The histogram is constructed/found for each call.
126// May be used for histograms with infrequent updates.
127#define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000128 do { \
129 webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
asapersson1fe48a52016-01-07 01:02:42 -0800130 webrtc::metrics::HistogramAdd(histogram_pointer, name, sample); \
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000131 } while (0)
132
asapersson040b79f2016-02-02 07:13:01 -0800133
134// Helper macros.
135// Macros for calling a histogram with varying name (e.g. when using a metric
136// in different modes such as real-time vs screenshare).
137#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
138 RTC_HISTOGRAMS_COMMON(index, name, sample, \
139 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
140
141#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
142 RTC_HISTOGRAMS_COMMON(index, name, sample, \
143 RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
144
145#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
146 RTC_HISTOGRAMS_COMMON(index, name, sample, \
147 RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
148
149#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
150 RTC_HISTOGRAMS_COMMON(index, name, sample, \
151 RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
152
153#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
154 RTC_HISTOGRAMS_COMMON(index, name, sample, \
155 RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
156
157#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
158 RTC_HISTOGRAMS_COMMON(index, name, sample, \
159 RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
160
161#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
162 RTC_HISTOGRAMS_COMMON(index, name, sample, \
163 RTC_HISTOGRAM_PERCENTAGE(name, sample))
164
165#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
166 do { \
asapersson040b79f2016-02-02 07:13:01 -0800167 switch (index) { \
168 case 0: \
169 macro_invocation; \
170 break; \
171 case 1: \
172 macro_invocation; \
173 break; \
174 case 2: \
175 macro_invocation; \
176 break; \
177 default: \
178 RTC_NOTREACHED(); \
179 } \
180 } while (0)
181
182
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000183namespace webrtc {
184namespace metrics {
185
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000186// Time that should have elapsed for stats that are gathered once per call.
187enum { kMinRunTimeInSeconds = 10 };
188
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000189class Histogram;
190
191// Functions for getting pointer to histogram (constructs or finds the named
192// histogram).
193
194// Get histogram for counters.
195Histogram* HistogramFactoryGetCounts(
196 const std::string& name, int min, int max, int bucket_count);
197
198// Get histogram for enumerators.
199// |boundary| should be above the max enumerator sample.
200Histogram* HistogramFactoryGetEnumeration(
201 const std::string& name, int boundary);
202
203// Function for adding a |sample| to a histogram.
204// |name| can be used to verify that it matches the histogram name.
205void HistogramAdd(
206 Histogram* histogram_pointer, const std::string& name, int sample);
207
208} // namespace metrics
209} // namespace webrtc
210
kjellanderd56d68c2015-11-02 02:12:41 -0800211#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000212