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