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

Side by Side Diff: content/child/scheduler/scheduler_helper.h

Issue 1025323003: Introduce a SchedulerHelper in content/child/scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added the delegate Created 5 years, 8 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 5 #ifndef CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_
6 #define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 6 #define CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_
7 7
8 #include "base/atomicops.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "cc/test/test_now_source.h" 8 #include "cc/test/test_now_source.h"
12 #include "content/renderer/scheduler/cancelable_closure_holder.h" 9 #include "content/child/scheduler/cancelable_closure_holder.h"
13 #include "content/renderer/scheduler/deadline_task_runner.h" 10 #include "content/child/scheduler/single_thread_idle_task_runner.h"
14 #include "content/renderer/scheduler/renderer_scheduler.h" 11 #include "content/child/scheduler/task_queue_manager.h"
15 #include "content/renderer/scheduler/single_thread_idle_task_runner.h"
16 #include "content/renderer/scheduler/task_queue_manager.h"
17
18 namespace base {
19 namespace trace_event {
20 class ConvertableToTraceFormat;
21 }
22 }
23 12
24 namespace content { 13 namespace content {
25 14
26 class RendererTaskQueueSelector; 15 class TaskQueueSelectorImpl;
27 class NestableSingleThreadTaskRunner; 16 class NestableSingleThreadTaskRunner;
28 17
29 class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { 18 // Common scheduler functionality for Default and Idle tasks.
19 class CONTENT_EXPORT SchedulerHelper {
30 public: 20 public:
31 RendererSchedulerImpl( 21 // Used to customize Long Idle Period behaviour.
32 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner); 22 class LongIdlePeriodDelegate {
Sami 2015/03/27 01:15:55 Should we just call this SchedulerHelperDelegate?
alex clarke (OOO till 29th) 2015/03/27 10:55:29 Done.
33 ~RendererSchedulerImpl() override; 23 public:
24 LongIdlePeriodDelegate();
25 virtual ~LongIdlePeriodDelegate();
34 26
35 // RendererScheduler implementation: 27 // If it's ok to enter a Long Idle period, return true. Otherwise return
36 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override; 28 // false and set next_long_idle_period_delay_out so we know when to try
37 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override; 29 // again.
38 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override; 30 virtual bool CanEnterLongIdlePeriod(
39 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override; 31 base::TimeTicks now,
40 void WillBeginFrame(const cc::BeginFrameArgs& args) override; 32 base::TimeDelta* next_long_idle_period_delay_out) = 0;
41 void BeginFrameNotExpectedSoon() override; 33 };
42 void DidCommitFrameToCompositor() override; 34
43 void DidReceiveInputEventOnCompositorThread( 35 SchedulerHelper(
44 const blink::WebInputEvent& web_input_event) override; 36 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner,
45 void DidAnimateForInputOnCompositorThread() override; 37 const char* tracing_category,
46 bool CanExceedIdleDeadlineIfRequired() const override; 38 const char* disabled_by_default_tracing_category,
47 bool IsHighPriorityWorkAnticipated() override; 39 size_t task_queue_count,
Sami 2015/03/27 01:15:55 Since everyone needs these four task queues, how a
alex clarke (OOO till 29th) 2015/03/27 10:55:29 Done.
48 bool ShouldYieldForHighPriorityWork() override; 40 size_t control_task_queue,
49 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; 41 size_t control_task_after_wakeup_queue,
50 void RemoveTaskObserver( 42 size_t default_task_queue,
51 base::MessageLoop::TaskObserver* task_observer) override; 43 size_t idle_task_queue,
52 void Shutdown() override; 44 LongIdlePeriodDelegate* long_idle_period_delegate);
45 ~SchedulerHelper();
53 46
54 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); 47 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source);
55 void SetWorkBatchSizeForTesting(size_t work_batch_size); 48 void SetWorkBatchSizeForTesting(size_t work_batch_size);
56 49
57 private: 50 // Returns the default task runner.
58 friend class RendererSchedulerImplTest; 51 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner();
59 friend class RendererSchedulerImplForTest;
60 52
61 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. 53 // Returns the idle task runner. Tasks posted to this runner may be reordered
62 enum QueueId { 54 // relative to other task types and may be starved for an arbitrarily long
63 DEFAULT_TASK_QUEUE, 55 // time if no idle time is available.
64 COMPOSITOR_TASK_QUEUE, 56 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner();
65 LOADING_TASK_QUEUE,
66 IDLE_TASK_QUEUE,
67 CONTROL_TASK_QUEUE,
68 CONTROL_TASK_AFTER_WAKEUP_QUEUE,
69 // Must be the last entry.
70 TASK_QUEUE_COUNT,
71 };
72 57
73 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. 58 // Returns true if a currently running idle task could exceed its deadline
74 enum class Policy { 59 // without impacting user experience too much. This should only be used if
75 NORMAL, 60 // there is a task which cannot be pre-empted and is likely to take longer
76 COMPOSITOR_PRIORITY, 61 // than the largest expected idle task deadline. It should NOT be polled to
77 TOUCHSTART_PRIORITY, 62 // check whether more work can be performed on the current idle task after
78 }; 63 // its deadline has expired - post a new idle task for the continuation of the
64 // work in this case.
65 // Must be called from the main thread.
66 bool CanExceedIdleDeadlineIfRequired() const;
79 67
80 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this 68 // Adds or removes a task observer from the scheduler. The observer will be
81 // enum. 69 // notified before and after every executed task. These functions can only be
82 enum class InputStreamState { 70 // called on the main thread.
83 INACTIVE, 71 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer);
84 ACTIVE, 72 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer);
85 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 };
87 73
88 // Keep RendererSchedulerImpl::IdlePeriodStateToString in sync with this enum. 74 // Shuts down the scheduler by dropping any remaining pending work in the work
75 // queues. After this call any work posted to the task runners will be
76 // silently dropped.
77 void Shutdown();
78
79 scoped_refptr<base::SingleThreadTaskRunner> ControlTaskRunner();
80
81 // Keep SchedulerHelper::IdlePeriodStateToString in sync with this enum.
89 enum class IdlePeriodState { 82 enum class IdlePeriodState {
90 NOT_IN_IDLE_PERIOD, 83 NOT_IN_IDLE_PERIOD,
91 IN_SHORT_IDLE_PERIOD, 84 IN_SHORT_IDLE_PERIOD,
92 IN_LONG_IDLE_PERIOD, 85 IN_LONG_IDLE_PERIOD,
93 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE, 86 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE,
94 ENDING_LONG_IDLE_PERIOD 87 ENDING_LONG_IDLE_PERIOD
95 }; 88 };
96 89
97 class PollableNeedsUpdateFlag {
98 public:
99 PollableNeedsUpdateFlag(base::Lock* write_lock);
100 ~PollableNeedsUpdateFlag();
101
102 // Set the flag. May only be called if |write_lock| is held.
103 void SetWhileLocked(bool value);
104
105 // Returns true iff the flag is set to true.
106 bool IsSet() const;
107
108 private:
109 base::subtle::Atomic32 flag_;
110 base::Lock* write_lock_; // Not owned.
111
112 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
113 };
114
115 // Returns the serialized scheduler state for tracing.
116 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked(
117 base::TimeTicks optional_now) const;
118 static const char* TaskQueueIdToString(QueueId queue_id);
119 static const char* PolicyToString(Policy policy);
120 static const char* InputStreamStateToString(InputStreamState state);
121 static const char* IdlePeriodStateToString(IdlePeriodState state); 90 static const char* IdlePeriodStateToString(IdlePeriodState state);
122 91
123 static InputStreamState ComputeNewInputStreamState(
124 InputStreamState current_state,
125 blink::WebInputEvent::Type new_input_event,
126 blink::WebInputEvent::Type last_input_event);
127
128 // The time we should stay in a priority-escalated mode after an input event.
129 static const int kPriorityEscalationAfterInputMillis = 100;
130
131 // The maximum length of an idle period. 92 // The maximum length of an idle period.
132 static const int kMaximumIdlePeriodMillis = 50; 93 static const int kMaximumIdlePeriodMillis = 50;
133 94
134 // The minimum delay to wait between retrying to initiate a long idle time. 95 // The minimum delay to wait between retrying to initiate a long idle time.
135 static const int kRetryInitiateLongIdlePeriodDelayMillis = 1; 96 static const int kRetryInitiateLongIdlePeriodDelayMillis = 1;
136 97
137 // IdleTaskDeadlineSupplier Implementation: 98 // IdleTaskDeadlineSupplier Implementation:
138 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const; 99 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const;
139 100
140 // Returns the current scheduler policy. Must be called from the main thread.
141 Policy SchedulerPolicy() const;
142
143 // Schedules an immediate PolicyUpdate, if there isn't one already pending and
144 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be
145 // locked.
146 void EnsureUrgentPolicyUpdatePostedOnMainThread(
147 const tracked_objects::Location& from_here);
148
149 // Update the policy if a new signal has arrived. Must be called from the main
150 // thread.
151 void MaybeUpdatePolicy();
152
153 // Locks |incoming_signals_lock_| and updates the scheduler policy.
154 // Must be called from the main thread.
155 void UpdatePolicy();
156 virtual void UpdatePolicyLocked();
157
158 // Returns the amount of time left in the current input escalated priority
159 // policy.
160 base::TimeDelta TimeLeftInInputEscalatedPolicy(base::TimeTicks now) const;
161
162 // Helper for computing the new policy. |new_policy_duration| will be filled
163 // with the amount of time after which the policy should be updated again. If
164 // the duration is zero, a new policy update will not be scheduled. Must be
165 // called with |incoming_signals_lock_| held.
166 Policy ComputeNewPolicy(base::TimeTicks now,
167 base::TimeDelta* new_policy_duration);
168
169 // An input event of some sort happened, the policy may need updating.
170 void UpdateForInputEvent(blink::WebInputEvent::Type type);
171
172 // Called when a previously queued input event was processed.
173 // |begin_frame_time|, if non-zero, identifies the frame time at which the
174 // input was processed.
175 void DidProcessInputEvent(base::TimeTicks begin_frame_time);
176
177 // Returns the new idle period state for the next long idle period. Fills in 101 // Returns the new idle period state for the next long idle period. Fills in
178 // |next_long_idle_period_delay_out| with the next time we should try to 102 // |next_long_idle_period_delay_out| with the next time we should try to
179 // initiate the next idle period. 103 // initiate the next idle period.
180 IdlePeriodState ComputeNewLongIdlePeriodState( 104 IdlePeriodState ComputeNewLongIdlePeriodState(
181 const base::TimeTicks now, 105 const base::TimeTicks now,
182 base::TimeDelta* next_long_idle_period_delay_out); 106 base::TimeDelta* next_long_idle_period_delay_out);
183 107
184 // Initiate a long idle period. 108 // Initiate a long idle period.
185 void InitiateLongIdlePeriod(); 109 void InitiateLongIdlePeriod();
186 void InitiateLongIdlePeriodAfterWakeup(); 110 void InitiateLongIdlePeriodAfterWakeup();
187 111
188 // Start and end an idle period. 112 // Start and end an idle period.
189 void StartIdlePeriod(IdlePeriodState new_idle_period_state); 113 void StartIdlePeriod(IdlePeriodState new_idle_period_state);
190 void EndIdlePeriod(); 114 void EndIdlePeriod();
191 115
192 // Returns true if |state| represents being within an idle period state. 116 // Returns true if |state| represents being within an idle period state.
193 static bool IsInIdlePeriod(IdlePeriodState state); 117 static bool IsInIdlePeriod(IdlePeriodState state);
194 118
119 void CheckOnValidThread() const {
120 DCHECK(main_thread_checker_.CalledOnValidThread());
121 }
122
123 // Acessor methods.
195 base::TimeTicks Now() const; 124 base::TimeTicks Now() const;
125 base::TimeTicks EstimatedNextFrameBegin() const;
126 void SetEstimatedNextFrameBegin(base::TimeTicks estimated_next_frame_begin);
127
128 const CancelableClosureHolder& EndIdlePeriodClosure() const;
129 IdlePeriodState SchedulerIdlePeriodState() const;
130 TaskQueueSelectorImpl* SchedulerTaskQueueSelector() const;
131 TaskQueueManager* SchedulerTaskQueueManager() const;
132
133 private:
134 friend class SchedulerHelperTest;
196 135
197 base::ThreadChecker main_thread_checker_; 136 base::ThreadChecker main_thread_checker_;
198 scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_; 137 scoped_ptr<TaskQueueSelectorImpl> task_queue_selector_;
199 scoped_ptr<TaskQueueManager> task_queue_manager_; 138 scoped_ptr<TaskQueueManager> task_queue_manager_;
200 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
201 scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_;
202 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
203 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
204 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
205 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
206 139
207 base::Closure update_policy_closure_;
208 DeadlineTaskRunner delayed_update_policy_runner_;
209 CancelableClosureHolder end_idle_period_closure_; 140 CancelableClosureHolder end_idle_period_closure_;
210 CancelableClosureHolder initiate_next_long_idle_period_closure_; 141 CancelableClosureHolder initiate_next_long_idle_period_closure_;
211 CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_; 142 CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_;
212 143
213 // Don't access current_policy_ directly, instead use SchedulerPolicy().
214 Policy current_policy_;
215 IdlePeriodState idle_period_state_; 144 IdlePeriodState idle_period_state_;
145 LongIdlePeriodDelegate* long_idle_period_delegate_; // NOT OWNED
146
147 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
148 scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_;
149 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
150 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
216 151
217 base::TimeTicks estimated_next_frame_begin_; 152 base::TimeTicks estimated_next_frame_begin_;
218 base::TimeTicks current_policy_expiration_time_;
219
220 // The incoming_signals_lock_ mutex protects access to all variables in the
221 // (contiguous) block below.
222 base::Lock incoming_signals_lock_;
223 base::TimeTicks last_input_receipt_time_on_compositor_;
224 base::TimeTicks last_input_process_time_on_main_;
225 blink::WebInputEvent::Type last_input_type_;
226 InputStreamState input_stream_state_;
227 PollableNeedsUpdateFlag policy_may_need_update_;
228
229 scoped_refptr<cc::TestNowSource> time_source_; 153 scoped_refptr<cc::TestNowSource> time_source_;
230 154
231 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_; 155 const char* tracing_category_;
232 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; 156 const char* disabled_by_default_tracing_category_;
157 const size_t idle_task_queue_;
233 158
234 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); 159 base::WeakPtr<SchedulerHelper> weak_scheduler_ptr_;
160 base::WeakPtrFactory<SchedulerHelper> weak_factory_;
161
162 DISALLOW_COPY_AND_ASSIGN(SchedulerHelper);
235 }; 163 };
236 164
237 } // namespace content 165 } // namespace content
238 166
239 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 167 #endif // CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698