2 * kevent_qos: Tests Synchronous IPC QOS override.
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 T_GLOBAL_META(T_META_NAMESPACE("xnu.kevent_qos"));
28 #define ARRAYLEN(arr) (sizeof(arr) / sizeof(arr[0]))
30 #define RECV_TIMEOUT_SECS (4)
31 #define SEND_TIMEOUT_SECS (6)
32 #define HELPER_TIMEOUT_SECS (15)
34 #define ENV_VAR_QOS (3)
35 static const char *qos_env
[ENV_VAR_QOS
] = {"XNU_TEST_QOS_BO", "XNU_TEST_QOS_QO", "XNU_TEST_QOS_AO"};
36 static const char *qos_name_env
[ENV_VAR_QOS
] = {"XNU_TEST_QOS_NAME_BO", "XNU_TEST_QOS_NAME_QO", "XNU_TEST_QOS_NAME_AO"};
38 #define ENV_VAR_FUNCTION (1)
39 static const char *wl_function_name
= "XNU_TEST_WL_FUNCTION";
41 static qos_class_t g_expected_qos
[ENV_VAR_QOS
];
42 static const char *g_expected_qos_name
[ENV_VAR_QOS
];
44 #define ENV_QOS_BEFORE_OVERRIDE (0)
45 #define ENV_QOS_QUEUE_OVERRIDE (1)
46 #define ENV_QOS_AFTER_OVERRIDE (2)
48 #pragma mark pthread callbacks
51 worker_cb(pthread_priority_t __unused priority
)
53 T_FAIL("a worker thread was created");
57 event_cb(void ** __unused events
, int * __unused nevents
)
59 T_FAIL("a kevent routine was called instead of workloop");
63 * Basic WL handler callback, it sleeps for n seconds and then checks the
64 * effective Qos of the servicer thread.
67 workloop_cb_test_intransit(uint64_t *workloop_id __unused
, void **eventslist __unused
, int *events
)
69 T_LOG("Workloop handler workloop_cb_test_intransit called. "
70 "Will wait for %d seconds to make sure client enqueues the sync msg \n",
71 2 * RECV_TIMEOUT_SECS
);
73 /* Wait for the client to send the high priority message to override the qos */
74 sleep(2 * RECV_TIMEOUT_SECS
);
76 /* Skip the test if we can't check Qos */
78 T_SKIP("kevent_qos test requires root privileges to run.");
81 /* The effective Qos should be the one expected after override */
82 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
83 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
90 * WL handler which checks if the servicer thread has correct Qos.
93 workloop_cb_test_sync_send(uint64_t *workloop_id __unused
, void **eventslist __unused
, int *events
)
95 T_LOG("Workloop handler workloop_cb_test_sync_send called");
98 T_SKIP("kevent_qos test requires root privileges to run.");
101 /* The effective Qos should be the one expected after override */
102 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
103 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
110 * WL handler which checks the overridden Qos and then enables the knote and checks
111 * for the Qos again if that dropped the sync ipc override.
114 workloop_cb_test_sync_send_and_enable(uint64_t *workloop_id
, struct kevent_qos_s
**eventslist
, int *events
)
117 T_LOG("Workloop handler workloop_cb_test_sync_send_and_enable called");
119 if (geteuid() != 0) {
120 T_SKIP("kevent_qos test requires root privileges to run.");
123 /* The effective Qos should be the one expected after override */
124 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
125 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
127 /* Enable the knote */
128 struct kevent_qos_s
*kev
= *eventslist
;
129 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
130 struct kevent_qos_s kev_err
[] = {{ 0 }};
132 r
= kevent_id(*workloop_id
, kev
, 1, kev_err
, 1, NULL
,
133 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
| KEVENT_FLAG_DYNAMIC_KQ_MUST_EXIST
);
134 T_QUIET
; T_ASSERT_POSIX_SUCCESS(r
, "kevent_id");
136 /* Sync override should have been removed */
137 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
],
138 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_BEFORE_OVERRIDE
]);
145 * WL handler receives the first message and checks sync ipc override, then enables the knote
146 * and receives 2nd message and checks it sync ipc override.
148 static int send_two_sync_handler_called
= 0;
150 workloop_cb_test_send_two_sync(uint64_t *workloop_id __unused
, struct kevent_qos_s
**eventslist
, int *events
)
152 T_LOG("Workloop handler workloop_cb_test_send_two_sync called for %d time", send_two_sync_handler_called
+ 1);
154 if (geteuid() != 0) {
155 T_SKIP("kevent_qos test requires root privileges to run.");
158 T_LOG("Number of events received is %d\n", *events
);
160 if (send_two_sync_handler_called
== 0) {
161 /* The effective Qos should be the one expected after override */
162 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
163 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
165 /* Enable the knote to get 2nd message */
166 struct kevent_qos_s
*kev
= *eventslist
;
167 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
168 kev
->fflags
= (MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
169 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
170 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
174 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
],
175 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_BEFORE_OVERRIDE
]);
179 send_two_sync_handler_called
++;
183 * Checks the sync ipc override and then waits for client to destroy the
184 * special reply port and checks if that removes the sync ipc override.
186 static boolean_t two_send_and_destroy_test_passed
= FALSE
;
187 static int two_send_and_destroy_handler
= 0;
189 workloop_cb_test_two_send_and_destroy(uint64_t *workloop_id __unused
, struct kevent_qos_s
**eventslist __unused
, int *events
)
191 T_LOG("Workloop handler workloop_cb_test_two_send_and_destroy called %d times", two_send_and_destroy_handler
+ 1);
193 if (geteuid() != 0) {
194 T_SKIP("kevent_qos test requires root privileges to run.");
197 if (two_send_and_destroy_handler
== 0) {
198 /* The effective Qos should be the one expected after override */
199 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
200 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
202 sleep(2 * RECV_TIMEOUT_SECS
);
204 /* Special reply port should have been destroyed, check Qos again */
205 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
],
206 "dispatch_source event handler QoS should be %s", g_expected_qos_name
[ENV_QOS_BEFORE_OVERRIDE
]);
208 two_send_and_destroy_test_passed
= TRUE
;
210 if (two_send_and_destroy_test_passed
) {
215 /* Enable the knote to get next message */
216 struct kevent_qos_s
*kev
= *eventslist
;
217 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
218 kev
->fflags
= (MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
219 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
220 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
223 two_send_and_destroy_handler
++;
224 T_LOG("Handler returning \n");
227 #pragma mark Mach receive
229 #define KEVENT_QOS_SERVICE_NAME "com.apple.xnu.test.kevent_qos"
232 get_server_port(void)
235 kern_return_t kr
= bootstrap_check_in(bootstrap_port
,
236 KEVENT_QOS_SERVICE_NAME
, &port
);
237 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "server bootstrap_check_in");
242 env_set_qos(char **env
, qos_class_t qos
[], const char *qos_name
[], const char *wl_function
)
245 char *qos_str
, *qos_name_str
;
246 for (i
= 0; i
< ENV_VAR_QOS
; i
++) {
247 T_QUIET
; T_ASSERT_POSIX_SUCCESS(asprintf(&qos_str
, "%s=%d", qos_env
[i
] , qos
[i
]),
249 T_QUIET
; T_ASSERT_POSIX_SUCCESS(
250 asprintf(&qos_name_str
, "%s=%s", qos_name_env
[i
], qos_name
[i
]), NULL
);
251 env
[2 * i
] = qos_str
;
252 env
[2 * i
+ 1] = qos_name_str
;
254 T_QUIET
; T_ASSERT_POSIX_SUCCESS(asprintf(&env
[2 * i
], "%s=%s", wl_function_name
, wl_function
),
256 env
[2 * i
+ 1] = NULL
;
260 environ_get_qos(qos_class_t qos
[], const char *qos_name
[], const char **wl_function
)
266 for (i
= 0; i
< ENV_VAR_QOS
; i
++) {
267 qos_str
= getenv(qos_env
[i
]);
268 T_QUIET
; T_ASSERT_NOTNULL(qos_str
, "getenv(%s)", qos_env
[i
]);
270 unsigned long qos_l
= strtoul(qos_str
, &qos_end
, 10);
271 T_QUIET
; T_ASSERT_EQ(*qos_end
, '\0', "getenv(%s) = '%s' should be an "
272 "integer", qos_env
[i
], qos_str
);
274 T_QUIET
; T_ASSERT_LT(qos_l
, (unsigned long)100, "getenv(%s) = '%s' should "
275 "be less than 100", qos_env
[i
], qos_str
);
277 qos
[i
] = (qos_class_t
)qos_l
;
278 qos_name
[i
] = getenv(qos_name_env
[i
]);
279 T_QUIET
; T_ASSERT_NOTNULL(qos_name
[i
], "getenv(%s)", qos_name_env
[i
]);
281 *wl_function
= getenv(wl_function_name
);
282 T_QUIET
; T_ASSERT_NOTNULL(*wl_function
, "getenv(%s)", wl_function_name
);
285 static mach_voucher_t
286 create_pthpriority_voucher(mach_msg_priority_t qos
)
288 char voucher_buf
[sizeof(mach_voucher_attr_recipe_data_t
) + sizeof(ipc_pthread_priority_value_t
)];
290 mach_voucher_t voucher
= MACH_PORT_NULL
;
292 ipc_pthread_priority_value_t ipc_pthread_priority_value
=
293 (ipc_pthread_priority_value_t
)qos
;
295 mach_voucher_attr_raw_recipe_array_t recipes
;
296 mach_voucher_attr_raw_recipe_size_t recipe_size
= 0;
297 mach_voucher_attr_recipe_t recipe
=
298 (mach_voucher_attr_recipe_t
)&voucher_buf
[recipe_size
];
300 recipe
->key
= MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
;
301 recipe
->command
= MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE
;
302 recipe
->previous_voucher
= MACH_VOUCHER_NULL
;
303 memcpy((char *)&recipe
->content
[0], &ipc_pthread_priority_value
, sizeof(ipc_pthread_priority_value
));
304 recipe
->content_size
= sizeof(ipc_pthread_priority_value_t
);
305 recipe_size
+= sizeof(mach_voucher_attr_recipe_data_t
) + recipe
->content_size
;
307 recipes
= (mach_voucher_attr_raw_recipe_array_t
)&voucher_buf
[0];
309 ret
= host_create_mach_voucher(mach_host_self(),
314 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client host_create_mach_voucher");
320 mach_port_t send_port
,
321 mach_port_t reply_port
,
322 mach_port_t msg_port
,
323 mach_msg_priority_t qos
)
325 kern_return_t ret
= 0;
328 mach_msg_header_t header
;
329 mach_msg_body_t body
;
330 mach_msg_port_descriptor_t port_descriptor
;
334 .msgh_remote_port
= send_port
,
335 .msgh_local_port
= reply_port
,
336 .msgh_bits
= MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
,
337 reply_port
? MACH_MSG_TYPE_MAKE_SEND_ONCE
: 0,
338 MACH_MSG_TYPE_MOVE_SEND
,
339 MACH_MSGH_BITS_COMPLEX
),
341 .msgh_size
= sizeof(send_msg
),
342 .msgh_voucher_port
= create_pthpriority_voucher(qos
),
346 .msgh_descriptor_count
= 1,
350 .name
= msg_port
, .disposition
= MACH_MSG_TYPE_MOVE_RECEIVE
, .type
= MACH_MSG_PORT_DESCRIPTOR
,
354 if (msg_port
== MACH_PORT_NULL
) {
355 send_msg
.body
.msgh_descriptor_count
= 0;
358 ret
= mach_msg(&(send_msg
.header
),
362 (reply_port
? MACH_SEND_SYNC_OVERRIDE
: 0) ,
363 send_msg
.header
.msgh_size
,
369 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client mach_msg");
374 mach_port_t rcv_port
,
375 mach_port_t notify_port
)
377 kern_return_t ret
= 0;
380 mach_msg_header_t header
;
381 mach_msg_body_t body
;
382 mach_msg_port_descriptor_t port_descriptor
;
386 .msgh_remote_port
= MACH_PORT_NULL
,
387 .msgh_local_port
= rcv_port
,
388 .msgh_size
= sizeof(rcv_msg
),
392 T_LOG("Client: Starting sync receive\n");
394 ret
= mach_msg(&(rcv_msg
.header
),
399 rcv_msg
.header
.msgh_size
,
401 SEND_TIMEOUT_SECS
* 1000,
404 if (!(ret
== MACH_RCV_TIMED_OUT
|| ret
== MACH_MSG_SUCCESS
)) {
405 T_ASSERT_FAIL("Sync rcv failed \n");
409 T_HELPER_DECL(qos_get_special_reply_port
,
410 "Test get_special_reply_port and it's corner cases.")
412 mach_port_t special_reply_port
;
413 mach_port_t new_special_reply_port
;
415 special_reply_port
= thread_get_special_reply_port();
416 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
418 new_special_reply_port
= thread_get_special_reply_port();
419 T_QUIET
; T_ASSERT_NOTNULL(new_special_reply_port
, "get_thread_special_reply_port");
421 mach_port_destroy(mach_task_self(), special_reply_port
);
422 mach_port_destroy(mach_task_self(), new_special_reply_port
);
424 new_special_reply_port
= thread_get_special_reply_port();
425 T_QUIET
; T_ASSERT_NOTNULL(new_special_reply_port
, "get_thread_special_reply_port");
430 T_HELPER_DECL(qos_client_send_to_intransit
,
431 "Send synchronous messages to an intransit port")
433 mach_port_t qos_send_port
;
434 mach_port_t msg_port
;
435 mach_port_t special_reply_port
;
437 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
438 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
439 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
441 special_reply_port
= thread_get_special_reply_port();
442 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
444 /* Create a rcv right to send in a msg */
445 kr
= mach_port_allocate(mach_task_self(),
446 MACH_PORT_RIGHT_RECEIVE
,
449 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client mach_port_allocate");
451 kr
= mach_port_insert_right(mach_task_self(),
454 MACH_MSG_TYPE_MAKE_SEND
);
456 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client mach_port_insert_right");
458 /* Send an empty msg on the port to fire the WL thread */
459 send(qos_send_port
, MACH_PORT_NULL
, MACH_PORT_NULL
,
460 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
], 0, 0));
462 sleep(SEND_TIMEOUT_SECS
);
464 /* Send the message with msg port as in-transit port, this msg will not be dequeued */
465 send(qos_send_port
, MACH_PORT_NULL
, msg_port
,
466 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
], 0, 0));
468 /* Send the message to the in-transit port, it should override the rcv's workloop */
469 send(msg_port
, special_reply_port
, MACH_PORT_NULL
,
470 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
471 T_LOG("Client done sending messages, now waiting for server to end the test");
472 sleep(2 * SEND_TIMEOUT_SECS
);
474 T_ASSERT_FAIL("client timed out");
477 T_HELPER_DECL(qos_client_send_sync_and_enqueue_rcv
,
478 "Send synchronous messages and enqueue the rcv right")
480 mach_port_t qos_send_port
;
481 mach_port_t msg_port
;
482 mach_port_t special_reply_port
;
484 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
485 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
486 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
488 special_reply_port
= thread_get_special_reply_port();
489 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
491 /* Create a rcv right to send in a msg */
492 kr
= mach_port_allocate(mach_task_self(),
493 MACH_PORT_RIGHT_RECEIVE
,
496 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client mach_port_allocate");
498 kr
= mach_port_insert_right(mach_task_self(),
501 MACH_MSG_TYPE_MAKE_SEND
);
503 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client mach_port_insert_right");
505 /* Send the message to msg port */
506 send(msg_port
, special_reply_port
, MACH_PORT_NULL
,
507 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
509 /* Send the message with msg port as in-transit port, copyin of in-transit will cause sync override */
510 send(qos_send_port
, MACH_PORT_NULL
, msg_port
,
511 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
], 0, 0));
513 T_LOG("Client done sending messages, now waiting for server to end the test");
514 sleep(3 * SEND_TIMEOUT_SECS
);
516 T_ASSERT_FAIL("client timed out");
520 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *))
522 qos_class_t qos_thread
;
527 ret
= setpriority(PRIO_DARWIN_ROLE
, 0, PRIO_DARWIN_ROLE_UI_FOCAL
);
529 T_LOG("set priority failed\n");
532 pthread_attr_init(&attr
);
533 pthread_attr_set_qos_class_np(&attr
, qos
, 0);
534 pthread_create(&thread
, &attr
, function
, NULL
);
536 T_LOG("pthread created\n");
537 pthread_get_qos_class_np(thread
, &qos_thread
, NULL
);
538 T_EXPECT_EQ(qos_thread
, (qos_class_t
)qos
, NULL
);
542 qos_send_and_sync_rcv(void *arg __unused
)
544 mach_port_t qos_send_port
;
545 mach_port_t special_reply_port
;
547 T_LOG("Client: from created thread\n");
549 T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
],
550 "pthread QoS should be %s", g_expected_qos_name
[ENV_QOS_AFTER_OVERRIDE
]);
552 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
553 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
554 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
556 special_reply_port
= thread_get_special_reply_port();
557 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
559 /* enqueue two messages to make sure that mqueue is not empty */
560 send(qos_send_port
, MACH_PORT_NULL
, MACH_PORT_NULL
,
561 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_QUEUE_OVERRIDE
], 0, 0));
563 send(qos_send_port
, MACH_PORT_NULL
, MACH_PORT_NULL
,
564 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_QUEUE_OVERRIDE
], 0, 0));
566 sleep(SEND_TIMEOUT_SECS
);
568 /* sync wait on msg port */
569 receive(special_reply_port
, qos_send_port
);
571 T_LOG("Client done doing sync rcv, now waiting for server to end the test");
572 sleep(SEND_TIMEOUT_SECS
);
574 T_ASSERT_FAIL("client timed out");
578 T_HELPER_DECL(qos_client_send_sync_and_sync_rcv
,
579 "Send messages and syncronously wait for rcv")
581 thread_create_at_qos(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], qos_send_and_sync_rcv
);
582 sleep(HELPER_TIMEOUT_SECS
);
585 T_HELPER_DECL(qos_client_send_sync_msg
,
586 "Send synchronous messages")
588 mach_port_t qos_send_port
;
589 mach_port_t special_reply_port
;
591 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
592 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
593 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
595 special_reply_port
= thread_get_special_reply_port();
596 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
598 /* Send the message to msg port */
599 send(qos_send_port
, special_reply_port
, MACH_PORT_NULL
,
600 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
602 T_LOG("Client done sending messages, now waiting for server to end the test");
603 sleep(2 * SEND_TIMEOUT_SECS
);
605 T_ASSERT_FAIL("client timed out");
608 T_HELPER_DECL(qos_client_send_two_sync_msg
,
609 "Send two synchronous messages at different qos")
611 mach_port_t qos_send_port
;
612 mach_port_t special_reply_port
;
614 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
615 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
616 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
618 special_reply_port
= thread_get_special_reply_port();
619 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
621 /* Send the message to msg port */
622 send(qos_send_port
, special_reply_port
, MACH_PORT_NULL
,
623 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
625 /* Send the message to msg port */
626 send(qos_send_port
, special_reply_port
, MACH_PORT_NULL
,
627 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_BEFORE_OVERRIDE
], 0, 0));
629 T_LOG("Client done sending messages, now waiting for server to end the test");
630 sleep(SEND_TIMEOUT_SECS
);
632 T_ASSERT_FAIL("client timed out");
635 T_HELPER_DECL(qos_client_send_two_msg_and_destroy
,
636 "Send two messages with 2nd one as sync and then destory the special reply port")
638 mach_port_t qos_send_port
;
639 mach_port_t special_reply_port
;
641 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
642 KEVENT_QOS_SERVICE_NAME
, &qos_send_port
);
643 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
645 special_reply_port
= thread_get_special_reply_port();
646 T_QUIET
; T_ASSERT_NOTNULL(special_reply_port
, "get_thread_special_reply_port");
648 /* Send an async message to msg port */
649 send(qos_send_port
, MACH_PORT_NULL
, MACH_PORT_NULL
,
650 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
652 /* Send the message to msg port */
653 send(qos_send_port
, special_reply_port
, MACH_PORT_NULL
,
654 (uint32_t)_pthread_qos_class_encode(g_expected_qos
[ENV_QOS_AFTER_OVERRIDE
], 0, 0));
656 T_LOG("Client done sending messages, waiting for destroy the special reply_port");
657 sleep(SEND_TIMEOUT_SECS
);
659 mach_port_destroy(mach_task_self(), special_reply_port
);
660 sleep(SEND_TIMEOUT_SECS
);
662 T_ASSERT_FAIL("client timed out");
666 run_client_server(const char *server_name
, const char *client_name
, qos_class_t qos
[],
667 const char *qos_name
[], const char *wl_function
)
669 char *env
[2 * ENV_VAR_QOS
+ ENV_VAR_FUNCTION
+ 1];
670 env_set_qos(env
, qos
, qos_name
, wl_function
);
672 for (int i
= 0; i
< ENV_VAR_QOS
; i
++) {
673 g_expected_qos
[i
] = qos
[i
];
674 g_expected_qos_name
[i
] = qos_name
[i
];
677 dt_helper_t helpers
[] = {
678 dt_launchd_helper_env("com.apple.xnu.test.kevent_qos.plist",
680 dt_fork_helper(client_name
)
682 dt_run_helpers(helpers
, 2, HELPER_TIMEOUT_SECS
);
685 #pragma mark Mach receive - kevent_qos
689 expect_kevent_id_recv(mach_port_t port
, qos_class_t qos
[], const char *qos_name
[], const char *wl_function
)
693 /* Qos expected by workloop thread */
694 for (int i
= 0; i
< ENV_VAR_QOS
; i
++) {
695 g_expected_qos
[i
] = qos
[i
];
696 g_expected_qos_name
[i
] = qos_name
[i
];
699 if (strcmp(wl_function
, "workloop_cb_test_intransit") == 0) {
700 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
702 (pthread_workqueue_function_workloop_t
)workloop_cb_test_intransit
, 0, 0), NULL
);
703 } else if (strcmp(wl_function
, "workloop_cb_test_sync_send") == 0) {
704 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
706 (pthread_workqueue_function_workloop_t
)workloop_cb_test_sync_send
, 0, 0), NULL
);
707 } else if (strcmp(wl_function
, "workloop_cb_test_sync_send_and_enable") == 0) {
708 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
710 (pthread_workqueue_function_workloop_t
)workloop_cb_test_sync_send_and_enable
, 0, 0), NULL
);
711 } else if (strcmp(wl_function
, "workloop_cb_test_send_two_sync") == 0) {
712 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
714 (pthread_workqueue_function_workloop_t
)workloop_cb_test_send_two_sync
, 0, 0), NULL
);
715 } else if (strcmp(wl_function
, "workloop_cb_test_two_send_and_destroy") == 0) {
716 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
718 (pthread_workqueue_function_workloop_t
)workloop_cb_test_two_send_and_destroy
, 0, 0), NULL
);
720 T_ASSERT_FAIL("no workloop function specified \n");
723 struct kevent_qos_s kev
[] = {{
725 .filter
= EVFILT_MACHPORT
,
726 .flags
= EV_ADD
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
,
727 .fflags
= (MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
728 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
729 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
732 .qos
= (int32_t)_pthread_qos_class_encode(qos
[ENV_QOS_QUEUE_OVERRIDE
], 0, 0)
735 struct kevent_qos_s kev_err
[] = {{ 0 }};
737 /* Setup workloop for mach msg rcv */
738 r
= kevent_id(25, kev
, 1, kev_err
, 1, NULL
,
739 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
741 T_QUIET
; T_ASSERT_POSIX_SUCCESS(r
, "kevent_id");
742 T_QUIET
; T_ASSERT_EQ(r
, 0, "no errors returned from kevent_id");
743 sleep(HELPER_TIMEOUT_SECS
);
746 T_HELPER_DECL(server_kevent_id
,
747 "Reply with the QoS that a dispatch source event handler ran with")
749 qos_class_t qos
[ENV_VAR_QOS
];
750 const char *qos_name
[ENV_VAR_QOS
];
751 const char *wl_function
;
752 environ_get_qos(qos
, qos_name
, &wl_function
);
754 expect_kevent_id_recv(get_server_port(), qos
, qos_name
, wl_function
);
755 sleep(HELPER_TIMEOUT_SECS
);
756 T_ASSERT_FAIL("should receive a message within %d seconds",
760 #define TEST_QOS(server_name, client_name, name, wl_function_name, qos_bo, qos_bo_name, qos_qo, qos_qo_name, qos_ao, qos_ao_name) \
761 T_DECL(server_kevent_id_##name, \
762 "Event delivery at " qos_ao_name " QoS using a kevent_id", \
763 T_META_ASROOT(YES)) \
765 qos_class_t qos_array[ENV_VAR_QOS] = {qos_bo, qos_qo, qos_ao}; \
766 const char *qos_name_array[ENV_VAR_QOS] = {qos_bo_name, qos_qo_name, qos_ao_name}; \
767 run_client_server(server_name, client_name, qos_array, qos_name_array, wl_function_name); \
771 * Test 1: Test special reply port SPI
773 * Create thread special reply port and check any subsequent calls to
774 * the same should return MACH_PORT_NULL, unless the reply port is destroyed.
776 TEST_QOS("server_kevent_id", "qos_get_special_reply_port", special_reply_port
, "workloop_cb_test_intransit",
777 QOS_CLASS_DEFAULT
, "default",
778 QOS_CLASS_DEFAULT
, "default",
779 QOS_CLASS_DEFAULT
, "default")
782 * Test 2: Test sync ipc send to an in-transit port
784 * Send a sync ipc message (at IN qos) to an in-transit port enqueued in a port
785 * attached to a workloop. Test that the servicer of the workloop gets
788 TEST_QOS("server_kevent_id", "qos_client_send_to_intransit", transit_IN
, "workloop_cb_test_intransit",
789 QOS_CLASS_DEFAULT
, "default",
790 QOS_CLASS_MAINTENANCE
, "maintenance",
791 QOS_CLASS_USER_INITIATED
, "user initiated")
794 * Test 3: Test sync ipc send to an in-transit port
796 * Send a sync ipc message (at UI qos) to an in-transit port enqueued in a port
797 * attached to a workloop. Test that the servicer of the workloop gets
800 TEST_QOS("server_kevent_id", "qos_client_send_to_intransit", transit_UI
, "workloop_cb_test_intransit",
801 QOS_CLASS_USER_INITIATED
, "user initiated",
802 QOS_CLASS_MAINTENANCE
, "maintenance",
803 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
806 * Test 4: Test enqueue of a receive right having sync ipc override
808 * Enqueue a receive right which has a sync ipc override (at IN qos)
809 * and test that servicer of the workloop on other side gets sync ipc
812 TEST_QOS("server_kevent_id", "qos_client_send_sync_and_enqueue_rcv", enqueue_IN
, "workloop_cb_test_intransit",
813 QOS_CLASS_DEFAULT
, "default",
814 QOS_CLASS_MAINTENANCE
, "maintenance",
815 QOS_CLASS_USER_INITIATED
, "user initiated")
818 * Test 5: Test enqueue of a receive right having sync ipc override
820 * Enqueue a receive right which has a sync ipc override (at UI qos)
821 * and test that servicer of the workloop on other side gets sync ipc
824 TEST_QOS("server_kevent_id", "qos_client_send_sync_and_enqueue_rcv", enqueue_UI
, "workloop_cb_test_intransit",
825 QOS_CLASS_DEFAULT
, "default",
826 QOS_CLASS_MAINTENANCE
, "maintenance",
827 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
830 * Test 6: Test starting a sync rcv overrides the servicer
832 * Send an async message to a port and then start waiting on
833 * the port in mach msg rcv (at IN qos) with sync wait and test if the
834 * servicer of the workloop gets sync ipc override.
836 TEST_QOS("server_kevent_id", "qos_client_send_sync_and_sync_rcv", rcv_IN
, "workloop_cb_test_intransit",
837 QOS_CLASS_DEFAULT
, "default",
838 QOS_CLASS_MAINTENANCE
, "maintenance",
839 QOS_CLASS_USER_INITIATED
, "user initiated")
842 * Test 7: Test starting a sync rcv overrides the servicer
844 * Send an async message to a port and then start waiting on
845 * the port in mach msg rcv (at UI qos) with sync wait and test if the
846 * servicer of the workloop gets sync ipc override.
848 TEST_QOS("server_kevent_id", "qos_client_send_sync_and_sync_rcv", rcv_UI
, "workloop_cb_test_intransit",
849 QOS_CLASS_DEFAULT
, "default",
850 QOS_CLASS_MAINTENANCE
, "maintenance",
851 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
854 * Test 8: test sending sync ipc message (at IN qos) to port will override the servicer
856 * Send a message with sync ipc override to a port and check if the servicer
857 * of the workloop on other side gets sync ipc override.
859 TEST_QOS("server_kevent_id", "qos_client_send_sync_msg", send_sync_IN
, "workloop_cb_test_sync_send",
860 QOS_CLASS_DEFAULT
, "default",
861 QOS_CLASS_MAINTENANCE
, "maintenance",
862 QOS_CLASS_USER_INITIATED
, "user initiated")
865 * Test 9: test sending sync ipc message (at UI qos) to port will override the servicer
867 * Send a message with sync ipc override to a port and check if the servicer
868 * of the workloop on other side gets sync ipc override.
870 TEST_QOS("server_kevent_id", "qos_client_send_sync_msg", send_sync_UI
, "workloop_cb_test_sync_send",
871 QOS_CLASS_USER_INITIATED
, "user initiated",
872 QOS_CLASS_MAINTENANCE
, "maintenance",
873 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
876 * Test 10: test enabling a knote in workloop handler will drop the sync ipc override of delivered message
878 * Send a sync ipc message to port and check the servicer of the workloop
879 * on other side gets sync ipc override and once the handler enables the knote,
880 * that sync ipc override is dropped.
882 TEST_QOS("server_kevent_id", "qos_client_send_sync_msg", send_sync_UI_and_enable
, "workloop_cb_test_sync_send_and_enable",
883 QOS_CLASS_USER_INITIATED
, "user initiated",
884 QOS_CLASS_MAINTENANCE
, "maintenance",
885 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
888 * Test 11: test returning to begin processing drops sync ipc override of delivered message
890 * Send a sync ipc message and check if enabling the knote clears the override of
891 * the delivered message, but should still have the override of an enqueued message.
893 TEST_QOS("server_kevent_id", "qos_client_send_two_sync_msg", send_two_sync_UI
, "workloop_cb_test_send_two_sync",
894 QOS_CLASS_USER_INITIATED
, "user initiated",
895 QOS_CLASS_MAINTENANCE
, "maintenance",
896 QOS_CLASS_USER_INTERACTIVE
, "user interactive")
899 * Test 12: test destorying the special reply port drops the override
901 * Send two async messages and a sync ipc message, the workloop handler
902 * should get a sync ipc override, now test if destroying the special
903 * reply port drops the sync ipc override on the servicer.
905 TEST_QOS("server_kevent_id", "qos_client_send_two_msg_and_destroy", send_two_UI_and_destroy
, "workloop_cb_test_two_send_and_destroy",
906 QOS_CLASS_USER_INITIATED
, "user initiated",
907 QOS_CLASS_MAINTENANCE
, "maintenance",
908 QOS_CLASS_USER_INTERACTIVE
, "user interactive")