OLD | NEW |
---|---|
1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // | 8 // |
9 | 9 |
10 #include "webrtc/system_wrappers/include/metrics_default.h" | |
11 | |
12 #include "webrtc/base/criticalsection.h" | |
13 #include "webrtc/base/thread_annotations.h" | |
10 #include "webrtc/system_wrappers/include/metrics.h" | 14 #include "webrtc/system_wrappers/include/metrics.h" |
11 | 15 |
12 // Default implementation of histogram methods for WebRTC clients that do not | 16 // Default implementation of histogram methods for WebRTC clients that do not |
13 // want to provide their own implementation. | 17 // want to provide their own implementation. |
14 | 18 |
15 namespace webrtc { | 19 namespace webrtc { |
16 namespace metrics { | 20 namespace metrics { |
21 class Histogram; | |
17 | 22 |
18 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, | 23 namespace { |
19 int bucket_count) { return NULL; } | 24 // Limit for the maximum number of sample values that can be stored. |
25 // TODO(asapersson): Consider using bucket count (and set up | |
26 // linearly/exponentially spaced buckets) if samples are logged more frequently. | |
pbos-webrtc
2016/05/09 21:21:13
Note that this would be done for performance reaso
åsapersson
2016/05/13 09:15:53
Acknowledged.
| |
27 const int kMaxSampleMapSize = 2500; | |
20 | 28 |
29 class RtcHistogram { | |
30 public: | |
31 RtcHistogram(const std::string& name, int min, int max, int bucket_count) | |
32 : info_(name, min, max, bucket_count) { | |
33 RTC_CHECK_GT(bucket_count, 0); | |
34 } | |
35 ~RtcHistogram() {} | |
36 | |
37 int NumSamples() const { | |
38 int num_samples = 0; | |
39 rtc::CritScope cs(&crit_); | |
40 for (const auto& sample : info_.samples) { | |
41 num_samples += sample.second; | |
42 } | |
43 return num_samples; | |
44 } | |
45 | |
46 int MinSample() const { | |
47 rtc::CritScope cs(&crit_); | |
48 if (info_.samples.empty()) { | |
49 return -1; | |
50 } | |
51 return info_.samples.begin()->first; | |
52 } | |
53 | |
54 int NumEvents(int sample) const { | |
55 rtc::CritScope cs(&crit_); | |
56 const auto it = info_.samples.find(sample); | |
57 if (it == info_.samples.end()) { | |
58 return 0; | |
59 } | |
60 return it->second; | |
61 } | |
62 | |
63 void Add(int sample) { | |
64 if (sample < info_.min) | |
65 sample = info_.min - 1; // Underflow bucket. | |
66 if (sample > info_.max) | |
67 sample = info_.max; | |
68 | |
69 rtc::CritScope cs(&crit_); | |
70 if (info_.samples.size() == kMaxSampleMapSize && | |
71 info_.samples.find(sample) == info_.samples.end()) { | |
72 return; | |
73 } | |
74 ++info_.samples[sample]; | |
75 } | |
76 | |
77 // Clears samples. | |
78 void Reset() { | |
79 rtc::CritScope cs(&crit_); | |
80 info_.samples.clear(); | |
81 } | |
82 | |
83 // Returns a copy (or nullptr if there are no samples) and clears samples. | |
84 SampleInfo* GetAndReset() { | |
pbos-webrtc
2016/05/09 21:21:13
Should this return a std::unique_ptr?
åsapersson
2016/05/13 09:15:53
Done.
| |
85 rtc::CritScope cs(&crit_); | |
86 if (info_.samples.empty()) | |
87 return nullptr; | |
88 | |
89 SampleInfo* copy = | |
90 new SampleInfo(info_.name, info_.min, info_.max, info_.bucket_count); | |
91 copy->samples = info_.samples; | |
92 info_.samples.clear(); | |
93 return copy; | |
94 } | |
95 | |
96 const std::string& name() const { return info_.name; } | |
97 | |
98 private: | |
99 rtc::CriticalSection crit_; | |
100 SampleInfo info_; | |
101 | |
102 RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogram); | |
103 }; | |
104 | |
105 // Map holding samples of a histogram (mapped by the histogram name). | |
106 bool rtc_histograms_enabled_ = false; | |
107 rtc::CriticalSection rtc_histograms_crit_; | |
juberti2
2016/05/10 05:29:33
non-pod data at global scope is not allowed..
åsapersson
2016/05/13 09:15:53
Done.
| |
108 std::map<std::string, std::unique_ptr<RtcHistogram>> rtc_histograms_ | |
109 GUARDED_BY(rtc_histograms_crit_); | |
110 } // namespace | |
111 | |
112 // Implementation of histogram methods in | |
113 // webrtc/system_wrappers/interface/metrics.h. | |
114 | |
115 // Histogram with exponentially spaced buckets. | |
116 Histogram* HistogramFactoryGetCounts(const std::string& name, | |
117 int min, | |
118 int max, | |
119 int bucket_count) { | |
120 rtc::CritScope cs(&rtc_histograms_crit_); | |
121 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { | |
122 rtc_histograms_[name].reset(new RtcHistogram(name, min, max, bucket_count)); | |
123 } | |
124 const auto& it = rtc_histograms_.find(name); | |
125 return reinterpret_cast<Histogram*>(it->second.get()); | |
126 } | |
127 | |
128 // Histogram with linearly spaced buckets. | |
21 Histogram* HistogramFactoryGetEnumeration(const std::string& name, | 129 Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
22 int boundary) { return NULL; } | 130 int boundary) { |
131 rtc::CritScope cs(&rtc_histograms_crit_); | |
132 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { | |
133 rtc_histograms_[name].reset( | |
134 new RtcHistogram(name, 1, boundary, boundary + 1)); | |
135 } | |
136 const auto& it = rtc_histograms_.find(name); | |
137 return reinterpret_cast<Histogram*>(it->second.get()); | |
138 } | |
23 | 139 |
24 void HistogramAdd( | 140 void HistogramAdd(Histogram* histogram_pointer, |
25 Histogram* histogram_pointer, const std::string& name, int sample) {} | 141 const std::string& name, |
142 int sample) { | |
143 if (!rtc_histograms_enabled_) | |
144 return; | |
145 | |
146 RtcHistogram* ptr = reinterpret_cast<RtcHistogram*>(histogram_pointer); | |
147 RTC_DCHECK_EQ(name, ptr->name()) << "The name should not vary."; | |
148 ptr->Add(sample); | |
149 } | |
150 | |
151 SampleInfo::SampleInfo(const std::string& name, | |
152 int min, | |
153 int max, | |
154 size_t bucket_count) | |
155 : name(name), min(min), max(max), bucket_count(bucket_count) {} | |
156 | |
157 SampleInfo::~SampleInfo() {} | |
158 | |
159 // Implementation of global functions in metrics_default.h. | |
160 void Enable() { | |
161 rtc_histograms_enabled_ = true; | |
162 } | |
163 | |
164 void GetAndReset( | |
165 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) { | |
166 histograms->clear(); | |
167 rtc::CritScope cs(&rtc_histograms_crit_); | |
168 for (const auto& kv : rtc_histograms_) { | |
169 SampleInfo* info = kv.second->GetAndReset(); | |
170 if (info) | |
171 (*histograms)[kv.first].reset(info); | |
172 } | |
173 } | |
174 | |
175 void Reset() { | |
176 rtc::CritScope cs(&rtc_histograms_crit_); | |
177 for (const auto& kv : rtc_histograms_) | |
178 kv.second->Reset(); | |
179 } | |
180 | |
181 int NumEvents(const std::string& name, int sample) { | |
182 rtc::CritScope cs(&rtc_histograms_crit_); | |
183 const auto& it = rtc_histograms_.find(name); | |
184 if (it == rtc_histograms_.end()) | |
185 return 0; | |
186 | |
187 return it->second->NumEvents(sample); | |
188 } | |
189 | |
190 int NumSamples(const std::string& name) { | |
191 rtc::CritScope cs(&rtc_histograms_crit_); | |
192 const auto& it = rtc_histograms_.find(name); | |
193 if (it == rtc_histograms_.end()) | |
194 return 0; | |
195 | |
196 return it->second->NumSamples(); | |
197 } | |
198 | |
199 int MinSample(const std::string& name) { | |
200 rtc::CritScope cs(&rtc_histograms_crit_); | |
201 const auto& it = rtc_histograms_.find(name); | |
202 if (it == rtc_histograms_.end()) | |
203 return -1; | |
204 | |
205 return it->second->MinSample(); | |
206 } | |
26 | 207 |
27 } // namespace metrics | 208 } // namespace metrics |
28 } // namespace webrtc | 209 } // namespace webrtc |
29 | |
OLD | NEW |