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