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