1cc9a6355SApple OSS Distributions #include <sys/types.h>
2cc9a6355SApple OSS Distributions #include <sys/event.h>
3cc9a6355SApple OSS Distributions #include <sys/time.h>
4cc9a6355SApple OSS Distributions #include <assert.h>
5cc9a6355SApple OSS Distributions #include <errno.h>
6cc9a6355SApple OSS Distributions #include <stdio.h>
7cc9a6355SApple OSS Distributions #include <stdlib.h>
8cc9a6355SApple OSS Distributions #include <unistd.h>
9cc9a6355SApple OSS Distributions #include <mach/mach.h>
10cc9a6355SApple OSS Distributions #include <mach/task.h>
11cc9a6355SApple OSS Distributions
12cc9a6355SApple OSS Distributions #include <TargetConditionals.h>
13cc9a6355SApple OSS Distributions #include <darwintest.h>
14cc9a6355SApple OSS Distributions
15cc9a6355SApple OSS Distributions #ifndef NOTE_MACHTIME
16cc9a6355SApple OSS Distributions #define NOTE_MACHTIME 0x00000100
17cc9a6355SApple OSS Distributions #endif
18cc9a6355SApple OSS Distributions
19cc9a6355SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
20cc9a6355SApple OSS Distributions
21a5e72196SApple OSS Distributions static uint64_t
nanos_to_abs(uint64_t nanos)22a5e72196SApple OSS Distributions nanos_to_abs(uint64_t nanos)
23a5e72196SApple OSS Distributions {
24a5e72196SApple OSS Distributions return nanos * timebase_info.denom / timebase_info.numer;
25a5e72196SApple OSS Distributions }
26a5e72196SApple OSS Distributions static uint64_t
abs_to_nanos(uint64_t abs)27a5e72196SApple OSS Distributions abs_to_nanos(uint64_t abs)
28a5e72196SApple OSS Distributions {
29a5e72196SApple OSS Distributions return abs * timebase_info.numer / timebase_info.denom;
30a5e72196SApple OSS Distributions }
31cc9a6355SApple OSS Distributions
32cc9a6355SApple OSS Distributions static int kq, passed, failed;
33cc9a6355SApple OSS Distributions
34cc9a6355SApple OSS Distributions static struct timespec failure_timeout = { .tv_sec = 10, .tv_nsec = 0 };
35cc9a6355SApple OSS Distributions
36cc9a6355SApple OSS Distributions /*
37cc9a6355SApple OSS Distributions * Wait for given kevent, which should return in 'expected' usecs.
38cc9a6355SApple OSS Distributions */
39cc9a6355SApple OSS Distributions static int
do_simple_kevent(struct kevent64_s * kev,uint64_t expected)40cc9a6355SApple OSS Distributions do_simple_kevent(struct kevent64_s *kev, uint64_t expected)
41cc9a6355SApple OSS Distributions {
42cc9a6355SApple OSS Distributions int ret;
43cc9a6355SApple OSS Distributions int64_t elapsed_usecs;
44cc9a6355SApple OSS Distributions uint64_t delta_usecs;
45cc9a6355SApple OSS Distributions struct timespec timeout;
46cc9a6355SApple OSS Distributions struct timeval before, after;
47cc9a6355SApple OSS Distributions
48cc9a6355SApple OSS Distributions /* time out after 1 sec extra delay */
49cc9a6355SApple OSS Distributions timeout.tv_sec = (expected / USEC_PER_SEC) + 1;
50cc9a6355SApple OSS Distributions timeout.tv_nsec = (expected % USEC_PER_SEC) * 1000;
51cc9a6355SApple OSS Distributions
52cc9a6355SApple OSS Distributions T_SETUPBEGIN;
53cc9a6355SApple OSS Distributions
54cc9a6355SApple OSS Distributions /* measure time for the kevent */
55cc9a6355SApple OSS Distributions gettimeofday(&before, NULL);
56cc9a6355SApple OSS Distributions ret = kevent64(kq, kev, 1, kev, 1, 0, &timeout);
57cc9a6355SApple OSS Distributions gettimeofday(&after, NULL);
58cc9a6355SApple OSS Distributions
59cc9a6355SApple OSS Distributions if (ret < 1 || (kev->flags & EV_ERROR)) {
60cc9a6355SApple OSS Distributions T_LOG("%s() failure: kevent returned %d, error %d\n", __func__, ret,
61cc9a6355SApple OSS Distributions (ret == -1 ? errno : (int) kev->data));
62cc9a6355SApple OSS Distributions return 0;
63cc9a6355SApple OSS Distributions }
64cc9a6355SApple OSS Distributions
65cc9a6355SApple OSS Distributions T_SETUPEND;
66cc9a6355SApple OSS Distributions
67cc9a6355SApple OSS Distributions /* did it work? */
68cc9a6355SApple OSS Distributions elapsed_usecs = (after.tv_sec - before.tv_sec) * (int64_t)USEC_PER_SEC +
69cc9a6355SApple OSS Distributions (after.tv_usec - before.tv_usec);
70cc9a6355SApple OSS Distributions delta_usecs = (uint64_t)llabs(elapsed_usecs - ((int64_t)expected));
71cc9a6355SApple OSS Distributions
72cc9a6355SApple OSS Distributions /* failure if we're 30% off, or 50 mics late */
73cc9a6355SApple OSS Distributions if (delta_usecs > (30 * expected / 100.0) && delta_usecs > 50) {
74cc9a6355SApple OSS Distributions T_LOG("\tfailure: expected %lld usec, measured %lld usec.\n",
75cc9a6355SApple OSS Distributions expected, elapsed_usecs);
76cc9a6355SApple OSS Distributions return 0;
77cc9a6355SApple OSS Distributions } else {
78cc9a6355SApple OSS Distributions T_LOG("\tsuccess, measured %lld usec.\n", elapsed_usecs);
79cc9a6355SApple OSS Distributions return 1;
80cc9a6355SApple OSS Distributions }
81cc9a6355SApple OSS Distributions }
82cc9a6355SApple OSS Distributions
83cc9a6355SApple OSS Distributions static void
test_absolute_kevent(int time,int scale)84cc9a6355SApple OSS Distributions test_absolute_kevent(int time, int scale)
85cc9a6355SApple OSS Distributions {
86cc9a6355SApple OSS Distributions struct timeval tv;
87cc9a6355SApple OSS Distributions struct kevent64_s kev;
88cc9a6355SApple OSS Distributions uint64_t nowus, expected, timescale = 0;
89cc9a6355SApple OSS Distributions int ret;
90cc9a6355SApple OSS Distributions int64_t deadline;
91cc9a6355SApple OSS Distributions
92cc9a6355SApple OSS Distributions gettimeofday(&tv, NULL);
93cc9a6355SApple OSS Distributions nowus = (uint64_t)tv.tv_sec * USEC_PER_SEC + (uint64_t)tv.tv_usec;
94cc9a6355SApple OSS Distributions
95cc9a6355SApple OSS Distributions T_SETUPBEGIN;
96cc9a6355SApple OSS Distributions
97cc9a6355SApple OSS Distributions switch (scale) {
98cc9a6355SApple OSS Distributions case NOTE_MACHTIME:
99cc9a6355SApple OSS Distributions T_LOG("Testing %d MATUs absolute timer...\n", time);
100cc9a6355SApple OSS Distributions break;
101cc9a6355SApple OSS Distributions case NOTE_SECONDS:
102cc9a6355SApple OSS Distributions T_LOG("Testing %d sec absolute timer...\n", time);
103cc9a6355SApple OSS Distributions timescale = USEC_PER_SEC;
104cc9a6355SApple OSS Distributions break;
105cc9a6355SApple OSS Distributions case NOTE_USECONDS:
106cc9a6355SApple OSS Distributions T_LOG("Testing %d usec absolute timer...\n", time);
107cc9a6355SApple OSS Distributions timescale = 1;
108cc9a6355SApple OSS Distributions break;
109cc9a6355SApple OSS Distributions case 0:
110cc9a6355SApple OSS Distributions T_LOG("Testing %d msec absolute timer...\n", time);
111cc9a6355SApple OSS Distributions timescale = 1000;
112cc9a6355SApple OSS Distributions break;
113cc9a6355SApple OSS Distributions default:
114cc9a6355SApple OSS Distributions T_FAIL("Failure: scale 0x%x not recognized.\n", scale);
115cc9a6355SApple OSS Distributions return;
116cc9a6355SApple OSS Distributions }
117cc9a6355SApple OSS Distributions
118cc9a6355SApple OSS Distributions T_SETUPEND;
119cc9a6355SApple OSS Distributions
120cc9a6355SApple OSS Distributions if (scale == NOTE_MACHTIME) {
121cc9a6355SApple OSS Distributions expected = abs_to_nanos((uint64_t)time) / NSEC_PER_USEC;
122cc9a6355SApple OSS Distributions deadline = (int64_t)mach_absolute_time() + time;
123cc9a6355SApple OSS Distributions } else {
124cc9a6355SApple OSS Distributions expected = (uint64_t)time * timescale;
125cc9a6355SApple OSS Distributions deadline = (int64_t)(nowus / timescale) + time;
126cc9a6355SApple OSS Distributions }
127cc9a6355SApple OSS Distributions
128cc9a6355SApple OSS Distributions /* deadlines in the past should fire immediately */
129a5e72196SApple OSS Distributions if (time < 0) {
130cc9a6355SApple OSS Distributions expected = 0;
131a5e72196SApple OSS Distributions }
132cc9a6355SApple OSS Distributions
133cc9a6355SApple OSS Distributions EV_SET64(&kev, 1, EVFILT_TIMER, EV_ADD,
134cc9a6355SApple OSS Distributions NOTE_ABSOLUTE | scale, deadline, 0, 0, 0);
135cc9a6355SApple OSS Distributions ret = do_simple_kevent(&kev, expected);
136cc9a6355SApple OSS Distributions
137cc9a6355SApple OSS Distributions if (ret) {
138cc9a6355SApple OSS Distributions passed++;
139cc9a6355SApple OSS Distributions T_PASS("%s time:%d, scale:0x%x", __func__, time, scale);
140cc9a6355SApple OSS Distributions } else {
141cc9a6355SApple OSS Distributions failed++;
142cc9a6355SApple OSS Distributions T_FAIL("%s time:%d, scale:0x%x", __func__, time, scale);
143cc9a6355SApple OSS Distributions }
144cc9a6355SApple OSS Distributions }
145cc9a6355SApple OSS Distributions
146cc9a6355SApple OSS Distributions static void
test_oneshot_kevent(int time,int scale)147cc9a6355SApple OSS Distributions test_oneshot_kevent(int time, int scale)
148cc9a6355SApple OSS Distributions {
149cc9a6355SApple OSS Distributions int ret;
150cc9a6355SApple OSS Distributions uint64_t expected = 0;
151cc9a6355SApple OSS Distributions struct kevent64_s kev;
152cc9a6355SApple OSS Distributions
153cc9a6355SApple OSS Distributions T_SETUPBEGIN;
154cc9a6355SApple OSS Distributions
155cc9a6355SApple OSS Distributions switch (scale) {
156cc9a6355SApple OSS Distributions case NOTE_MACHTIME:
157cc9a6355SApple OSS Distributions T_LOG("Testing %d MATUs interval timer...\n", time);
158cc9a6355SApple OSS Distributions expected = abs_to_nanos((uint64_t)time) / NSEC_PER_USEC;
159cc9a6355SApple OSS Distributions break;
160cc9a6355SApple OSS Distributions case NOTE_SECONDS:
161cc9a6355SApple OSS Distributions T_LOG("Testing %d sec interval timer...\n", time);
162cc9a6355SApple OSS Distributions expected = (uint64_t)time * USEC_PER_SEC;
163cc9a6355SApple OSS Distributions break;
164cc9a6355SApple OSS Distributions case NOTE_USECONDS:
165cc9a6355SApple OSS Distributions T_LOG("Testing %d usec interval timer...\n", time);
166cc9a6355SApple OSS Distributions expected = (uint64_t)time;
167cc9a6355SApple OSS Distributions break;
168cc9a6355SApple OSS Distributions case NOTE_NSECONDS:
169cc9a6355SApple OSS Distributions T_LOG("Testing %d nsec interval timer...\n", time);
170cc9a6355SApple OSS Distributions expected = (uint64_t)time / 1000;
171cc9a6355SApple OSS Distributions break;
172cc9a6355SApple OSS Distributions case 0:
173cc9a6355SApple OSS Distributions T_LOG("Testing %d msec interval timer...\n", time);
174cc9a6355SApple OSS Distributions expected = (uint64_t)time * 1000;
175cc9a6355SApple OSS Distributions break;
176cc9a6355SApple OSS Distributions default:
177cc9a6355SApple OSS Distributions T_FAIL("Failure: scale 0x%x not recognized.\n", scale);
178cc9a6355SApple OSS Distributions return;
179cc9a6355SApple OSS Distributions }
180cc9a6355SApple OSS Distributions
181cc9a6355SApple OSS Distributions T_SETUPEND;
182cc9a6355SApple OSS Distributions
183cc9a6355SApple OSS Distributions /* deadlines in the past should fire immediately */
184a5e72196SApple OSS Distributions if (time < 0) {
185cc9a6355SApple OSS Distributions expected = 0;
186a5e72196SApple OSS Distributions }
187cc9a6355SApple OSS Distributions
188cc9a6355SApple OSS Distributions EV_SET64(&kev, 2, EVFILT_TIMER, EV_ADD | EV_ONESHOT, scale, time,
189cc9a6355SApple OSS Distributions 0, 0, 0);
190cc9a6355SApple OSS Distributions ret = do_simple_kevent(&kev, expected);
191cc9a6355SApple OSS Distributions
192cc9a6355SApple OSS Distributions if (ret) {
193cc9a6355SApple OSS Distributions passed++;
194cc9a6355SApple OSS Distributions T_PASS("%s time:%d, scale:0x%x", __func__, time, scale);
195cc9a6355SApple OSS Distributions } else {
196cc9a6355SApple OSS Distributions failed++;
197cc9a6355SApple OSS Distributions T_FAIL("%s time:%d, scale:0x%x", __func__, time, scale);
198cc9a6355SApple OSS Distributions }
199cc9a6355SApple OSS Distributions }
200cc9a6355SApple OSS Distributions
201cc9a6355SApple OSS Distributions /* Test that the timer goes ding multiple times */
202cc9a6355SApple OSS Distributions static void
test_interval_kevent(int usec)203cc9a6355SApple OSS Distributions test_interval_kevent(int usec)
204cc9a6355SApple OSS Distributions {
205cc9a6355SApple OSS Distributions struct kevent64_s kev;
206cc9a6355SApple OSS Distributions int ret;
207cc9a6355SApple OSS Distributions
208cc9a6355SApple OSS Distributions T_SETUPBEGIN;
209cc9a6355SApple OSS Distributions
210cc9a6355SApple OSS Distributions uint64_t test_duration_us = USEC_PER_SEC; /* 1 second */
211cc9a6355SApple OSS Distributions uint64_t expected_pops;
212cc9a6355SApple OSS Distributions
213a5e72196SApple OSS Distributions if (usec < 0) {
214cc9a6355SApple OSS Distributions expected_pops = 1; /* TODO: test 'and only once' */
215a5e72196SApple OSS Distributions } else {
216cc9a6355SApple OSS Distributions expected_pops = test_duration_us / (uint64_t)usec;
217a5e72196SApple OSS Distributions }
218cc9a6355SApple OSS Distributions
219cc9a6355SApple OSS Distributions T_LOG("Testing interval kevent at %d usec intervals (%lld pops/second)...\n",
220cc9a6355SApple OSS Distributions usec, expected_pops);
221cc9a6355SApple OSS Distributions
222cc9a6355SApple OSS Distributions EV_SET64(&kev, 3, EVFILT_TIMER, EV_ADD, NOTE_USECONDS, usec, 0, 0, 0);
223cc9a6355SApple OSS Distributions ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
224cc9a6355SApple OSS Distributions if (ret != 0 || (kev.flags & EV_ERROR)) {
225cc9a6355SApple OSS Distributions T_FAIL("%s() setup failure: kevent64 returned %d\n", __func__, ret);
226cc9a6355SApple OSS Distributions failed++;
227cc9a6355SApple OSS Distributions return;
228cc9a6355SApple OSS Distributions }
229cc9a6355SApple OSS Distributions
230cc9a6355SApple OSS Distributions T_SETUPEND;
231cc9a6355SApple OSS Distributions
232cc9a6355SApple OSS Distributions struct timeval before, after;
233cc9a6355SApple OSS Distributions uint64_t elapsed_usecs;
234cc9a6355SApple OSS Distributions
235cc9a6355SApple OSS Distributions gettimeofday(&before, NULL);
236cc9a6355SApple OSS Distributions
237cc9a6355SApple OSS Distributions uint64_t pops = 0;
238cc9a6355SApple OSS Distributions
239cc9a6355SApple OSS Distributions for (uint32_t i = 0; i < expected_pops; i++) {
240cc9a6355SApple OSS Distributions ret = kevent64(kq, NULL, 0, &kev, 1, 0, &failure_timeout);
241cc9a6355SApple OSS Distributions if (ret != 1) {
242cc9a6355SApple OSS Distributions T_FAIL("%s() failure: kevent64 returned %d\n", __func__, ret);
243cc9a6355SApple OSS Distributions failed++;
244cc9a6355SApple OSS Distributions return;
245cc9a6355SApple OSS Distributions }
246cc9a6355SApple OSS Distributions
247cc9a6355SApple OSS Distributions //T_LOG("\t ding: %lld\n", kev.data);
248cc9a6355SApple OSS Distributions
249cc9a6355SApple OSS Distributions pops += (uint64_t)kev.data;
250cc9a6355SApple OSS Distributions gettimeofday(&after, NULL);
251cc9a6355SApple OSS Distributions elapsed_usecs = (uint64_t)((after.tv_sec - before.tv_sec) * (int64_t)USEC_PER_SEC +
252cc9a6355SApple OSS Distributions (after.tv_usec - before.tv_usec));
253cc9a6355SApple OSS Distributions
254a5e72196SApple OSS Distributions if (elapsed_usecs > test_duration_us) {
255cc9a6355SApple OSS Distributions break;
256cc9a6355SApple OSS Distributions }
257a5e72196SApple OSS Distributions }
258cc9a6355SApple OSS Distributions
259cc9a6355SApple OSS Distributions /* check how many times the timer fired: within 5%? */
260cc9a6355SApple OSS Distributions if (pops > expected_pops + (expected_pops / 20) ||
261cc9a6355SApple OSS Distributions pops < expected_pops - (expected_pops / 20)) {
262cc9a6355SApple OSS Distributions T_FAIL("%s() usec:%d (saw %lld of %lld expected pops)", __func__, usec, pops, expected_pops);
263cc9a6355SApple OSS Distributions failed++;
264cc9a6355SApple OSS Distributions } else {
265cc9a6355SApple OSS Distributions T_PASS("%s() usec:%d (saw %lld pops)", __func__, usec, pops);
266cc9a6355SApple OSS Distributions passed++;
267cc9a6355SApple OSS Distributions }
268cc9a6355SApple OSS Distributions
269cc9a6355SApple OSS Distributions EV_SET64(&kev, 3, EVFILT_TIMER, EV_DELETE, 0, 0, 0, 0, 0);
270cc9a6355SApple OSS Distributions ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
271cc9a6355SApple OSS Distributions if (ret != 0) {
272cc9a6355SApple OSS Distributions T_LOG("\tfailed to stop repeating timer: %d\n", ret);
273cc9a6355SApple OSS Distributions }
274cc9a6355SApple OSS Distributions }
275cc9a6355SApple OSS Distributions
276cc9a6355SApple OSS Distributions /* Test that the repeating timer repeats even while not polling in kqueue */
277cc9a6355SApple OSS Distributions static void
test_repeating_kevent(int usec)278cc9a6355SApple OSS Distributions test_repeating_kevent(int usec)
279cc9a6355SApple OSS Distributions {
280cc9a6355SApple OSS Distributions struct kevent64_s kev;
281cc9a6355SApple OSS Distributions int ret;
282cc9a6355SApple OSS Distributions
283cc9a6355SApple OSS Distributions T_SETUPBEGIN;
284cc9a6355SApple OSS Distributions
285cc9a6355SApple OSS Distributions uint64_t test_duration_us = USEC_PER_SEC; /* 1 second */
286cc9a6355SApple OSS Distributions
287cc9a6355SApple OSS Distributions uint64_t expected_pops = test_duration_us / (uint64_t)usec;
288cc9a6355SApple OSS Distributions T_LOG("Testing repeating kevent at %d usec intervals (%lld pops/second)...\n",
289cc9a6355SApple OSS Distributions usec, expected_pops);
290cc9a6355SApple OSS Distributions
291cc9a6355SApple OSS Distributions EV_SET64(&kev, 4, EVFILT_TIMER, EV_ADD, NOTE_USECONDS, usec, 0, 0, 0);
292cc9a6355SApple OSS Distributions ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
293cc9a6355SApple OSS Distributions if (ret != 0) {
294cc9a6355SApple OSS Distributions T_FAIL("%s() setup failure: kevent64 returned %d\n", __func__, ret);
295cc9a6355SApple OSS Distributions failed++;
296cc9a6355SApple OSS Distributions return;
297cc9a6355SApple OSS Distributions }
298cc9a6355SApple OSS Distributions
299cc9a6355SApple OSS Distributions usleep((useconds_t)test_duration_us);
300cc9a6355SApple OSS Distributions
301cc9a6355SApple OSS Distributions ret = kevent64(kq, NULL, 0, &kev, 1, 0, &failure_timeout);
302cc9a6355SApple OSS Distributions if (ret != 1 || (kev.flags & EV_ERROR)) {
303cc9a6355SApple OSS Distributions T_FAIL("%s() setup failure: kevent64 returned %d\n", __func__, ret);
304cc9a6355SApple OSS Distributions failed++;
305cc9a6355SApple OSS Distributions return;
306cc9a6355SApple OSS Distributions }
307cc9a6355SApple OSS Distributions
308cc9a6355SApple OSS Distributions T_SETUPEND;
309cc9a6355SApple OSS Distributions
310cc9a6355SApple OSS Distributions uint64_t pops = (uint64_t) kev.data;
311cc9a6355SApple OSS Distributions
312cc9a6355SApple OSS Distributions /* check how many times the timer fired: within 5%? */
313cc9a6355SApple OSS Distributions if (pops > expected_pops + (expected_pops / 20) ||
314cc9a6355SApple OSS Distributions pops < expected_pops - (expected_pops / 20)) {
315cc9a6355SApple OSS Distributions T_FAIL("%s() usec:%d (saw %lld of %lld expected pops)", __func__, usec, pops, expected_pops);
316cc9a6355SApple OSS Distributions failed++;
317cc9a6355SApple OSS Distributions } else {
318cc9a6355SApple OSS Distributions T_PASS("%s() usec:%d (saw %lld pops)", __func__, usec, pops);
319cc9a6355SApple OSS Distributions passed++;
320cc9a6355SApple OSS Distributions }
321cc9a6355SApple OSS Distributions
322cc9a6355SApple OSS Distributions EV_SET64(&kev, 4, EVFILT_TIMER, EV_DELETE, 0, 0, 0, 0, 0);
323cc9a6355SApple OSS Distributions ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
324cc9a6355SApple OSS Distributions if (ret != 0) {
325cc9a6355SApple OSS Distributions T_LOG("\tfailed to stop repeating timer: %d\n", ret);
326cc9a6355SApple OSS Distributions }
327cc9a6355SApple OSS Distributions }
328cc9a6355SApple OSS Distributions
329cc9a6355SApple OSS Distributions
330cc9a6355SApple OSS Distributions static void
test_updated_kevent(int first,int second)331cc9a6355SApple OSS Distributions test_updated_kevent(int first, int second)
332cc9a6355SApple OSS Distributions {
333cc9a6355SApple OSS Distributions struct kevent64_s kev;
334cc9a6355SApple OSS Distributions int ret;
335cc9a6355SApple OSS Distributions
336cc9a6355SApple OSS Distributions T_LOG("Testing update from %d to %d msecs...\n", first, second);
337cc9a6355SApple OSS Distributions
338cc9a6355SApple OSS Distributions T_SETUPBEGIN;
339cc9a6355SApple OSS Distributions
340cc9a6355SApple OSS Distributions EV_SET64(&kev, 4, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, first, 0, 0, 0);
341cc9a6355SApple OSS Distributions ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
342cc9a6355SApple OSS Distributions if (ret != 0) {
343cc9a6355SApple OSS Distributions T_FAIL("%s() failure: initial kevent returned %d\n", __func__, ret);
344cc9a6355SApple OSS Distributions failed++;
345cc9a6355SApple OSS Distributions return;
346cc9a6355SApple OSS Distributions }
347cc9a6355SApple OSS Distributions
348cc9a6355SApple OSS Distributions T_SETUPEND;
349cc9a6355SApple OSS Distributions
350cc9a6355SApple OSS Distributions EV_SET64(&kev, 4, EVFILT_TIMER, EV_ONESHOT, 0, second, 0, 0, 0);
351cc9a6355SApple OSS Distributions
352cc9a6355SApple OSS Distributions uint64_t expected_us = (uint64_t)second * 1000;
353cc9a6355SApple OSS Distributions
354a5e72196SApple OSS Distributions if (second < 0) {
355cc9a6355SApple OSS Distributions expected_us = 0;
356a5e72196SApple OSS Distributions }
357cc9a6355SApple OSS Distributions
358cc9a6355SApple OSS Distributions ret = do_simple_kevent(&kev, expected_us);
359cc9a6355SApple OSS Distributions
360cc9a6355SApple OSS Distributions if (ret) {
361cc9a6355SApple OSS Distributions passed++;
362cc9a6355SApple OSS Distributions T_PASS("%s() %d, %d", __func__, first, second);
363cc9a6355SApple OSS Distributions } else {
364cc9a6355SApple OSS Distributions failed++;
365cc9a6355SApple OSS Distributions T_FAIL("%s() %d, %d", __func__, first, second);
366cc9a6355SApple OSS Distributions }
367cc9a6355SApple OSS Distributions }
368cc9a6355SApple OSS Distributions
369cc9a6355SApple OSS Distributions static void
disable_timer_coalescing(void)370cc9a6355SApple OSS Distributions disable_timer_coalescing(void)
371cc9a6355SApple OSS Distributions {
372cc9a6355SApple OSS Distributions struct task_qos_policy qosinfo;
373cc9a6355SApple OSS Distributions kern_return_t kr;
374cc9a6355SApple OSS Distributions
375cc9a6355SApple OSS Distributions T_SETUPBEGIN;
376cc9a6355SApple OSS Distributions
377cc9a6355SApple OSS Distributions qosinfo.task_latency_qos_tier = LATENCY_QOS_TIER_0;
378cc9a6355SApple OSS Distributions qosinfo.task_throughput_qos_tier = THROUGHPUT_QOS_TIER_0;
379cc9a6355SApple OSS Distributions
380cc9a6355SApple OSS Distributions kr = task_policy_set(mach_task_self(), TASK_OVERRIDE_QOS_POLICY, (task_policy_t)&qosinfo,
381cc9a6355SApple OSS Distributions TASK_QOS_POLICY_COUNT);
382cc9a6355SApple OSS Distributions if (kr != KERN_SUCCESS) {
383cc9a6355SApple OSS Distributions T_FAIL("task_policy_set(... TASK_OVERRIDE_QOS_POLICY ...) failed: %d (%s)", kr, mach_error_string(kr));
384cc9a6355SApple OSS Distributions }
385cc9a6355SApple OSS Distributions
386cc9a6355SApple OSS Distributions T_SETUPEND;
387cc9a6355SApple OSS Distributions }
388cc9a6355SApple OSS Distributions
389cc9a6355SApple OSS Distributions T_DECL(kqueue_timer_tests,
390e6231be0SApple OSS Distributions "Tests assorted kqueue operations for timer-related events",
391*8d741a5dSApple OSS Distributions T_META_REQUIRES_SYSCTL_NE("kern.kasan.available", 1), T_META_TAG_VM_PREFERRED)
392cc9a6355SApple OSS Distributions {
393cc9a6355SApple OSS Distributions /*
394cc9a6355SApple OSS Distributions * Since we're trying to test timers here, disable timer coalescing
395cc9a6355SApple OSS Distributions * to improve the accuracy of timer fires for this process.
396cc9a6355SApple OSS Distributions */
397cc9a6355SApple OSS Distributions disable_timer_coalescing();
398cc9a6355SApple OSS Distributions
399cc9a6355SApple OSS Distributions mach_timebase_info(&timebase_info);
400cc9a6355SApple OSS Distributions
401cc9a6355SApple OSS Distributions kq = kqueue();
402cc9a6355SApple OSS Distributions assert(kq > 0);
403cc9a6355SApple OSS Distributions passed = 0;
404cc9a6355SApple OSS Distributions failed = 0;
405cc9a6355SApple OSS Distributions
406cc9a6355SApple OSS Distributions test_absolute_kevent(100, 0);
407cc9a6355SApple OSS Distributions test_absolute_kevent(200, 0);
408cc9a6355SApple OSS Distributions test_absolute_kevent(300, 0);
409cc9a6355SApple OSS Distributions test_absolute_kevent(1000, 0);
410cc9a6355SApple OSS Distributions T_MAYFAIL;
411cc9a6355SApple OSS Distributions test_absolute_kevent(500, NOTE_USECONDS);
412cc9a6355SApple OSS Distributions T_MAYFAIL;
413cc9a6355SApple OSS Distributions test_absolute_kevent(100, NOTE_USECONDS);
414cc9a6355SApple OSS Distributions T_MAYFAIL;
415cc9a6355SApple OSS Distributions test_absolute_kevent(2, NOTE_SECONDS);
416cc9a6355SApple OSS Distributions T_MAYFAIL;
417cc9a6355SApple OSS Distributions test_absolute_kevent(-1000, 0);
418cc9a6355SApple OSS Distributions
419cc9a6355SApple OSS Distributions T_MAYFAIL;
420cc9a6355SApple OSS Distributions test_absolute_kevent((int)nanos_to_abs(10 * NSEC_PER_MSEC), NOTE_MACHTIME);
421cc9a6355SApple OSS Distributions
422cc9a6355SApple OSS Distributions test_oneshot_kevent(1, NOTE_SECONDS);
423cc9a6355SApple OSS Distributions T_MAYFAIL;
424cc9a6355SApple OSS Distributions test_oneshot_kevent(10, 0);
425cc9a6355SApple OSS Distributions T_MAYFAIL;
426cc9a6355SApple OSS Distributions test_oneshot_kevent(200, NOTE_USECONDS);
427cc9a6355SApple OSS Distributions T_MAYFAIL;
428cc9a6355SApple OSS Distributions test_oneshot_kevent(300000, NOTE_NSECONDS);
429cc9a6355SApple OSS Distributions T_MAYFAIL;
430cc9a6355SApple OSS Distributions test_oneshot_kevent(-1, NOTE_SECONDS);
431cc9a6355SApple OSS Distributions
432cc9a6355SApple OSS Distributions T_MAYFAIL;
433cc9a6355SApple OSS Distributions test_oneshot_kevent((int)nanos_to_abs(10 * NSEC_PER_MSEC), NOTE_MACHTIME);
434cc9a6355SApple OSS Distributions
435cc9a6355SApple OSS Distributions test_interval_kevent(250 * 1000);
436cc9a6355SApple OSS Distributions T_MAYFAIL;
437cc9a6355SApple OSS Distributions test_interval_kevent(5 * 1000);
438cc9a6355SApple OSS Distributions T_MAYFAIL;
439cc9a6355SApple OSS Distributions test_interval_kevent(200);
440cc9a6355SApple OSS Distributions T_MAYFAIL;
441cc9a6355SApple OSS Distributions test_interval_kevent(50);
442cc9a6355SApple OSS Distributions
443cc9a6355SApple OSS Distributions test_interval_kevent(-1000);
444cc9a6355SApple OSS Distributions
445cc9a6355SApple OSS Distributions test_repeating_kevent(10000); /* 10ms */
446cc9a6355SApple OSS Distributions
447cc9a6355SApple OSS Distributions test_updated_kevent(1000, 2000);
448cc9a6355SApple OSS Distributions test_updated_kevent(2000, 1000);
449e6231be0SApple OSS Distributions T_MAYFAIL;
450cc9a6355SApple OSS Distributions test_updated_kevent(1000, -1);
451cc9a6355SApple OSS Distributions }
452