sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/rate_statistics.h" |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 12 | |
Stefan Holmer | fb8fc53 | 2016-04-22 13:48:23 | [diff] [blame] | 13 | #include <algorithm> |
Yves Gerey | a688d11 | 2019-12-31 15:16:51 | [diff] [blame] | 14 | #include <limits> |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 15 | #include <memory> |
Stefan Holmer | fb8fc53 | 2016-04-22 13:48:23 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [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 | [diff] [blame] | 20 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 23 | RateStatistics::Bucket::Bucket(int64_t timestamp) |
| 24 | : sum(0), num_samples(0), timestamp(timestamp) {} |
| 25 | |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 26 | RateStatistics::RateStatistics(int64_t window_size_ms, float scale) |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 27 | : accumulated_count_(0), |
| 28 | first_timestamp_(-1), |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 29 | num_samples_(0), |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 30 | scale_(scale), |
| 31 | max_window_size_ms_(window_size_ms), |
| 32 | current_window_size_ms_(max_window_size_ms_) {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 33 | |
Sergey Silkin | 40b7050 | 2018-08-27 08:55:07 | [diff] [blame] | 34 | RateStatistics::RateStatistics(const RateStatistics& other) |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 35 | : buckets_(other.buckets_), |
| 36 | accumulated_count_(other.accumulated_count_), |
| 37 | first_timestamp_(other.first_timestamp_), |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 38 | overflow_(other.overflow_), |
Sergey Silkin | 40b7050 | 2018-08-27 08:55:07 | [diff] [blame] | 39 | num_samples_(other.num_samples_), |
Sergey Silkin | 40b7050 | 2018-08-27 08:55:07 | [diff] [blame] | 40 | scale_(other.scale_), |
| 41 | max_window_size_ms_(other.max_window_size_ms_), |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 42 | current_window_size_ms_(other.current_window_size_ms_) {} |
Sergey Silkin | 40b7050 | 2018-08-27 08:55:07 | [diff] [blame] | 43 | |
| 44 | RateStatistics::RateStatistics(RateStatistics&& other) = default; |
| 45 | |
tkchin | f75d008 | 2016-02-24 06:49:42 | [diff] [blame] | 46 | RateStatistics::~RateStatistics() {} |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 47 | |
| 48 | void RateStatistics::Reset() { |
| 49 | accumulated_count_ = 0; |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 50 | overflow_ = false; |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 51 | num_samples_ = 0; |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 52 | first_timestamp_ = -1; |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 53 | current_window_size_ms_ = max_window_size_ms_; |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 54 | buckets_.clear(); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 55 | } |
| 56 | |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 57 | void RateStatistics::Update(int64_t count, int64_t now_ms) { |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 58 | RTC_DCHECK_GE(count, 0); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 59 | |
| 60 | EraseOld(now_ms); |
Yun Zhang | 4774a9f | 2021-11-23 09:11:20 | [diff] [blame] | 61 | if (first_timestamp_ == -1 || num_samples_ == 0) { |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 62 | first_timestamp_ = now_ms; |
| 63 | } |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 64 | |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 65 | if (buckets_.empty() || now_ms != buckets_.back().timestamp) { |
| 66 | if (!buckets_.empty() && now_ms < buckets_.back().timestamp) { |
| 67 | RTC_LOG(LS_WARNING) << "Timestamp " << now_ms |
| 68 | << " is before the last added " |
| 69 | "timestamp in the rate window: " |
| 70 | << buckets_.back().timestamp << ", aligning to that."; |
| 71 | now_ms = buckets_.back().timestamp; |
| 72 | } |
| 73 | buckets_.emplace_back(now_ms); |
| 74 | } |
| 75 | Bucket& last_bucket = buckets_.back(); |
| 76 | last_bucket.sum += count; |
| 77 | ++last_bucket.num_samples; |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 78 | |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 79 | if (std::numeric_limits<int64_t>::max() - accumulated_count_ > count) { |
| 80 | accumulated_count_ += count; |
| 81 | } else { |
| 82 | overflow_ = true; |
| 83 | } |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 84 | ++num_samples_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 85 | } |
| 86 | |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 87 | absl::optional<int64_t> RateStatistics::Rate(int64_t now_ms) const { |
sprang | cd349d9 | 2016-07-13 16:11:28 | [diff] [blame] | 88 | // Yeah, this const_cast ain't pretty, but the alternative is to declare most |
| 89 | // of the members as mutable... |
| 90 | const_cast<RateStatistics*>(this)->EraseOld(now_ms); |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 91 | |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 92 | int active_window_size = 0; |
| 93 | if (first_timestamp_ != -1) { |
| 94 | if (first_timestamp_ <= now_ms - current_window_size_ms_) { |
| 95 | // Count window as full even if no data points currently in view, if the |
| 96 | // data stream started before the window. |
| 97 | active_window_size = current_window_size_ms_; |
| 98 | } else { |
| 99 | // Size of a single bucket is 1ms, so even if now_ms == first_timestmap_ |
| 100 | // the window size should be 1. |
| 101 | active_window_size = now_ms - first_timestamp_ + 1; |
| 102 | } |
| 103 | } |
| 104 | |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 105 | // If window is a single bucket or there is only one sample in a data set that |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 106 | // has not grown to the full window size, or if the accumulator has |
| 107 | // overflowed, treat this as rate unavailable. |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 108 | if (num_samples_ == 0 || active_window_size <= 1 || |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 109 | (num_samples_ <= 1 && |
| 110 | rtc::SafeLt(active_window_size, current_window_size_ms_)) || |
| 111 | overflow_) { |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 112 | return absl::nullopt; |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 113 | } |
| 114 | |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 115 | float scale = static_cast<float>(scale_) / active_window_size; |
Yves Gerey | a688d11 | 2019-12-31 15:16:51 | [diff] [blame] | 116 | float result = accumulated_count_ * scale + 0.5f; |
| 117 | |
| 118 | // Better return unavailable rate than garbage value (undefined behavior). |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 119 | if (result > static_cast<float>(std::numeric_limits<int64_t>::max())) { |
Yves Gerey | a688d11 | 2019-12-31 15:16:51 | [diff] [blame] | 120 | return absl::nullopt; |
| 121 | } |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 122 | return rtc::dchecked_cast<int64_t>(result); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void RateStatistics::EraseOld(int64_t now_ms) { |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 126 | // New oldest time that is included in data set. |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 127 | const int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1; |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 128 | |
| 129 | // Loop over buckets and remove too old data points. |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 130 | while (!buckets_.empty() && buckets_.front().timestamp < new_oldest_time) { |
| 131 | const Bucket& oldest_bucket = buckets_.front(); |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 132 | RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum); |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 133 | RTC_DCHECK_GE(num_samples_, oldest_bucket.num_samples); |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 134 | accumulated_count_ -= oldest_bucket.sum; |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 135 | num_samples_ -= oldest_bucket.num_samples; |
| 136 | buckets_.pop_front(); |
Harald Alvestrand | a846cef | 2020-01-15 13:02:12 | [diff] [blame] | 137 | // This does not clear overflow_ even when counter is empty. |
| 138 | // TODO(https://bugs.webrtc.org/11247): Consider if overflow_ can be reset. |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 139 | } |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 140 | } |
| 141 | |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 142 | bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) { |
| 143 | if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_) |
| 144 | return false; |
Erik Språng | 03d9e52 | 2020-05-25 10:58:03 | [diff] [blame] | 145 | if (first_timestamp_ != -1) { |
| 146 | // If the window changes (e.g. decreases - removing data point, then |
| 147 | // increases again) we need to update the first timestamp mark as |
| 148 | // otherwise it indicates the window coveres a region of zeros, suddenly |
| 149 | // under-estimating the rate. |
| 150 | first_timestamp_ = std::max(first_timestamp_, now_ms - window_size_ms + 1); |
| 151 | } |
Erik Språng | 51e6030 | 2016-06-10 20:13:21 | [diff] [blame] | 152 | current_window_size_ms_ = window_size_ms; |
| 153 | EraseOld(now_ms); |
| 154 | return true; |
| 155 | } |
| 156 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 | [diff] [blame] | 157 | } // namespace webrtc |