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