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