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

Side by Side Diff: content/renderer/raster_worker_pool.cc

Issue 1666283002: Reland - Refactor signaling in RWP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/raster_worker_pool.h" 5 #include "content/renderer/raster_worker_pool.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
13 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
14 #include "cc/base/math_util.h" 14 #include "cc/base/math_util.h"
15 #include "cc/raster/task_category.h" 15 #include "cc/raster/task_category.h"
16 16
17 namespace content { 17 namespace content {
18 namespace { 18 namespace {
19 19
20 // A thread which forwards to RasterWorkerPool::Run with the runnable 20 // A thread which forwards to RasterWorkerPool::Run with the runnable
21 // categories. 21 // categories.
22 class RasterWorkerPoolThread : public base::SimpleThread { 22 class RasterWorkerPoolThread : public base::SimpleThread {
23 public: 23 public:
24 explicit RasterWorkerPoolThread(const std::string& name_prefix, 24 RasterWorkerPoolThread(const std::string& name_prefix,
25 const Options& options, 25 const Options& options,
26 RasterWorkerPool* pool, 26 RasterWorkerPool* pool,
27 std::vector<cc::TaskCategory> categories) 27 std::vector<cc::TaskCategory> categories,
28 base::ConditionVariable* work_ready_cv)
28 : SimpleThread(name_prefix, options), 29 : SimpleThread(name_prefix, options),
29 pool_(pool), 30 pool_(pool),
30 categories_(categories) {} 31 categories_(categories),
32 work_ready_cv_(work_ready_cv) {}
31 33
32 void Run() override { pool_->Run(categories_); } 34 void Run() override { pool_->Run(categories_, work_ready_cv_); }
33 35
34 private: 36 private:
35 RasterWorkerPool* const pool_; 37 RasterWorkerPool* const pool_;
36 const std::vector<cc::TaskCategory> categories_; 38 const std::vector<cc::TaskCategory> categories_;
39 base::ConditionVariable* const work_ready_cv_;
reveman 2016/02/08 19:10:21 nit: ready_to_run_tasks_cv_ for consistency
ericrk 2016/02/10 18:44:49 Done.
37 }; 40 };
38 41
39 } // namespace 42 } // namespace
40 43
41 // A sequenced task runner which posts tasks to a RasterWorkerPool. 44 // A sequenced task runner which posts tasks to a RasterWorkerPool.
42 class RasterWorkerPool::RasterWorkerPoolSequencedTaskRunner 45 class RasterWorkerPool::RasterWorkerPoolSequencedTaskRunner
43 : public base::SequencedTaskRunner { 46 : public base::SequencedTaskRunner {
44 public: 47 public:
45 explicit RasterWorkerPoolSequencedTaskRunner( 48 explicit RasterWorkerPoolSequencedTaskRunner(
46 cc::TaskGraphRunner* task_graph_runner) 49 cc::TaskGraphRunner* task_graph_runner)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 cc::Task::Vector tasks_; 112 cc::Task::Vector tasks_;
110 // Graph object used for scheduling tasks. 113 // Graph object used for scheduling tasks.
111 cc::TaskGraph graph_; 114 cc::TaskGraph graph_;
112 // Cached vector to avoid allocation when getting the list of complete 115 // Cached vector to avoid allocation when getting the list of complete
113 // tasks. 116 // tasks.
114 cc::Task::Vector completed_tasks_; 117 cc::Task::Vector completed_tasks_;
115 }; 118 };
116 119
117 RasterWorkerPool::RasterWorkerPool() 120 RasterWorkerPool::RasterWorkerPool()
118 : namespace_token_(GetNamespaceToken()), 121 : namespace_token_(GetNamespaceToken()),
119 has_ready_to_run_tasks_cv_(&lock_), 122 foreground_has_ready_to_run_tasks_cv_(&lock_),
123 background_has_ready_to_run_tasks_cv_(&lock_),
124 target_running_task_count_(0),
120 has_namespaces_with_finished_running_tasks_cv_(&lock_), 125 has_namespaces_with_finished_running_tasks_cv_(&lock_),
121 shutdown_(false) {} 126 shutdown_(false) {}
122 127
123 void RasterWorkerPool::Start( 128 void RasterWorkerPool::Start(
124 int num_threads, 129 int num_threads,
125 const base::SimpleThread::Options& thread_options) { 130 const base::SimpleThread::Options& thread_options) {
126 DCHECK(threads_.empty()); 131 DCHECK(threads_.empty());
127 while (threads_.size() < static_cast<size_t>(num_threads)) {
128 // Determine the categories that each thread can run.
129 std::vector<cc::TaskCategory> task_categories;
130 132
131 // The first thread can run nonconcurrent tasks. 133 target_running_task_count_ = num_threads;
132 if (threads_.size() == 0) {
133 task_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND);
134 }
135 134
136 // All threads can run foreground tasks. 135 // start |num_threads| threads for foreground work, including nonconcurrent
reveman 2016/02/08 19:10:21 nit: s/start/Start/
ericrk 2016/02/10 18:44:49 Done.
137 task_categories.push_back(cc::TASK_CATEGORY_FOREGROUND); 136 // foreground work.
137 std::vector<cc::TaskCategory> foreground_categories;
138 foreground_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND);
139 foreground_categories.push_back(cc::TASK_CATEGORY_FOREGROUND);
138 140
139 // The last thread can run background tasks. 141 for (int i = 0; i < num_threads; i++) {
140 if (threads_.size() == (static_cast<size_t>(num_threads) - 1)) {
141 task_categories.push_back(cc::TASK_CATEGORY_BACKGROUND);
142 }
143
144 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread( 142 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread(
145 base::StringPrintf("CompositorTileWorker%u", 143 base::StringPrintf("CompositorTileWorker%u",
146 static_cast<unsigned>(threads_.size() + 1)) 144 static_cast<unsigned>(threads_.size() + 1))
147 .c_str(), 145 .c_str(),
148 thread_options, this, task_categories)); 146 thread_options, this, foreground_categories,
147 &foreground_has_ready_to_run_tasks_cv_));
149 thread->Start(); 148 thread->Start();
150 threads_.push_back(std::move(thread)); 149 threads_.push_back(std::move(thread));
151 } 150 }
151
152 // Start a single thread for background work.
153 {
154 std::vector<cc::TaskCategory> background_categories;
155 background_categories.push_back(cc::TASK_CATEGORY_BACKGROUND);
156 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread(
157 base::StringPrintf("CompositorTileWorker%u",
158 static_cast<unsigned>(threads_.size() + 1))
159 .c_str(),
160 thread_options, this, background_categories,
161 &background_has_ready_to_run_tasks_cv_));
162 thread->Start();
163 threads_.push_back(std::move(thread));
164 }
152 } 165 }
153 166
154 void RasterWorkerPool::Shutdown() { 167 void RasterWorkerPool::Shutdown() {
155 WaitForTasksToFinishRunning(namespace_token_); 168 WaitForTasksToFinishRunning(namespace_token_);
156 CollectCompletedTasks(namespace_token_, &completed_tasks_); 169 CollectCompletedTasks(namespace_token_, &completed_tasks_);
157 // Shutdown raster threads. 170 // Shutdown raster threads.
158 { 171 {
159 base::AutoLock lock(lock_); 172 base::AutoLock lock(lock_);
160 173
161 DCHECK(!work_queue_.HasReadyToRunTasks()); 174 DCHECK(!work_queue_.HasReadyToRunTasks());
162 DCHECK(!work_queue_.HasAnyNamespaces()); 175 DCHECK(!work_queue_.HasAnyNamespaces());
163 176
164 DCHECK(!shutdown_); 177 DCHECK(!shutdown_);
165 shutdown_ = true; 178 shutdown_ = true;
166 179
167 // Wake up all workers so they exit. 180 // Wake up all workers so they exit.
168 has_ready_to_run_tasks_cv_.Broadcast(); 181 foreground_has_ready_to_run_tasks_cv_.Broadcast();
182 background_has_ready_to_run_tasks_cv_.Broadcast();
169 } 183 }
170 while (!threads_.empty()) { 184 while (!threads_.empty()) {
171 threads_.back()->Join(); 185 threads_.back()->Join();
172 threads_.pop_back(); 186 threads_.pop_back();
173 } 187 }
174 } 188 }
175 189
176 // Overridden from base::TaskRunner: 190 // Overridden from base::TaskRunner:
177 bool RasterWorkerPool::PostDelayedTask( 191 bool RasterWorkerPool::PostDelayedTask(
178 const tracked_objects::Location& from_here, 192 const tracked_objects::Location& from_here,
(...skipping 25 matching lines...) Expand all
204 218
205 ScheduleTasksWithLockAcquired(namespace_token_, &graph_); 219 ScheduleTasksWithLockAcquired(namespace_token_, &graph_);
206 completed_tasks_.clear(); 220 completed_tasks_.clear();
207 return true; 221 return true;
208 } 222 }
209 223
210 bool RasterWorkerPool::RunsTasksOnCurrentThread() const { 224 bool RasterWorkerPool::RunsTasksOnCurrentThread() const {
211 return true; 225 return true;
212 } 226 }
213 227
214 void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories) { 228 void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories,
229 base::ConditionVariable* work_ready_cv) {
215 base::AutoLock lock(lock_); 230 base::AutoLock lock(lock_);
216 231
217 while (true) { 232 while (true) {
218 if (!RunTaskWithLockAcquired(categories)) { 233 if (!RunTaskWithLockAcquired(categories)) {
234 // We are no longer running tasks, which may allow another category to
235 // start running. Signal other worker threads.
236 SignalTaskReadyConditionVariables();
237
219 // Exit when shutdown is set and no more tasks are pending. 238 // Exit when shutdown is set and no more tasks are pending.
220 if (shutdown_) 239 if (shutdown_)
221 break; 240 break;
222 241
223 // Wait for more tasks. 242 // Wait for more tasks.
224 has_ready_to_run_tasks_cv_.Wait(); 243 work_ready_cv->Wait();
225 continue; 244 continue;
226 } 245 }
227 } 246 }
228 } 247 }
229 248
230 void RasterWorkerPool::FlushForTesting() { 249 void RasterWorkerPool::FlushForTesting() {
231 base::AutoLock lock(lock_); 250 base::AutoLock lock(lock_);
232 251
233 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) { 252 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) {
234 has_namespaces_with_finished_running_tasks_cv_.Wait(); 253 has_namespaces_with_finished_running_tasks_cv_.Wait();
(...skipping 24 matching lines...) Expand all
259 } 278 }
260 279
261 void RasterWorkerPool::ScheduleTasksWithLockAcquired(cc::NamespaceToken token, 280 void RasterWorkerPool::ScheduleTasksWithLockAcquired(cc::NamespaceToken token,
262 cc::TaskGraph* graph) { 281 cc::TaskGraph* graph) {
263 DCHECK(token.IsValid()); 282 DCHECK(token.IsValid());
264 DCHECK(!cc::TaskGraphWorkQueue::DependencyMismatch(graph)); 283 DCHECK(!cc::TaskGraphWorkQueue::DependencyMismatch(graph));
265 DCHECK(!shutdown_); 284 DCHECK(!shutdown_);
266 285
267 work_queue_.ScheduleTasks(token, graph); 286 work_queue_.ScheduleTasks(token, graph);
268 287
269 // If there is more work available, wake up the other worker threads. 288 // There may be more work available, so wake up another worker thread.
270 if (work_queue_.HasReadyToRunTasks()) 289 SignalTaskReadyConditionVariables();
271 has_ready_to_run_tasks_cv_.Broadcast();
272 } 290 }
273 291
274 void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) { 292 void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) {
275 TRACE_EVENT0("disabled-by-default-cc.debug", 293 TRACE_EVENT0("disabled-by-default-cc.debug",
276 "RasterWorkerPool::WaitForTasksToFinishRunning"); 294 "RasterWorkerPool::WaitForTasksToFinishRunning");
277 295
278 DCHECK(token.IsValid()); 296 DCHECK(token.IsValid());
279 297
280 { 298 {
281 base::AutoLock lock(lock_); 299 base::AutoLock lock(lock_);
282 base::ThreadRestrictions::ScopedAllowWait allow_wait; 300 base::ThreadRestrictions::ScopedAllowWait allow_wait;
283 301
284 auto* task_namespace = work_queue_.GetNamespaceForToken(token); 302 auto* task_namespace = work_queue_.GetNamespaceForToken(token);
285 303
286 if (!task_namespace) 304 if (!task_namespace)
287 return; 305 return;
288 306
289 while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace)) 307 while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace))
290 has_namespaces_with_finished_running_tasks_cv_.Wait(); 308 has_namespaces_with_finished_running_tasks_cv_.Wait();
309
310 // There may be other namespaces that have finished running tasks, so wake
311 // up another origin thread.
312 has_namespaces_with_finished_running_tasks_cv_.Signal();
291 } 313 }
292 } 314 }
293 315
294 void RasterWorkerPool::CollectCompletedTasks( 316 void RasterWorkerPool::CollectCompletedTasks(
295 cc::NamespaceToken token, 317 cc::NamespaceToken token,
296 cc::Task::Vector* completed_tasks) { 318 cc::Task::Vector* completed_tasks) {
297 TRACE_EVENT0("disabled-by-default-cc.debug", 319 TRACE_EVENT0("disabled-by-default-cc.debug",
298 "RasterWorkerPool::CollectCompletedTasks"); 320 "RasterWorkerPool::CollectCompletedTasks");
299 321
300 { 322 {
(...skipping 12 matching lines...) Expand all
313 bool RasterWorkerPool::RunTaskWithLockAcquired( 335 bool RasterWorkerPool::RunTaskWithLockAcquired(
314 const std::vector<cc::TaskCategory>& categories) { 336 const std::vector<cc::TaskCategory>& categories) {
315 for (const auto& category : categories) { 337 for (const auto& category : categories) {
316 if (work_queue_.HasReadyToRunTasksForCategory(category)) { 338 if (work_queue_.HasReadyToRunTasksForCategory(category)) {
317 RunTaskInCategoryWithLockAcquired(category); 339 RunTaskInCategoryWithLockAcquired(category);
318 return true; 340 return true;
319 } 341 }
320 } 342 }
321 return false; 343 return false;
322 } 344 }
323
324 void RasterWorkerPool::RunTaskInCategoryWithLockAcquired( 345 void RasterWorkerPool::RunTaskInCategoryWithLockAcquired(
325 cc::TaskCategory category) { 346 cc::TaskCategory category) {
326 TRACE_EVENT0("toplevel", "TaskGraphRunner::RunTask"); 347 TRACE_EVENT0("toplevel", "TaskGraphRunner::RunTask");
327 348
328 lock_.AssertAcquired(); 349 lock_.AssertAcquired();
329 350
330 auto prioritized_task = work_queue_.GetNextTaskToRun(category); 351 auto prioritized_task = work_queue_.GetNextTaskToRun(category);
331 cc::Task* task = prioritized_task.task; 352 cc::Task* task = prioritized_task.task;
332 353
354 // There may be more work available, so wake up another worker thread.
355 SignalTaskReadyConditionVariables();
356
333 // Call WillRun() before releasing |lock_| and running task. 357 // Call WillRun() before releasing |lock_| and running task.
334 task->WillRun(); 358 task->WillRun();
335 359
336 { 360 {
337 base::AutoUnlock unlock(lock_); 361 base::AutoUnlock unlock(lock_);
338 362
339 task->RunOnWorkerThread(); 363 task->RunOnWorkerThread();
340 } 364 }
341 365
342 // This will mark task as finished running. 366 // This will mark task as finished running.
343 task->DidRun(); 367 task->DidRun();
344 368
345 work_queue_.CompleteTask(prioritized_task); 369 work_queue_.CompleteTask(prioritized_task);
346 370
347 // We may have just dequeued more tasks, wake up the other worker threads.
348 if (work_queue_.HasReadyToRunTasks())
349 has_ready_to_run_tasks_cv_.Broadcast();
350
351 // If namespace has finished running all tasks, wake up origin threads. 371 // If namespace has finished running all tasks, wake up origin threads.
352 if (work_queue_.HasFinishedRunningTasksInNamespace( 372 if (work_queue_.HasFinishedRunningTasksInNamespace(
353 prioritized_task.task_namespace)) 373 prioritized_task.task_namespace))
354 has_namespaces_with_finished_running_tasks_cv_.Broadcast(); 374 has_namespaces_with_finished_running_tasks_cv_.Signal();
375 }
376
377 bool RasterWorkerPool::ShouldRunTaskForCategory(cc::TaskCategory category) {
reveman 2016/02/08 19:10:21 nit: lock_.AssertAcquired() and add "WithLockAcqui
ericrk 2016/02/10 18:44:49 Done.
378 // During shutdown we want to flush threads as fast as possible - don't bother
379 // enforcing running task limits.
reveman 2016/02/08 19:10:21 Is this special case worthwhile? Seems like it mak
ericrk 2016/02/10 18:44:49 makes sense - will remove this.
380 if (!shutdown_) {
381 // Enforce running task limits.
382 int running_task_count = work_queue_.NumRunningTasks();
383 if (running_task_count >= target_running_task_count_) {
384 return false;
385 }
386
387 if (category == cc::TASK_CATEGORY_BACKGROUND && running_task_count > 0) {
388 return false;
389 }
390 }
391
392 // Enforce that only one nonconcurrent task runs at a time.
393 if (category == cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND &&
394 work_queue_.NumRunningTasksForCategory(
395 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) > 0) {
396 return false;
397 }
398
399 return work_queue_.HasReadyToRunTasksForCategory(category);
400 }
401
402 void RasterWorkerPool::SignalTaskReadyConditionVariables() {
reveman 2016/02/08 19:10:21 nit: add "WithLockAcquired" suffix to function nam
ericrk 2016/02/10 18:44:49 Done.
403 lock_.AssertAcquired();
reveman 2016/02/08 19:10:21 nit: blank line after this to separate it from the
ericrk 2016/02/10 18:44:49 Done.
404 if (ShouldRunTaskForCategory(cc::TASK_CATEGORY_FOREGROUND) ||
405 ShouldRunTaskForCategory(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) {
406 foreground_has_ready_to_run_tasks_cv_.Signal();
407 } else if (ShouldRunTaskForCategory(cc::TASK_CATEGORY_BACKGROUND)) {
408 // Only run background tasks if there are no forground tasks left.
reveman 2016/02/08 19:10:21 hm, it looks like this logic is both here and insi
ericrk 2016/02/10 18:44:49 We can do this - results in a little extra work be
reveman 2016/02/10 20:49:31 sorry- I was assuming that a separate ShouldRunTas
409 background_has_ready_to_run_tasks_cv_.Signal();
410 }
355 } 411 }
356 412
357 RasterWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure) 413 RasterWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure)
358 : closure_(closure) {} 414 : closure_(closure) {}
359 415
360 // Overridden from cc::Task: 416 // Overridden from cc::Task:
361 void RasterWorkerPool::ClosureTask::RunOnWorkerThread() { 417 void RasterWorkerPool::ClosureTask::RunOnWorkerThread() {
362 closure_.Run(); 418 closure_.Run();
363 closure_.Reset(); 419 closure_.Reset();
364 } 420 }
365 421
366 RasterWorkerPool::ClosureTask::~ClosureTask() {} 422 RasterWorkerPool::ClosureTask::~ClosureTask() {}
367 423
368 } // namespace content 424 } // namespace content
OLDNEW
« content/renderer/raster_worker_pool.h ('K') | « content/renderer/raster_worker_pool.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698