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)
37 static boolean_t spin_for_ever
= false;
40 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *));
42 nanoseconds_to_absolutetime(uint64_t nanoseconds
);
44 sched_create_load_at_qos(qos_class_t qos
, void **load_token
);
46 sched_terminate_load(void *load_token
) __unused
;
47 static void do_work(int num
);
49 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
);
51 static void *sched_load_thread(void *);
53 struct load_token_context
{
54 volatile int threads_should_exit
;
60 static struct mach_timebase_info sched_mti
;
61 static pthread_once_t sched_mti_once_control
= PTHREAD_ONCE_INIT
;
66 mach_timebase_info(&sched_mti
);
69 nanoseconds_to_absolutetime(uint64_t nanoseconds
)
71 pthread_once(&sched_mti_once_control
, sched_mti_init
);
73 return (uint64_t)(nanoseconds
* (((double)sched_mti
.denom
) / ((double)sched_mti
.numer
)));
77 sched_create_load_at_qos(qos_class_t qos
, void **load_token
)
79 struct load_token_context
*context
= NULL
;
82 size_t ncpu_size
= sizeof(ncpu
);
87 ret
= sysctlbyname("hw.ncpu", &ncpu
, &ncpu_size
, NULL
, 0);
89 T_LOG("sysctlbyname(hw.ncpu)");
93 T_QUIET
; T_LOG("%s: Detected %d CPUs\n", __FUNCTION__
, ncpu
);
96 T_QUIET
; T_LOG("%s: Will create %d threads\n", __FUNCTION__
, nthreads
);
98 ret
= pthread_attr_init(&attr
);
99 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_init");
101 if (&pthread_attr_set_qos_class_np
) {
102 ret
= pthread_attr_set_qos_class_np(&attr
, qos
, 0);
103 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_set_qos_class_np");
106 context
= calloc(1, sizeof(*context
));
107 if (context
== NULL
) {
108 T_QUIET
; T_LOG("calloc returned error"); return ENOMEM
;
111 context
->threads_should_exit
= 0;
112 context
->thread_count
= nthreads
;
114 context
->threads
= calloc((unsigned int)nthreads
, sizeof(pthread_t
));
118 for (i
= 0; i
< nthreads
; i
++) {
119 ret
= pthread_create(&context
->threads
[i
], &attr
, sched_load_thread
, context
);
120 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_create");
121 T_QUIET
; T_LOG("%s: Created thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
124 ret
= pthread_attr_destroy(&attr
);
125 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_destroy");
127 *load_token
= context
;
133 sched_load_thread(void *arg
)
135 struct load_token_context
*context
= (struct load_token_context
*)arg
;
137 T_QUIET
; T_LOG("%s: Thread started %p\n", __FUNCTION__
, (void *)pthread_self());
139 while (!context
->threads_should_exit
) {
140 uint64_t start
= mach_absolute_time();
141 uint64_t end
= start
+ nanoseconds_to_absolutetime(900ULL * NSEC_PER_MSEC
);
143 while ((mach_absolute_time() < end
) && !context
->threads_should_exit
) {
148 T_QUIET
; T_LOG("%s: Thread terminating %p\n", __FUNCTION__
, (void *)pthread_self());
154 sched_terminate_load(void *load_token
)
158 struct load_token_context
*context
= (struct load_token_context
*)load_token
;
160 context
->threads_should_exit
= 1;
163 for (i
= 0; i
< context
->thread_count
; i
++) {
164 T_QUIET
; T_LOG("%s: Joining thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
165 ret
= pthread_join(context
->threads
[i
], NULL
);
166 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_join");
169 free(context
->threads
);
176 // Find the first num primes, simply as a means of doing work
180 volatile int i
= 3, count
, c
;
182 for (count
= 2; count
<= num
;) {
183 for (c
= 2; c
<= i
; c
++) {
195 #pragma mark pthread callbacks
198 worker_cb(pthread_priority_t __unused priority
)
200 T_FAIL("a worker thread was created");
204 event_cb(void ** __unused events
, int * __unused nevents
)
206 T_FAIL("a kevent routine was called instead of workloop");
210 get_user_promotion_basepri(void)
212 mach_msg_type_number_t count
= THREAD_POLICY_STATE_COUNT
;
213 struct thread_policy_state thread_policy
;
214 boolean_t get_default
= FALSE
;
215 mach_port_t thread_port
= pthread_mach_thread_np(pthread_self());
217 kern_return_t kr
= thread_policy_get(thread_port
, THREAD_POLICY_STATE
,
218 (thread_policy_t
)&thread_policy
, &count
, &get_default
);
219 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_policy_get");
220 return thread_policy
.thps_user_promotion_basepri
;
223 static int messages_received
= 0;
225 * Basic WL handler callback, it checks the
226 * effective Qos of the servicer thread.
229 workloop_cb_test_intransit(uint64_t *workloop_id __unused
, void **eventslist __unused
, int *events
)
232 T_LOG("Workloop handler workloop_cb_test_intransit called. Received message no %d",
236 /* Skip the test if we can't check Qos */
237 if (geteuid() != 0) {
238 T_SKIP("kevent_qos test requires root privileges to run.");
241 if (messages_received
== 1) {
243 T_LOG("Do some CPU work.");
246 /* Check if the override now is IN + 60 boost */
247 T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED
,
248 "dispatch_source event handler QoS should be QOS_CLASS_USER_INITIATED");
249 T_EXPECT_EQ(get_user_promotion_basepri(), 60u,
250 "dispatch_source event handler should be overridden at 60");
252 /* Enable the knote to get 2nd message */
253 struct kevent_qos_s
*kev
= *eventslist
;
254 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
255 kev
->fflags
= (MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
256 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
257 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
267 run_client_server(const char *server_name
, const char *client_name
)
269 dt_helper_t helpers
[] = {
270 dt_launchd_helper_domain("com.apple.xnu.test.turnstile_multihop.plist",
271 server_name
, NULL
, LAUNCH_SYSTEM_DOMAIN
),
272 dt_fork_helper(client_name
)
274 dt_run_helpers(helpers
, 2, HELPER_TIMEOUT_SECS
);
277 #pragma mark Mach receive
279 #define TURNSTILE_MULTIHOP_SERVICE_NAME "com.apple.xnu.test.turnstile_multihop"
282 get_server_port(void)
285 kern_return_t kr
= bootstrap_check_in(bootstrap_port
,
286 TURNSTILE_MULTIHOP_SERVICE_NAME
, &port
);
287 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "server bootstrap_check_in");
291 static mach_voucher_t
292 create_pthpriority_voucher(mach_msg_priority_t qos
)
294 char voucher_buf
[sizeof(mach_voucher_attr_recipe_data_t
) + sizeof(ipc_pthread_priority_value_t
)];
296 mach_voucher_t voucher
= MACH_PORT_NULL
;
298 ipc_pthread_priority_value_t ipc_pthread_priority_value
=
299 (ipc_pthread_priority_value_t
)qos
;
301 mach_voucher_attr_raw_recipe_array_t recipes
;
302 mach_voucher_attr_raw_recipe_size_t recipe_size
= 0;
303 mach_voucher_attr_recipe_t recipe
=
304 (mach_voucher_attr_recipe_t
)&voucher_buf
[recipe_size
];
306 recipe
->key
= MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
;
307 recipe
->command
= MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE
;
308 recipe
->previous_voucher
= MACH_VOUCHER_NULL
;
309 memcpy((char *)&recipe
->content
[0], &ipc_pthread_priority_value
, sizeof(ipc_pthread_priority_value
));
310 recipe
->content_size
= sizeof(ipc_pthread_priority_value_t
);
311 recipe_size
+= sizeof(mach_voucher_attr_recipe_data_t
) + recipe
->content_size
;
313 recipes
= (mach_voucher_attr_raw_recipe_array_t
)&voucher_buf
[0];
315 ret
= host_create_mach_voucher(mach_host_self(),
320 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client host_create_mach_voucher");
326 mach_port_t send_port
,
327 mach_port_t reply_port
,
328 mach_port_t msg_port
,
329 mach_msg_priority_t qos
,
330 mach_msg_option_t options
)
332 kern_return_t ret
= 0;
335 mach_msg_header_t header
;
336 mach_msg_body_t body
;
337 mach_msg_port_descriptor_t port_descriptor
;
340 .msgh_remote_port
= send_port
,
341 .msgh_local_port
= reply_port
,
342 .msgh_bits
= MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
,
343 reply_port
? MACH_MSG_TYPE_MAKE_SEND_ONCE
: 0,
344 MACH_MSG_TYPE_MOVE_SEND
,
345 MACH_MSGH_BITS_COMPLEX
),
347 .msgh_size
= sizeof(send_msg
),
350 .msgh_descriptor_count
= 1,
354 .disposition
= MACH_MSG_TYPE_MOVE_RECEIVE
,
355 .type
= MACH_MSG_PORT_DESCRIPTOR
,
359 if (options
& MACH_SEND_SYNC_USE_THRPRI
) {
360 send_msg
.header
.msgh_voucher_port
= create_pthpriority_voucher(qos
);
363 if (msg_port
== MACH_PORT_NULL
) {
364 send_msg
.body
.msgh_descriptor_count
= 0;
367 ret
= mach_msg(&(send_msg
.header
),
371 ((reply_port
? MACH_SEND_SYNC_OVERRIDE
: 0) | options
),
372 send_msg
.header
.msgh_size
,
378 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client mach_msg");
383 mach_port_t rcv_port
,
384 mach_port_t notify_port
)
386 kern_return_t ret
= 0;
389 mach_msg_header_t header
;
390 mach_msg_body_t body
;
391 mach_msg_port_descriptor_t port_descriptor
;
395 .msgh_remote_port
= MACH_PORT_NULL
,
396 .msgh_local_port
= rcv_port
,
397 .msgh_size
= sizeof(rcv_msg
),
401 T_LOG("Client: Starting sync receive\n");
403 ret
= mach_msg(&(rcv_msg
.header
),
407 rcv_msg
.header
.msgh_size
,
413 static lock_t lock_DEF
;
414 static lock_t lock_IN
;
415 static lock_t lock_UI
;
417 static mach_port_t main_thread_port
;
418 static mach_port_t def_thread_port
;
419 static mach_port_t in_thread_port
;
420 static mach_port_t ui_thread_port
;
421 static mach_port_t sixty_thread_port
;
423 static uint64_t dispatch_sync_owner
;
426 get_pri(thread_t thread_port
)
430 thread_extended_info_data_t extended_info
;
431 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
432 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
433 (thread_info_t
)&extended_info
, &count
);
435 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
437 return extended_info
.pth_curpri
;
441 set_thread_name(const char *fn_name
)
445 thread_t thread_port
= pthread_mach_thread_np(pthread_self());
447 int pri
= get_pri(thread_port
);
449 snprintf(name
, sizeof(name
), "%s at pri %2d", fn_name
, pri
);
450 pthread_setname_np(name
);
454 thread_wait_to_block(mach_port_t thread_port
)
456 thread_extended_info_data_t extended_info
;
460 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
461 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
462 (thread_info_t
)&extended_info
, &count
);
464 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
466 if (extended_info
.pth_run_state
== TH_STATE_WAITING
) {
467 T_LOG("Target thread blocked\n");
470 thread_switch(thread_port
, SWITCH_OPTION_DEPRESS
, 0);
475 thread_wait_to_boost(mach_port_t thread_port
, mach_port_t yield_thread
, int priority
)
477 thread_extended_info_data_t extended_info
;
481 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
482 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
483 (thread_info_t
)&extended_info
, &count
);
485 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
487 if (extended_info
.pth_priority
>= priority
) {
488 T_LOG("Target thread boosted\n");
491 thread_switch(yield_thread
, SWITCH_OPTION_DEPRESS
, 0);
496 dispatch_sync_wait(mach_port_t owner_thread
, qos_class_t promote_qos
)
498 struct kevent_qos_s kev_err
[] = {{ 0 }};
504 action
= EV_ADD
| EV_DISABLE
;
505 fflags
= NOTE_WL_SYNC_WAIT
| NOTE_WL_DISCOVER_OWNER
;
507 dispatch_sync_owner
= owner_thread
;
509 struct kevent_qos_s kev
[] = {{
510 .ident
= mach_thread_self(),
511 .filter
= EVFILT_WORKLOOP
,
514 .udata
= (uintptr_t) &def_thread_port
,
515 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
516 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
517 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
518 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
521 /* Setup workloop to fake dispatch sync wait on a workloop */
522 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
523 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
524 T_QUIET
; T_LOG("dispatch_sync_wait returned\n");
528 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
)
530 struct kevent_qos_s kev_err
[] = {{ 0 }};
536 action
= EV_DELETE
| EV_ENABLE
;
537 fflags
= NOTE_WL_SYNC_WAKE
| NOTE_WL_END_OWNERSHIP
;
539 dispatch_sync_owner
= owner_thread
;
541 struct kevent_qos_s kev
[] = {{
542 .ident
= def_thread_port
,
543 .filter
= EVFILT_WORKLOOP
,
546 .udata
= (uintptr_t) &def_thread_port
,
547 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
548 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
549 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
550 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
553 /* Setup workloop to fake dispatch sync wake on a workloop */
554 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
555 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
556 T_QUIET
; T_LOG("dispatch_sync_cancel returned\n");
560 thread_at_sixty(void *arg __unused
)
563 struct sched_param param
;
566 uint64_t before_lock_time
, after_lock_time
;
568 sixty_thread_port
= mach_thread_self();
570 set_thread_name(__FUNCTION__
);
572 /* Change our priority to 60 */
573 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
574 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
576 param
.sched_priority
= 60;
578 ret
= pthread_setschedparam(pthread_self(), policy
, ¶m
);
579 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_setschedparam");
581 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
582 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
584 T_LOG("My priority is %d", param
.sched_priority
);
586 thread_wait_to_boost(in_thread_port
, ui_thread_port
, 46);
589 /* Schedule load at Default */
590 sched_create_load_at_qos(QOS_CLASS_DEFAULT
, &load_token
);
593 T_LOG("Thread at priority 60 trying to acquire UI lock");
595 before_lock_time
= mach_absolute_time();
596 ull_lock(&lock_UI
, 3, UL_UNFAIR_LOCK
, 0);
597 after_lock_time
= mach_absolute_time();
599 T_QUIET
; T_LOG("The time for priority 60 thread to acquire lock was %llu \n",
600 (after_lock_time
- before_lock_time
));
605 thread_at_ui(void *arg __unused
)
607 ui_thread_port
= mach_thread_self();
609 set_thread_name(__FUNCTION__
);
611 /* Grab the first ulock */
612 ull_lock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
614 thread_wait_to_boost(def_thread_port
, in_thread_port
, 37);
615 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_sixty
);
617 T_LOG("Thread at UI priority trying to acquire IN lock");
618 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
619 ull_unlock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
624 thread_at_in(void *arg __unused
)
626 in_thread_port
= mach_thread_self();
628 set_thread_name(__FUNCTION__
);
630 /* Grab the first ulock */
631 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
633 T_LOG("Thread at IN priority got first lock ");
635 thread_wait_to_boost(main_thread_port
, def_thread_port
, 31);
637 /* Create a new thread at QOS_CLASS_USER_INTERACTIVE qos */
638 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_ui
);
640 T_LOG("Thread at IN priority trying to acquire default lock");
641 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
642 ull_unlock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
647 thread_at_default(void *arg __unused
)
649 def_thread_port
= mach_thread_self();
651 set_thread_name(__FUNCTION__
);
653 /* Grab the first ulock */
654 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
656 T_LOG("Thread at DEFAULT priority got first lock ");
658 thread_wait_to_block(main_thread_port
);
660 /* Create a new thread at QOS_CLASS_USER_INITIATED qos */
661 thread_create_at_qos(QOS_CLASS_USER_INITIATED
, thread_at_in
);
663 T_LOG("Thread at Default priority trying to wait on dispatch sync for maintenance thread");
664 dispatch_sync_wait(main_thread_port
, QOS_CLASS_DEFAULT
);
665 ull_unlock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
670 thread_at_maintenance(void *arg __unused
)
672 mach_port_t qos_send_port
;
673 mach_port_t special_reply_port
;
675 main_thread_port
= mach_thread_self();
677 set_thread_name(__FUNCTION__
);
679 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
680 TURNSTILE_MULTIHOP_SERVICE_NAME
, &qos_send_port
);
681 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
683 special_reply_port
= thread_get_special_reply_port();
684 T_QUIET
; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port
), "get_thread_special_reply_port");
686 /* Become the dispatch sync owner, dispatch_sync_owner will be set in dispatch_sync_wait function */
688 /* Send an async message */
689 send(qos_send_port
, MACH_PORT_NULL
, MACH_PORT_NULL
,
690 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
692 /* Send a sync message */
693 send(qos_send_port
, special_reply_port
, MACH_PORT_NULL
,
694 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
696 /* Create a new thread at QOS_CLASS_DEFAULT qos */
697 thread_create_at_qos(QOS_CLASS_DEFAULT
, thread_at_default
);
699 /* Block on Sync IPC */
700 receive(special_reply_port
, qos_send_port
);
702 dispatch_sync_cancel(def_thread_port
, QOS_CLASS_DEFAULT
);
706 T_HELPER_DECL(three_ulock_sync_ipc_hop
,
707 "Create chain of 4 threads with 3 ulocks and 1 sync IPC at different qos")
709 dt_stat_time_t roundtrip_stat
= dt_stat_time_create("multihop_lock_acquire");
711 T_STAT_MEASURE_LOOP(roundtrip_stat
) {
713 thread_create_at_qos(QOS_CLASS_MAINTENANCE
, thread_at_maintenance
);
720 dt_stat_finalize(roundtrip_stat
);
725 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *))
727 qos_class_t qos_thread
;
732 ret
= setpriority(PRIO_DARWIN_ROLE
, 0, PRIO_DARWIN_ROLE_UI_FOCAL
);
734 T_LOG("set priority failed\n");
737 pthread_attr_init(&attr
);
738 pthread_attr_set_qos_class_np(&attr
, qos
, 0);
739 pthread_create(&thread
, &attr
, function
, NULL
);
741 T_LOG("pthread created\n");
742 pthread_get_qos_class_np(thread
, &qos_thread
, NULL
);
745 #pragma mark Mach receive - kevent_qos
748 expect_kevent_id_recv(mach_port_t port
)
752 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
754 (pthread_workqueue_function_workloop_t
)workloop_cb_test_intransit
, 0, 0), NULL
);
756 struct kevent_qos_s kev
[] = {{
758 .filter
= EVFILT_MACHPORT
,
759 .flags
= EV_ADD
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
,
760 .fflags
= (MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
761 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
762 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
765 .qos
= (int32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0)
768 struct kevent_qos_s kev_err
[] = {{ 0 }};
770 /* Setup workloop for mach msg rcv */
771 r
= kevent_id(25, kev
, 1, kev_err
, 1, NULL
,
772 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
774 T_QUIET
; T_ASSERT_POSIX_SUCCESS(r
, "kevent_id");
775 T_QUIET
; T_ASSERT_EQ(r
, 0, "no errors returned from kevent_id");
778 T_HELPER_DECL(server_kevent_id
,
779 "Reply with the QoS that a dispatch source event handler ran with")
781 expect_kevent_id_recv(get_server_port());
783 T_ASSERT_FAIL("should receive a message");
786 #define TEST_MULTIHOP(server_name, client_name, name) \
787 T_DECL(server_kevent_id_##name, \
788 "Event delivery using a kevent_id", \
789 T_META_ASROOT(YES)) \
791 run_client_server(server_name, client_name); \
794 #define TEST_MULTIHOP_SPIN(server_name, client_name, name) \
795 T_DECL(server_kevent_id_##name, \
796 "Event delivery using a kevent_id", \
797 T_META_ASROOT(YES), T_META_ENABLED(FALSE)) \
799 spin_for_ever = true; \
800 run_client_server(server_name, client_name); \
801 spin_for_ever = false; \
805 * Test 1: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
807 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
808 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
810 TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop
)
813 * Test 2: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
815 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
816 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
817 * Before the last priority 60 thread blocks on ulock, it also starts spinforeverd at priority 31.
819 TEST_MULTIHOP_SPIN("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop_spin
)