Chromium Code Reviews| 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 RateTracker::RateTracker(int bucket_milliseconds, size_t bucket_count) |
| 23 : bucket_milliseconds_(bucket_milliseconds), | 23 : bucket_milliseconds_(bucket_milliseconds), |
| 24 bucket_count_(bucket_count), | 24 bucket_count_(bucket_count), |
| 25 sample_buckets_(new size_t[bucket_count + 1]), | 25 sample_buckets_(new size_t[bucket_count + 1]), |
| 26 total_sample_count_(0u), | 26 total_sample_count_(0u), |
| 27 bucket_start_time_milliseconds_(~0u) { | 27 bucket_start_time_milliseconds_(~0) { |
|
Taylor Brandstetter
2016/04/05 01:08:14
It would be more clear if you replaced "~0" with "
pthatcher1
2016/04/11 20:56:59
Or, better yet, make this use rtc::optional.
Or i
honghaiz3
2016/04/18 23:39:03
Used a named constant kTimeUnset.
| |
| 28 RTC_CHECK(bucket_milliseconds > 0u); | 28 RTC_CHECK(bucket_milliseconds > 0); |
| 29 RTC_CHECK(bucket_count > 0u); | 29 RTC_CHECK(bucket_count > 0); |
| 30 } | 30 } |
| 31 | 31 |
| 32 RateTracker::~RateTracker() { | 32 RateTracker::~RateTracker() { |
| 33 delete[] sample_buckets_; | 33 delete[] sample_buckets_; |
| 34 } | 34 } |
| 35 | 35 |
| 36 double RateTracker::ComputeRateForInterval( | 36 double RateTracker::ComputeRateForInterval(int interval_milliseconds) const { |
| 37 uint32_t interval_milliseconds) const { | 37 if (bucket_start_time_milliseconds_ == ~0) { |
| 38 if (bucket_start_time_milliseconds_ == ~0u) { | |
| 39 return 0.0; | 38 return 0.0; |
| 40 } | 39 } |
| 41 uint32_t current_time = Time(); | 40 int64_t current_time = Time(); |
| 42 // Calculate which buckets to sum up given the current time. If the time | 41 // 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. | 42 // 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>( | 43 int available_interval_milliseconds = |
| 45 interval_milliseconds, | 44 std::min(interval_milliseconds, |
| 46 bucket_milliseconds_ * static_cast<uint32_t>(bucket_count_)); | 45 bucket_milliseconds_ * static_cast<int>(bucket_count_)); |
| 47 // number of old buckets (i.e. after the current bucket in the ring buffer) | 46 // number of old buckets (i.e. after the current bucket in the ring buffer) |
| 48 // that are expired given our current time interval. | 47 // that are expired given our current time interval. |
| 49 size_t buckets_to_skip; | 48 size_t buckets_to_skip; |
| 50 // Number of milliseconds of the first bucket that are not a portion of the | 49 // Number of milliseconds of the first bucket that are not a portion of the |
| 51 // current interval. | 50 // current interval. |
| 52 uint32_t milliseconds_to_skip; | 51 int milliseconds_to_skip; |
| 53 if (current_time > | 52 if (current_time > |
| 54 initialization_time_milliseconds_ + available_interval_milliseconds) { | 53 initialization_time_milliseconds_ + available_interval_milliseconds) { |
| 55 uint32_t time_to_skip = | 54 int time_to_skip = current_time - bucket_start_time_milliseconds_ + |
| 56 current_time - bucket_start_time_milliseconds_ + | 55 static_cast<int>(bucket_count_) * bucket_milliseconds_ - |
| 57 static_cast<uint32_t>(bucket_count_) * bucket_milliseconds_ - | 56 available_interval_milliseconds; |
|
pthatcher1
2016/04/11 20:56:59
Why not just use uint64_t for all of these?
honghaiz3
2016/04/18 23:39:03
Done.
| |
| 58 available_interval_milliseconds; | |
| 59 buckets_to_skip = time_to_skip / bucket_milliseconds_; | 57 buckets_to_skip = time_to_skip / bucket_milliseconds_; |
| 60 milliseconds_to_skip = time_to_skip % bucket_milliseconds_; | 58 milliseconds_to_skip = time_to_skip % bucket_milliseconds_; |
| 61 } else { | 59 } else { |
| 62 buckets_to_skip = bucket_count_ - current_bucket_; | 60 buckets_to_skip = bucket_count_ - current_bucket_; |
| 63 milliseconds_to_skip = 0u; | 61 milliseconds_to_skip = 0; |
| 64 available_interval_milliseconds = | 62 available_interval_milliseconds = |
| 65 TimeDiff(current_time, initialization_time_milliseconds_); | 63 TimeDiff(current_time, initialization_time_milliseconds_); |
| 66 // Let one bucket interval pass after initialization before reporting. | 64 // Let one bucket interval pass after initialization before reporting. |
| 67 if (available_interval_milliseconds < bucket_milliseconds_) { | 65 if (available_interval_milliseconds < bucket_milliseconds_) { |
| 68 return 0.0; | 66 return 0.0; |
| 69 } | 67 } |
| 70 } | 68 } |
| 71 // If we're skipping all buckets that means that there have been no samples | 69 // If we're skipping all buckets that means that there have been no samples |
| 72 // within the sampling interval so report 0. | 70 // within the sampling interval so report 0. |
| 73 if (buckets_to_skip > bucket_count_ || | 71 if (buckets_to_skip > bucket_count_ || available_interval_milliseconds == 0) { |
| 74 available_interval_milliseconds == 0u) { | |
| 75 return 0.0; | 72 return 0.0; |
| 76 } | 73 } |
| 77 size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip); | 74 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 | 75 // Only count a portion of the first bucket according to how much of the |
| 79 // first bucket is within the current interval. | 76 // first bucket is within the current interval. |
| 80 size_t total_samples = ((sample_buckets_[start_bucket] * | 77 size_t total_samples = ((sample_buckets_[start_bucket] * |
| 81 (bucket_milliseconds_ - milliseconds_to_skip)) + | 78 (bucket_milliseconds_ - milliseconds_to_skip)) + |
| 82 (bucket_milliseconds_ >> 1)) / | 79 (bucket_milliseconds_ >> 1)) / |
| 83 bucket_milliseconds_; | 80 bucket_milliseconds_; |
| 84 // All other buckets in the interval are counted in their entirety. | 81 // All other buckets in the interval are counted in their entirety. |
| 85 for (size_t i = NextBucketIndex(start_bucket); | 82 for (size_t i = NextBucketIndex(start_bucket); |
| 86 i != NextBucketIndex(current_bucket_); | 83 i != NextBucketIndex(current_bucket_); |
| 87 i = NextBucketIndex(i)) { | 84 i = NextBucketIndex(i)) { |
| 88 total_samples += sample_buckets_[i]; | 85 total_samples += sample_buckets_[i]; |
| 89 } | 86 } |
| 90 // Convert to samples per second. | 87 // Convert to samples per second. |
| 91 return static_cast<double>(total_samples * 1000u) / | 88 return static_cast<double>(total_samples * 1000) / |
| 92 static_cast<double>(available_interval_milliseconds); | 89 static_cast<double>(available_interval_milliseconds); |
| 93 } | 90 } |
| 94 | 91 |
| 95 double RateTracker::ComputeTotalRate() const { | 92 double RateTracker::ComputeTotalRate() const { |
| 96 if (bucket_start_time_milliseconds_ == ~0u) { | 93 if (bucket_start_time_milliseconds_ == ~0) { |
| 97 return 0.0; | 94 return 0.0; |
| 98 } | 95 } |
| 99 uint32_t current_time = Time(); | 96 int64_t current_time = Time(); |
| 100 if (TimeIsLaterOrEqual(current_time, initialization_time_milliseconds_)) { | 97 if (current_time <= initialization_time_milliseconds_) { |
| 101 return 0.0; | 98 return 0.0; |
| 102 } | 99 } |
| 103 return static_cast<double>(total_sample_count_ * 1000u) / | 100 return static_cast<double>(total_sample_count_ * 1000) / |
| 104 static_cast<double>( | 101 static_cast<double>( |
| 105 TimeDiff(current_time, initialization_time_milliseconds_)); | 102 TimeDiff(current_time, initialization_time_milliseconds_)); |
| 106 } | 103 } |
| 107 | 104 |
| 108 size_t RateTracker::TotalSampleCount() const { | 105 size_t RateTracker::TotalSampleCount() const { |
| 109 return total_sample_count_; | 106 return total_sample_count_; |
| 110 } | 107 } |
| 111 | 108 |
| 112 void RateTracker::AddSamples(size_t sample_count) { | 109 void RateTracker::AddSamples(size_t sample_count) { |
| 113 EnsureInitialized(); | 110 EnsureInitialized(); |
| 114 uint32_t current_time = Time(); | 111 int64_t current_time = Time(); |
| 115 // Advance the current bucket as needed for the current time, and reset | 112 // Advance the current bucket as needed for the current time, and reset |
| 116 // bucket counts as we advance. | 113 // bucket counts as we advance. |
| 117 for (size_t i = 0u; i <= bucket_count_ && | 114 for (size_t i = 0; |
|
Taylor Brandstetter
2016/04/05 01:08:14
Why was this changed from 0u? size_t is unsigned.
honghaiz3
2016/04/18 23:39:03
I do not think the "u" here is necessary. I would
| |
| 118 current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; | 115 i <= bucket_count_ && |
| 119 ++i) { | 116 current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; |
| 117 ++i) { | |
| 120 bucket_start_time_milliseconds_ += bucket_milliseconds_; | 118 bucket_start_time_milliseconds_ += bucket_milliseconds_; |
| 121 current_bucket_ = NextBucketIndex(current_bucket_); | 119 current_bucket_ = NextBucketIndex(current_bucket_); |
| 122 sample_buckets_[current_bucket_] = 0u; | 120 sample_buckets_[current_bucket_] = 0; |
| 123 } | 121 } |
| 124 // Ensure that bucket_start_time_milliseconds_ is updated appropriately if | 122 // Ensure that bucket_start_time_milliseconds_ is updated appropriately if |
| 125 // the entire buffer of samples has been expired. | 123 // the entire buffer of samples has been expired. |
| 126 bucket_start_time_milliseconds_ += bucket_milliseconds_ * | 124 bucket_start_time_milliseconds_ += bucket_milliseconds_ * |
| 127 ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_); | 125 ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_); |
| 128 // Add all samples in the bucket that includes the current time. | 126 // Add all samples in the bucket that includes the current time. |
| 129 sample_buckets_[current_bucket_] += sample_count; | 127 sample_buckets_[current_bucket_] += sample_count; |
| 130 total_sample_count_ += sample_count; | 128 total_sample_count_ += sample_count; |
| 131 } | 129 } |
| 132 | 130 |
| 133 uint32_t RateTracker::Time() const { | 131 int64_t RateTracker::Time() const { |
| 134 return rtc::Time(); | 132 return rtc::Time(); |
| 135 } | 133 } |
| 136 | 134 |
| 137 void RateTracker::EnsureInitialized() { | 135 void RateTracker::EnsureInitialized() { |
| 138 if (bucket_start_time_milliseconds_ == ~0u) { | 136 if (bucket_start_time_milliseconds_ == ~0) { |
| 139 initialization_time_milliseconds_ = Time(); | 137 initialization_time_milliseconds_ = Time(); |
| 140 bucket_start_time_milliseconds_ = initialization_time_milliseconds_; | 138 bucket_start_time_milliseconds_ = initialization_time_milliseconds_; |
| 141 current_bucket_ = 0u; | 139 current_bucket_ = 0; |
| 142 // We only need to initialize the first bucket because we reset buckets when | 140 // We only need to initialize the first bucket because we reset buckets when |
| 143 // current_bucket_ increments. | 141 // current_bucket_ increments. |
| 144 sample_buckets_[current_bucket_] = 0u; | 142 sample_buckets_[current_bucket_] = 0; |
| 145 } | 143 } |
| 146 } | 144 } |
| 147 | 145 |
| 148 size_t RateTracker::NextBucketIndex(size_t bucket_index) const { | 146 size_t RateTracker::NextBucketIndex(size_t bucket_index) const { |
| 149 return (bucket_index + 1u) % (bucket_count_ + 1u); | 147 return (bucket_index + 1u) % (bucket_count_ + 1u); |
| 150 } | 148 } |
| 151 | 149 |
| 152 } // namespace rtc | 150 } // namespace rtc |
| OLD | NEW |