2 * turnstile_multihop: Tests turnstile and multi hop priority propagation.
9 #include <darwintest.h>
10 #include <darwintest_multiprocess.h>
12 #include <dispatch/dispatch.h>
15 #include <mach/mach.h>
16 #include <mach/message.h>
17 #include <mach/mach_voucher.h>
18 #include <pthread/workqueue_private.h>
19 #include <voucher/ipc_pthread_priority_types.h>
20 #include <servers/bootstrap.h>
22 #include <sys/event.h>
24 #include <crt_externs.h>
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #include <libkern/OSAtomic.h>
31 #include "turnstile_multihop_helper.h"
33 T_GLOBAL_META(T_META_NAMESPACE("xnu.turnstile_multihop"));
35 #define HELPER_TIMEOUT_SECS (3000)
38 mach_msg_header_t header
;
40 mach_msg_port_descriptor_t port_descriptor
;
43 static boolean_t spin_for_ever
= false;
45 static boolean_t test_noimportance
= false;
47 #define EXPECTED_MESSAGE_ID 0x100
50 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *));
52 nanoseconds_to_absolutetime(uint64_t nanoseconds
);
54 sched_create_load_at_qos(qos_class_t qos
, void **load_token
);
56 sched_terminate_load(void *load_token
) __unused
;
57 static void do_work(int num
);
59 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
);
61 static void *sched_load_thread(void *);
63 struct load_token_context
{
64 volatile int threads_should_exit
;
70 static struct mach_timebase_info sched_mti
;
71 static pthread_once_t sched_mti_once_control
= PTHREAD_ONCE_INIT
;
76 mach_timebase_info(&sched_mti
);
79 nanoseconds_to_absolutetime(uint64_t nanoseconds
)
81 pthread_once(&sched_mti_once_control
, sched_mti_init
);
83 return (uint64_t)(nanoseconds
* (((double)sched_mti
.denom
) / ((double)sched_mti
.numer
)));
87 sched_create_load_at_qos(qos_class_t qos
, void **load_token
)
89 struct load_token_context
*context
= NULL
;
92 size_t ncpu_size
= sizeof(ncpu
);
97 ret
= sysctlbyname("hw.ncpu", &ncpu
, &ncpu_size
, NULL
, 0);
99 T_LOG("sysctlbyname(hw.ncpu)");
103 T_QUIET
; T_LOG("%s: Detected %d CPUs\n", __FUNCTION__
, ncpu
);
106 T_QUIET
; T_LOG("%s: Will create %d threads\n", __FUNCTION__
, nthreads
);
108 ret
= pthread_attr_init(&attr
);
109 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_init");
111 if (&pthread_attr_set_qos_class_np
) {
112 ret
= pthread_attr_set_qos_class_np(&attr
, qos
, 0);
113 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_set_qos_class_np");
116 context
= calloc(1, sizeof(*context
));
117 if (context
== NULL
) {
118 T_QUIET
; T_LOG("calloc returned error"); return ENOMEM
;
121 context
->threads_should_exit
= 0;
122 context
->thread_count
= nthreads
;
124 context
->threads
= calloc((unsigned int)nthreads
, sizeof(pthread_t
));
128 for (i
= 0; i
< nthreads
; i
++) {
129 ret
= pthread_create(&context
->threads
[i
], &attr
, sched_load_thread
, context
);
130 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_create");
131 T_QUIET
; T_LOG("%s: Created thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
134 ret
= pthread_attr_destroy(&attr
);
135 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_destroy");
137 *load_token
= context
;
143 sched_load_thread(void *arg
)
145 struct load_token_context
*context
= (struct load_token_context
*)arg
;
147 T_QUIET
; T_LOG("%s: Thread started %p\n", __FUNCTION__
, (void *)pthread_self());
149 while (!context
->threads_should_exit
) {
150 uint64_t start
= mach_absolute_time();
151 uint64_t end
= start
+ nanoseconds_to_absolutetime(900ULL * NSEC_PER_MSEC
);
153 while ((mach_absolute_time() < end
) && !context
->threads_should_exit
) {
158 T_QUIET
; T_LOG("%s: Thread terminating %p\n", __FUNCTION__
, (void *)pthread_self());
164 sched_terminate_load(void *load_token
)
168 struct load_token_context
*context
= (struct load_token_context
*)load_token
;
170 context
->threads_should_exit
= 1;
173 for (i
= 0; i
< context
->thread_count
; i
++) {
174 T_QUIET
; T_LOG("%s: Joining thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
175 ret
= pthread_join(context
->threads
[i
], NULL
);
176 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_join");
179 free(context
->threads
);
186 // Find the first num primes, simply as a means of doing work
190 volatile int i
= 3, count
, c
;
192 for (count
= 2; count
<= num
;) {
193 for (c
= 2; c
<= i
; c
++) {
205 #pragma mark pthread callbacks
208 worker_cb(pthread_priority_t __unused priority
)
210 T_FAIL("a worker thread was created");
214 event_cb(void ** __unused events
, int * __unused nevents
)
216 T_FAIL("a kevent routine was called instead of workloop");
220 get_user_promotion_basepri(void)
222 mach_msg_type_number_t count
= THREAD_POLICY_STATE_COUNT
;
223 struct thread_policy_state thread_policy
;
224 boolean_t get_default
= FALSE
;
225 mach_port_t thread_port
= pthread_mach_thread_np(pthread_self());
227 kern_return_t kr
= thread_policy_get(thread_port
, THREAD_POLICY_STATE
,
228 (thread_policy_t
)&thread_policy
, &count
, &get_default
);
229 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_policy_get");
230 return thread_policy
.thps_user_promotion_basepri
;
234 get_thread_base_priority(void)
237 mach_port_t thread_port
= pthread_mach_thread_np(pthread_self());
239 policy_timeshare_info_data_t timeshare_info
;
240 mach_msg_type_number_t count
= POLICY_TIMESHARE_INFO_COUNT
;
242 kr
= thread_info(thread_port
, THREAD_SCHED_TIMESHARE_INFO
,
243 (thread_info_t
)×hare_info
, &count
);
244 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
246 return (uint32_t)timeshare_info
.base_priority
;
250 #define LISTENER_WLID 0x100
251 #define CONN_WLID 0x200
254 register_port_options(void)
256 return MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
257 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
258 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
263 register_port(uint64_t wlid
, mach_port_t port
)
267 struct kevent_qos_s kev
= {
269 .filter
= EVFILT_MACHPORT
,
270 .flags
= EV_ADD
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
,
271 .fflags
= register_port_options(),
273 .qos
= (int32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0)
276 struct kevent_qos_s kev_err
= { 0 };
278 /* Setup workloop for mach msg rcv */
279 r
= kevent_id(wlid
, &kev
, 1, &kev_err
, 1, NULL
,
280 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
282 T_QUIET
; T_ASSERT_POSIX_SUCCESS(r
, "kevent_id");
283 T_QUIET
; T_ASSERT_EQ(r
, 0, "no errors returned from kevent_id");
287 * Basic WL handler callback, it checks the
288 * effective Qos of the servicer thread.
291 workloop_cb_test_intransit(uint64_t *workloop_id
, void **eventslist
, int *events
)
293 static bool got_peer
;
295 struct kevent_qos_s
*kev
= eventslist
[0];
296 mach_msg_header_t
*hdr
;
297 struct test_msg
*tmsg
;
299 T_LOG("Workloop handler %s called. Received message on 0x%llx",
300 __func__
, *workloop_id
);
302 /* Skip the test if we can't check Qos */
303 if (geteuid() != 0) {
304 T_SKIP("kevent_qos test requires root privileges to run.");
307 T_QUIET
; T_ASSERT_EQ(*events
, 1, "should have one event");
309 T_EXPECT_REQUESTED_QOS_EQ(QOS_CLASS_MAINTENANCE
, "message handler should have MT requested QoS");
311 hdr
= (mach_msg_header_t
*)kev
->ext
[0];
312 T_ASSERT_NOTNULL(hdr
, "has a message");
313 T_ASSERT_EQ(hdr
->msgh_size
, (uint32_t)sizeof(struct test_msg
), "of the right size");
314 tmsg
= (struct test_msg
*)hdr
;
316 switch (*workloop_id
) {
318 T_LOG("Registering peer connection");
319 T_QUIET
; T_ASSERT_FALSE(got_peer
, "Should not have seen peer yet");
324 T_LOG("Received message on peer");
332 T_LOG("Do some CPU work.");
335 /* Check if the override now is IN + 60 boost */
336 T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED
,
337 "dispatch_source event handler QoS should be QOS_CLASS_USER_INITIATED");
338 T_EXPECT_EQ(get_user_promotion_basepri(), 60u,
339 "dispatch_source event handler should be overridden at 60");
341 T_EXPECT_EQ(get_thread_base_priority(), 60u,
342 "dispatch_source event handler should have base pri at 60");
344 if (*workloop_id
== LISTENER_WLID
) {
345 register_port(CONN_WLID
, tmsg
->port_descriptor
.name
);
347 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
348 kev
->fflags
= register_port_options();
349 kev
->ext
[0] = kev
->ext
[1] = kev
->ext
[2] = kev
->ext
[3] = 0;
352 /* this will unblock the waiter */
353 mach_msg_destroy(hdr
);
356 /* now that the message is destroyed, the priority should be gone */
357 T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_MAINTENANCE
,
358 "dispatch_source event handler QoS should be QOS_CLASS_MAINTENANCE after destroying message");
359 T_EXPECT_LE(get_user_promotion_basepri(), 0u,
360 "dispatch_source event handler should not be overridden after destroying message");
361 T_EXPECT_LE(get_thread_base_priority(), 4u,
362 "dispatch_source event handler should have base pri at 4 or less after destroying message");
367 run_client_server(const char *server_name
, const char *client_name
)
369 dt_helper_t helpers
[] = {
370 dt_launchd_helper_domain("com.apple.xnu.test.turnstile_multihop.plist",
371 server_name
, NULL
, LAUNCH_SYSTEM_DOMAIN
),
372 dt_fork_helper(client_name
)
374 dt_run_helpers(helpers
, 2, HELPER_TIMEOUT_SECS
);
377 #pragma mark Mach receive
379 #define TURNSTILE_MULTIHOP_SERVICE_NAME "com.apple.xnu.test.turnstile_multihop"
382 get_server_port(void)
385 kern_return_t kr
= bootstrap_check_in(bootstrap_port
,
386 TURNSTILE_MULTIHOP_SERVICE_NAME
, &port
);
387 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "server bootstrap_check_in");
391 static mach_voucher_t
392 create_pthpriority_voucher(mach_msg_priority_t qos
)
394 char voucher_buf
[sizeof(mach_voucher_attr_recipe_data_t
) + sizeof(ipc_pthread_priority_value_t
)];
396 mach_voucher_t voucher
= MACH_PORT_NULL
;
398 ipc_pthread_priority_value_t ipc_pthread_priority_value
=
399 (ipc_pthread_priority_value_t
)qos
;
401 mach_voucher_attr_raw_recipe_array_t recipes
;
402 mach_voucher_attr_raw_recipe_size_t recipe_size
= 0;
403 mach_voucher_attr_recipe_t recipe
=
404 (mach_voucher_attr_recipe_t
)&voucher_buf
[recipe_size
];
406 recipe
->key
= MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
;
407 recipe
->command
= MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE
;
408 recipe
->previous_voucher
= MACH_VOUCHER_NULL
;
409 memcpy((char *)&recipe
->content
[0], &ipc_pthread_priority_value
, sizeof(ipc_pthread_priority_value
));
410 recipe
->content_size
= sizeof(ipc_pthread_priority_value_t
);
411 recipe_size
+= sizeof(mach_voucher_attr_recipe_data_t
) + recipe
->content_size
;
413 recipes
= (mach_voucher_attr_raw_recipe_array_t
)&voucher_buf
[0];
415 ret
= host_create_mach_voucher(mach_host_self(),
420 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client host_create_mach_voucher");
426 mach_port_t send_port
,
427 mach_port_t reply_port
,
428 mach_port_t msg_port
,
429 mach_msg_priority_t qos
,
430 mach_msg_option_t options
)
432 kern_return_t ret
= 0;
434 struct test_msg send_msg
= {
436 .msgh_remote_port
= send_port
,
437 .msgh_local_port
= reply_port
,
438 .msgh_bits
= MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
,
439 reply_port
? MACH_MSG_TYPE_MAKE_SEND_ONCE
: 0,
440 MACH_MSG_TYPE_MOVE_SEND
,
441 MACH_MSGH_BITS_COMPLEX
),
442 .msgh_id
= EXPECTED_MESSAGE_ID
,
443 .msgh_size
= sizeof(send_msg
),
446 .msgh_descriptor_count
= 1,
450 .disposition
= MACH_MSG_TYPE_MOVE_RECEIVE
,
451 .type
= MACH_MSG_PORT_DESCRIPTOR
,
455 if (options
& MACH_SEND_SYNC_USE_THRPRI
) {
456 send_msg
.header
.msgh_voucher_port
= create_pthpriority_voucher(qos
);
459 if (msg_port
== MACH_PORT_NULL
) {
460 send_msg
.body
.msgh_descriptor_count
= 0;
463 ret
= mach_msg(&(send_msg
.header
),
467 (test_noimportance
? MACH_SEND_NOIMPORTANCE
: 0) |
468 ((reply_port
? MACH_SEND_SYNC_OVERRIDE
: 0) | options
),
469 send_msg
.header
.msgh_size
,
475 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client mach_msg");
480 mach_port_t rcv_port
,
481 mach_port_t notify_port
)
484 mach_msg_header_t header
;
485 mach_msg_body_t body
;
486 mach_msg_port_descriptor_t port_descriptor
;
490 .msgh_remote_port
= MACH_PORT_NULL
,
491 .msgh_local_port
= rcv_port
,
492 .msgh_size
= sizeof(rcv_msg
),
496 T_LOG("Client: Starting sync receive\n");
499 kr
= mach_msg(&(rcv_msg
.header
),
503 rcv_msg
.header
.msgh_size
,
508 T_ASSERT_MACH_SUCCESS(kr
, "mach_msg rcv");
510 return rcv_msg
.header
.msgh_id
;
513 static lock_t lock_DEF
;
514 static lock_t lock_IN
;
515 static lock_t lock_UI
;
517 static mach_port_t main_thread_port
;
518 static mach_port_t def_thread_port
;
519 static mach_port_t in_thread_port
;
520 static mach_port_t ui_thread_port
;
521 static mach_port_t sixty_thread_port
;
523 static uint64_t dispatch_sync_owner
;
526 get_pri(thread_t thread_port
)
530 thread_extended_info_data_t extended_info
;
531 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
532 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
533 (thread_info_t
)&extended_info
, &count
);
535 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
537 return extended_info
.pth_curpri
;
541 set_thread_name(const char *fn_name
)
545 thread_t thread_port
= pthread_mach_thread_np(pthread_self());
547 int pri
= get_pri(thread_port
);
549 snprintf(name
, sizeof(name
), "%s at pri %2d", fn_name
, pri
);
550 pthread_setname_np(name
);
554 thread_wait_to_block(mach_port_t thread_port
)
556 thread_extended_info_data_t extended_info
;
560 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
561 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
562 (thread_info_t
)&extended_info
, &count
);
564 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
566 if (extended_info
.pth_run_state
== TH_STATE_WAITING
) {
567 T_LOG("Target thread blocked\n");
570 thread_switch(thread_port
, SWITCH_OPTION_DEPRESS
, 0);
575 thread_wait_to_boost(mach_port_t thread_port
, mach_port_t yield_thread
, int priority
)
577 thread_extended_info_data_t extended_info
;
581 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
582 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
583 (thread_info_t
)&extended_info
, &count
);
585 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
587 if (extended_info
.pth_priority
>= priority
) {
588 T_LOG("Target thread boosted\n");
591 thread_switch(yield_thread
, SWITCH_OPTION_DEPRESS
, 0);
596 dispatch_sync_wait(mach_port_t owner_thread
, qos_class_t promote_qos
)
598 struct kevent_qos_s kev_err
[] = {{ 0 }};
604 action
= EV_ADD
| EV_DISABLE
;
605 fflags
= NOTE_WL_SYNC_WAIT
| NOTE_WL_DISCOVER_OWNER
;
607 dispatch_sync_owner
= owner_thread
;
609 struct kevent_qos_s kev
[] = {{
610 .ident
= mach_thread_self(),
611 .filter
= EVFILT_WORKLOOP
,
614 .udata
= (uintptr_t) &def_thread_port
,
615 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
616 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
617 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
618 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
621 /* Setup workloop to fake dispatch sync wait on a workloop */
622 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
623 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
624 T_QUIET
; T_LOG("dispatch_sync_wait returned\n");
628 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
)
630 struct kevent_qos_s kev_err
[] = {{ 0 }};
636 action
= EV_DELETE
| EV_ENABLE
;
637 fflags
= NOTE_WL_SYNC_WAKE
| NOTE_WL_END_OWNERSHIP
;
639 dispatch_sync_owner
= owner_thread
;
641 struct kevent_qos_s kev
[] = {{
642 .ident
= def_thread_port
,
643 .filter
= EVFILT_WORKLOOP
,
646 .udata
= (uintptr_t) &def_thread_port
,
647 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
648 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
649 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
650 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
653 /* Setup workloop to fake dispatch sync wake on a workloop */
654 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
655 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
656 T_QUIET
; T_LOG("dispatch_sync_cancel returned\n");
660 thread_at_sixty(void *arg __unused
)
663 struct sched_param param
;
666 uint64_t before_lock_time
, after_lock_time
;
668 sixty_thread_port
= mach_thread_self();
670 set_thread_name(__FUNCTION__
);
672 /* Change our priority to 60 */
673 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
674 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
676 param
.sched_priority
= 60;
678 ret
= pthread_setschedparam(pthread_self(), policy
, ¶m
);
679 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_setschedparam");
681 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
682 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
684 T_LOG("My priority is %d", param
.sched_priority
);
686 thread_wait_to_boost(in_thread_port
, ui_thread_port
, 46);
689 /* Schedule load at Default */
690 sched_create_load_at_qos(QOS_CLASS_DEFAULT
, &load_token
);
693 T_LOG("Thread at priority 60 trying to acquire UI lock");
695 before_lock_time
= mach_absolute_time();
696 ull_lock(&lock_UI
, 3, UL_UNFAIR_LOCK
, 0);
697 after_lock_time
= mach_absolute_time();
699 T_QUIET
; T_LOG("The time for priority 60 thread to acquire lock was %llu \n",
700 (after_lock_time
- before_lock_time
));
705 thread_at_ui(void *arg __unused
)
707 ui_thread_port
= mach_thread_self();
709 set_thread_name(__FUNCTION__
);
711 /* Grab the first ulock */
712 ull_lock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
714 thread_wait_to_boost(def_thread_port
, in_thread_port
, 37);
715 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_sixty
);
717 T_LOG("Thread at UI priority trying to acquire IN lock");
718 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
719 ull_unlock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
724 thread_at_in(void *arg __unused
)
726 in_thread_port
= mach_thread_self();
728 set_thread_name(__FUNCTION__
);
730 /* Grab the first ulock */
731 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
733 T_LOG("Thread at IN priority got first lock ");
735 thread_wait_to_boost(main_thread_port
, def_thread_port
, 31);
737 /* Create a new thread at QOS_CLASS_USER_INTERACTIVE qos */
738 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_ui
);
740 T_LOG("Thread at IN priority trying to acquire default lock");
741 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
742 ull_unlock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
747 thread_at_default(void *arg __unused
)
749 def_thread_port
= mach_thread_self();
751 set_thread_name(__FUNCTION__
);
753 /* Grab the first ulock */
754 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
756 T_LOG("Thread at DEFAULT priority got first lock ");
758 thread_wait_to_block(main_thread_port
);
760 /* Create a new thread at QOS_CLASS_USER_INITIATED qos */
761 thread_create_at_qos(QOS_CLASS_USER_INITIATED
, thread_at_in
);
763 T_LOG("Thread at Default priority trying to wait on dispatch sync for maintenance thread");
764 dispatch_sync_wait(main_thread_port
, QOS_CLASS_DEFAULT
);
765 ull_unlock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
770 thread_at_maintenance(void *arg __unused
)
772 mach_port_t service_port
;
773 mach_port_t conn_port
;
774 mach_port_t special_reply_port
;
775 mach_port_options_t opts
= {
776 .flags
= MPO_INSERT_SEND_RIGHT
,
779 main_thread_port
= mach_thread_self();
781 set_thread_name(__FUNCTION__
);
783 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
784 TURNSTILE_MULTIHOP_SERVICE_NAME
, &service_port
);
785 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
787 kr
= mach_port_construct(mach_task_self(), &opts
, 0ull, &conn_port
);
788 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "mach_port_construct");
790 special_reply_port
= thread_get_special_reply_port();
791 T_QUIET
; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port
), "get_thread_special_reply_port");
793 /* Become the dispatch sync owner, dispatch_sync_owner will be set in dispatch_sync_wait function */
795 /* Send a sync message */
796 send(conn_port
, special_reply_port
, MACH_PORT_NULL
,
797 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
799 /* Send an async checkin message */
800 send(service_port
, MACH_PORT_NULL
, conn_port
,
801 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
803 /* Create a new thread at QOS_CLASS_DEFAULT qos */
804 thread_create_at_qos(QOS_CLASS_DEFAULT
, thread_at_default
);
806 /* Block on Sync IPC */
807 mach_msg_id_t message_id
= receive(special_reply_port
, service_port
);
809 T_ASSERT_EQ(message_id
, MACH_NOTIFY_SEND_ONCE
, "got the expected send-once notification");
811 T_LOG("received reply");
813 dispatch_sync_cancel(def_thread_port
, QOS_CLASS_DEFAULT
);
817 T_HELPER_DECL(three_ulock_sync_ipc_hop
,
818 "Create chain of 4 threads with 3 ulocks and 1 sync IPC at different qos")
820 thread_create_at_qos(QOS_CLASS_MAINTENANCE
, thread_at_maintenance
);
824 T_HELPER_DECL(three_ulock_sync_ipc_hop_noimportance
,
825 "Create chain of 4 threads with 3 ulocks and 1 no-importance sync IPC at different qos")
827 test_noimportance
= true;
828 thread_create_at_qos(QOS_CLASS_MAINTENANCE
, thread_at_maintenance
);
834 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *))
836 qos_class_t qos_thread
;
841 ret
= setpriority(PRIO_DARWIN_ROLE
, 0, PRIO_DARWIN_ROLE_UI_FOCAL
);
843 T_LOG("set priority failed\n");
846 pthread_attr_init(&attr
);
847 pthread_attr_set_qos_class_np(&attr
, qos
, 0);
848 pthread_create(&thread
, &attr
, function
, NULL
);
850 T_LOG("pthread created\n");
851 pthread_get_qos_class_np(thread
, &qos_thread
, NULL
);
854 #pragma mark Mach receive - kevent_qos
856 T_HELPER_DECL(server_kevent_id
,
857 "Reply with the QoS that a dispatch source event handler ran with")
859 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
861 (pthread_workqueue_function_workloop_t
)workloop_cb_test_intransit
, 0, 0), NULL
);
863 register_port(LISTENER_WLID
, get_server_port());
865 T_ASSERT_FAIL("should receive a message");
868 #define TEST_MULTIHOP(server_name, client_name, name) \
869 T_DECL(server_kevent_id_##name, \
870 "Event delivery using a kevent_id", \
871 T_META_ASROOT(YES)) \
873 run_client_server(server_name, client_name); \
876 #define TEST_MULTIHOP_SPIN(server_name, client_name, name) \
877 T_DECL(server_kevent_id_##name, \
878 "Event delivery using a kevent_id", \
879 T_META_ASROOT(YES), T_META_ENABLED(FALSE)) \
881 spin_for_ever = true; \
882 run_client_server(server_name, client_name); \
883 spin_for_ever = false; \
887 * Test 1: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
889 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
890 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
892 TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop
)
894 TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop_noimportance", three_ulock_sync_ipc_hop_noimportance
)
897 * Test 2: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
899 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
900 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
901 * Before the last priority 60 thread blocks on ulock, it also starts spinforeverd at priority 31.
903 TEST_MULTIHOP_SPIN("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop_spin
)