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