| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/task_scheduler/scheduler_thread_pool.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <unordered_set> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/bind.h" | |
| 14 #include "base/bind_helpers.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ptr_util.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/synchronization/condition_variable.h" | |
| 19 #include "base/synchronization/lock.h" | |
| 20 #include "base/synchronization/waitable_event.h" | |
| 21 #include "base/task_runner.h" | |
| 22 #include "base/task_scheduler/delayed_task_manager.h" | |
| 23 #include "base/task_scheduler/sequence.h" | |
| 24 #include "base/task_scheduler/sequence_sort_key.h" | |
| 25 #include "base/task_scheduler/task_tracker.h" | |
| 26 #include "base/threading/platform_thread.h" | |
| 27 #include "base/threading/simple_thread.h" | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 | |
| 30 namespace base { | |
| 31 namespace internal { | |
| 32 namespace { | |
| 33 | |
| 34 const size_t kNumThreadsInThreadPool = 4; | |
| 35 const size_t kNumThreadsPostingTasks = 4; | |
| 36 const size_t kNumTasksPostedPerThread = 150; | |
| 37 | |
| 38 class TaskSchedulerThreadPoolTest | |
| 39 : public testing::TestWithParam<ExecutionMode> { | |
| 40 protected: | |
| 41 TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {} | |
| 42 | |
| 43 void SetUp() override { | |
| 44 thread_pool_ = SchedulerThreadPool::CreateThreadPool( | |
| 45 ThreadPriority::NORMAL, kNumThreadsInThreadPool, | |
| 46 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback, | |
| 47 Unretained(this)), | |
| 48 &task_tracker_, &delayed_task_manager_); | |
| 49 ASSERT_TRUE(thread_pool_); | |
| 50 } | |
| 51 | |
| 52 void TearDown() override { | |
| 53 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 54 thread_pool_->JoinForTesting(); | |
| 55 } | |
| 56 | |
| 57 std::unique_ptr<SchedulerThreadPool> thread_pool_; | |
| 58 | |
| 59 private: | |
| 60 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | |
| 61 // In production code, this callback would be implemented by the | |
| 62 // TaskScheduler which would first determine which PriorityQueue the | |
| 63 // sequence must be reinserted. | |
| 64 const SequenceSortKey sort_key(sequence->GetSortKey()); | |
| 65 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); | |
| 66 } | |
| 67 | |
| 68 TaskTracker task_tracker_; | |
| 69 DelayedTaskManager delayed_task_manager_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); | |
| 72 }; | |
| 73 | |
| 74 class TaskFactory { | |
| 75 public: | |
| 76 // Constructs a TaskFactory that posts tasks with |execution_mode| to | |
| 77 // |thread_pool|. | |
| 78 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) | |
| 79 : cv_(&lock_), | |
| 80 task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), | |
| 81 execution_mode)), | |
| 82 execution_mode_(execution_mode) {} | |
| 83 | |
| 84 // Posts a task through |task_runner_|. If |post_nested_task| is true, the | |
| 85 // task will post a new task when it runs. If |event| is set, the task will | |
| 86 // block until it is signaled. | |
| 87 void PostTestTask(bool post_nested_task, WaitableEvent* event) { | |
| 88 AutoLock auto_lock(lock_); | |
| 89 EXPECT_TRUE(task_runner_->PostTask( | |
| 90 FROM_HERE, | |
| 91 Bind(&TaskFactory::RunTaskCallback, Unretained(this), | |
| 92 num_created_tasks_++, post_nested_task, Unretained(event)))); | |
| 93 } | |
| 94 | |
| 95 // Waits for all tasks posted by PostTestTask() to start running. It is not | |
| 96 // guaranteed that the tasks have completed their execution when this returns. | |
| 97 void WaitForAllTasksToRun() const { | |
| 98 AutoLock auto_lock(lock_); | |
| 99 while (ran_tasks_.size() < num_created_tasks_) | |
| 100 cv_.Wait(); | |
| 101 } | |
| 102 | |
| 103 size_t NumRunTasks() const { | |
| 104 AutoLock auto_lock(lock_); | |
| 105 return ran_tasks_.size(); | |
| 106 } | |
| 107 | |
| 108 const TaskRunner* task_runner() const { return task_runner_.get(); } | |
| 109 | |
| 110 private: | |
| 111 void RunTaskCallback(size_t task_index, | |
| 112 bool post_nested_task, | |
| 113 WaitableEvent* event) { | |
| 114 if (post_nested_task) | |
| 115 PostTestTask(false, nullptr); | |
| 116 | |
| 117 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); | |
| 118 | |
| 119 { | |
| 120 AutoLock auto_lock(lock_); | |
| 121 | |
| 122 if (execution_mode_ == ExecutionMode::SEQUENCED && | |
| 123 task_index != ran_tasks_.size()) { | |
| 124 ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; | |
| 125 } | |
| 126 | |
| 127 if (ran_tasks_.find(task_index) != ran_tasks_.end()) | |
| 128 ADD_FAILURE() << "A task ran more than once."; | |
| 129 ran_tasks_.insert(task_index); | |
| 130 | |
| 131 cv_.Signal(); | |
| 132 } | |
| 133 | |
| 134 if (event) | |
| 135 event->Wait(); | |
| 136 } | |
| 137 | |
| 138 // Synchronizes access to all members below. | |
| 139 mutable Lock lock_; | |
| 140 | |
| 141 // Condition variable signaled when a task runs. | |
| 142 mutable ConditionVariable cv_; | |
| 143 | |
| 144 // Task runner through which this factory posts tasks. | |
| 145 const scoped_refptr<TaskRunner> task_runner_; | |
| 146 | |
| 147 // Execution mode of |task_runner_|. | |
| 148 const ExecutionMode execution_mode_; | |
| 149 | |
| 150 // Number of tasks posted by PostTestTask(). | |
| 151 size_t num_created_tasks_ = 0; | |
| 152 | |
| 153 // Indexes of tasks that ran. | |
| 154 std::unordered_set<size_t> ran_tasks_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(TaskFactory); | |
| 157 }; | |
| 158 | |
| 159 class ThreadPostingTasks : public SimpleThread { | |
| 160 public: | |
| 161 // Constructs a thread that posts tasks to |thread_pool| through an | |
| 162 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the | |
| 163 // thread wait until all worker threads in |thread_pool| are idle before | |
| 164 // posting a new task. If |post_nested_task| is true, each task posted by this | |
| 165 // thread posts another task when it runs. | |
| 166 ThreadPostingTasks(SchedulerThreadPool* thread_pool, | |
| 167 ExecutionMode execution_mode, | |
| 168 bool wait_for_all_threads_idle, | |
| 169 bool post_nested_task) | |
| 170 : SimpleThread("ThreadPostingTasks"), | |
| 171 thread_pool_(thread_pool), | |
| 172 wait_for_all_threads_idle_(wait_for_all_threads_idle), | |
| 173 post_nested_task_(post_nested_task), | |
| 174 factory_(thread_pool_, execution_mode) { | |
| 175 DCHECK(thread_pool_); | |
| 176 } | |
| 177 | |
| 178 const TaskFactory* factory() const { return &factory_; } | |
| 179 | |
| 180 private: | |
| 181 void Run() override { | |
| 182 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | |
| 183 | |
| 184 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | |
| 185 if (wait_for_all_threads_idle_) | |
| 186 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 187 factory_.PostTestTask(post_nested_task_, nullptr); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 SchedulerThreadPool* const thread_pool_; | |
| 192 const scoped_refptr<TaskRunner> task_runner_; | |
| 193 const bool wait_for_all_threads_idle_; | |
| 194 const bool post_nested_task_; | |
| 195 TaskFactory factory_; | |
| 196 | |
| 197 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | |
| 198 }; | |
| 199 | |
| 200 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { | |
| 201 // Create threads to post tasks. | |
| 202 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | |
| 203 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | |
| 204 const bool kWaitForAllThreadIdle = false; | |
| 205 const bool kPostNestedTasks = false; | |
| 206 threads_posting_tasks.push_back(WrapUnique( | |
| 207 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 208 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 209 threads_posting_tasks.back()->Start(); | |
| 210 } | |
| 211 | |
| 212 // Wait for all tasks to run. | |
| 213 for (const auto& thread_posting_tasks : threads_posting_tasks) { | |
| 214 thread_posting_tasks->Join(); | |
| 215 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | |
| 216 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 217 thread_posting_tasks->factory()->NumRunTasks()); | |
| 218 } | |
| 219 | |
| 220 // Wait until all worker threads are idle to be sure that no task accesses | |
| 221 // its TaskFactory after |thread_posting_tasks| is destroyed. | |
| 222 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 223 } | |
| 224 | |
| 225 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { | |
| 226 // Create threads to post tasks. To verify that worker threads can sleep and | |
| 227 // be woken up when new tasks are posted, wait for all threads to become idle | |
| 228 // before posting a new task. | |
| 229 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | |
| 230 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | |
| 231 const bool kWaitForAllThreadIdle = true; | |
| 232 const bool kPostNestedTasks = false; | |
| 233 threads_posting_tasks.push_back(WrapUnique( | |
| 234 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 235 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 236 threads_posting_tasks.back()->Start(); | |
| 237 } | |
| 238 | |
| 239 // Wait for all tasks to run. | |
| 240 for (const auto& thread_posting_tasks : threads_posting_tasks) { | |
| 241 thread_posting_tasks->Join(); | |
| 242 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | |
| 243 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 244 thread_posting_tasks->factory()->NumRunTasks()); | |
| 245 } | |
| 246 | |
| 247 // Wait until all worker threads are idle to be sure that no task accesses | |
| 248 // its TaskFactory after |thread_posting_tasks| is destroyed. | |
| 249 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 250 } | |
| 251 | |
| 252 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { | |
| 253 // Create threads to post tasks. Each task posted by these threads will post | |
| 254 // another task when it runs. | |
| 255 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | |
| 256 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | |
| 257 const bool kWaitForAllThreadIdle = false; | |
| 258 const bool kPostNestedTasks = true; | |
| 259 threads_posting_tasks.push_back(WrapUnique( | |
| 260 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 261 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 262 threads_posting_tasks.back()->Start(); | |
| 263 } | |
| 264 | |
| 265 // Wait for all tasks to run. | |
| 266 for (const auto& thread_posting_tasks : threads_posting_tasks) { | |
| 267 thread_posting_tasks->Join(); | |
| 268 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | |
| 269 EXPECT_EQ(2 * kNumTasksPostedPerThread, | |
| 270 thread_posting_tasks->factory()->NumRunTasks()); | |
| 271 } | |
| 272 | |
| 273 // Wait until all worker threads are idle to be sure that no task accesses | |
| 274 // its TaskFactory after |thread_posting_tasks| is destroyed. | |
| 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 276 } | |
| 277 | |
| 278 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { | |
| 279 // Post tasks to keep all threads busy except one until |event| is signaled. | |
| 280 // Use different factories so that tasks are added to different sequences and | |
| 281 // can run simultaneously when the execution mode is SEQUENCED. | |
| 282 WaitableEvent event(true, false); | |
| 283 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; | |
| 284 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { | |
| 285 blocked_task_factories.push_back( | |
| 286 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | |
| 287 blocked_task_factories.back()->PostTestTask(false, &event); | |
| 288 blocked_task_factories.back()->WaitForAllTasksToRun(); | |
| 289 } | |
| 290 | |
| 291 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | |
| 292 // that only one thread in |thread_pool_| isn't busy. | |
| 293 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); | |
| 294 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | |
| 295 short_task_factory.PostTestTask(false, nullptr); | |
| 296 short_task_factory.WaitForAllTasksToRun(); | |
| 297 | |
| 298 // Release tasks waiting on |event|. | |
| 299 event.Signal(); | |
| 300 | |
| 301 // Wait until all worker threads are idle to be sure that no task accesses | |
| 302 // its TaskFactory after it is destroyed. | |
| 303 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 304 } | |
| 305 | |
| 306 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { | |
| 307 // Verify that it is possible to have |kNumThreadsInThreadPool| | |
| 308 // tasks/sequences running simultaneously. Use different factories so that | |
| 309 // tasks are added to different sequences and can run simultaneously when the | |
| 310 // execution mode is SEQUENCED. | |
| 311 WaitableEvent event(true, false); | |
| 312 std::vector<std::unique_ptr<TaskFactory>> factories; | |
| 313 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { | |
| 314 factories.push_back( | |
| 315 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | |
| 316 factories.back()->PostTestTask(false, &event); | |
| 317 factories.back()->WaitForAllTasksToRun(); | |
| 318 } | |
| 319 | |
| 320 // Release tasks waiting on |event|. | |
| 321 event.Signal(); | |
| 322 | |
| 323 // Wait until all worker threads are idle to be sure that no task accesses | |
| 324 // its TaskFactory after it is destroyed. | |
| 325 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | |
| 326 } | |
| 327 | |
| 328 INSTANTIATE_TEST_CASE_P(Parallel, | |
| 329 TaskSchedulerThreadPoolTest, | |
| 330 ::testing::Values(ExecutionMode::PARALLEL)); | |
| 331 INSTANTIATE_TEST_CASE_P(Sequenced, | |
| 332 TaskSchedulerThreadPoolTest, | |
| 333 ::testing::Values(ExecutionMode::SEQUENCED)); | |
| 334 | |
| 335 } // namespace | |
| 336 } // namespace internal | |
| 337 } // namespace base | |
| OLD | NEW |