OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/download/rate_estimator.h" | 5 #include "content/browser/download/rate_estimator.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 using base::TimeDelta; | 9 using base::TimeDelta; |
10 using base::TimeTicks; | 10 using base::TimeTicks; |
(...skipping 22 matching lines...) Expand all Loading... |
33 bucket_time_(bucket_time), | 33 bucket_time_(bucket_time), |
34 oldest_index_(0), | 34 oldest_index_(0), |
35 bucket_count_(1) { | 35 bucket_count_(1) { |
36 DCHECK(bucket_time_.InSeconds() > 0); | 36 DCHECK(bucket_time_.InSeconds() > 0); |
37 ResetBuckets(now); | 37 ResetBuckets(now); |
38 } | 38 } |
39 | 39 |
40 RateEstimator::~RateEstimator() { | 40 RateEstimator::~RateEstimator() { |
41 } | 41 } |
42 | 42 |
43 void RateEstimator::Increment(uint32 count) { | 43 void RateEstimator::Increment(uint32_t count) { |
44 Increment(count, TimeTicks::Now()); | 44 Increment(count, TimeTicks::Now()); |
45 } | 45 } |
46 | 46 |
47 void RateEstimator::Increment(uint32 count, TimeTicks now) { | 47 void RateEstimator::Increment(uint32_t count, TimeTicks now) { |
48 ClearOldBuckets(now); | 48 ClearOldBuckets(now); |
49 int64 seconds_since_oldest = (now - oldest_time_).InSeconds(); | 49 int64_t seconds_since_oldest = (now - oldest_time_).InSeconds(); |
50 DCHECK(seconds_since_oldest >= 0); | 50 DCHECK(seconds_since_oldest >= 0); |
51 int64 delta_buckets = seconds_since_oldest / bucket_time_.InSeconds(); | 51 int64_t delta_buckets = seconds_since_oldest / bucket_time_.InSeconds(); |
52 DCHECK(delta_buckets >= 0); | 52 DCHECK(delta_buckets >= 0); |
53 size_t index_offset = static_cast<size_t>(delta_buckets); | 53 size_t index_offset = static_cast<size_t>(delta_buckets); |
54 DCHECK(index_offset <= history_.size()); | 54 DCHECK(index_offset <= history_.size()); |
55 int current_index = (oldest_index_ + delta_buckets) % history_.size(); | 55 int current_index = (oldest_index_ + delta_buckets) % history_.size(); |
56 history_[current_index] += count; | 56 history_[current_index] += count; |
57 } | 57 } |
58 | 58 |
59 uint64 RateEstimator::GetCountPerSecond() const { | 59 uint64_t RateEstimator::GetCountPerSecond() const { |
60 return GetCountPerSecond(TimeTicks::Now()); | 60 return GetCountPerSecond(TimeTicks::Now()); |
61 } | 61 } |
62 | 62 |
63 uint64 RateEstimator::GetCountPerSecond(TimeTicks now) const { | 63 uint64_t RateEstimator::GetCountPerSecond(TimeTicks now) const { |
64 const_cast<RateEstimator*>(this)->ClearOldBuckets(now); | 64 const_cast<RateEstimator*>(this)->ClearOldBuckets(now); |
65 // TODO(cbentzel): Support fractional seconds for active bucket? | 65 // TODO(cbentzel): Support fractional seconds for active bucket? |
66 // We explicitly don't check for overflow here. If it happens, unsigned | 66 // We explicitly don't check for overflow here. If it happens, unsigned |
67 // arithmetic at least guarantees behavior by wrapping around. The estimate | 67 // arithmetic at least guarantees behavior by wrapping around. The estimate |
68 // will be off, but the code will still be valid. | 68 // will be off, but the code will still be valid. |
69 uint64 total_count = 0; | 69 uint64_t total_count = 0; |
70 for (size_t i = 0; i < bucket_count_; ++i) { | 70 for (size_t i = 0; i < bucket_count_; ++i) { |
71 size_t index = (oldest_index_ + i) % history_.size(); | 71 size_t index = (oldest_index_ + i) % history_.size(); |
72 total_count += history_[index]; | 72 total_count += history_[index]; |
73 } | 73 } |
74 return total_count / (bucket_count_ * bucket_time_.InSeconds()); | 74 return total_count / (bucket_count_ * bucket_time_.InSeconds()); |
75 } | 75 } |
76 | 76 |
77 void RateEstimator::ClearOldBuckets(TimeTicks now) { | 77 void RateEstimator::ClearOldBuckets(TimeTicks now) { |
78 int64 seconds_since_oldest = (now - oldest_time_).InSeconds(); | 78 int64_t seconds_since_oldest = (now - oldest_time_).InSeconds(); |
79 | 79 |
80 int64 delta_buckets = seconds_since_oldest / bucket_time_.InSeconds(); | 80 int64_t delta_buckets = seconds_since_oldest / bucket_time_.InSeconds(); |
81 | 81 |
82 // It's possible (although unlikely) for there to be rollover with TimeTicks. | 82 // It's possible (although unlikely) for there to be rollover with TimeTicks. |
83 // If that's the case, just reset the history. | 83 // If that's the case, just reset the history. |
84 if (delta_buckets < 0) { | 84 if (delta_buckets < 0) { |
85 ResetBuckets(now); | 85 ResetBuckets(now); |
86 return; | 86 return; |
87 } | 87 } |
88 size_t delta_index = static_cast<size_t>(delta_buckets); | 88 size_t delta_index = static_cast<size_t>(delta_buckets); |
89 | 89 |
90 // If we are within the current window, keep the existing data. | 90 // If we are within the current window, keep the existing data. |
(...skipping 22 matching lines...) Expand all Loading... |
113 void RateEstimator::ResetBuckets(TimeTicks now) { | 113 void RateEstimator::ResetBuckets(TimeTicks now) { |
114 for (size_t i = 0; i < history_.size(); ++i) { | 114 for (size_t i = 0; i < history_.size(); ++i) { |
115 history_[i] = 0; | 115 history_[i] = 0; |
116 } | 116 } |
117 oldest_index_ = 0; | 117 oldest_index_ = 0; |
118 bucket_count_ = 1; | 118 bucket_count_ = 1; |
119 oldest_time_ = now; | 119 oldest_time_ = now; |
120 } | 120 } |
121 | 121 |
122 } // namespace content | 122 } // namespace content |
OLD | NEW |