Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: content/child/scheduler/task_queue_selector_impl.cc

Issue 1025323003: Introduce a SchedulerHelper in content/child/scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added the delegate Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/renderer_task_queue_selector.h" 5 #include "content/child/scheduler/task_queue_selector_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/pending_task.h" 8 #include "base/pending_task.h"
9 #include "base/trace_event/trace_event_argument.h" 9 #include "base/trace_event/trace_event_argument.h"
10 10
11 namespace content { 11 namespace content {
12 12
13 RendererTaskQueueSelector::RendererTaskQueueSelector() : starvation_count_(0) { 13 TaskQueueSelectorImpl::TaskQueueSelectorImpl() : starvation_count_(0) {
14 } 14 }
15 15
16 RendererTaskQueueSelector::~RendererTaskQueueSelector() { 16 TaskQueueSelectorImpl::~TaskQueueSelectorImpl() {
17 } 17 }
18 18
19 void RendererTaskQueueSelector::RegisterWorkQueues( 19 void TaskQueueSelectorImpl::RegisterWorkQueues(
20 const std::vector<const base::TaskQueue*>& work_queues) { 20 const std::vector<const base::TaskQueue*>& work_queues) {
21 DCHECK(main_thread_checker_.CalledOnValidThread()); 21 DCHECK(main_thread_checker_.CalledOnValidThread());
22 work_queues_ = work_queues; 22 work_queues_ = work_queues;
23 for (auto& queue_priority : queue_priorities_) { 23 for (auto& queue_priority : queue_priorities_) {
24 queue_priority.clear(); 24 queue_priority.clear();
25 } 25 }
26 26
27 // By default, all work queues are set to normal priority. 27 // By default, all work queues are set to normal priority.
28 for (size_t i = 0; i < work_queues.size(); i++) { 28 for (size_t i = 0; i < work_queues.size(); i++) {
29 queue_priorities_[NORMAL_PRIORITY].insert(i); 29 queue_priorities_[NORMAL_PRIORITY].insert(i);
30 } 30 }
31 } 31 }
32 32
33 void RendererTaskQueueSelector::SetQueuePriority(size_t queue_index, 33 void TaskQueueSelectorImpl::SetQueuePriority(size_t queue_index,
34 QueuePriority priority) { 34 QueuePriority priority) {
35 DCHECK(main_thread_checker_.CalledOnValidThread()); 35 DCHECK(main_thread_checker_.CalledOnValidThread());
36 DCHECK_LT(queue_index, work_queues_.size()); 36 DCHECK_LT(queue_index, work_queues_.size());
37 DCHECK_LT(priority, QUEUE_PRIORITY_COUNT); 37 DCHECK_LT(priority, QUEUE_PRIORITY_COUNT);
38 DisableQueue(queue_index); 38 DisableQueue(queue_index);
39 queue_priorities_[priority].insert(queue_index); 39 queue_priorities_[priority].insert(queue_index);
40 } 40 }
41 41
42 void RendererTaskQueueSelector::EnableQueue(size_t queue_index, 42 void TaskQueueSelectorImpl::EnableQueue(size_t queue_index,
43 QueuePriority priority) { 43 QueuePriority priority) {
44 SetQueuePriority(queue_index, priority); 44 SetQueuePriority(queue_index, priority);
45 } 45 }
46 46
47 void RendererTaskQueueSelector::DisableQueue(size_t queue_index) { 47 void TaskQueueSelectorImpl::DisableQueue(size_t queue_index) {
48 DCHECK(main_thread_checker_.CalledOnValidThread()); 48 DCHECK(main_thread_checker_.CalledOnValidThread());
49 DCHECK_LT(queue_index, work_queues_.size()); 49 DCHECK_LT(queue_index, work_queues_.size());
50 for (auto& queue_priority : queue_priorities_) { 50 for (auto& queue_priority : queue_priorities_) {
51 queue_priority.erase(queue_index); 51 queue_priority.erase(queue_index);
52 } 52 }
53 } 53 }
54 54
55 bool RendererTaskQueueSelector::IsQueueEnabled(size_t queue_index) const { 55 bool TaskQueueSelectorImpl::IsQueueEnabled(size_t queue_index) const {
56 DCHECK(main_thread_checker_.CalledOnValidThread()); 56 DCHECK(main_thread_checker_.CalledOnValidThread());
57 DCHECK_LT(queue_index, work_queues_.size()); 57 DCHECK_LT(queue_index, work_queues_.size());
58 for (const auto& queue_priority : queue_priorities_) { 58 for (const auto& queue_priority : queue_priorities_) {
59 if (queue_priority.find(queue_index) != queue_priority.end()) 59 if (queue_priority.find(queue_index) != queue_priority.end())
60 return true; 60 return true;
61 } 61 }
62 return false; 62 return false;
63 } 63 }
64 64
65 bool RendererTaskQueueSelector::IsOlder(const base::TaskQueue* queueA, 65 bool TaskQueueSelectorImpl::IsOlder(const base::TaskQueue* queueA,
66 const base::TaskQueue* queueB) { 66 const base::TaskQueue* queueB) {
67 // Note: the comparison is correct due to the fact that the PendingTask 67 // Note: the comparison is correct due to the fact that the PendingTask
68 // operator inverts its comparison operation in order to work well in a heap 68 // operator inverts its comparison operation in order to work well in a heap
69 // based priority queue. 69 // based priority queue.
70 return queueB->front() < queueA->front(); 70 return queueB->front() < queueA->front();
71 } 71 }
72 72
73 RendererTaskQueueSelector::QueuePriority 73 TaskQueueSelectorImpl::QueuePriority TaskQueueSelectorImpl::NextPriority(
74 RendererTaskQueueSelector::NextPriority(QueuePriority priority) { 74 QueuePriority priority) {
75 DCHECK(priority < QUEUE_PRIORITY_COUNT); 75 DCHECK(priority < QUEUE_PRIORITY_COUNT);
76 return static_cast<QueuePriority>(static_cast<int>(priority) + 1); 76 return static_cast<QueuePriority>(static_cast<int>(priority) + 1);
77 } 77 }
78 78
79 bool RendererTaskQueueSelector::ChooseOldestWithPriority( 79 bool TaskQueueSelectorImpl::ChooseOldestWithPriority(
80 QueuePriority priority, 80 QueuePriority priority,
81 size_t* out_queue_index) const { 81 size_t* out_queue_index) const {
82 bool found_non_empty_queue = false; 82 bool found_non_empty_queue = false;
83 size_t chosen_queue = 0; 83 size_t chosen_queue = 0;
84 for (int queue_index : queue_priorities_[priority]) { 84 for (int queue_index : queue_priorities_[priority]) {
85 if (work_queues_[queue_index]->empty()) { 85 if (work_queues_[queue_index]->empty()) {
86 continue; 86 continue;
87 } 87 }
88 if (!found_non_empty_queue || 88 if (!found_non_empty_queue ||
89 IsOlder(work_queues_[queue_index], work_queues_[chosen_queue])) { 89 IsOlder(work_queues_[queue_index], work_queues_[chosen_queue])) {
90 found_non_empty_queue = true; 90 found_non_empty_queue = true;
91 chosen_queue = queue_index; 91 chosen_queue = queue_index;
92 } 92 }
93 } 93 }
94 94
95 if (found_non_empty_queue) { 95 if (found_non_empty_queue) {
96 *out_queue_index = chosen_queue; 96 *out_queue_index = chosen_queue;
97 } 97 }
98 return found_non_empty_queue; 98 return found_non_empty_queue;
99 } 99 }
100 100
101 bool RendererTaskQueueSelector::SelectWorkQueueToService( 101 bool TaskQueueSelectorImpl::SelectWorkQueueToService(size_t* out_queue_index) {
102 size_t* out_queue_index) {
103 DCHECK(main_thread_checker_.CalledOnValidThread()); 102 DCHECK(main_thread_checker_.CalledOnValidThread());
104 DCHECK(work_queues_.size()); 103 DCHECK(work_queues_.size());
105 // Always service the control queue if it has any work. 104 // Always service the control queue if it has any work.
106 if (ChooseOldestWithPriority(CONTROL_PRIORITY, out_queue_index)) { 105 if (ChooseOldestWithPriority(CONTROL_PRIORITY, out_queue_index)) {
107 DidSelectQueueWithPriority(CONTROL_PRIORITY); 106 DidSelectQueueWithPriority(CONTROL_PRIORITY);
108 return true; 107 return true;
109 } 108 }
110 // Select from the normal priority queue if we are starving it. 109 // Select from the normal priority queue if we are starving it.
111 if (starvation_count_ >= kMaxStarvationTasks && 110 if (starvation_count_ >= kMaxStarvationTasks &&
112 ChooseOldestWithPriority(NORMAL_PRIORITY, out_queue_index)) { 111 ChooseOldestWithPriority(NORMAL_PRIORITY, out_queue_index)) {
113 DidSelectQueueWithPriority(NORMAL_PRIORITY); 112 DidSelectQueueWithPriority(NORMAL_PRIORITY);
114 return true; 113 return true;
115 } 114 }
116 // Otherwise choose in priority order. 115 // Otherwise choose in priority order.
117 for (QueuePriority priority = HIGH_PRIORITY; priority < QUEUE_PRIORITY_COUNT; 116 for (QueuePriority priority = HIGH_PRIORITY; priority < QUEUE_PRIORITY_COUNT;
118 priority = NextPriority(priority)) { 117 priority = NextPriority(priority)) {
119 if (ChooseOldestWithPriority(priority, out_queue_index)) { 118 if (ChooseOldestWithPriority(priority, out_queue_index)) {
120 DidSelectQueueWithPriority(priority); 119 DidSelectQueueWithPriority(priority);
121 return true; 120 return true;
122 } 121 }
123 } 122 }
124 return false; 123 return false;
125 } 124 }
126 125
127 void RendererTaskQueueSelector::DidSelectQueueWithPriority( 126 void TaskQueueSelectorImpl::DidSelectQueueWithPriority(QueuePriority priority) {
128 QueuePriority priority) {
129 switch (priority) { 127 switch (priority) {
130 case CONTROL_PRIORITY: 128 case CONTROL_PRIORITY:
131 break; 129 break;
132 case HIGH_PRIORITY: 130 case HIGH_PRIORITY:
133 starvation_count_++; 131 starvation_count_++;
134 break; 132 break;
135 case NORMAL_PRIORITY: 133 case NORMAL_PRIORITY:
136 case BEST_EFFORT_PRIORITY: 134 case BEST_EFFORT_PRIORITY:
137 starvation_count_ = 0; 135 starvation_count_ = 0;
138 break; 136 break;
139 default: 137 default:
140 NOTREACHED(); 138 NOTREACHED();
141 } 139 }
142 } 140 }
143 141
144 // static 142 // static
145 const char* RendererTaskQueueSelector::PriorityToString( 143 const char* TaskQueueSelectorImpl::PriorityToString(QueuePriority priority) {
146 QueuePriority priority) {
147 switch (priority) { 144 switch (priority) {
148 case CONTROL_PRIORITY: 145 case CONTROL_PRIORITY:
149 return "control"; 146 return "control";
150 case HIGH_PRIORITY: 147 case HIGH_PRIORITY:
151 return "high"; 148 return "high";
152 case NORMAL_PRIORITY: 149 case NORMAL_PRIORITY:
153 return "normal"; 150 return "normal";
154 case BEST_EFFORT_PRIORITY: 151 case BEST_EFFORT_PRIORITY:
155 return "best_effort"; 152 return "best_effort";
156 default: 153 default:
157 NOTREACHED(); 154 NOTREACHED();
158 return nullptr; 155 return nullptr;
159 } 156 }
160 } 157 }
161 158
162 void RendererTaskQueueSelector::AsValueInto( 159 void TaskQueueSelectorImpl::AsValueInto(
163 base::trace_event::TracedValue* state) const { 160 base::trace_event::TracedValue* state) const {
164 DCHECK(main_thread_checker_.CalledOnValidThread()); 161 DCHECK(main_thread_checker_.CalledOnValidThread());
165 state->BeginDictionary("priorities"); 162 state->BeginDictionary("priorities");
166 for (QueuePriority priority = FIRST_QUEUE_PRIORITY; 163 for (QueuePriority priority = FIRST_QUEUE_PRIORITY;
167 priority < QUEUE_PRIORITY_COUNT; priority = NextPriority(priority)) { 164 priority < QUEUE_PRIORITY_COUNT; priority = NextPriority(priority)) {
168 state->BeginArray(PriorityToString(priority)); 165 state->BeginArray(PriorityToString(priority));
169 for (size_t queue_index : queue_priorities_[priority]) 166 for (size_t queue_index : queue_priorities_[priority])
170 state->AppendInteger(queue_index); 167 state->AppendInteger(queue_index);
171 state->EndArray(); 168 state->EndArray();
172 } 169 }
173 state->EndDictionary(); 170 state->EndDictionary();
174 state->SetInteger("starvation_count", starvation_count_); 171 state->SetInteger("starvation_count", starvation_count_);
175 } 172 }
176 173
177 } // namespace content 174 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698