xref: /xnu-8796.141.3/tests/manual_wq_cooperative.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <assert.h>
2*1b191cb5SApple OSS Distributions #include <errno.h>
3*1b191cb5SApple OSS Distributions #include <stdio.h>
4*1b191cb5SApple OSS Distributions #include <stdlib.h>
5*1b191cb5SApple OSS Distributions #include <string.h>
6*1b191cb5SApple OSS Distributions #include <unistd.h>
7*1b191cb5SApple OSS Distributions #include <sys/types.h>
8*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
9*1b191cb5SApple OSS Distributions #include <sys/qos.h>
10*1b191cb5SApple OSS Distributions 
11*1b191cb5SApple OSS Distributions #include <dispatch/dispatch.h>
12*1b191cb5SApple OSS Distributions #include <os/lock.h>
13*1b191cb5SApple OSS Distributions #include <mach/mach.h>
14*1b191cb5SApple OSS Distributions #include <mach/mach_time.h>
15*1b191cb5SApple OSS Distributions 
16*1b191cb5SApple OSS Distributions #include <pthread/workqueue_private.h>
17*1b191cb5SApple OSS Distributions #include <pthread/qos_private.h>
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions static dispatch_group_t group;
20*1b191cb5SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions static void
req_cooperative_wq_threads(qos_class_t qos,size_t num_threads)23*1b191cb5SApple OSS Distributions req_cooperative_wq_threads(qos_class_t qos, size_t num_threads)
24*1b191cb5SApple OSS Distributions {
25*1b191cb5SApple OSS Distributions 	int ret;
26*1b191cb5SApple OSS Distributions 
27*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < num_threads; i++) {
28*1b191cb5SApple OSS Distributions 		dispatch_group_enter(group);
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions 		ret = _pthread_workqueue_add_cooperativethreads(1,
31*1b191cb5SApple OSS Distributions 		    _pthread_qos_class_encode(qos, 0, 0));
32*1b191cb5SApple OSS Distributions 		assert(ret == 0);
33*1b191cb5SApple OSS Distributions 	}
34*1b191cb5SApple OSS Distributions }
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions static void
req_wq_threads(qos_class_t qos,size_t num_threads,bool overcommit)37*1b191cb5SApple OSS Distributions req_wq_threads(qos_class_t qos, size_t num_threads, bool overcommit)
38*1b191cb5SApple OSS Distributions {
39*1b191cb5SApple OSS Distributions 	int ret;
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < num_threads; i++) {
42*1b191cb5SApple OSS Distributions 		dispatch_group_enter(group);
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions 		ret = _pthread_workqueue_addthreads(1,
45*1b191cb5SApple OSS Distributions 		    _pthread_qos_class_encode(qos, 0,
46*1b191cb5SApple OSS Distributions 		    (overcommit ? _PTHREAD_PRIORITY_OVERCOMMIT_FLAG : 0)));
47*1b191cb5SApple OSS Distributions 		assert(ret == 0);
48*1b191cb5SApple OSS Distributions 	}
49*1b191cb5SApple OSS Distributions }
50*1b191cb5SApple OSS Distributions 
51*1b191cb5SApple OSS Distributions static uint32_t
ncpus(void)52*1b191cb5SApple OSS Distributions ncpus(void)
53*1b191cb5SApple OSS Distributions {
54*1b191cb5SApple OSS Distributions 	static uint32_t num_cpus;
55*1b191cb5SApple OSS Distributions 	if (!num_cpus) {
56*1b191cb5SApple OSS Distributions 		uint32_t n;
57*1b191cb5SApple OSS Distributions 		size_t s = sizeof(n);
58*1b191cb5SApple OSS Distributions 		sysctlbyname("hw.ncpu", &n, &s, NULL, 0);
59*1b191cb5SApple OSS Distributions 		num_cpus = n;
60*1b191cb5SApple OSS Distributions 	}
61*1b191cb5SApple OSS Distributions 	return num_cpus;
62*1b191cb5SApple OSS Distributions }
63*1b191cb5SApple OSS Distributions 
64*1b191cb5SApple OSS Distributions static inline bool
thread_is_overcommit(pthread_priority_t priority)65*1b191cb5SApple OSS Distributions thread_is_overcommit(pthread_priority_t priority)
66*1b191cb5SApple OSS Distributions {
67*1b191cb5SApple OSS Distributions 	return (priority & _PTHREAD_PRIORITY_OVERCOMMIT_FLAG) != 0;
68*1b191cb5SApple OSS Distributions }
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions static inline bool
thread_is_nonovercommit(pthread_priority_t priority)71*1b191cb5SApple OSS Distributions thread_is_nonovercommit(pthread_priority_t priority)
72*1b191cb5SApple OSS Distributions {
73*1b191cb5SApple OSS Distributions 	return (priority & (_PTHREAD_PRIORITY_OVERCOMMIT_FLAG | _PTHREAD_PRIORITY_COOPERATIVE_FLAG)) != 0;
74*1b191cb5SApple OSS Distributions }
75*1b191cb5SApple OSS Distributions 
76*1b191cb5SApple OSS Distributions static inline bool
thread_is_cooperative(pthread_priority_t priority)77*1b191cb5SApple OSS Distributions thread_is_cooperative(pthread_priority_t priority)
78*1b191cb5SApple OSS Distributions {
79*1b191cb5SApple OSS Distributions 	return (priority & _PTHREAD_PRIORITY_COOPERATIVE_FLAG) != 0;
80*1b191cb5SApple OSS Distributions }
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions qos_class_t
thread_has_qos(pthread_priority_t pri)83*1b191cb5SApple OSS Distributions thread_has_qos(pthread_priority_t pri)
84*1b191cb5SApple OSS Distributions {
85*1b191cb5SApple OSS Distributions 	return _pthread_qos_class_decode(pri, NULL, NULL);
86*1b191cb5SApple OSS Distributions }
87*1b191cb5SApple OSS Distributions 
88*1b191cb5SApple OSS Distributions char *
qos_to_str(qos_class_t qos)89*1b191cb5SApple OSS Distributions qos_to_str(qos_class_t qos)
90*1b191cb5SApple OSS Distributions {
91*1b191cb5SApple OSS Distributions 	switch (qos) {
92*1b191cb5SApple OSS Distributions 	case QOS_CLASS_MAINTENANCE:
93*1b191cb5SApple OSS Distributions 		return "MT";
94*1b191cb5SApple OSS Distributions 	case QOS_CLASS_BACKGROUND:
95*1b191cb5SApple OSS Distributions 		return "BG";
96*1b191cb5SApple OSS Distributions 	case QOS_CLASS_UTILITY:
97*1b191cb5SApple OSS Distributions 		return "UT";
98*1b191cb5SApple OSS Distributions 	case QOS_CLASS_DEFAULT:
99*1b191cb5SApple OSS Distributions 		return "DEF";
100*1b191cb5SApple OSS Distributions 	case QOS_CLASS_USER_INITIATED:
101*1b191cb5SApple OSS Distributions 		return "IN";
102*1b191cb5SApple OSS Distributions 	case QOS_CLASS_USER_INTERACTIVE:
103*1b191cb5SApple OSS Distributions 		return "UI";
104*1b191cb5SApple OSS Distributions 	}
105*1b191cb5SApple OSS Distributions }
106*1b191cb5SApple OSS Distributions 
107*1b191cb5SApple OSS Distributions /*
108*1b191cb5SApple OSS Distributions  * Test that we handle cooperative requests first and then overcommit if they
109*1b191cb5SApple OSS Distributions  * are at the same QoS
110*1b191cb5SApple OSS Distributions  */
111*1b191cb5SApple OSS Distributions 
112*1b191cb5SApple OSS Distributions static bool overcommit_thread_request_handled = false;
113*1b191cb5SApple OSS Distributions static bool cooperative_thread_request_handled = false;
114*1b191cb5SApple OSS Distributions 
115*1b191cb5SApple OSS Distributions static void
worker_cooperative_then_overcommit(pthread_priority_t priority)116*1b191cb5SApple OSS Distributions worker_cooperative_then_overcommit(pthread_priority_t priority)
117*1b191cb5SApple OSS Distributions {
118*1b191cb5SApple OSS Distributions 	if (thread_is_cooperative(priority)) {
119*1b191cb5SApple OSS Distributions 		assert(!overcommit_thread_request_handled);
120*1b191cb5SApple OSS Distributions 		cooperative_thread_request_handled = true;
121*1b191cb5SApple OSS Distributions 	} else if (thread_is_overcommit(priority)) {
122*1b191cb5SApple OSS Distributions 		assert(cooperative_thread_request_handled);
123*1b191cb5SApple OSS Distributions 		overcommit_thread_request_handled = true;
124*1b191cb5SApple OSS Distributions 	}
125*1b191cb5SApple OSS Distributions 
126*1b191cb5SApple OSS Distributions 	dispatch_group_leave(group);
127*1b191cb5SApple OSS Distributions }
128*1b191cb5SApple OSS Distributions 
129*1b191cb5SApple OSS Distributions int
do_cooperative_then_overcommit()130*1b191cb5SApple OSS Distributions do_cooperative_then_overcommit()
131*1b191cb5SApple OSS Distributions {
132*1b191cb5SApple OSS Distributions 	int ret = _pthread_workqueue_init(worker_cooperative_then_overcommit, 0, 0);
133*1b191cb5SApple OSS Distributions 	assert(ret == 0);
134*1b191cb5SApple OSS Distributions 
135*1b191cb5SApple OSS Distributions 	req_wq_threads(QOS_CLASS_USER_INITIATED, 1, true);
136*1b191cb5SApple OSS Distributions 	req_cooperative_wq_threads(QOS_CLASS_USER_INITIATED, 1);
137*1b191cb5SApple OSS Distributions 
138*1b191cb5SApple OSS Distributions 	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
139*1b191cb5SApple OSS Distributions 	return 0;
140*1b191cb5SApple OSS Distributions }
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions /*
143*1b191cb5SApple OSS Distributions  * Test thread reuse from cooperative requests
144*1b191cb5SApple OSS Distributions  */
145*1b191cb5SApple OSS Distributions 
146*1b191cb5SApple OSS Distributions bool test_should_end = false;
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions qos_class_t
get_rand_qos_class(void)149*1b191cb5SApple OSS Distributions get_rand_qos_class(void)
150*1b191cb5SApple OSS Distributions {
151*1b191cb5SApple OSS Distributions 	switch (rand() % 6) {
152*1b191cb5SApple OSS Distributions 	case 0:
153*1b191cb5SApple OSS Distributions 		return QOS_CLASS_MAINTENANCE;
154*1b191cb5SApple OSS Distributions 	case 1:
155*1b191cb5SApple OSS Distributions 		return QOS_CLASS_BACKGROUND;
156*1b191cb5SApple OSS Distributions 	case 2:
157*1b191cb5SApple OSS Distributions 		return QOS_CLASS_UTILITY;
158*1b191cb5SApple OSS Distributions 	case 3:
159*1b191cb5SApple OSS Distributions 		return QOS_CLASS_DEFAULT;
160*1b191cb5SApple OSS Distributions 	case 4:
161*1b191cb5SApple OSS Distributions 		return QOS_CLASS_USER_INITIATED;
162*1b191cb5SApple OSS Distributions 	case 5:
163*1b191cb5SApple OSS Distributions 		return QOS_CLASS_USER_INTERACTIVE;
164*1b191cb5SApple OSS Distributions 	}
165*1b191cb5SApple OSS Distributions }
166*1b191cb5SApple OSS Distributions 
167*1b191cb5SApple OSS Distributions int
get_rand_num_thread_requests(void)168*1b191cb5SApple OSS Distributions get_rand_num_thread_requests(void)
169*1b191cb5SApple OSS Distributions {
170*1b191cb5SApple OSS Distributions 	return rand() % (ncpus() * 2);
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions uint64_t
get_rand_spin_duration_nsecs(void)174*1b191cb5SApple OSS Distributions get_rand_spin_duration_nsecs(void)
175*1b191cb5SApple OSS Distributions {
176*1b191cb5SApple OSS Distributions 	/* Spin for at most half a second */
177*1b191cb5SApple OSS Distributions 	return rand() % (NSEC_PER_SEC / 2);
178*1b191cb5SApple OSS Distributions }
179*1b191cb5SApple OSS Distributions 
180*1b191cb5SApple OSS Distributions void
spin(uint64_t spin_duration_nsecs)181*1b191cb5SApple OSS Distributions spin(uint64_t spin_duration_nsecs)
182*1b191cb5SApple OSS Distributions {
183*1b191cb5SApple OSS Distributions 	uint64_t duration = spin_duration_nsecs * timebase_info.denom / timebase_info.numer;
184*1b191cb5SApple OSS Distributions 	uint64_t deadline = mach_absolute_time() + duration;
185*1b191cb5SApple OSS Distributions 	while (mach_absolute_time() < deadline) {
186*1b191cb5SApple OSS Distributions 		;
187*1b191cb5SApple OSS Distributions 	}
188*1b191cb5SApple OSS Distributions }
189*1b191cb5SApple OSS Distributions 
190*1b191cb5SApple OSS Distributions static void
worker_cb_stress(pthread_priority_t priority)191*1b191cb5SApple OSS Distributions worker_cb_stress(pthread_priority_t priority)
192*1b191cb5SApple OSS Distributions {
193*1b191cb5SApple OSS Distributions 	if (test_should_end) {
194*1b191cb5SApple OSS Distributions 		dispatch_group_leave(group);
195*1b191cb5SApple OSS Distributions 		return;
196*1b191cb5SApple OSS Distributions 	}
197*1b191cb5SApple OSS Distributions 
198*1b191cb5SApple OSS Distributions 	if (thread_is_cooperative(priority)) {
199*1b191cb5SApple OSS Distributions 		printf("\t Cooperative thread of QoS %s\n", qos_to_str(thread_has_qos(priority)));
200*1b191cb5SApple OSS Distributions 		spin(get_rand_spin_duration_nsecs());
201*1b191cb5SApple OSS Distributions 		req_wq_threads(get_rand_qos_class(), get_rand_num_thread_requests(), false);
202*1b191cb5SApple OSS Distributions 	} else if (thread_is_nonovercommit(priority)) {
203*1b191cb5SApple OSS Distributions 		printf("\t Nonovercommit thread of QoS %s\n", qos_to_str(thread_has_qos(priority)));
204*1b191cb5SApple OSS Distributions 
205*1b191cb5SApple OSS Distributions 		spin(get_rand_spin_duration_nsecs());
206*1b191cb5SApple OSS Distributions 		req_cooperative_wq_threads(get_rand_qos_class(), get_rand_num_thread_requests());
207*1b191cb5SApple OSS Distributions 	} else {
208*1b191cb5SApple OSS Distributions 		printf("\t Overcommit thread of QoS %s\n", qos_to_str(thread_has_qos(priority)));
209*1b191cb5SApple OSS Distributions 		req_wq_threads(get_rand_qos_class(), get_rand_num_thread_requests(), true);
210*1b191cb5SApple OSS Distributions 		spin(get_rand_spin_duration_nsecs());
211*1b191cb5SApple OSS Distributions 	}
212*1b191cb5SApple OSS Distributions 
213*1b191cb5SApple OSS Distributions 	dispatch_group_leave(group);
214*1b191cb5SApple OSS Distributions }
215*1b191cb5SApple OSS Distributions 
216*1b191cb5SApple OSS Distributions int
do_stress_test()217*1b191cb5SApple OSS Distributions do_stress_test()
218*1b191cb5SApple OSS Distributions {
219*1b191cb5SApple OSS Distributions 	int ret = _pthread_workqueue_init(worker_cb_stress, 0, 0);
220*1b191cb5SApple OSS Distributions 	assert(ret == 0);
221*1b191cb5SApple OSS Distributions 
222*1b191cb5SApple OSS Distributions 	req_wq_threads(QOS_CLASS_DEFAULT, ncpus() / 2, true);
223*1b191cb5SApple OSS Distributions 	req_cooperative_wq_threads(QOS_CLASS_USER_INITIATED, ncpus());
224*1b191cb5SApple OSS Distributions 	req_wq_threads(QOS_CLASS_DEFAULT, ncpus(), false);
225*1b191cb5SApple OSS Distributions 
226*1b191cb5SApple OSS Distributions 	sleep(10);
227*1b191cb5SApple OSS Distributions 
228*1b191cb5SApple OSS Distributions 	test_should_end = true;
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions 	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
231*1b191cb5SApple OSS Distributions 	printf("\n All thread requests completed\n");
232*1b191cb5SApple OSS Distributions 	return 0;
233*1b191cb5SApple OSS Distributions }
234*1b191cb5SApple OSS Distributions 
235*1b191cb5SApple OSS Distributions int
main(int argc,char * argv[])236*1b191cb5SApple OSS Distributions main(int argc, char * argv[])
237*1b191cb5SApple OSS Distributions {
238*1b191cb5SApple OSS Distributions 	int ret = 0;
239*1b191cb5SApple OSS Distributions 
240*1b191cb5SApple OSS Distributions 	if (argc < 2) {
241*1b191cb5SApple OSS Distributions 		return EINVAL;
242*1b191cb5SApple OSS Distributions 	}
243*1b191cb5SApple OSS Distributions 
244*1b191cb5SApple OSS Distributions 	const char *cmd = argv[1];
245*1b191cb5SApple OSS Distributions 
246*1b191cb5SApple OSS Distributions 	group = dispatch_group_create();
247*1b191cb5SApple OSS Distributions 	mach_timebase_info(&timebase_info);
248*1b191cb5SApple OSS Distributions 
249*1b191cb5SApple OSS Distributions 	if (strcmp(cmd, "cooperative_then_overcommit") == 0) {
250*1b191cb5SApple OSS Distributions 		return do_cooperative_then_overcommit();
251*1b191cb5SApple OSS Distributions 	}
252*1b191cb5SApple OSS Distributions 
253*1b191cb5SApple OSS Distributions 	if (strcmp(cmd, "stress_test") == 0) {
254*1b191cb5SApple OSS Distributions 		return do_stress_test();
255*1b191cb5SApple OSS Distributions 	}
256*1b191cb5SApple OSS Distributions 
257*1b191cb5SApple OSS Distributions 	return -1;
258*1b191cb5SApple OSS Distributions }
259