OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_SCHEDULER_CHILD_IDLE_HELPER_H_ | |
6 #define COMPONENTS_SCHEDULER_CHILD_IDLE_HELPER_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "components/scheduler/base/cancelable_closure_holder.h" | |
11 #include "components/scheduler/base/task_queue_selector.h" | |
12 #include "components/scheduler/child/scheduler_helper.h" | |
13 #include "components/scheduler/child/single_thread_idle_task_runner.h" | |
14 #include "components/scheduler/scheduler_export.h" | |
15 | |
16 namespace scheduler { | |
17 | |
18 class SchedulerHelper; | |
19 | |
20 // Common scheduler functionality for Idle tasks. | |
21 class SCHEDULER_EXPORT IdleHelper | |
22 : public base::MessageLoop::TaskObserver, | |
23 public SingleThreadIdleTaskRunner::Delegate { | |
24 public: | |
25 // Used to by scheduler implementations to customize idle behaviour. | |
26 class SCHEDULER_EXPORT Delegate { | |
27 public: | |
28 Delegate(); | |
29 virtual ~Delegate(); | |
30 | |
31 // If it's ok to enter a long idle period, return true. Otherwise return | |
32 // false and set next_long_idle_period_delay_out so we know when to try | |
33 // again. | |
34 virtual bool CanEnterLongIdlePeriod( | |
35 base::TimeTicks now, | |
36 base::TimeDelta* next_long_idle_period_delay_out) = 0; | |
37 | |
38 // Signals that the Long Idle Period hasn't started yet because the system | |
39 // isn't quiescent. | |
40 virtual void IsNotQuiescent() = 0; | |
41 | |
42 // Signals that we have started an Idle Period. | |
43 virtual void OnIdlePeriodStarted() = 0; | |
44 | |
45 // Signals that we have finished an Idle Period. | |
46 virtual void OnIdlePeriodEnded() = 0; | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
50 }; | |
51 | |
52 // Keep IdleHelper::IdlePeriodStateToString in sync with this enum. | |
53 enum class IdlePeriodState { | |
54 NOT_IN_IDLE_PERIOD, | |
55 IN_SHORT_IDLE_PERIOD, | |
56 IN_LONG_IDLE_PERIOD, | |
57 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE, | |
58 IN_LONG_IDLE_PERIOD_PAUSED, | |
59 // Must be the last entry. | |
60 IDLE_PERIOD_STATE_COUNT, | |
61 FIRST_IDLE_PERIOD_STATE = NOT_IN_IDLE_PERIOD, | |
62 }; | |
63 | |
64 // The maximum length of an idle period. | |
65 static const int kMaximumIdlePeriodMillis = 50; | |
66 | |
67 // |helper| and |delegate| are not owned by IdleHelper object and must | |
68 // outlive it. | |
69 IdleHelper( | |
70 SchedulerHelper* helper, | |
71 Delegate* delegate, | |
72 const char* tracing_category, | |
73 const char* disabled_by_default_tracing_category, | |
74 const char* idle_period_tracing_name, | |
75 base::TimeDelta required_quiescence_duration_before_long_idle_period); | |
76 ~IdleHelper() override; | |
77 | |
78 // Returns the idle task runner. Tasks posted to this runner may be reordered | |
79 // relative to other task types and may be starved for an arbitrarily long | |
80 // time if no idle time is available. | |
81 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner(); | |
82 | |
83 // If |required_quiescence_duration_before_long_idle_period_| is zero then | |
84 // immediately initiate a long idle period, otherwise check if any tasks have | |
85 // run recently and if so, check again after a delay of | |
86 // |required_quiescence_duration_before_long_idle_period_|. | |
87 // Calling this function will end any previous idle period immediately, and | |
88 // potentially again later if | |
89 // |required_quiescence_duration_before_long_idle_period_| is non-zero. | |
90 // NOTE EndIdlePeriod will disable the long idle periods. | |
91 void EnableLongIdlePeriod(); | |
92 | |
93 // Start an idle period with a given idle period deadline. | |
94 void StartIdlePeriod(IdlePeriodState new_idle_period_state, | |
95 base::TimeTicks now, | |
96 base::TimeTicks idle_period_deadline); | |
97 | |
98 // This will end an idle period either started with StartIdlePeriod or | |
99 // EnableLongIdlePeriod. | |
100 void EndIdlePeriod(); | |
101 | |
102 // Returns true if a currently running idle task could exceed its deadline | |
103 // without impacting user experience too much. This should only be used if | |
104 // there is a task which cannot be pre-empted and is likely to take longer | |
105 // than the largest expected idle task deadline. It should NOT be polled to | |
106 // check whether more work can be performed on the current idle task after | |
107 // its deadline has expired - post a new idle task for the continuation of the | |
108 // work in this case. | |
109 // Must be called from the thread this class was created on. | |
110 bool CanExceedIdleDeadlineIfRequired() const; | |
111 | |
112 // Returns the deadline for the current idle task. | |
113 base::TimeTicks CurrentIdleTaskDeadline() const; | |
114 | |
115 // SingleThreadIdleTaskRunner::Delegate implementation: | |
116 void OnIdleTaskPosted() override; | |
117 base::TimeTicks WillProcessIdleTask() override; | |
118 void DidProcessIdleTask() override; | |
119 | |
120 // base::MessageLoop::TaskObserver implementation: | |
121 void WillProcessTask(const base::PendingTask& pending_task) override; | |
122 void DidProcessTask(const base::PendingTask& pending_task) override; | |
123 | |
124 IdlePeriodState SchedulerIdlePeriodState() const; | |
125 static const char* IdlePeriodStateToString(IdlePeriodState state); | |
126 | |
127 private: | |
128 friend class BaseIdleHelperTest; | |
129 friend class IdleHelperTest; | |
130 | |
131 class State { | |
132 public: | |
133 State(SchedulerHelper* helper, | |
134 Delegate* delegate, | |
135 const char* tracing_category, | |
136 const char* disabled_by_default_tracing_category, | |
137 const char* idle_period_tracing_name); | |
138 virtual ~State(); | |
139 | |
140 void UpdateState(IdlePeriodState new_state, | |
141 base::TimeTicks new_deadline, | |
142 base::TimeTicks optional_now); | |
143 bool IsIdlePeriodPaused() const; | |
144 | |
145 IdlePeriodState idle_period_state() const; | |
146 base::TimeTicks idle_period_deadline() const; | |
147 | |
148 void TraceIdleIdleTaskStart(); | |
149 void TraceIdleIdleTaskEnd(); | |
150 | |
151 private: | |
152 void TraceEventIdlePeriodStateChange(IdlePeriodState new_state, | |
153 bool new_running_idle_task, | |
154 base::TimeTicks new_deadline, | |
155 base::TimeTicks optional_now); | |
156 | |
157 SchedulerHelper* helper_; // NOT OWNED | |
158 Delegate* delegate_; // NOT OWNED | |
159 | |
160 IdlePeriodState idle_period_state_; | |
161 base::TimeTicks idle_period_deadline_; | |
162 | |
163 base::TimeTicks last_idle_task_trace_time_; | |
164 bool idle_period_trace_event_started_; | |
165 bool running_idle_task_for_tracing_; | |
166 const char* tracing_category_; | |
167 const char* disabled_by_default_tracing_category_; | |
168 const char* idle_period_tracing_name_; | |
169 | |
170 DISALLOW_COPY_AND_ASSIGN(State); | |
171 }; | |
172 | |
173 // The minimum duration of an idle period. | |
174 static const int kMinimumIdlePeriodDurationMillis = 1; | |
175 | |
176 // The minimum delay to wait between retrying to initiate a long idle time. | |
177 static const int kRetryEnableLongIdlePeriodDelayMillis = 1; | |
178 | |
179 // Returns the new idle period state for the next long idle period. Fills in | |
180 // |next_long_idle_period_delay_out| with the next time we should try to | |
181 // initiate the next idle period. | |
182 IdlePeriodState ComputeNewLongIdlePeriodState( | |
183 const base::TimeTicks now, | |
184 base::TimeDelta* next_long_idle_period_delay_out); | |
185 | |
186 bool ShouldWaitForQuiescence(); | |
187 void OnIdleTaskPostedOnMainThread(); | |
188 void UpdateLongIdlePeriodStateAfterIdleTask(); | |
189 | |
190 void SetIdlePeriodState(IdlePeriodState new_state, | |
191 base::TimeTicks new_deadline, | |
192 base::TimeTicks optional_now); | |
193 | |
194 // Returns true if |state| represents being within an idle period state. | |
195 static bool IsInIdlePeriod(IdlePeriodState state); | |
196 // Returns true if |state| represents being within a long idle period state. | |
197 static bool IsInLongIdlePeriod(IdlePeriodState state); | |
198 | |
199 SchedulerHelper* helper_; // NOT OWNED | |
200 Delegate* delegate_; // NOT OWNED | |
201 scoped_refptr<TaskQueue> idle_queue_; | |
202 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; | |
203 | |
204 CancelableClosureHolder enable_next_long_idle_period_closure_; | |
205 CancelableClosureHolder on_idle_task_posted_closure_; | |
206 | |
207 State state_; | |
208 | |
209 base::TimeDelta required_quiescence_duration_before_long_idle_period_; | |
210 | |
211 const char* disabled_by_default_tracing_category_; | |
212 | |
213 base::WeakPtr<IdleHelper> weak_idle_helper_ptr_; | |
214 base::WeakPtrFactory<IdleHelper> weak_factory_; | |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(IdleHelper); | |
217 }; | |
218 | |
219 } // namespace scheduler | |
220 | |
221 #endif // COMPONENTS_SCHEDULER_CHILD_IDLE_HELPER_H_ | |
OLD | NEW |