t_sleep.c revision 1.12 1 1.12 riastrad /* $NetBSD: t_sleep.c,v 1.12 2025/04/06 14:24:23 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.1 pgoyette EV_SET(&ktimer, 1, EVFILT_TIMER, EV_ADD, 0, tmo, 0);
169 1.1 pgoyette
170 1.1 pgoyette rtc = kevent(kq, &ktimer, 1, &kresult, 1, delay);
171 1.1 pgoyette kerrno = errno;
172 1.1 pgoyette
173 1.1 pgoyette (void)close(kq);
174 1.1 pgoyette
175 1.4 pgoyette if (rtc == -1) {
176 1.9 kre ATF_REQUIRE_MSG(kerrno == EINTR, "kevent: %s",
177 1.9 kre strerror(kerrno));
178 1.4 pgoyette return 0;
179 1.4 pgoyette }
180 1.1 pgoyette
181 1.3 pgoyette if (delay->tv_sec * BILLION + delay->tv_nsec > tmo * MILLION)
182 1.3 pgoyette ATF_REQUIRE_MSG(rtc > 0,
183 1.5 pgoyette "kevent: KEVNT_TIMEOUT did not cause EVFILT_TIMER event");
184 1.1 pgoyette
185 1.1 pgoyette return 0;
186 1.1 pgoyette }
187 1.1 pgoyette
188 1.1 pgoyette ATF_TC(nanosleep);
189 1.12 riastrad ATF_TC_HEAD(nanosleep, tc)
190 1.1 pgoyette {
191 1.12 riastrad
192 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test nanosleep(2) timing");
193 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
194 1.12 riastrad }
195 1.1 pgoyette
196 1.1 pgoyette ATF_TC_BODY(nanosleep, tc)
197 1.1 pgoyette {
198 1.1 pgoyette
199 1.1 pgoyette sleeptest(do_nanosleep, true, false);
200 1.1 pgoyette }
201 1.1 pgoyette
202 1.1 pgoyette ATF_TC(select);
203 1.12 riastrad ATF_TC_HEAD(select, tc)
204 1.1 pgoyette {
205 1.12 riastrad
206 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test select(2) timing");
207 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
208 1.12 riastrad }
209 1.1 pgoyette
210 1.1 pgoyette ATF_TC_BODY(select, tc)
211 1.1 pgoyette {
212 1.1 pgoyette
213 1.1 pgoyette sleeptest(do_select, true, true);
214 1.1 pgoyette }
215 1.1 pgoyette
216 1.1 pgoyette ATF_TC(poll);
217 1.12 riastrad ATF_TC_HEAD(poll, tc)
218 1.1 pgoyette {
219 1.12 riastrad
220 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test poll(2) timing");
221 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
222 1.12 riastrad }
223 1.1 pgoyette
224 1.1 pgoyette ATF_TC_BODY(poll, tc)
225 1.1 pgoyette {
226 1.1 pgoyette
227 1.1 pgoyette sleeptest(do_poll, true, true);
228 1.1 pgoyette }
229 1.1 pgoyette
230 1.1 pgoyette ATF_TC(sleep);
231 1.12 riastrad ATF_TC_HEAD(sleep, tc)
232 1.1 pgoyette {
233 1.12 riastrad
234 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test sleep(3) timing");
235 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
236 1.12 riastrad }
237 1.1 pgoyette
238 1.1 pgoyette ATF_TC_BODY(sleep, tc)
239 1.1 pgoyette {
240 1.1 pgoyette
241 1.1 pgoyette sleeptest(do_sleep, false, false);
242 1.1 pgoyette }
243 1.1 pgoyette
244 1.1 pgoyette ATF_TC(kevent);
245 1.12 riastrad ATF_TC_HEAD(kevent, tc)
246 1.1 pgoyette {
247 1.12 riastrad
248 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test kevent(2) timing");
249 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "65");
250 1.12 riastrad }
251 1.1 pgoyette
252 1.1 pgoyette ATF_TC_BODY(kevent, tc)
253 1.1 pgoyette {
254 1.1 pgoyette
255 1.1 pgoyette sleeptest(do_kevent, true, true);
256 1.1 pgoyette }
257 1.1 pgoyette
258 1.1 pgoyette int
259 1.1 pgoyette sleeptest(int (*test)(struct timespec *, struct timespec *),
260 1.1 pgoyette bool subsec, bool sim_remain)
261 1.1 pgoyette {
262 1.1 pgoyette struct timespec tsa, tsb, tslp, tremain;
263 1.1 pgoyette int64_t delta1, delta2, delta3, round;
264 1.1 pgoyette
265 1.1 pgoyette sig = 0;
266 1.1 pgoyette signal(SIGALRM, sigalrm);
267 1.1 pgoyette
268 1.1 pgoyette if (subsec) {
269 1.1 pgoyette round = 1;
270 1.1 pgoyette delta3 = FUZZ;
271 1.1 pgoyette } else {
272 1.1 pgoyette round = 1000000000;
273 1.1 pgoyette delta3 = round;
274 1.1 pgoyette }
275 1.1 pgoyette
276 1.1 pgoyette tslp.tv_sec = delta3 / 1000000000;
277 1.1 pgoyette tslp.tv_nsec = delta3 % 1000000000;
278 1.1 pgoyette
279 1.5 pgoyette while (tslp.tv_sec <= MAXSLEEP) {
280 1.1 pgoyette /*
281 1.1 pgoyette * disturb sleep by signal on purpose
282 1.12 riastrad */
283 1.5 pgoyette if (tslp.tv_sec > ALARM && sig == 0)
284 1.1 pgoyette alarm(ALARM);
285 1.1 pgoyette
286 1.1 pgoyette clock_gettime(CLOCK_REALTIME, &tsa);
287 1.1 pgoyette (*test)(&tslp, &tremain);
288 1.1 pgoyette clock_gettime(CLOCK_REALTIME, &tsb);
289 1.1 pgoyette
290 1.1 pgoyette if (sim_remain) {
291 1.1 pgoyette timespecsub(&tsb, &tsa, &tremain);
292 1.1 pgoyette timespecsub(&tslp, &tremain, &tremain);
293 1.1 pgoyette }
294 1.1 pgoyette
295 1.1 pgoyette delta1 = (int64_t)tsb.tv_sec - (int64_t)tsa.tv_sec;
296 1.1 pgoyette delta1 *= BILLION;
297 1.1 pgoyette delta1 += (int64_t)tsb.tv_nsec - (int64_t)tsa.tv_nsec;
298 1.1 pgoyette
299 1.1 pgoyette delta2 = (int64_t)tremain.tv_sec * BILLION;
300 1.1 pgoyette delta2 += (int64_t)tremain.tv_nsec;
301 1.1 pgoyette
302 1.1 pgoyette delta3 = (int64_t)tslp.tv_sec * BILLION;
303 1.1 pgoyette delta3 += (int64_t)tslp.tv_nsec - delta1 - delta2;
304 1.1 pgoyette
305 1.1 pgoyette delta3 /= round;
306 1.1 pgoyette delta3 *= round;
307 1.1 pgoyette
308 1.4 pgoyette if (delta3 > FUZZ || delta3 < -FUZZ) {
309 1.6 jmmv if (!sim_remain)
310 1.4 pgoyette atf_tc_expect_fail("Long reschedule latency "
311 1.4 pgoyette "due to PR kern/43997");
312 1.1 pgoyette
313 1.4 pgoyette atf_tc_fail("Reschedule latency %"PRId64" exceeds "
314 1.4 pgoyette "allowable fuzz %lld", delta3, FUZZ);
315 1.4 pgoyette }
316 1.1 pgoyette delta3 = (int64_t)tslp.tv_sec * 2 * BILLION;
317 1.1 pgoyette delta3 += (int64_t)tslp.tv_nsec * 2;
318 1.1 pgoyette
319 1.1 pgoyette delta3 /= round;
320 1.1 pgoyette delta3 *= round;
321 1.1 pgoyette if (delta3 < FUZZ)
322 1.1 pgoyette break;
323 1.1 pgoyette tslp.tv_sec = delta3 / BILLION;
324 1.1 pgoyette tslp.tv_nsec = delta3 % BILLION;
325 1.1 pgoyette }
326 1.1 pgoyette ATF_REQUIRE_MSG(sig == 1, "Alarm did not fire!");
327 1.1 pgoyette
328 1.1 pgoyette atf_tc_pass();
329 1.1 pgoyette }
330 1.1 pgoyette
331 1.12 riastrad ATF_TP_ADD_TCS(tp)
332 1.1 pgoyette {
333 1.1 pgoyette ATF_TP_ADD_TC(tp, nanosleep);
334 1.1 pgoyette ATF_TP_ADD_TC(tp, select);
335 1.12 riastrad ATF_TP_ADD_TC(tp, poll);
336 1.1 pgoyette ATF_TP_ADD_TC(tp, sleep);
337 1.1 pgoyette ATF_TP_ADD_TC(tp, kevent);
338 1.12 riastrad
339 1.1 pgoyette return atf_no_error();
340 1.1 pgoyette }
341