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