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