sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 11 | #include "webrtc/base/rate_statistics.h" |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 12 | |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 13 | #include <algorithm> |
| 14 | |
| 15 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 16 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
| 19 | RateStatistics::RateStatistics(uint32_t window_size_ms, float scale) |
| 20 | : num_buckets_(window_size_ms + 1), // N ms in (N+1) buckets. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 21 | buckets_(new size_t[num_buckets_]()), |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 22 | accumulated_count_(0), |
| 23 | oldest_time_(0), |
| 24 | oldest_index_(0), |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 25 | scale_(scale) {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 26 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 27 | RateStatistics::~RateStatistics() {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 28 | |
| 29 | void RateStatistics::Reset() { |
| 30 | accumulated_count_ = 0; |
| 31 | oldest_time_ = 0; |
| 32 | oldest_index_ = 0; |
| 33 | for (int i = 0; i < num_buckets_; i++) { |
| 34 | buckets_[i] = 0; |
| 35 | } |
| 36 | } |
| 37 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 38 | void RateStatistics::Update(size_t count, int64_t now_ms) { |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 39 | if (now_ms < oldest_time_) { |
| 40 | // Too old data is ignored. |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | EraseOld(now_ms); |
| 45 | |
| 46 | int now_offset = static_cast<int>(now_ms - oldest_time_); |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 47 | RTC_DCHECK_LT(now_offset, num_buckets_); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 48 | int index = oldest_index_ + now_offset; |
| 49 | if (index >= num_buckets_) { |
| 50 | index -= num_buckets_; |
| 51 | } |
| 52 | buckets_[index] += count; |
| 53 | accumulated_count_ += count; |
| 54 | } |
| 55 | |
| 56 | uint32_t RateStatistics::Rate(int64_t now_ms) { |
| 57 | EraseOld(now_ms); |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 58 | float scale = scale_ / (now_ms - oldest_time_ + 1); |
| 59 | return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void RateStatistics::EraseOld(int64_t now_ms) { |
| 63 | int64_t new_oldest_time = now_ms - num_buckets_ + 1; |
| 64 | if (new_oldest_time <= oldest_time_) { |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 65 | if (accumulated_count_ == 0) |
| 66 | oldest_time_ = now_ms; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 69 | while (oldest_time_ < new_oldest_time) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 70 | size_t count_in_oldest_bucket = buckets_[oldest_index_]; |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 71 | RTC_DCHECK_GE(accumulated_count_, count_in_oldest_bucket); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 72 | accumulated_count_ -= count_in_oldest_bucket; |
| 73 | buckets_[oldest_index_] = 0; |
| 74 | if (++oldest_index_ >= num_buckets_) { |
| 75 | oldest_index_ = 0; |
| 76 | } |
| 77 | ++oldest_time_; |
| 78 | if (accumulated_count_ == 0) { |
| 79 | // This guarantees we go through all the buckets at most once, even if |
| 80 | // |new_oldest_time| is far greater than |oldest_time_|. |
Stefan Holmer | fb8fc53 | 2016-04-22 15:48:23 +0200 | [diff] [blame^] | 81 | new_oldest_time = now_ms; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | } |
| 85 | oldest_time_ = new_oldest_time; |
| 86 | } |
| 87 | |
| 88 | } // namespace webrtc |