sched_test.cpp revision b743790ccabd9b0b93355ff693066478d10dae0d
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#ifdef __BIONIC__ 25static int child_fn(void* i_ptr) { 26 *reinterpret_cast<int*>(i_ptr) = 42; 27 return 123; 28} 29 30TEST(sched, clone) { 31 void* child_stack[1024]; 32 33 int i = 0; 34 pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i); 35 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} 44#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 53 54TEST(sched, cpu_set) { 55 cpu_set_t set; 56 57 CPU_ZERO(&set); 58 CPU_SET(0, &set); 59 CPU_SET(17, &set); 60 for (int i = 0; i < CPU_SETSIZE; i++) { 61 ASSERT_EQ(i == 0 || i == 17, CPU_ISSET(i, &set)); 62 } 63 64 // We should fail silently if we try to set/test outside the range. 65 CPU_SET(CPU_SETSIZE, &set); 66 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set)); 67} 68 69TEST(sched, cpu_count) { 70 cpu_set_t set; 71 72 CPU_ZERO(&set); 73 ASSERT_EQ(0, CPU_COUNT(&set)); 74 CPU_SET(2, &set); 75 CPU_SET(10, &set); 76 ASSERT_EQ(2, CPU_COUNT(&set)); 77 CPU_CLR(10, &set); 78 ASSERT_EQ(1, CPU_COUNT(&set)); 79} 80 81TEST(sched, cpu_zero) { 82 cpu_set_t set; 83 84 CPU_ZERO(&set); 85 ASSERT_EQ(0, CPU_COUNT(&set)); 86 for (int i = 0; i < CPU_SETSIZE; i++) { 87 ASSERT_FALSE(CPU_ISSET(i, &set)); 88 } 89} 90 91TEST(sched, cpu_clr) { 92 cpu_set_t set; 93 94 CPU_ZERO(&set); 95 CPU_SET(0, &set); 96 CPU_SET(1, &set); 97 for (int i = 0; i < CPU_SETSIZE; i++) { 98 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set)); 99 } 100 CPU_CLR(1, &set); 101 for (int i = 0; i < CPU_SETSIZE; i++) { 102 ASSERT_EQ(i == 0, CPU_ISSET(i, &set)); 103 } 104 105 // We should fail silently if we try to clear/test outside the range. 106 CPU_CLR(CPU_SETSIZE, &set); 107 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set)); 108} 109 110TEST(sched, cpu_equal) { 111 cpu_set_t set1; 112 cpu_set_t set2; 113 114 CPU_ZERO(&set1); 115 CPU_ZERO(&set2); 116 CPU_SET(1, &set1); 117 ASSERT_FALSE(CPU_EQUAL(&set1, &set2)); 118 CPU_SET(1, &set2); 119 ASSERT_TRUE(CPU_EQUAL(&set1, &set2)); 120} 121 122TEST(sched, cpu_op) { 123 cpu_set_t set1; 124 cpu_set_t set2; 125 cpu_set_t set3; 126 127 CPU_ZERO(&set1); 128 CPU_ZERO(&set2); 129 CPU_ZERO(&set3); 130 CPU_SET(0, &set1); 131 CPU_SET(0, &set2); 132 CPU_SET(1, &set2); 133 134 CPU_AND(&set3, &set1, &set2); 135 for (int i = 0; i < CPU_SETSIZE; i++) { 136 ASSERT_EQ(i == 0, CPU_ISSET(i, &set3)); 137 } 138 139 CPU_XOR(&set3, &set1, &set2); 140 for (int i = 0; i < CPU_SETSIZE; i++) { 141 ASSERT_EQ(i == 1, CPU_ISSET(i, &set3)); 142 } 143 144 CPU_OR(&set3, &set1, &set2); 145 for (int i = 0; i < CPU_SETSIZE; i++) { 146 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set3)); 147 } 148} 149 150 151TEST(sched, cpu_alloc_small) { 152 cpu_set_t* set = CPU_ALLOC(17); 153 size_t size = CPU_ALLOC_SIZE(17); 154 155 CPU_ZERO_S(size, set); 156 ASSERT_EQ(0, CPU_COUNT_S(size, set)); 157 CPU_SET_S(16, size, set); 158 ASSERT_TRUE(CPU_ISSET_S(16, size, set)); 159 160 CPU_FREE(set); 161} 162 163TEST(sched, cpu_alloc_big) { 164 cpu_set_t* set = CPU_ALLOC(10 * CPU_SETSIZE); 165 size_t size = CPU_ALLOC_SIZE(10 * CPU_SETSIZE); 166 167 CPU_ZERO_S(size, set); 168 ASSERT_EQ(0, CPU_COUNT_S(size, set)); 169 CPU_SET_S(CPU_SETSIZE, size, set); 170 ASSERT_TRUE(CPU_ISSET_S(CPU_SETSIZE, size, set)); 171 172 CPU_FREE(set); 173} 174 175TEST(sched, cpu_s_macros) { 176 int set_size = 64; 177 size_t size = CPU_ALLOC_SIZE(set_size); 178 cpu_set_t* set = CPU_ALLOC(set_size); 179 180 CPU_ZERO_S(size, set); 181 for (int i = 0; i < set_size; i++) { 182 ASSERT_FALSE(CPU_ISSET_S(i, size, set)); 183 CPU_SET_S(i, size, set); 184 ASSERT_TRUE(CPU_ISSET_S(i, size, set)); 185 ASSERT_EQ(i + 1, CPU_COUNT_S(size, set)); 186 } 187 188 for (int i = 0; i < set_size; i++) { 189 CPU_CLR_S(i, size, set); 190 ASSERT_FALSE(CPU_ISSET_S(i, size, set)); 191 ASSERT_EQ(set_size - i - 1, CPU_COUNT_S(size, set)); 192 } 193 194 CPU_FREE(set); 195} 196 197TEST(sched, cpu_op_s_macros) { 198 int set_size1 = 64; 199 int set_size2 = set_size1 * 2; 200 int set_size3 = set_size1 * 3; 201 size_t size1 = CPU_ALLOC_SIZE(set_size1); 202 size_t size2 = CPU_ALLOC_SIZE(set_size2); 203 size_t size3 = CPU_ALLOC_SIZE(set_size3); 204 205 cpu_set_t* set1 = CPU_ALLOC(size1); 206 cpu_set_t* set2 = CPU_ALLOC(size2); 207 cpu_set_t* set3 = CPU_ALLOC(size3); 208 CPU_ZERO_S(size1, set1); 209 CPU_ZERO_S(size2, set2); 210 CPU_ZERO_S(size3, set3); 211 212 CPU_SET_S(0, size1, set1); 213 CPU_SET_S(0, size2, set2); 214 CPU_SET_S(1, size3, set2); 215 216 CPU_AND_S(size1, set3, set1, set2); 217 for (int i = 0; i < set_size3; i++) { 218 ASSERT_EQ(i == 0, CPU_ISSET_S(i, size3, set3)); 219 } 220 221 CPU_OR_S(size1, set3, set1, set2); 222 for (int i = 0; i < set_size3; i++) { 223 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET_S(i, size3, set3)); 224 } 225 226 CPU_XOR_S(size1, set3, set1, set2); 227 for (int i = 0; i < set_size3; i++) { 228 ASSERT_EQ(i == 1, CPU_ISSET_S(i, size3, set3)); 229 } 230 231 CPU_FREE(set1); 232 CPU_FREE(set2); 233 CPU_FREE(set3); 234} 235 236TEST(sched, cpu_equal_s) { 237 int set_size1 = 64; 238 int set_size2 = set_size1 * 2; 239 size_t size1 = CPU_ALLOC_SIZE(set_size1); 240 size_t size2 = CPU_ALLOC_SIZE(set_size2); 241 242 cpu_set_t* set1 = CPU_ALLOC(size1); 243 cpu_set_t* set2 = CPU_ALLOC(size2); 244 245 CPU_ZERO_S(size1, set1); 246 CPU_ZERO_S(size2, set2); 247 248 CPU_SET_S(0, size1, set1); 249 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set1)); 250 ASSERT_FALSE(CPU_EQUAL_S(size1, set1, set2)); 251 CPU_SET_S(0, size2, set2); 252 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set2)); 253 254 CPU_FREE(set1); 255 CPU_FREE(set2); 256} 257