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