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