xref: /xnu-11215.41.3/tests/sched/cluster_bound_threads.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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 <darwintest_utils.h>
13 #include "test_utils.h"
14 #include "sched_test_utils.h"
15 
16 T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
17     T_META_RADAR_COMPONENT_NAME("xnu"),
18     T_META_RADAR_COMPONENT_VERSION("scheduler"));
19 
20 
21 static void *
spin_thread(__unused void * arg)22 spin_thread(__unused void *arg)
23 {
24 	spin_for_duration(8);
25 	return NULL;
26 }
27 
28 static void
bind_to_cluster(char type)29 bind_to_cluster(char type)
30 {
31 	char old_type;
32 	size_t type_size = sizeof(type);
33 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.sched_thread_bind_cluster_type",
34 	    &old_type, &type_size, &type, sizeof(type)),
35 	    "bind current thread to cluster %c", type);
36 }
37 
38 static void *
spin_bound_thread(void * arg)39 spin_bound_thread(void *arg)
40 {
41 	char type = (char)arg;
42 	bind_to_cluster(type);
43 	spin_for_duration(10);
44 	return NULL;
45 }
46 
47 #define SPINNER_THREAD_LOAD_FACTOR (4)
48 
49 T_DECL(test_cluster_bound_thread_timeshare, "Make sure the low priority bound threads get CPU in the presence of non-bound CPU spinners",
50     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, T_META_TAG_VM_NOT_ELIGIBLE)
51 {
52 	pthread_setname_np("main thread");
53 
54 	kern_return_t kr;
55 
56 	int rv;
57 	pthread_attr_t attr;
58 
59 	rv = pthread_attr_init(&attr);
60 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_init");
61 
62 	rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
63 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_setdetachstate");
64 
65 	rv = pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
66 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_set_qos_class_np");
67 
68 	unsigned int ncpu = (unsigned int)dt_ncpu();
69 	pthread_t unbound_thread;
70 	pthread_t bound_thread;
71 
72 	wait_for_quiescence_default();
73 	trace_handle_t trace = begin_collect_trace("test_cluster_bound_thread_timeshare");
74 
75 	T_LOG("creating %u non-bound threads\n", ncpu * SPINNER_THREAD_LOAD_FACTOR);
76 
77 	for (int i = 0; i < ncpu * SPINNER_THREAD_LOAD_FACTOR; i++) {
78 		rv = pthread_create(&unbound_thread, &attr, spin_thread, NULL);
79 		T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create (non-bound)");
80 	}
81 
82 	struct sched_param param = { .sched_priority = (int)20 };
83 	T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr, &param), "pthread_attr_setschedparam");
84 
85 	rv = pthread_create(&bound_thread, &attr, spin_bound_thread, (void *)(uintptr_t)'P');
86 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create (P-bound)");
87 
88 	rv = pthread_attr_destroy(&attr);
89 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_attr_destroy");
90 
91 	sleep(8);
92 
93 	mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
94 	mach_port_t thread_port = pthread_mach_thread_np(bound_thread);
95 	thread_basic_info_data_t bound_thread_info;
96 
97 	kr = thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&bound_thread_info, &count);
98 	if (kr != KERN_SUCCESS) {
99 		T_FAIL("%#x == thread_info(bound_thread, THREAD_BASIC_INFO)", kr);
100 	}
101 
102 	end_collect_trace(trace);
103 
104 	uint64_t bound_usr_usec = bound_thread_info.user_time.seconds * USEC_PER_SEC + bound_thread_info.user_time.microseconds;
105 
106 	T_ASSERT_GT(bound_usr_usec, 75000, "Check that bound thread got atleast 75ms CPU time");
107 	T_PASS("Low priority bound threads got some CPU time in the presence of high priority unbound spinners");
108 }
109