blob: f98b4cda69ef7fa083574787970dcf0d9aa670da [file] [log] [blame]
asapersson@webrtc.org580d3672014-10-23 12:57:56 +00001// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the LICENSE file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8//
9
Mirko Bonadeic1c2a882018-09-06 13:34:51 +020010#include "system_wrappers/include/metrics.h"
asapersson01d70a32016-05-20 06:29:46 -070011
asapersson1731c9c2016-11-30 00:29:09 -080012#include <algorithm>
13
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/thread_annotations.h"
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000016
17// Default implementation of histogram methods for WebRTC clients that do not
18// want to provide their own implementation.
19
20namespace webrtc {
21namespace metrics {
asapersson01d70a32016-05-20 06:29:46 -070022class Histogram;
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000023
asapersson01d70a32016-05-20 06:29:46 -070024namespace {
25// Limit for the maximum number of sample values that can be stored.
26// TODO(asapersson): Consider using bucket count (and set up
27// linearly/exponentially spaced buckets) if samples are logged more frequently.
asapersson7d569972016-05-24 06:03:38 -070028const int kMaxSampleMapSize = 300;
asapersson@webrtc.org580d3672014-10-23 12:57:56 +000029
asapersson01d70a32016-05-20 06:29:46 -070030class RtcHistogram {
31 public:
32 RtcHistogram(const std::string& name, int min, int max, int bucket_count)
33 : min_(min), max_(max), info_(name, min, max, bucket_count) {
34 RTC_DCHECK_GT(bucket_count, 0);
35 }
36
37 void Add(int sample) {
asapersson1731c9c2016-11-30 00:29:09 -080038 sample = std::min(sample, max_);
39 sample = std::max(sample, min_ - 1); // Underflow bucket.
asapersson01d70a32016-05-20 06:29:46 -070040
41 rtc::CritScope cs(&crit_);
42 if (info_.samples.size() == kMaxSampleMapSize &&
43 info_.samples.find(sample) == info_.samples.end()) {
44 return;
45 }
46 ++info_.samples[sample];
47 }
48
49 // Returns a copy (or nullptr if there are no samples) and clears samples.
50 std::unique_ptr<SampleInfo> GetAndReset() {
51 rtc::CritScope cs(&crit_);
52 if (info_.samples.empty())
53 return nullptr;
54
55 SampleInfo* copy =
56 new SampleInfo(info_.name, info_.min, info_.max, info_.bucket_count);
asapersson1731c9c2016-11-30 00:29:09 -080057
58 std::swap(info_.samples, copy->samples);
59
asapersson01d70a32016-05-20 06:29:46 -070060 return std::unique_ptr<SampleInfo>(copy);
61 }
62
63 const std::string& name() const { return info_.name; }
64
65 // Functions only for testing.
66 void Reset() {
67 rtc::CritScope cs(&crit_);
68 info_.samples.clear();
69 }
70
71 int NumEvents(int sample) const {
72 rtc::CritScope cs(&crit_);
73 const auto it = info_.samples.find(sample);
74 return (it == info_.samples.end()) ? 0 : it->second;
75 }
76
77 int NumSamples() const {
78 int num_samples = 0;
79 rtc::CritScope cs(&crit_);
80 for (const auto& sample : info_.samples) {
81 num_samples += sample.second;
82 }
83 return num_samples;
84 }
85
86 int MinSample() const {
87 rtc::CritScope cs(&crit_);
88 return (info_.samples.empty()) ? -1 : info_.samples.begin()->first;
89 }
90
91 private:
92 rtc::CriticalSection crit_;
93 const int min_;
94 const int max_;
danilchapa37de392017-09-09 04:17:22 -070095 SampleInfo info_ RTC_GUARDED_BY(crit_);
asapersson01d70a32016-05-20 06:29:46 -070096
97 RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogram);
98};
99
100class RtcHistogramMap {
101 public:
102 RtcHistogramMap() {}
103 ~RtcHistogramMap() {}
104
105 Histogram* GetCountsHistogram(const std::string& name,
106 int min,
107 int max,
108 int bucket_count) {
109 rtc::CritScope cs(&crit_);
110 const auto& it = map_.find(name);
111 if (it != map_.end())
112 return reinterpret_cast<Histogram*>(it->second.get());
113
114 RtcHistogram* hist = new RtcHistogram(name, min, max, bucket_count);
115 map_[name].reset(hist);
116 return reinterpret_cast<Histogram*>(hist);
117 }
118
119 Histogram* GetEnumerationHistogram(const std::string& name, int boundary) {
120 rtc::CritScope cs(&crit_);
121 const auto& it = map_.find(name);
122 if (it != map_.end())
123 return reinterpret_cast<Histogram*>(it->second.get());
124
125 RtcHistogram* hist = new RtcHistogram(name, 1, boundary, boundary + 1);
126 map_[name].reset(hist);
127 return reinterpret_cast<Histogram*>(hist);
128 }
129
130 void GetAndReset(
131 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) {
132 rtc::CritScope cs(&crit_);
133 for (const auto& kv : map_) {
134 std::unique_ptr<SampleInfo> info = kv.second->GetAndReset();
135 if (info)
136 histograms->insert(std::make_pair(kv.first, std::move(info)));
137 }
138 }
139
140 // Functions only for testing.
141 void Reset() {
142 rtc::CritScope cs(&crit_);
143 for (const auto& kv : map_)
144 kv.second->Reset();
145 }
146
147 int NumEvents(const std::string& name, int sample) const {
148 rtc::CritScope cs(&crit_);
149 const auto& it = map_.find(name);
150 return (it == map_.end()) ? 0 : it->second->NumEvents(sample);
151 }
152
153 int NumSamples(const std::string& name) const {
154 rtc::CritScope cs(&crit_);
155 const auto& it = map_.find(name);
156 return (it == map_.end()) ? 0 : it->second->NumSamples();
157 }
158
159 int MinSample(const std::string& name) const {
160 rtc::CritScope cs(&crit_);
161 const auto& it = map_.find(name);
162 return (it == map_.end()) ? -1 : it->second->MinSample();
163 }
164
165 private:
166 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700167 std::map<std::string, std::unique_ptr<RtcHistogram>> map_
168 RTC_GUARDED_BY(crit_);
asapersson01d70a32016-05-20 06:29:46 -0700169
170 RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogramMap);
171};
172
173// RtcHistogramMap is allocated upon call to Enable().
174// The histogram getter functions, which return pointer values to the histograms
175// in the map, are cached in WebRTC. Therefore, this memory is not freed by the
176// application (the memory will be reclaimed by the OS).
177static RtcHistogramMap* volatile g_rtc_histogram_map = nullptr;
178
179void CreateMap() {
180 RtcHistogramMap* map = rtc::AtomicOps::AcquireLoadPtr(&g_rtc_histogram_map);
181 if (map == nullptr) {
182 RtcHistogramMap* new_map = new RtcHistogramMap();
183 RtcHistogramMap* old_map = rtc::AtomicOps::CompareAndSwapPtr(
184 &g_rtc_histogram_map, static_cast<RtcHistogramMap*>(nullptr), new_map);
185 if (old_map != nullptr)
186 delete new_map;
187 }
188}
189
190// Set the first time we start using histograms. Used to make sure Enable() is
191// not called thereafter.
192#if RTC_DCHECK_IS_ON
193static volatile int g_rtc_histogram_called = 0;
194#endif
195
196// Gets the map (or nullptr).
197RtcHistogramMap* GetMap() {
198#if RTC_DCHECK_IS_ON
199 rtc::AtomicOps::ReleaseStore(&g_rtc_histogram_called, 1);
200#endif
201 return g_rtc_histogram_map;
202}
203} // namespace
204
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200205#ifndef WEBRTC_EXCLUDE_METRICS_DEFAULT
asapersson01d70a32016-05-20 06:29:46 -0700206// Implementation of histogram methods in
207// webrtc/system_wrappers/interface/metrics.h.
208
209// Histogram with exponentially spaced buckets.
210// Creates (or finds) histogram.
211// The returned histogram pointer is cached (and used for adding samples in
212// subsequent calls).
213Histogram* HistogramFactoryGetCounts(const std::string& name,
214 int min,
215 int max,
216 int bucket_count) {
henrik.lundinf29e05d2016-12-01 09:58:45 -0800217 // TODO(asapersson): Alternative implementation will be needed if this
218 // histogram type should be truly exponential.
219 return HistogramFactoryGetCountsLinear(name, min, max, bucket_count);
220}
221
222// Histogram with linearly spaced buckets.
223// Creates (or finds) histogram.
224// The returned histogram pointer is cached (and used for adding samples in
225// subsequent calls).
226Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
227 int min,
228 int max,
229 int bucket_count) {
asapersson01d70a32016-05-20 06:29:46 -0700230 RtcHistogramMap* map = GetMap();
231 if (!map)
232 return nullptr;
233
234 return map->GetCountsHistogram(name, min, max, bucket_count);
235}
236
237// Histogram with linearly spaced buckets.
238// Creates (or finds) histogram.
239// The returned histogram pointer is cached (and used for adding samples in
240// subsequent calls).
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000241Histogram* HistogramFactoryGetEnumeration(const std::string& name,
asapersson01d70a32016-05-20 06:29:46 -0700242 int boundary) {
243 RtcHistogramMap* map = GetMap();
244 if (!map)
245 return nullptr;
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000246
asapersson01d70a32016-05-20 06:29:46 -0700247 return map->GetEnumerationHistogram(name, boundary);
248}
249
Qingsi Wangd6eb71e2018-06-26 12:30:04 -0700250// Our default implementation reuses the non-sparse histogram.
251Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
252 int boundary) {
253 return HistogramFactoryGetEnumeration(name, boundary);
254}
255
sakal2a5f3712016-09-09 00:11:48 -0700256// Fast path. Adds |sample| to cached |histogram_pointer|.
257void HistogramAdd(Histogram* histogram_pointer, int sample) {
sakal2a5f3712016-09-09 00:11:48 -0700258 RtcHistogram* ptr = reinterpret_cast<RtcHistogram*>(histogram_pointer);
259 ptr->Add(sample);
260}
261
Mirko Bonadeic1c2a882018-09-06 13:34:51 +0200262#endif // WEBRTC_EXCLUDE_METRICS_DEFAULT
263
asapersson01d70a32016-05-20 06:29:46 -0700264SampleInfo::SampleInfo(const std::string& name,
265 int min,
266 int max,
267 size_t bucket_count)
268 : name(name), min(min), max(max), bucket_count(bucket_count) {}
269
270SampleInfo::~SampleInfo() {}
271
Mirko Bonadei17f48782018-09-28 08:51:10 +0200272// Implementation of global functions in metrics.h.
asapersson01d70a32016-05-20 06:29:46 -0700273void Enable() {
274 RTC_DCHECK(g_rtc_histogram_map == nullptr);
275#if RTC_DCHECK_IS_ON
276 RTC_DCHECK_EQ(0, rtc::AtomicOps::AcquireLoad(&g_rtc_histogram_called));
277#endif
278 CreateMap();
279}
280
281void GetAndReset(
282 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) {
283 histograms->clear();
284 RtcHistogramMap* map = GetMap();
285 if (map)
286 map->GetAndReset(histograms);
287}
288
289void Reset() {
290 RtcHistogramMap* map = GetMap();
291 if (map)
292 map->Reset();
293}
294
295int NumEvents(const std::string& name, int sample) {
296 RtcHistogramMap* map = GetMap();
297 return map ? map->NumEvents(name, sample) : 0;
298}
299
300int NumSamples(const std::string& name) {
301 RtcHistogramMap* map = GetMap();
302 return map ? map->NumSamples(name) : 0;
303}
304
305int MinSample(const std::string& name) {
306 RtcHistogramMap* map = GetMap();
307 return map ? map->MinSample(name) : -1;
308}
asapersson@webrtc.org580d3672014-10-23 12:57:56 +0000309
310} // namespace metrics
311} // namespace webrtc