1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions * Tests to validate that:
3*699cd480SApple OSS Distributions * - we can schedule threads on all hw.ncpus cores according to _os_cpu_number
4*699cd480SApple OSS Distributions * - we can schedule threads on all hw.cpuclusters clusters according to _os_cpu_cluster_number
5*699cd480SApple OSS Distributions * - the cluster id returned by _os_cpu_cluster_number aligns with mappings from IORegistry
6*699cd480SApple OSS Distributions *
7*699cd480SApple OSS Distributions * <rdar://problem/29545645>
8*699cd480SApple OSS Distributions * <rdar://problem/30445216>
9*699cd480SApple OSS Distributions *
10*699cd480SApple OSS Distributions * xcrun -sdk macosx.internal clang -o cpucount cpucount.c -ldarwintest -framework IOKit -framework CoreFoundation -g -Weverything
11*699cd480SApple OSS Distributions * xcrun -sdk iphoneos.internal clang -arch arm64 -o cpucount-ios cpucount.c -ldarwintest -framework IOKit -framework CoreFoundation -g -Weverything
12*699cd480SApple OSS Distributions * xcrun -sdk macosx.internal clang -o cpucount cpucount.c -ldarwintest -framework IOKit -framework CoreFoundation -arch arm64e -Weverything
13*699cd480SApple OSS Distributions */
14*699cd480SApple OSS Distributions
15*699cd480SApple OSS Distributions #include <darwintest.h>
16*699cd480SApple OSS Distributions #include "test_utils.h"
17*699cd480SApple OSS Distributions
18*699cd480SApple OSS Distributions #include <stdio.h>
19*699cd480SApple OSS Distributions #include <stdlib.h>
20*699cd480SApple OSS Distributions #include <unistd.h>
21*699cd480SApple OSS Distributions #include <pthread.h>
22*699cd480SApple OSS Distributions #include <sys/commpage.h>
23*699cd480SApple OSS Distributions #include <sys/sysctl.h>
24*699cd480SApple OSS Distributions #include <sys/proc_info.h>
25*699cd480SApple OSS Distributions #include <libproc.h>
26*699cd480SApple OSS Distributions
27*699cd480SApple OSS Distributions #include <CoreFoundation/CoreFoundation.h>
28*699cd480SApple OSS Distributions #include <IOKit/IOKitLib.h>
29*699cd480SApple OSS Distributions
30*699cd480SApple OSS Distributions #include <mach/mach.h>
31*699cd480SApple OSS Distributions #include <mach/mach_time.h>
32*699cd480SApple OSS Distributions #include <machine/cpu_capabilities.h>
33*699cd480SApple OSS Distributions
34*699cd480SApple OSS Distributions #include <os/tsd.h> /* private header for _os_cpu_number, _os_cpu_cluster_number */
35*699cd480SApple OSS Distributions
36*699cd480SApple OSS Distributions T_GLOBAL_META(
37*699cd480SApple OSS Distributions T_META_RUN_CONCURRENTLY(false),
38*699cd480SApple OSS Distributions T_META_BOOTARGS_SET("enable_skstb=1"),
39*699cd480SApple OSS Distributions T_META_CHECK_LEAKS(false),
40*699cd480SApple OSS Distributions T_META_ASROOT(true),
41*699cd480SApple OSS Distributions T_META_ALL_VALID_ARCHS(true),
42*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
43*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
44*699cd480SApple OSS Distributions T_META_OWNER("jarrad")
45*699cd480SApple OSS Distributions );
46*699cd480SApple OSS Distributions
47*699cd480SApple OSS Distributions #define KERNEL_BOOTARGS_MAX_SIZE 1024
48*699cd480SApple OSS Distributions static char kernel_bootargs[KERNEL_BOOTARGS_MAX_SIZE];
49*699cd480SApple OSS Distributions
50*699cd480SApple OSS Distributions #define KERNEL_VERSION_MAX_SIZE 1024
51*699cd480SApple OSS Distributions static char kernel_version[KERNEL_VERSION_MAX_SIZE];
52*699cd480SApple OSS Distributions
53*699cd480SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
54*699cd480SApple OSS Distributions
55*699cd480SApple OSS Distributions // Source: libktrace:corefoundation_helpers.c
56*699cd480SApple OSS Distributions
57*699cd480SApple OSS Distributions static void
dict_number_internal(CFDictionaryRef dict,CFStringRef key,void * dst_out,CFNumberType nbr_type)58*699cd480SApple OSS Distributions dict_number_internal(CFDictionaryRef dict, CFStringRef key, void *dst_out, CFNumberType nbr_type)
59*699cd480SApple OSS Distributions {
60*699cd480SApple OSS Distributions bool success;
61*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(dict, "dict must not be null");
62*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(key, " key must not be null");
63*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(dst_out, "dst out must not be null");
64*699cd480SApple OSS Distributions
65*699cd480SApple OSS Distributions CFTypeRef val = CFDictionaryGetValue(dict, key);
66*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(val, "unable to get value for key %s", CFStringGetCStringPtr(key, kCFStringEncodingASCII));
67*699cd480SApple OSS Distributions
68*699cd480SApple OSS Distributions CFTypeID type = CFGetTypeID(val);
69*699cd480SApple OSS Distributions if (type == CFNumberGetTypeID()) {
70*699cd480SApple OSS Distributions CFNumberRef val_nbr = (CFNumberRef)val;
71*699cd480SApple OSS Distributions success = CFNumberGetValue(val_nbr, nbr_type, dst_out);
72*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(success, "dictionary number at key '%s' is not the right type", CFStringGetCStringPtr(key, kCFStringEncodingASCII));
73*699cd480SApple OSS Distributions } else if (type == CFDataGetTypeID()) {
74*699cd480SApple OSS Distributions CFDataRef val_data = (CFDataRef)val;
75*699cd480SApple OSS Distributions size_t raw_size = (size_t)CFDataGetLength(val_data);
76*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(raw_size, (size_t)4, "cannot convert CFData of size %zu to number", raw_size);
77*699cd480SApple OSS Distributions CFDataGetBytes(val_data, CFRangeMake(0, (CFIndex)raw_size), dst_out);
78*699cd480SApple OSS Distributions } else {
79*699cd480SApple OSS Distributions T_ASSERT_FAIL("dictionary value at key '%s' should be a number or data", CFStringGetCStringPtr(key, kCFStringEncodingASCII));
80*699cd480SApple OSS Distributions }
81*699cd480SApple OSS Distributions }
82*699cd480SApple OSS Distributions
83*699cd480SApple OSS Distributions static void
dict_uint32(CFDictionaryRef dict,CFStringRef key,uint32_t * dst_out)84*699cd480SApple OSS Distributions dict_uint32(CFDictionaryRef dict, CFStringRef key, uint32_t *dst_out)
85*699cd480SApple OSS Distributions {
86*699cd480SApple OSS Distributions dict_number_internal(dict, key, dst_out, kCFNumberSInt32Type);
87*699cd480SApple OSS Distributions }
88*699cd480SApple OSS Distributions
89*699cd480SApple OSS Distributions static uint64_t
abs_to_nanos(uint64_t abs)90*699cd480SApple OSS Distributions abs_to_nanos(uint64_t abs)
91*699cd480SApple OSS Distributions {
92*699cd480SApple OSS Distributions return abs * timebase_info.numer / timebase_info.denom;
93*699cd480SApple OSS Distributions }
94*699cd480SApple OSS Distributions
95*699cd480SApple OSS Distributions static int32_t
get_csw_count(void)96*699cd480SApple OSS Distributions get_csw_count(void)
97*699cd480SApple OSS Distributions {
98*699cd480SApple OSS Distributions struct proc_taskinfo taskinfo;
99*699cd480SApple OSS Distributions int rv;
100*699cd480SApple OSS Distributions
101*699cd480SApple OSS Distributions rv = proc_pidinfo(getpid(), PROC_PIDTASKINFO, 0, &taskinfo, sizeof(taskinfo));
102*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "PROC_PIDTASKINFO");
103*699cd480SApple OSS Distributions
104*699cd480SApple OSS Distributions return taskinfo.pti_csw;
105*699cd480SApple OSS Distributions }
106*699cd480SApple OSS Distributions
107*699cd480SApple OSS Distributions // noinline hopefully keeps the optimizer from hoisting it out of the loop
108*699cd480SApple OSS Distributions // until rdar://68253516 is fixed.
109*699cd480SApple OSS Distributions __attribute__((noinline))
110*699cd480SApple OSS Distributions static uint32_t
fixed_os_cpu_number(void)111*699cd480SApple OSS Distributions fixed_os_cpu_number(void)
112*699cd480SApple OSS Distributions {
113*699cd480SApple OSS Distributions uint32_t cpu_number = _os_cpu_number();
114*699cd480SApple OSS Distributions return cpu_number;
115*699cd480SApple OSS Distributions }
116*699cd480SApple OSS Distributions
117*699cd480SApple OSS Distributions static unsigned int
commpage_cpu_cluster_number(void)118*699cd480SApple OSS Distributions commpage_cpu_cluster_number(void)
119*699cd480SApple OSS Distributions {
120*699cd480SApple OSS Distributions uint8_t cpu_number = (uint8_t)fixed_os_cpu_number();
121*699cd480SApple OSS Distributions volatile uint8_t *cpu_to_cluster = COMM_PAGE_SLOT(uint8_t, CPU_TO_CLUSTER);
122*699cd480SApple OSS Distributions return (unsigned int)*(cpu_to_cluster + cpu_number);
123*699cd480SApple OSS Distributions }
124*699cd480SApple OSS Distributions
125*699cd480SApple OSS Distributions static void
cpucount_setup(void)126*699cd480SApple OSS Distributions cpucount_setup(void)
127*699cd480SApple OSS Distributions {
128*699cd480SApple OSS Distributions int rv;
129*699cd480SApple OSS Distributions kern_return_t kr;
130*699cd480SApple OSS Distributions
131*699cd480SApple OSS Distributions T_SETUPBEGIN;
132*699cd480SApple OSS Distributions
133*699cd480SApple OSS Distributions setvbuf(stdout, NULL, _IONBF, 0);
134*699cd480SApple OSS Distributions setvbuf(stderr, NULL, _IONBF, 0);
135*699cd480SApple OSS Distributions
136*699cd480SApple OSS Distributions /* Validate what kind of kernel we're on */
137*699cd480SApple OSS Distributions size_t kernel_version_size = sizeof(kernel_version);
138*699cd480SApple OSS Distributions rv = sysctlbyname("kern.version", kernel_version, &kernel_version_size, NULL, 0);
139*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.version");
140*699cd480SApple OSS Distributions
141*699cd480SApple OSS Distributions T_LOG("kern.version: %s\n", kernel_version);
142*699cd480SApple OSS Distributions
143*699cd480SApple OSS Distributions /* Double check that darwintest set the boot arg we requested */
144*699cd480SApple OSS Distributions size_t kernel_bootargs_size = sizeof(kernel_bootargs);
145*699cd480SApple OSS Distributions rv = sysctlbyname("kern.bootargs", kernel_bootargs, &kernel_bootargs_size, NULL, 0);
146*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.bootargs");
147*699cd480SApple OSS Distributions
148*699cd480SApple OSS Distributions T_LOG("kern.bootargs: %s\n", kernel_bootargs);
149*699cd480SApple OSS Distributions
150*699cd480SApple OSS Distributions if (NULL == strstr(kernel_bootargs, "enable_skstb=1")) {
151*699cd480SApple OSS Distributions T_ASSERT_FAIL("enable_skstb=1 boot-arg is missing");
152*699cd480SApple OSS Distributions }
153*699cd480SApple OSS Distributions
154*699cd480SApple OSS Distributions kr = mach_timebase_info(&timebase_info);
155*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_timebase_info");
156*699cd480SApple OSS Distributions
157*699cd480SApple OSS Distributions struct sched_param param = {.sched_priority = 63};
158*699cd480SApple OSS Distributions
159*699cd480SApple OSS Distributions rv = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
160*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_setschedparam");
161*699cd480SApple OSS Distributions
162*699cd480SApple OSS Distributions T_SETUPEND;
163*699cd480SApple OSS Distributions }
164*699cd480SApple OSS Distributions
165*699cd480SApple OSS Distributions
166*699cd480SApple OSS Distributions T_DECL(count_cpus,
167*699cd480SApple OSS Distributions "Tests we can schedule bound threads on all hw.ncpus cores and that _os_cpu_number matches",
168*699cd480SApple OSS Distributions XNU_T_META_SOC_SPECIFIC)
169*699cd480SApple OSS Distributions {
170*699cd480SApple OSS Distributions int rv;
171*699cd480SApple OSS Distributions
172*699cd480SApple OSS Distributions cpucount_setup();
173*699cd480SApple OSS Distributions
174*699cd480SApple OSS Distributions int bound_cpu_out = 0;
175*699cd480SApple OSS Distributions size_t bound_cpu_out_size = sizeof(bound_cpu_out);
176*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0);
177*699cd480SApple OSS Distributions
178*699cd480SApple OSS Distributions if (rv == -1) {
179*699cd480SApple OSS Distributions if (errno == ENOENT) {
180*699cd480SApple OSS Distributions T_ASSERT_FAIL("kern.sched_thread_bind_cpu doesn't exist, must set enable_skstb=1 boot-arg on development kernel");
181*699cd480SApple OSS Distributions }
182*699cd480SApple OSS Distributions if (errno == EPERM) {
183*699cd480SApple OSS Distributions T_ASSERT_FAIL("must run as root");
184*699cd480SApple OSS Distributions }
185*699cd480SApple OSS Distributions }
186*699cd480SApple OSS Distributions
187*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu");
188*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(bound_cpu_out, -1, "kern.sched_thread_bind_cpu should exist, start unbound");
189*699cd480SApple OSS Distributions
190*699cd480SApple OSS Distributions uint32_t sysctl_ncpu = 0;
191*699cd480SApple OSS Distributions size_t ncpu_size = sizeof(sysctl_ncpu);
192*699cd480SApple OSS Distributions rv = sysctlbyname("hw.ncpu", &sysctl_ncpu, &ncpu_size, NULL, 0);
193*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "sysctlbyname(hw.ncpu)");
194*699cd480SApple OSS Distributions
195*699cd480SApple OSS Distributions T_LOG("hw.ncpu: %2d\n", sysctl_ncpu);
196*699cd480SApple OSS Distributions
197*699cd480SApple OSS Distributions T_ASSERT_GT(sysctl_ncpu, 0, "at least one CPU exists");
198*699cd480SApple OSS Distributions
199*699cd480SApple OSS Distributions for (uint32_t cpu_to_bind = 0; cpu_to_bind < sysctl_ncpu; cpu_to_bind++) {
200*699cd480SApple OSS Distributions int32_t before_csw_count = get_csw_count();
201*699cd480SApple OSS Distributions T_LOG("(csw %4d) attempting to bind to cpu %2d\n", before_csw_count, cpu_to_bind);
202*699cd480SApple OSS Distributions
203*699cd480SApple OSS Distributions uint64_t start = mach_absolute_time();
204*699cd480SApple OSS Distributions
205*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", NULL, 0, &cpu_to_bind, sizeof(cpu_to_bind));
206*699cd480SApple OSS Distributions
207*699cd480SApple OSS Distributions uint64_t end = mach_absolute_time();
208*699cd480SApple OSS Distributions
209*699cd480SApple OSS Distributions if (rv == -1 && errno == ENOTSUP) {
210*699cd480SApple OSS Distributions T_SKIP("Binding is available, but this process doesn't support binding (e.g. Rosetta on Aruba)");
211*699cd480SApple OSS Distributions }
212*699cd480SApple OSS Distributions
213*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cpu(%u)", cpu_to_bind);
214*699cd480SApple OSS Distributions
215*699cd480SApple OSS Distributions uint32_t os_cpu_number_reported = fixed_os_cpu_number();
216*699cd480SApple OSS Distributions
217*699cd480SApple OSS Distributions bound_cpu_out = 0;
218*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0);
219*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu");
220*699cd480SApple OSS Distributions
221*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ((int)cpu_to_bind, bound_cpu_out,
222*699cd480SApple OSS Distributions "should report bound cpu id matching requested bind target");
223*699cd480SApple OSS Distributions
224*699cd480SApple OSS Distributions uint64_t delta_abs = end - start;
225*699cd480SApple OSS Distributions uint64_t delta_ns = abs_to_nanos(delta_abs);
226*699cd480SApple OSS Distributions
227*699cd480SApple OSS Distributions int32_t after_csw_count = get_csw_count();
228*699cd480SApple OSS Distributions
229*699cd480SApple OSS Distributions T_LOG("(csw %4d) bound to cpu %2d in %f milliseconds\n",
230*699cd480SApple OSS Distributions after_csw_count, cpu_to_bind,
231*699cd480SApple OSS Distributions ((double)delta_ns / 1000000.0));
232*699cd480SApple OSS Distributions
233*699cd480SApple OSS Distributions if (cpu_to_bind > 0) {
234*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_LT(before_csw_count, after_csw_count,
235*699cd480SApple OSS Distributions "should have had to context switch to execute the bind");
236*699cd480SApple OSS Distributions }
237*699cd480SApple OSS Distributions
238*699cd480SApple OSS Distributions T_LOG("cpu %2d reported id %2d\n",
239*699cd480SApple OSS Distributions cpu_to_bind, os_cpu_number_reported);
240*699cd480SApple OSS Distributions
241*699cd480SApple OSS Distributions T_QUIET;
242*699cd480SApple OSS Distributions T_EXPECT_EQ(cpu_to_bind, os_cpu_number_reported,
243*699cd480SApple OSS Distributions "should report same CPU number as was bound to");
244*699cd480SApple OSS Distributions }
245*699cd480SApple OSS Distributions
246*699cd480SApple OSS Distributions int unbind = -1; /* pass -1 in order to unbind the thread */
247*699cd480SApple OSS Distributions
248*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", NULL, 0, &unbind, sizeof(unbind));
249*699cd480SApple OSS Distributions
250*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cpu(%u)", unbind);
251*699cd480SApple OSS Distributions
252*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0);
253*699cd480SApple OSS Distributions
254*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu");
255*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(bound_cpu_out, -1, "thread should be unbound at the end");
256*699cd480SApple OSS Distributions
257*699cd480SApple OSS Distributions T_PASS("test has run threads on all CPUS");
258*699cd480SApple OSS Distributions }
259*699cd480SApple OSS Distributions
260*699cd480SApple OSS Distributions T_DECL(count_clusters,
261*699cd480SApple OSS Distributions "Tests we can schedule bound threads on all cpu clusters and that _os_cpu_cluster_number matches",
262*699cd480SApple OSS Distributions XNU_T_META_SOC_SPECIFIC)
263*699cd480SApple OSS Distributions {
264*699cd480SApple OSS Distributions int rv;
265*699cd480SApple OSS Distributions
266*699cd480SApple OSS Distributions cpucount_setup();
267*699cd480SApple OSS Distributions
268*699cd480SApple OSS Distributions uint8_t cpuclusters = COMM_PAGE_READ(uint8_t, CPU_CLUSTERS);
269*699cd480SApple OSS Distributions T_LOG("cpuclusters: %2d\n", cpuclusters);
270*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GT(cpuclusters, 0, "at least one CPU cluster exists");
271*699cd480SApple OSS Distributions if (cpuclusters == 1) {
272*699cd480SApple OSS Distributions T_SKIP("Test is unsupported on non-AMP platforms");
273*699cd480SApple OSS Distributions }
274*699cd480SApple OSS Distributions
275*699cd480SApple OSS Distributions uint32_t sysctl_ncpu = 0;
276*699cd480SApple OSS Distributions size_t ncpu_size = sizeof(sysctl_ncpu);
277*699cd480SApple OSS Distributions rv = sysctlbyname("hw.ncpu", &sysctl_ncpu, &ncpu_size, NULL, 0);
278*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "sysctlbyname(hw.ncpu)");
279*699cd480SApple OSS Distributions T_LOG("hw.ncpu: %2d\n", sysctl_ncpu);
280*699cd480SApple OSS Distributions
281*699cd480SApple OSS Distributions uint64_t recommended_cores = 0;
282*699cd480SApple OSS Distributions size_t recommended_cores_size = sizeof(recommended_cores);
283*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_recommended_cores", &recommended_cores, &recommended_cores_size, NULL, 0);
284*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "sysctlbyname(kern.sched_recommended_cores)");
285*699cd480SApple OSS Distributions T_LOG("kern.sched_recommended_cores: %llu", recommended_cores);
286*699cd480SApple OSS Distributions if ((uint32_t)__builtin_popcountll(recommended_cores) != sysctl_ncpu) {
287*699cd480SApple OSS Distributions T_SKIP("Missing recommended cores");
288*699cd480SApple OSS Distributions }
289*699cd480SApple OSS Distributions
290*699cd480SApple OSS Distributions int bound_cluster_out = 0;
291*699cd480SApple OSS Distributions size_t bound_cluster_out_size = sizeof(bound_cluster_out);
292*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cluster_id", &bound_cluster_out, &bound_cluster_out_size, NULL, 0);
293*699cd480SApple OSS Distributions
294*699cd480SApple OSS Distributions if (rv == -1) {
295*699cd480SApple OSS Distributions if (errno == ENOENT) {
296*699cd480SApple OSS Distributions T_ASSERT_FAIL("kern.sched_thread_bind_cluster_id doesn't exist, must set enable_skstb=1 boot-arg on development kernel");
297*699cd480SApple OSS Distributions }
298*699cd480SApple OSS Distributions if (errno == EPERM) {
299*699cd480SApple OSS Distributions T_ASSERT_FAIL("must run as root");
300*699cd480SApple OSS Distributions }
301*699cd480SApple OSS Distributions }
302*699cd480SApple OSS Distributions
303*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cluster_id");
304*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(bound_cluster_out, -1, "kern.sched_thread_bind_cluster_id should exist, start unbound");
305*699cd480SApple OSS Distributions
306*699cd480SApple OSS Distributions for (uint32_t cluster_to_bind = 0; cluster_to_bind < cpuclusters; cluster_to_bind++) {
307*699cd480SApple OSS Distributions int32_t before_csw_count = get_csw_count();
308*699cd480SApple OSS Distributions T_LOG("(csw %4d) attempting to bind to cluster %2d\n", before_csw_count, cluster_to_bind);
309*699cd480SApple OSS Distributions
310*699cd480SApple OSS Distributions uint64_t start = mach_absolute_time();
311*699cd480SApple OSS Distributions
312*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cluster_id", NULL, 0, &cluster_to_bind, sizeof(cluster_to_bind));
313*699cd480SApple OSS Distributions
314*699cd480SApple OSS Distributions uint64_t end = mach_absolute_time();
315*699cd480SApple OSS Distributions
316*699cd480SApple OSS Distributions if (rv == -1 && errno == ENOTSUP) {
317*699cd480SApple OSS Distributions T_SKIP("Binding is available, but this process doesn't support binding (e.g. Rosetta on Aruba)");
318*699cd480SApple OSS Distributions }
319*699cd480SApple OSS Distributions
320*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cluster_id(%u)", cluster_to_bind);
321*699cd480SApple OSS Distributions
322*699cd480SApple OSS Distributions T_LOG("CPU ID: %d", fixed_os_cpu_number());
323*699cd480SApple OSS Distributions
324*699cd480SApple OSS Distributions #if TARGET_CPU_X86_64
325*699cd480SApple OSS Distributions T_LOG("_os_cpu_cluster_number unsupported under x86.");
326*699cd480SApple OSS Distributions #else
327*699cd480SApple OSS Distributions unsigned int os_cluster_number_reported = _os_cpu_cluster_number();
328*699cd480SApple OSS Distributions T_LOG("OS reported cluster number: %2d\n",
329*699cd480SApple OSS Distributions os_cluster_number_reported);
330*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(cluster_to_bind, os_cluster_number_reported,
331*699cd480SApple OSS Distributions "_os_cpu_cluster_number should report same cluster number as was bound to");
332*699cd480SApple OSS Distributions #endif
333*699cd480SApple OSS Distributions
334*699cd480SApple OSS Distributions unsigned int commpage_cluster_number_reported = commpage_cpu_cluster_number();
335*699cd480SApple OSS Distributions T_LOG("Comm Page reported cluster number: %u", commpage_cluster_number_reported);
336*699cd480SApple OSS Distributions T_EXPECT_EQ(commpage_cluster_number_reported, cluster_to_bind, "comm page cluster number matches commpage for this CPU");
337*699cd480SApple OSS Distributions
338*699cd480SApple OSS Distributions bound_cluster_out = 0;
339*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cluster_id", &bound_cluster_out, &bound_cluster_out_size, NULL, 0);
340*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cluster_id");
341*699cd480SApple OSS Distributions
342*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ((int)cluster_to_bind, bound_cluster_out,
343*699cd480SApple OSS Distributions "bound cluster id matches requested bind target");
344*699cd480SApple OSS Distributions
345*699cd480SApple OSS Distributions uint64_t delta_abs = end - start;
346*699cd480SApple OSS Distributions uint64_t delta_ns = abs_to_nanos(delta_abs);
347*699cd480SApple OSS Distributions
348*699cd480SApple OSS Distributions int32_t after_csw_count = get_csw_count();
349*699cd480SApple OSS Distributions
350*699cd480SApple OSS Distributions T_LOG("(csw %4d) bound to cluster %2d in %f milliseconds\n",
351*699cd480SApple OSS Distributions after_csw_count, cluster_to_bind,
352*699cd480SApple OSS Distributions ((double)delta_ns / 1000000.0));
353*699cd480SApple OSS Distributions
354*699cd480SApple OSS Distributions if (cluster_to_bind > 0) {
355*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_LT(before_csw_count, after_csw_count,
356*699cd480SApple OSS Distributions "should have had to context switch to execute the bind");
357*699cd480SApple OSS Distributions }
358*699cd480SApple OSS Distributions }
359*699cd480SApple OSS Distributions
360*699cd480SApple OSS Distributions int unbind = -1; /* pass -1 in order to unbind the thread */
361*699cd480SApple OSS Distributions
362*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cluster_id", NULL, 0, &unbind, sizeof(unbind));
363*699cd480SApple OSS Distributions
364*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cluster_id(%u)", unbind);
365*699cd480SApple OSS Distributions
366*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cluster_id", &bound_cluster_out, &bound_cluster_out_size, NULL, 0);
367*699cd480SApple OSS Distributions
368*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cluster_id");
369*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(bound_cluster_out, -1, "thread should be unbound at the end");
370*699cd480SApple OSS Distributions
371*699cd480SApple OSS Distributions T_PASS("test has run threads on all clusters");
372*699cd480SApple OSS Distributions }
373*699cd480SApple OSS Distributions
374*699cd480SApple OSS Distributions T_DECL(check_cpu_topology,
375*699cd480SApple OSS Distributions "Verify _os_cpu_cluster_number(), _os_cpu_number() against IORegistry",
376*699cd480SApple OSS Distributions XNU_T_META_SOC_SPECIFIC,
377*699cd480SApple OSS Distributions T_META_ENABLED(TARGET_CPU_ARM || TARGET_CPU_ARM64))
378*699cd480SApple OSS Distributions {
379*699cd480SApple OSS Distributions int rv;
380*699cd480SApple OSS Distributions uint32_t cpu_id, cluster_id;
381*699cd480SApple OSS Distributions kern_return_t kr;
382*699cd480SApple OSS Distributions io_iterator_t cpus_iter = 0;
383*699cd480SApple OSS Distributions io_service_t cpus_service = 0;
384*699cd480SApple OSS Distributions io_service_t cpu_service = 0;
385*699cd480SApple OSS Distributions CFDictionaryRef match = NULL;
386*699cd480SApple OSS Distributions
387*699cd480SApple OSS Distributions cpucount_setup();
388*699cd480SApple OSS Distributions
389*699cd480SApple OSS Distributions int bound_cpu_out = 0;
390*699cd480SApple OSS Distributions size_t bound_cpu_out_size = sizeof(bound_cpu_out);
391*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", &bound_cpu_out, &bound_cpu_out_size, NULL, 0);
392*699cd480SApple OSS Distributions
393*699cd480SApple OSS Distributions if (rv == -1) {
394*699cd480SApple OSS Distributions if (errno == ENOENT) {
395*699cd480SApple OSS Distributions T_FAIL("kern.sched_thread_bind_cpu doesn't exist, must set enable_skstb=1 boot-arg on development kernel");
396*699cd480SApple OSS Distributions }
397*699cd480SApple OSS Distributions if (errno == EPERM) {
398*699cd480SApple OSS Distributions T_FAIL("must run as root");
399*699cd480SApple OSS Distributions }
400*699cd480SApple OSS Distributions }
401*699cd480SApple OSS Distributions
402*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "read kern.sched_thread_bind_cpu");
403*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(bound_cpu_out, -1, "kern.sched_thread_bind_cpu should exist, start unbound");
404*699cd480SApple OSS Distributions
405*699cd480SApple OSS Distributions match = IOServiceNameMatching("cpus");
406*699cd480SApple OSS Distributions cpus_service = IOServiceGetMatchingService(kIOMainPortDefault, match);
407*699cd480SApple OSS Distributions match = NULL; // consumes reference to match
408*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NE(cpus_service, (io_service_t)0, "Failed get cpus IOService");
409*699cd480SApple OSS Distributions
410*699cd480SApple OSS Distributions kr = IORegistryEntryGetChildIterator(cpus_service, "IODeviceTree", &cpus_iter);
411*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "IORegistryEntryGetChildIterator");
412*699cd480SApple OSS Distributions
413*699cd480SApple OSS Distributions while ((cpu_service = IOIteratorNext(cpus_iter)) != 0) {
414*699cd480SApple OSS Distributions CFMutableDictionaryRef props = NULL;
415*699cd480SApple OSS Distributions kr = IORegistryEntryCreateCFProperties(cpu_service, &props, kCFAllocatorDefault, 0);
416*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "IORegistryEntryCreateCFProperties");
417*699cd480SApple OSS Distributions
418*699cd480SApple OSS Distributions dict_uint32(props, CFSTR("logical-cpu-id"), &cpu_id);
419*699cd480SApple OSS Distributions T_LOG("IORegistry logical cpu id: %u", cpu_id);
420*699cd480SApple OSS Distributions dict_uint32(props, CFSTR("logical-cluster-id"), &cluster_id);
421*699cd480SApple OSS Distributions T_LOG("IORegistry logical cpu cluster id: %u", cluster_id);
422*699cd480SApple OSS Distributions
423*699cd480SApple OSS Distributions T_LOG("Binding thread to cpu %u", cpu_id);
424*699cd480SApple OSS Distributions rv = sysctlbyname("kern.sched_thread_bind_cpu", NULL, 0, &cpu_id, sizeof(cpu_id));
425*699cd480SApple OSS Distributions if (rv == -1 && errno == ENOTSUP) {
426*699cd480SApple OSS Distributions T_SKIP("Binding is available, but this process doesn't support binding (e.g. Rosetta on Aruba)");
427*699cd480SApple OSS Distributions }
428*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.sched_thread_bind_cpu(%u)", cpu_id);
429*699cd480SApple OSS Distributions
430*699cd480SApple OSS Distributions unsigned int os_cpu_number_reported = fixed_os_cpu_number();
431*699cd480SApple OSS Distributions T_EXPECT_EQ(os_cpu_number_reported, cpu_id, "_os_cpu_number matches IORegistry entry for this CPU");
432*699cd480SApple OSS Distributions unsigned int os_cluster_number_reported = _os_cpu_cluster_number();
433*699cd480SApple OSS Distributions T_EXPECT_EQ(os_cluster_number_reported, cluster_id, "_os_cpu_cluster_number matches IORegistry entry for this CPU");
434*699cd480SApple OSS Distributions unsigned int commpage_cluster_number_reported = commpage_cpu_cluster_number();
435*699cd480SApple OSS Distributions T_EXPECT_EQ(commpage_cluster_number_reported, cluster_id, "comm page cluster number matches IORegistry entry for this CPU");
436*699cd480SApple OSS Distributions
437*699cd480SApple OSS Distributions CFRelease(props);
438*699cd480SApple OSS Distributions IOObjectRelease(cpu_service);
439*699cd480SApple OSS Distributions }
440*699cd480SApple OSS Distributions T_PASS("All cluster IDs match with IORegistry");
441*699cd480SApple OSS Distributions }
442