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