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

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

Issue 1708773002: TaskScheduler [7] SchedulerThreadPool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_5_worker_thread
Patch Set: self review Created 4 years, 8 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
(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/sequence.h"
23 #include "base/task_scheduler/sequence_sort_key.h"
24 #include "base/task_scheduler/task_tracker.h"
25 #include "base/threading/platform_thread.h"
26 #include "base/threading/simple_thread.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28
29 namespace base {
30 namespace internal {
31 namespace {
32
33 const size_t kNumThreadsInThreadPool = 4;
34 const size_t kNumThreadsPostingTasks = 4;
35 const size_t kNumTasksPostedPerThread = 150;
36
37 class TaskSchedulerThreadPoolTest : public testing::Test {
38 protected:
39 TaskSchedulerThreadPoolTest() = default;
40
41 void SetUp() override {
42 thread_pool_ = SchedulerThreadPool::CreateThreadPool(
43 ThreadPriority::NORMAL, kNumThreadsInThreadPool,
44 Bind(&TaskSchedulerThreadPoolTest::RanTaskFromSequenceCallback,
45 Unretained(this)),
46 &task_tracker_);
47 ASSERT_TRUE(thread_pool_);
48 }
49
50 void TearDown() override {
51 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
52 thread_pool_->JoinForTesting();
53 }
54
55 std::unique_ptr<SchedulerThreadPool> thread_pool_;
56
57 private:
58 void RanTaskFromSequenceCallback(scoped_refptr<Sequence> sequence) {
59 // Reinsert |sequence| in |thread_pool_|'s shared PriorityQueue if it isn't
60 // empty after popping one of its Tasks. In production code, this callback
61 // would be implemented by the TaskScheduler which would first determine in
62 // which PriorityQueue the sequence must be reinserted.
63 const bool sequence_became_empty = sequence->PopTask();
64 if (!sequence_became_empty) {
65 const SequenceSortKey sort_key(sequence->GetSortKey());
66 thread_pool_->InsertSequenceAfterTaskRan(std::move(sequence), sort_key);
67 }
68 }
69
70 TaskTracker task_tracker_;
71
72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest);
73 };
74
75 class TaskFactory {
76 public:
77 TaskFactory() : cv_(&lock_) {}
78
79 // Posts a task through |task_runner|. If |post_nested_task| is true, the task
80 // will post a new task through |task_runner| when it runs. If |event| is set,
81 // the task will block until it is signaled.
82 void PostTask(scoped_refptr<TaskRunner> task_runner,
83 bool post_nested_task,
84 WaitableEvent* event) {
85 AutoLock auto_lock(lock_);
86 EXPECT_TRUE(task_runner->PostTask(
87 FROM_HERE, Bind(&TaskFactory::RunTaskCallback, Unretained(this),
88 num_created_tasks_++, task_runner, post_nested_task,
89 Unretained(event))));
90 }
91
92 // Waits for all tasks posted by PostTask() to start running. It is not
93 // guaranteed that the tasks have completed their execution when this returns.
94 void WaitForAllTasksToRun() const {
95 AutoLock auto_lock(lock_);
96 while (run_tasks_.size() < num_created_tasks_)
97 cv_.Wait();
98 }
99
100 size_t NumRunTasks() const {
101 AutoLock auto_lock(lock_);
102 return run_tasks_.size();
103 }
104
105 private:
106 void RunTaskCallback(size_t task_index,
107 scoped_refptr<TaskRunner> task_runner,
108 bool post_nested_task,
109 WaitableEvent* event) {
110 if (post_nested_task)
111 PostTask(task_runner, false, nullptr);
112
113 EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread());
114
115 {
116 AutoLock auto_lock(lock_);
117
118 if (run_tasks_.find(task_index) != run_tasks_.end())
119 ADD_FAILURE() << "A task ran more than once.";
120 run_tasks_.insert(task_index);
121
122 cv_.Signal();
123 }
124
125 if (event)
126 event->Wait();
127 }
128
129 // Synchronizes access to all members below.
130 mutable Lock lock_;
131
132 // Condition variable signaled when a task runs.
133 mutable ConditionVariable cv_;
134
135 // Number of tasks posted by PostTask().
136 size_t num_created_tasks_ = 0;
137
138 // Indexes of tasks that ran.
139 std::unordered_set<size_t> run_tasks_;
140
141 DISALLOW_COPY_AND_ASSIGN(TaskFactory);
142 };
143
144 class ThreadPostingTasks : public SimpleThread {
145 public:
146 // Constructs a thread that posts tasks to |thread_pool| through an
147 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the
148 // thread wait until all worker threads in |thread_pool| are idle before
149 // posting a new task. If |post_nested_task| is true, each task posted by this
150 // thread posts another task when it runs.
151 ThreadPostingTasks(SchedulerThreadPool* thread_pool,
152 ExecutionMode execution_mode,
153 bool wait_for_all_threads_idle,
154 bool post_nested_task)
155 : SimpleThread("ThreadPostingTasks"),
156 thread_pool_(thread_pool),
157 task_runner_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(),
158 execution_mode)),
159 wait_for_all_threads_idle_(wait_for_all_threads_idle),
160 post_nested_task_(post_nested_task) {}
161
162 const TaskFactory* factory() const { return &factory_; }
163
164 private:
165 void Run() override {
166 EXPECT_FALSE(task_runner_->RunsTasksOnCurrentThread());
167
168 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) {
169 if (wait_for_all_threads_idle_)
170 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
171 factory_.PostTask(task_runner_, post_nested_task_, nullptr);
172 }
173 }
174
175 SchedulerThreadPool* const thread_pool_;
176 scoped_refptr<TaskRunner> task_runner_;
177 const bool wait_for_all_threads_idle_;
178 const bool post_nested_task_;
179 TaskFactory factory_;
180
181 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
182 };
183
184 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasks) {
185 // Create threads to post tasks to PARALLEL TaskRunners.
186 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
187 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) {
188 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
189 thread_pool_.get(), ExecutionMode::PARALLEL, false, false)));
190 threads_posting_tasks.back()->Start();
191 }
192
193 // Wait for all tasks to run.
194 for (const auto& thread_posting_tasks : threads_posting_tasks) {
195 thread_posting_tasks->Join();
196 thread_posting_tasks->factory()->WaitForAllTasksToRun();
197 EXPECT_EQ(kNumTasksPostedPerThread,
198 thread_posting_tasks->factory()->NumRunTasks());
199 }
200
201 // Wait until all worker threads are idle to be sure that no task accesses
202 // its TaskFactory after |thread_posting_tasks| is destroyed.
203 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
204 }
205
206 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWaitAllThreadsIdle) {
207 // Create threads to post tasks to PARALLEL TaskRunners. To verify that
208 // worker threads can sleep and be woken up when new tasks are posted, wait
209 // for all threads to become idle before posting a new task.
210 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
211 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) {
212 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
213 thread_pool_.get(), ExecutionMode::PARALLEL, true, false)));
214 threads_posting_tasks.back()->Start();
215 }
216
217 // Wait for all tasks to run.
218 for (const auto& thread_posting_tasks : threads_posting_tasks) {
219 thread_posting_tasks->Join();
220 thread_posting_tasks->factory()->WaitForAllTasksToRun();
221 EXPECT_EQ(kNumTasksPostedPerThread,
222 thread_posting_tasks->factory()->NumRunTasks());
223 }
224
225 // Wait until all worker threads are idle to be sure that no task accesses
226 // its TaskFactory after |thread_posting_tasks| is destroyed.
227 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
228 }
229
230 TEST_F(TaskSchedulerThreadPoolTest, NestedPostParallelTasks) {
231 // Create threads to post tasks to PARALLEL TaskRunners. Each task posted by
232 // these threads will post another task when it runs.
233 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
234 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) {
235 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
236 thread_pool_.get(), ExecutionMode::PARALLEL, false, true)));
237 threads_posting_tasks.back()->Start();
238 }
239
240 // Wait for all tasks to run.
241 for (const auto& thread_posting_tasks : threads_posting_tasks) {
242 thread_posting_tasks->Join();
243 thread_posting_tasks->factory()->WaitForAllTasksToRun();
244 EXPECT_EQ(2 * kNumTasksPostedPerThread,
245 thread_posting_tasks->factory()->NumRunTasks());
246 }
247
248 // Wait until all worker threads are idle to be sure that no task accesses
249 // its TaskFactory after |thread_posting_tasks| is destroyed.
250 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
251 }
252
253 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWithOneAvailableThread) {
254 TaskFactory factory;
255
256 // Post tasks to keep all threads busy except one until |event| is signaled.
257 WaitableEvent event(true, false);
258 auto task_runner = thread_pool_->CreateTaskRunnerWithTraits(
259 TaskTraits(), ExecutionMode::PARALLEL);
260 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i)
261 factory.PostTask(task_runner, false, &event);
262 factory.WaitForAllTasksToRun();
263
264 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact
265 // that only one thread in |thread_pool_| isn't busy.
266 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i)
267 factory.PostTask(task_runner, false, nullptr);
268 factory.WaitForAllTasksToRun();
269
270 // Release tasks waiting on |event|.
271 event.Signal();
272
273 // Wait until all worker threads are idle to be sure that no task accesses
274 // |factory| after it is destroyed.
275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
276 }
277
278 TEST_F(TaskSchedulerThreadPoolTest, Saturate) {
279 TaskFactory factory;
280
281 // Verify that it is possible to have |kNumThreadsInThreadPool| tasks running
282 // simultaneously.
283 WaitableEvent event(true, false);
284 auto task_runner = thread_pool_->CreateTaskRunnerWithTraits(
285 TaskTraits(), ExecutionMode::PARALLEL);
286 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i)
287 factory.PostTask(task_runner, false, &event);
288 factory.WaitForAllTasksToRun();
289
290 // Release tasks waiting on |event|.
291 event.Signal();
292
293 // Wait until all worker threads are idle to be sure that no task accesses
294 // |factory| after it is destroyed.
295 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
296 }
297
298 } // namespace
299 } // namespace internal
300 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698