1 /*
2 * Copyright (C) 2013 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
17 #include <gtest/gtest.h>
18
19 #include <errno.h>
20 #include <sched.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23
24 #include "utils.h"
25
child_fn(void * i_ptr)26 static int child_fn(void* i_ptr) {
27 *reinterpret_cast<int*>(i_ptr) = 42;
28 return 123;
29 }
30
31 #if defined(__BIONIC__)
TEST(sched,clone)32 TEST(sched, clone) {
33 void* child_stack[1024];
34
35 int i = 0;
36 pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i);
37
38 int status;
39 ASSERT_EQ(tid, TEMP_FAILURE_RETRY(waitpid(tid, &status, __WCLONE)));
40
41 ASSERT_EQ(42, i);
42
43 ASSERT_TRUE(WIFEXITED(status));
44 ASSERT_EQ(123, WEXITSTATUS(status));
45 }
46 #else
47 // For glibc, any call to clone with CLONE_VM set will cause later pthread
48 // calls in the same process to misbehave.
49 // See https://sourceware.org/bugzilla/show_bug.cgi?id=10311 for more details.
TEST(sched,clone)50 TEST(sched, clone) {
51 // In order to enumerate all possible tests for CTS, create an empty test.
52 GTEST_SKIP() << "glibc is broken";
53 }
54 #endif
55
TEST(sched,clone_errno)56 TEST(sched, clone_errno) {
57 // Check that our hand-written clone assembler sets errno correctly on failure.
58 uintptr_t fake_child_stack[16];
59 errno = 0;
60 // If CLONE_THREAD is set, CLONE_SIGHAND must be set too.
61 ASSERT_EQ(-1, clone(child_fn, &fake_child_stack[16], CLONE_THREAD, nullptr));
62 ASSERT_ERRNO(EINVAL);
63 }
64
TEST(sched,clone_null_child_stack)65 TEST(sched, clone_null_child_stack) {
66 int i = 0;
67 errno = 0;
68 ASSERT_EQ(-1, clone(child_fn, nullptr, CLONE_VM, &i));
69 ASSERT_ERRNO(EINVAL);
70 }
71
TEST(sched,cpu_set)72 TEST(sched, cpu_set) {
73 cpu_set_t set;
74
75 CPU_ZERO(&set);
76 CPU_SET(0, &set);
77 CPU_SET(17, &set);
78 for (int i = 0; i < CPU_SETSIZE; i++) {
79 ASSERT_EQ(i == 0 || i == 17, CPU_ISSET(i, &set));
80 }
81
82 // We should fail silently if we try to set/test outside the range.
83 CPU_SET(CPU_SETSIZE, &set);
84 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
85 }
86
TEST(sched,cpu_count)87 TEST(sched, cpu_count) {
88 cpu_set_t set;
89
90 CPU_ZERO(&set);
91 ASSERT_EQ(0, CPU_COUNT(&set));
92 CPU_SET(2, &set);
93 CPU_SET(10, &set);
94 ASSERT_EQ(2, CPU_COUNT(&set));
95 CPU_CLR(10, &set);
96 ASSERT_EQ(1, CPU_COUNT(&set));
97 }
98
TEST(sched,cpu_zero)99 TEST(sched, cpu_zero) {
100 cpu_set_t set;
101
102 CPU_ZERO(&set);
103 ASSERT_EQ(0, CPU_COUNT(&set));
104 for (int i = 0; i < CPU_SETSIZE; i++) {
105 ASSERT_FALSE(CPU_ISSET(i, &set));
106 }
107 }
108
TEST(sched,cpu_clr)109 TEST(sched, cpu_clr) {
110 cpu_set_t set;
111
112 CPU_ZERO(&set);
113 CPU_SET(0, &set);
114 CPU_SET(1, &set);
115 for (int i = 0; i < CPU_SETSIZE; i++) {
116 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set));
117 }
118 CPU_CLR(1, &set);
119 for (int i = 0; i < CPU_SETSIZE; i++) {
120 ASSERT_EQ(i == 0, CPU_ISSET(i, &set));
121 }
122
123 // We should fail silently if we try to clear/test outside the range.
124 CPU_CLR(CPU_SETSIZE, &set);
125 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
126 }
127
TEST(sched,cpu_equal)128 TEST(sched, cpu_equal) {
129 cpu_set_t set1;
130 cpu_set_t set2;
131
132 CPU_ZERO(&set1);
133 CPU_ZERO(&set2);
134 CPU_SET(1, &set1);
135 ASSERT_FALSE(CPU_EQUAL(&set1, &set2));
136 CPU_SET(1, &set2);
137 ASSERT_TRUE(CPU_EQUAL(&set1, &set2));
138 }
139
TEST(sched,cpu_op)140 TEST(sched, cpu_op) {
141 cpu_set_t set1;
142 cpu_set_t set2;
143 cpu_set_t set3;
144
145 CPU_ZERO(&set1);
146 CPU_ZERO(&set2);
147 CPU_ZERO(&set3);
148 CPU_SET(0, &set1);
149 CPU_SET(0, &set2);
150 CPU_SET(1, &set2);
151
152 CPU_AND(&set3, &set1, &set2);
153 for (int i = 0; i < CPU_SETSIZE; i++) {
154 ASSERT_EQ(i == 0, CPU_ISSET(i, &set3));
155 }
156
157 CPU_XOR(&set3, &set1, &set2);
158 for (int i = 0; i < CPU_SETSIZE; i++) {
159 ASSERT_EQ(i == 1, CPU_ISSET(i, &set3));
160 }
161
162 CPU_OR(&set3, &set1, &set2);
163 for (int i = 0; i < CPU_SETSIZE; i++) {
164 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set3));
165 }
166 }
167
168
TEST(sched,cpu_alloc_small)169 TEST(sched, cpu_alloc_small) {
170 cpu_set_t* set = CPU_ALLOC(17);
171 size_t size = CPU_ALLOC_SIZE(17);
172
173 CPU_ZERO_S(size, set);
174 ASSERT_EQ(0, CPU_COUNT_S(size, set));
175 CPU_SET_S(16, size, set);
176 ASSERT_TRUE(CPU_ISSET_S(16, size, set));
177
178 CPU_FREE(set);
179 }
180
TEST(sched,cpu_alloc_big)181 TEST(sched, cpu_alloc_big) {
182 cpu_set_t* set = CPU_ALLOC(10 * CPU_SETSIZE);
183 size_t size = CPU_ALLOC_SIZE(10 * CPU_SETSIZE);
184
185 CPU_ZERO_S(size, set);
186 ASSERT_EQ(0, CPU_COUNT_S(size, set));
187 CPU_SET_S(CPU_SETSIZE, size, set);
188 ASSERT_TRUE(CPU_ISSET_S(CPU_SETSIZE, size, set));
189
190 CPU_FREE(set);
191 }
192
TEST(sched,cpu_s_macros)193 TEST(sched, cpu_s_macros) {
194 int set_size = 64;
195 size_t size = CPU_ALLOC_SIZE(set_size);
196 cpu_set_t* set = CPU_ALLOC(set_size);
197
198 CPU_ZERO_S(size, set);
199 for (int i = 0; i < set_size; i++) {
200 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
201 CPU_SET_S(i, size, set);
202 ASSERT_TRUE(CPU_ISSET_S(i, size, set));
203 ASSERT_EQ(i + 1, CPU_COUNT_S(size, set));
204 }
205
206 for (int i = 0; i < set_size; i++) {
207 CPU_CLR_S(i, size, set);
208 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
209 ASSERT_EQ(set_size - i - 1, CPU_COUNT_S(size, set));
210 }
211
212 CPU_FREE(set);
213 }
214
TEST(sched,cpu_op_s_macros)215 TEST(sched, cpu_op_s_macros) {
216 int set_size1 = 64;
217 int set_size2 = set_size1 * 2;
218 int set_size3 = set_size1 * 3;
219 size_t size1 = CPU_ALLOC_SIZE(set_size1);
220 size_t size2 = CPU_ALLOC_SIZE(set_size2);
221 size_t size3 = CPU_ALLOC_SIZE(set_size3);
222
223 cpu_set_t* set1 = CPU_ALLOC(set_size1);
224 cpu_set_t* set2 = CPU_ALLOC(set_size2);
225 cpu_set_t* set3 = CPU_ALLOC(set_size3);
226 CPU_ZERO_S(size1, set1);
227 CPU_ZERO_S(size2, set2);
228 CPU_ZERO_S(size3, set3);
229
230 CPU_SET_S(0, size1, set1);
231 CPU_SET_S(0, size2, set2);
232 CPU_SET_S(1, size3, set2);
233
234 CPU_AND_S(size1, set3, set1, set2);
235 for (int i = 0; i < set_size3; i++) {
236 ASSERT_EQ(i == 0, CPU_ISSET_S(i, size3, set3));
237 }
238
239 CPU_OR_S(size1, set3, set1, set2);
240 for (int i = 0; i < set_size3; i++) {
241 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET_S(i, size3, set3));
242 }
243
244 CPU_XOR_S(size1, set3, set1, set2);
245 for (int i = 0; i < set_size3; i++) {
246 ASSERT_EQ(i == 1, CPU_ISSET_S(i, size3, set3));
247 }
248
249 CPU_FREE(set1);
250 CPU_FREE(set2);
251 CPU_FREE(set3);
252 }
253
TEST(sched,cpu_equal_s)254 TEST(sched, cpu_equal_s) {
255 int set_size1 = 64;
256 int set_size2 = set_size1 * 2;
257 size_t size1 = CPU_ALLOC_SIZE(set_size1);
258 size_t size2 = CPU_ALLOC_SIZE(set_size2);
259
260 cpu_set_t* set1 = CPU_ALLOC(set_size1);
261 cpu_set_t* set2 = CPU_ALLOC(set_size2);
262
263 CPU_ZERO_S(size1, set1);
264 CPU_ZERO_S(size2, set2);
265
266 CPU_SET_S(0, size1, set1);
267 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set1));
268 ASSERT_FALSE(CPU_EQUAL_S(size1, set1, set2));
269 CPU_SET_S(0, size2, set2);
270 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set2));
271
272 CPU_FREE(set1);
273 CPU_FREE(set2);
274 }
275
TEST(sched,sched_get_priority_min_sched_get_priority_max)276 TEST(sched, sched_get_priority_min_sched_get_priority_max) {
277 EXPECT_LE(sched_get_priority_min(SCHED_BATCH), sched_get_priority_max(SCHED_BATCH));
278 EXPECT_LE(sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO));
279 EXPECT_LE(sched_get_priority_min(SCHED_IDLE), sched_get_priority_max(SCHED_IDLE));
280 EXPECT_LE(sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER));
281 EXPECT_LE(sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR));
282 }
283
TEST(sched,sched_getscheduler_sched_setscheduler)284 TEST(sched, sched_getscheduler_sched_setscheduler) {
285 // POSIX: "If pid is zero, the scheduling policy shall be returned for the
286 // calling process".
287 ASSERT_EQ(sched_getscheduler(getpid()), sched_getscheduler(0));
288
289 const int original_policy = sched_getscheduler(getpid());
290 sched_param p = {};
291 p.sched_priority = sched_get_priority_min(original_policy);
292 errno = 0;
293 ASSERT_EQ(-1, sched_setscheduler(getpid(), INT_MAX, &p));
294 ASSERT_ERRNO(EINVAL);
295
296 ASSERT_EQ(0, sched_getparam(getpid(), &p));
297 ASSERT_EQ(original_policy, sched_setscheduler(getpid(), SCHED_BATCH, &p));
298 // POSIX says this should return the previous policy (here SCHED_BATCH),
299 // but the Linux system call doesn't, and the glibc wrapper doesn't correct
300 // this (the "returns 0" behavior is even documented on the man page in
301 // the BUGS section). This was our historical behavior too, so in the
302 // absence of reasons to break compatibility with ourselves and glibc, we
303 // don't behave as POSIX specifies. http://b/26203902.
304 ASSERT_EQ(0, sched_setscheduler(getpid(), original_policy, &p));
305 }
306
TEST(sched,sched_getaffinity_failure)307 TEST(sched, sched_getaffinity_failure) {
308 #pragma clang diagnostic push
309 #pragma clang diagnostic ignored "-Wnonnull"
310 ASSERT_EQ(-1, sched_getaffinity(getpid(), 0, nullptr));
311 #pragma clang diagnostic pop
312 }
313