1 #include <sys/sysctl.h>
2 #include <dispatch/dispatch.h>
3 #include <dispatch/private.h>
4 #include "darwintest_defaults.h"
6 T_DECL(wq_pool_limits
, "test overcommit limit")
8 dispatch_semaphore_t sema
= dispatch_semaphore_create(0);
9 dispatch_group_t g
= dispatch_group_create();
11 uint32_t wq_max_threads
, wq_max_constrained_threads
;
13 dispatch_block_t b
= ^{
14 dispatch_group_leave(g
);
15 dispatch_semaphore_wait(sema
, DISPATCH_TIME_FOREVER
);
18 size_t s
= sizeof(uint32_t);
19 sysctlbyname("kern.wq_max_threads", &wq_max_threads
, &s
, NULL
, 0);
20 sysctlbyname("kern.wq_max_constrained_threads", &wq_max_constrained_threads
,
23 for (uint32_t i
= 0; i
< wq_max_constrained_threads
; i
++) {
24 dispatch_group_enter(g
);
25 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT
, 0), b
);
28 t
= dispatch_time(DISPATCH_TIME_NOW
, 10 * NSEC_PER_SEC
);
29 T_ASSERT_EQ(dispatch_group_wait(g
, t
), 0L,
30 "%d constrained threads bringup", wq_max_constrained_threads
);
32 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT
, 0), ^{
33 T_ASSERT_FAIL("Should never run");
37 T_PASS("constrained limit looks fine");
39 for (uint32_t i
= wq_max_constrained_threads
; i
< wq_max_threads
; i
++) {
40 dispatch_group_enter(g
);
41 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT
,
42 DISPATCH_QUEUE_OVERCOMMIT
), b
);
44 t
= dispatch_time(DISPATCH_TIME_NOW
, 10 * NSEC_PER_SEC
);
45 T_ASSERT_EQ(dispatch_group_wait(g
, t
), 0L,
46 "%d threads bringup", wq_max_threads
);
49 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT
,
50 DISPATCH_QUEUE_OVERCOMMIT
), ^{
51 T_ASSERT_FAIL("Should never run");
55 T_PASS("thread limit looks fine");