henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/rate_tracker.h" |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 12 | |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 20 | static const int64_t kTimeUnset = -1; |
| 21 | |
| 22 | RateTracker::RateTracker(int64_t bucket_milliseconds, size_t bucket_count) |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 23 | : bucket_milliseconds_(bucket_milliseconds), |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 24 | bucket_count_(bucket_count), |
| 25 | sample_buckets_(new size_t[bucket_count + 1]), |
| 26 | total_sample_count_(0u), |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 27 | bucket_start_time_milliseconds_(kTimeUnset) { |
| 28 | RTC_CHECK(bucket_milliseconds > 0); |
| 29 | RTC_CHECK(bucket_count > 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 30 | } |
| 31 | |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 32 | RateTracker::~RateTracker() { |
| 33 | delete[] sample_buckets_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 34 | } |
| 35 | |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 36 | double RateTracker::ComputeRateForInterval( |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 37 | int64_t interval_milliseconds) const { |
| 38 | if (bucket_start_time_milliseconds_ == kTimeUnset) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 39 | return 0.0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 40 | } |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 41 | int64_t current_time = Time(); |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 42 | // Calculate which buckets to sum up given the current time. If the time |
| 43 | // has passed to a new bucket then we have to skip some of the oldest buckets. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 44 | int64_t available_interval_milliseconds = |
| 45 | std::min(interval_milliseconds, |
| 46 | bucket_milliseconds_ * static_cast<int64_t>(bucket_count_)); |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 47 | // number of old buckets (i.e. after the current bucket in the ring buffer) |
| 48 | // that are expired given our current time interval. |
| 49 | size_t buckets_to_skip; |
| 50 | // Number of milliseconds of the first bucket that are not a portion of the |
| 51 | // current interval. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 52 | int64_t milliseconds_to_skip; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 53 | if (current_time > |
| 54 | initialization_time_milliseconds_ + available_interval_milliseconds) { |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 55 | int64_t time_to_skip = |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 56 | current_time - bucket_start_time_milliseconds_ + |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 57 | static_cast<int64_t>(bucket_count_) * bucket_milliseconds_ - |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 58 | available_interval_milliseconds; |
| 59 | buckets_to_skip = time_to_skip / bucket_milliseconds_; |
| 60 | milliseconds_to_skip = time_to_skip % bucket_milliseconds_; |
| 61 | } else { |
| 62 | buckets_to_skip = bucket_count_ - current_bucket_; |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 63 | milliseconds_to_skip = 0; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 64 | available_interval_milliseconds = |
| 65 | TimeDiff(current_time, initialization_time_milliseconds_); |
asapersson | 799379e | 2016-02-02 09:46:53 | [diff] [blame] | 66 | // Let one bucket interval pass after initialization before reporting. |
| 67 | if (available_interval_milliseconds < bucket_milliseconds_) { |
| 68 | return 0.0; |
| 69 | } |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 70 | } |
| 71 | // If we're skipping all buckets that means that there have been no samples |
| 72 | // within the sampling interval so report 0. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 73 | if (buckets_to_skip > bucket_count_ || available_interval_milliseconds == 0) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 74 | return 0.0; |
| 75 | } |
| 76 | size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip); |
| 77 | // Only count a portion of the first bucket according to how much of the |
| 78 | // first bucket is within the current interval. |
Tim Psiaki | ad13d2f | 2015-11-11 00:34:50 | [diff] [blame] | 79 | size_t total_samples = ((sample_buckets_[start_bucket] * |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 80 | (bucket_milliseconds_ - milliseconds_to_skip)) + |
| 81 | (bucket_milliseconds_ >> 1)) / |
| 82 | bucket_milliseconds_; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 83 | // All other buckets in the interval are counted in their entirety. |
| 84 | for (size_t i = NextBucketIndex(start_bucket); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 85 | i != NextBucketIndex(current_bucket_); i = NextBucketIndex(i)) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 86 | total_samples += sample_buckets_[i]; |
| 87 | } |
| 88 | // Convert to samples per second. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 89 | return static_cast<double>(total_samples * 1000) / |
| 90 | static_cast<double>(available_interval_milliseconds); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 91 | } |
| 92 | |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 93 | double RateTracker::ComputeTotalRate() const { |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 94 | if (bucket_start_time_milliseconds_ == kTimeUnset) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 95 | return 0.0; |
| 96 | } |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 97 | int64_t current_time = Time(); |
| 98 | if (current_time <= initialization_time_milliseconds_) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 99 | return 0.0; |
| 100 | } |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 101 | return static_cast<double>(total_sample_count_ * 1000) / |
| 102 | static_cast<double>( |
| 103 | TimeDiff(current_time, initialization_time_milliseconds_)); |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | size_t RateTracker::TotalSampleCount() const { |
| 107 | return total_sample_count_; |
| 108 | } |
| 109 | |
| 110 | void RateTracker::AddSamples(size_t sample_count) { |
| 111 | EnsureInitialized(); |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 112 | int64_t current_time = Time(); |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 113 | // Advance the current bucket as needed for the current time, and reset |
| 114 | // bucket counts as we advance. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 115 | for (size_t i = 0; |
| 116 | i <= bucket_count_ && |
| 117 | current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; |
| 118 | ++i) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 119 | bucket_start_time_milliseconds_ += bucket_milliseconds_; |
| 120 | current_bucket_ = NextBucketIndex(current_bucket_); |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 121 | sample_buckets_[current_bucket_] = 0; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 122 | } |
| 123 | // Ensure that bucket_start_time_milliseconds_ is updated appropriately if |
| 124 | // the entire buffer of samples has been expired. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 125 | bucket_start_time_milliseconds_ += |
| 126 | bucket_milliseconds_ * |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 127 | ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_); |
| 128 | // Add all samples in the bucket that includes the current time. |
| 129 | sample_buckets_[current_bucket_] += sample_count; |
| 130 | total_sample_count_ += sample_count; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 131 | } |
| 132 | |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 133 | int64_t RateTracker::Time() const { |
| 134 | return rtc::TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 135 | } |
| 136 | |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 137 | void RateTracker::EnsureInitialized() { |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 138 | if (bucket_start_time_milliseconds_ == kTimeUnset) { |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 139 | initialization_time_milliseconds_ = Time(); |
| 140 | bucket_start_time_milliseconds_ = initialization_time_milliseconds_; |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 141 | current_bucket_ = 0; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 142 | // We only need to initialize the first bucket because we reset buckets when |
| 143 | // current_bucket_ increments. |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 144 | sample_buckets_[current_bucket_] = 0; |
Tim Psiaki | 6304626 | 2015-09-14 17:38:08 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | size_t RateTracker::NextBucketIndex(size_t bucket_index) const { |
| 149 | return (bucket_index + 1u) % (bucket_count_ + 1u); |
| 150 | } |
| 151 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 152 | } // namespace rtc |