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