OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "remoting/base/rate_counter.h" | 5 #include "remoting/base/rate_counter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace remoting { | 9 namespace remoting { |
10 | 10 |
11 RateCounter::RateCounter(base::TimeDelta time_window) | 11 RateCounter::RateCounter(base::TimeDelta time_window) |
12 : time_window_(time_window), sum_(0) { | 12 : time_window_(time_window), sum_(0) { |
13 DCHECK_GT(time_window, base::TimeDelta()); | 13 DCHECK_GT(time_window, base::TimeDelta()); |
14 } | 14 } |
15 | 15 |
16 RateCounter::~RateCounter() {} | 16 RateCounter::~RateCounter() { |
| 17 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 18 } |
17 | 19 |
18 void RateCounter::Record(int64_t value) { | 20 void RateCounter::Record(int64_t value) { |
19 DCHECK(CalledOnValidThread()); | 21 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
20 | 22 |
21 base::TimeTicks now = tick_clock_->NowTicks(); | 23 base::TimeTicks now = tick_clock_->NowTicks(); |
22 EvictOldDataPoints(now); | 24 EvictOldDataPoints(now); |
23 sum_ += value; | 25 sum_ += value; |
24 data_points_.push(std::make_pair(now, value)); | 26 data_points_.push(std::make_pair(now, value)); |
25 } | 27 } |
26 | 28 |
27 double RateCounter::Rate() { | 29 double RateCounter::Rate() { |
28 DCHECK(CalledOnValidThread()); | 30 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
29 | 31 |
30 EvictOldDataPoints(tick_clock_->NowTicks()); | 32 EvictOldDataPoints(tick_clock_->NowTicks()); |
31 return sum_ / time_window_.InSecondsF(); | 33 return sum_ / time_window_.InSecondsF(); |
32 } | 34 } |
33 | 35 |
34 void RateCounter::EvictOldDataPoints(base::TimeTicks now) { | 36 void RateCounter::EvictOldDataPoints(base::TimeTicks now) { |
35 // Remove data points outside of the window. | 37 // Remove data points outside of the window. |
36 base::TimeTicks window_start = now - time_window_; | 38 base::TimeTicks window_start = now - time_window_; |
37 | 39 |
38 while (!data_points_.empty()) { | 40 while (!data_points_.empty()) { |
39 if (data_points_.front().first > window_start) | 41 if (data_points_.front().first > window_start) |
40 break; | 42 break; |
41 | 43 |
42 sum_ -= data_points_.front().second; | 44 sum_ -= data_points_.front().second; |
43 data_points_.pop(); | 45 data_points_.pop(); |
44 } | 46 } |
45 } | 47 } |
46 | 48 |
47 } // namespace remoting | 49 } // namespace remoting |
OLD | NEW |