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

Side by Side Diff: content/renderer/scheduler/renderer_scheduler_selector.cc

Issue 664963002: content: Add RendererScheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Alex's comments. Created 6 years, 1 month 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
(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 : normal_queue_starvation_count_(0) {
14 }
15
16 RendererSchedulerSelector::~RendererSchedulerSelector() {
17 }
18
19 void RendererSchedulerSelector::RegisterWorkQueues(
20 const std::vector<const base::TaskQueue*>& work_queues) {
21 main_thread_checker_.CalledOnValidThread();
22 work_queues_ = work_queues;
23 // By default, all work queues are set to normal priority.
24 for (size_t i = 0; i < work_queues.size(); i++) {
25 queue_priorities_[kNormalPriority].insert(i);
26 }
27 queue_priorities_[kControlPriority].clear();
28 queue_priorities_[kHighPriority].clear();
29 queue_priorities_[kBestEffortPriority].clear();
30 }
31
32 void RendererSchedulerSelector::SetQueuePriority(size_t queue_index,
33 QueuePriority priority) {
34 main_thread_checker_.CalledOnValidThread();
35 DCHECK_LT(queue_index, work_queues_.size());
36 DCHECK_LT(priority, kQueuePriorityCount);
37 DisableQueue(queue_index);
38 queue_priorities_[priority].insert(queue_index);
39 }
40
41 void RendererSchedulerSelector::EnableQueue(size_t queue_index,
42 QueuePriority priority) {
43 SetQueuePriority(queue_index, priority);
44 }
45
46 void RendererSchedulerSelector::DisableQueue(size_t queue_index) {
47 main_thread_checker_.CalledOnValidThread();
48 DCHECK_LT(queue_index, work_queues_.size());
49 static const QueuePriority kPriorities[] = {
50 kControlPriority, kHighPriority, kNormalPriority, kBestEffortPriority};
51 for (QueuePriority priority : kPriorities) {
52 queue_priorities_[priority].erase(queue_index);
53 }
54 }
55
56 bool RendererSchedulerSelector::IsOlder(const base::TaskQueue* queueA,
57 const base::TaskQueue* queueB) const {
58 return queueB->front() < queueA->front();
59 }
60
61 bool RendererSchedulerSelector::ChooseOldestWithPriority(
62 QueuePriority priority,
63 size_t* out_queue_index) const {
64 bool found_non_empty_queue = false;
65 size_t chosen_queue = 0;
66 for (int queue_index : queue_priorities_[priority]) {
67 if (work_queues_[queue_index]->empty()) {
68 continue;
69 }
70 // Note: the comparison between |chosen_queue| and |queue_index| is correct
71 // due to the fact that the PendingTask operator inverts comparison for
72 // priority queue.
73 if (!found_non_empty_queue ||
74 IsOlder(work_queues_[queue_index], work_queues_[chosen_queue])) {
75 found_non_empty_queue = true;
76 chosen_queue = queue_index;
77 }
78 }
79
80 if (found_non_empty_queue) {
81 *out_queue_index = chosen_queue;
82 }
83 return found_non_empty_queue;
84 }
85
86 bool RendererSchedulerSelector::SelectWorkQueueToService(
87 size_t* out_queue_index) {
88 main_thread_checker_.CalledOnValidThread();
89 DCHECK(work_queues_.size());
90 // Always service the control queue if it has any work.
91 if (ChooseOldestWithPriority(kControlPriority, out_queue_index)) {
92 return true;
93 }
94 // Select from the normal priority queue if we are starving it.
95 if (normal_queue_starvation_count_ >= kMaxStarvationTasks &&
96 ChooseOldestWithPriority(kNormalPriority, out_queue_index)) {
97 normal_queue_starvation_count_ = 0;
98 return true;
99 }
100 // Otherwise choose in priority order.
101 static const QueuePriority kPriorities[] = {
102 kHighPriority, kNormalPriority, kBestEffortPriority};
103 for (QueuePriority priority : kPriorities) {
104 if (ChooseOldestWithPriority(priority, out_queue_index)) {
105 if (priority == kHighPriority) {
106 normal_queue_starvation_count_++;
107 } else {
108 normal_queue_starvation_count_ = 0;
109 }
110 return true;
111 }
112 }
113 return false;
114 }
115
116 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698