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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_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 | |
Sergey Silkin | 40b7050 | 2018-08-27 10:55:07 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 17 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 20 | RateStatistics::RateStatistics(int64_t window_size_ms, float scale) |
| 21 | : buckets_(new Bucket[window_size_ms]()), |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 22 | accumulated_count_(0), |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 23 | num_samples_(0), |
| 24 | oldest_time_(-window_size_ms), |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 25 | oldest_index_(0), |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 26 | scale_(scale), |
| 27 | max_window_size_ms_(window_size_ms), |
| 28 | current_window_size_ms_(max_window_size_ms_) {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 29 | |
Sergey Silkin | 40b7050 | 2018-08-27 10:55:07 +0200 | [diff] [blame] | 30 | RateStatistics::RateStatistics(const RateStatistics& other) |
| 31 | : accumulated_count_(other.accumulated_count_), |
| 32 | num_samples_(other.num_samples_), |
| 33 | oldest_time_(other.oldest_time_), |
| 34 | oldest_index_(other.oldest_index_), |
| 35 | scale_(other.scale_), |
| 36 | max_window_size_ms_(other.max_window_size_ms_), |
| 37 | current_window_size_ms_(other.current_window_size_ms_) { |
| 38 | buckets_ = absl::make_unique<Bucket[]>(other.max_window_size_ms_); |
| 39 | std::copy(other.buckets_.get(), |
| 40 | other.buckets_.get() + other.max_window_size_ms_, buckets_.get()); |
| 41 | } |
| 42 | |
| 43 | RateStatistics::RateStatistics(RateStatistics&& other) = default; |
| 44 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 45 | RateStatistics::~RateStatistics() {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 46 | |
| 47 | void RateStatistics::Reset() { |
| 48 | accumulated_count_ = 0; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 49 | num_samples_ = 0; |
| 50 | oldest_time_ = -max_window_size_ms_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 51 | oldest_index_ = 0; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 52 | current_window_size_ms_ = max_window_size_ms_; |
| 53 | for (int64_t i = 0; i < max_window_size_ms_; i++) |
| 54 | buckets_[i] = Bucket(); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 55 | } |
| 56 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 57 | void RateStatistics::Update(size_t count, int64_t now_ms) { |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 58 | if (now_ms < oldest_time_) { |
| 59 | // Too old data is ignored. |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | EraseOld(now_ms); |
| 64 | |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 65 | // First ever sample, reset window to start now. |
| 66 | if (!IsInitialized()) |
| 67 | oldest_time_ = now_ms; |
| 68 | |
| 69 | uint32_t now_offset = static_cast<uint32_t>(now_ms - oldest_time_); |
| 70 | RTC_DCHECK_LT(now_offset, max_window_size_ms_); |
| 71 | uint32_t index = oldest_index_ + now_offset; |
| 72 | if (index >= max_window_size_ms_) |
| 73 | index -= max_window_size_ms_; |
| 74 | buckets_[index].sum += count; |
| 75 | ++buckets_[index].samples; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 76 | accumulated_count_ += count; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 77 | ++num_samples_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 80 | absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const { |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 81 | // Yeah, this const_cast ain't pretty, but the alternative is to declare most |
| 82 | // of the members as mutable... |
| 83 | const_cast<RateStatistics*>(this)->EraseOld(now_ms); |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 84 | |
| 85 | // If window is a single bucket or there is only one sample in a data set that |
| 86 | // has not grown to the full window size, treat this as rate unavailable. |
| 87 | int64_t active_window_size = now_ms - oldest_time_ + 1; |
| 88 | if (num_samples_ == 0 || active_window_size <= 1 || |
| 89 | (num_samples_ <= 1 && active_window_size < current_window_size_ms_)) { |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 90 | return absl::nullopt; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | float scale = scale_ / active_window_size; |
Oskar Sundbom | f820003 | 2017-11-16 10:57:26 +0100 | [diff] [blame] | 94 | return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void RateStatistics::EraseOld(int64_t now_ms) { |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 98 | if (!IsInitialized()) |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 99 | return; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 100 | |
| 101 | // New oldest time that is included in data set. |
| 102 | int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1; |
| 103 | |
| 104 | // New oldest time is older than the current one, no need to cull data. |
| 105 | if (new_oldest_time <= oldest_time_) |
| 106 | return; |
| 107 | |
| 108 | // Loop over buckets and remove too old data points. |
| 109 | while (num_samples_ > 0 && oldest_time_ < new_oldest_time) { |
| 110 | const Bucket& oldest_bucket = buckets_[oldest_index_]; |
| 111 | RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum); |
| 112 | RTC_DCHECK_GE(num_samples_, oldest_bucket.samples); |
| 113 | accumulated_count_ -= oldest_bucket.sum; |
| 114 | num_samples_ -= oldest_bucket.samples; |
| 115 | buckets_[oldest_index_] = Bucket(); |
| 116 | if (++oldest_index_ >= max_window_size_ms_) |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 117 | oldest_index_ = 0; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 118 | ++oldest_time_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 119 | } |
| 120 | oldest_time_ = new_oldest_time; |
| 121 | } |
| 122 | |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 123 | bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) { |
| 124 | if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_) |
| 125 | return false; |
| 126 | |
| 127 | current_window_size_ms_ = window_size_ms; |
| 128 | EraseOld(now_ms); |
| 129 | return true; |
| 130 | } |
| 131 | |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 132 | bool RateStatistics::IsInitialized() const { |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 133 | return oldest_time_ != -max_window_size_ms_; |
| 134 | } |
| 135 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 136 | } // namespace webrtc |