1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2012 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 "barrier.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <string>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h"
22*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
23*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "thread_pool.h"
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
28*795d594fSAndroid Build Coastguard Worker class CheckWaitTask : public Task {
29*795d594fSAndroid Build Coastguard Worker public:
CheckWaitTask(Barrier * barrier,AtomicInteger * count1,AtomicInteger * count2)30*795d594fSAndroid Build Coastguard Worker CheckWaitTask(Barrier* barrier, AtomicInteger* count1, AtomicInteger* count2)
31*795d594fSAndroid Build Coastguard Worker : barrier_(barrier),
32*795d594fSAndroid Build Coastguard Worker count1_(count1),
33*795d594fSAndroid Build Coastguard Worker count2_(count2) {}
34*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)35*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override {
36*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Before barrier" << *self;
37*795d594fSAndroid Build Coastguard Worker ++*count1_;
38*795d594fSAndroid Build Coastguard Worker barrier_->Wait(self);
39*795d594fSAndroid Build Coastguard Worker ++*count2_;
40*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "After barrier" << *self;
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker
Finalize()43*795d594fSAndroid Build Coastguard Worker void Finalize() override {
44*795d594fSAndroid Build Coastguard Worker delete this;
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker
47*795d594fSAndroid Build Coastguard Worker private:
48*795d594fSAndroid Build Coastguard Worker Barrier* const barrier_;
49*795d594fSAndroid Build Coastguard Worker AtomicInteger* const count1_;
50*795d594fSAndroid Build Coastguard Worker AtomicInteger* const count2_;
51*795d594fSAndroid Build Coastguard Worker };
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker class BarrierTest : public CommonRuntimeTest {
54*795d594fSAndroid Build Coastguard Worker public:
BarrierTest()55*795d594fSAndroid Build Coastguard Worker BarrierTest() {
56*795d594fSAndroid Build Coastguard Worker use_boot_image_ = true; // Make the Runtime creation cheaper.
57*795d594fSAndroid Build Coastguard Worker }
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker static int32_t num_threads;
60*795d594fSAndroid Build Coastguard Worker };
61*795d594fSAndroid Build Coastguard Worker
62*795d594fSAndroid Build Coastguard Worker int32_t BarrierTest::num_threads = 4;
63*795d594fSAndroid Build Coastguard Worker
64*795d594fSAndroid Build Coastguard Worker // Check that barrier wait and barrier increment work.
TEST_F(BarrierTest,CheckWait)65*795d594fSAndroid Build Coastguard Worker TEST_F(BarrierTest, CheckWait) {
66*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
67*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ThreadPool> thread_pool(
68*795d594fSAndroid Build Coastguard Worker ThreadPool::Create("Barrier test thread pool", num_threads));
69*795d594fSAndroid Build Coastguard Worker Barrier barrier(num_threads + 1); // One extra Wait() in main thread.
70*795d594fSAndroid Build Coastguard Worker Barrier timeout_barrier(0); // Only used for sleeping on timeout.
71*795d594fSAndroid Build Coastguard Worker AtomicInteger count1(0);
72*795d594fSAndroid Build Coastguard Worker AtomicInteger count2(0);
73*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < num_threads; ++i) {
74*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new CheckWaitTask(&barrier, &count1, &count2));
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
77*795d594fSAndroid Build Coastguard Worker while (count1.load(std::memory_order_relaxed) != num_threads) {
78*795d594fSAndroid Build Coastguard Worker timeout_barrier.Increment(self, 1, 100); // sleep 100 msecs
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker // Count 2 should still be zero since no thread should have gone past the barrier.
81*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(0, count2.load(std::memory_order_relaxed));
82*795d594fSAndroid Build Coastguard Worker // Perform one additional Wait(), allowing pool threads to proceed.
83*795d594fSAndroid Build Coastguard Worker barrier.Wait(self);
84*795d594fSAndroid Build Coastguard Worker // Wait for all the threads to finish.
85*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, false);
86*795d594fSAndroid Build Coastguard Worker // Both counts should be equal to num_threads now.
87*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(count1.load(std::memory_order_relaxed), num_threads);
88*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(count2.load(std::memory_order_relaxed), num_threads);
89*795d594fSAndroid Build Coastguard Worker timeout_barrier.Init(self, 0); // Reset to zero for destruction.
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
92*795d594fSAndroid Build Coastguard Worker class CheckPassTask : public Task {
93*795d594fSAndroid Build Coastguard Worker public:
CheckPassTask(Barrier * barrier,AtomicInteger * count,size_t subtasks)94*795d594fSAndroid Build Coastguard Worker CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
95*795d594fSAndroid Build Coastguard Worker : barrier_(barrier),
96*795d594fSAndroid Build Coastguard Worker count_(count),
97*795d594fSAndroid Build Coastguard Worker subtasks_(subtasks) {}
98*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)99*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override {
100*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < subtasks_; ++i) {
101*795d594fSAndroid Build Coastguard Worker ++*count_;
102*795d594fSAndroid Build Coastguard Worker // Pass through to next subtask.
103*795d594fSAndroid Build Coastguard Worker barrier_->Pass(self);
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
Finalize()107*795d594fSAndroid Build Coastguard Worker void Finalize() override {
108*795d594fSAndroid Build Coastguard Worker delete this;
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker private:
111*795d594fSAndroid Build Coastguard Worker Barrier* const barrier_;
112*795d594fSAndroid Build Coastguard Worker AtomicInteger* const count_;
113*795d594fSAndroid Build Coastguard Worker const size_t subtasks_;
114*795d594fSAndroid Build Coastguard Worker };
115*795d594fSAndroid Build Coastguard Worker
116*795d594fSAndroid Build Coastguard Worker // Check that barrier pass through works.
TEST_F(BarrierTest,CheckPass)117*795d594fSAndroid Build Coastguard Worker TEST_F(BarrierTest, CheckPass) {
118*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
119*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ThreadPool> thread_pool(
120*795d594fSAndroid Build Coastguard Worker ThreadPool::Create("Barrier test thread pool", num_threads));
121*795d594fSAndroid Build Coastguard Worker Barrier barrier(0);
122*795d594fSAndroid Build Coastguard Worker AtomicInteger count(0);
123*795d594fSAndroid Build Coastguard Worker const int32_t num_tasks = num_threads * 4;
124*795d594fSAndroid Build Coastguard Worker const int32_t num_sub_tasks = 128;
125*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < num_tasks; ++i) {
126*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
129*795d594fSAndroid Build Coastguard Worker const int32_t expected_total_tasks = num_sub_tasks * num_tasks;
130*795d594fSAndroid Build Coastguard Worker // Wait for all the tasks to complete using the barrier.
131*795d594fSAndroid Build Coastguard Worker barrier.Increment(self, expected_total_tasks);
132*795d594fSAndroid Build Coastguard Worker // The total number of completed tasks should be equal to expected_total_tasks.
133*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(count.load(std::memory_order_relaxed), expected_total_tasks);
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker
136*795d594fSAndroid Build Coastguard Worker } // namespace art
137