Chromium Code Reviews| 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 #include "content/renderer/scheduler/renderer_scheduler_selector.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/pending_task.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 RendererSchedulerSelector::RendererSchedulerSelector() { | |
| 13 } | |
| 14 | |
| 15 RendererSchedulerSelector::~RendererSchedulerSelector() { | |
| 16 } | |
| 17 | |
| 18 void RendererSchedulerSelector::RegisterWorkQueues( | |
| 19 const std::vector<const base::TaskQueue*>& work_queues) { | |
| 20 main_thread_checker_.CalledOnValidThread(); | |
| 21 DCHECK_LT(work_queues.size(), | |
|
alexclarke
2014/10/24 14:37:26
Maybe add a comment, I had to read this several ti
rmcilroy
2014/10/24 14:58:29
No longer applicable.
| |
| 22 sizeof(queue_priorities_[kNormalPriority]) * CHAR_BIT); | |
| 23 work_queues_ = work_queues; | |
| 24 // By default, all work queues are set to normal priority. | |
| 25 queue_priorities_[kControlPriority] = 0; | |
| 26 queue_priorities_[kHighPriority] = 0; | |
| 27 queue_priorities_[kNormalPriority] = (1 << work_queues.size()) - 1; | |
|
alexclarke
2014/10/24 14:37:26
Is there a reason to use work_queues.size() instea
rmcilroy
2014/10/24 14:58:29
No longer applicable (plus work_queues.size() is n
| |
| 28 queue_priorities_[kBestEffortPriority] = 0; | |
| 29 } | |
| 30 | |
| 31 size_t RendererSchedulerSelector::PrioritiesMaskFromQueueIndex( | |
| 32 size_t queue_index) const { | |
| 33 return 1 << queue_index; | |
| 34 } | |
| 35 | |
| 36 void RendererSchedulerSelector::SetQueuePriority(size_t queue_index, | |
| 37 QueuePriority priority) { | |
| 38 main_thread_checker_.CalledOnValidThread(); | |
| 39 DCHECK_LT(queue_index, work_queues_.size()); | |
| 40 DCHECK_LT(priority, kQueuePriorityCount); | |
| 41 DisableQueue(queue_index); | |
| 42 queue_priorities_[priority] |= PrioritiesMaskFromQueueIndex(queue_index); | |
| 43 } | |
| 44 | |
| 45 void RendererSchedulerSelector::EnableQueue(size_t queue_index, | |
| 46 QueuePriority priority) { | |
| 47 SetQueuePriority(queue_index, priority); | |
| 48 } | |
| 49 | |
| 50 void RendererSchedulerSelector::DisableQueue(size_t queue_index) { | |
| 51 main_thread_checker_.CalledOnValidThread(); | |
| 52 DCHECK_LT(queue_index, work_queues_.size()); | |
| 53 | |
| 54 for (auto priority: { kControlPriority, | |
|
alexclarke
2014/10/24 14:37:26
fyi: gcc doesn't like this, although I do (apart f
rmcilroy
2014/10/24 14:58:29
I changed it to a static array.
rmcilroy
2014/10/24 14:58:29
Modified this to be a ranged for on a static const
| |
| 55 kHighPriority, | |
| 56 kNormalPriority, | |
| 57 kBestEffortPriority }) { | |
| 58 queue_priorities_[priority] &= ~PrioritiesMaskFromQueueIndex(queue_index); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 bool RendererSchedulerSelector::QueueEnabledWithPriority( | |
| 63 size_t queue_index, | |
| 64 QueuePriority priority) const { | |
| 65 DCHECK_LT(queue_index, work_queues_.size()); | |
| 66 DCHECK_LT(priority, kQueuePriorityCount); | |
| 67 return queue_priorities_[priority] & | |
| 68 PrioritiesMaskFromQueueIndex(queue_index); | |
| 69 } | |
| 70 | |
| 71 bool RendererSchedulerSelector::ChooseOldestWithPriority( | |
| 72 QueuePriority priority, | |
| 73 size_t* out_queue_index) const { | |
| 74 bool found_non_empty_queue = false; | |
| 75 size_t chosen_queue = 0; | |
| 76 for (size_t i = 0; i < work_queues_.size(); i++) { | |
| 77 if (work_queues_[i]->empty() || | |
| 78 !QueueEnabledWithPriority(i, priority)) { | |
| 79 continue; | |
| 80 } | |
| 81 // Note: the comparison between |chosen_queue| and |i| is correct due to the | |
| 82 // fact that the PendingTask operator inverts comparison for priority queue. | |
| 83 if (!found_non_empty_queue || | |
| 84 work_queues_[chosen_queue]->front() < work_queues_[i]->front()) { | |
| 85 found_non_empty_queue = true; | |
| 86 chosen_queue = i; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 if (found_non_empty_queue) { | |
| 91 *out_queue_index = chosen_queue; | |
| 92 } | |
| 93 return found_non_empty_queue; | |
| 94 } | |
| 95 | |
| 96 bool RendererSchedulerSelector::SelectWorkQueueToService( | |
| 97 size_t* out_queue_index) { | |
| 98 main_thread_checker_.CalledOnValidThread(); | |
| 99 DCHECK(work_queues_.size()); | |
| 100 // Always service the control queue if it has any work. | |
| 101 if (ChooseOldestWithPriority(kControlPriority, out_queue_index)) { | |
| 102 return true; | |
| 103 } | |
| 104 // Select from the normal priority queue if we are starving it. | |
| 105 if (normal_queue_starvation_count_ >= kMaxStarvationTasks && | |
| 106 ChooseOldestWithPriority(kNormalPriority, out_queue_index)) { | |
| 107 normal_queue_starvation_count_ = 0; | |
| 108 return true; | |
| 109 } | |
| 110 // Otherwise choose in priority order. | |
| 111 for (auto priority: { kHighPriority, kNormalPriority, kBestEffortPriority }) { | |
| 112 if (ChooseOldestWithPriority(priority, out_queue_index)) { | |
| 113 if (priority == kHighPriority) { | |
| 114 normal_queue_starvation_count_++; | |
| 115 } else { | |
| 116 normal_queue_starvation_count_ = 0; | |
| 117 } | |
| 118 return true; | |
| 119 } | |
| 120 } | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 } // namespace content | |
| OLD | NEW |