1 #include <pthread.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/time.h>
7 #include <mach/mach_time.h>
8 #include <mach/thread_policy.h>
9 #include <pthread.h>
10
11 #include <pthread/tsd_private.h>
12 #include <pthread/qos_private.h>
13
14 #include <dispatch/dispatch.h>
15 #include <dispatch/private.h>
16 #include <darwintest.h>
17 #include <pthread/workqueue_private.h>
18
19 T_GLOBAL_META(
20 T_META_NAMESPACE("xnu.workq"),
21 T_META_RADAR_COMPONENT_NAME("xnu"),
22 T_META_RADAR_COMPONENT_VERSION("workq"),
23 T_META_RUN_CONCURRENTLY(true));
24
25 static mach_timebase_info_data_t timebase_info;
26
27 static uint64_t
nanos_to_abs(uint64_t nanos)28 nanos_to_abs(uint64_t nanos)
29 {
30 return nanos * timebase_info.denom / timebase_info.numer;
31 }
32
33 static void
spin_for_duration(uint32_t seconds)34 spin_for_duration(uint32_t seconds)
35 {
36 kern_return_t kr = mach_timebase_info(&timebase_info);
37 assert(kr == KERN_SUCCESS);
38
39 uint64_t duration = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC);
40 uint64_t current_time = mach_absolute_time();
41 uint64_t timeout = duration + current_time;
42
43 uint64_t spin_count = 0;
44
45 while (mach_absolute_time() < timeout) {
46 spin_count++;
47 }
48 return;
49 }
50
51 T_DECL(cooperative_workqueue_and_vfork, "rdar://74489806", T_META_TAG_VM_PREFERRED) {
52 dispatch_queue_t dq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
53 T_ASSERT_NE(dq, NULL, "global_queue");
54
55 dispatch_async(dq, ^{
56 /* We are a workqueue non-overcommit thread, we should be getting a
57 * quantum */
58 spin_for_duration(1);
59
60 pid_t child;
61 if ((child = vfork()) == 0) {
62 usleep(100);
63 spin_for_duration(1);
64 _exit(0);
65 }
66
67 int status;
68 waitpid(child, &status, 0);
69 T_ASSERT_EQ(status, 0, "child status");
70
71 T_END;
72 });
73
74 dispatch_main();
75 }
76
77 T_DECL(adjust_quantum_nonovercommit_to_overcommit_switch, "rdar://75084197", T_META_TAG_VM_PREFERRED)
78 {
79 dispatch_queue_t dq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
80 T_ASSERT_NE(dq, NULL, "global_queue");
81
82 dispatch_async(dq, ^{
83 /* We are a workqueue non-overcommit thread, we should be getting a
84 * quantum that we expire here */
85 spin_for_duration(1);
86
87 /* Should not panic when we switch to overcommit */
88 pthread_priority_t overcommit = (pthread_priority_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS) |
89 _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
90 T_ASSERT_POSIX_ZERO(_pthread_set_properties_self(_PTHREAD_SET_SELF_QOS_FLAG, overcommit, 0), NULL);
91
92 T_END;
93 });
94
95 dispatch_main();
96 }
97
98 T_DECL(cooperative_to_overcommit_switch, "Switching from cooperative queue to another type should not panic", T_META_TAG_VM_PREFERRED)
99 {
100 dispatch_queue_t cooperative_dq = dispatch_get_global_queue(QOS_CLASS_UTILITY, DISPATCH_QUEUE_COOPERATIVE);
101 T_ASSERT_NE(cooperative_dq, NULL, "global_queue");
102
103 dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL, QOS_CLASS_USER_INITIATED, 0);
104 dispatch_queue_t dq = dispatch_queue_create("serial IN overcommit queue", attr);
105
106 dispatch_async(cooperative_dq, ^{
107 spin_for_duration(1);
108
109 dispatch_async_and_wait(dq, ^{
110 spin_for_duration(1);
111 });
112
113 dispatch_release(dq);
114 T_END;
115 });
116
117 dispatch_main();
118 }
119
120 T_DECL(maintenance_bg_coalesced, "BG and MT coalescing should work", T_META_TAG_VM_PREFERRED)
121 {
122 dispatch_queue_t dq = dispatch_get_global_queue(QOS_CLASS_MAINTENANCE, DISPATCH_QUEUE_COOPERATIVE);
123 T_ASSERT_NE(dq, NULL, "global_queue");
124 dispatch_group_t dg = dispatch_group_create();
125
126 dispatch_group_async(dg, dq, ^{
127 spin_for_duration(1);
128 });
129
130 dispatch_group_wait(dg, DISPATCH_TIME_FOREVER);
131 dispatch_release(dg);
132 }
133