OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/renderer/scheduler/task_queue_manager.h" | 5 #include "content/renderer/scheduler/task_queue_manager.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 base::TimeDelta delay) override { | 36 base::TimeDelta delay) override { |
37 return PostDelayedTaskImpl(from_here, task, delay, TaskType::NON_NESTABLE); | 37 return PostDelayedTaskImpl(from_here, task, delay, TaskType::NON_NESTABLE); |
38 } | 38 } |
39 | 39 |
40 bool IsQueueEmpty() const; | 40 bool IsQueueEmpty() const; |
41 | 41 |
42 void SetPumpPolicy(TaskQueueManager::PumpPolicy pump_policy); | 42 void SetPumpPolicy(TaskQueueManager::PumpPolicy pump_policy); |
43 void PumpQueue(); | 43 void PumpQueue(); |
44 | 44 |
45 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, | 45 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, |
46 TaskQueueManager::WorkQueueUpdateEventType event_type); | 46 const base::PendingTask* previous_task); |
47 base::PendingTask TakeTaskFromWorkQueue(); | 47 base::PendingTask TakeTaskFromWorkQueue(); |
48 | 48 |
49 void WillDeleteTaskQueueManager(); | 49 void WillDeleteTaskQueueManager(); |
50 | 50 |
51 base::TaskQueue& work_queue() { return work_queue_; } | 51 base::TaskQueue& work_queue() { return work_queue_; } |
52 | 52 |
53 void set_name(const char* name) { name_ = name; } | 53 void set_name(const char* name) { name_ = name; } |
54 | 54 |
55 void AsValueInto(base::trace_event::TracedValue* state) const; | 55 void AsValueInto(base::trace_event::TracedValue* state) const; |
56 | 56 |
57 private: | 57 private: |
58 enum class TaskType { | 58 enum class TaskType { |
59 NORMAL, | 59 NORMAL, |
60 NON_NESTABLE, | 60 NON_NESTABLE, |
61 }; | 61 }; |
62 | 62 |
63 ~TaskQueue() override; | 63 ~TaskQueue() override; |
64 | 64 |
65 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, | 65 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, |
66 const base::Closure& task, | 66 const base::Closure& task, |
67 base::TimeDelta delay, | 67 base::TimeDelta delay, |
68 TaskType task_type); | 68 TaskType task_type); |
69 | 69 |
70 // Adds a task at the end of the incoming task queue and schedules a call to | 70 // Adds a task at the end of the incoming task queue and schedules a call to |
71 // TaskQueueManager::DoWork() if the incoming queue was empty and automatic | 71 // TaskQueueManager::DoWork() if the incoming queue was empty and automatic |
72 // pumping is enabled. Can be called on an arbitrary thread. | 72 // pumping is enabled. Can be called on an arbitrary thread. |
73 void EnqueueTask(const base::PendingTask& pending_task); | 73 void EnqueueTask(const base::PendingTask& pending_task); |
74 | 74 |
75 void PumpQueueLocked(); | 75 void PumpQueueLocked(); |
76 bool ShouldAutoPumpQueueLocked( | 76 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); |
77 TaskQueueManager::WorkQueueUpdateEventType event_type); | 77 bool ShouldAutoPumpQueueLocked(const base::PendingTask* previous_task); |
78 void EnqueueTaskLocked(const base::PendingTask& pending_task); | 78 void EnqueueTaskLocked(const base::PendingTask& pending_task); |
79 | 79 |
80 void TraceQueueSize(bool is_locked) const; | 80 void TraceQueueSize(bool is_locked) const; |
81 static const char* PumpPolicyToString( | 81 static const char* PumpPolicyToString( |
82 TaskQueueManager::PumpPolicy pump_policy); | 82 TaskQueueManager::PumpPolicy pump_policy); |
83 static void QueueAsValueInto(const base::TaskQueue& queue, | 83 static void QueueAsValueInto(const base::TaskQueue& queue, |
84 base::trace_event::TracedValue* state); | 84 base::trace_event::TracedValue* state); |
85 static void TaskAsValueInto(const base::PendingTask& task, | 85 static void TaskAsValueInto(const base::PendingTask& task, |
86 base::trace_event::TracedValue* state); | 86 base::trace_event::TracedValue* state); |
87 | 87 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 bool TaskQueue::IsQueueEmpty() const { | 146 bool TaskQueue::IsQueueEmpty() const { |
147 if (!work_queue_.empty()) | 147 if (!work_queue_.empty()) |
148 return false; | 148 return false; |
149 | 149 |
150 { | 150 { |
151 base::AutoLock lock(lock_); | 151 base::AutoLock lock(lock_); |
152 return incoming_queue_.empty(); | 152 return incoming_queue_.empty(); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
| 156 bool TaskQueue::TaskIsOlderThanQueuedTasks(const base::PendingTask* task) { |
| 157 lock_.AssertAcquired(); |
| 158 // A null task is passed when UpdateQueue is called before any task is run. |
| 159 // In this case we don't want to pump an after_wakeup queue, so return true |
| 160 // here. |
| 161 if (!task) |
| 162 return true; |
| 163 |
| 164 // Return false if there are no task in the incoming queue. |
| 165 if (incoming_queue_.empty()) |
| 166 return false; |
| 167 |
| 168 base::PendingTask oldest_queued_task = incoming_queue_.front(); |
| 169 DCHECK(oldest_queued_task.delayed_run_time.is_null()); |
| 170 DCHECK(task->delayed_run_time.is_null()); |
| 171 |
| 172 // Note: the comparison is correct due to the fact that the PendingTask |
| 173 // operator inverts its comparison operation in order to work well in a heap |
| 174 // based priority queue. |
| 175 return oldest_queued_task < *task; |
| 176 } |
| 177 |
156 bool TaskQueue::ShouldAutoPumpQueueLocked( | 178 bool TaskQueue::ShouldAutoPumpQueueLocked( |
157 TaskQueueManager::WorkQueueUpdateEventType event_type) { | 179 const base::PendingTask* previous_task) { |
158 lock_.AssertAcquired(); | 180 lock_.AssertAcquired(); |
159 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) | 181 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) |
160 return false; | 182 return false; |
161 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && | 183 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && |
162 event_type != TaskQueueManager::WorkQueueUpdateEventType::AFTER_WAKEUP) | 184 TaskIsOlderThanQueuedTasks(previous_task)) |
163 return false; | 185 return false; |
164 if (incoming_queue_.empty()) | 186 if (incoming_queue_.empty()) |
165 return false; | 187 return false; |
166 return true; | 188 return true; |
167 } | 189 } |
168 | 190 |
169 bool TaskQueue::UpdateWorkQueue( | 191 bool TaskQueue::UpdateWorkQueue( |
170 base::TimeTicks* next_pending_delayed_task, | 192 base::TimeTicks* next_pending_delayed_task, |
171 TaskQueueManager::WorkQueueUpdateEventType event_type) { | 193 const base::PendingTask* previous_task) { |
172 if (!work_queue_.empty()) | 194 if (!work_queue_.empty()) |
173 return true; | 195 return true; |
174 | 196 |
175 { | 197 { |
176 base::AutoLock lock(lock_); | 198 base::AutoLock lock(lock_); |
177 if (!delayed_task_run_times_.empty()) { | 199 if (!delayed_task_run_times_.empty()) { |
178 *next_pending_delayed_task = | 200 *next_pending_delayed_task = |
179 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); | 201 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); |
180 } | 202 } |
181 if (!ShouldAutoPumpQueueLocked(event_type)) | 203 if (!ShouldAutoPumpQueueLocked(previous_task)) |
182 return false; | 204 return false; |
183 work_queue_.Swap(&incoming_queue_); | 205 work_queue_.Swap(&incoming_queue_); |
184 TraceQueueSize(true); | 206 TraceQueueSize(true); |
185 return true; | 207 return true; |
186 } | 208 } |
187 } | 209 } |
188 | 210 |
189 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { | 211 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { |
190 base::PendingTask pending_task = work_queue_.front(); | 212 base::PendingTask pending_task = work_queue_.front(); |
191 work_queue_.pop(); | 213 work_queue_.pop(); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 } | 398 } |
377 | 399 |
378 void TaskQueueManager::PumpQueue(size_t queue_index) { | 400 void TaskQueueManager::PumpQueue(size_t queue_index) { |
379 DCHECK(main_thread_checker_.CalledOnValidThread()); | 401 DCHECK(main_thread_checker_.CalledOnValidThread()); |
380 internal::TaskQueue* queue = Queue(queue_index); | 402 internal::TaskQueue* queue = Queue(queue_index); |
381 queue->PumpQueue(); | 403 queue->PumpQueue(); |
382 } | 404 } |
383 | 405 |
384 bool TaskQueueManager::UpdateWorkQueues( | 406 bool TaskQueueManager::UpdateWorkQueues( |
385 base::TimeTicks* next_pending_delayed_task, | 407 base::TimeTicks* next_pending_delayed_task, |
386 WorkQueueUpdateEventType event_type) { | 408 const base::PendingTask* previous_task) { |
387 // TODO(skyostil): This is not efficient when the number of queues grows very | 409 // TODO(skyostil): This is not efficient when the number of queues grows very |
388 // large due to the number of locks taken. Consider optimizing when we get | 410 // large due to the number of locks taken. Consider optimizing when we get |
389 // there. | 411 // there. |
390 DCHECK(main_thread_checker_.CalledOnValidThread()); | 412 DCHECK(main_thread_checker_.CalledOnValidThread()); |
391 bool has_work = false; | 413 bool has_work = false; |
392 for (auto& queue : queues_) { | 414 for (auto& queue : queues_) { |
393 has_work |= queue->UpdateWorkQueue(next_pending_delayed_task, event_type); | 415 has_work |= queue->UpdateWorkQueue(next_pending_delayed_task, |
| 416 previous_task); |
394 if (!queue->work_queue().empty()) { | 417 if (!queue->work_queue().empty()) { |
395 // Currently we should not be getting tasks with delayed run times in any | 418 // Currently we should not be getting tasks with delayed run times in any |
396 // of the work queues. | 419 // of the work queues. |
397 DCHECK(queue->work_queue().front().delayed_run_time.is_null()); | 420 DCHECK(queue->work_queue().front().delayed_run_time.is_null()); |
398 } | 421 } |
399 } | 422 } |
400 return has_work; | 423 return has_work; |
401 } | 424 } |
402 | 425 |
403 void TaskQueueManager::MaybePostDoWorkOnMainRunner() { | 426 void TaskQueueManager::MaybePostDoWorkOnMainRunner() { |
(...skipping 15 matching lines...) Expand all Loading... |
419 void TaskQueueManager::DoWork(bool posted_from_main_thread) { | 442 void TaskQueueManager::DoWork(bool posted_from_main_thread) { |
420 if (posted_from_main_thread) { | 443 if (posted_from_main_thread) { |
421 pending_dowork_count_--; | 444 pending_dowork_count_--; |
422 DCHECK_GE(pending_dowork_count_, 0); | 445 DCHECK_GE(pending_dowork_count_, 0); |
423 } | 446 } |
424 DCHECK(main_thread_checker_.CalledOnValidThread()); | 447 DCHECK(main_thread_checker_.CalledOnValidThread()); |
425 | 448 |
426 base::TimeTicks next_pending_delayed_task( | 449 base::TimeTicks next_pending_delayed_task( |
427 base::TimeTicks::FromInternalValue(kMaxTimeTicks)); | 450 base::TimeTicks::FromInternalValue(kMaxTimeTicks)); |
428 | 451 |
429 if (!UpdateWorkQueues(&next_pending_delayed_task, | 452 // Pass nullptr to UpdateWorkQueues here to prevent waking up an |
430 WorkQueueUpdateEventType::BEFORE_WAKEUP)) | 453 // pump-after-wakeup queue. |
| 454 if (!UpdateWorkQueues(&next_pending_delayed_task, nullptr)) |
431 return; | 455 return; |
432 | 456 |
433 base::PendingTask previous_task((tracked_objects::Location()), | 457 base::PendingTask previous_task((tracked_objects::Location()), |
434 (base::Closure())); | 458 (base::Closure())); |
435 for (int i = 0; i < work_batch_size_; i++) { | 459 for (int i = 0; i < work_batch_size_; i++) { |
436 // Interrupt the work batch if we should run the next delayed task. | 460 // Interrupt the work batch if we should run the next delayed task. |
437 if (i > 0 && next_pending_delayed_task.ToInternalValue() != kMaxTimeTicks && | 461 if (i > 0 && next_pending_delayed_task.ToInternalValue() != kMaxTimeTicks && |
438 Now() >= next_pending_delayed_task) | 462 Now() >= next_pending_delayed_task) |
439 return; | 463 return; |
440 | 464 |
441 size_t queue_index; | 465 size_t queue_index; |
442 if (!SelectWorkQueueToService(&queue_index)) | 466 if (!SelectWorkQueueToService(&queue_index)) |
443 return; | 467 return; |
444 // Note that this function won't post another call to DoWork if one is | 468 // Note that this function won't post another call to DoWork if one is |
445 // already pending, so it is safe to call it in a loop. | 469 // already pending, so it is safe to call it in a loop. |
446 MaybePostDoWorkOnMainRunner(); | 470 MaybePostDoWorkOnMainRunner(); |
447 ProcessTaskFromWorkQueue(queue_index, i > 0, &previous_task); | 471 ProcessTaskFromWorkQueue(queue_index, i > 0, &previous_task); |
448 | 472 |
449 if (!UpdateWorkQueues(&next_pending_delayed_task, | 473 if (!UpdateWorkQueues(&next_pending_delayed_task, &previous_task)) |
450 WorkQueueUpdateEventType::AFTER_WAKEUP)) | |
451 return; | 474 return; |
452 } | 475 } |
453 } | 476 } |
454 | 477 |
455 bool TaskQueueManager::SelectWorkQueueToService(size_t* out_queue_index) { | 478 bool TaskQueueManager::SelectWorkQueueToService(size_t* out_queue_index) { |
456 bool should_run = selector_->SelectWorkQueueToService(out_queue_index); | 479 bool should_run = selector_->SelectWorkQueueToService(out_queue_index); |
457 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 480 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
458 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager", this, | 481 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager", this, |
459 AsValueWithSelectorResult(should_run, *out_queue_index)); | 482 AsValueWithSelectorResult(should_run, *out_queue_index)); |
460 return should_run; | 483 return should_run; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 state->EndArray(); | 577 state->EndArray(); |
555 state->BeginDictionary("selector"); | 578 state->BeginDictionary("selector"); |
556 selector_->AsValueInto(state.get()); | 579 selector_->AsValueInto(state.get()); |
557 state->EndDictionary(); | 580 state->EndDictionary(); |
558 if (should_run) | 581 if (should_run) |
559 state->SetInteger("selected_queue", selected_queue); | 582 state->SetInteger("selected_queue", selected_queue); |
560 return state; | 583 return state; |
561 } | 584 } |
562 | 585 |
563 } // namespace content | 586 } // namespace content |
OLD | NEW |