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

Side by Side Diff: cc/scheduler/delay_based_time_source.cc

Issue 18589002: cc: Fix and simplify DelayBasedTimeSource (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix tests; Tick now if next tick time can be now, rather than skipping; Created 7 years, 5 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
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 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 "cc/scheduler/delay_based_time_source.h" 5 #include "cc/scheduler/delay_based_time_source.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 13
14 namespace cc { 14 namespace cc {
15 15
16 namespace { 16 namespace {
17 17
18 // kDoubleTickThreshold prevents ticks from running within the specified 18 // kDoubleTickDivisor prevents ticks from running within the specified
19 // fraction of an interval. This helps account for jitter in the timebase as 19 // fraction of an interval. This helps account for jitter in the timebase as
20 // well as quick timer reactivation. 20 // well as quick timer reactivation.
21 static const double kDoubleTickThreshold = 0.25; 21 static const int kDoubleTickDivisor = 4;
22 22
23 // kIntervalChangeThreshold is the fraction of the interval that will trigger an 23 // kIntervalChangeThreshold is the fraction of the interval that will trigger an
24 // immediate interval change. kPhaseChangeThreshold is the fraction of the 24 // immediate interval change. kPhaseChangeThreshold is the fraction of the
25 // interval that will trigger an immediate phase change. If the changes are 25 // interval that will trigger an immediate phase change. If the changes are
26 // within the thresholds, the change will take place on the next tick. If 26 // within the thresholds, the change will take place on the next tick. If
27 // either change is outside the thresholds, the next tick will be canceled and 27 // either change is outside the thresholds, the next tick will be canceled and
28 // reissued immediately. 28 // reissued immediately.
29 static const double kIntervalChangeThreshold = 0.25; 29 static const double kIntervalChangeThreshold = 0.25;
30 static const double kPhaseChangeThreshold = 0.25; 30 static const double kPhaseChangeThreshold = 0.25;
31 31
32 } // namespace 32 } // namespace
33 33
34 scoped_refptr<DelayBasedTimeSource> DelayBasedTimeSource::Create( 34 scoped_refptr<DelayBasedTimeSource> DelayBasedTimeSource::Create(
35 base::TimeDelta interval, 35 base::TimeDelta interval,
36 base::SingleThreadTaskRunner* task_runner) { 36 base::SingleThreadTaskRunner* task_runner) {
37 return make_scoped_refptr(new DelayBasedTimeSource(interval, task_runner)); 37 return make_scoped_refptr(new DelayBasedTimeSource(interval, task_runner));
38 } 38 }
39 39
40 DelayBasedTimeSource::DelayBasedTimeSource( 40 DelayBasedTimeSource::DelayBasedTimeSource(
41 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner) 41 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner)
42 : client_(NULL), 42 : client_(NULL),
43 has_tick_target_(false), 43 last_tick_time_(base::TimeTicks() - interval),
44 current_parameters_(interval, base::TimeTicks()), 44 current_parameters_(interval, base::TimeTicks()),
45 next_parameters_(interval, base::TimeTicks()), 45 next_parameters_(interval, base::TimeTicks()),
46 state_(STATE_INACTIVE), 46 active_(false),
47 task_runner_(task_runner), 47 task_runner_(task_runner),
48 weak_factory_(this) {} 48 weak_factory_(this) {}
49 49
50 DelayBasedTimeSource::~DelayBasedTimeSource() {} 50 DelayBasedTimeSource::~DelayBasedTimeSource() {}
51 51
52 void DelayBasedTimeSource::SetActive(bool active) { 52 void DelayBasedTimeSource::SetActive(bool active) {
53 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active); 53 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active);
54 if (!active) { 54 if (active == active_)
55 state_ = STATE_INACTIVE; 55 return;
56 active_ = active;
57
58 if (!active_) {
56 weak_factory_.InvalidateWeakPtrs(); 59 weak_factory_.InvalidateWeakPtrs();
57 return; 60 return;
58 } 61 }
59 62
60 if (state_ == STATE_STARTING || state_ == STATE_ACTIVE)
61 return;
62
63 if (!has_tick_target_) {
64 // Becoming active the first time is deferred: we post a 0-delay task.
65 // When it runs, we use that to establish the timebase, become truly
66 // active, and fire the first tick.
67 state_ = STATE_STARTING;
68 task_runner_->PostTask(FROM_HERE,
69 base::Bind(&DelayBasedTimeSource::OnTimerFired,
70 weak_factory_.GetWeakPtr()));
71 return;
72 }
73
74 state_ = STATE_ACTIVE;
75
76 PostNextTickTask(Now()); 63 PostNextTickTask(Now());
77 } 64 }
78 65
79 bool DelayBasedTimeSource::Active() const { return state_ != STATE_INACTIVE; } 66 bool DelayBasedTimeSource::Active() const { return active_; }
80 67
81 base::TimeTicks DelayBasedTimeSource::LastTickTime() { return last_tick_time_; } 68 base::TimeTicks DelayBasedTimeSource::LastTickTime() { return last_tick_time_; }
82 69
83 base::TimeTicks DelayBasedTimeSource::NextTickTime() { 70 base::TimeTicks DelayBasedTimeSource::NextTickTime() {
84 return Active() ? current_parameters_.tick_target : base::TimeTicks(); 71 return Active() ? current_parameters_.tick_target : base::TimeTicks();
85 } 72 }
86 73
87 void DelayBasedTimeSource::OnTimerFired() { 74 void DelayBasedTimeSource::OnTimerFired() {
88 DCHECK(state_ != STATE_INACTIVE); 75 DCHECK(active_);
89 76
90 base::TimeTicks now = this->Now(); 77 last_tick_time_ = current_parameters_.tick_target;
91 last_tick_time_ = now;
92 78
93 if (state_ == STATE_STARTING) { 79 PostNextTickTask(Now());
94 SetTimebaseAndInterval(now, current_parameters_.interval);
95 state_ = STATE_ACTIVE;
96 }
97
98 PostNextTickTask(now);
99 80
100 // Fire the tick. 81 // Fire the tick.
101 if (client_) 82 if (client_)
102 client_->OnTimerTick(); 83 client_->OnTimerTick();
103 } 84 }
104 85
105 void DelayBasedTimeSource::SetClient(TimeSourceClient* client) { 86 void DelayBasedTimeSource::SetClient(TimeSourceClient* client) {
106 client_ = client; 87 client_ = client;
107 } 88 }
108 89
109 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase, 90 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase,
110 base::TimeDelta interval) { 91 base::TimeDelta interval) {
111 next_parameters_.interval = interval; 92 next_parameters_.interval = interval;
112 next_parameters_.tick_target = timebase; 93 next_parameters_.tick_target = timebase;
113 has_tick_target_ = true;
114 94
115 if (state_ != STATE_ACTIVE) { 95 if (!active_) {
116 // If we aren't active, there's no need to reset the timer. 96 // If we aren't active, there's no need to reset the timer.
117 return; 97 return;
118 } 98 }
119 99
120 // If the change in interval is larger than the change threshold, 100 // If the change in interval is larger than the change threshold,
121 // request an immediate reset. 101 // request an immediate reset.
122 double interval_delta = 102 double interval_delta =
123 std::abs((interval - current_parameters_.interval).InSecondsF()); 103 std::abs((interval - current_parameters_.interval).InSecondsF());
124 double interval_change = interval_delta / interval.InSecondsF(); 104 double interval_change = interval_delta / interval.InSecondsF();
125 if (interval_change > kIntervalChangeThreshold) { 105 if (interval_change > kIntervalChangeThreshold) {
106 TRACE_EVENT0("cc", "DelayBasedTimeSource::IntervalChanged");
nduca 2013/07/03 05:37:25 I'd suggest using an instant event with thread sco
126 SetActive(false); 107 SetActive(false);
127 SetActive(true); 108 SetActive(true);
128 return; 109 return;
129 } 110 }
130 111
131 // If the change in phase is greater than the change threshold in either 112 // If the change in phase is greater than the change threshold in either
132 // direction, request an immediate reset. This logic might result in a false 113 // direction, request an immediate reset. This logic might result in a false
133 // negative if there is a simultaneous small change in the interval and the 114 // negative if there is a simultaneous small change in the interval and the
134 // fmod just happens to return something near zero. Assuming the timebase 115 // fmod just happens to return something near zero. Assuming the timebase
135 // is very recent though, which it should be, we'll still be ok because the 116 // is very recent though, which it should be, we'll still be ok because the
136 // old clock and new clock just happen to line up. 117 // old clock and new clock just happen to line up.
137 double target_delta = 118 double target_delta =
138 std::abs((timebase - current_parameters_.tick_target).InSecondsF()); 119 std::abs((timebase - current_parameters_.tick_target).InSecondsF());
139 double phase_change = 120 double phase_change =
140 fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF(); 121 fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF();
141 if (phase_change > kPhaseChangeThreshold && 122 if (phase_change > kPhaseChangeThreshold &&
142 phase_change < (1.0 - kPhaseChangeThreshold)) { 123 phase_change < (1.0 - kPhaseChangeThreshold)) {
124 TRACE_EVENT0("cc", "DelayBasedTimeSource::PhaseChanged");
143 SetActive(false); 125 SetActive(false);
144 SetActive(true); 126 SetActive(true);
145 return; 127 return;
146 } 128 }
147 } 129 }
148 130
149 base::TimeTicks DelayBasedTimeSource::Now() const { 131 base::TimeTicks DelayBasedTimeSource::Now() const {
150 return base::TimeTicks::Now(); 132 return base::TimeTicks::Now();
151 } 133 }
152 134
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 // now=18 tick_target=16.667 new_target=33.333 --> 180 // now=18 tick_target=16.667 new_target=33.333 -->
199 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15) 181 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15)
200 // This brings us back to 18+15 = 33, which was where we would have been if the 182 // This brings us back to 18+15 = 33, which was where we would have been if the
201 // task hadn't been late. 183 // task hadn't been late.
202 // 184 //
203 // For the really late delay, we we move to the next logical tick. The timebase 185 // For the really late delay, we we move to the next logical tick. The timebase
204 // is not reset. 186 // is not reset.
205 // now=37 tick_target=16.667 new_target=50.000 --> 187 // now=37 tick_target=16.667 new_target=50.000 -->
206 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13) 188 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13)
207 base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) { 189 base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) {
190 const base::TimeDelta epsilon(base::TimeDelta::FromMicroseconds(1));
208 base::TimeDelta new_interval = next_parameters_.interval; 191 base::TimeDelta new_interval = next_parameters_.interval;
209 int intervals_elapsed = 192 int intervals_elapsed =
210 static_cast<int>(floor((now - next_parameters_.tick_target).InSecondsF() / 193 (now - next_parameters_.tick_target + new_interval - epsilon) /
211 new_interval.InSecondsF())); 194 new_interval;
212 base::TimeTicks last_effective_tick = 195 base::TimeTicks new_tick_target =
213 next_parameters_.tick_target + new_interval * intervals_elapsed; 196 next_parameters_.tick_target + new_interval * intervals_elapsed;
214 base::TimeTicks new_tick_target = last_effective_tick + new_interval; 197 DCHECK(now <= new_tick_target)
215 DCHECK(now < new_tick_target)
216 << "now = " << now.ToInternalValue() 198 << "now = " << now.ToInternalValue()
217 << "; new_tick_target = " << new_tick_target.ToInternalValue() 199 << "; new_tick_target = " << new_tick_target.ToInternalValue()
218 << "; new_interval = " << new_interval.InMicroseconds() 200 << "; new_interval = " << new_interval.InMicroseconds()
219 << "; tick_target = " << next_parameters_.tick_target.ToInternalValue() 201 << "; tick_target = " << next_parameters_.tick_target.ToInternalValue()
220 << "; intervals_elapsed = " << intervals_elapsed 202 << "; intervals_elapsed = " << intervals_elapsed;
221 << "; last_effective_tick = " << last_effective_tick.ToInternalValue();
222 203
223 // Avoid double ticks when: 204 // Avoid double ticks when:
224 // 1) Turning off the timer and turning it right back on. 205 // 1) Turning off the timer and turning it right back on.
225 // 2) Jittery data is passed to SetTimebaseAndInterval(). 206 // 2) Jittery data is passed to SetTimebaseAndInterval().
226 if (new_tick_target - last_tick_time_ <= 207 if (new_tick_target - last_tick_time_ <= new_interval / kDoubleTickDivisor)
227 new_interval / static_cast<int>(1.0 / kDoubleTickThreshold))
228 new_tick_target += new_interval; 208 new_tick_target += new_interval;
229 209
230 return new_tick_target; 210 return new_tick_target;
231 } 211 }
232 212
233 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) { 213 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) {
234 base::TimeTicks new_tick_target = NextTickTarget(now); 214 base::TimeTicks new_tick_target = NextTickTarget(now);
235 215
236 // Post another task *before* the tick and update state 216 // Post another task *before* the tick and update state
237 base::TimeDelta delay = new_tick_target - now; 217 base::TimeDelta delay;
238 DCHECK(delay.InMillisecondsF() <= 218 if (now <= new_tick_target)
239 next_parameters_.interval.InMillisecondsF() * 219 delay = new_tick_target - now;
240 (1.0 + kDoubleTickThreshold));
241 task_runner_->PostDelayedTask(FROM_HERE, 220 task_runner_->PostDelayedTask(FROM_HERE,
242 base::Bind(&DelayBasedTimeSource::OnTimerFired, 221 base::Bind(&DelayBasedTimeSource::OnTimerFired,
243 weak_factory_.GetWeakPtr()), 222 weak_factory_.GetWeakPtr()),
244 delay); 223 delay);
245 224
246 next_parameters_.tick_target = new_tick_target; 225 next_parameters_.tick_target = new_tick_target;
247 current_parameters_ = next_parameters_; 226 current_parameters_ = next_parameters_;
248 } 227 }
249 228
250 } // namespace cc 229 } // namespace cc
OLDNEW
« no previous file with comments | « cc/scheduler/delay_based_time_source.h ('k') | cc/scheduler/delay_based_time_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698