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 <memory> | |
13 | |
14 #include "webrtc/base/criticalsection.h" | |
15 #include "webrtc/base/thread_annotations.h" | |
10 #include "webrtc/system_wrappers/include/metrics.h" | 16 #include "webrtc/system_wrappers/include/metrics.h" |
11 | 17 |
12 // Default implementation of histogram methods for WebRTC clients that do not | 18 // Default implementation of histogram methods for WebRTC clients that do not |
13 // want to provide their own implementation. | 19 // want to provide their own implementation. |
14 | 20 |
21 namespace { | |
22 // Limit for the maximum number of sample values that can be stored. | |
23 const int kMaxSampleMapSize = 10000; | |
pbos-webrtc
2016/05/06 19:58:40
I think this should be covered by bucket_count?
åsapersson
2016/05/09 14:47:41
see other comment
| |
24 | |
25 class RtcHistogram { | |
26 public: | |
27 RtcHistogram(const std::string& name, int min, int max, int bucket_count) | |
28 : info_(name, min, max, bucket_count) {} | |
pbos-webrtc
2016/05/06 19:58:40
I think the ctor here should set the size of sampl
åsapersson
2016/05/09 14:47:41
see other comment
| |
29 ~RtcHistogram() {} | |
30 | |
31 int NumSamples() const { | |
32 int num_samples = 0; | |
33 rtc::CritScope cs(&crit_); | |
34 for (const auto& sample : info_.samples) { | |
35 num_samples += sample.second; | |
36 } | |
37 return num_samples; | |
38 } | |
39 | |
40 int MinSample() const { | |
41 rtc::CritScope cs(&crit_); | |
42 if (info_.samples.empty()) { | |
43 return -1; | |
44 } | |
45 return info_.samples.begin()->first; | |
46 } | |
47 | |
48 int NumEvents(int sample) const { | |
49 rtc::CritScope cs(&crit_); | |
50 const auto it = info_.samples.find(sample); | |
51 if (info_.samples.find(sample) == info_.samples.end()) { | |
52 return 0; | |
53 } | |
54 return it->second; | |
55 } | |
56 | |
57 void Add(int sample) { | |
58 if (sample < 0) | |
59 sample = 0; | |
pbos-webrtc
2016/05/06 19:58:40
Is this an invalid sample? E.g. should we DCHECK i
åsapersson
2016/05/09 14:47:41
Changed to limit by min bucket value.
| |
60 if (sample > info_.max) | |
61 sample = info_.max; | |
62 | |
63 rtc::CritScope cs(&crit_); | |
64 if (info_.samples.size() == kMaxSampleMapSize && | |
pbos-webrtc
2016/05/06 19:58:40
samples here should be fixed-size to number of buc
åsapersson
2016/05/09 14:47:41
see other comment
| |
65 info_.samples.find(sample) == info_.samples.end()) { | |
66 return; | |
67 } | |
68 ++info_.samples[sample]; | |
69 } | |
70 | |
71 // Clears samples. | |
72 void Reset() { | |
73 rtc::CritScope cs(&crit_); | |
74 info_.samples.clear(); | |
75 } | |
76 | |
77 // Returns a copy (or nullptr if there are no samples) and clears samples. | |
78 webrtc::metrics::SampleInfo* GetAndReset() { | |
pbos-webrtc
2016/05/06 19:58:40
Can this just be SampleInfo? You can put the anony
åsapersson
2016/05/09 14:47:41
Done.
| |
79 rtc::CritScope cs(&crit_); | |
80 if (info_.samples.empty()) | |
81 return nullptr; | |
82 | |
83 webrtc::metrics::SampleInfo* copy = new webrtc::metrics::SampleInfo( | |
84 info_.name, info_.min, info_.max, info_.bucket_count); | |
85 copy->samples = info_.samples; | |
86 info_.samples.clear(); | |
87 return copy; | |
88 } | |
89 | |
90 const std::string& name() const { return info_.name; } | |
91 | |
92 private: | |
93 rtc::CriticalSection crit_; | |
94 webrtc::metrics::SampleInfo info_; | |
95 | |
96 RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogram); | |
97 }; | |
98 | |
99 // Map holding samples of a histogram (mapped by the histogram name). | |
100 bool rtc_histograms_enabled_ = false; | |
101 rtc::CriticalSection rtc_histograms_crit_; | |
102 std::map<std::string, std::unique_ptr<RtcHistogram>> rtc_histograms_ | |
103 GUARDED_BY(rtc_histograms_crit_); | |
104 } // namespace | |
105 | |
15 namespace webrtc { | 106 namespace webrtc { |
16 namespace metrics { | 107 namespace metrics { |
17 | 108 class Histogram; |
18 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, | 109 |
19 int bucket_count) { return NULL; } | 110 // Implementation of histogram methods in |
20 | 111 // webrtc/system_wrappers/interface/metrics.h. |
112 | |
113 // Histogram with exponentially spaced buckets. | |
114 Histogram* HistogramFactoryGetCounts(const std::string& name, | |
115 int min, | |
116 int max, | |
117 int bucket_count) { | |
118 rtc::CritScope cs(&rtc_histograms_crit_); | |
119 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { | |
120 rtc_histograms_[name].reset(new RtcHistogram(name, min, max, bucket_count)); | |
121 } | |
122 const auto& it = rtc_histograms_.find(name); | |
123 return reinterpret_cast<Histogram*>(it->second.get()); | |
124 } | |
125 | |
126 // Histogram with linearly spaced buckets. | |
21 Histogram* HistogramFactoryGetEnumeration(const std::string& name, | 127 Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
22 int boundary) { return NULL; } | 128 int boundary) { |
23 | 129 rtc::CritScope cs(&rtc_histograms_crit_); |
24 void HistogramAdd( | 130 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { |
25 Histogram* histogram_pointer, const std::string& name, int sample) {} | 131 rtc_histograms_[name].reset( |
132 new RtcHistogram(name, 1, boundary, boundary + 1)); | |
133 } | |
134 const auto& it = rtc_histograms_.find(name); | |
135 return reinterpret_cast<Histogram*>(it->second.get()); | |
136 } | |
137 | |
138 void HistogramAdd(Histogram* histogram_pointer, | |
139 const std::string& name, | |
140 int sample) { | |
141 if (!rtc_histograms_enabled_) | |
142 return; | |
143 | |
144 RtcHistogram* ptr = reinterpret_cast<RtcHistogram*>(histogram_pointer); | |
145 RTC_DCHECK(ptr->name() == name) << "The name should not vary."; | |
pbos-webrtc
2016/05/06 19:58:40
RTC_DCHECK_EQ(name, ptr->name())?
åsapersson
2016/05/09 14:47:41
Done.
| |
146 ptr->Add(sample); | |
147 } | |
148 | |
149 SampleInfo::SampleInfo(const std::string& name, | |
150 int min, | |
151 int max, | |
152 int bucket_count) | |
153 : name(name), min(min), max(max), bucket_count(bucket_count) {} | |
154 | |
155 SampleInfo::~SampleInfo() {} | |
pbos-webrtc
2016/05/06 19:58:40
Don't need a dtor I think
åsapersson
2016/05/09 14:47:41
Gives compile error w/o.
pbos-webrtc
2016/05/09 21:21:13
Even without ~SampleInfo() in header?
åsapersson
2016/05/13 09:15:53
yes
| |
156 | |
157 // Implementation of methods in metrics_default.h. | |
pbos-webrtc
2016/05/06 19:58:40
s/methods/global functions/
åsapersson
2016/05/09 14:47:41
Done.
| |
158 void Enable() { | |
159 rtc_histograms_enabled_ = true; | |
160 } | |
161 | |
162 void GetAndReset( | |
163 std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) { | |
164 histograms->clear(); | |
165 rtc::CritScope cs(&rtc_histograms_crit_); | |
166 for (const auto& kv : rtc_histograms_) { | |
167 SampleInfo* info = kv.second->GetAndReset(); | |
168 if (info) | |
169 (*histograms)[kv.first].reset(info); | |
170 } | |
171 } | |
172 | |
173 void Reset() { | |
174 rtc::CritScope cs(&rtc_histograms_crit_); | |
175 for (const auto& kv : rtc_histograms_) | |
176 kv.second->Reset(); | |
177 } | |
178 | |
179 int NumEvents(const std::string& name, int sample) { | |
180 rtc::CritScope cs(&rtc_histograms_crit_); | |
181 const auto& it = rtc_histograms_.find(name); | |
182 if (it == rtc_histograms_.end()) | |
183 return 0; | |
184 | |
185 return it->second->NumEvents(sample); | |
186 } | |
187 | |
188 int NumSamples(const std::string& name) { | |
189 rtc::CritScope cs(&rtc_histograms_crit_); | |
190 const auto& it = rtc_histograms_.find(name); | |
191 if (it == rtc_histograms_.end()) | |
192 return 0; | |
193 | |
194 return it->second->NumSamples(); | |
195 } | |
196 | |
197 int MinSample(const std::string& name) { | |
198 rtc::CritScope cs(&rtc_histograms_crit_); | |
199 const auto& it = rtc_histograms_.find(name); | |
200 if (it == rtc_histograms_.end()) | |
201 return -1; | |
202 | |
203 return it->second->MinSample(); | |
204 } | |
26 | 205 |
27 } // namespace metrics | 206 } // namespace metrics |
28 } // namespace webrtc | 207 } // namespace webrtc |
29 | |
OLD | NEW |