OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 |
| 11 #include "webrtc/base/bucketratetracker.h" |
| 12 |
| 13 #include <stddef.h> |
| 14 #include <assert.h> |
| 15 |
| 16 #include <algorithm> |
| 17 |
| 18 #include "webrtc/base/timeutils.h" |
| 19 |
| 20 namespace rtc { |
| 21 |
| 22 BucketRateTracker::BucketRateTracker( |
| 23 uint32 bucket_milliseconds, size_t bucket_count) |
| 24 : bucket_milliseconds_(bucket_milliseconds), |
| 25 bucket_count_(bucket_count), |
| 26 sample_buckets_(new size_t[bucket_count + 1]), |
| 27 bucket_start_time_(~0u) { |
| 28 assert(bucket_milliseconds > 0u); |
| 29 assert(bucket_count > 0u); |
| 30 } |
| 31 |
| 32 BucketRateTracker::~BucketRateTracker() { |
| 33 delete[] sample_buckets_; |
| 34 } |
| 35 |
| 36 double BucketRateTracker::ComputeCurrentRate( |
| 37 uint32 interval_milliseconds) const { |
| 38 if (bucket_start_time_ == ~0u) { |
| 39 return 0.0; |
| 40 } |
| 41 uint32 current_time = Time(); |
| 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. |
| 44 uint32 available_interval_milliseconds = std::min<uint32>( |
| 45 interval_milliseconds, |
| 46 bucket_milliseconds_ * static_cast<uint32>(bucket_count_)); |
| 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. |
| 52 uint32 milliseconds_to_skip; |
| 53 if (current_time > initialization_time_ + available_interval_milliseconds) { |
| 54 uint32 time_to_skip = current_time - bucket_start_time_ + |
| 55 static_cast<uint32>(bucket_count_) * bucket_milliseconds_ - |
| 56 available_interval_milliseconds; |
| 57 buckets_to_skip = time_to_skip / bucket_milliseconds_; |
| 58 milliseconds_to_skip = time_to_skip % bucket_milliseconds_; |
| 59 } else { |
| 60 buckets_to_skip = bucket_count_ - current_bucket_; |
| 61 milliseconds_to_skip = 0u; |
| 62 available_interval_milliseconds = current_time - initialization_time_; |
| 63 } |
| 64 // If we're skipping all buckets that means that there have been no samples |
| 65 // within the sampling interval so report 0. |
| 66 if (buckets_to_skip > bucket_count_ || |
| 67 available_interval_milliseconds == 0u) { |
| 68 return 0.0; |
| 69 } |
| 70 size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip); |
| 71 // Only count a portion of the first bucket according to how much of the |
| 72 // first bucket is within the current interval. |
| 73 size_t total_samples = sample_buckets_[start_bucket] * |
| 74 (bucket_milliseconds_ - milliseconds_to_skip) / |
| 75 bucket_milliseconds_; |
| 76 // All other buckets in the interval are counted in their entirety. |
| 77 for (size_t i = NextBucketIndex(start_bucket); |
| 78 i != NextBucketIndex(current_bucket_); |
| 79 i = NextBucketIndex(i)) { |
| 80 total_samples += sample_buckets_[i]; |
| 81 } |
| 82 // Convert to samples per second. |
| 83 return static_cast<double>(total_samples * 1000u) / |
| 84 static_cast<double>(available_interval_milliseconds); |
| 85 } |
| 86 |
| 87 void BucketRateTracker::AddSamples(size_t sample_count) { |
| 88 EnsureInitialized(); |
| 89 uint32 current_time = Time(); |
| 90 // Advance the current bucket as needed for the current time, and reset |
| 91 // bucket counts as we advance. |
| 92 for (size_t i = 0u; i <= bucket_count_ && |
| 93 current_time >= bucket_start_time_ + bucket_milliseconds_; ++i) { |
| 94 bucket_start_time_ += bucket_milliseconds_; |
| 95 current_bucket_ = NextBucketIndex(current_bucket_); |
| 96 sample_buckets_[current_bucket_] = 0u; |
| 97 } |
| 98 // Ensure that bucket_start_time_ is updated appropriately if we've expired |
| 99 // the entire buffer of samples. |
| 100 bucket_start_time_ += bucket_milliseconds_ * |
| 101 ((current_time - bucket_start_time_) / bucket_milliseconds_); |
| 102 // Add all samples in the bucket that includes the current time. |
| 103 sample_buckets_[current_bucket_] += sample_count; |
| 104 } |
| 105 |
| 106 uint32 BucketRateTracker::Time() const { |
| 107 return rtc::Time(); |
| 108 } |
| 109 |
| 110 void BucketRateTracker::EnsureInitialized() { |
| 111 if (bucket_start_time_ == ~0u) { |
| 112 initialization_time_ = Time(); |
| 113 bucket_start_time_ = initialization_time_; |
| 114 current_bucket_ = 0u; |
| 115 // We only need to initialize the first bucket because we reset buckets when |
| 116 // current_bucket_ increments. |
| 117 sample_buckets_[current_bucket_] = 0u; |
| 118 } |
| 119 } |
| 120 |
| 121 size_t BucketRateTracker::NextBucketIndex(size_t bucket_index) const { |
| 122 return (bucket_index + 1u) % (bucket_count_ + 1u); |
| 123 } |
| 124 |
| 125 } // namespace rtc |
OLD | NEW |