| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | sched.c |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file is the scheduler test. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "lxtcommon.h" |
| 16 | #include "unittests.h" |
| 17 | #include <sched.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sys/syscall.h> |
| 20 | |
| 21 | #define LXT_NAME "sched" |
| 22 | |
| 23 | int GetDefaultScheduler(PLXT_ARGS Args); |
| 24 | |
| 25 | int SetScheduler(PLXT_ARGS Args); |
| 26 | |
| 27 | int SetSchedulerChild(PLXT_ARGS Args); |
| 28 | |
| 29 | int SetGetAffinity(PLXT_ARGS Args); |
| 30 | |
| 31 | int SetGetAffinityNp(PLXT_ARGS Args); |
| 32 | |
| 33 | // |
| 34 | // Global constants |
| 35 | // |
| 36 | |
| 37 | static const LXT_VARIATION g_LxtVariations[] = { |
| 38 | {"Get Scheduler Default", GetDefaultScheduler}, |
| 39 | {"Set Scheduler", SetScheduler}, |
| 40 | {"Set-Get Affinity", SetGetAffinity}, |
| 41 | {"Set-Get Affinity np", SetGetAffinityNp}}; |
| 42 | |
| 43 | int SchedTestEntry(int Argc, char* Argv[]) |
| 44 | |
| 45 | /*++ |
| 46 | --*/ |
| 47 | |
| 48 | { |
| 49 | |
| 50 | LXT_ARGS Args; |
| 51 | int Result; |
| 52 | |
| 53 | LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME)); |
| 54 | LxtCheckResult(LxtRunVariations(&Args, g_LxtVariations, LXT_COUNT_OF(g_LxtVariations))); |
| 55 | |
| 56 | ErrorExit: |
| 57 | LxtUninitialize(); |
| 58 | return !LXT_SUCCESS(Result); |
| 59 | } |
| 60 | |
| 61 | int GetDefaultScheduler(PLXT_ARGS Args) |
| 62 | |
| 63 | /*++ |
| 64 | --*/ |
| 65 | |
| 66 | { |
| 67 | |
| 68 | int Result; |
| 69 | int Policy; |
| 70 | |
| 71 | Policy = sched_getscheduler(0); |
| 72 | LxtLogInfo("Policy received %d", Policy); |
| 73 | if (Policy == SCHED_OTHER) |
| 74 | { |
| 75 | Result = LXT_RESULT_SUCCESS; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | LxtLogError("Bad policy. Expected(%d) != Returned(%d)", SCHED_OTHER, Policy); |
| 80 | |
| 81 | Result = LXT_RESULT_FAILURE; |
| 82 | } |
| 83 | |
| 84 | ErrorExit: |
| 85 | return Result; |
| 86 | } |
| 87 | |
| 88 | int SetScheduler(PLXT_ARGS Args) |
| 89 | |
| 90 | /*++ |
| 91 | --*/ |
| 92 | |
| 93 | { |
| 94 | |
| 95 | int Result; |
| 96 | struct sched_param Param; |
| 97 | int Policy; |
| 98 | |
| 99 | Policy = sched_getscheduler(0); |
| 100 | LxtLogInfo("Policy received %d", Policy); |
| 101 | if (Policy < 0) |
| 102 | { |
| 103 | LxtLogError("Bad policy. errno %d = %s", errno, strerror(errno)); |
| 104 | |
| 105 | Result = LXT_RESULT_FAILURE; |
| 106 | goto ErrorExit; |
| 107 | } |
| 108 | |
| 109 | // |
| 110 | // Set a different policy |
| 111 | // |
| 112 | |
| 113 | if (Policy == SCHED_OTHER) |
| 114 | { |
| 115 | Policy = SCHED_FIFO; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | Policy = SCHED_OTHER; |
| 120 | } |
| 121 | |
| 122 | LxtLogInfo("Setting policy %d", Policy); |
| 123 | Param.sched_priority = 17; |
| 124 | Result = sched_setscheduler(0, Policy, &Param); |
| 125 | if (Result < 0) |
| 126 | { |
| 127 | LxtLogError("Set scheduler failed errno %d = %s", errno, strerror(errno)); |
| 128 | |
| 129 | Result = LXT_RESULT_FAILURE; |
| 130 | goto ErrorExit; |
| 131 | } |
| 132 | |
| 133 | Result = sched_getscheduler(0); |
| 134 | LxtLogInfo("Policy received %d", Result); |
| 135 | if (Policy != Result) |
| 136 | { |
| 137 | LxtLogError("Bad policy. Expected(%d) != Returned(%d)", Policy, Result); |
| 138 | |
| 139 | Result = LXT_RESULT_FAILURE; |
| 140 | goto ErrorExit; |
| 141 | } |
| 142 | |
| 143 | Result = LXT_RESULT_SUCCESS; |
| 144 | |
| 145 | ErrorExit: |
| 146 | return Result; |
| 147 | } |
| 148 | |
| 149 | int SetSchedulerChild(PLXT_ARGS Args) |
| 150 | |
| 151 | /*++ |
| 152 | --*/ |
| 153 | |
| 154 | { |
| 155 | |
| 156 | int Result; |
| 157 | int Policy; |
| 158 | int Pid; |
| 159 | |
| 160 | // |
| 161 | // The child should inherit this scheduler |
| 162 | // |
| 163 | |
| 164 | sched_setscheduler(0, SCHED_OTHER, NULL); |
| 165 | |
| 166 | Pid = fork(); |
| 167 | if (Pid != 0) |
| 168 | { |
| 169 | sleep(1); |
| 170 | |
| 171 | Result = sched_setscheduler(Pid, SCHED_FIFO, NULL); |
| 172 | if (Result < 0) |
| 173 | { |
| 174 | LxtLogError("Set scheduler failed errno %d = %s", errno, strerror(errno)); |
| 175 | |
| 176 | Result = LXT_RESULT_FAILURE; |
| 177 | goto ErrorExit; |
| 178 | } |
| 179 | |
| 180 | sleep(2); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | Policy = sched_getscheduler(0); |
| 185 | LxtLogInfo("Child - Policy gotten %d", Policy); |
| 186 | if (Policy != SCHED_OTHER) |
| 187 | { |
| 188 | LxtLogError("Bad policy. Expected(%d) != Returned(%d)", SCHED_OTHER, Policy); |
| 189 | |
| 190 | Result = LXT_RESULT_FAILURE; |
| 191 | goto ErrorExit; |
| 192 | } |
| 193 | sleep(2); |
| 194 | |
| 195 | Policy = sched_getscheduler(0); |
| 196 | printf("Child - Policy gotten %d", Policy); |
| 197 | if (Policy != SCHED_FIFO) |
| 198 | { |
| 199 | LxtLogError("Bad policy. Expected(%d) != Returned(%d)", SCHED_FIFO, Policy); |
| 200 | |
| 201 | Result = LXT_RESULT_FAILURE; |
| 202 | goto ErrorExit; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | Result = LXT_RESULT_SUCCESS; |
| 207 | |
| 208 | ErrorExit: |
| 209 | return Result; |
| 210 | } |
| 211 | |
| 212 | int SetGetAffinity(PLXT_ARGS Args) |
| 213 | |
| 214 | /*++ |
| 215 | --*/ |
| 216 | |
| 217 | { |
| 218 | |
| 219 | int Result; |
| 220 | int Size = -1; |
| 221 | cpu_set_t Set; |
| 222 | cpu_set_t Desired; |
| 223 | int Sizes[] = {-8, 8, 16, 24, 32, 40, 64, 128, 256}; |
| 224 | int SizeExpected; |
| 225 | int Index; |
| 226 | |
| 227 | CPU_ZERO(&Set); |
| 228 | |
| 229 | LxtLogInfo("sizeof(cpu_set_t) = %Iu", sizeof(cpu_set_t)); |
| 230 | LxtCheckErrno(Size = LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 231 | LxtCheckEqual(Size, 64, "%d"); |
| 232 | LxtLogInfo("Affinity before: %08x", *(uint32_t*)&Set); |
| 233 | CPU_ZERO(&Desired); |
| 234 | CPU_SET(0, &Desired); |
| 235 | LxtCheckErrno(LxtSched_SetAffinity(0, 1, &Desired)); |
| 236 | LxtCheckErrno(Size = LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 237 | LxtCheckEqual(Size, 64, "%d"); |
| 238 | LxtCheckErrno(LxtSched_SetAffinity(0, 3, &Desired)); |
| 239 | LxtCheckErrno(Size = LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 240 | LxtCheckEqual(Size, 64, "%d"); |
| 241 | LxtCheckErrno(LxtSched_SetAffinity(0, sizeof(Desired), &Desired)); |
| 242 | LxtCheckErrno(Size = LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 243 | LxtCheckEqual(Size, 64, "%d"); |
| 244 | LxtLogInfo("Affinity after: %08x", *(uint32_t*)&Set); |
| 245 | if (!CPU_EQUAL(&Set, &Desired)) |
| 246 | { |
| 247 | LxtLogError("sched_setaffinity failed to set the affinity. "); |
| 248 | Result = LXT_RESULT_FAILURE; |
| 249 | goto ErrorExit; |
| 250 | } |
| 251 | |
| 252 | // |
| 253 | // Test with various buffer sizes. |
| 254 | // |
| 255 | |
| 256 | for (Index = 0; Index < (int)LXT_COUNT_OF(Sizes); Index++) |
| 257 | { |
| 258 | LxtLogInfo("Testing size %d", Sizes[Index]); |
| 259 | LxtCheckErrno(Size = LxtSched_GetAffinity(0, Sizes[Index], &Set)); |
| 260 | SizeExpected = Sizes[Index]; |
| 261 | if ((SizeExpected > 64) || (SizeExpected < 0)) |
| 262 | { |
| 263 | SizeExpected = 64; |
| 264 | } |
| 265 | |
| 266 | LxtCheckEqual(Size, SizeExpected, "%d"); |
| 267 | if (!CPU_EQUAL(&Set, &Desired)) |
| 268 | { |
| 269 | LxtLogError("sched_setaffinity failed to set the affinity. "); |
| 270 | Result = LXT_RESULT_FAILURE; |
| 271 | goto ErrorExit; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | LxtCheckErrno(Size = LxtSched_GetAffinity(getpid(), sizeof(Set), &Set)); |
| 276 | |
| 277 | // |
| 278 | // Invalid parameter variations. |
| 279 | // |
| 280 | |
| 281 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 0, &Set), EINVAL); |
| 282 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 1, &Set), EINVAL); |
| 283 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 2, &Set), EINVAL); |
| 284 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 7, &Set), EINVAL); |
| 285 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 9, &Set), EINVAL); |
| 286 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 10, &Set), EINVAL); |
| 287 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 31, &Set), EINVAL); |
| 288 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 33, &Set), EINVAL); |
| 289 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 63, &Set), EINVAL); |
| 290 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, 65, &Set), EINVAL); |
| 291 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, -1, &Set), EINVAL); |
| 292 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, -63, &Set), EINVAL); |
| 293 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, -1, NULL), EINVAL); |
| 294 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, -1, -1), EINVAL); |
| 295 | LxtCheckErrnoFailure(LxtSched_GetAffinity(-1, -1, &Set), EINVAL); |
| 296 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, sizeof(Set), NULL), EFAULT); |
| 297 | LxtCheckErrnoFailure(LxtSched_GetAffinity(0, sizeof(Set), -1), EFAULT); |
| 298 | LxtCheckErrnoFailure(LxtSched_GetAffinity(-1, sizeof(Set), &Set), ESRCH); |
| 299 | LxtCheckErrnoFailure(LxtSched_GetAffinity(-1, sizeof(Set), NULL), ESRCH); |
| 300 | LxtCheckErrnoFailure(LxtSched_GetAffinity(-1, sizeof(Set), -1), ESRCH); |
| 301 | Result = LXT_RESULT_SUCCESS; |
| 302 | |
| 303 | ErrorExit: |
| 304 | return Result; |
| 305 | } |
| 306 | |
| 307 | int SetGetAffinityNp(PLXT_ARGS Args) |
| 308 | |
| 309 | /*++ |
| 310 | --*/ |
| 311 | |
| 312 | { |
| 313 | |
| 314 | int Result; |
| 315 | cpu_set_t Set; |
| 316 | CPU_ZERO(&Set); |
| 317 | CPU_SET(0, &Set); |
| 318 | LxtCheckErrno(LxtSched_SetAffinity(0, sizeof(Set), &Set)); |
| 319 | LxtCheckErrno(LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 320 | |
| 321 | // |
| 322 | // N.B Affinity cannot be validated because its not guaranteed for it to |
| 323 | // take effect. |
| 324 | // |
| 325 | |
| 326 | LxtLogInfo("Current Affinity: %08x", *(uint32_t*)&Set); |
| 327 | CPU_ZERO(&Set); |
| 328 | CPU_SET(1, &Set); |
| 329 | LxtCheckErrno(LxtSched_SetAffinity(0, sizeof(Set), &Set)); |
| 330 | LxtCheckErrno(LxtSched_GetAffinity(0, sizeof(Set), &Set)); |
| 331 | LxtLogInfo("Current Affinity: %08x", *(uint32_t*)&Set); |
| 332 | Result = LXT_RESULT_SUCCESS; |
| 333 | |
| 334 | ErrorExit: |
| 335 | return Result; |
| 336 | } |