1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "task_processor.h"
18*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
19*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
20*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "thread_pool.h"
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
24*795d594fSAndroid Build Coastguard Worker namespace gc {
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker class TaskProcessorTest : public CommonRuntimeTest {
27*795d594fSAndroid Build Coastguard Worker public:
28*795d594fSAndroid Build Coastguard Worker };
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker class RecursiveTask : public HeapTask {
31*795d594fSAndroid Build Coastguard Worker public:
RecursiveTask(TaskProcessor * task_processor,Atomic<size_t> * counter,size_t max_recursion)32*795d594fSAndroid Build Coastguard Worker RecursiveTask(TaskProcessor* task_processor, Atomic<size_t>* counter, size_t max_recursion)
33*795d594fSAndroid Build Coastguard Worker : HeapTask(NanoTime() + MsToNs(10)), task_processor_(task_processor), counter_(counter),
34*795d594fSAndroid Build Coastguard Worker max_recursion_(max_recursion) {
35*795d594fSAndroid Build Coastguard Worker }
Run(Thread * self)36*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override {
37*795d594fSAndroid Build Coastguard Worker if (max_recursion_ > 0) {
38*795d594fSAndroid Build Coastguard Worker task_processor_->AddTask(self,
39*795d594fSAndroid Build Coastguard Worker new RecursiveTask(task_processor_, counter_, max_recursion_ - 1));
40*795d594fSAndroid Build Coastguard Worker counter_->fetch_add(1U, std::memory_order_seq_cst);
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker private:
45*795d594fSAndroid Build Coastguard Worker TaskProcessor* const task_processor_;
46*795d594fSAndroid Build Coastguard Worker Atomic<size_t>* const counter_;
47*795d594fSAndroid Build Coastguard Worker const size_t max_recursion_;
48*795d594fSAndroid Build Coastguard Worker };
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker class WorkUntilDoneTask : public SelfDeletingTask {
51*795d594fSAndroid Build Coastguard Worker public:
WorkUntilDoneTask(TaskProcessor * task_processor,Atomic<bool> * done_running)52*795d594fSAndroid Build Coastguard Worker WorkUntilDoneTask(TaskProcessor* task_processor, Atomic<bool>* done_running)
53*795d594fSAndroid Build Coastguard Worker : task_processor_(task_processor), done_running_(done_running) {
54*795d594fSAndroid Build Coastguard Worker }
Run(Thread * self)55*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override {
56*795d594fSAndroid Build Coastguard Worker task_processor_->RunAllTasks(self);
57*795d594fSAndroid Build Coastguard Worker done_running_->store(true, std::memory_order_seq_cst);
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker private:
61*795d594fSAndroid Build Coastguard Worker TaskProcessor* const task_processor_;
62*795d594fSAndroid Build Coastguard Worker Atomic<bool>* done_running_;
63*795d594fSAndroid Build Coastguard Worker };
64*795d594fSAndroid Build Coastguard Worker
TEST_F(TaskProcessorTest,Interrupt)65*795d594fSAndroid Build Coastguard Worker TEST_F(TaskProcessorTest, Interrupt) {
66*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ThreadPool> thread_pool(ThreadPool::Create("task processor test", 1U));
67*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
68*795d594fSAndroid Build Coastguard Worker TaskProcessor task_processor;
69*795d594fSAndroid Build Coastguard Worker static constexpr size_t kRecursion = 10;
70*795d594fSAndroid Build Coastguard Worker Atomic<bool> done_running(false);
71*795d594fSAndroid Build Coastguard Worker Atomic<size_t> counter(0);
72*795d594fSAndroid Build Coastguard Worker task_processor.AddTask(self, new RecursiveTask(&task_processor, &counter, kRecursion));
73*795d594fSAndroid Build Coastguard Worker task_processor.Start(self);
74*795d594fSAndroid Build Coastguard Worker // Add a task which will wait until interrupted to the thread pool.
75*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new WorkUntilDoneTask(&task_processor, &done_running));
76*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
77*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(done_running);
78*795d594fSAndroid Build Coastguard Worker // Wait until all the tasks are done, but since we didn't interrupt, done_running should be 0.
79*795d594fSAndroid Build Coastguard Worker while (counter.load(std::memory_order_seq_cst) != kRecursion) {
80*795d594fSAndroid Build Coastguard Worker usleep(10);
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(done_running);
83*795d594fSAndroid Build Coastguard Worker task_processor.Stop(self);
84*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, false);
85*795d594fSAndroid Build Coastguard Worker // After the interrupt and wait, the WorkUntilInterruptedTasktask should have terminated and
86*795d594fSAndroid Build Coastguard Worker // set done_running_ to true.
87*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(done_running.load(std::memory_order_seq_cst));
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker // Test that we finish remaining tasks before returning from RunTasksUntilInterrupted.
90*795d594fSAndroid Build Coastguard Worker counter.store(0, std::memory_order_seq_cst);
91*795d594fSAndroid Build Coastguard Worker done_running.store(false, std::memory_order_seq_cst);
92*795d594fSAndroid Build Coastguard Worker // Self interrupt before any of the other tasks run, but since we added them we should keep on
93*795d594fSAndroid Build Coastguard Worker // working until all the tasks are completed.
94*795d594fSAndroid Build Coastguard Worker task_processor.Stop(self);
95*795d594fSAndroid Build Coastguard Worker task_processor.AddTask(self, new RecursiveTask(&task_processor, &counter, kRecursion));
96*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new WorkUntilDoneTask(&task_processor, &done_running));
97*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
98*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, false);
99*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(done_running.load(std::memory_order_seq_cst));
100*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(counter.load(std::memory_order_seq_cst), kRecursion);
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker
103*795d594fSAndroid Build Coastguard Worker class TestOrderTask : public HeapTask {
104*795d594fSAndroid Build Coastguard Worker public:
TestOrderTask(uint64_t expected_time,size_t expected_counter,size_t * counter)105*795d594fSAndroid Build Coastguard Worker TestOrderTask(uint64_t expected_time, size_t expected_counter, size_t* counter)
106*795d594fSAndroid Build Coastguard Worker : HeapTask(expected_time), expected_counter_(expected_counter), counter_(counter) {
107*795d594fSAndroid Build Coastguard Worker }
Run(Thread * thread)108*795d594fSAndroid Build Coastguard Worker void Run([[maybe_unused]] Thread* thread) override {
109*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*counter_, expected_counter_);
110*795d594fSAndroid Build Coastguard Worker ++*counter_;
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker
113*795d594fSAndroid Build Coastguard Worker private:
114*795d594fSAndroid Build Coastguard Worker const size_t expected_counter_;
115*795d594fSAndroid Build Coastguard Worker size_t* const counter_;
116*795d594fSAndroid Build Coastguard Worker };
117*795d594fSAndroid Build Coastguard Worker
TEST_F(TaskProcessorTest,Ordering)118*795d594fSAndroid Build Coastguard Worker TEST_F(TaskProcessorTest, Ordering) {
119*795d594fSAndroid Build Coastguard Worker static const size_t kNumTasks = 25;
120*795d594fSAndroid Build Coastguard Worker const uint64_t current_time = NanoTime();
121*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
122*795d594fSAndroid Build Coastguard Worker TaskProcessor task_processor;
123*795d594fSAndroid Build Coastguard Worker task_processor.Stop(self);
124*795d594fSAndroid Build Coastguard Worker size_t counter = 0;
125*795d594fSAndroid Build Coastguard Worker std::vector<std::pair<uint64_t, size_t>> orderings;
126*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < kNumTasks; ++i) {
127*795d594fSAndroid Build Coastguard Worker orderings.push_back(std::make_pair(current_time + MsToNs(10U * i), i));
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < kNumTasks; ++i) {
130*795d594fSAndroid Build Coastguard Worker std::swap(orderings[i], orderings[(i * 87654231 + 12345) % orderings.size()]);
131*795d594fSAndroid Build Coastguard Worker }
132*795d594fSAndroid Build Coastguard Worker for (const auto& pair : orderings) {
133*795d594fSAndroid Build Coastguard Worker auto* task = new TestOrderTask(pair.first, pair.second, &counter);
134*795d594fSAndroid Build Coastguard Worker task_processor.AddTask(self, task);
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ThreadPool> thread_pool(ThreadPool::Create("task processor test", 1U));
137*795d594fSAndroid Build Coastguard Worker Atomic<bool> done_running(false);
138*795d594fSAndroid Build Coastguard Worker // Add a task which will wait until interrupted to the thread pool.
139*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new WorkUntilDoneTask(&task_processor, &done_running));
140*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(done_running.load(std::memory_order_seq_cst));
141*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
142*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, false);
143*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(done_running.load(std::memory_order_seq_cst));
144*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(counter, kNumTasks);
145*795d594fSAndroid Build Coastguard Worker }
146*795d594fSAndroid Build Coastguard Worker
147*795d594fSAndroid Build Coastguard Worker } // namespace gc
148*795d594fSAndroid Build Coastguard Worker } // namespace art
149