OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | |
6 #define CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/atomic_sequence_num.h" | |
11 #include "base/debug/task_annotator.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/pending_task.h" | |
16 #include "base/synchronization/lock.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "components/scheduler/base/enqueue_order.h" | |
19 #include "components/scheduler/base/task_queue_impl.h" | |
20 #include "components/scheduler/base/task_queue_selector.h" | |
21 #include "components/scheduler/scheduler_export.h" | |
22 | |
23 namespace base { | |
24 class TickClock; | |
25 | |
26 namespace trace_event { | |
27 class ConvertableToTraceFormat; | |
28 class TracedValue; | |
29 } // namespace trace_event | |
30 } // namespace base | |
31 | |
32 namespace scheduler { | |
33 namespace internal { | |
34 class TaskQueueImpl; | |
35 } // namespace internal | |
36 | |
37 class LazyNow; | |
38 class RealTimeDomain; | |
39 class TimeDomain; | |
40 class TaskQueueManagerDelegate; | |
41 class TaskTimeTracker; | |
42 | |
43 // The task queue manager provides N task queues and a selector interface for | |
44 // choosing which task queue to service next. Each task queue consists of two | |
45 // sub queues: | |
46 // | |
47 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | |
48 // When a task is appended into an empty incoming queue, the task manager | |
49 // work function (DoWork) is scheduled to run on the main task runner. | |
50 // | |
51 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | |
52 // the incoming task queue (if any) are moved here. The work queues are | |
53 // registered with the selector as input to the scheduling decision. | |
54 // | |
55 class SCHEDULER_EXPORT TaskQueueManager | |
56 : public internal::TaskQueueSelector::Observer { | |
57 public: | |
58 // Create a task queue manager where |delegate| identifies the thread | |
59 // on which where the tasks are eventually run. Category strings must have | |
60 // application lifetime (statics or literals). They may not include " chars. | |
61 TaskQueueManager(scoped_refptr<TaskQueueManagerDelegate> delegate, | |
62 const char* tracing_category, | |
63 const char* disabled_by_default_tracing_category, | |
64 const char* disabled_by_default_verbose_tracing_category); | |
65 ~TaskQueueManager() override; | |
66 | |
67 // Requests that a task to process work is posted on the main task runner. | |
68 // These tasks are de-duplicated in two buckets: main-thread and all other | |
69 // threads. This distinction is done to reduce the overehead from locks, we | |
70 // assume the main-thread path will be hot. | |
71 void MaybeScheduleImmediateWork(const tracked_objects::Location& from_here); | |
72 | |
73 // Requests that a delayed task to process work is posted on the main task | |
74 // runner. These delayed tasks are de-duplicated. Must be called on the thread | |
75 // this class was created on. | |
76 void MaybeScheduleDelayedWork(const tracked_objects::Location& from_here, | |
77 base::TimeTicks now, | |
78 base::TimeDelta delay); | |
79 | |
80 // Set the number of tasks executed in a single invocation of the task queue | |
81 // manager. Increasing the batch size can reduce the overhead of yielding | |
82 // back to the main message loop -- at the cost of potentially delaying other | |
83 // tasks posted to the main loop. The batch size is 1 by default. | |
84 void SetWorkBatchSize(int work_batch_size); | |
85 | |
86 // When given a non-null TaskTimeTracker, the TaskQueueManager calls its | |
87 // ReportTaskTime method for every top level task. The task_time_tracker must | |
88 // outlive this object, or be removed via SetTaskTimeTracker(nullptr). | |
89 void SetTaskTimeTracker(TaskTimeTracker* task_time_tracker) { | |
90 task_time_tracker_ = task_time_tracker; | |
91 } | |
92 | |
93 // These functions can only be called on the same thread that the task queue | |
94 // manager executes its tasks on. | |
95 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
96 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
97 | |
98 // Returns true if any task from a monitored task queue was was run since the | |
99 // last call to GetAndClearSystemIsQuiescentBit. | |
100 bool GetAndClearSystemIsQuiescentBit(); | |
101 | |
102 // Creates a task queue with the given |spec|. Must be called on the thread | |
103 // this class was created on. | |
104 scoped_refptr<internal::TaskQueueImpl> NewTaskQueue( | |
105 const TaskQueue::Spec& spec); | |
106 | |
107 class SCHEDULER_EXPORT Observer { | |
108 public: | |
109 virtual ~Observer() {} | |
110 | |
111 // Called when |queue| is unregistered. | |
112 virtual void OnUnregisterTaskQueue( | |
113 const scoped_refptr<TaskQueue>& queue) = 0; | |
114 | |
115 // Called when the manager tried to execute a task from a disabled | |
116 // queue. See TaskQueue::Spec::SetShouldReportWhenExecutionBlocked. | |
117 virtual void OnTriedToExecuteBlockedTask(const TaskQueue& queue, | |
118 const base::PendingTask& task) = 0; | |
119 }; | |
120 | |
121 // Called once to set the Observer. This function is called on the main | |
122 // thread. If |observer| is null, then no callbacks will occur. | |
123 // Note |observer| is expected to outlive the SchedulerHelper. | |
124 void SetObserver(Observer* observer); | |
125 | |
126 // Returns the delegate used by the TaskQueueManager. | |
127 const scoped_refptr<TaskQueueManagerDelegate>& delegate() const; | |
128 | |
129 // Time domains must be registered for the task queues to get updated. | |
130 void RegisterTimeDomain(TimeDomain* time_domain); | |
131 void UnregisterTimeDomain(TimeDomain* time_domain); | |
132 | |
133 RealTimeDomain* real_time_domain() const { return real_time_domain_.get(); } | |
134 | |
135 LazyNow CreateLazyNow() const; | |
136 | |
137 // Returns the currently executing TaskQueue if any. Must be called on the | |
138 // thread this class was created on. | |
139 TaskQueue* currently_executing_task_queue() const { | |
140 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
141 return currently_executing_task_queue_; | |
142 } | |
143 | |
144 private: | |
145 friend class LazyNow; | |
146 friend class internal::TaskQueueImpl; | |
147 friend class TaskQueueManagerTest; | |
148 | |
149 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { | |
150 private: | |
151 friend class base::RefCounted<DeletionSentinel>; | |
152 ~DeletionSentinel() {} | |
153 }; | |
154 | |
155 // Unregisters a TaskQueue previously created by |NewTaskQueue()|. | |
156 // NOTE we have to flush the queue from |newly_updatable_| which means as a | |
157 // side effect MoveNewlyUpdatableQueuesIntoUpdatableQueueSet is called by this | |
158 // function. | |
159 void UnregisterTaskQueue(scoped_refptr<internal::TaskQueueImpl> task_queue); | |
160 | |
161 // TaskQueueSelector::Observer implementation: | |
162 void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) override; | |
163 void OnTriedToSelectBlockedWorkQueue( | |
164 internal::WorkQueue* work_queue) override; | |
165 | |
166 // Called by the task queue to register a new pending task. | |
167 void DidQueueTask(const internal::TaskQueueImpl::Task& pending_task); | |
168 | |
169 // Use the selector to choose a pending task and run it. | |
170 void DoWork(base::TimeTicks run_time, bool from_main_thread); | |
171 | |
172 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue. | |
173 // Reloads any empty work queues which have automatic pumping enabled and | |
174 // which are eligible to be auto pumped based on the |previous_task| which was | |
175 // run and |should_trigger_wakeup|. Call with an empty |previous_task| if no | |
176 // task was just run. | |
177 void UpdateWorkQueues(bool should_trigger_wakeup, | |
178 const internal::TaskQueueImpl::Task* previous_task, | |
179 LazyNow lazy_now); | |
180 | |
181 // Chooses the next work queue to service. Returns true if |out_queue| | |
182 // indicates the queue from which the next task should be run, false to | |
183 // avoid running any tasks. | |
184 bool SelectWorkQueueToService(internal::WorkQueue** out_work_queue); | |
185 | |
186 // Runs a single nestable task from the |queue|. On exit, |out_task| will | |
187 // contain the task which was executed. Non-nestable task are reposted on the | |
188 // run loop. The queue must not be empty. | |
189 enum class ProcessTaskResult { | |
190 DEFERRED, | |
191 EXECUTED, | |
192 TASK_QUEUE_MANAGER_DELETED | |
193 }; | |
194 ProcessTaskResult ProcessTaskFromWorkQueue( | |
195 internal::WorkQueue* work_queue, | |
196 internal::TaskQueueImpl::Task* out_previous_task); | |
197 | |
198 bool RunsTasksOnCurrentThread() const; | |
199 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
200 const base::Closure& task, | |
201 base::TimeDelta delay); | |
202 | |
203 internal::EnqueueOrder GetNextSequenceNumber(); | |
204 | |
205 // Calls MaybeAdvanceTime on all time domains and returns true if one of them | |
206 // was able to advance. | |
207 bool TryAdvanceTimeDomains(); | |
208 | |
209 void MaybeRecordTaskDelayHistograms( | |
210 const internal::TaskQueueImpl::Task& pending_task, | |
211 const internal::TaskQueueImpl* queue); | |
212 | |
213 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> | |
214 AsValueWithSelectorResult(bool should_run, | |
215 internal::WorkQueue* selected_work_queue) const; | |
216 | |
217 std::set<TimeDomain*> time_domains_; | |
218 std::unique_ptr<RealTimeDomain> real_time_domain_; | |
219 | |
220 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_; | |
221 | |
222 // We have to be careful when deleting a queue because some of the code uses | |
223 // raw pointers and doesn't expect the rug to be pulled out from underneath. | |
224 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_to_delete_; | |
225 | |
226 internal::EnqueueOrderGenerator enqueue_order_generator_; | |
227 base::debug::TaskAnnotator task_annotator_; | |
228 | |
229 base::ThreadChecker main_thread_checker_; | |
230 scoped_refptr<TaskQueueManagerDelegate> delegate_; | |
231 internal::TaskQueueSelector selector_; | |
232 | |
233 base::Closure from_main_thread_immediate_do_work_closure_; | |
234 base::Closure from_other_thread_immediate_do_work_closure_; | |
235 | |
236 bool task_was_run_on_quiescence_monitored_queue_; | |
237 | |
238 // To reduce locking overhead we track pending calls to DoWork separately for | |
239 // the main thread and other threads. | |
240 std::set<base::TimeTicks> main_thread_pending_wakeups_; | |
241 | |
242 // Protects |other_thread_pending_wakeups_|. | |
243 mutable base::Lock other_thread_lock_; | |
244 std::set<base::TimeTicks> other_thread_pending_wakeups_; | |
245 | |
246 int work_batch_size_; | |
247 size_t task_count_; | |
248 | |
249 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; | |
250 | |
251 TaskTimeTracker* task_time_tracker_; // NOT OWNED | |
252 | |
253 const char* tracing_category_; | |
254 const char* disabled_by_default_tracing_category_; | |
255 const char* disabled_by_default_verbose_tracing_category_; | |
256 | |
257 internal::TaskQueueImpl* currently_executing_task_queue_; // NOT OWNED | |
258 | |
259 Observer* observer_; // NOT OWNED | |
260 scoped_refptr<DeletionSentinel> deletion_sentinel_; | |
261 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | |
262 | |
263 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | |
264 }; | |
265 | |
266 } // namespace scheduler | |
267 | |
268 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | |
OLD | NEW |