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