1*27b03b36SApple OSS Distributions /* test that the header doesn't implicitly depend on others */
2*27b03b36SApple OSS Distributions #include <sys/work_interval.h>
3*27b03b36SApple OSS Distributions
4*27b03b36SApple OSS Distributions #include <stdlib.h>
5*27b03b36SApple OSS Distributions #include <stdio.h>
6*27b03b36SApple OSS Distributions #include <unistd.h>
7*27b03b36SApple OSS Distributions #include <errno.h>
8*27b03b36SApple OSS Distributions #include <err.h>
9*27b03b36SApple OSS Distributions #include <string.h>
10*27b03b36SApple OSS Distributions #include <pthread.h>
11*27b03b36SApple OSS Distributions
12*27b03b36SApple OSS Distributions #include <mach/mach.h>
13*27b03b36SApple OSS Distributions
14*27b03b36SApple OSS Distributions #include <darwintest.h>
15*27b03b36SApple OSS Distributions
16*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
17*27b03b36SApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
18*27b03b36SApple OSS Distributions
19*27b03b36SApple OSS Distributions static mach_port_t port = MACH_PORT_NULL;
20*27b03b36SApple OSS Distributions
21*27b03b36SApple OSS Distributions static void *
joining_thread_fn(__unused void * arg)22*27b03b36SApple OSS Distributions joining_thread_fn(__unused void *arg)
23*27b03b36SApple OSS Distributions {
24*27b03b36SApple OSS Distributions int ret = 0;
25*27b03b36SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
26*27b03b36SApple OSS Distributions
27*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
28*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, another thread");
29*27b03b36SApple OSS Distributions
30*27b03b36SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
31*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port, another thread");
32*27b03b36SApple OSS Distributions
33*27b03b36SApple OSS Distributions /* deliberately exit with joined work interval */
34*27b03b36SApple OSS Distributions return NULL;
35*27b03b36SApple OSS Distributions }
36*27b03b36SApple OSS Distributions
37*27b03b36SApple OSS Distributions T_DECL(work_interval, "work interval interface")
38*27b03b36SApple OSS Distributions {
39*27b03b36SApple OSS Distributions int ret = 0;
40*27b03b36SApple OSS Distributions work_interval_t handle = NULL;
41*27b03b36SApple OSS Distributions uint64_t now = mach_absolute_time();
42*27b03b36SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
43*27b03b36SApple OSS Distributions
44*27b03b36SApple OSS Distributions ret = work_interval_create(NULL, 0);
45*27b03b36SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "create with null errno EINVAL");
46*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, -1, "create with null returns -1");
47*27b03b36SApple OSS Distributions
48*27b03b36SApple OSS Distributions /* Binary must be entitled for this to succeed */
49*27b03b36SApple OSS Distributions ret = work_interval_create(&handle, 0);
50*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, no flags");
51*27b03b36SApple OSS Distributions
52*27b03b36SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
53*27b03b36SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "work_interval_copy_port on non-joinable interval errno EINVAL");
54*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, -1, "work_interval_copy_port on non-joinable interval returns -1");
55*27b03b36SApple OSS Distributions
56*27b03b36SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
57*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, no flags");
58*27b03b36SApple OSS Distributions
59*27b03b36SApple OSS Distributions ret = work_interval_destroy(handle);
60*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy, no flags");
61*27b03b36SApple OSS Distributions
62*27b03b36SApple OSS Distributions uint32_t flags[] = {
63*27b03b36SApple OSS Distributions WORK_INTERVAL_FLAG_JOINABLE,
64*27b03b36SApple OSS Distributions WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP,
65*27b03b36SApple OSS Distributions };
66*27b03b36SApple OSS Distributions
67*27b03b36SApple OSS Distributions for (uint32_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
68*27b03b36SApple OSS Distributions ret = work_interval_create(&handle, flags[i]);
69*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
70*27b03b36SApple OSS Distributions
71*27b03b36SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
72*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
73*27b03b36SApple OSS Distributions
74*27b03b36SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
75*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, -1, "work_interval_notify on non-joined thread returns -1");
76*27b03b36SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "work_interval_copy_port on non-joined thread errno EINVAL");
77*27b03b36SApple OSS Distributions
78*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
79*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, joinable");
80*27b03b36SApple OSS Distributions
81*27b03b36SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
82*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, on joined thread");
83*27b03b36SApple OSS Distributions
84*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
85*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, join the same interval after destroy");
86*27b03b36SApple OSS Distributions
87*27b03b36SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
88*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port");
89*27b03b36SApple OSS Distributions
90*27b03b36SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
91*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, on joined thread after destroy");
92*27b03b36SApple OSS Distributions
93*27b03b36SApple OSS Distributions ret = work_interval_destroy(handle);
94*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy, joinable, on joined thread");
95*27b03b36SApple OSS Distributions
96*27b03b36SApple OSS Distributions ret = work_interval_leave();
97*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave, on destroyed work interval");
98*27b03b36SApple OSS Distributions }
99*27b03b36SApple OSS Distributions
100*27b03b36SApple OSS Distributions ret = work_interval_create(&handle, WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP);
101*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
102*27b03b36SApple OSS Distributions
103*27b03b36SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
104*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
105*27b03b36SApple OSS Distributions
106*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
107*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, join before handing to another thread");
108*27b03b36SApple OSS Distributions
109*27b03b36SApple OSS Distributions pthread_t joining_thread;
110*27b03b36SApple OSS Distributions
111*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&joining_thread, NULL, joining_thread_fn, NULL), "pthread_create");
112*27b03b36SApple OSS Distributions
113*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(joining_thread, NULL), "pthread_join");
114*27b03b36SApple OSS Distributions
115*27b03b36SApple OSS Distributions ret = work_interval_leave();
116*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave");
117*27b03b36SApple OSS Distributions
118*27b03b36SApple OSS Distributions ret = work_interval_destroy(handle);
119*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy");
120*27b03b36SApple OSS Distributions }
121*27b03b36SApple OSS Distributions
122*27b03b36SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
123*27b03b36SApple OSS Distributions
124*27b03b36SApple OSS Distributions static uint64_t
nanos_to_abs(uint64_t nanos)125*27b03b36SApple OSS Distributions nanos_to_abs(uint64_t nanos)
126*27b03b36SApple OSS Distributions {
127*27b03b36SApple OSS Distributions mach_timebase_info(&timebase_info);
128*27b03b36SApple OSS Distributions return nanos * timebase_info.denom / timebase_info.numer;
129*27b03b36SApple OSS Distributions }
130*27b03b36SApple OSS Distributions
131*27b03b36SApple OSS Distributions static void
set_realtime(pthread_t thread)132*27b03b36SApple OSS Distributions set_realtime(pthread_t thread)
133*27b03b36SApple OSS Distributions {
134*27b03b36SApple OSS Distributions kern_return_t kr;
135*27b03b36SApple OSS Distributions thread_time_constraint_policy_data_t pol;
136*27b03b36SApple OSS Distributions
137*27b03b36SApple OSS Distributions mach_port_t target_thread = pthread_mach_thread_np(thread);
138*27b03b36SApple OSS Distributions T_ASSERT_NOTNULL(target_thread, "pthread_mach_thread_np");
139*27b03b36SApple OSS Distributions
140*27b03b36SApple OSS Distributions /* 1s 100ms 10ms */
141*27b03b36SApple OSS Distributions pol.period = (uint32_t)nanos_to_abs(1000000000);
142*27b03b36SApple OSS Distributions pol.constraint = (uint32_t)nanos_to_abs(100000000);
143*27b03b36SApple OSS Distributions pol.computation = (uint32_t)nanos_to_abs(10000000);
144*27b03b36SApple OSS Distributions
145*27b03b36SApple OSS Distributions pol.preemptible = 0; /* Ignored by OS */
146*27b03b36SApple OSS Distributions kr = thread_policy_set(target_thread, THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t) &pol,
147*27b03b36SApple OSS Distributions THREAD_TIME_CONSTRAINT_POLICY_COUNT);
148*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "thread_policy_set(THREAD_TIME_CONSTRAINT_POLICY)");
149*27b03b36SApple OSS Distributions }
150*27b03b36SApple OSS Distributions
151*27b03b36SApple OSS Distributions static void
set_nonrealtime(pthread_t thread)152*27b03b36SApple OSS Distributions set_nonrealtime(pthread_t thread)
153*27b03b36SApple OSS Distributions {
154*27b03b36SApple OSS Distributions kern_return_t kr;
155*27b03b36SApple OSS Distributions thread_standard_policy_data_t pol = {0};
156*27b03b36SApple OSS Distributions
157*27b03b36SApple OSS Distributions mach_port_t target_thread = pthread_mach_thread_np(thread);
158*27b03b36SApple OSS Distributions T_ASSERT_NOTNULL(target_thread, "pthread_mach_thread_np");
159*27b03b36SApple OSS Distributions
160*27b03b36SApple OSS Distributions kr = thread_policy_set(target_thread, THREAD_STANDARD_POLICY, (thread_policy_t) &pol,
161*27b03b36SApple OSS Distributions THREAD_STANDARD_POLICY_COUNT);
162*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "thread_policy_set(THREAD_STANDARD_POLICY)");
163*27b03b36SApple OSS Distributions }
164*27b03b36SApple OSS Distributions
165*27b03b36SApple OSS Distributions T_DECL(work_interval_audio_realtime_only, "joining RT threads to audio work interval", T_META_ASROOT(YES))
166*27b03b36SApple OSS Distributions {
167*27b03b36SApple OSS Distributions int ret = 0;
168*27b03b36SApple OSS Distributions work_interval_t handle = NULL;
169*27b03b36SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
170*27b03b36SApple OSS Distributions
171*27b03b36SApple OSS Distributions uint32_t flags = WORK_INTERVAL_FLAG_GROUP | WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_TYPE_COREAUDIO;
172*27b03b36SApple OSS Distributions
173*27b03b36SApple OSS Distributions ret = work_interval_create(&handle, flags);
174*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
175*27b03b36SApple OSS Distributions
176*27b03b36SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
177*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
178*27b03b36SApple OSS Distributions
179*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
180*27b03b36SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, EINVAL, "work_interval_join_port for audio on non-RT thread");
181*27b03b36SApple OSS Distributions
182*27b03b36SApple OSS Distributions set_realtime(pthread_self());
183*27b03b36SApple OSS Distributions
184*27b03b36SApple OSS Distributions ret = work_interval_join_port(port);
185*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port for audio on RT thread");
186*27b03b36SApple OSS Distributions
187*27b03b36SApple OSS Distributions ret = work_interval_leave();
188*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave");
189*27b03b36SApple OSS Distributions
190*27b03b36SApple OSS Distributions ret = work_interval_destroy(handle);
191*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy");
192*27b03b36SApple OSS Distributions
193*27b03b36SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
194*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port");
195*27b03b36SApple OSS Distributions
196*27b03b36SApple OSS Distributions set_nonrealtime(pthread_self());
197*27b03b36SApple OSS Distributions }
198*27b03b36SApple OSS Distributions
199*27b03b36SApple OSS Distributions T_DECL(work_interval_get_flags, "querying a port for create flags")
200*27b03b36SApple OSS Distributions {
201*27b03b36SApple OSS Distributions int ret = 0;
202*27b03b36SApple OSS Distributions work_interval_t handle = NULL;
203*27b03b36SApple OSS Distributions uint32_t flags = WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP | WORK_INTERVAL_TYPE_COREAUDIO;
204*27b03b36SApple OSS Distributions
205*27b03b36SApple OSS Distributions ret = work_interval_create(&handle, flags);
206*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create(AUDIO)");
207*27b03b36SApple OSS Distributions
208*27b03b36SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
209*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port");
210*27b03b36SApple OSS Distributions T_ASSERT_TRUE(MACH_PORT_VALID(port), "port from copy port is a valid port");
211*27b03b36SApple OSS Distributions
212*27b03b36SApple OSS Distributions uint32_t expected_flags = 0;
213*27b03b36SApple OSS Distributions
214*27b03b36SApple OSS Distributions ret = work_interval_get_flags_from_port(port, &expected_flags);
215*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, 0, "work_interval_get_flags_from_port");
216*27b03b36SApple OSS Distributions
217*27b03b36SApple OSS Distributions T_ASSERT_EQ(expected_flags, flags, "Flags match with what work interval was created with");
218*27b03b36SApple OSS Distributions
219*27b03b36SApple OSS Distributions mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1);
220*27b03b36SApple OSS Distributions work_interval_destroy(handle);
221*27b03b36SApple OSS Distributions
222*27b03b36SApple OSS Distributions // Negative test
223*27b03b36SApple OSS Distributions
224*27b03b36SApple OSS Distributions mach_port_t fake_port = MACH_PORT_NULL;
225*27b03b36SApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &fake_port);
226*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, 0, "successfully allocated a port");
227*27b03b36SApple OSS Distributions T_ASSERT_TRUE(MACH_PORT_VALID(fake_port), "allocated port is valid");
228*27b03b36SApple OSS Distributions
229*27b03b36SApple OSS Distributions ret = mach_port_insert_right(mach_task_self(), fake_port, fake_port, MACH_MSG_TYPE_MAKE_SEND);
230*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, 0, "successfully inserted a send right");
231*27b03b36SApple OSS Distributions
232*27b03b36SApple OSS Distributions ret = work_interval_get_flags_from_port(fake_port, &expected_flags);
233*27b03b36SApple OSS Distributions T_ASSERT_EQ(ret, -1, "query port failed as expected");
234*27b03b36SApple OSS Distributions
235*27b03b36SApple OSS Distributions mach_port_mod_refs(mach_task_self(), fake_port, MACH_PORT_RIGHT_SEND, -1);
236*27b03b36SApple OSS Distributions mach_port_mod_refs(mach_task_self(), fake_port, MACH_PORT_RIGHT_RECEIVE, -1);
237*27b03b36SApple OSS Distributions }
238