t_sleep.c revision 1.14 1 1.14 riastrad /* $NetBSD: t_sleep.c,v 1.14 2025/04/08 01:29:08 riastradh Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2006 Frank Kardel
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.10 christos #include <sys/cdefs.h>
30 1.10 christos #include <sys/event.h>
31 1.10 christos #include <sys/signal.h>
32 1.10 christos #include <sys/time.h> /* for TIMESPEC_TO_TIMEVAL on FreeBSD */
33 1.10 christos
34 1.1 pgoyette #include <atf-c.h>
35 1.1 pgoyette #include <errno.h>
36 1.11 maya #include <inttypes.h>
37 1.1 pgoyette #include <poll.h>
38 1.1 pgoyette #include <stdio.h>
39 1.1 pgoyette #include <stdlib.h>
40 1.1 pgoyette #include <string.h>
41 1.1 pgoyette #include <time.h>
42 1.1 pgoyette #include <unistd.h>
43 1.1 pgoyette
44 1.7 christos #include "isqemu.h"
45 1.7 christos
46 1.1 pgoyette #define BILLION 1000000000LL /* nano-seconds per second */
47 1.1 pgoyette #define MILLION 1000000LL /* nano-seconds per milli-second */
48 1.1 pgoyette
49 1.5 pgoyette #define ALARM 6 /* SIGALRM after this many seconds */
50 1.5 pgoyette #define MAXSLEEP 22 /* Maximum delay in seconds */
51 1.5 pgoyette #define KEVNT_TIMEOUT 10300 /* measured in milli-seconds */
52 1.4 pgoyette #define FUZZ (40 * MILLION) /* scheduling fuzz accepted - 40 ms */
53 1.4 pgoyette
54 1.4 pgoyette /*
55 1.4 pgoyette * Timer notes
56 1.4 pgoyette *
57 1.4 pgoyette * Most tests use FUZZ as their initial delay value, but 'sleep'
58 1.4 pgoyette * starts at 1sec (since it cannot handle sub-second intervals).
59 1.4 pgoyette * Subsequent passes double the previous interval, up to MAXSLEEP.
60 1.4 pgoyette *
61 1.5 pgoyette * The current values result in 5 passes for the 'sleep' test (at 1,
62 1.5 pgoyette * 2, 4, 8, and 16 seconds) and 10 passes for the other tests (at
63 1.5 pgoyette * 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, and 20.48
64 1.5 pgoyette * seconds).
65 1.4 pgoyette *
66 1.5 pgoyette * The ALARM is only set if the current pass's delay is longer, and
67 1.5 pgoyette * only if the ALARM has not already been triggered.
68 1.4 pgoyette *
69 1.5 pgoyette * The 'kevent' test needs the ALARM to be set on a different pass
70 1.5 pgoyette * from when the KEVNT_TIMEOUT fires. So set ALARM to fire on the
71 1.5 pgoyette * penultimate pass, and the KEVNT_TIMEOUT on the final pass. We
72 1.5 pgoyette * set KEVNT_TIMEOUT just barely long enough to put it into the
73 1.5 pgoyette * last test pass, and set MAXSLEEP a couple seconds longer than
74 1.8 gson * necessary, in order to avoid a QEMU bug which nearly doubles
75 1.5 pgoyette * some timers.
76 1.4 pgoyette */
77 1.4 pgoyette
78 1.4 pgoyette static volatile int sig;
79 1.1 pgoyette
80 1.1 pgoyette int sleeptest(int (*)(struct timespec *, struct timespec *), bool, bool);
81 1.1 pgoyette int do_nanosleep(struct timespec *, struct timespec *);
82 1.1 pgoyette int do_select(struct timespec *, struct timespec *);
83 1.1 pgoyette int do_poll(struct timespec *, struct timespec *);
84 1.1 pgoyette int do_sleep(struct timespec *, struct timespec *);
85 1.1 pgoyette int do_kevent(struct timespec *, struct timespec *);
86 1.1 pgoyette void sigalrm(int);
87 1.1 pgoyette
88 1.1 pgoyette void
89 1.1 pgoyette sigalrm(int s)
90 1.1 pgoyette {
91 1.4 pgoyette
92 1.1 pgoyette sig++;
93 1.1 pgoyette }
94 1.1 pgoyette
95 1.1 pgoyette int
96 1.1 pgoyette do_nanosleep(struct timespec *delay, struct timespec *remain)
97 1.1 pgoyette {
98 1.1 pgoyette int ret;
99 1.1 pgoyette
100 1.1 pgoyette if (nanosleep(delay, remain) == -1)
101 1.1 pgoyette ret = (errno == EINTR ? 0 : errno);
102 1.1 pgoyette else
103 1.1 pgoyette ret = 0;
104 1.1 pgoyette return ret;
105 1.1 pgoyette }
106 1.1 pgoyette
107 1.1 pgoyette int
108 1.1 pgoyette do_select(struct timespec *delay, struct timespec *remain)
109 1.1 pgoyette {
110 1.1 pgoyette int ret;
111 1.1 pgoyette struct timeval tv;
112 1.1 pgoyette
113 1.1 pgoyette TIMESPEC_TO_TIMEVAL(&tv, delay);
114 1.1 pgoyette if (select(0, NULL, NULL, NULL, &tv) == -1)
115 1.1 pgoyette ret = (errno == EINTR ? 0 : errno);
116 1.1 pgoyette else
117 1.1 pgoyette ret = 0;
118 1.1 pgoyette return ret;
119 1.1 pgoyette }
120 1.1 pgoyette
121 1.1 pgoyette int
122 1.1 pgoyette do_poll(struct timespec *delay, struct timespec *remain)
123 1.1 pgoyette {
124 1.1 pgoyette int ret;
125 1.1 pgoyette struct timeval tv;
126 1.1 pgoyette
127 1.1 pgoyette TIMESPEC_TO_TIMEVAL(&tv, delay);
128 1.1 pgoyette if (pollts(NULL, 0, delay, NULL) == -1)
129 1.1 pgoyette ret = (errno == EINTR ? 0 : errno);
130 1.1 pgoyette else
131 1.1 pgoyette ret = 0;
132 1.1 pgoyette return ret;
133 1.1 pgoyette }
134 1.1 pgoyette
135 1.1 pgoyette int
136 1.1 pgoyette do_sleep(struct timespec *delay, struct timespec *remain)
137 1.1 pgoyette {
138 1.1 pgoyette struct timeval tv;
139 1.1 pgoyette
140 1.1 pgoyette TIMESPEC_TO_TIMEVAL(&tv, delay);
141 1.1 pgoyette remain->tv_sec = sleep(delay->tv_sec);
142 1.1 pgoyette remain->tv_nsec = 0;
143 1.1 pgoyette
144 1.1 pgoyette return 0;
145 1.1 pgoyette }
146 1.1 pgoyette
147 1.1 pgoyette int
148 1.1 pgoyette do_kevent(struct timespec *delay, struct timespec *remain)
149 1.1 pgoyette {
150 1.1 pgoyette struct kevent ktimer;
151 1.1 pgoyette struct kevent kresult;
152 1.1 pgoyette int rtc, kq, kerrno;
153 1.4 pgoyette int tmo;
154 1.1 pgoyette
155 1.1 pgoyette ATF_REQUIRE_MSG((kq = kqueue()) != -1, "kqueue: %s", strerror(errno));
156 1.1 pgoyette
157 1.4 pgoyette tmo = KEVNT_TIMEOUT;
158 1.5 pgoyette
159 1.5 pgoyette /*
160 1.5 pgoyette * If we expect the KEVNT_TIMEOUT to fire, and we're running
161 1.5 pgoyette * under QEMU, make sure the delay is long enough to account
162 1.5 pgoyette * for the effects of PR kern/43997 !
163 1.5 pgoyette */
164 1.7 christos if (isQEMU() &&
165 1.5 pgoyette tmo/1000 < delay->tv_sec && tmo/500 > delay->tv_sec)
166 1.5 pgoyette delay->tv_sec = MAXSLEEP;
167 1.5 pgoyette
168 1.14 riastrad fprintf(stderr, "kevent: set EVFILT_TIMER tmo=%d\n", tmo);
169 1.1 pgoyette EV_SET(&ktimer, 1, EVFILT_TIMER, EV_ADD, 0, tmo, 0);
170 1.1 pgoyette
171 1.14 riastrad fprintf(stderr, "kevent: wait up to %lld.%09ld sec\n",
172 1.14 riastrad (long long)delay->tv_sec, (long)delay->tv_nsec);
173 1.1 pgoyette rtc = kevent(kq, &ktimer, 1, &kresult, 1, delay);
174 1.1 pgoyette kerrno = errno;
175 1.14 riastrad fprintf(stderr, "kevent returned rtc=%d\n", rtc);
176 1.1 pgoyette
177 1.1 pgoyette (void)close(kq);
178 1.1 pgoyette
179 1.4 pgoyette if (rtc == -1) {
180 1.9 kre ATF_REQUIRE_MSG(kerrno == EINTR, "kevent: %s",
181 1.9 kre strerror(kerrno));
182 1.4 pgoyette return 0;
183 1.4 pgoyette }
184 1.1 pgoyette
185 1.3 pgoyette if (delay->tv_sec * BILLION + delay->tv_nsec > tmo * MILLION)
186 1.14 riastrad ATF_CHECK_MSG(rtc > 0,
187 1.5 pgoyette "kevent: KEVNT_TIMEOUT did not cause EVFILT_TIMER event");
188 1.1 pgoyette
189 1.1 pgoyette return 0;
190 1.1 pgoyette }
191 1.1 pgoyette
192 1.1 pgoyette ATF_TC(nanosleep);
193 1.12 riastrad ATF_TC_HEAD(nanosleep, tc)
194 1.1 pgoyette {
195 1.12 riastrad
196 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test nanosleep(2) timing");
197 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
198 1.12 riastrad }
199 1.1 pgoyette
200 1.1 pgoyette ATF_TC_BODY(nanosleep, tc)
201 1.1 pgoyette {
202 1.1 pgoyette
203 1.1 pgoyette sleeptest(do_nanosleep, true, false);
204 1.1 pgoyette }
205 1.1 pgoyette
206 1.1 pgoyette ATF_TC(select);
207 1.12 riastrad ATF_TC_HEAD(select, tc)
208 1.1 pgoyette {
209 1.12 riastrad
210 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test select(2) timing");
211 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
212 1.12 riastrad }
213 1.1 pgoyette
214 1.1 pgoyette ATF_TC_BODY(select, tc)
215 1.1 pgoyette {
216 1.1 pgoyette
217 1.1 pgoyette sleeptest(do_select, true, true);
218 1.1 pgoyette }
219 1.1 pgoyette
220 1.1 pgoyette ATF_TC(poll);
221 1.12 riastrad ATF_TC_HEAD(poll, tc)
222 1.1 pgoyette {
223 1.12 riastrad
224 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test poll(2) timing");
225 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
226 1.12 riastrad }
227 1.1 pgoyette
228 1.1 pgoyette ATF_TC_BODY(poll, tc)
229 1.1 pgoyette {
230 1.1 pgoyette
231 1.1 pgoyette sleeptest(do_poll, true, true);
232 1.1 pgoyette }
233 1.1 pgoyette
234 1.1 pgoyette ATF_TC(sleep);
235 1.12 riastrad ATF_TC_HEAD(sleep, tc)
236 1.1 pgoyette {
237 1.12 riastrad
238 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test sleep(3) timing");
239 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
240 1.12 riastrad }
241 1.1 pgoyette
242 1.1 pgoyette ATF_TC_BODY(sleep, tc)
243 1.1 pgoyette {
244 1.1 pgoyette
245 1.1 pgoyette sleeptest(do_sleep, false, false);
246 1.1 pgoyette }
247 1.1 pgoyette
248 1.1 pgoyette ATF_TC(kevent);
249 1.12 riastrad ATF_TC_HEAD(kevent, tc)
250 1.1 pgoyette {
251 1.12 riastrad
252 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test kevent(2) timing");
253 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
254 1.12 riastrad }
255 1.1 pgoyette
256 1.1 pgoyette ATF_TC_BODY(kevent, tc)
257 1.1 pgoyette {
258 1.1 pgoyette
259 1.1 pgoyette sleeptest(do_kevent, true, true);
260 1.1 pgoyette }
261 1.1 pgoyette
262 1.1 pgoyette int
263 1.1 pgoyette sleeptest(int (*test)(struct timespec *, struct timespec *),
264 1.1 pgoyette bool subsec, bool sim_remain)
265 1.1 pgoyette {
266 1.1 pgoyette struct timespec tsa, tsb, tslp, tremain;
267 1.1 pgoyette int64_t delta1, delta2, delta3, round;
268 1.1 pgoyette
269 1.1 pgoyette sig = 0;
270 1.1 pgoyette signal(SIGALRM, sigalrm);
271 1.1 pgoyette
272 1.1 pgoyette if (subsec) {
273 1.1 pgoyette round = 1;
274 1.1 pgoyette delta3 = FUZZ;
275 1.1 pgoyette } else {
276 1.1 pgoyette round = 1000000000;
277 1.1 pgoyette delta3 = round;
278 1.1 pgoyette }
279 1.13 riastrad fprintf(stderr, "round=%"PRId64" delta3=%"PRId64"\n", round, delta3);
280 1.1 pgoyette
281 1.1 pgoyette tslp.tv_sec = delta3 / 1000000000;
282 1.1 pgoyette tslp.tv_nsec = delta3 % 1000000000;
283 1.13 riastrad fprintf(stderr, "initial tslp = %lld.%09ld sec\n",
284 1.13 riastrad (long long)tslp.tv_sec, (long)tslp.tv_nsec);
285 1.1 pgoyette
286 1.5 pgoyette while (tslp.tv_sec <= MAXSLEEP) {
287 1.13 riastrad fprintf(stderr, "\n");
288 1.13 riastrad
289 1.1 pgoyette /*
290 1.1 pgoyette * disturb sleep by signal on purpose
291 1.12 riastrad */
292 1.13 riastrad if (tslp.tv_sec > ALARM && sig == 0) {
293 1.13 riastrad fprintf(stderr, "request alarm after %d sec\n", ALARM);
294 1.1 pgoyette alarm(ALARM);
295 1.13 riastrad }
296 1.13 riastrad
297 1.13 riastrad fprintf(stderr, "sleep for %lld.%09ld sec\n",
298 1.13 riastrad (long long)tslp.tv_sec, (long)tslp.tv_nsec);
299 1.1 pgoyette
300 1.1 pgoyette clock_gettime(CLOCK_REALTIME, &tsa);
301 1.1 pgoyette (*test)(&tslp, &tremain);
302 1.1 pgoyette clock_gettime(CLOCK_REALTIME, &tsb);
303 1.1 pgoyette
304 1.13 riastrad fprintf(stderr, "slept from %lld.%09ld to %lld.%09ld\n",
305 1.13 riastrad (long long)tsa.tv_sec, (long)tsa.tv_nsec,
306 1.13 riastrad (long long)tsb.tv_sec, (long)tsb.tv_nsec);
307 1.13 riastrad
308 1.1 pgoyette if (sim_remain) {
309 1.1 pgoyette timespecsub(&tsb, &tsa, &tremain);
310 1.14 riastrad fprintf(stderr, "slept %lld.%09ld sec\n",
311 1.14 riastrad (long long)tremain.tv_sec, (long)tremain.tv_nsec);
312 1.1 pgoyette timespecsub(&tslp, &tremain, &tremain);
313 1.1 pgoyette }
314 1.1 pgoyette
315 1.13 riastrad fprintf(stderr, "remaining %lld.%09ld sec\n",
316 1.13 riastrad (long long)tremain.tv_sec, (long)tremain.tv_nsec);
317 1.13 riastrad
318 1.1 pgoyette delta1 = (int64_t)tsb.tv_sec - (int64_t)tsa.tv_sec;
319 1.1 pgoyette delta1 *= BILLION;
320 1.1 pgoyette delta1 += (int64_t)tsb.tv_nsec - (int64_t)tsa.tv_nsec;
321 1.1 pgoyette
322 1.13 riastrad fprintf(stderr, "delta1=%"PRId64"\n", delta1);
323 1.13 riastrad
324 1.1 pgoyette delta2 = (int64_t)tremain.tv_sec * BILLION;
325 1.1 pgoyette delta2 += (int64_t)tremain.tv_nsec;
326 1.1 pgoyette
327 1.13 riastrad fprintf(stderr, "delta2=%"PRId64"\n", delta2);
328 1.13 riastrad
329 1.1 pgoyette delta3 = (int64_t)tslp.tv_sec * BILLION;
330 1.1 pgoyette delta3 += (int64_t)tslp.tv_nsec - delta1 - delta2;
331 1.1 pgoyette
332 1.13 riastrad fprintf(stderr, "delta3=%"PRId64"\n", delta3);
333 1.13 riastrad
334 1.1 pgoyette delta3 /= round;
335 1.1 pgoyette delta3 *= round;
336 1.1 pgoyette
337 1.13 riastrad fprintf(stderr, " ->%"PRId64"\n", delta3);
338 1.13 riastrad
339 1.4 pgoyette if (delta3 > FUZZ || delta3 < -FUZZ) {
340 1.6 jmmv if (!sim_remain)
341 1.4 pgoyette atf_tc_expect_fail("Long reschedule latency "
342 1.4 pgoyette "due to PR kern/43997");
343 1.1 pgoyette
344 1.4 pgoyette atf_tc_fail("Reschedule latency %"PRId64" exceeds "
345 1.4 pgoyette "allowable fuzz %lld", delta3, FUZZ);
346 1.4 pgoyette }
347 1.1 pgoyette delta3 = (int64_t)tslp.tv_sec * 2 * BILLION;
348 1.1 pgoyette delta3 += (int64_t)tslp.tv_nsec * 2;
349 1.1 pgoyette
350 1.13 riastrad fprintf(stderr, "delta3=%"PRId64"\n", delta3);
351 1.13 riastrad
352 1.1 pgoyette delta3 /= round;
353 1.1 pgoyette delta3 *= round;
354 1.13 riastrad fprintf(stderr, " ->%"PRId64"\n", delta3);
355 1.1 pgoyette if (delta3 < FUZZ)
356 1.1 pgoyette break;
357 1.1 pgoyette tslp.tv_sec = delta3 / BILLION;
358 1.1 pgoyette tslp.tv_nsec = delta3 % BILLION;
359 1.13 riastrad fprintf(stderr, "tslp = %lld.%ld sec\n",
360 1.13 riastrad (long long)tslp.tv_sec, (long)tslp.tv_nsec);
361 1.1 pgoyette }
362 1.1 pgoyette ATF_REQUIRE_MSG(sig == 1, "Alarm did not fire!");
363 1.1 pgoyette
364 1.1 pgoyette atf_tc_pass();
365 1.1 pgoyette }
366 1.1 pgoyette
367 1.12 riastrad ATF_TP_ADD_TCS(tp)
368 1.1 pgoyette {
369 1.1 pgoyette ATF_TP_ADD_TC(tp, nanosleep);
370 1.1 pgoyette ATF_TP_ADD_TC(tp, select);
371 1.12 riastrad ATF_TP_ADD_TC(tp, poll);
372 1.1 pgoyette ATF_TP_ADD_TC(tp, sleep);
373 1.1 pgoyette ATF_TP_ADD_TC(tp, kevent);
374 1.12 riastrad
375 1.1 pgoyette return atf_no_error();
376 1.1 pgoyette }
377