1 // Copyright 2019 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "marl_test.h"
16
17 #include "marl/thread.h"
18
19 namespace {
20
core(int idx)21 marl::Thread::Core core(int idx) {
22 marl::Thread::Core c;
23 c.pthread.index = static_cast<uint16_t>(idx);
24 return c;
25 }
26
27 } // anonymous namespace
28
TEST_F(WithoutBoundScheduler,ThreadAffinityCount)29 TEST_F(WithoutBoundScheduler, ThreadAffinityCount) {
30 auto affinity = marl::Thread::Affinity(
31 {
32 core(10),
33 core(20),
34 core(30),
35 core(40),
36 },
37 allocator);
38 EXPECT_EQ(affinity.count(), 4U);
39 }
40
TEST_F(WithoutBoundScheduler,ThreadAdd)41 TEST_F(WithoutBoundScheduler, ThreadAdd) {
42 auto affinity = marl::Thread::Affinity(
43 {
44 core(10),
45 core(20),
46 core(30),
47 core(40),
48 },
49 allocator);
50
51 affinity
52 .add(marl::Thread::Affinity(
53 {
54 core(25),
55 core(15),
56 },
57 allocator))
58 .add(marl::Thread::Affinity({core(35)}, allocator));
59
60 EXPECT_EQ(affinity.count(), 7U);
61 EXPECT_EQ(affinity[0], core(10));
62 EXPECT_EQ(affinity[1], core(15));
63 EXPECT_EQ(affinity[2], core(20));
64 EXPECT_EQ(affinity[3], core(25));
65 EXPECT_EQ(affinity[4], core(30));
66 EXPECT_EQ(affinity[5], core(35));
67 EXPECT_EQ(affinity[6], core(40));
68 }
69
TEST_F(WithoutBoundScheduler,ThreadRemove)70 TEST_F(WithoutBoundScheduler, ThreadRemove) {
71 auto affinity = marl::Thread::Affinity(
72 {
73 core(10),
74 core(20),
75 core(30),
76 core(40),
77 },
78 allocator);
79
80 affinity
81 .remove(marl::Thread::Affinity(
82 {
83 core(25),
84 core(20),
85 },
86 allocator))
87 .remove(marl::Thread::Affinity({core(40)}, allocator));
88
89 EXPECT_EQ(affinity.count(), 2U);
90 EXPECT_EQ(affinity[0], core(10));
91 EXPECT_EQ(affinity[1], core(30));
92 }
93
TEST_F(WithoutBoundScheduler,ThreadAffinityAllCountNonzero)94 TEST_F(WithoutBoundScheduler, ThreadAffinityAllCountNonzero) {
95 auto affinity = marl::Thread::Affinity::all(allocator);
96 if (marl::Thread::Affinity::supported) {
97 EXPECT_NE(affinity.count(), 0U);
98 } else {
99 EXPECT_EQ(affinity.count(), 0U);
100 }
101 }
102
TEST_F(WithoutBoundScheduler,ThreadAffinityFromVector)103 TEST_F(WithoutBoundScheduler, ThreadAffinityFromVector) {
104 marl::containers::vector<marl::Thread::Core, 32> cores(allocator);
105 cores.push_back(core(10));
106 cores.push_back(core(20));
107 cores.push_back(core(30));
108 cores.push_back(core(40));
109 auto affinity = marl::Thread::Affinity(cores, allocator);
110 EXPECT_EQ(affinity.count(), cores.size());
111 EXPECT_EQ(affinity[0], core(10));
112 EXPECT_EQ(affinity[1], core(20));
113 EXPECT_EQ(affinity[2], core(30));
114 EXPECT_EQ(affinity[3], core(40));
115 }
116
TEST_F(WithoutBoundScheduler,ThreadAffinityPolicyOneOf)117 TEST_F(WithoutBoundScheduler, ThreadAffinityPolicyOneOf) {
118 auto all = marl::Thread::Affinity(
119 {
120 core(10),
121 core(20),
122 core(30),
123 core(40),
124 },
125 allocator);
126
127 auto policy =
128 marl::Thread::Affinity::Policy::oneOf(std::move(all), allocator);
129 EXPECT_EQ(policy->get(0, allocator).count(), 1U);
130 EXPECT_EQ(policy->get(0, allocator)[0].pthread.index, 10);
131 EXPECT_EQ(policy->get(1, allocator).count(), 1U);
132 EXPECT_EQ(policy->get(1, allocator)[0].pthread.index, 20);
133 EXPECT_EQ(policy->get(2, allocator).count(), 1U);
134 EXPECT_EQ(policy->get(2, allocator)[0].pthread.index, 30);
135 EXPECT_EQ(policy->get(3, allocator).count(), 1U);
136 EXPECT_EQ(policy->get(3, allocator)[0].pthread.index, 40);
137 }
138