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

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: Ross's suggestions Created 5 years, 9 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 PrioritizingTaskQueueSelector;
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 by scheduler implementatiosn to customize idle behaviour.
Sami 2015/03/27 17:22:27 s/sn/ns/
alex clarke (OOO till 29th) 2015/03/27 17:38:51 Done.
32 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner); 22 class SchedulerHelperDelegate {
33 ~RendererSchedulerImpl() override; 23 public:
24 SchedulerHelperDelegate();
25 virtual ~SchedulerHelperDelegate();
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;
42 void DidCommitFrameToCompositor() override;
43 void DidReceiveInputEventOnCompositorThread(
44 const blink::WebInputEvent& web_input_event) override;
45 void DidAnimateForInputOnCompositorThread() override;
46 bool CanExceedIdleDeadlineIfRequired() const override;
47 bool IsHighPriorityWorkAnticipated() override;
48 bool ShouldYieldForHighPriorityWork() override;
49 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override;
50 void RemoveTaskObserver(
51 base::MessageLoop::TaskObserver* task_observer) override;
52 void Shutdown() override;
53 33
54 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); 34 private:
55 void SetWorkBatchSizeForTesting(size_t work_batch_size); 35 DISALLOW_COPY_AND_ASSIGN(SchedulerHelperDelegate);
36 };
56 37
57 private: 38 // NOTE Category strings must have application lifetime (statics or
58 friend class RendererSchedulerImplTest; 39 // literals). They may not include " chars.
Sami 2015/03/27 17:22:27 Please also mention that total_task_queue_count mu
alex clarke (OOO till 29th) 2015/03/27 17:38:51 Done.
59 friend class RendererSchedulerImplForTest; 40 SchedulerHelper(
41 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner,
42 SchedulerHelperDelegate* scheduler_helper_delegate,
43 const char* tracing_category,
44 const char* disabled_by_default_tracing_category,
45 size_t total_task_queue_count);
46 ~SchedulerHelper();
60 47
61 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. 48 // Returns the default task runner.
49 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner();
50
51 // Returns the idle task runner. Tasks posted to this runner may be reordered
52 // relative to other task types and may be starved for an arbitrarily long
53 // time if no idle time is available.
54 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner();
55
56 // Returns the control task runner. Tasks posted to this runner are executed
57 // with the highest priority. Care must be taken to avoid starvation of other
58 // task queues.
59 scoped_refptr<base::SingleThreadTaskRunner> ControlTaskRunner();
60
61 // Returns true if a currently running idle task could exceed its deadline
62 // without impacting user experience too much. This should only be used if
63 // there is a task which cannot be pre-empted and is likely to take longer
64 // than the largest expected idle task deadline. It should NOT be polled to
65 // check whether more work can be performed on the current idle task after
66 // its deadline has expired - post a new idle task for the continuation of the
67 // work in this case.
68 // Must be called from the main thread.
Sami 2015/03/27 17:22:27 Since we're no longer limited to _the_ main thread
alex clarke (OOO till 29th) 2015/03/27 17:38:51 Done.
69 bool CanExceedIdleDeadlineIfRequired() const;
70
71 // Adds or removes a task observer from the scheduler. The observer will be
72 // notified before and after every executed task. These functions can only be
73 // called on the main thread.
74 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer);
75 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer);
76
77 // Shuts down the scheduler by dropping any remaining pending work in the work
78 // queues. After this call any work posted to the task runners will be
79 // silently dropped.
80 void Shutdown();
81
82 // Returns true if Shutdown() has been called. Otherwise returns false.
83 bool IsShutdown() const { return !task_queue_manager_.get(); }
84
85 // Keep SchedulerHelper::TaskQueueIdToString in sync with this enum.
62 enum QueueId { 86 enum QueueId {
63 DEFAULT_TASK_QUEUE, 87 DEFAULT_TASK_QUEUE,
64 COMPOSITOR_TASK_QUEUE,
65 LOADING_TASK_QUEUE,
66 IDLE_TASK_QUEUE, 88 IDLE_TASK_QUEUE,
67 CONTROL_TASK_QUEUE, 89 CONTROL_TASK_QUEUE,
68 CONTROL_TASK_AFTER_WAKEUP_QUEUE, 90 CONTROL_TASK_AFTER_WAKEUP_QUEUE,
69 // Must be the last entry. 91 // Must be the last entry.
70 TASK_QUEUE_COUNT, 92 TASK_QUEUE_COUNT,
71 }; 93 };
72 94
73 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. 95 // Keep SchedulerHelper::IdlePeriodStateToString in sync with this enum.
74 enum class Policy {
75 NORMAL,
76 COMPOSITOR_PRIORITY,
77 TOUCHSTART_PRIORITY,
78 };
79
80 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this
81 // enum.
82 enum class InputStreamState {
83 INACTIVE,
84 ACTIVE,
85 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 };
87
88 // Keep RendererSchedulerImpl::IdlePeriodStateToString in sync with this enum.
89 enum class IdlePeriodState { 96 enum class IdlePeriodState {
90 NOT_IN_IDLE_PERIOD, 97 NOT_IN_IDLE_PERIOD,
91 IN_SHORT_IDLE_PERIOD, 98 IN_SHORT_IDLE_PERIOD,
92 IN_LONG_IDLE_PERIOD, 99 IN_LONG_IDLE_PERIOD,
93 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE, 100 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE,
94 ENDING_LONG_IDLE_PERIOD 101 ENDING_LONG_IDLE_PERIOD
95 }; 102 };
96 103
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); 104 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); 105 static const char* IdlePeriodStateToString(IdlePeriodState state);
122 106
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. 107 // The maximum length of an idle period.
132 static const int kMaximumIdlePeriodMillis = 50; 108 static const int kMaximumIdlePeriodMillis = 50;
133 109
134 // The minimum delay to wait between retrying to initiate a long idle time. 110 // The minimum delay to wait between retrying to initiate a long idle time.
135 static const int kRetryInitiateLongIdlePeriodDelayMillis = 1; 111 static const int kRetryInitiateLongIdlePeriodDelayMillis = 1;
136 112
137 // IdleTaskDeadlineSupplier Implementation: 113 // IdleTaskDeadlineSupplier Implementation:
138 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const; 114 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const;
139 115
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 116 // 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 117 // |next_long_idle_period_delay_out| with the next time we should try to
179 // initiate the next idle period. 118 // initiate the next idle period.
180 IdlePeriodState ComputeNewLongIdlePeriodState( 119 IdlePeriodState ComputeNewLongIdlePeriodState(
181 const base::TimeTicks now, 120 const base::TimeTicks now,
182 base::TimeDelta* next_long_idle_period_delay_out); 121 base::TimeDelta* next_long_idle_period_delay_out);
183 122
184 // Initiate a long idle period. 123 // Initiate a long idle period.
185 void InitiateLongIdlePeriod(); 124 void InitiateLongIdlePeriod();
186 void InitiateLongIdlePeriodAfterWakeup(); 125 void InitiateLongIdlePeriodAfterWakeup();
187 126
188 // Start and end an idle period. 127 // Start and end an idle period.
189 void StartIdlePeriod(IdlePeriodState new_idle_period_state); 128 void StartIdlePeriod(IdlePeriodState new_idle_period_state);
190 void EndIdlePeriod(); 129 void EndIdlePeriod();
191 130
192 // Returns true if |state| represents being within an idle period state. 131 // Returns true if |state| represents being within an idle period state.
193 static bool IsInIdlePeriod(IdlePeriodState state); 132 static bool IsInIdlePeriod(IdlePeriodState state);
194 133
134 void CheckOnValidThread() const {
Sami 2015/03/27 17:22:27 nit: CheckOnMainThread() to make this match the me
alex clarke (OOO till 29th) 2015/03/27 17:38:51 Not sure I agree in light of your comment above ab
Sami 2015/03/27 17:53:11 Right, now that we're no longer calling it the mai
135 DCHECK(main_thread_checker_.CalledOnValidThread());
136 }
137
138 // Acessor methods.
Sami 2015/03/27 17:22:27 s/Acessor/Accessor/
alex clarke (OOO till 29th) 2015/03/27 17:38:51 Done.
195 base::TimeTicks Now() const; 139 base::TimeTicks Now() const;
140 base::TimeTicks EstimatedEndOfIdle() const;
141 void SetEstimatedEndOfIdle(base::TimeTicks estimated_next_frame_begin);
142
143 const CancelableClosureHolder& EndIdlePeriodClosure() const;
144 IdlePeriodState SchedulerIdlePeriodState() const;
145 PrioritizingTaskQueueSelector* SchedulerTaskQueueSelector() const;
146 TaskQueueManager* SchedulerTaskQueueManager() const;
147
148 // Test helpers.
149 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source);
150 void SetWorkBatchSizeForTesting(size_t work_batch_size);
151
152 private:
153 friend class SchedulerHelperTest;
196 154
197 base::ThreadChecker main_thread_checker_; 155 base::ThreadChecker main_thread_checker_;
198 scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_; 156 scoped_ptr<PrioritizingTaskQueueSelector> task_queue_selector_;
199 scoped_ptr<TaskQueueManager> task_queue_manager_; 157 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 158
207 base::Closure update_policy_closure_;
208 DeadlineTaskRunner delayed_update_policy_runner_;
209 CancelableClosureHolder end_idle_period_closure_; 159 CancelableClosureHolder end_idle_period_closure_;
210 CancelableClosureHolder initiate_next_long_idle_period_closure_; 160 CancelableClosureHolder initiate_next_long_idle_period_closure_;
211 CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_; 161 CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_;
212 162
213 // Don't access current_policy_ directly, instead use SchedulerPolicy().
214 Policy current_policy_;
215 IdlePeriodState idle_period_state_; 163 IdlePeriodState idle_period_state_;
164 SchedulerHelperDelegate* scheduler_helper_delegate_; // NOT OWNED
216 165
217 base::TimeTicks estimated_next_frame_begin_; 166 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
218 base::TimeTicks current_policy_expiration_time_; 167 scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_;
168 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
169 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
219 170
220 // The incoming_signals_lock_ mutex protects access to all variables in the 171 base::TimeTicks estimated_end_of_idle_;
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_; 172 scoped_refptr<cc::TestNowSource> time_source_;
230 173
231 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_; 174 const char* tracing_category_;
232 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; 175 const char* disabled_by_default_tracing_category_;
233 176
234 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); 177 base::WeakPtr<SchedulerHelper> weak_scheduler_ptr_;
178 base::WeakPtrFactory<SchedulerHelper> weak_factory_;
179
180 DISALLOW_COPY_AND_ASSIGN(SchedulerHelper);
235 }; 181 };
236 182
237 } // namespace content 183 } // namespace content
238 184
239 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 185 #endif // CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698