xref: /xnu-12377.1.9/tests/sched/contextswitch.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <pthread.h>
5 #include <errno.h>
6 #include <err.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <sysexits.h>
10 #include <getopt.h>
11 #include <spawn.h>
12 #include <stdbool.h>
13 #include <sys/sysctl.h>
14 #include <mach/mach_time.h>
15 #include <mach/mach.h>
16 #include <mach/semaphore.h>
17 #include <TargetConditionals.h>
18 
19 #ifdef T_NAMESPACE
20 #undef T_NAMESPACE
21 #endif
22 
23 #include <darwintest.h>
24 #include <darwintest_utils.h>
25 #include <stdatomic.h>
26 
27 #include "sched_test_utils.h"
28 
29 T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
30     T_META_RADAR_COMPONENT_NAME("xnu"),
31     T_META_RADAR_COMPONENT_VERSION("scheduler"));
32 
33 #define MAX_THREADS     32
34 #define SPIN_SECS       6
35 #define THR_SPINNER_PRI 63
36 #define THR_MANAGER_PRI 62
37 #define WARMUP_ITERATIONS 100
38 #define POWERCTRL_SUCCESS_STR "Factor1: 1.000000"
39 
40 static semaphore_t semaphore;
41 static semaphore_t worker_sem;
42 static uint32_t g_numcpus;
43 static _Atomic uint32_t keep_going = 1;
44 static dt_stat_time_t s;
45 
46 static struct {
47 	pthread_t thread;
48 	bool measure_thread;
49 } threads[MAX_THREADS];
50 
51 extern char **environ;
52 
53 static void
csw_perf_test_init(void)54 csw_perf_test_init(void)
55 {
56 	int spawn_ret, pid;
57 	char *const clpcctrl_args[] = {"/usr/local/bin/clpcctrl", "-f", "5000", NULL};
58 	spawn_ret = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, environ);
59 	waitpid(pid, &spawn_ret, 0);
60 }
61 
62 static void
csw_perf_test_cleanup(void)63 csw_perf_test_cleanup(void)
64 {
65 	int spawn_ret, pid;
66 	char *const clpcctrl_args[] = {"/usr/local/bin/clpcctrl", "-d", NULL};
67 	spawn_ret = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, environ);
68 	waitpid(pid, &spawn_ret, 0);
69 }
70 
71 static pthread_t
make_thread(uint32_t thread_id,uint32_t priority,bool fixpri,void * (* start_routine)(void *))72 make_thread(uint32_t thread_id, uint32_t priority, bool fixpri,
73     void *(*start_routine)(void *))
74 {
75 	int rv;
76 	pthread_t new_thread;
77 	struct sched_param param = { .sched_priority = (int)priority };
78 	pthread_attr_t attr;
79 
80 	T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), "pthread_attr_init");
81 
82 	T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr, &param),
83 	    "pthread_attr_setschedparam");
84 
85 	if (fixpri) {
86 		T_ASSERT_POSIX_ZERO(pthread_attr_setschedpolicy(&attr, SCHED_RR),
87 		    "pthread_attr_setschedpolicy");
88 	}
89 
90 	T_ASSERT_POSIX_ZERO(pthread_create(&new_thread, &attr, start_routine,
91 	    (void*)(uintptr_t)thread_id), "pthread_create");
92 
93 	T_ASSERT_POSIX_ZERO(pthread_attr_destroy(&attr), "pthread_attr_destroy");
94 
95 	threads[thread_id].thread = new_thread;
96 
97 	return new_thread;
98 }
99 
100 /* Spin until a specified number of seconds elapses */
101 static void
spin_for_timeout(uint32_t seconds)102 spin_for_timeout(uint32_t seconds)
103 {
104 	uint64_t duration       = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC);
105 	uint64_t current_time   = mach_absolute_time();
106 	uint64_t timeout        = duration + current_time;
107 
108 	uint64_t spin_count = 0;
109 
110 	while (mach_absolute_time() < timeout && atomic_load_explicit(&keep_going,
111 	    memory_order_relaxed)) {
112 		spin_count++;
113 	}
114 }
115 
116 static void *
spin_thread(void * arg)117 spin_thread(void *arg)
118 {
119 	uint32_t thread_id = (uint32_t) arg;
120 	char name[30] = "";
121 
122 	snprintf(name, sizeof(name), "spin thread %2d", thread_id);
123 	pthread_setname_np(name);
124 	T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore, worker_sem),
125 	    "semaphore_wait_signal");
126 	spin_for_timeout(SPIN_SECS);
127 	return NULL;
128 }
129 
130 static void *
thread(void * arg)131 thread(void *arg)
132 {
133 	uint32_t thread_id = (uint32_t) arg;
134 	char name[30] = "";
135 
136 	snprintf(name, sizeof(name), "thread %2d", thread_id);
137 	pthread_setname_np(name);
138 	T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore, worker_sem), "semaphore_wait");
139 
140 	if (threads[thread_id].measure_thread) {
141 		for (int i = 0; i < WARMUP_ITERATIONS; i++) {
142 			thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0);
143 		}
144 		T_STAT_MEASURE_LOOP(s) {
145 			if (thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0)) {
146 				T_ASSERT_FAIL("thread_switch");
147 			}
148 		}
149 		atomic_store_explicit(&keep_going, 0, memory_order_relaxed);
150 	} else {
151 		while (atomic_load_explicit(&keep_going, memory_order_relaxed)) {
152 			if (thread_switch(THREAD_NULL, SWITCH_OPTION_NONE, 0)) {
153 				T_ASSERT_FAIL("thread_switch");
154 			}
155 		}
156 	}
157 	return NULL;
158 }
159 
160 void
check_device_temperature(void)161 check_device_temperature(void)
162 {
163 	char buffer[256];
164 	FILE *pipe = popen("powerctrl Factor1", "r");
165 
166 	if (pipe == NULL) {
167 		T_FAIL("Failed to check device temperature");
168 		T_END;
169 	}
170 
171 	fgets(buffer, sizeof(buffer), pipe);
172 
173 	if (strncmp(POWERCTRL_SUCCESS_STR, buffer, strlen(POWERCTRL_SUCCESS_STR))) {
174 		T_PERF("temperature", 0.0, "factor", "device temperature");
175 	} else {
176 		T_PASS("Device temperature check pass");
177 		T_PERF("temperature", 1.0, "factor", "device temperature");
178 	}
179 	pclose(pipe);
180 }
181 
182 void
record_perfcontrol_stats(const char * sysctlname,const char * units,const char * info)183 record_perfcontrol_stats(const char *sysctlname, const char *units, const char *info)
184 {
185 	int data = 0;
186 	size_t data_size = sizeof(data);
187 	T_ASSERT_POSIX_ZERO(sysctlbyname(sysctlname,
188 	    &data, &data_size, NULL, 0),
189 	    "%s", sysctlname);
190 	T_PERF(info, data, units, info);
191 }
192 
193 /* Disable the test on MacOS for now */
194 T_DECL(perf_csw, "context switch performance", T_META_TAG_PERF, T_META_CHECK_LEAKS(false), T_META_ASROOT(true), T_META_TAG_VM_NOT_ELIGIBLE)
195 {
196 #if !defined(__arm64__)
197 	T_SKIP("Not supported on Intel platforms");
198 	return;
199 #endif /* !defined(__arm64__) */
200 	check_device_temperature();
201 
202 	T_ATEND(csw_perf_test_cleanup);
203 
204 	csw_perf_test_init();
205 	pthread_setname_np("main thread");
206 
207 	struct sched_param param = {.sched_priority = 48};
208 
209 	T_ASSERT_POSIX_ZERO(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param),
210 	    "pthread_setschedparam");
211 
212 	T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore,
213 	    SYNC_POLICY_FIFO, 0), "semaphore_create");
214 
215 	T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &worker_sem,
216 	    SYNC_POLICY_FIFO, 0), "semaphore_create");
217 
218 	g_numcpus = (uint32_t)dt_ncpu();
219 	printf("hw.ncpu: %d\n", g_numcpus);
220 	uint32_t n_spinners = g_numcpus - 1;
221 
222 	int mt_supported = 0;
223 	size_t mt_supported_size = sizeof(mt_supported);
224 	T_ASSERT_POSIX_ZERO(sysctlbyname("kern.monotonic.supported", &mt_supported,
225 	    &mt_supported_size, NULL, 0), "sysctlbyname kern.monotonic.supported");
226 
227 	for (uint32_t thread_id = 0; thread_id < n_spinners; thread_id++) {
228 		threads[thread_id].thread = make_thread(thread_id, THR_SPINNER_PRI,
229 		    true, &spin_thread);
230 	}
231 
232 	s = dt_stat_time_create("context switch time");
233 
234 	make_thread(n_spinners, THR_MANAGER_PRI, true, &thread);
235 	threads[n_spinners].measure_thread = true;
236 	make_thread(n_spinners + 1, THR_MANAGER_PRI, true, &thread);
237 
238 	/* Allow the context switch threads to get into sem_wait() */
239 	for (uint32_t thread_id = 0; thread_id < n_spinners + 2; thread_id++) {
240 		T_ASSERT_MACH_SUCCESS(semaphore_wait(worker_sem), "semaphore_wait");
241 	}
242 
243 	int enable_callout_stats = 1;
244 	size_t enable_size = sizeof(enable_callout_stats);
245 
246 	if (mt_supported) {
247 		/* Enable callout stat collection */
248 		T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
249 		    NULL, 0, &enable_callout_stats, enable_size),
250 		    "sysctlbyname kern.perfcontrol_callout.stats_enabled");
251 	}
252 
253 	T_ASSERT_MACH_SUCCESS(semaphore_signal_all(semaphore), "semaphore_signal");
254 
255 
256 	for (uint32_t thread_id = 0; thread_id < n_spinners + 2; thread_id++) {
257 		T_ASSERT_POSIX_ZERO(pthread_join(threads[thread_id].thread, NULL),
258 		    "pthread_join %d", thread_id);
259 	}
260 
261 	if (mt_supported) {
262 		record_perfcontrol_stats("kern.perfcontrol_callout.oncore_instr",
263 		    "instructions", "oncore.instructions");
264 		record_perfcontrol_stats("kern.perfcontrol_callout.offcore_instr",
265 		    "instructions", "offcore.instructions");
266 		record_perfcontrol_stats("kern.perfcontrol_callout.oncore_cycles",
267 		    "cycles", "oncore.cycles");
268 		record_perfcontrol_stats("kern.perfcontrol_callout.offcore_cycles",
269 		    "cycles", "offcore.cycles");
270 
271 		/* Disable callout stat collection */
272 		enable_callout_stats = 0;
273 		T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
274 		    NULL, 0, &enable_callout_stats, enable_size),
275 		    "sysctlbyname kern.perfcontrol_callout.stats_enabled");
276 	}
277 
278 	check_device_temperature();
279 	dt_stat_finalize(s);
280 }
281