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 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // Class holding gathered samples within a process interval. | 81 // Class holding gathered samples within a process interval. |
82 class Samples { | 82 class Samples { |
83 public: | 83 public: |
84 Samples() : total_count_(0) {} | 84 Samples() : total_count_(0) {} |
85 ~Samples() {} | 85 ~Samples() {} |
86 | 86 |
87 void Add(int sample, uint32_t stream_id) { | 87 void Add(int sample, uint32_t stream_id) { |
88 samples_[stream_id].Add(sample); | 88 samples_[stream_id].Add(sample); |
89 ++total_count_; | 89 ++total_count_; |
90 } | 90 } |
91 void Set(int sample, uint32_t stream_id) { | 91 void Set(int64_t sample, uint32_t stream_id) { |
92 samples_[stream_id].Set(sample); | 92 samples_[stream_id].Set(sample); |
93 ++total_count_; | 93 ++total_count_; |
94 } | 94 } |
| 95 void SetLast(int64_t sample, uint32_t stream_id) { |
| 96 samples_[stream_id].SetLast(sample); |
| 97 } |
| 98 int64_t GetLast(uint32_t stream_id) { return samples_[stream_id].GetLast(); } |
95 | 99 |
96 int64_t Count() const { return total_count_; } | 100 int64_t Count() const { return total_count_; } |
97 bool Empty() const { return total_count_ == 0; } | 101 bool Empty() const { return total_count_ == 0; } |
98 | 102 |
99 int64_t Sum() const { | 103 int64_t Sum() const { |
100 int64_t sum = 0; | 104 int64_t sum = 0; |
101 for (const auto& it : samples_) | 105 for (const auto& it : samples_) |
102 sum += it.second.sum_; | 106 sum += it.second.sum_; |
103 return sum; | 107 return sum; |
104 } | 108 } |
(...skipping 26 matching lines...) Expand all Loading... |
131 return (count > 0) ? sum_diff : -1; | 135 return (count > 0) ? sum_diff : -1; |
132 } | 136 } |
133 | 137 |
134 private: | 138 private: |
135 struct Stats { | 139 struct Stats { |
136 void Add(int sample) { | 140 void Add(int sample) { |
137 sum_ += sample; | 141 sum_ += sample; |
138 ++count_; | 142 ++count_; |
139 max_ = std::max(sample, max_); | 143 max_ = std::max(sample, max_); |
140 } | 144 } |
141 void Set(int sample) { | 145 void Set(int64_t sample) { |
142 sum_ = sample; | 146 sum_ = sample; |
143 ++count_; | 147 ++count_; |
144 } | 148 } |
| 149 void SetLast(int64_t sample) { last_sum_ = sample; } |
| 150 int64_t GetLast() const { return last_sum_; } |
145 void Reset() { | 151 void Reset() { |
146 if (count_ > 0) | 152 if (count_ > 0) |
147 last_sum_ = sum_; | 153 last_sum_ = sum_; |
148 sum_ = 0; | 154 sum_ = 0; |
149 count_ = 0; | 155 count_ = 0; |
150 max_ = std::numeric_limits<int>::min(); | 156 max_ = std::numeric_limits<int>::min(); |
151 } | 157 } |
152 | 158 |
153 int max_ = std::numeric_limits<int>::min(); | 159 int max_ = std::numeric_limits<int>::min(); |
154 int64_t count_ = 0; | 160 int64_t count_ = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
165 int64_t process_intervals_ms, | 171 int64_t process_intervals_ms, |
166 bool include_empty_intervals, | 172 bool include_empty_intervals, |
167 StatsCounterObserver* observer) | 173 StatsCounterObserver* observer) |
168 : include_empty_intervals_(include_empty_intervals), | 174 : include_empty_intervals_(include_empty_intervals), |
169 process_intervals_ms_(process_intervals_ms), | 175 process_intervals_ms_(process_intervals_ms), |
170 aggregated_counter_(new AggregatedCounter()), | 176 aggregated_counter_(new AggregatedCounter()), |
171 samples_(new Samples()), | 177 samples_(new Samples()), |
172 clock_(clock), | 178 clock_(clock), |
173 observer_(observer), | 179 observer_(observer), |
174 last_process_time_ms_(-1), | 180 last_process_time_ms_(-1), |
175 paused_(false) { | 181 paused_(false), |
| 182 pause_time_ms_(-1), |
| 183 min_pause_time_ms_(0) { |
176 RTC_DCHECK_GT(process_intervals_ms_, 0); | 184 RTC_DCHECK_GT(process_intervals_ms_, 0); |
177 } | 185 } |
178 | 186 |
179 StatsCounter::~StatsCounter() {} | 187 StatsCounter::~StatsCounter() {} |
180 | 188 |
181 AggregatedStats StatsCounter::GetStats() { | 189 AggregatedStats StatsCounter::GetStats() { |
182 return aggregated_counter_->ComputeStats(); | 190 return aggregated_counter_->ComputeStats(); |
183 } | 191 } |
184 | 192 |
185 AggregatedStats StatsCounter::ProcessAndGetStats() { | 193 AggregatedStats StatsCounter::ProcessAndGetStats() { |
186 if (HasSample()) | 194 if (HasSample()) |
187 TryProcess(); | 195 TryProcess(); |
188 return aggregated_counter_->ComputeStats(); | 196 return aggregated_counter_->ComputeStats(); |
189 } | 197 } |
190 | 198 |
| 199 void StatsCounter::ProcessAndPauseForDuration(int64_t min_pause_time_ms) { |
| 200 ProcessAndPause(); |
| 201 min_pause_time_ms_ = min_pause_time_ms; |
| 202 } |
| 203 |
191 void StatsCounter::ProcessAndPause() { | 204 void StatsCounter::ProcessAndPause() { |
192 if (HasSample()) | 205 if (HasSample()) |
193 TryProcess(); | 206 TryProcess(); |
194 paused_ = true; | 207 paused_ = true; |
| 208 pause_time_ms_ = clock_->TimeInMilliseconds(); |
| 209 } |
| 210 |
| 211 void StatsCounter::ProcessAndStopPause() { |
| 212 if (HasSample()) |
| 213 TryProcess(); |
| 214 Resume(); |
195 } | 215 } |
196 | 216 |
197 bool StatsCounter::HasSample() const { | 217 bool StatsCounter::HasSample() const { |
198 return last_process_time_ms_ != -1; | 218 return last_process_time_ms_ != -1; |
199 } | 219 } |
200 | 220 |
201 bool StatsCounter::TimeToProcess(int* elapsed_intervals) { | 221 bool StatsCounter::TimeToProcess(int* elapsed_intervals) { |
202 int64_t now = clock_->TimeInMilliseconds(); | 222 int64_t now = clock_->TimeInMilliseconds(); |
203 if (last_process_time_ms_ == -1) | 223 if (last_process_time_ms_ == -1) |
204 last_process_time_ms_ = now; | 224 last_process_time_ms_ = now; |
205 | 225 |
206 int64_t diff_ms = now - last_process_time_ms_; | 226 int64_t diff_ms = now - last_process_time_ms_; |
207 if (diff_ms < process_intervals_ms_) | 227 if (diff_ms < process_intervals_ms_) |
208 return false; | 228 return false; |
209 | 229 |
210 // Advance number of complete |process_intervals_ms_| that have passed. | 230 // Advance number of complete |process_intervals_ms_| that have passed. |
211 int64_t num_intervals = diff_ms / process_intervals_ms_; | 231 int64_t num_intervals = diff_ms / process_intervals_ms_; |
212 last_process_time_ms_ += num_intervals * process_intervals_ms_; | 232 last_process_time_ms_ += num_intervals * process_intervals_ms_; |
213 | 233 |
214 *elapsed_intervals = num_intervals; | 234 *elapsed_intervals = num_intervals; |
215 return true; | 235 return true; |
216 } | 236 } |
217 | 237 |
218 void StatsCounter::Set(int sample, uint32_t stream_id) { | |
219 TryProcess(); | |
220 samples_->Set(sample, stream_id); | |
221 paused_ = false; | |
222 } | |
223 | |
224 void StatsCounter::Add(int sample) { | 238 void StatsCounter::Add(int sample) { |
225 TryProcess(); | 239 TryProcess(); |
226 samples_->Add(sample, kStreamId0); | 240 samples_->Add(sample, kStreamId0); |
227 paused_ = false; | 241 ResumeIfMinTimePassed(); |
| 242 } |
| 243 |
| 244 void StatsCounter::Set(int64_t sample, uint32_t stream_id) { |
| 245 if (paused_ && sample == samples_->GetLast(stream_id)) { |
| 246 // Do not add same sample while paused (will reset pause). |
| 247 return; |
| 248 } |
| 249 TryProcess(); |
| 250 samples_->Set(sample, stream_id); |
| 251 ResumeIfMinTimePassed(); |
| 252 } |
| 253 |
| 254 void StatsCounter::SetLast(int64_t sample, uint32_t stream_id) { |
| 255 RTC_DCHECK(!HasSample()) << "Should be set before first sample is added."; |
| 256 samples_->SetLast(sample, stream_id); |
228 } | 257 } |
229 | 258 |
230 // Reports periodically computed metric. | 259 // Reports periodically computed metric. |
231 void StatsCounter::ReportMetricToAggregatedCounter( | 260 void StatsCounter::ReportMetricToAggregatedCounter( |
232 int value, | 261 int value, |
233 int num_values_to_add) const { | 262 int num_values_to_add) const { |
234 for (int i = 0; i < num_values_to_add; ++i) { | 263 for (int i = 0; i < num_values_to_add; ++i) { |
235 aggregated_counter_->Add(value); | 264 aggregated_counter_->Add(value); |
236 if (observer_) | 265 if (observer_) |
237 observer_->OnMetricUpdated(value); | 266 observer_->OnMetricUpdated(value); |
(...skipping 20 matching lines...) Expand all Loading... |
258 empty_intervals); | 287 empty_intervals); |
259 } | 288 } |
260 | 289 |
261 // Reset samples for elapsed interval. | 290 // Reset samples for elapsed interval. |
262 samples_->Reset(); | 291 samples_->Reset(); |
263 } | 292 } |
264 | 293 |
265 bool StatsCounter::IncludeEmptyIntervals() const { | 294 bool StatsCounter::IncludeEmptyIntervals() const { |
266 return include_empty_intervals_ && !paused_ && !aggregated_counter_->Empty(); | 295 return include_empty_intervals_ && !paused_ && !aggregated_counter_->Empty(); |
267 } | 296 } |
| 297 void StatsCounter::ResumeIfMinTimePassed() { |
| 298 if (paused_ && |
| 299 (clock_->TimeInMilliseconds() - pause_time_ms_) >= min_pause_time_ms_) { |
| 300 Resume(); |
| 301 } |
| 302 } |
| 303 |
| 304 void StatsCounter::Resume() { |
| 305 paused_ = false; |
| 306 min_pause_time_ms_ = 0; |
| 307 } |
268 | 308 |
269 // StatsCounter sub-classes. | 309 // StatsCounter sub-classes. |
270 AvgCounter::AvgCounter(Clock* clock, | 310 AvgCounter::AvgCounter(Clock* clock, |
271 StatsCounterObserver* observer, | 311 StatsCounterObserver* observer, |
272 bool include_empty_intervals) | 312 bool include_empty_intervals) |
273 : StatsCounter(clock, | 313 : StatsCounter(clock, |
274 kDefaultProcessIntervalMs, | 314 kDefaultProcessIntervalMs, |
275 include_empty_intervals, | 315 include_empty_intervals, |
276 observer) {} | 316 observer) {} |
277 | 317 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 } | 431 } |
392 | 432 |
393 RateAccCounter::RateAccCounter(Clock* clock, | 433 RateAccCounter::RateAccCounter(Clock* clock, |
394 StatsCounterObserver* observer, | 434 StatsCounterObserver* observer, |
395 bool include_empty_intervals) | 435 bool include_empty_intervals) |
396 : StatsCounter(clock, | 436 : StatsCounter(clock, |
397 kDefaultProcessIntervalMs, | 437 kDefaultProcessIntervalMs, |
398 include_empty_intervals, | 438 include_empty_intervals, |
399 observer) {} | 439 observer) {} |
400 | 440 |
401 void RateAccCounter::Set(int sample, uint32_t stream_id) { | 441 void RateAccCounter::Set(int64_t sample, uint32_t stream_id) { |
402 StatsCounter::Set(sample, stream_id); | 442 StatsCounter::Set(sample, stream_id); |
403 } | 443 } |
404 | 444 |
| 445 void RateAccCounter::SetLast(int64_t sample, uint32_t stream_id) { |
| 446 StatsCounter::SetLast(sample, stream_id); |
| 447 } |
| 448 |
405 bool RateAccCounter::GetMetric(int* metric) const { | 449 bool RateAccCounter::GetMetric(int* metric) const { |
406 int64_t diff = samples_->Diff(); | 450 int64_t diff = samples_->Diff(); |
407 if (diff < 0 || (!include_empty_intervals_ && diff == 0)) | 451 if (diff < 0 || (!include_empty_intervals_ && diff == 0)) |
408 return false; | 452 return false; |
409 | 453 |
410 *metric = (diff * 1000 + process_intervals_ms_ / 2) / process_intervals_ms_; | 454 *metric = (diff * 1000 + process_intervals_ms_ / 2) / process_intervals_ms_; |
411 return true; | 455 return true; |
412 } | 456 } |
413 | 457 |
414 int RateAccCounter::GetValueForEmptyInterval() const { | 458 int RateAccCounter::GetValueForEmptyInterval() const { |
415 return 0; | 459 return 0; |
416 } | 460 } |
417 | 461 |
418 } // namespace webrtc | 462 } // namespace webrtc |
OLD | NEW |