1*5e3eaea3SApple OSS Distributions #include <stdio.h>
2*5e3eaea3SApple OSS Distributions #include <stdlib.h>
3*5e3eaea3SApple OSS Distributions #include <unistd.h>
4*5e3eaea3SApple OSS Distributions #include <string.h>
5*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
6*5e3eaea3SApple OSS Distributions #include <mach/mach_time.h>
7*5e3eaea3SApple OSS Distributions #include <pthread.h>
8*5e3eaea3SApple OSS Distributions #include <TargetConditionals.h>
9*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
10*5e3eaea3SApple OSS Distributions
11*5e3eaea3SApple OSS Distributions #include <darwintest.h>
12*5e3eaea3SApple OSS Distributions #include "test_utils.h"
13*5e3eaea3SApple OSS Distributions
14*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_RADAR_COMPONENT_NAME("xnu"),
15*5e3eaea3SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"));
16*5e3eaea3SApple OSS Distributions
17*5e3eaea3SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
18*5e3eaea3SApple OSS Distributions
19*5e3eaea3SApple OSS Distributions static uint64_t
nanos_to_abs(uint64_t nanos)20*5e3eaea3SApple OSS Distributions nanos_to_abs(uint64_t nanos)
21*5e3eaea3SApple OSS Distributions {
22*5e3eaea3SApple OSS Distributions return nanos * timebase_info.denom / timebase_info.numer;
23*5e3eaea3SApple OSS Distributions }
24*5e3eaea3SApple OSS Distributions static uint64_t
abs_to_nanos(uint64_t abs)25*5e3eaea3SApple OSS Distributions abs_to_nanos(uint64_t abs)
26*5e3eaea3SApple OSS Distributions {
27*5e3eaea3SApple OSS Distributions return abs * timebase_info.numer / timebase_info.denom;
28*5e3eaea3SApple OSS Distributions }
29*5e3eaea3SApple OSS Distributions
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions /* Spin until a specified number of seconds elapses */
32*5e3eaea3SApple OSS Distributions static void
spin_for_duration(uint32_t seconds)33*5e3eaea3SApple OSS Distributions spin_for_duration(uint32_t seconds)
34*5e3eaea3SApple OSS Distributions {
35*5e3eaea3SApple OSS Distributions uint64_t duration = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC);
36*5e3eaea3SApple OSS Distributions uint64_t current_time = mach_absolute_time();
37*5e3eaea3SApple OSS Distributions uint64_t timeout = duration + current_time;
38*5e3eaea3SApple OSS Distributions
39*5e3eaea3SApple OSS Distributions uint64_t spin_count = 0;
40*5e3eaea3SApple OSS Distributions
41*5e3eaea3SApple OSS Distributions while (mach_absolute_time() < timeout) {
42*5e3eaea3SApple OSS Distributions spin_count++;
43*5e3eaea3SApple OSS Distributions }
44*5e3eaea3SApple OSS Distributions }
45*5e3eaea3SApple OSS Distributions
46*5e3eaea3SApple OSS Distributions static void *
spin_thread(__unused void * arg)47*5e3eaea3SApple OSS Distributions spin_thread(__unused void *arg)
48*5e3eaea3SApple OSS Distributions {
49*5e3eaea3SApple OSS Distributions spin_for_duration(8);
50*5e3eaea3SApple OSS Distributions return NULL;
51*5e3eaea3SApple OSS Distributions }
52*5e3eaea3SApple OSS Distributions
53*5e3eaea3SApple OSS Distributions void
bind_to_cluster(char type)54*5e3eaea3SApple OSS Distributions bind_to_cluster(char type)
55*5e3eaea3SApple OSS Distributions {
56*5e3eaea3SApple OSS Distributions char old_type;
57*5e3eaea3SApple OSS Distributions size_t type_size = sizeof(type);
58*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.sched_thread_bind_cluster_type",
59*5e3eaea3SApple OSS Distributions &old_type, &type_size, &type, sizeof(type)),
60*5e3eaea3SApple OSS Distributions "bind current thread to cluster %c", type);
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions
63*5e3eaea3SApple OSS Distributions static void *
spin_bound_thread(void * arg)64*5e3eaea3SApple OSS Distributions spin_bound_thread(void *arg)
65*5e3eaea3SApple OSS Distributions {
66*5e3eaea3SApple OSS Distributions char type = (char)arg;
67*5e3eaea3SApple OSS Distributions bind_to_cluster(type);
68*5e3eaea3SApple OSS Distributions spin_for_duration(10);
69*5e3eaea3SApple OSS Distributions return NULL;
70*5e3eaea3SApple OSS Distributions }
71*5e3eaea3SApple OSS Distributions
72*5e3eaea3SApple OSS Distributions static unsigned int
get_ncpu(void)73*5e3eaea3SApple OSS Distributions get_ncpu(void)
74*5e3eaea3SApple OSS Distributions {
75*5e3eaea3SApple OSS Distributions int ncpu;
76*5e3eaea3SApple OSS Distributions size_t sysctl_size = sizeof(ncpu);
77*5e3eaea3SApple OSS Distributions int ret = sysctlbyname("hw.ncpu", &ncpu, &sysctl_size, NULL, 0);
78*5e3eaea3SApple OSS Distributions assert(ret == 0);
79*5e3eaea3SApple OSS Distributions return (unsigned int) ncpu;
80*5e3eaea3SApple OSS Distributions }
81*5e3eaea3SApple OSS Distributions
82*5e3eaea3SApple OSS Distributions #define SPINNER_THREAD_LOAD_FACTOR (4)
83*5e3eaea3SApple OSS Distributions
84*5e3eaea3SApple OSS Distributions T_DECL(test_cluster_bound_thread_timeshare, "Make sure the low priority bound threads get CPU in the presence of non-bound CPU spinners",
85*5e3eaea3SApple OSS Distributions T_META_BOOTARGS_SET("enable_skstb=1"), T_META_ASROOT(true), T_META_ENABLED(TARGET_CPU_ARM64 && TARGET_OS_OSX), XNU_T_META_SOC_SPECIFIC)
86*5e3eaea3SApple OSS Distributions {
87*5e3eaea3SApple OSS Distributions pthread_setname_np("main thread");
88*5e3eaea3SApple OSS Distributions
89*5e3eaea3SApple OSS Distributions kern_return_t kr;
90*5e3eaea3SApple OSS Distributions
91*5e3eaea3SApple OSS Distributions kr = mach_timebase_info(&timebase_info);
92*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_timebase_info");
93*5e3eaea3SApple OSS Distributions
94*5e3eaea3SApple OSS Distributions int rv;
95*5e3eaea3SApple OSS Distributions pthread_attr_t attr;
96*5e3eaea3SApple OSS Distributions
97*5e3eaea3SApple OSS Distributions rv = pthread_attr_init(&attr);
98*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_init");
99*5e3eaea3SApple OSS Distributions
100*5e3eaea3SApple OSS Distributions rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
101*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_setdetachstate");
102*5e3eaea3SApple OSS Distributions
103*5e3eaea3SApple OSS Distributions rv = pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
104*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_set_qos_class_np");
105*5e3eaea3SApple OSS Distributions
106*5e3eaea3SApple OSS Distributions unsigned int ncpu = get_ncpu();
107*5e3eaea3SApple OSS Distributions pthread_t unbound_thread;
108*5e3eaea3SApple OSS Distributions pthread_t bound_thread;
109*5e3eaea3SApple OSS Distributions
110*5e3eaea3SApple OSS Distributions T_LOG("creating %u non-bound threads\n", ncpu * SPINNER_THREAD_LOAD_FACTOR);
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions for (int i = 0; i < ncpu * SPINNER_THREAD_LOAD_FACTOR; i++) {
113*5e3eaea3SApple OSS Distributions rv = pthread_create(&unbound_thread, &attr, spin_thread, NULL);
114*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create (non-bound)");
115*5e3eaea3SApple OSS Distributions }
116*5e3eaea3SApple OSS Distributions
117*5e3eaea3SApple OSS Distributions struct sched_param param = { .sched_priority = (int)20 };
118*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr, ¶m), "pthread_attr_setschedparam");
119*5e3eaea3SApple OSS Distributions
120*5e3eaea3SApple OSS Distributions rv = pthread_create(&bound_thread, &attr, spin_bound_thread, (void *)(uintptr_t)'P');
121*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create (P-bound)");
122*5e3eaea3SApple OSS Distributions
123*5e3eaea3SApple OSS Distributions rv = pthread_attr_destroy(&attr);
124*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_destroy");
125*5e3eaea3SApple OSS Distributions
126*5e3eaea3SApple OSS Distributions sleep(8);
127*5e3eaea3SApple OSS Distributions
128*5e3eaea3SApple OSS Distributions mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
129*5e3eaea3SApple OSS Distributions mach_port_t thread_port = pthread_mach_thread_np(bound_thread);
130*5e3eaea3SApple OSS Distributions thread_basic_info_data_t bound_thread_info;
131*5e3eaea3SApple OSS Distributions
132*5e3eaea3SApple OSS Distributions kr = thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&bound_thread_info, &count);
133*5e3eaea3SApple OSS Distributions if (kr != KERN_SUCCESS) {
134*5e3eaea3SApple OSS Distributions T_FAIL("%#x == thread_info(bound_thread, THREAD_BASIC_INFO)", kr);
135*5e3eaea3SApple OSS Distributions }
136*5e3eaea3SApple OSS Distributions
137*5e3eaea3SApple OSS Distributions uint64_t bound_usr_usec = bound_thread_info.user_time.seconds * USEC_PER_SEC + bound_thread_info.user_time.microseconds;
138*5e3eaea3SApple OSS Distributions
139*5e3eaea3SApple OSS Distributions T_ASSERT_GT(bound_usr_usec, 75000, "Check that bound thread got atleast 75ms CPU time");
140*5e3eaea3SApple OSS Distributions T_PASS("Low priority bound threads got some CPU time in the presence of high priority unbound spinners");
141*5e3eaea3SApple OSS Distributions }
142