blob: 396d3b734eb76cb9bf3b74539a6322532a04cbc6 [file] [log] [blame]
Elliott Hughes53bfdae2013-10-18 19:39:09 -07001/*
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
Elliott Hughes95646e62023-09-21 14:11:19 -070024#include "utils.h"
25
Elliott Hughes53bfdae2013-10-18 19:39:09 -070026static int child_fn(void* i_ptr) {
27 *reinterpret_cast<int*>(i_ptr) = 42;
28 return 123;
29}
30
Greg Hackmannfb23fa32016-03-23 17:15:02 -070031#if defined(__BIONIC__)
Elliott Hughes53bfdae2013-10-18 19:39:09 -070032TEST(sched, clone) {
33 void* child_stack[1024];
34
35 int i = 0;
Christopher Ferris13613132013-10-28 15:24:04 -070036 pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i);
Elliott Hughes53bfdae2013-10-18 19:39:09 -070037
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}
Christopher Ferris13613132013-10-28 15:24:04 -070046#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.
50TEST(sched, clone) {
51 // In order to enumerate all possible tests for CTS, create an empty test.
Elliott Hughesbcaa4542019-03-08 15:20:23 -080052 GTEST_SKIP() << "glibc is broken";
Christopher Ferris13613132013-10-28 15:24:04 -070053}
54#endif
Calin Juravleb7437902014-04-29 20:25:26 +010055
Elliott Hughes954cf0d2014-05-08 19:00:23 -070056TEST(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;
Elliott Hughese104a2e2016-05-06 15:55:36 -070060 // If CLONE_THREAD is set, CLONE_SIGHAND must be set too.
Yi Kong32bc0fc2018-08-02 17:31:13 -070061 ASSERT_EQ(-1, clone(child_fn, &fake_child_stack[16], CLONE_THREAD, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -070062 ASSERT_ERRNO(EINVAL);
Elliott Hughes954cf0d2014-05-08 19:00:23 -070063}
64
Greg Hackmannfb23fa32016-03-23 17:15:02 -070065TEST(sched, clone_null_child_stack) {
66 int i = 0;
67 errno = 0;
68 ASSERT_EQ(-1, clone(child_fn, nullptr, CLONE_VM, &i));
Elliott Hughes95646e62023-09-21 14:11:19 -070069 ASSERT_ERRNO(EINVAL);
Greg Hackmannfb23fa32016-03-23 17:15:02 -070070}
71
Calin Juravleb7437902014-04-29 20:25:26 +010072TEST(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
87TEST(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
99TEST(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
109TEST(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
128TEST(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
140TEST(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
169TEST(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
181TEST(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
193TEST(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
215TEST(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
Elliott Hughes2d367502014-04-30 10:45:35 -0700223 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);
Calin Juravleb7437902014-04-29 20:25:26 +0100226 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
254TEST(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
Elliott Hughes2d367502014-04-30 10:45:35 -0700260 cpu_set_t* set1 = CPU_ALLOC(set_size1);
261 cpu_set_t* set2 = CPU_ALLOC(set_size2);
Calin Juravleb7437902014-04-29 20:25:26 +0100262
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}
Elliott Hughes2950f132017-08-01 23:02:48 -0700275
276TEST(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
284TEST(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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700294 ASSERT_ERRNO(EINVAL);
Elliott Hughes2950f132017-08-01 23:02:48 -0700295
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}
Elliott Hughes7cebf832020-08-12 14:25:41 -0700306
307TEST(sched, sched_getaffinity_failure) {
Elliott Hughese117d6e2024-11-14 17:31:13 +0000308 // Trivial test of the errno-preserving/returning behavior.
zijunzhao21463032023-03-18 00:38:14 +0000309#pragma clang diagnostic push
310#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes7cebf832020-08-12 14:25:41 -0700311 ASSERT_EQ(-1, sched_getaffinity(getpid(), 0, nullptr));
Elliott Hughese117d6e2024-11-14 17:31:13 +0000312 ASSERT_ERRNO(EINVAL);
313#pragma clang diagnostic pop
314}
315
316TEST(sched, sched_setaffinity_failure) {
317 // Trivial test of the errno-preserving/returning behavior.
318#pragma clang diagnostic push
319#pragma clang diagnostic ignored "-Wnonnull"
320 ASSERT_EQ(-1, sched_setaffinity(getpid(), 0, nullptr));
321 ASSERT_ERRNO(EINVAL);
zijunzhao21463032023-03-18 00:38:14 +0000322#pragma clang diagnostic pop
Elliott Hughes7cebf832020-08-12 14:25:41 -0700323}