| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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/base/rate_statistics.h" | 11 #include "webrtc/base/rate_statistics.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <algorithm> |
| 14 |
| 15 #include "webrtc/base/checks.h" |
| 14 | 16 |
| 15 namespace webrtc { | 17 namespace webrtc { |
| 16 | 18 |
| 17 RateStatistics::RateStatistics(uint32_t window_size_ms, float scale) | 19 RateStatistics::RateStatistics(uint32_t window_size_ms, float scale) |
| 18 : num_buckets_(window_size_ms + 1), // N ms in (N+1) buckets. | 20 : num_buckets_(window_size_ms + 1), // N ms in (N+1) buckets. |
| 19 buckets_(new size_t[num_buckets_]()), | 21 buckets_(new size_t[num_buckets_]()), |
| 20 accumulated_count_(0), | 22 accumulated_count_(0), |
| 21 oldest_time_(0), | 23 oldest_time_(0), |
| 22 oldest_index_(0), | 24 oldest_index_(0), |
| 23 scale_(scale / (num_buckets_ - 1)) {} | 25 scale_(scale) {} |
| 24 | 26 |
| 25 RateStatistics::~RateStatistics() {} | 27 RateStatistics::~RateStatistics() {} |
| 26 | 28 |
| 27 void RateStatistics::Reset() { | 29 void RateStatistics::Reset() { |
| 28 accumulated_count_ = 0; | 30 accumulated_count_ = 0; |
| 29 oldest_time_ = 0; | 31 oldest_time_ = 0; |
| 30 oldest_index_ = 0; | 32 oldest_index_ = 0; |
| 31 for (int i = 0; i < num_buckets_; i++) { | 33 for (int i = 0; i < num_buckets_; i++) { |
| 32 buckets_[i] = 0; | 34 buckets_[i] = 0; |
| 33 } | 35 } |
| 34 } | 36 } |
| 35 | 37 |
| 36 void RateStatistics::Update(size_t count, int64_t now_ms) { | 38 void RateStatistics::Update(size_t count, int64_t now_ms) { |
| 37 if (now_ms < oldest_time_) { | 39 if (now_ms < oldest_time_) { |
| 38 // Too old data is ignored. | 40 // Too old data is ignored. |
| 39 return; | 41 return; |
| 40 } | 42 } |
| 41 | 43 |
| 42 EraseOld(now_ms); | 44 EraseOld(now_ms); |
| 43 | 45 |
| 44 int now_offset = static_cast<int>(now_ms - oldest_time_); | 46 int now_offset = static_cast<int>(now_ms - oldest_time_); |
| 45 assert(now_offset < num_buckets_); | 47 RTC_DCHECK_LT(now_offset, num_buckets_); |
| 46 int index = oldest_index_ + now_offset; | 48 int index = oldest_index_ + now_offset; |
| 47 if (index >= num_buckets_) { | 49 if (index >= num_buckets_) { |
| 48 index -= num_buckets_; | 50 index -= num_buckets_; |
| 49 } | 51 } |
| 50 buckets_[index] += count; | 52 buckets_[index] += count; |
| 51 accumulated_count_ += count; | 53 accumulated_count_ += count; |
| 52 } | 54 } |
| 53 | 55 |
| 54 uint32_t RateStatistics::Rate(int64_t now_ms) { | 56 uint32_t RateStatistics::Rate(int64_t now_ms) { |
| 55 EraseOld(now_ms); | 57 EraseOld(now_ms); |
| 56 return static_cast<uint32_t>(accumulated_count_ * scale_ + 0.5f); | 58 float scale = scale_ / (now_ms - oldest_time_ + 1); |
| 59 return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void RateStatistics::EraseOld(int64_t now_ms) { | 62 void RateStatistics::EraseOld(int64_t now_ms) { |
| 60 int64_t new_oldest_time = now_ms - num_buckets_ + 1; | 63 int64_t new_oldest_time = now_ms - num_buckets_ + 1; |
| 61 if (new_oldest_time <= oldest_time_) { | 64 if (new_oldest_time <= oldest_time_) { |
| 65 if (accumulated_count_ == 0) |
| 66 oldest_time_ = now_ms; |
| 62 return; | 67 return; |
| 63 } | 68 } |
| 64 | |
| 65 while (oldest_time_ < new_oldest_time) { | 69 while (oldest_time_ < new_oldest_time) { |
| 66 size_t count_in_oldest_bucket = buckets_[oldest_index_]; | 70 size_t count_in_oldest_bucket = buckets_[oldest_index_]; |
| 67 assert(accumulated_count_ >= count_in_oldest_bucket); | 71 RTC_DCHECK_GE(accumulated_count_, count_in_oldest_bucket); |
| 68 accumulated_count_ -= count_in_oldest_bucket; | 72 accumulated_count_ -= count_in_oldest_bucket; |
| 69 buckets_[oldest_index_] = 0; | 73 buckets_[oldest_index_] = 0; |
| 70 if (++oldest_index_ >= num_buckets_) { | 74 if (++oldest_index_ >= num_buckets_) { |
| 71 oldest_index_ = 0; | 75 oldest_index_ = 0; |
| 72 } | 76 } |
| 73 ++oldest_time_; | 77 ++oldest_time_; |
| 74 if (accumulated_count_ == 0) { | 78 if (accumulated_count_ == 0) { |
| 75 // This guarantees we go through all the buckets at most once, even if | 79 // This guarantees we go through all the buckets at most once, even if |
| 76 // |new_oldest_time| is far greater than |oldest_time_|. | 80 // |new_oldest_time| is far greater than |oldest_time_|. |
| 81 new_oldest_time = now_ms; |
| 77 break; | 82 break; |
| 78 } | 83 } |
| 79 } | 84 } |
| 80 oldest_time_ = new_oldest_time; | 85 oldest_time_ = new_oldest_time; |
| 81 } | 86 } |
| 82 | 87 |
| 83 } // namespace webrtc | 88 } // namespace webrtc |
| OLD | NEW |