Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: content/browser/download/rate_estimator.cc

Issue 1549113002: Switch to standard integer types in content/browser/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/download/rate_estimator.h ('k') | content/browser/download/save_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « content/browser/download/rate_estimator.h ('k') | content/browser/download/save_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698