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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <errno.h> #include <err.h> #include <string.h> #include <assert.h> #include <sysexits.h> #include <getopt.h> #include <spawn.h> #include <stdbool.h> #include <sys/sysctl.h> #include <mach/mach_time.h> #include <mach/mach.h> #include <mach/semaphore.h> #include <TargetConditionals.h> #ifdef T_NAMESPACE #undef T_NAMESPACE #endif #include <darwintest.h> #include <stdatomic.h> T_GLOBAL_META(T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("scheduler")); #define MAX_THREADS 32 #define SPIN_SECS 6 #define THR_SPINNER_PRI 63 #define THR_MANAGER_PRI 62 #define WARMUP_ITERATIONS 100 #define POWERCTRL_SUCCESS_STR "Factor1: 1.000000" static mach_timebase_info_data_t timebase_info; static semaphore_t semaphore; static semaphore_t worker_sem; static uint32_t g_numcpus; static _Atomic uint32_t keep_going = 1; static dt_stat_time_t s; static struct { pthread_t thread; bool measure_thread; } threads[MAX_THREADS]; static uint64_t nanos_to_abs(uint64_t nanos) { return nanos * timebase_info.denom / timebase_info.numer; } extern char **environ; static void csw_perf_test_init(void) { int spawn_ret, pid; char *const clpcctrl_args[] = {"/usr/local/bin/clpcctrl", "-f", "5000", NULL}; spawn_ret = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, environ); waitpid(pid, &spawn_ret, 0); } static void csw_perf_test_cleanup(void) { int spawn_ret, pid; char *const clpcctrl_args[] = {"/usr/local/bin/clpcctrl", "-d", NULL}; spawn_ret = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, environ); waitpid(pid, &spawn_ret, 0); } static pthread_t create_thread(uint32_t thread_id, uint32_t priority, bool fixpri, void *(*start_routine)(void *)) { int rv; pthread_t new_thread; struct sched_param param = { .sched_priority = (int)priority }; pthread_attr_t attr; T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), "pthread_attr_init"); T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr, ¶m), "pthread_attr_setschedparam"); if (fixpri) { T_ASSERT_POSIX_ZERO(pthread_attr_setschedpolicy(&attr, SCHED_RR), "pthread_attr_setschedpolicy"); } T_ASSERT_POSIX_ZERO(pthread_create(&new_thread, &attr, start_routine, (void*)(uintptr_t)thread_id), "pthread_create"); T_ASSERT_POSIX_ZERO(pthread_attr_destroy(&attr), "pthread_attr_destroy"); threads[thread_id].thread = new_thread; return new_thread; } /* Spin until a specified number of seconds elapses */ static void spin_for_duration(uint32_t seconds) { uint64_t duration = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC); uint64_t current_time = mach_absolute_time(); uint64_t timeout = duration + current_time; uint64_t spin_count = 0; while (mach_absolute_time() < timeout && atomic_load_explicit(&keep_going, memory_order_relaxed)) { spin_count++; } } static void * spin_thread(void *arg) { uint32_t thread_id = (uint32_t) arg; char name[30] = ""; snprintf(name, sizeof(name), "spin thread %2d", thread_id); pthread_setname_np(name); T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore, worker_sem), "semaphore_wait_signal"); spin_for_duration(SPIN_SECS); return NULL; } static void * thread(void *arg) { uint32_t thread_id = (uint32_t) arg; char name[30] = ""; snprintf(name, sizeof(name), "thread %2d", thread_id); pthread_setname_np(name); T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore, worker_sem), "semaphore_wait"); if (threads[thread_id].measure_thread) { for (int i = 0; i < WARMUP_ITERATIONS; i++) { thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0); } T_STAT_MEASURE_LOOP(s) { if (thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0)) { T_ASSERT_FAIL("thread_switch"); } } atomic_store_explicit(&keep_going, 0, memory_order_relaxed); } else { while (atomic_load_explicit(&keep_going, memory_order_relaxed)) { if (thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0)) { T_ASSERT_FAIL("thread_switch"); } } } return NULL; } void check_device_temperature(void) { char buffer[256]; FILE *pipe = popen("powerctrl Factor1", "r"); if (pipe == NULL) { T_FAIL("Failed to check device temperature"); T_END; } fgets(buffer, sizeof(buffer), pipe); if (strncmp(POWERCTRL_SUCCESS_STR, buffer, strlen(POWERCTRL_SUCCESS_STR))) { T_PERF("temperature", 0.0, "factor", "device temperature"); } else { T_PASS("Device temperature check pass"); T_PERF("temperature", 1.0, "factor", "device temperature"); } pclose(pipe); } void record_perfcontrol_stats(const char *sysctlname, const char *units, const char *info) { int data = 0; size_t data_size = sizeof(data); T_ASSERT_POSIX_ZERO(sysctlbyname(sysctlname, &data, &data_size, NULL, 0), "%s", sysctlname); T_PERF(info, data, units, info); } T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler")); /* Disable the test on MacOS for now */ T_DECL(perf_csw, "context switch performance", T_META_TAG_PERF, T_META_CHECK_LEAKS(false), T_META_ASROOT(true)) { #if !defined (__arm__) && !defined(__arm64__) T_SKIP("Not supported on Intel platforms"); return; #endif /* !defined (__arm__) && !defined(__arm64__) */ check_device_temperature(); T_ATEND(csw_perf_test_cleanup); csw_perf_test_init(); pthread_setname_np("main thread"); T_ASSERT_MACH_SUCCESS(mach_timebase_info(&timebase_info), "mach_timebase_info"); struct sched_param param = {.sched_priority = 48}; T_ASSERT_POSIX_ZERO(pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m), "pthread_setschedparam"); T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore, SYNC_POLICY_FIFO, 0), "semaphore_create"); T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &worker_sem, SYNC_POLICY_FIFO, 0), "semaphore_create"); size_t ncpu_size = sizeof(g_numcpus); T_ASSERT_POSIX_ZERO(sysctlbyname("hw.ncpu", &g_numcpus, &ncpu_size, NULL, 0), "sysctlbyname hw.ncpu"); printf("hw.ncpu: %d\n", g_numcpus); uint32_t n_spinners = g_numcpus - 1; int mt_supported = 0; size_t mt_supported_size = sizeof(mt_supported); T_ASSERT_POSIX_ZERO(sysctlbyname("kern.monotonic.supported", &mt_supported, &mt_supported_size, NULL, 0), "sysctlbyname kern.monotonic.supported"); for (uint32_t thread_id = 0; thread_id < n_spinners; thread_id++) { threads[thread_id].thread = create_thread(thread_id, THR_SPINNER_PRI, true, &spin_thread); } s = dt_stat_time_create("context switch time"); create_thread(n_spinners, THR_MANAGER_PRI, true, &thread); threads[n_spinners].measure_thread = true; create_thread(n_spinners + 1, THR_MANAGER_PRI, true, &thread); /* Allow the context switch threads to get into sem_wait() */ for (uint32_t thread_id = 0; thread_id < n_spinners + 2; thread_id++) { T_ASSERT_MACH_SUCCESS(semaphore_wait(worker_sem), "semaphore_wait"); } int enable_callout_stats = 1; size_t enable_size = sizeof(enable_callout_stats); if (mt_supported) { /* Enable callout stat collection */ T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled", NULL, 0, &enable_callout_stats, enable_size), "sysctlbyname kern.perfcontrol_callout.stats_enabled"); } T_ASSERT_MACH_SUCCESS(semaphore_signal_all(semaphore), "semaphore_signal"); for (uint32_t thread_id = 0; thread_id < n_spinners + 2; thread_id++) { T_ASSERT_POSIX_ZERO(pthread_join(threads[thread_id].thread, NULL), "pthread_join %d", thread_id); } if (mt_supported) { record_perfcontrol_stats("kern.perfcontrol_callout.oncore_instr", "instructions", "oncore.instructions"); record_perfcontrol_stats("kern.perfcontrol_callout.offcore_instr", "instructions", "offcore.instructions"); record_perfcontrol_stats("kern.perfcontrol_callout.oncore_cycles", "cycles", "oncore.cycles"); record_perfcontrol_stats("kern.perfcontrol_callout.offcore_cycles", "cycles", "offcore.cycles"); /* Disable callout stat collection */ enable_callout_stats = 0; T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled", NULL, 0, &enable_callout_stats, enable_size), "sysctlbyname kern.perfcontrol_callout.stats_enabled"); } check_device_temperature(); dt_stat_finalize(s); } |