OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/video/stats_counter.h" | 11 #include "webrtc/video/stats_counter.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/system_wrappers/include/clock.h" | 16 #include "webrtc/system_wrappers/include/clock.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 namespace { | 20 namespace { |
21 // Periodic time interval for processing samples. | 21 // Default periodic time interval for processing samples. |
22 const int64_t kProcessIntervalMs = 2000; | 22 const int64_t kDefaultProcessIntervalMs = 2000; |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 // Class holding periodically computed metrics. | 25 // Class holding periodically computed metrics. |
26 class AggregatedCounter { | 26 class AggregatedCounter { |
27 public: | 27 public: |
28 AggregatedCounter() : last_sample_(0), sum_samples_(0) {} | 28 AggregatedCounter() : last_sample_(0), sum_samples_(0) {} |
29 ~AggregatedCounter() {} | 29 ~AggregatedCounter() {} |
30 | 30 |
31 void Add(int sample) { | 31 void Add(int sample) { |
32 last_sample_ = sample; | 32 last_sample_ = sample; |
(...skipping 24 matching lines...) Expand all Loading... |
57 stats_.average = | 57 stats_.average = |
58 (sum_samples_ + stats_.num_samples / 2) / stats_.num_samples; | 58 (sum_samples_ + stats_.num_samples / 2) / stats_.num_samples; |
59 } | 59 } |
60 int last_sample_; | 60 int last_sample_; |
61 int64_t sum_samples_; | 61 int64_t sum_samples_; |
62 AggregatedStats stats_; | 62 AggregatedStats stats_; |
63 }; | 63 }; |
64 | 64 |
65 // StatsCounter class. | 65 // StatsCounter class. |
66 StatsCounter::StatsCounter(Clock* clock, | 66 StatsCounter::StatsCounter(Clock* clock, |
| 67 int64_t process_intervals_ms, |
67 bool include_empty_intervals, | 68 bool include_empty_intervals, |
68 StatsCounterObserver* observer) | 69 StatsCounterObserver* observer) |
69 : max_(0), | 70 : max_(0), |
70 sum_(0), | 71 sum_(0), |
71 num_samples_(0), | 72 num_samples_(0), |
72 last_sum_(0), | 73 last_sum_(0), |
73 aggregated_counter_(new AggregatedCounter()), | 74 aggregated_counter_(new AggregatedCounter()), |
| 75 process_intervals_ms_(process_intervals_ms), |
74 clock_(clock), | 76 clock_(clock), |
75 include_empty_intervals_(include_empty_intervals), | 77 include_empty_intervals_(include_empty_intervals), |
76 observer_(observer), | 78 observer_(observer), |
77 last_process_time_ms_(-1), | 79 last_process_time_ms_(-1), |
78 paused_(false) {} | 80 paused_(false) { |
| 81 RTC_DCHECK_GT(process_intervals_ms_, 0); |
| 82 } |
79 | 83 |
80 StatsCounter::~StatsCounter() {} | 84 StatsCounter::~StatsCounter() {} |
81 | 85 |
82 AggregatedStats StatsCounter::GetStats() { | 86 AggregatedStats StatsCounter::GetStats() { |
83 return aggregated_counter_->ComputeStats(); | 87 return aggregated_counter_->ComputeStats(); |
84 } | 88 } |
85 | 89 |
86 AggregatedStats StatsCounter::ProcessAndGetStats() { | 90 AggregatedStats StatsCounter::ProcessAndGetStats() { |
87 if (HasSample()) | 91 if (HasSample()) |
88 TryProcess(); | 92 TryProcess(); |
89 return aggregated_counter_->ComputeStats(); | 93 return aggregated_counter_->ComputeStats(); |
90 } | 94 } |
91 | 95 |
92 void StatsCounter::ProcessAndPause() { | 96 void StatsCounter::ProcessAndPause() { |
93 if (HasSample()) | 97 if (HasSample()) |
94 TryProcess(); | 98 TryProcess(); |
95 paused_ = true; | 99 paused_ = true; |
96 } | 100 } |
97 | 101 |
98 bool StatsCounter::HasSample() const { | 102 bool StatsCounter::HasSample() const { |
99 return last_process_time_ms_ != -1; | 103 return last_process_time_ms_ != -1; |
100 } | 104 } |
101 | 105 |
102 bool StatsCounter::TimeToProcess(int* elapsed_intervals) { | 106 bool StatsCounter::TimeToProcess(int* elapsed_intervals) { |
103 int64_t now = clock_->TimeInMilliseconds(); | 107 int64_t now = clock_->TimeInMilliseconds(); |
104 if (last_process_time_ms_ == -1) | 108 if (last_process_time_ms_ == -1) |
105 last_process_time_ms_ = now; | 109 last_process_time_ms_ = now; |
106 | 110 |
107 int64_t diff_ms = now - last_process_time_ms_; | 111 int64_t diff_ms = now - last_process_time_ms_; |
108 if (diff_ms < kProcessIntervalMs) | 112 if (diff_ms < process_intervals_ms_) |
109 return false; | 113 return false; |
110 | 114 |
111 // Advance number of complete kProcessIntervalMs that have passed. | 115 // Advance number of complete |process_intervals_ms_| that have passed. |
112 int64_t num_intervals = diff_ms / kProcessIntervalMs; | 116 int64_t num_intervals = diff_ms / process_intervals_ms_; |
113 last_process_time_ms_ += num_intervals * kProcessIntervalMs; | 117 last_process_time_ms_ += num_intervals * process_intervals_ms_; |
114 | 118 |
115 *elapsed_intervals = num_intervals; | 119 *elapsed_intervals = num_intervals; |
116 return true; | 120 return true; |
117 } | 121 } |
118 | 122 |
119 void StatsCounter::Set(int sample) { | 123 void StatsCounter::Set(int sample) { |
120 TryProcess(); | 124 TryProcess(); |
121 ++num_samples_; | 125 ++num_samples_; |
122 sum_ = sample; | 126 sum_ = sample; |
123 paused_ = false; | 127 paused_ = false; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 178 } |
175 | 179 |
176 bool StatsCounter::IncludeEmptyIntervals() const { | 180 bool StatsCounter::IncludeEmptyIntervals() const { |
177 return include_empty_intervals_ && !paused_ && !aggregated_counter_->Empty(); | 181 return include_empty_intervals_ && !paused_ && !aggregated_counter_->Empty(); |
178 } | 182 } |
179 | 183 |
180 // StatsCounter sub-classes. | 184 // StatsCounter sub-classes. |
181 AvgCounter::AvgCounter(Clock* clock, | 185 AvgCounter::AvgCounter(Clock* clock, |
182 StatsCounterObserver* observer, | 186 StatsCounterObserver* observer, |
183 bool include_empty_intervals) | 187 bool include_empty_intervals) |
184 : StatsCounter(clock, include_empty_intervals, observer) {} | 188 : StatsCounter(clock, |
| 189 kDefaultProcessIntervalMs, |
| 190 include_empty_intervals, |
| 191 observer) {} |
185 | 192 |
186 void AvgCounter::Add(int sample) { | 193 void AvgCounter::Add(int sample) { |
187 StatsCounter::Add(sample); | 194 StatsCounter::Add(sample); |
188 } | 195 } |
189 | 196 |
190 bool AvgCounter::GetMetric(int* metric) const { | 197 bool AvgCounter::GetMetric(int* metric) const { |
191 if (num_samples_ == 0) | 198 if (num_samples_ == 0) |
192 return false; | 199 return false; |
193 *metric = (sum_ + num_samples_ / 2) / num_samples_; | 200 *metric = (sum_ + num_samples_ / 2) / num_samples_; |
194 return true; | 201 return true; |
195 } | 202 } |
196 | 203 |
197 int AvgCounter::GetValueForEmptyInterval() const { | 204 int AvgCounter::GetValueForEmptyInterval() const { |
198 return aggregated_counter_->last_sample(); | 205 return aggregated_counter_->last_sample(); |
199 } | 206 } |
200 | 207 |
201 MaxCounter::MaxCounter(Clock* clock, StatsCounterObserver* observer) | 208 MaxCounter::MaxCounter(Clock* clock, |
| 209 StatsCounterObserver* observer, |
| 210 int64_t process_intervals_ms) |
202 : StatsCounter(clock, | 211 : StatsCounter(clock, |
| 212 process_intervals_ms, |
203 false, // |include_empty_intervals| | 213 false, // |include_empty_intervals| |
204 observer) {} | 214 observer) {} |
205 | 215 |
206 void MaxCounter::Add(int sample) { | 216 void MaxCounter::Add(int sample) { |
207 StatsCounter::Add(sample); | 217 StatsCounter::Add(sample); |
208 } | 218 } |
209 | 219 |
210 bool MaxCounter::GetMetric(int* metric) const { | 220 bool MaxCounter::GetMetric(int* metric) const { |
211 if (num_samples_ == 0) | 221 if (num_samples_ == 0) |
212 return false; | 222 return false; |
213 *metric = max_; | 223 *metric = max_; |
214 return true; | 224 return true; |
215 } | 225 } |
216 | 226 |
217 int MaxCounter::GetValueForEmptyInterval() const { | 227 int MaxCounter::GetValueForEmptyInterval() const { |
218 RTC_NOTREACHED(); | 228 RTC_NOTREACHED(); |
219 return 0; | 229 return 0; |
220 } | 230 } |
221 | 231 |
222 PercentCounter::PercentCounter(Clock* clock, StatsCounterObserver* observer) | 232 PercentCounter::PercentCounter(Clock* clock, StatsCounterObserver* observer) |
223 : StatsCounter(clock, | 233 : StatsCounter(clock, |
| 234 kDefaultProcessIntervalMs, |
224 false, // |include_empty_intervals| | 235 false, // |include_empty_intervals| |
225 observer) {} | 236 observer) {} |
226 | 237 |
227 void PercentCounter::Add(bool sample) { | 238 void PercentCounter::Add(bool sample) { |
228 StatsCounter::Add(sample ? 1 : 0); | 239 StatsCounter::Add(sample ? 1 : 0); |
229 } | 240 } |
230 | 241 |
231 bool PercentCounter::GetMetric(int* metric) const { | 242 bool PercentCounter::GetMetric(int* metric) const { |
232 if (num_samples_ == 0) | 243 if (num_samples_ == 0) |
233 return false; | 244 return false; |
234 *metric = (sum_ * 100 + num_samples_ / 2) / num_samples_; | 245 *metric = (sum_ * 100 + num_samples_ / 2) / num_samples_; |
235 return true; | 246 return true; |
236 } | 247 } |
237 | 248 |
238 int PercentCounter::GetValueForEmptyInterval() const { | 249 int PercentCounter::GetValueForEmptyInterval() const { |
239 RTC_NOTREACHED(); | 250 RTC_NOTREACHED(); |
240 return 0; | 251 return 0; |
241 } | 252 } |
242 | 253 |
243 PermilleCounter::PermilleCounter(Clock* clock, StatsCounterObserver* observer) | 254 PermilleCounter::PermilleCounter(Clock* clock, StatsCounterObserver* observer) |
244 : StatsCounter(clock, | 255 : StatsCounter(clock, |
| 256 kDefaultProcessIntervalMs, |
245 false, // |include_empty_intervals| | 257 false, // |include_empty_intervals| |
246 observer) {} | 258 observer) {} |
247 | 259 |
248 void PermilleCounter::Add(bool sample) { | 260 void PermilleCounter::Add(bool sample) { |
249 StatsCounter::Add(sample ? 1 : 0); | 261 StatsCounter::Add(sample ? 1 : 0); |
250 } | 262 } |
251 | 263 |
252 bool PermilleCounter::GetMetric(int* metric) const { | 264 bool PermilleCounter::GetMetric(int* metric) const { |
253 if (num_samples_ == 0) | 265 if (num_samples_ == 0) |
254 return false; | 266 return false; |
255 *metric = (sum_ * 1000 + num_samples_ / 2) / num_samples_; | 267 *metric = (sum_ * 1000 + num_samples_ / 2) / num_samples_; |
256 return true; | 268 return true; |
257 } | 269 } |
258 | 270 |
259 int PermilleCounter::GetValueForEmptyInterval() const { | 271 int PermilleCounter::GetValueForEmptyInterval() const { |
260 RTC_NOTREACHED(); | 272 RTC_NOTREACHED(); |
261 return 0; | 273 return 0; |
262 } | 274 } |
263 | 275 |
264 RateCounter::RateCounter(Clock* clock, | 276 RateCounter::RateCounter(Clock* clock, |
265 StatsCounterObserver* observer, | 277 StatsCounterObserver* observer, |
266 bool include_empty_intervals) | 278 bool include_empty_intervals) |
267 : StatsCounter(clock, include_empty_intervals, observer) {} | 279 : StatsCounter(clock, |
| 280 kDefaultProcessIntervalMs, |
| 281 include_empty_intervals, |
| 282 observer) {} |
268 | 283 |
269 void RateCounter::Add(int sample) { | 284 void RateCounter::Add(int sample) { |
270 StatsCounter::Add(sample); | 285 StatsCounter::Add(sample); |
271 } | 286 } |
272 | 287 |
273 bool RateCounter::GetMetric(int* metric) const { | 288 bool RateCounter::GetMetric(int* metric) const { |
274 if (num_samples_ == 0) | 289 if (num_samples_ == 0) |
275 return false; | 290 return false; |
276 *metric = (sum_ * 1000 + kProcessIntervalMs / 2) / kProcessIntervalMs; | 291 *metric = (sum_ * 1000 + process_intervals_ms_ / 2) / process_intervals_ms_; |
277 return true; | 292 return true; |
278 } | 293 } |
279 | 294 |
280 int RateCounter::GetValueForEmptyInterval() const { | 295 int RateCounter::GetValueForEmptyInterval() const { |
281 return 0; | 296 return 0; |
282 } | 297 } |
283 | 298 |
284 RateAccCounter::RateAccCounter(Clock* clock, | 299 RateAccCounter::RateAccCounter(Clock* clock, |
285 StatsCounterObserver* observer, | 300 StatsCounterObserver* observer, |
286 bool include_empty_intervals) | 301 bool include_empty_intervals) |
287 : StatsCounter(clock, include_empty_intervals, observer) {} | 302 : StatsCounter(clock, |
| 303 kDefaultProcessIntervalMs, |
| 304 include_empty_intervals, |
| 305 observer) {} |
288 | 306 |
289 void RateAccCounter::Set(int sample) { | 307 void RateAccCounter::Set(int sample) { |
290 StatsCounter::Set(sample); | 308 StatsCounter::Set(sample); |
291 } | 309 } |
292 | 310 |
293 bool RateAccCounter::GetMetric(int* metric) const { | 311 bool RateAccCounter::GetMetric(int* metric) const { |
294 if (num_samples_ == 0 || last_sum_ > sum_) | 312 if (num_samples_ == 0 || last_sum_ > sum_) |
295 return false; | 313 return false; |
296 *metric = | 314 *metric = ((sum_ - last_sum_) * 1000 + process_intervals_ms_ / 2) / |
297 ((sum_ - last_sum_) * 1000 + kProcessIntervalMs / 2) / kProcessIntervalMs; | 315 process_intervals_ms_; |
298 return true; | 316 return true; |
299 } | 317 } |
300 | 318 |
301 int RateAccCounter::GetValueForEmptyInterval() const { | 319 int RateAccCounter::GetValueForEmptyInterval() const { |
302 return 0; | 320 return 0; |
303 } | 321 } |
304 | 322 |
305 } // namespace webrtc | 323 } // namespace webrtc |
OLD | NEW |