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