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