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