cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / scheduler / renderer / renderer_scheduler_impl.h
blob1898ca81bd8826a4762a350f4bcd127c8f5b6856
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SCHEDULER_RENDERER_RENDERER_SCHEDULER_IMPL_H_
6 #define COMPONENTS_SCHEDULER_RENDERER_RENDERER_SCHEDULER_IMPL_H_
8 #include "base/atomicops.h"
9 #include "base/synchronization/lock.h"
10 #include "components/scheduler/child/idle_helper.h"
11 #include "components/scheduler/child/pollable_thread_safe_flag.h"
12 #include "components/scheduler/child/scheduler_helper.h"
13 #include "components/scheduler/renderer/deadline_task_runner.h"
14 #include "components/scheduler/renderer/renderer_scheduler.h"
15 #include "components/scheduler/renderer/task_cost_estimator.h"
16 #include "components/scheduler/scheduler_export.h"
18 namespace base {
19 namespace trace_event {
20 class ConvertableToTraceFormat;
24 namespace scheduler {
26 class SCHEDULER_EXPORT RendererSchedulerImpl : public RendererScheduler,
27 public IdleHelper::Delegate {
28 public:
29 RendererSchedulerImpl(
30 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner);
31 ~RendererSchedulerImpl() override;
33 // RendererScheduler implementation:
34 scoped_refptr<TaskQueue> DefaultTaskRunner() override;
35 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override;
36 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override;
37 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override;
38 scoped_refptr<TaskQueue> TimerTaskRunner() override;
39 void WillBeginFrame(const cc::BeginFrameArgs& args) override;
40 void BeginFrameNotExpectedSoon() override;
41 void DidCommitFrameToCompositor() override;
42 void DidHandleInputEventOnCompositorThread(
43 const blink::WebInputEvent& web_input_event,
44 InputEventState event_state) override;
45 void DidHandleInputEventOnMainThread(
46 const blink::WebInputEvent& web_input_event) override;
47 void DidAnimateForInputOnCompositorThread() override;
48 void OnRendererHidden() override;
49 void OnRendererVisible() override;
50 void OnRendererBackgrounded() override;
51 void OnRendererForegrounded() override;
52 void OnPageLoadStarted() override;
53 bool IsHighPriorityWorkAnticipated() override;
54 bool ShouldYieldForHighPriorityWork() override;
55 bool CanExceedIdleDeadlineIfRequired() const override;
56 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override;
57 void RemoveTaskObserver(
58 base::MessageLoop::TaskObserver* task_observer) override;
59 void Shutdown() override;
60 void SuspendTimerQueue() override;
61 void ResumeTimerQueue() override;
62 void SetTimerQueueSuspensionWhenBackgroundedEnabled(bool enabled) override;
64 SchedulerHelper* GetSchedulerHelperForTesting();
65 base::TimeTicks CurrentIdleTaskDeadlineForTesting() const;
67 private:
68 friend class RendererSchedulerImplTest;
69 friend class RendererSchedulerImplForTest;
71 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum.
72 enum class Policy {
73 NORMAL,
74 COMPOSITOR_PRIORITY,
75 COMPOSITOR_CRITICAL_PATH_PRIORITY,
76 TOUCHSTART_PRIORITY,
77 LOADING_PRIORITY,
78 // Must be the last entry.
79 POLICY_COUNT,
80 FIRST_POLICY = NORMAL,
83 class PollableNeedsUpdateFlag {
84 public:
85 PollableNeedsUpdateFlag(base::Lock* write_lock);
86 ~PollableNeedsUpdateFlag();
88 // Set the flag. May only be called if |write_lock| is held.
89 void SetWhileLocked(bool value);
91 // Returns true iff the flag is set to true.
92 bool IsSet() const;
94 private:
95 base::subtle::Atomic32 flag_;
96 base::Lock* write_lock_; // Not owned.
98 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
101 // IdleHelper::Delegate implementation:
102 bool CanEnterLongIdlePeriod(
103 base::TimeTicks now,
104 base::TimeDelta* next_long_idle_period_delay_out) override;
105 void IsNotQuiescent() override {}
106 void OnIdlePeriodStarted() override;
107 void OnIdlePeriodEnded() override;
109 void EndIdlePeriod();
111 // Returns the serialized scheduler state for tracing.
112 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue(
113 base::TimeTicks optional_now) const;
114 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked(
115 base::TimeTicks optional_now) const;
116 static const char* PolicyToString(Policy policy);
118 static bool ShouldPrioritizeInputEvent(
119 const blink::WebInputEvent& web_input_event);
121 // The time we should stay in a priority-escalated mode after an input event.
122 static const int kPriorityEscalationAfterInputMillis = 100;
124 // The amount of time which idle periods can continue being scheduled when the
125 // renderer has been hidden, before going to sleep for good.
126 static const int kEndIdleWhenHiddenDelayMillis = 10000;
128 // The amount of time for which loading tasks will be prioritized over
129 // other tasks during the initial page load.
130 static const int kRailsInitialLoadingPrioritizationMillis = 1000;
132 // For the purposes of deciding whether or not it's safe to turn timers and
133 // loading tasks on only in idle periods, we regard the system as being as
134 // being "idle period" starved if there hasn't been an idle period in the last
135 // 10 seconds. This was chosen to be long enough to cover most anticipated
136 // user gestures.
137 static const int kIdlePeriodStarvationThresholdMillis = 10000;
139 // The amount of time to wait before suspending shared timers after the
140 // renderer has been backgrounded. This is use donly if background suspension
141 // of shared timers is enabled.
142 static const int kSuspendTimersWhenBackgroundedDelayMillis = 5 * 60 * 1000;
144 // Schedules an immediate PolicyUpdate, if there isn't one already pending and
145 // sets |policy_may_need_update_|. Note |any_thread_lock_| must be
146 // locked.
147 void EnsureUrgentPolicyUpdatePostedOnMainThread(
148 const tracked_objects::Location& from_here);
150 // Update the policy if a new signal has arrived. Must be called from the main
151 // thread.
152 void MaybeUpdatePolicy();
154 // Locks |any_thread_lock_| and updates the scheduler policy. May early
155 // out if the policy is unchanged. Must be called from the main thread.
156 void UpdatePolicy();
158 // Like UpdatePolicy, except it doesn't early out.
159 void ForceUpdatePolicy();
161 enum class UpdateType {
162 MAY_EARLY_OUT_IF_POLICY_UNCHANGED,
163 FORCE_UPDATE,
166 // The implelemtation of UpdatePolicy & ForceUpdatePolicy. It is allowed to
167 // early out if |update_type| is MAY_EARLY_OUT_IF_POLICY_UNCHANGED.
168 virtual void UpdatePolicyLocked(UpdateType update_type);
170 // Returns the amount of time left in the current input escalated priority
171 // policy. Can be called from any thread.
172 base::TimeDelta TimeLeftInInputEscalatedPolicy(base::TimeTicks now) const;
174 // Helper for computing the new policy. |new_policy_duration| will be filled
175 // with the amount of time after which the policy should be updated again. If
176 // the duration is zero, a new policy update will not be scheduled. Must be
177 // called with |any_thread_lock_| held. Can be called from any thread.
178 Policy ComputeNewPolicy(base::TimeTicks now,
179 base::TimeDelta* new_policy_duration) const;
181 // Works out if compositor tasks would be prioritized based on the current
182 // input signals. Can be called from any thread.
183 bool InputSignalsSuggestCompositorPriority(base::TimeTicks now) const;
185 // An input event of some sort happened, the policy may need updating.
186 void UpdateForInputEventOnCompositorThread(blink::WebInputEvent::Type type,
187 InputEventState input_event_state);
189 // Returns true if there has been at least one idle period in the last
190 // |kIdlePeriodStarvationThresholdMillis|.
191 bool HadAnIdlePeriodRecently(base::TimeTicks now) const;
193 // Helpers for safely suspending/resuming the timer queue after a
194 // background/foreground signal.
195 void SuspendTimerQueueWhenBackgrounded();
196 void ResumeTimerQueueWhenForegrounded();
198 SchedulerHelper helper_;
199 IdleHelper idle_helper_;
201 const scoped_refptr<TaskQueue> control_task_runner_;
202 const scoped_refptr<TaskQueue> compositor_task_runner_;
203 const scoped_refptr<TaskQueue> loading_task_runner_;
204 const scoped_refptr<TaskQueue> timer_task_runner_;
206 base::Closure update_policy_closure_;
207 DeadlineTaskRunner delayed_update_policy_runner_;
208 CancelableClosureHolder end_renderer_hidden_idle_period_closure_;
209 CancelableClosureHolder suspend_timers_when_backgrounded_closure_;
211 // We have decided to improve thread safety at the cost of some boilerplate
212 // (the accessors) for the following data members.
214 struct MainThreadOnly {
215 MainThreadOnly();
216 ~MainThreadOnly();
218 TaskCostEstimator timer_task_cost_estimator_;
219 cc::RollingTimeDeltaHistory short_idle_period_duration_;
220 Policy current_policy_;
221 base::TimeTicks current_policy_expiration_time_;
222 base::TimeTicks estimated_next_frame_begin_;
223 base::TimeDelta expected_short_idle_period_duration_;
224 int timer_queue_suspend_count_; // TIMER_TASK_QUEUE suspended if non-zero.
225 bool renderer_hidden_;
226 bool renderer_backgrounded_;
227 bool timer_queue_suspension_when_backgrounded_enabled_;
228 bool timer_queue_suspended_when_backgrounded_;
229 bool was_shutdown_;
232 struct AnyThread {
233 AnyThread();
235 base::TimeTicks last_input_signal_time_;
236 base::TimeTicks last_idle_period_end_time_;
237 base::TimeTicks rails_loading_priority_deadline_;
238 int pending_main_thread_input_event_count_;
239 bool awaiting_touch_start_response_;
240 bool in_idle_period_;
241 bool begin_main_frame_on_critical_path_;
242 bool timer_tasks_seem_expensive_;
245 struct CompositorThreadOnly {
246 CompositorThreadOnly();
247 ~CompositorThreadOnly();
249 blink::WebInputEvent::Type last_input_type_;
250 scoped_ptr<base::ThreadChecker> compositor_thread_checker_;
252 void CheckOnValidThread() {
253 #if DCHECK_IS_ON()
254 // We don't actually care which thread this called from, just so long as
255 // its consistent.
256 if (!compositor_thread_checker_)
257 compositor_thread_checker_.reset(new base::ThreadChecker());
258 DCHECK(compositor_thread_checker_->CalledOnValidThread());
259 #endif
263 // Don't access main_thread_only_, instead use MainThreadOnly().
264 MainThreadOnly main_thread_only_;
265 MainThreadOnly& MainThreadOnly() {
266 helper_.CheckOnValidThread();
267 return main_thread_only_;
269 const struct MainThreadOnly& MainThreadOnly() const {
270 helper_.CheckOnValidThread();
271 return main_thread_only_;
274 mutable base::Lock any_thread_lock_;
275 // Don't access any_thread_, instead use AnyThread().
276 AnyThread any_thread_;
277 AnyThread& AnyThread() {
278 any_thread_lock_.AssertAcquired();
279 return any_thread_;
281 const struct AnyThread& AnyThread() const {
282 any_thread_lock_.AssertAcquired();
283 return any_thread_;
286 // Don't access compositor_thread_only_, instead use CompositorThreadOnly().
287 CompositorThreadOnly compositor_thread_only_;
288 CompositorThreadOnly& CompositorThreadOnly() {
289 compositor_thread_only_.CheckOnValidThread();
290 return compositor_thread_only_;
293 PollableThreadSafeFlag policy_may_need_update_;
294 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
296 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
299 } // namespace scheduler
301 #endif // COMPONENTS_SCHEDULER_RENDERER_RENDERER_SCHEDULER_IMPL_H_