Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /* * Test to validate that we can schedule threads on all hw.ncpus cores according to _os_cpu_number * * <rdar://problem/29545645> * <rdar://problem/30445216> * * xcrun -sdk macosx.internal clang -o cpucount cpucount.c -ldarwintest -g -Weverything * xcrun -sdk iphoneos.internal clang -arch arm64 -o cpucount-ios cpucount.c -ldarwintest -g -Weverything * xcrun -sdk macosx.internal clang -o cpucount cpucount.c -ldarwintest -arch arm64e -Weverything */ #include <darwintest.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <sys/sysctl.h> #include <sys/proc_info.h> #include <libproc.h> #include <mach/mach.h> #include <mach/mach_time.h> #include <os/tsd.h> /* private header for _os_cpu_number */ T_GLOBAL_META( T_META_RUN_CONCURRENTLY(false), T_META_BOOTARGS_SET("enable_skstb=1"), T_META_CHECK_LEAKS(false), T_META_ASROOT(true), T_META_ALL_VALID_ARCHS(true), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("scheduler") ); #define KERNEL_BOOTARGS_MAX_SIZE 1024 static char kernel_bootargs[KERNEL_BOOTARGS_MAX_SIZE]; #define KERNEL_VERSION_MAX_SIZE 1024 static char kernel_version[KERNEL_VERSION_MAX_SIZE]; static mach_timebase_info_data_t timebase_info; static uint64_t abs_to_nanos(uint64_t abs) { return abs * timebase_info.numer / timebase_info.denom; } static int32_t get_csw_count() { struct proc_taskinfo taskinfo; int rv; rv = proc_pidinfo(getpid(), PROC_PIDTASKINFO, 0, &taskinfo, sizeof(taskinfo)); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "PROC_PIDTASKINFO"); return taskinfo.pti_csw; } // noinline hopefully keeps the optimizer from hoisting it out of the loop // until rdar://68253516 is fixed. __attribute__((noinline)) static uint32_t fixed_os_cpu_number(void) { uint32_t cpu_number = _os_cpu_number(); return cpu_number; } T_DECL(count_cpus, "Tests we can schedule bound threads on all hw.ncpus cores and that _os_cpu_number matches") { int rv; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); /* Validate what kind of kernel we're on */ size_t kernel_version_size = sizeof(kernel_version); rv = sysctlbyname("kern.version", kernel_version, &kernel_version_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.version"); T_LOG("kern.version: %s\n", kernel_version); /* Double check that darwintest set the boot arg we requested */ size_t kernel_bootargs_size = sizeof(kernel_bootargs); rv = sysctlbyname("kern.bootargs", kernel_bootargs, &kernel_bootargs_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.bootargs"); T_LOG("kern.bootargs: %s\n", kernel_bootargs); if (NULL == strstr(kernel_bootargs, "enable_skstb=1")) { T_FAIL("enable_skstb=1 boot-arg is missing"); } kern_return_t kr; kr = mach_timebase_info(&timebase_info); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_timebase_info"); int bound_cpu_out = 0; size_t bound_cpu_out_size = sizeof(bound_cpu_out); rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0); if (rv == -1) { if (errno == ENOENT) { T_FAIL("kern.sched_thread_bind_cpu doesn't exist, must set enable_skstb=1 boot-arg on development kernel"); } if (errno == EPERM) { T_FAIL("must run as root"); } } T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu"); T_QUIET; T_ASSERT_EQ(bound_cpu_out, -1, "kern.sched_thread_bind_cpu should exist, start unbound"); struct sched_param param = {.sched_priority = 63}; rv = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_setschedparam"); uint32_t sysctl_ncpu = 0; size_t ncpu_size = sizeof(sysctl_ncpu); rv = sysctlbyname("hw.ncpu", &sysctl_ncpu, &ncpu_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "sysctlbyname(hw.ncpu)"); T_LOG("hw.ncpu: %2d\n", sysctl_ncpu); T_ASSERT_GT(sysctl_ncpu, 0, "at least one CPU exists"); for (uint32_t cpu_to_bind = 0; cpu_to_bind < sysctl_ncpu; cpu_to_bind++) { int32_t before_csw_count = get_csw_count(); T_LOG("(csw %4d) attempting to bind to cpu %2d\n", before_csw_count, cpu_to_bind); uint64_t start = mach_absolute_time(); rv = sysctlbyname("kern.sched_thread_bind_cpu", NULL, 0, &cpu_to_bind, sizeof(cpu_to_bind)); uint64_t end = mach_absolute_time(); if (rv == -1 && errno == ENOTSUP) { T_SKIP("Binding is available, but this process doesn't support binding (e.g. Rosetta on Aruba)"); } T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cpu(%u)", cpu_to_bind); uint32_t os_cpu_number_reported = fixed_os_cpu_number(); bound_cpu_out = 0; rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu"); T_QUIET; T_EXPECT_EQ((int)cpu_to_bind, bound_cpu_out, "should report bound cpu id matching requested bind target"); uint64_t delta_abs = end - start; uint64_t delta_ns = abs_to_nanos(delta_abs); int32_t after_csw_count = get_csw_count(); T_LOG("(csw %4d) bound to cpu %2d in %f milliseconds\n", after_csw_count, cpu_to_bind, ((double)delta_ns / 1000000.0)); if (cpu_to_bind > 0) { T_QUIET; T_EXPECT_LT(before_csw_count, after_csw_count, "should have had to context switch to execute the bind"); } T_LOG("cpu %2d reported id %2d\n", cpu_to_bind, os_cpu_number_reported); T_QUIET; T_EXPECT_EQ(cpu_to_bind, os_cpu_number_reported, "should report same CPU number as was bound to"); } int unbind = -1; /* pass -1 in order to unbind the thread */ rv = sysctlbyname("kern.sched_thread_bind_cpu", NULL, 0, &unbind, sizeof(unbind)); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cpu(%u)", unbind); rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu"); T_QUIET; T_ASSERT_EQ(bound_cpu_out, -1, "thread should be unbound at the end"); T_PASS("test has run threads on all CPUS"); } |