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

Side by Side Diff: Source/platform/scheduler/Scheduler.cpp

Issue 439923006: Prioritizing input and compositor tasks in the blink scheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removing a couple of tests that fail due to the assert we added. Created 6 years, 4 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 "config.h" 5 #include "config.h"
6 #include "platform/scheduler/Scheduler.h" 6 #include "platform/scheduler/Scheduler.h"
7 7
8 #include "platform/PlatformThreadData.h"
8 #include "platform/Task.h" 9 #include "platform/Task.h"
10 #include "platform/ThreadTimers.h"
9 #include "platform/TraceEvent.h" 11 #include "platform/TraceEvent.h"
10 #include "platform/TraceLocation.h"
11 #include "public/platform/Platform.h" 12 #include "public/platform/Platform.h"
12 #include "public/platform/WebThread.h" 13 #include "wtf/ThreadingPrimitives.h"
13 14
14 namespace blink { 15 namespace blink {
15 16
16 namespace { 17 namespace {
17 18
18 class MainThreadTaskAdapter : public blink::WebThread::Task { 19 // Can be created from any thread.
20 // Note if the scheduler gets shutdown, this may be run after.
21 class MainThreadIdleTaskAdapter : public WebThread::Task {
19 public: 22 public:
20 explicit MainThreadTaskAdapter(const TraceLocation& location, const Schedule r::Task& task) 23 MainThreadIdleTaskAdapter(const Scheduler::IdleTask& idleTask, double allott edTimeMs, const TraceLocation& location)
21 : m_location(location) 24 : m_idleTask(idleTask)
22 , m_task(task) 25 , m_allottedTimeMs(allottedTimeMs)
26 , m_location(location)
23 { 27 {
24 } 28 }
25 29
26 // WebThread::Task implementation. 30 // WebThread::Task implementation.
27 virtual void run() OVERRIDE 31 virtual void run() OVERRIDE
28 { 32 {
29 TRACE_EVENT2("blink", "MainThreadTaskAdapter::run", 33 TRACE_EVENT2("blink", "MainThreadIdleTaskAdapter::run",
30 "src_file", m_location.fileName(), 34 "src_file", m_location.fileName(),
31 "src_func", m_location.functionName()); 35 "src_func", m_location.functionName());
32 m_task();
33 }
34
35 private:
36 const TraceLocation m_location;
37 Scheduler::Task m_task;
38 };
39
40 class MainThreadIdleTaskAdapter : public blink::WebThread::Task {
41 public:
42 MainThreadIdleTaskAdapter(const Scheduler::IdleTask& idleTask, double allott edTimeMs)
43 : m_idleTask(idleTask)
44 , m_allottedTimeMs(allottedTimeMs)
45 {
46 }
47
48 // WebThread::Task implementation.
49 virtual void run() OVERRIDE
50 {
51 TRACE_EVENT1("blink", "MainThreadIdleTaskAdapter::run", "allottedTime", m_allottedTimeMs);
52 m_idleTask(m_allottedTimeMs); 36 m_idleTask(m_allottedTimeMs);
53 } 37 }
54 38
55 private: 39 private:
56 Scheduler::IdleTask m_idleTask; 40 Scheduler::IdleTask m_idleTask;
57 double m_allottedTimeMs; 41 double m_allottedTimeMs;
42 TraceLocation m_location;
58 }; 43 };
59 44
60 } 45 } // namespace
46
47 // Typically only created from compositor or render threads.
48 // Note if the scheduler gets shutdown, this may be run after.
49 class Scheduler::MainThreadPendingHighPriorityTaskRunner : public WebThread::Tas k {
50 public:
51 MainThreadPendingHighPriorityTaskRunner()
52 {
53 ASSERT(Scheduler::shared());
54 }
55
56 ~MainThreadPendingHighPriorityTaskRunner()
57 {
58 Scheduler* scheduler = Scheduler::shared();
Sami 2014/08/12 13:02:59 Looks like the destructor should be empty here?
alexclarke 2014/08/12 13:59:28 Done.
59 if (!scheduler)
60 return;
61 }
62
63 // WebThread::Task implementation.
64 virtual void run() OVERRIDE
65 {
66 Scheduler* scheduler = Scheduler::shared();
67 if (!scheduler)
68 return;
69 scheduler->runHighPriorityTasks();
70 }
71 };
72
73
74 // Can be created from any thread.
75 // Note if the scheduler gets shutdown, this may be run after.
76 class Scheduler::MainThreadPendingTaskRunner : public WebThread::Task {
77 public:
78 MainThreadPendingTaskRunner(
79 const Scheduler::Task& task, const TraceLocation& location)
80 : m_task(task, location)
81 {
82 ASSERT(Scheduler::shared());
83 }
84
85 ~MainThreadPendingTaskRunner()
86 {
87 Scheduler* scheduler = Scheduler::shared();
Sami 2014/08/12 13:02:59 Same here?
alexclarke 2014/08/12 13:59:28 Done.
88 if (!scheduler)
89 return;
90 }
91
92 // WebThread::Task implementation.
93 virtual void run() OVERRIDE
94 {
95 Scheduler* scheduler = Scheduler::shared();
96 // This will assert if a posted task outlives blink, which is probably a bug.
97 ASSERT(scheduler);
98 if (scheduler)
99 Scheduler::shared()->runHighPriorityTasks();
100 m_task.run();
101 }
102
103 Scheduler::TracedTask m_task;
104 };
61 105
62 Scheduler* Scheduler::s_sharedScheduler = nullptr; 106 Scheduler* Scheduler::s_sharedScheduler = nullptr;
63 107
64 void Scheduler::initializeOnMainThread() 108 void Scheduler::initializeOnMainThread()
65 { 109 {
66 s_sharedScheduler = new Scheduler(); 110 s_sharedScheduler = new Scheduler();
67 } 111 }
68 112
69 void Scheduler::shutdown() 113 void Scheduler::shutdown()
70 { 114 {
71 delete s_sharedScheduler; 115 delete s_sharedScheduler;
72 s_sharedScheduler = nullptr; 116 s_sharedScheduler = nullptr;
73 } 117 }
74 118
75 Scheduler* Scheduler::shared() 119 Scheduler* Scheduler::shared()
76 { 120 {
77 return s_sharedScheduler; 121 return s_sharedScheduler;
78 } 122 }
79 123
80 Scheduler::Scheduler() 124 Scheduler::Scheduler()
81 : m_mainThread(blink::Platform::current()->currentThread()) 125 : m_mainThread(blink::Platform::current()->currentThread())
82 , m_sharedTimerFunction(nullptr) 126 , m_sharedTimerFunction(nullptr)
127 , m_highPriotityTaskCount(0)
83 { 128 {
84 } 129 }
85 130
86 Scheduler::~Scheduler() 131 Scheduler::~Scheduler()
87 { 132 {
133 while (shouldYieldForHighPriorityWork()) {
Sami 2014/08/12 13:02:59 I'm slightly worried that shouldYieldForHighPriori
alexclarke 2014/08/12 13:59:28 Done.
134 runHighPriorityTasks();
135 }
88 } 136 }
89 137
90 void Scheduler::scheduleTask(const TraceLocation& location, const Task& task) 138 void Scheduler::scheduleIdleTask(const TraceLocation& location, const IdleTask& idleTask)
91 {
92 m_mainThread->postTask(new MainThreadTaskAdapter(location, task));
93 }
94
95 void Scheduler::scheduleIdleTask(const IdleTask& idleTask)
96 { 139 {
97 // TODO: send a real allottedTime here. 140 // TODO: send a real allottedTime here.
98 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0)); 141 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0, location)) ;
99 } 142 }
100 143
101 void Scheduler::postTask(const TraceLocation& location, const Task& task) 144 void Scheduler::postTask(const TraceLocation& location, const Task& task)
102 { 145 {
103 scheduleTask(location, task); 146 m_mainThread->postTask(new MainThreadPendingTaskRunner(task, location));
104 } 147 }
105 148
106 void Scheduler::postInputTask(const TraceLocation& location, const Task& task) 149 void Scheduler::postInputTask(const TraceLocation& location, const Task& task)
107 { 150 {
108 scheduleTask(location, task); 151 Locker<Mutex> lock(m_pendingTasksMutex);
152 m_pendingInputTasks.append(TracedTask(task, location));
153 atomicIncrement(&m_highPriotityTaskCount);
154 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner());
109 } 155 }
110 156
111 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta sk) 157 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta sk)
112 { 158 {
113 scheduleTask(location, task); 159 Locker<Mutex> lock(m_pendingTasksMutex);
160 m_pendingCompositorTasks.append(TracedTask(task, location));
161 atomicIncrement(&m_highPriotityTaskCount);
162 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner());
114 } 163 }
115 164
116 void Scheduler::postIdleTask(const IdleTask& idleTask) 165 void Scheduler::postIdleTask(const TraceLocation& location, const IdleTask& idle Task)
117 { 166 {
118 scheduleIdleTask(idleTask); 167 scheduleIdleTask(location, idleTask);
119 } 168 }
120 169
121 void Scheduler::tickSharedTimer() 170 void Scheduler::tickSharedTimer()
122 { 171 {
123 TRACE_EVENT0("blink", "Scheduler::tickSharedTimer"); 172 TRACE_EVENT0("blink", "Scheduler::tickSharedTimer");
173
174 // Run any high priority tasks that are queued up, otherwise the blink timer s will yield immediately.
175 runHighPriorityTasks();
124 m_sharedTimerFunction(); 176 m_sharedTimerFunction();
177
178 // The blink timers may have just yielded, so run any high priority tasks th at where queued up
179 // while the blink timers were executing.
180 runHighPriorityTasks();
181 }
182
183 void Scheduler::runHighPriorityTasks()
184 {
185 TRACE_EVENT0("blink", "Scheduler::runHighPriorityTasks");
186
187 m_pendingTasksMutex.lock();
188 Deque<TracedTask>& inputTasks = m_pendingInputTasks.swapBuffers();
189 Deque<TracedTask>& compositorTasks = m_pendingCompositorTasks.swapBuffers();
190 m_pendingTasksMutex.unlock();
191
192 for (;;) {
193 if (!inputTasks.isEmpty()) {
194 inputTasks.takeFirst().run();
195 atomicDecrement(&m_highPriotityTaskCount);
Sami 2014/08/12 13:02:59 I think we can replace these atomics with just one
alexclarke 2014/08/12 13:59:28 Done.
196 ASSERT(m_highPriotityTaskCount >= 0);
197 continue;
198 }
199
200 if (compositorTasks.isEmpty())
201 break;
202 compositorTasks.takeFirst().run();
203 atomicDecrement(&m_highPriotityTaskCount);
204 ASSERT(m_highPriotityTaskCount >= 0);
205 }
125 } 206 }
126 207
127 void Scheduler::sharedTimerAdapter() 208 void Scheduler::sharedTimerAdapter()
128 { 209 {
129 shared()->tickSharedTimer(); 210 shared()->tickSharedTimer();
130 } 211 }
131 212
132 void Scheduler::setSharedTimerFiredFunction(void (*function)()) 213 void Scheduler::setSharedTimerFiredFunction(void (*function)())
133 { 214 {
134 m_sharedTimerFunction = function; 215 m_sharedTimerFunction = function;
135 blink::Platform::current()->setSharedTimerFiredFunction(function ? &Schedule r::sharedTimerAdapter : nullptr); 216 blink::Platform::current()->setSharedTimerFiredFunction(function ? &Schedule r::sharedTimerAdapter : nullptr);
136 } 217 }
137 218
138 void Scheduler::setSharedTimerFireInterval(double interval) 219 void Scheduler::setSharedTimerFireInterval(double interval)
139 { 220 {
140 blink::Platform::current()->setSharedTimerFireInterval(interval); 221 blink::Platform::current()->setSharedTimerFireInterval(interval);
141 } 222 }
142 223
143 void Scheduler::stopSharedTimer() 224 void Scheduler::stopSharedTimer()
144 { 225 {
145 blink::Platform::current()->stopSharedTimer(); 226 blink::Platform::current()->stopSharedTimer();
146 } 227 }
147 228
148 bool Scheduler::shouldYieldForHighPriorityWork() 229 bool Scheduler::shouldYieldForHighPriorityWork()
149 { 230 {
150 return false; 231 return acquireLoad(&m_highPriotityTaskCount) != 0;
232 }
233
234 void Scheduler::TracedTask::run()
235 {
236 TRACE_EVENT2("blink", "TracedTask::run",
237 "src_file", m_location.fileName(),
238 "src_func", m_location.functionName());
239 m_task();
151 } 240 }
152 241
153 } // namespace blink 242 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698