Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "thread_pool.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 21 | #include "base/atomic.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "common_runtime_test.h" |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 24 | #include "thread-inl.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 28 | class CountTask : public Task { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 29 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 30 | explicit CountTask(AtomicInteger* count) : count_(count), verbose_(false) {} |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 32 | void Run(Thread* self) override { |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 33 | if (verbose_) { |
| 34 | LOG(INFO) << "Running: " << *self; |
| 35 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 36 | // Simulate doing some work. |
| 37 | usleep(100); |
| 38 | // Increment the counter which keeps track of work completed. |
| 39 | ++*count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 42 | void Finalize() override { |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 43 | if (verbose_) { |
| 44 | LOG(INFO) << "Finalizing: " << *Thread::Current(); |
| 45 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 46 | delete this; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | AtomicInteger* const count_; |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 51 | const bool verbose_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 54 | class ThreadPoolTest : public CommonRuntimeTest { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 55 | public: |
| 56 | static int32_t num_threads; |
| 57 | }; |
| 58 | |
| 59 | int32_t ThreadPoolTest::num_threads = 4; |
| 60 | |
| 61 | // Check that the thread pool actually runs tasks that you assign it. |
| 62 | TEST_F(ThreadPoolTest, CheckRun) { |
| 63 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 64 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 65 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 66 | static const int32_t num_tasks = num_threads * 4; |
| 67 | for (int32_t i = 0; i < num_tasks; ++i) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 68 | thread_pool.AddTask(self, new CountTask(&count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 69 | } |
| 70 | thread_pool.StartWorkers(self); |
| 71 | // Wait for tasks to complete. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 72 | thread_pool.Wait(self, true, false); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 73 | // Make sure that we finished all the work. |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 74 | EXPECT_EQ(num_tasks, count.load(std::memory_order_seq_cst)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(ThreadPoolTest, StopStart) { |
| 78 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 79 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 80 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 81 | static const int32_t num_tasks = num_threads * 4; |
| 82 | for (int32_t i = 0; i < num_tasks; ++i) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 83 | thread_pool.AddTask(self, new CountTask(&count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 84 | } |
| 85 | usleep(200); |
| 86 | // Check that no threads started prematurely. |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 87 | EXPECT_EQ(0, count.load(std::memory_order_seq_cst)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 88 | // Signal the threads to start processing tasks. |
| 89 | thread_pool.StartWorkers(self); |
| 90 | usleep(200); |
| 91 | thread_pool.StopWorkers(self); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 92 | AtomicInteger bad_count(0); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 93 | thread_pool.AddTask(self, new CountTask(&bad_count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 94 | usleep(200); |
| 95 | // Ensure that the task added after the workers were stopped doesn't get run. |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 96 | EXPECT_EQ(0, bad_count.load(std::memory_order_seq_cst)); |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 97 | // Allow tasks to finish up and delete themselves. |
| 98 | thread_pool.StartWorkers(self); |
Mathieu Chartier | 7947b54 | 2014-11-09 12:30:50 -0800 | [diff] [blame] | 99 | thread_pool.Wait(self, false, false); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 102 | TEST_F(ThreadPoolTest, StopWait) { |
| 103 | Thread* self = Thread::Current(); |
| 104 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
| 105 | |
| 106 | AtomicInteger count(0); |
| 107 | static const int32_t num_tasks = num_threads * 100; |
| 108 | for (int32_t i = 0; i < num_tasks; ++i) { |
| 109 | thread_pool.AddTask(self, new CountTask(&count)); |
| 110 | } |
| 111 | |
| 112 | // Signal the threads to start processing tasks. |
| 113 | thread_pool.StartWorkers(self); |
| 114 | usleep(200); |
| 115 | thread_pool.StopWorkers(self); |
| 116 | |
| 117 | thread_pool.Wait(self, false, false); // We should not deadlock here. |
Andreas Gampe | 1013bde | 2016-11-18 15:06:21 -0800 | [diff] [blame] | 118 | |
| 119 | // Drain the task list. Note: we have to restart here, as no tasks will be finished when |
| 120 | // the pool is stopped. |
| 121 | thread_pool.StartWorkers(self); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 122 | thread_pool.Wait(self, /* do_work= */ true, false); |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 125 | class TreeTask : public Task { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 126 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 127 | TreeTask(ThreadPool* const thread_pool, AtomicInteger* count, int depth) |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 128 | : thread_pool_(thread_pool), |
| 129 | count_(count), |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 130 | depth_(depth) {} |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 131 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 132 | void Run(Thread* self) override { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 133 | if (depth_ > 1) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 134 | thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1)); |
| 135 | thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 136 | } |
| 137 | // Increment the counter which keeps track of work completed. |
| 138 | ++*count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 141 | void Finalize() override { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 142 | delete this; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | ThreadPool* const thread_pool_; |
| 147 | AtomicInteger* const count_; |
| 148 | const int depth_; |
| 149 | }; |
| 150 | |
| 151 | // Test that adding new tasks from within a task works. |
| 152 | TEST_F(ThreadPoolTest, RecursiveTest) { |
| 153 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 154 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 155 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 156 | static const int depth = 8; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 157 | thread_pool.AddTask(self, new TreeTask(&thread_pool, &count, depth)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 158 | thread_pool.StartWorkers(self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | thread_pool.Wait(self, true, false); |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 160 | EXPECT_EQ((1 << depth) - 1, count.load(std::memory_order_seq_cst)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 163 | class PeerTask : public Task { |
| 164 | public: |
| 165 | PeerTask() {} |
| 166 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 167 | void Run(Thread* self) override { |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 168 | ScopedObjectAccess soa(self); |
| 169 | CHECK(self->GetPeer() != nullptr); |
| 170 | } |
| 171 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 172 | void Finalize() override { |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 173 | delete this; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | class NoPeerTask : public Task { |
| 178 | public: |
| 179 | NoPeerTask() {} |
| 180 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 181 | void Run(Thread* self) override { |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 182 | ScopedObjectAccess soa(self); |
| 183 | CHECK(self->GetPeer() == nullptr); |
| 184 | } |
| 185 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 186 | void Finalize() override { |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 187 | delete this; |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | // Tests for create_peer functionality. |
| 192 | TEST_F(ThreadPoolTest, PeerTest) { |
| 193 | Thread* self = Thread::Current(); |
| 194 | { |
| 195 | ThreadPool thread_pool("Thread pool test thread pool", 1); |
| 196 | thread_pool.AddTask(self, new NoPeerTask()); |
| 197 | thread_pool.StartWorkers(self); |
| 198 | thread_pool.Wait(self, false, false); |
| 199 | } |
| 200 | |
| 201 | { |
| 202 | // To create peers, the runtime needs to be started. |
| 203 | self->TransitionFromSuspendedToRunnable(); |
| 204 | bool started = runtime_->Start(); |
| 205 | ASSERT_TRUE(started); |
| 206 | |
| 207 | ThreadPool thread_pool("Thread pool test thread pool", 1, true); |
| 208 | thread_pool.AddTask(self, new PeerTask()); |
| 209 | thread_pool.StartWorkers(self); |
| 210 | thread_pool.Wait(self, false, false); |
| 211 | } |
| 212 | } |
| 213 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 214 | } // namespace art |