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

Side by Side Diff: remoting/base/rate_counter.cc

Issue 2911893003: Deprecate NonThreadSafe in remoting in favor of SequenceChecker. (Closed)
Patch Set: Created 3 years, 6 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 | « remoting/base/rate_counter.h ('k') | remoting/host/BUILD.gn » ('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 (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
OLDNEW
« no previous file with comments | « remoting/base/rate_counter.h ('k') | remoting/host/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698