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

Side by Side Diff: base/task_scheduler/task_scheduler_impl.cc

Issue 2064073003: TaskScheduler: Make the worker pools of TaskSchedulerImpl configurable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: delegate Created 4 years, 6 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/task_scheduler/task_scheduler_impl.h" 5 #include "base/task_scheduler/task_scheduler_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/task_scheduler/scheduler_service_thread.h" 12 #include "base/task_scheduler/scheduler_service_thread.h"
13 #include "base/task_scheduler/scheduler_thread_pool_impl.h"
14 #include "base/task_scheduler/sequence_sort_key.h" 13 #include "base/task_scheduler/sequence_sort_key.h"
15 #include "base/task_scheduler/task.h" 14 #include "base/task_scheduler/task.h"
16 #include "base/time/time.h" 15 #include "base/time/time.h"
17 16
18 namespace base { 17 namespace base {
19 namespace internal { 18 namespace internal {
20 19
21 // static 20 // static
22 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create() { 21 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create(
23 std::unique_ptr<TaskSchedulerImpl> scheduler(new TaskSchedulerImpl); 22 std::unique_ptr<Delegate> delegate) {
23 std::unique_ptr<TaskSchedulerImpl> scheduler(
24 new TaskSchedulerImpl(std::move(delegate)));
24 scheduler->Initialize(); 25 scheduler->Initialize();
25 return scheduler; 26 return scheduler;
26 } 27 }
27 28
28 TaskSchedulerImpl::~TaskSchedulerImpl() { 29 TaskSchedulerImpl::~TaskSchedulerImpl() {
29 #if DCHECK_IS_ON() 30 #if DCHECK_IS_ON()
30 DCHECK(join_for_testing_returned_.IsSignaled()); 31 DCHECK(join_for_testing_returned_.IsSignaled());
31 #endif 32 #endif
32 } 33 }
33 34
(...skipping 16 matching lines...) Expand all
50 51
51 void TaskSchedulerImpl::Shutdown() { 52 void TaskSchedulerImpl::Shutdown() {
52 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. 53 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown.
53 task_tracker_.Shutdown(); 54 task_tracker_.Shutdown();
54 } 55 }
55 56
56 void TaskSchedulerImpl::JoinForTesting() { 57 void TaskSchedulerImpl::JoinForTesting() {
57 #if DCHECK_IS_ON() 58 #if DCHECK_IS_ON()
58 DCHECK(!join_for_testing_returned_.IsSignaled()); 59 DCHECK(!join_for_testing_returned_.IsSignaled());
59 #endif 60 #endif
60 background_thread_pool_->JoinForTesting(); 61 for (const auto& thread_pool : thread_pools_)
61 background_file_io_thread_pool_->JoinForTesting(); 62 thread_pool->JoinForTesting();
62 normal_thread_pool_->JoinForTesting();
63 normal_file_io_thread_pool_->JoinForTesting();
64 service_thread_->JoinForTesting(); 63 service_thread_->JoinForTesting();
65 #if DCHECK_IS_ON() 64 #if DCHECK_IS_ON()
66 join_for_testing_returned_.Signal(); 65 join_for_testing_returned_.Signal();
67 #endif 66 #endif
68 } 67 }
69 68
70 TaskSchedulerImpl::TaskSchedulerImpl() 69 TaskSchedulerImpl::TaskSchedulerImpl(std::unique_ptr<Delegate> delegate)
71 : delayed_task_manager_( 70 : delegate_(std::move(delegate)),
71 delayed_task_manager_(
72 Bind(&TaskSchedulerImpl::OnDelayedRunTimeUpdated, Unretained(this))) 72 Bind(&TaskSchedulerImpl::OnDelayedRunTimeUpdated, Unretained(this)))
73 #if DCHECK_IS_ON() 73 #if DCHECK_IS_ON()
74 , 74 ,
75 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, 75 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL,
76 WaitableEvent::InitialState::NOT_SIGNALED) 76 WaitableEvent::InitialState::NOT_SIGNALED)
77 #endif 77 #endif
78 { 78 {
79 DCHECK(delegate_);
79 } 80 }
80 81
81 void TaskSchedulerImpl::Initialize() { 82 void TaskSchedulerImpl::Initialize() {
82 using IORestriction = SchedulerThreadPoolImpl::IORestriction; 83 constexpr char kTaskSchedulerThreadNamePrefix[] = "TaskScheduler";
83 84
84 const SchedulerThreadPoolImpl::ReEnqueueSequenceCallback 85 const SchedulerThreadPoolImpl::ReEnqueueSequenceCallback
85 re_enqueue_sequence_callback = 86 re_enqueue_sequence_callback =
86 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); 87 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this));
87 88
88 // TODO(fdoray): Derive the number of threads per pool from hardware 89 const size_t num_thread_pools = delegate_->GetNumThreadPools();
robliao 2016/06/16 20:51:15 I'm not sure it's necessary to make the TaskSchedu
fdoray 2016/06/17 15:14:34 We don't plan to reconfigure the thread pool hiera
89 // characteristics rather than using hard-coded constants. 90 for (size_t i = 0; i < num_thread_pools; ++i) {
91 const Delegate::ThreadPoolCreationArgs creation_args =
92 delegate_->GetCreationArgsForThreadPool(i);
90 93
91 // Passing pointers to objects owned by |this| to 94 // Passing pointers to objects owned by |this| to
92 // SchedulerThreadPoolImpl::Create() is safe because a TaskSchedulerImpl can't 95 // SchedulerThreadPoolImpl::Create() is safe because a TaskSchedulerImpl
93 // be deleted before all its thread pools have been joined. 96 // can't be deleted before all its thread pools have been joined.
94 background_thread_pool_ = SchedulerThreadPoolImpl::Create( 97 thread_pools_.push_back(SchedulerThreadPoolImpl::Create(
95 "TaskSchedulerBackground", ThreadPriority::BACKGROUND, 1U, 98 kTaskSchedulerThreadNamePrefix + creation_args.name,
96 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_, 99 creation_args.thread_priority, creation_args.max_threads,
97 &delayed_task_manager_); 100 creation_args.io_restriction, re_enqueue_sequence_callback,
98 CHECK(background_thread_pool_); 101 &task_tracker_, &delayed_task_manager_));
99 102 CHECK(thread_pools_.back());
100 background_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create( 103 }
101 "TaskSchedulerBackgroundFileIO", ThreadPriority::BACKGROUND, 1U,
102 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_,
103 &delayed_task_manager_);
104 CHECK(background_file_io_thread_pool_);
105
106 normal_thread_pool_ = SchedulerThreadPoolImpl::Create(
107 "TaskSchedulerForeground", ThreadPriority::NORMAL, 4U,
108 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_,
109 &delayed_task_manager_);
110 CHECK(normal_thread_pool_);
111
112 normal_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create(
113 "TaskSchedulerForegroundFileIO", ThreadPriority::NORMAL, 12U,
114 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_,
115 &delayed_task_manager_);
116 CHECK(normal_file_io_thread_pool_);
117 104
118 service_thread_ = SchedulerServiceThread::Create(&task_tracker_, 105 service_thread_ = SchedulerServiceThread::Create(&task_tracker_,
119 &delayed_task_manager_); 106 &delayed_task_manager_);
120 CHECK(service_thread_); 107 CHECK(service_thread_);
121 } 108 }
122 109
123 SchedulerThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits( 110 SchedulerThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits(
124 const TaskTraits& traits) { 111 const TaskTraits& traits) {
125 if (traits.with_file_io()) { 112 const size_t index = delegate_->GetThreadPoolIndexForTraits(traits);
126 if (traits.priority() == TaskPriority::BACKGROUND) 113 DCHECK_LT(index, thread_pools_.size());
127 return background_file_io_thread_pool_.get(); 114 return thread_pools_[index].get();
128 return normal_file_io_thread_pool_.get();
129 }
130
131 if (traits.priority() == TaskPriority::BACKGROUND)
132 return background_thread_pool_.get();
133 return normal_thread_pool_.get();
134 } 115 }
135 116
136 void TaskSchedulerImpl::ReEnqueueSequenceCallback( 117 void TaskSchedulerImpl::ReEnqueueSequenceCallback(
137 scoped_refptr<Sequence> sequence) { 118 scoped_refptr<Sequence> sequence) {
138 DCHECK(sequence); 119 DCHECK(sequence);
139 120
140 const SequenceSortKey sort_key = sequence->GetSortKey(); 121 const SequenceSortKey sort_key = sequence->GetSortKey();
141 TaskTraits traits(sequence->PeekTask()->traits); 122 TaskTraits traits(sequence->PeekTask()->traits);
142 123
143 // Update the priority of |traits| so that the next task in |sequence| runs 124 // Update the priority of |traits| so that the next task in |sequence| runs
144 // with the highest priority in |sequence| as opposed to the next task's 125 // with the highest priority in |sequence| as opposed to the next task's
145 // specific priority. 126 // specific priority.
146 traits.WithPriority(sort_key.priority()); 127 traits.WithPriority(sort_key.priority());
147 128
148 GetThreadPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), 129 GetThreadPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence),
149 sort_key); 130 sort_key);
150 } 131 }
151 132
152 void TaskSchedulerImpl::OnDelayedRunTimeUpdated() { 133 void TaskSchedulerImpl::OnDelayedRunTimeUpdated() {
153 service_thread_->WakeUp(); 134 service_thread_->WakeUp();
154 } 135 }
155 136
156 } // namespace internal 137 } // namespace internal
157 } // namespace base 138 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698