Chromium Code Reviews| Index: content/renderer/scheduler/renderer_scheduler_selector.cc |
| diff --git a/content/renderer/scheduler/renderer_scheduler_selector.cc b/content/renderer/scheduler/renderer_scheduler_selector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f9046883aab98d225929dbc95c76f67a742b27bf |
| --- /dev/null |
| +++ b/content/renderer/scheduler/renderer_scheduler_selector.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/scheduler/renderer_scheduler_selector.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/pending_task.h" |
| + |
| +namespace content { |
| + |
| +RendererSchedulerSelector::RendererSchedulerSelector() { |
| +} |
| + |
| +RendererSchedulerSelector::~RendererSchedulerSelector() { |
| +} |
| + |
| +void RendererSchedulerSelector::RegisterWorkQueues( |
| + const std::vector<const base::TaskQueue*>& work_queues) { |
| + main_thread_checker_.CalledOnValidThread(); |
| + DCHECK(work_queues.size() < queue_priorities_[kNormalPriority] * CHAR_BIT); |
|
Sami
2014/10/22 13:26:06
Still missing the sizeof here? Also, DCHECK_LT.
rmcilroy
2014/10/23 14:54:49
Sorry missed your comment here last time - done.
|
| + work_queues_ = work_queues; |
| + // By default, all work queues are set to normal priority. |
| + queue_priorities_[kControlPriority] = 0; |
| + queue_priorities_[kHighPriority] = 0; |
| + queue_priorities_[kNormalPriority] = (1 << work_queues.size()) - 1; |
| + queue_priorities_[kBestEffortPriority] = 0; |
| +} |
| + |
| +RendererSchedulerSelector::QueuePriority |
| +RendererSchedulerSelector::NextPriority(QueuePriority priority) { |
| + DCHECK(priority < kQueuePriorityCount); |
|
Sami
2014/10/22 13:26:06
DCHECK_LT
rmcilroy
2014/10/23 14:54:49
Done.
|
| + return static_cast<QueuePriority>(static_cast<int>(priority) + 1); |
| +} |
| + |
| +size_t RendererSchedulerSelector::PrioritiesMaskFromQueueIndex( |
| + size_t queue_index) const { |
| + return 1 << queue_index; |
| +} |
| + |
| +void RendererSchedulerSelector::SetQueuePriority(size_t queue_index, |
| + QueuePriority priority) { |
| + main_thread_checker_.CalledOnValidThread(); |
| + DCHECK(queue_index < work_queues_.size()); |
|
Sami
2014/10/22 13:26:06
DCHECK_LT again (2x).
rmcilroy
2014/10/23 14:54:50
Done.
|
| + DCHECK(priority < kQueuePriorityCount); |
| + DisableQueue(queue_index); |
| + queue_priorities_[priority] |= PrioritiesMaskFromQueueIndex(queue_index); |
| +} |
| + |
| +void RendererSchedulerSelector::EnableQueue(size_t queue_index, |
| + QueuePriority priority) { |
| + SetQueuePriority(queue_index, priority); |
| +} |
| + |
| +void RendererSchedulerSelector::DisableQueue(size_t queue_index) { |
| + main_thread_checker_.CalledOnValidThread(); |
| + DCHECK(queue_index < work_queues_.size()); |
|
Sami
2014/10/22 13:26:06
DCHECK_LT
rmcilroy
2014/10/23 14:54:49
Done.
|
| + for (QueuePriority priority = kControlPriority; |
| + priority < kQueuePriorityCount; |
| + priority = NextPriority(priority)) { |
| + queue_priorities_[priority] &= ~(PrioritiesMaskFromQueueIndex(queue_index)); |
|
Sami
2014/10/22 13:26:06
nit: No parens needed around the function call.
rmcilroy
2014/10/23 14:54:49
Done.
|
| + } |
| +} |
| + |
| +bool RendererSchedulerSelector::QueueEnabledWithPriority( |
| + size_t queue_index, |
| + QueuePriority priority) const { |
| + DCHECK(queue_index < work_queues_.size()); |
| + DCHECK(priority < kQueuePriorityCount); |
| + return queue_priorities_[priority] & |
| + PrioritiesMaskFromQueueIndex(queue_index); |
| +} |
| + |
| +bool RendererSchedulerSelector::ChooseOldestWithPriority( |
| + QueuePriority priority, |
| + size_t* out_queue_index) const { |
| + bool found_non_empty_queue = false; |
| + size_t chosen_queue = 0; |
| + for (size_t i = 0; i < work_queues_.size(); i++) { |
| + if (work_queues_[i]->empty() || |
| + !QueueEnabledWithPriority(i, priority)) { |
| + continue; |
| + } |
| + if (!found_non_empty_queue || |
| + work_queues_[i]->front() < work_queues_[chosen_queue]->front()) { |
| + found_non_empty_queue = true; |
| + chosen_queue = i; |
| + } |
| + } |
| + |
| + if (found_non_empty_queue) { |
| + *out_queue_index = chosen_queue; |
| + } |
| + return found_non_empty_queue; |
| +} |
| + |
| +bool RendererSchedulerSelector::SelectWorkQueueToService( |
| + size_t* out_queue_index) { |
| + main_thread_checker_.CalledOnValidThread(); |
| + DCHECK(work_queues_.size()); |
| + // Always service the control queue if it has any work. |
| + if (ChooseOldestWithPriority(kControlPriority, out_queue_index)) { |
| + return true; |
| + } |
| + // Select from the normal priority queue if we are starving it. |
| + if (normal_queue_starvation_count_ >= kMaxStarvationTasks && |
| + ChooseOldestWithPriority(kNormalPriority, out_queue_index)) { |
| + normal_queue_starvation_count_ = 0; |
| + return true; |
| + } |
| + // Otherwise choose in priority order. |
| + for (QueuePriority priority = kHighPriority; |
| + priority < kQueuePriorityCount; |
| + priority = NextPriority(priority)) { |
| + if (ChooseOldestWithPriority(priority, out_queue_index)) { |
| + if (priority == kHighPriority) { |
| + normal_queue_starvation_count_++; |
| + } else { |
| + normal_queue_starvation_count_ = 0; |
| + } |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace content |