blob: 92d6c268e206989351295212bd933a3dff112dc8 [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
24static int child_fn(void* i_ptr) {
25 *reinterpret_cast<int*>(i_ptr) = 42;
26 return 123;
27}
28
Greg Hackmannfb23fa32016-03-23 17:15:02 -070029#if defined(__BIONIC__)
Elliott Hughes53bfdae2013-10-18 19:39:09 -070030TEST(sched, clone) {
31 void* child_stack[1024];
32
33 int i = 0;
Christopher Ferris13613132013-10-28 15:24:04 -070034 pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i);
Elliott Hughes53bfdae2013-10-18 19:39:09 -070035
36 int status;
37 ASSERT_EQ(tid, TEMP_FAILURE_RETRY(waitpid(tid, &status, __WCLONE)));
38
39 ASSERT_EQ(42, i);
40
41 ASSERT_TRUE(WIFEXITED(status));
42 ASSERT_EQ(123, WEXITSTATUS(status));
43}
Christopher Ferris13613132013-10-28 15:24:04 -070044#else
45// For glibc, any call to clone with CLONE_VM set will cause later pthread
46// calls in the same process to misbehave.
47// See https://sourceware.org/bugzilla/show_bug.cgi?id=10311 for more details.
48TEST(sched, clone) {
49 // In order to enumerate all possible tests for CTS, create an empty test.
50 GTEST_LOG_(INFO) << "This test does nothing.\n";
51}
52#endif
Calin Juravleb7437902014-04-29 20:25:26 +010053
Elliott Hughes954cf0d2014-05-08 19:00:23 -070054TEST(sched, clone_errno) {
55 // Check that our hand-written clone assembler sets errno correctly on failure.
56 uintptr_t fake_child_stack[16];
57 errno = 0;
58 ASSERT_EQ(-1, clone(NULL, &fake_child_stack[16], CLONE_THREAD, NULL));
59 ASSERT_EQ(EINVAL, errno);
60}
61
Greg Hackmannfb23fa32016-03-23 17:15:02 -070062TEST(sched, clone_null_child_stack) {
63 int i = 0;
64 errno = 0;
65 ASSERT_EQ(-1, clone(child_fn, nullptr, CLONE_VM, &i));
66 ASSERT_EQ(EINVAL, errno);
67}
68
Calin Juravleb7437902014-04-29 20:25:26 +010069TEST(sched, cpu_set) {
70 cpu_set_t set;
71
72 CPU_ZERO(&set);
73 CPU_SET(0, &set);
74 CPU_SET(17, &set);
75 for (int i = 0; i < CPU_SETSIZE; i++) {
76 ASSERT_EQ(i == 0 || i == 17, CPU_ISSET(i, &set));
77 }
78
79 // We should fail silently if we try to set/test outside the range.
80 CPU_SET(CPU_SETSIZE, &set);
81 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
82}
83
84TEST(sched, cpu_count) {
85 cpu_set_t set;
86
87 CPU_ZERO(&set);
88 ASSERT_EQ(0, CPU_COUNT(&set));
89 CPU_SET(2, &set);
90 CPU_SET(10, &set);
91 ASSERT_EQ(2, CPU_COUNT(&set));
92 CPU_CLR(10, &set);
93 ASSERT_EQ(1, CPU_COUNT(&set));
94}
95
96TEST(sched, cpu_zero) {
97 cpu_set_t set;
98
99 CPU_ZERO(&set);
100 ASSERT_EQ(0, CPU_COUNT(&set));
101 for (int i = 0; i < CPU_SETSIZE; i++) {
102 ASSERT_FALSE(CPU_ISSET(i, &set));
103 }
104}
105
106TEST(sched, cpu_clr) {
107 cpu_set_t set;
108
109 CPU_ZERO(&set);
110 CPU_SET(0, &set);
111 CPU_SET(1, &set);
112 for (int i = 0; i < CPU_SETSIZE; i++) {
113 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set));
114 }
115 CPU_CLR(1, &set);
116 for (int i = 0; i < CPU_SETSIZE; i++) {
117 ASSERT_EQ(i == 0, CPU_ISSET(i, &set));
118 }
119
120 // We should fail silently if we try to clear/test outside the range.
121 CPU_CLR(CPU_SETSIZE, &set);
122 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
123}
124
125TEST(sched, cpu_equal) {
126 cpu_set_t set1;
127 cpu_set_t set2;
128
129 CPU_ZERO(&set1);
130 CPU_ZERO(&set2);
131 CPU_SET(1, &set1);
132 ASSERT_FALSE(CPU_EQUAL(&set1, &set2));
133 CPU_SET(1, &set2);
134 ASSERT_TRUE(CPU_EQUAL(&set1, &set2));
135}
136
137TEST(sched, cpu_op) {
138 cpu_set_t set1;
139 cpu_set_t set2;
140 cpu_set_t set3;
141
142 CPU_ZERO(&set1);
143 CPU_ZERO(&set2);
144 CPU_ZERO(&set3);
145 CPU_SET(0, &set1);
146 CPU_SET(0, &set2);
147 CPU_SET(1, &set2);
148
149 CPU_AND(&set3, &set1, &set2);
150 for (int i = 0; i < CPU_SETSIZE; i++) {
151 ASSERT_EQ(i == 0, CPU_ISSET(i, &set3));
152 }
153
154 CPU_XOR(&set3, &set1, &set2);
155 for (int i = 0; i < CPU_SETSIZE; i++) {
156 ASSERT_EQ(i == 1, CPU_ISSET(i, &set3));
157 }
158
159 CPU_OR(&set3, &set1, &set2);
160 for (int i = 0; i < CPU_SETSIZE; i++) {
161 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set3));
162 }
163}
164
165
166TEST(sched, cpu_alloc_small) {
167 cpu_set_t* set = CPU_ALLOC(17);
168 size_t size = CPU_ALLOC_SIZE(17);
169
170 CPU_ZERO_S(size, set);
171 ASSERT_EQ(0, CPU_COUNT_S(size, set));
172 CPU_SET_S(16, size, set);
173 ASSERT_TRUE(CPU_ISSET_S(16, size, set));
174
175 CPU_FREE(set);
176}
177
178TEST(sched, cpu_alloc_big) {
179 cpu_set_t* set = CPU_ALLOC(10 * CPU_SETSIZE);
180 size_t size = CPU_ALLOC_SIZE(10 * CPU_SETSIZE);
181
182 CPU_ZERO_S(size, set);
183 ASSERT_EQ(0, CPU_COUNT_S(size, set));
184 CPU_SET_S(CPU_SETSIZE, size, set);
185 ASSERT_TRUE(CPU_ISSET_S(CPU_SETSIZE, size, set));
186
187 CPU_FREE(set);
188}
189
190TEST(sched, cpu_s_macros) {
191 int set_size = 64;
192 size_t size = CPU_ALLOC_SIZE(set_size);
193 cpu_set_t* set = CPU_ALLOC(set_size);
194
195 CPU_ZERO_S(size, set);
196 for (int i = 0; i < set_size; i++) {
197 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
198 CPU_SET_S(i, size, set);
199 ASSERT_TRUE(CPU_ISSET_S(i, size, set));
200 ASSERT_EQ(i + 1, CPU_COUNT_S(size, set));
201 }
202
203 for (int i = 0; i < set_size; i++) {
204 CPU_CLR_S(i, size, set);
205 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
206 ASSERT_EQ(set_size - i - 1, CPU_COUNT_S(size, set));
207 }
208
209 CPU_FREE(set);
210}
211
212TEST(sched, cpu_op_s_macros) {
213 int set_size1 = 64;
214 int set_size2 = set_size1 * 2;
215 int set_size3 = set_size1 * 3;
216 size_t size1 = CPU_ALLOC_SIZE(set_size1);
217 size_t size2 = CPU_ALLOC_SIZE(set_size2);
218 size_t size3 = CPU_ALLOC_SIZE(set_size3);
219
Elliott Hughes2d367502014-04-30 10:45:35 -0700220 cpu_set_t* set1 = CPU_ALLOC(set_size1);
221 cpu_set_t* set2 = CPU_ALLOC(set_size2);
222 cpu_set_t* set3 = CPU_ALLOC(set_size3);
Calin Juravleb7437902014-04-29 20:25:26 +0100223 CPU_ZERO_S(size1, set1);
224 CPU_ZERO_S(size2, set2);
225 CPU_ZERO_S(size3, set3);
226
227 CPU_SET_S(0, size1, set1);
228 CPU_SET_S(0, size2, set2);
229 CPU_SET_S(1, size3, set2);
230
231 CPU_AND_S(size1, set3, set1, set2);
232 for (int i = 0; i < set_size3; i++) {
233 ASSERT_EQ(i == 0, CPU_ISSET_S(i, size3, set3));
234 }
235
236 CPU_OR_S(size1, set3, set1, set2);
237 for (int i = 0; i < set_size3; i++) {
238 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET_S(i, size3, set3));
239 }
240
241 CPU_XOR_S(size1, set3, set1, set2);
242 for (int i = 0; i < set_size3; i++) {
243 ASSERT_EQ(i == 1, CPU_ISSET_S(i, size3, set3));
244 }
245
246 CPU_FREE(set1);
247 CPU_FREE(set2);
248 CPU_FREE(set3);
249}
250
251TEST(sched, cpu_equal_s) {
252 int set_size1 = 64;
253 int set_size2 = set_size1 * 2;
254 size_t size1 = CPU_ALLOC_SIZE(set_size1);
255 size_t size2 = CPU_ALLOC_SIZE(set_size2);
256
Elliott Hughes2d367502014-04-30 10:45:35 -0700257 cpu_set_t* set1 = CPU_ALLOC(set_size1);
258 cpu_set_t* set2 = CPU_ALLOC(set_size2);
Calin Juravleb7437902014-04-29 20:25:26 +0100259
260 CPU_ZERO_S(size1, set1);
261 CPU_ZERO_S(size2, set2);
262
263 CPU_SET_S(0, size1, set1);
264 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set1));
265 ASSERT_FALSE(CPU_EQUAL_S(size1, set1, set2));
266 CPU_SET_S(0, size2, set2);
267 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set2));
268
269 CPU_FREE(set1);
270 CPU_FREE(set2);
271}