t_timer.c revision 1.3 1 1.3 thorpej /* $NetBSD: t_timer.c,v 1.3 2021/10/22 13:53:20 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej
29 1.1 thorpej #include <sys/cdefs.h>
30 1.3 thorpej __RCSID("$NetBSD: t_timer.c,v 1.3 2021/10/22 13:53:20 thorpej Exp $");
31 1.1 thorpej
32 1.1 thorpej #include <sys/types.h>
33 1.1 thorpej #include <sys/event.h>
34 1.1 thorpej #include <errno.h>
35 1.1 thorpej #include <stdio.h>
36 1.1 thorpej #include <stdlib.h>
37 1.1 thorpej #include <time.h>
38 1.1 thorpej #include <unistd.h>
39 1.1 thorpej
40 1.1 thorpej #include <atf-c.h>
41 1.1 thorpej
42 1.1 thorpej ATF_TC(basic_timer);
43 1.1 thorpej ATF_TC_HEAD(basic_timer, tc)
44 1.1 thorpej {
45 1.1 thorpej atf_tc_set_md_var(tc, "descr",
46 1.1 thorpej "tests basic EVFILT_TIMER functionality");
47 1.1 thorpej }
48 1.1 thorpej
49 1.1 thorpej #define TIME1 1000 /* 1000ms -> 1s */
50 1.1 thorpej #define TIME1_COUNT 5
51 1.1 thorpej #define TIME2 6000 /* 6000ms -> 6s */
52 1.1 thorpej
53 1.1 thorpej #define TIME1_TOTAL_SEC ((TIME1 * TIME1_COUNT) / 1000)
54 1.1 thorpej #define TIME2_TOTAL_SEC (TIME2 / 1000)
55 1.1 thorpej
56 1.1 thorpej ATF_TC_BODY(basic_timer, tc)
57 1.1 thorpej {
58 1.1 thorpej struct kevent event[2];
59 1.1 thorpej int ntimer1 = 0, ntimer2 = 0;
60 1.1 thorpej struct timespec ots, ts;
61 1.1 thorpej int kq;
62 1.1 thorpej
63 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
64 1.1 thorpej
65 1.1 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, TIME1, NULL);
66 1.1 thorpej EV_SET(&event[1], 2, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, TIME2, NULL);
67 1.1 thorpej
68 1.1 thorpej ATF_REQUIRE(kevent(kq, event, 2, NULL, 0, NULL) == 0);
69 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
70 1.1 thorpej
71 1.1 thorpej for (;;) {
72 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, NULL) == 1);
73 1.1 thorpej ATF_REQUIRE(event[0].filter == EVFILT_TIMER);
74 1.1 thorpej ATF_REQUIRE(event[0].ident == 1 ||
75 1.1 thorpej event[0].ident == 2);
76 1.1 thorpej if (event[0].ident == 1) {
77 1.1 thorpej ATF_REQUIRE(ntimer1 < TIME1_COUNT);
78 1.1 thorpej if (++ntimer1 == TIME1_COUNT) {
79 1.1 thorpej /*
80 1.1 thorpej * Make sure TIME1_TOTAL_SEC seconds have
81 1.1 thorpej * elapsed, allowing for a little slop.
82 1.1 thorpej */
83 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC,
84 1.1 thorpej &ts) == 0);
85 1.1 thorpej timespecsub(&ts, &ots, &ts);
86 1.1 thorpej ATF_REQUIRE(ts.tv_sec ==
87 1.1 thorpej (TIME1_TOTAL_SEC - 1) ||
88 1.1 thorpej ts.tv_sec == TIME1_TOTAL_SEC);
89 1.1 thorpej if (ts.tv_sec == TIME1_TOTAL_SEC - 1) {
90 1.1 thorpej ATF_REQUIRE(ts.tv_nsec >=
91 1.1 thorpej 900000000);
92 1.1 thorpej }
93 1.1 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_DELETE,
94 1.1 thorpej 0, 0, NULL);
95 1.1 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0,
96 1.1 thorpej NULL) == 0);
97 1.1 thorpej }
98 1.1 thorpej } else {
99 1.1 thorpej ATF_REQUIRE(ntimer1 == TIME1_COUNT);
100 1.1 thorpej ATF_REQUIRE(ntimer2 == 0);
101 1.1 thorpej ntimer2++;
102 1.1 thorpej /*
103 1.1 thorpej * Make sure TIME2_TOTAL_SEC seconds have
104 1.1 thorpej * elapsed, allowing for a little slop.
105 1.1 thorpej */
106 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC,
107 1.1 thorpej &ts) == 0);
108 1.1 thorpej timespecsub(&ts, &ots, &ts);
109 1.1 thorpej ATF_REQUIRE(ts.tv_sec ==
110 1.1 thorpej (TIME2_TOTAL_SEC - 1) ||
111 1.1 thorpej ts.tv_sec == TIME2_TOTAL_SEC);
112 1.1 thorpej if (ts.tv_sec == TIME2_TOTAL_SEC - 1) {
113 1.1 thorpej ATF_REQUIRE(ts.tv_nsec >= 900000000);
114 1.1 thorpej }
115 1.1 thorpej EV_SET(&event[0], 2, EVFILT_TIMER, EV_DELETE,
116 1.1 thorpej 0, 0, NULL);
117 1.1 thorpej ATF_REQUIRE_ERRNO(ENOENT,
118 1.1 thorpej kevent(kq, event, 1, NULL, 0, NULL) == -1);
119 1.1 thorpej break;
120 1.1 thorpej }
121 1.1 thorpej }
122 1.1 thorpej
123 1.1 thorpej /*
124 1.1 thorpej * Now block in kqueue for TIME2_TOTAL_SEC, and ensure we
125 1.1 thorpej * don't receive any new events.
126 1.1 thorpej */
127 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
128 1.1 thorpej ts.tv_sec = TIME2_TOTAL_SEC;
129 1.1 thorpej ts.tv_nsec = 0;
130 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 0);
131 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
132 1.1 thorpej timespecsub(&ts, &ots, &ts);
133 1.1 thorpej ATF_REQUIRE(ts.tv_sec == (TIME2_TOTAL_SEC - 1) ||
134 1.1 thorpej ts.tv_sec == TIME2_TOTAL_SEC ||
135 1.1 thorpej ts.tv_sec == (TIME2_TOTAL_SEC + 1));
136 1.1 thorpej if (ts.tv_sec == TIME2_TOTAL_SEC - 1) {
137 1.1 thorpej ATF_REQUIRE(ts.tv_nsec >= 900000000);
138 1.1 thorpej } else if (ts.tv_sec == TIME2_TOTAL_SEC + 1) {
139 1.1 thorpej ATF_REQUIRE(ts.tv_nsec < 500000000);
140 1.1 thorpej }
141 1.1 thorpej }
142 1.1 thorpej
143 1.1 thorpej ATF_TC(count_expirations);
144 1.1 thorpej ATF_TC_HEAD(count_expirations, tc)
145 1.1 thorpej {
146 1.1 thorpej atf_tc_set_md_var(tc, "descr",
147 1.1 thorpej "tests counting timer expirations");
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej ATF_TC_BODY(count_expirations, tc)
151 1.1 thorpej {
152 1.1 thorpej struct kevent event[1];
153 1.1 thorpej struct timespec ts = { 0, 0 };
154 1.1 thorpej struct timespec sleepts;
155 1.1 thorpej int kq;
156 1.1 thorpej
157 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
158 1.1 thorpej
159 1.1 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, TIME1, NULL);
160 1.1 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
161 1.1 thorpej
162 1.1 thorpej /* Sleep a little longer to mitigate timing jitter. */
163 1.1 thorpej sleepts.tv_sec = TIME1_TOTAL_SEC;
164 1.1 thorpej sleepts.tv_nsec = 500000000;
165 1.1 thorpej ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
166 1.1 thorpej
167 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
168 1.1 thorpej ATF_REQUIRE(event[0].ident == 1);
169 1.1 thorpej ATF_REQUIRE(event[0].data == TIME1_COUNT ||
170 1.1 thorpej event[0].data == TIME1_COUNT + 1);
171 1.1 thorpej }
172 1.1 thorpej
173 1.2 thorpej ATF_TC(modify);
174 1.2 thorpej ATF_TC_HEAD(modify, tc)
175 1.2 thorpej {
176 1.2 thorpej atf_tc_set_md_var(tc, "descr",
177 1.2 thorpej "tests modifying a timer");
178 1.2 thorpej }
179 1.2 thorpej
180 1.2 thorpej ATF_TC_BODY(modify, tc)
181 1.2 thorpej {
182 1.2 thorpej struct kevent event[1];
183 1.2 thorpej struct timespec ts = { 0, 0 };
184 1.2 thorpej struct timespec sleepts;
185 1.2 thorpej int kq;
186 1.2 thorpej
187 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
188 1.2 thorpej
189 1.2 thorpej /*
190 1.2 thorpej * Start a 500ms timer, sleep for 5 seconds, and check
191 1.2 thorpej * the total count.
192 1.2 thorpej */
193 1.2 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, 500, NULL);
194 1.2 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
195 1.2 thorpej
196 1.2 thorpej sleepts.tv_sec = 5;
197 1.2 thorpej sleepts.tv_nsec = 0;
198 1.2 thorpej ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
199 1.2 thorpej
200 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
201 1.2 thorpej ATF_REQUIRE(event[0].ident == 1);
202 1.2 thorpej ATF_REQUIRE(event[0].data >= 9 && event[0].data <= 11);
203 1.2 thorpej
204 1.2 thorpej /*
205 1.2 thorpej * Modify to a 4 second timer, sleep for 5 seconds, and check
206 1.2 thorpej * the total count.
207 1.2 thorpej */
208 1.2 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, 4000, NULL);
209 1.2 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
210 1.2 thorpej
211 1.3 thorpej /*
212 1.3 thorpej * Before we sleep, verify that the knote for this timer is
213 1.3 thorpej * no longer activated.
214 1.3 thorpej */
215 1.3 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 0);
216 1.3 thorpej
217 1.2 thorpej sleepts.tv_sec = 5;
218 1.2 thorpej sleepts.tv_nsec = 0;
219 1.2 thorpej ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
220 1.2 thorpej
221 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
222 1.2 thorpej ATF_REQUIRE(event[0].ident == 1);
223 1.2 thorpej ATF_REQUIRE(event[0].data == 1);
224 1.2 thorpej
225 1.2 thorpej /*
226 1.2 thorpej * Start a 500ms timer, sleep for 2 seconds.
227 1.2 thorpej */
228 1.2 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, 500, NULL);
229 1.2 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
230 1.2 thorpej
231 1.2 thorpej sleepts.tv_sec = 2;
232 1.2 thorpej sleepts.tv_nsec = 0;
233 1.2 thorpej ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
234 1.2 thorpej
235 1.2 thorpej /*
236 1.2 thorpej * Set the SAME timer, sleep for 2 seconds.
237 1.2 thorpej */
238 1.2 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, 500, NULL);
239 1.2 thorpej ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
240 1.2 thorpej
241 1.2 thorpej sleepts.tv_sec = 2;
242 1.2 thorpej sleepts.tv_nsec = 0;
243 1.2 thorpej ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
244 1.2 thorpej
245 1.2 thorpej /*
246 1.2 thorpej * The kernel should have reset the count when modifying the
247 1.2 thorpej * timer, so we should only expect to see the expiration count
248 1.2 thorpej * for the second sleep.
249 1.2 thorpej */
250 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
251 1.2 thorpej ATF_REQUIRE(event[0].ident == 1);
252 1.2 thorpej ATF_REQUIRE(event[0].data >= 3 && event[0].data <= 5);
253 1.2 thorpej }
254 1.2 thorpej
255 1.1 thorpej ATF_TC(abstime);
256 1.1 thorpej ATF_TC_HEAD(abstime, tc)
257 1.1 thorpej {
258 1.1 thorpej atf_tc_set_md_var(tc, "descr",
259 1.1 thorpej "tests timers with NOTE_ABSTIME");
260 1.1 thorpej }
261 1.1 thorpej
262 1.1 thorpej ATF_TC_BODY(abstime, tc)
263 1.1 thorpej {
264 1.1 thorpej struct kevent event[1];
265 1.1 thorpej struct timespec ts, ots;
266 1.1 thorpej time_t seconds;
267 1.1 thorpej int kq;
268 1.1 thorpej
269 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
270 1.1 thorpej
271 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ots) == 0);
272 1.1 thorpej ATF_REQUIRE(ots.tv_sec < INTPTR_MAX - TIME1_TOTAL_SEC);
273 1.1 thorpej
274 1.1 thorpej seconds = ots.tv_sec + TIME1_TOTAL_SEC;
275 1.1 thorpej if (ots.tv_nsec >= 500000000) {
276 1.1 thorpej seconds++;
277 1.1 thorpej }
278 1.1 thorpej
279 1.1 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD,
280 1.1 thorpej NOTE_ABSTIME | NOTE_SECONDS, seconds, NULL);
281 1.1 thorpej ATF_REQUIRE(kevent(kq, event, 1, event, 1, NULL) == 1);
282 1.1 thorpej
283 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts) == 0);
284 1.1 thorpej timespecsub(&ts, &ots, &ts);
285 1.1 thorpej
286 1.1 thorpej /*
287 1.1 thorpej * We're not going for precision here; just verify that it was
288 1.1 thorpej * delivered anywhere between 4.5-6.whatever seconds later.
289 1.1 thorpej */
290 1.1 thorpej ATF_REQUIRE(ts.tv_sec >= 4 && ts.tv_sec <= 6);
291 1.1 thorpej if (ts.tv_sec == 4) {
292 1.1 thorpej ATF_REQUIRE(ts.tv_nsec >= 500000000);
293 1.1 thorpej }
294 1.1 thorpej
295 1.1 thorpej ts.tv_sec = 0;
296 1.1 thorpej ts.tv_nsec = 0;
297 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej #define PREC_TIMEOUT_SEC 2
301 1.1 thorpej
302 1.1 thorpej static void
303 1.1 thorpej do_test_timer_units(const char *which, uint32_t fflag, int64_t data)
304 1.1 thorpej {
305 1.1 thorpej struct kevent event[1];
306 1.1 thorpej struct timespec ts, ots;
307 1.1 thorpej int kq;
308 1.1 thorpej
309 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
310 1.1 thorpej
311 1.1 thorpej EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD | EV_ONESHOT,
312 1.1 thorpej fflag, data, NULL);
313 1.1 thorpej
314 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
315 1.1 thorpej ATF_REQUIRE(kevent(kq, event, 1, event, 1, NULL) == 1);
316 1.1 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
317 1.1 thorpej
318 1.1 thorpej timespecsub(&ts, &ots, &ts);
319 1.1 thorpej ATF_REQUIRE_MSG(ts.tv_sec == (PREC_TIMEOUT_SEC - 1) ||
320 1.1 thorpej ts.tv_sec == PREC_TIMEOUT_SEC,
321 1.1 thorpej "units '%s' failed [sec]", which);
322 1.1 thorpej if (ts.tv_sec == PREC_TIMEOUT_SEC - 1) {
323 1.1 thorpej ATF_REQUIRE_MSG(ts.tv_nsec >= 900000000,
324 1.1 thorpej "units '%s' failed [nsec]", which);
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej (void)close(kq);
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej #define test_timer_units(fflag, data) \
331 1.1 thorpej do_test_timer_units(#fflag, fflag, data)
332 1.1 thorpej
333 1.1 thorpej ATF_TC(timer_units);
334 1.1 thorpej ATF_TC_HEAD(timer_units, tc)
335 1.1 thorpej {
336 1.1 thorpej atf_tc_set_md_var(tc, "descr",
337 1.1 thorpej "tests timers with NOTE_* units modifiers");
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej ATF_TC_BODY(timer_units, tc)
341 1.1 thorpej {
342 1.1 thorpej test_timer_units(NOTE_SECONDS, PREC_TIMEOUT_SEC);
343 1.1 thorpej test_timer_units(NOTE_MSECONDS, PREC_TIMEOUT_SEC * 1000);
344 1.1 thorpej test_timer_units(NOTE_USECONDS, PREC_TIMEOUT_SEC * 1000000);
345 1.1 thorpej test_timer_units(NOTE_NSECONDS, PREC_TIMEOUT_SEC * 1000000000);
346 1.1 thorpej }
347 1.1 thorpej
348 1.1 thorpej ATF_TP_ADD_TCS(tp)
349 1.1 thorpej {
350 1.1 thorpej ATF_TP_ADD_TC(tp, basic_timer);
351 1.1 thorpej ATF_TP_ADD_TC(tp, count_expirations);
352 1.1 thorpej ATF_TP_ADD_TC(tp, abstime);
353 1.1 thorpej ATF_TP_ADD_TC(tp, timer_units);
354 1.2 thorpej ATF_TP_ADD_TC(tp, modify);
355 1.1 thorpej
356 1.1 thorpej return atf_no_error();
357 1.1 thorpej }
358