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