1*699cd480SApple OSS Distributions #include <unistd.h>
2*699cd480SApple OSS Distributions #include <stdlib.h>
3*699cd480SApple OSS Distributions #include <pthread.h>
4*699cd480SApple OSS Distributions #include <spawn.h>
5*699cd480SApple OSS Distributions #include <mach/mach.h>
6*699cd480SApple OSS Distributions #include <mach/mach_time.h>
7*699cd480SApple OSS Distributions #include <TargetConditionals.h>
8*699cd480SApple OSS Distributions #include <sys/work_interval.h>
9*699cd480SApple OSS Distributions #include <sys/stat.h>
10*699cd480SApple OSS Distributions #include <sys/sysctl.h>
11*699cd480SApple OSS Distributions
12*699cd480SApple OSS Distributions #include <darwintest.h>
13*699cd480SApple OSS Distributions #include <perfdata/perfdata.h>
14*699cd480SApple OSS Distributions
15*699cd480SApple OSS Distributions extern unsigned char sched_thread_group_fairness_workload_config_plist[];
16*699cd480SApple OSS Distributions extern unsigned int sched_thread_group_fairness_workload_config_plist_len;
17*699cd480SApple OSS Distributions
18*699cd480SApple OSS Distributions #include "sched_thread_group_fairness_workload_config.h"
19*699cd480SApple OSS Distributions
20*699cd480SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
21*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
22*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
23*699cd480SApple OSS Distributions T_META_TAG_PERF);
24*699cd480SApple OSS Distributions
25*699cd480SApple OSS Distributions static const size_t MAX_PDJ_PATH_LEN = 256;
26*699cd480SApple OSS Distributions static unsigned int num_cores;
27*699cd480SApple OSS Distributions static unsigned int num_perf_levels;
28*699cd480SApple OSS Distributions
29*699cd480SApple OSS Distributions static bool
platform_is_amp(void)30*699cd480SApple OSS Distributions platform_is_amp(void)
31*699cd480SApple OSS Distributions {
32*699cd480SApple OSS Distributions if (num_perf_levels != 0) {
33*699cd480SApple OSS Distributions return num_perf_levels > 1;
34*699cd480SApple OSS Distributions }
35*699cd480SApple OSS Distributions int ret;
36*699cd480SApple OSS Distributions num_perf_levels = 0;
37*699cd480SApple OSS Distributions ret = sysctlbyname("hw.nperflevels", &num_perf_levels, &(size_t){ sizeof(num_perf_levels) }, NULL, 0);
38*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "hw.nperflevels");
39*699cd480SApple OSS Distributions return num_perf_levels > 1;
40*699cd480SApple OSS Distributions }
41*699cd480SApple OSS Distributions
42*699cd480SApple OSS Distributions static unsigned int
get_ncpu(void)43*699cd480SApple OSS Distributions get_ncpu(void)
44*699cd480SApple OSS Distributions {
45*699cd480SApple OSS Distributions int ret;
46*699cd480SApple OSS Distributions int ncpu;
47*699cd480SApple OSS Distributions char cpu_sysctl_name[32];
48*699cd480SApple OSS Distributions if (platform_is_amp()) {
49*699cd480SApple OSS Distributions sprintf(cpu_sysctl_name, "hw.perflevel%u.logicalcpu", num_perf_levels - 1);
50*699cd480SApple OSS Distributions } else {
51*699cd480SApple OSS Distributions sprintf(cpu_sysctl_name, "hw.ncpu");
52*699cd480SApple OSS Distributions }
53*699cd480SApple OSS Distributions ret = sysctlbyname(cpu_sysctl_name, &ncpu, &(size_t){ sizeof(ncpu) }, NULL, 0);
54*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "%s", cpu_sysctl_name);
55*699cd480SApple OSS Distributions return (unsigned int) ncpu;
56*699cd480SApple OSS Distributions }
57*699cd480SApple OSS Distributions
58*699cd480SApple OSS Distributions extern char **environ;
59*699cd480SApple OSS Distributions
60*699cd480SApple OSS Distributions static void
execute_clpcctrl(char * const clpcctrl_args[])61*699cd480SApple OSS Distributions execute_clpcctrl(char *const clpcctrl_args[])
62*699cd480SApple OSS Distributions {
63*699cd480SApple OSS Distributions int ret, pid;
64*699cd480SApple OSS Distributions ret = posix_spawn(&pid, clpcctrl_args[0], NULL, NULL, clpcctrl_args, environ);
65*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
66*699cd480SApple OSS Distributions waitpid(pid, &ret, 0);
67*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "waitpid");
68*699cd480SApple OSS Distributions }
69*699cd480SApple OSS Distributions
70*699cd480SApple OSS Distributions static void
clpcctrl_cleanup(void)71*699cd480SApple OSS Distributions clpcctrl_cleanup(void)
72*699cd480SApple OSS Distributions {
73*699cd480SApple OSS Distributions char *const recommend_all_cores_args[] = {"/usr/local/bin/clpcctrl", "-C", "all", NULL};
74*699cd480SApple OSS Distributions execute_clpcctrl(recommend_all_cores_args);
75*699cd480SApple OSS Distributions char *const restore_dynamic_control_args[] = {"/usr/local/bin/clpcctrl", "-d", NULL};
76*699cd480SApple OSS Distributions execute_clpcctrl(restore_dynamic_control_args);
77*699cd480SApple OSS Distributions }
78*699cd480SApple OSS Distributions
79*699cd480SApple OSS Distributions static void
workload_config_load(void)80*699cd480SApple OSS Distributions workload_config_load(void)
81*699cd480SApple OSS Distributions {
82*699cd480SApple OSS Distributions int ret;
83*699cd480SApple OSS Distributions size_t len = 0;
84*699cd480SApple OSS Distributions ret = sysctlbyname("kern.workload_config", NULL, &len,
85*699cd480SApple OSS Distributions sched_thread_group_fairness_workload_config_plist,
86*699cd480SApple OSS Distributions sched_thread_group_fairness_workload_config_plist_len);
87*699cd480SApple OSS Distributions if (ret == -1 && errno == ENOENT) {
88*699cd480SApple OSS Distributions T_SKIP("kern.workload_config failed");
89*699cd480SApple OSS Distributions }
90*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kern.workload_config");
91*699cd480SApple OSS Distributions }
92*699cd480SApple OSS Distributions
93*699cd480SApple OSS Distributions static void
workload_config_cleanup(void)94*699cd480SApple OSS Distributions workload_config_cleanup(void)
95*699cd480SApple OSS Distributions {
96*699cd480SApple OSS Distributions size_t len = 0;
97*699cd480SApple OSS Distributions sysctlbyname("kern.workload_config", NULL, &len, "", 1);
98*699cd480SApple OSS Distributions }
99*699cd480SApple OSS Distributions
100*699cd480SApple OSS Distributions static void
environment_init(void)101*699cd480SApple OSS Distributions environment_init(void)
102*699cd480SApple OSS Distributions {
103*699cd480SApple OSS Distributions num_cores = get_ncpu();
104*699cd480SApple OSS Distributions
105*699cd480SApple OSS Distributions if (platform_is_amp()) {
106*699cd480SApple OSS Distributions /*
107*699cd480SApple OSS Distributions * Derecommend all clusters except the E cores, to ensure that thread groups
108*699cd480SApple OSS Distributions * compete over the same cores irrespective of CLPC's cluster recommendations
109*699cd480SApple OSS Distributions */
110*699cd480SApple OSS Distributions T_ATEND(clpcctrl_cleanup);
111*699cd480SApple OSS Distributions char *const clpcctrl_args[] = {"/usr/local/bin/clpcctrl", "-C", "e", NULL};
112*699cd480SApple OSS Distributions execute_clpcctrl(clpcctrl_args);
113*699cd480SApple OSS Distributions }
114*699cd480SApple OSS Distributions
115*699cd480SApple OSS Distributions /*
116*699cd480SApple OSS Distributions * Load a test workload plist containing a Workload ID with
117*699cd480SApple OSS Distributions * WorkloadClass == DISCRETIONARY, in order to mark the thread group
118*699cd480SApple OSS Distributions * for that workload as THREAD_GROUP_FLAGS_EFFICIENT
119*699cd480SApple OSS Distributions */
120*699cd480SApple OSS Distributions T_ATEND(workload_config_cleanup);
121*699cd480SApple OSS Distributions workload_config_load();
122*699cd480SApple OSS Distributions }
123*699cd480SApple OSS Distributions
124*699cd480SApple OSS Distributions static void
set_work_interval_id(work_interval_t * handle,uint32_t work_interval_flags)125*699cd480SApple OSS Distributions set_work_interval_id(work_interval_t *handle, uint32_t work_interval_flags)
126*699cd480SApple OSS Distributions {
127*699cd480SApple OSS Distributions int ret;
128*699cd480SApple OSS Distributions mach_port_t port = MACH_PORT_NULL;
129*699cd480SApple OSS Distributions
130*699cd480SApple OSS Distributions ret = work_interval_copy_port(*handle, &port);
131*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "work_interval_copy_port");
132*699cd480SApple OSS Distributions
133*699cd480SApple OSS Distributions struct work_interval_workload_id_params wlid_params = {
134*699cd480SApple OSS Distributions .wlidp_flags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
135*699cd480SApple OSS Distributions .wlidp_wicreate_flags = work_interval_flags,
136*699cd480SApple OSS Distributions .wlidp_name = (uintptr_t)"com.test.myapp.discretionary",
137*699cd480SApple OSS Distributions };
138*699cd480SApple OSS Distributions
139*699cd480SApple OSS Distributions ret = __work_interval_ctl(WORK_INTERVAL_OPERATION_SET_WORKLOAD_ID, port, &wlid_params, sizeof(wlid_params));
140*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "WORK_INTERVAL_OPERATION_SET_WORKLOAD_ID");
141*699cd480SApple OSS Distributions }
142*699cd480SApple OSS Distributions
143*699cd480SApple OSS Distributions static uint32_t
make_work_interval(work_interval_t * handle,uint32_t work_type_flags)144*699cd480SApple OSS Distributions make_work_interval(work_interval_t *handle, uint32_t work_type_flags)
145*699cd480SApple OSS Distributions {
146*699cd480SApple OSS Distributions int ret;
147*699cd480SApple OSS Distributions uint32_t work_interval_flags = WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP | work_type_flags;
148*699cd480SApple OSS Distributions ret = work_interval_create(handle, work_interval_flags);
149*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create");
150*699cd480SApple OSS Distributions
151*699cd480SApple OSS Distributions if (work_type_flags & WORK_INTERVAL_FLAG_HAS_WORKLOAD_ID) {
152*699cd480SApple OSS Distributions set_work_interval_id(handle, work_interval_flags);
153*699cd480SApple OSS Distributions }
154*699cd480SApple OSS Distributions return work_interval_flags;
155*699cd480SApple OSS Distributions }
156*699cd480SApple OSS Distributions
157*699cd480SApple OSS Distributions struct thread_data {
158*699cd480SApple OSS Distributions work_interval_t *handle;
159*699cd480SApple OSS Distributions uint32_t work_interval_flags;
160*699cd480SApple OSS Distributions };
161*699cd480SApple OSS Distributions
162*699cd480SApple OSS Distributions static void *
spin_thread_fn(void * arg)163*699cd480SApple OSS Distributions spin_thread_fn(void *arg)
164*699cd480SApple OSS Distributions {
165*699cd480SApple OSS Distributions struct thread_data *info = (struct thread_data *)arg;
166*699cd480SApple OSS Distributions int ret;
167*699cd480SApple OSS Distributions
168*699cd480SApple OSS Distributions /* Join the thread group associated with the work interval handle */
169*699cd480SApple OSS Distributions ret = work_interval_join(*(info->handle));
170*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "work_interval_join");
171*699cd480SApple OSS Distributions
172*699cd480SApple OSS Distributions /* Spin indefinitely */
173*699cd480SApple OSS Distributions volatile uint64_t spin_count = 0;
174*699cd480SApple OSS Distributions while (mach_absolute_time() < UINT64_MAX) {
175*699cd480SApple OSS Distributions spin_count++;
176*699cd480SApple OSS Distributions }
177*699cd480SApple OSS Distributions return NULL;
178*699cd480SApple OSS Distributions }
179*699cd480SApple OSS Distributions
180*699cd480SApple OSS Distributions static void
start_threads(pthread_t * threads,struct thread_data * thread_datas,work_interval_t * handle,uint32_t work_interval_flags)181*699cd480SApple OSS Distributions start_threads(pthread_t *threads, struct thread_data *thread_datas, work_interval_t *handle, uint32_t work_interval_flags)
182*699cd480SApple OSS Distributions {
183*699cd480SApple OSS Distributions int ret;
184*699cd480SApple OSS Distributions for (unsigned int i = 0; i < num_cores; i++) {
185*699cd480SApple OSS Distributions thread_datas[i].handle = handle;
186*699cd480SApple OSS Distributions thread_datas[i].work_interval_flags = work_interval_flags;
187*699cd480SApple OSS Distributions ret = pthread_create(&threads[i], NULL, spin_thread_fn, &thread_datas[i]);
188*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "pthread_create");
189*699cd480SApple OSS Distributions }
190*699cd480SApple OSS Distributions }
191*699cd480SApple OSS Distributions
192*699cd480SApple OSS Distributions static uint64_t
snapshot_user_time_usec(pthread_t * threads)193*699cd480SApple OSS Distributions snapshot_user_time_usec(pthread_t *threads)
194*699cd480SApple OSS Distributions {
195*699cd480SApple OSS Distributions kern_return_t kr;
196*699cd480SApple OSS Distributions uint64_t cumulative_user_time_usec = 0;
197*699cd480SApple OSS Distributions mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
198*699cd480SApple OSS Distributions for (unsigned int i = 0; i < num_cores; i++) {
199*699cd480SApple OSS Distributions mach_port_t thread_port = pthread_mach_thread_np(threads[i]);
200*699cd480SApple OSS Distributions thread_basic_info_data_t info;
201*699cd480SApple OSS Distributions kr = thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
202*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
203*699cd480SApple OSS Distributions uint64_t thread_usr_usec = (uint64_t) (info.user_time.seconds) * USEC_PER_SEC + (uint64_t) info.user_time.microseconds;
204*699cd480SApple OSS Distributions cumulative_user_time_usec += thread_usr_usec;
205*699cd480SApple OSS Distributions }
206*699cd480SApple OSS Distributions return cumulative_user_time_usec;
207*699cd480SApple OSS Distributions }
208*699cd480SApple OSS Distributions
209*699cd480SApple OSS Distributions T_DECL(thread_group_fairness,
210*699cd480SApple OSS Distributions "Ensure that thread groups tagged as higher priority do not starve out "
211*699cd480SApple OSS Distributions "thread groups tagged as lower priority when both behave as CPU spinners",
212*699cd480SApple OSS Distributions T_META_ASROOT(YES))
213*699cd480SApple OSS Distributions {
214*699cd480SApple OSS Distributions T_SETUPBEGIN;
215*699cd480SApple OSS Distributions
216*699cd480SApple OSS Distributions environment_init();
217*699cd480SApple OSS Distributions
218*699cd480SApple OSS Distributions /*
219*699cd480SApple OSS Distributions * Create two work intervals with corresponding thread groups that would
220*699cd480SApple OSS Distributions * be associated with differing priorities.
221*699cd480SApple OSS Distributions */
222*699cd480SApple OSS Distributions work_interval_t lower_pri_handle, higher_pri_handle;
223*699cd480SApple OSS Distributions uint32_t lower_pri_flags = make_work_interval(&lower_pri_handle, WORK_INTERVAL_TYPE_DEFAULT | WORK_INTERVAL_FLAG_HAS_WORKLOAD_ID);
224*699cd480SApple OSS Distributions uint32_t higher_pri_flags = make_work_interval(&higher_pri_handle, WORK_INTERVAL_TYPE_DEFAULT);
225*699cd480SApple OSS Distributions
226*699cd480SApple OSS Distributions /* Start threads to join the lower priority thread group */
227*699cd480SApple OSS Distributions pthread_t lower_threads[num_cores];
228*699cd480SApple OSS Distributions struct thread_data lower_thread_datas[num_cores];
229*699cd480SApple OSS Distributions start_threads(lower_threads, lower_thread_datas, &lower_pri_handle, lower_pri_flags);
230*699cd480SApple OSS Distributions
231*699cd480SApple OSS Distributions /* Start threads to join the higher priority thread group */
232*699cd480SApple OSS Distributions pthread_t higher_threads[num_cores];
233*699cd480SApple OSS Distributions struct thread_data higher_thread_datas[num_cores];
234*699cd480SApple OSS Distributions start_threads(higher_threads, higher_thread_datas, &higher_pri_handle, higher_pri_flags);
235*699cd480SApple OSS Distributions
236*699cd480SApple OSS Distributions T_SETUPEND;
237*699cd480SApple OSS Distributions
238*699cd480SApple OSS Distributions /* Snapshot thread runtimes */
239*699cd480SApple OSS Distributions uint64_t start_lower_priority_runtime_usec = snapshot_user_time_usec(lower_threads);
240*699cd480SApple OSS Distributions uint64_t start_higher_priority_runtime_usec = snapshot_user_time_usec(higher_threads);
241*699cd480SApple OSS Distributions
242*699cd480SApple OSS Distributions /* Allow thread groups time to compete */
243*699cd480SApple OSS Distributions sleep(3);
244*699cd480SApple OSS Distributions
245*699cd480SApple OSS Distributions /*
246*699cd480SApple OSS Distributions * Snapshot runtimes again and compare the usage ratio between the lower and
247*699cd480SApple OSS Distributions * higher priority thread groups, to determine whether the lower priority group
248*699cd480SApple OSS Distributions * has been starved
249*699cd480SApple OSS Distributions */
250*699cd480SApple OSS Distributions uint64_t finish_lower_priority_runtime_usec = snapshot_user_time_usec(lower_threads);
251*699cd480SApple OSS Distributions uint64_t finish_higher_priority_runtime_usec = snapshot_user_time_usec(higher_threads);
252*699cd480SApple OSS Distributions
253*699cd480SApple OSS Distributions uint64_t lower_priority_runtime = finish_lower_priority_runtime_usec - start_lower_priority_runtime_usec;
254*699cd480SApple OSS Distributions uint64_t higher_priority_runtime = finish_higher_priority_runtime_usec - start_higher_priority_runtime_usec;
255*699cd480SApple OSS Distributions
256*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GT(lower_priority_runtime, 10000LL, "lower priority thread group got at least 10ms of CPU time");
257*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GT(higher_priority_runtime, 10000LL, "higher priority thread group got at least 10ms of CPU time");
258*699cd480SApple OSS Distributions
259*699cd480SApple OSS Distributions /* Record the observed runtime ratio */
260*699cd480SApple OSS Distributions char pdj_path[MAX_PDJ_PATH_LEN];
261*699cd480SApple OSS Distributions pdwriter_t writer = pdwriter_open_tmp("xnu", "scheduler.thread_group_fairness", 0, 0, pdj_path, MAX_PDJ_PATH_LEN);
262*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(writer, "pdwriter_open_tmp");
263*699cd480SApple OSS Distributions
264*699cd480SApple OSS Distributions double runtime_ratio_value;
265*699cd480SApple OSS Distributions double total_runtime = (double)(lower_priority_runtime + higher_priority_runtime);
266*699cd480SApple OSS Distributions if (lower_priority_runtime <= higher_priority_runtime) {
267*699cd480SApple OSS Distributions runtime_ratio_value = (double)(lower_priority_runtime) / total_runtime;
268*699cd480SApple OSS Distributions } else {
269*699cd480SApple OSS Distributions runtime_ratio_value = (double)(higher_priority_runtime) / total_runtime;
270*699cd480SApple OSS Distributions }
271*699cd480SApple OSS Distributions
272*699cd480SApple OSS Distributions pdwriter_new_value(writer, "Thread Group Runtime Ratio", PDUNIT_CUSTOM(runtime_ratio), runtime_ratio_value);
273*699cd480SApple OSS Distributions pdwriter_record_larger_better(writer);
274*699cd480SApple OSS Distributions pdwriter_close(writer);
275*699cd480SApple OSS Distributions /* Ensure that the perfdata file can be copied by BATS */
276*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(chmod(pdj_path, 0644), "chmod");
277*699cd480SApple OSS Distributions
278*699cd480SApple OSS Distributions T_END;
279*699cd480SApple OSS Distributions }
280