1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_sleep.c,v 1.8 2014/07/15 14:56:34 gson Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2006 Frank Kardel
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #include <atf-c.h>
3011be35a1SLionel Sambuc #include <errno.h>
3111be35a1SLionel Sambuc #include <poll.h>
3211be35a1SLionel Sambuc #include <stdio.h>
3311be35a1SLionel Sambuc #include <stdlib.h>
3411be35a1SLionel Sambuc #include <string.h>
3511be35a1SLionel Sambuc #include <time.h>
3611be35a1SLionel Sambuc #include <unistd.h>
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc #include <sys/cdefs.h>
3911be35a1SLionel Sambuc #include <sys/event.h>
4011be35a1SLionel Sambuc #include <sys/signal.h>
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc #include "isqemu.h"
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #define BILLION 1000000000LL /* nano-seconds per second */
4511be35a1SLionel Sambuc #define MILLION 1000000LL /* nano-seconds per milli-second */
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc #define ALARM 6 /* SIGALRM after this many seconds */
4811be35a1SLionel Sambuc #define MAXSLEEP 22 /* Maximum delay in seconds */
4911be35a1SLionel Sambuc #define KEVNT_TIMEOUT 10300 /* measured in milli-seconds */
5011be35a1SLionel Sambuc #define FUZZ (40 * MILLION) /* scheduling fuzz accepted - 40 ms */
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc /*
5311be35a1SLionel Sambuc * Timer notes
5411be35a1SLionel Sambuc *
5511be35a1SLionel Sambuc * Most tests use FUZZ as their initial delay value, but 'sleep'
5611be35a1SLionel Sambuc * starts at 1sec (since it cannot handle sub-second intervals).
5711be35a1SLionel Sambuc * Subsequent passes double the previous interval, up to MAXSLEEP.
5811be35a1SLionel Sambuc *
5911be35a1SLionel Sambuc * The current values result in 5 passes for the 'sleep' test (at 1,
6011be35a1SLionel Sambuc * 2, 4, 8, and 16 seconds) and 10 passes for the other tests (at
6111be35a1SLionel Sambuc * 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, and 20.48
6211be35a1SLionel Sambuc * seconds).
6311be35a1SLionel Sambuc *
6411be35a1SLionel Sambuc * The ALARM is only set if the current pass's delay is longer, and
6511be35a1SLionel Sambuc * only if the ALARM has not already been triggered.
6611be35a1SLionel Sambuc *
6711be35a1SLionel Sambuc * The 'kevent' test needs the ALARM to be set on a different pass
6811be35a1SLionel Sambuc * from when the KEVNT_TIMEOUT fires. So set ALARM to fire on the
6911be35a1SLionel Sambuc * penultimate pass, and the KEVNT_TIMEOUT on the final pass. We
7011be35a1SLionel Sambuc * set KEVNT_TIMEOUT just barely long enough to put it into the
7111be35a1SLionel Sambuc * last test pass, and set MAXSLEEP a couple seconds longer than
7211be35a1SLionel Sambuc * necessary, in order to avoid a QEMU bug which nearly doubles
7311be35a1SLionel Sambuc * some timers.
7411be35a1SLionel Sambuc */
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc static volatile int sig;
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc int sleeptest(int (*)(struct timespec *, struct timespec *), bool, bool);
7911be35a1SLionel Sambuc int do_nanosleep(struct timespec *, struct timespec *);
8011be35a1SLionel Sambuc int do_select(struct timespec *, struct timespec *);
8111be35a1SLionel Sambuc int do_poll(struct timespec *, struct timespec *);
8211be35a1SLionel Sambuc int do_sleep(struct timespec *, struct timespec *);
8311be35a1SLionel Sambuc int do_kevent(struct timespec *, struct timespec *);
8411be35a1SLionel Sambuc void sigalrm(int);
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc void
sigalrm(int s)8711be35a1SLionel Sambuc sigalrm(int s)
8811be35a1SLionel Sambuc {
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc sig++;
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc int
do_nanosleep(struct timespec * delay,struct timespec * remain)9411be35a1SLionel Sambuc do_nanosleep(struct timespec *delay, struct timespec *remain)
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc int ret;
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambuc if (nanosleep(delay, remain) == -1)
9911be35a1SLionel Sambuc ret = (errno == EINTR ? 0 : errno);
10011be35a1SLionel Sambuc else
10111be35a1SLionel Sambuc ret = 0;
10211be35a1SLionel Sambuc return ret;
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc int
do_select(struct timespec * delay,struct timespec * remain)10611be35a1SLionel Sambuc do_select(struct timespec *delay, struct timespec *remain)
10711be35a1SLionel Sambuc {
10811be35a1SLionel Sambuc int ret;
10911be35a1SLionel Sambuc struct timeval tv;
11011be35a1SLionel Sambuc
11111be35a1SLionel Sambuc TIMESPEC_TO_TIMEVAL(&tv, delay);
11211be35a1SLionel Sambuc if (select(0, NULL, NULL, NULL, &tv) == -1)
11311be35a1SLionel Sambuc ret = (errno == EINTR ? 0 : errno);
11411be35a1SLionel Sambuc else
11511be35a1SLionel Sambuc ret = 0;
11611be35a1SLionel Sambuc return ret;
11711be35a1SLionel Sambuc }
11811be35a1SLionel Sambuc
11911be35a1SLionel Sambuc int
do_poll(struct timespec * delay,struct timespec * remain)12011be35a1SLionel Sambuc do_poll(struct timespec *delay, struct timespec *remain)
12111be35a1SLionel Sambuc {
12211be35a1SLionel Sambuc int ret;
12311be35a1SLionel Sambuc struct timeval tv;
12411be35a1SLionel Sambuc
12511be35a1SLionel Sambuc TIMESPEC_TO_TIMEVAL(&tv, delay);
12611be35a1SLionel Sambuc if (pollts(NULL, 0, delay, NULL) == -1)
12711be35a1SLionel Sambuc ret = (errno == EINTR ? 0 : errno);
12811be35a1SLionel Sambuc else
12911be35a1SLionel Sambuc ret = 0;
13011be35a1SLionel Sambuc return ret;
13111be35a1SLionel Sambuc }
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc int
do_sleep(struct timespec * delay,struct timespec * remain)13411be35a1SLionel Sambuc do_sleep(struct timespec *delay, struct timespec *remain)
13511be35a1SLionel Sambuc {
13611be35a1SLionel Sambuc struct timeval tv;
13711be35a1SLionel Sambuc
13811be35a1SLionel Sambuc TIMESPEC_TO_TIMEVAL(&tv, delay);
13911be35a1SLionel Sambuc remain->tv_sec = sleep(delay->tv_sec);
14011be35a1SLionel Sambuc remain->tv_nsec = 0;
14111be35a1SLionel Sambuc
14211be35a1SLionel Sambuc return 0;
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc
14511be35a1SLionel Sambuc int
do_kevent(struct timespec * delay,struct timespec * remain)14611be35a1SLionel Sambuc do_kevent(struct timespec *delay, struct timespec *remain)
14711be35a1SLionel Sambuc {
14811be35a1SLionel Sambuc struct kevent ktimer;
14911be35a1SLionel Sambuc struct kevent kresult;
15011be35a1SLionel Sambuc int rtc, kq, kerrno;
15111be35a1SLionel Sambuc int tmo;
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc ATF_REQUIRE_MSG((kq = kqueue()) != -1, "kqueue: %s", strerror(errno));
15411be35a1SLionel Sambuc
15511be35a1SLionel Sambuc tmo = KEVNT_TIMEOUT;
15611be35a1SLionel Sambuc
15711be35a1SLionel Sambuc /*
15811be35a1SLionel Sambuc * If we expect the KEVNT_TIMEOUT to fire, and we're running
15911be35a1SLionel Sambuc * under QEMU, make sure the delay is long enough to account
16011be35a1SLionel Sambuc * for the effects of PR kern/43997 !
16111be35a1SLionel Sambuc */
16211be35a1SLionel Sambuc if (isQEMU() &&
16311be35a1SLionel Sambuc tmo/1000 < delay->tv_sec && tmo/500 > delay->tv_sec)
16411be35a1SLionel Sambuc delay->tv_sec = MAXSLEEP;
16511be35a1SLionel Sambuc
16611be35a1SLionel Sambuc EV_SET(&ktimer, 1, EVFILT_TIMER, EV_ADD, 0, tmo, 0);
16711be35a1SLionel Sambuc
16811be35a1SLionel Sambuc rtc = kevent(kq, &ktimer, 1, &kresult, 1, delay);
16911be35a1SLionel Sambuc kerrno = errno;
17011be35a1SLionel Sambuc
17111be35a1SLionel Sambuc (void)close(kq);
17211be35a1SLionel Sambuc
17311be35a1SLionel Sambuc if (rtc == -1) {
17411be35a1SLionel Sambuc ATF_REQUIRE_MSG(kerrno == EINTR, "kevent: %s", strerror(errno));
17511be35a1SLionel Sambuc return 0;
17611be35a1SLionel Sambuc }
17711be35a1SLionel Sambuc
17811be35a1SLionel Sambuc if (delay->tv_sec * BILLION + delay->tv_nsec > tmo * MILLION)
17911be35a1SLionel Sambuc ATF_REQUIRE_MSG(rtc > 0,
18011be35a1SLionel Sambuc "kevent: KEVNT_TIMEOUT did not cause EVFILT_TIMER event");
18111be35a1SLionel Sambuc
18211be35a1SLionel Sambuc return 0;
18311be35a1SLionel Sambuc }
18411be35a1SLionel Sambuc
18511be35a1SLionel Sambuc ATF_TC(nanosleep);
ATF_TC_HEAD(nanosleep,tc)18611be35a1SLionel Sambuc ATF_TC_HEAD(nanosleep, tc)
18711be35a1SLionel Sambuc {
18811be35a1SLionel Sambuc
18911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test nanosleep(2) timing");
19011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "65");
19111be35a1SLionel Sambuc }
19211be35a1SLionel Sambuc
ATF_TC_BODY(nanosleep,tc)19311be35a1SLionel Sambuc ATF_TC_BODY(nanosleep, tc)
19411be35a1SLionel Sambuc {
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc sleeptest(do_nanosleep, true, false);
19711be35a1SLionel Sambuc }
19811be35a1SLionel Sambuc
19911be35a1SLionel Sambuc ATF_TC(select);
ATF_TC_HEAD(select,tc)20011be35a1SLionel Sambuc ATF_TC_HEAD(select, tc)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc
20311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test select(2) timing");
20411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "65");
20511be35a1SLionel Sambuc }
20611be35a1SLionel Sambuc
ATF_TC_BODY(select,tc)20711be35a1SLionel Sambuc ATF_TC_BODY(select, tc)
20811be35a1SLionel Sambuc {
20911be35a1SLionel Sambuc
21011be35a1SLionel Sambuc sleeptest(do_select, true, true);
21111be35a1SLionel Sambuc }
21211be35a1SLionel Sambuc
21311be35a1SLionel Sambuc ATF_TC(poll);
ATF_TC_HEAD(poll,tc)21411be35a1SLionel Sambuc ATF_TC_HEAD(poll, tc)
21511be35a1SLionel Sambuc {
21611be35a1SLionel Sambuc
21711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test poll(2) timing");
21811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "65");
21911be35a1SLionel Sambuc }
22011be35a1SLionel Sambuc
ATF_TC_BODY(poll,tc)22111be35a1SLionel Sambuc ATF_TC_BODY(poll, tc)
22211be35a1SLionel Sambuc {
22311be35a1SLionel Sambuc
22411be35a1SLionel Sambuc sleeptest(do_poll, true, true);
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc
22711be35a1SLionel Sambuc ATF_TC(sleep);
ATF_TC_HEAD(sleep,tc)22811be35a1SLionel Sambuc ATF_TC_HEAD(sleep, tc)
22911be35a1SLionel Sambuc {
23011be35a1SLionel Sambuc
23111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sleep(3) timing");
23211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "65");
23311be35a1SLionel Sambuc }
23411be35a1SLionel Sambuc
ATF_TC_BODY(sleep,tc)23511be35a1SLionel Sambuc ATF_TC_BODY(sleep, tc)
23611be35a1SLionel Sambuc {
23711be35a1SLionel Sambuc
23811be35a1SLionel Sambuc sleeptest(do_sleep, false, false);
23911be35a1SLionel Sambuc }
24011be35a1SLionel Sambuc
24111be35a1SLionel Sambuc ATF_TC(kevent);
ATF_TC_HEAD(kevent,tc)24211be35a1SLionel Sambuc ATF_TC_HEAD(kevent, tc)
24311be35a1SLionel Sambuc {
24411be35a1SLionel Sambuc
24511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test kevent(2) timing");
24611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "65");
24711be35a1SLionel Sambuc }
24811be35a1SLionel Sambuc
ATF_TC_BODY(kevent,tc)24911be35a1SLionel Sambuc ATF_TC_BODY(kevent, tc)
25011be35a1SLionel Sambuc {
25111be35a1SLionel Sambuc
25211be35a1SLionel Sambuc sleeptest(do_kevent, true, true);
25311be35a1SLionel Sambuc }
25411be35a1SLionel Sambuc
25511be35a1SLionel Sambuc int
sleeptest(int (* test)(struct timespec *,struct timespec *),bool subsec,bool sim_remain)25611be35a1SLionel Sambuc sleeptest(int (*test)(struct timespec *, struct timespec *),
25711be35a1SLionel Sambuc bool subsec, bool sim_remain)
25811be35a1SLionel Sambuc {
25911be35a1SLionel Sambuc struct timespec tsa, tsb, tslp, tremain;
26011be35a1SLionel Sambuc int64_t delta1, delta2, delta3, round;
26111be35a1SLionel Sambuc
26211be35a1SLionel Sambuc sig = 0;
26311be35a1SLionel Sambuc signal(SIGALRM, sigalrm);
26411be35a1SLionel Sambuc
26511be35a1SLionel Sambuc if (subsec) {
26611be35a1SLionel Sambuc round = 1;
26711be35a1SLionel Sambuc delta3 = FUZZ;
26811be35a1SLionel Sambuc } else {
26911be35a1SLionel Sambuc round = 1000000000;
27011be35a1SLionel Sambuc delta3 = round;
27111be35a1SLionel Sambuc }
27211be35a1SLionel Sambuc
27311be35a1SLionel Sambuc tslp.tv_sec = delta3 / 1000000000;
27411be35a1SLionel Sambuc tslp.tv_nsec = delta3 % 1000000000;
27511be35a1SLionel Sambuc
27611be35a1SLionel Sambuc while (tslp.tv_sec <= MAXSLEEP) {
27711be35a1SLionel Sambuc /*
27811be35a1SLionel Sambuc * disturb sleep by signal on purpose
27911be35a1SLionel Sambuc */
28011be35a1SLionel Sambuc if (tslp.tv_sec > ALARM && sig == 0)
28111be35a1SLionel Sambuc alarm(ALARM);
28211be35a1SLionel Sambuc
28311be35a1SLionel Sambuc clock_gettime(CLOCK_REALTIME, &tsa);
28411be35a1SLionel Sambuc (*test)(&tslp, &tremain);
28511be35a1SLionel Sambuc clock_gettime(CLOCK_REALTIME, &tsb);
28611be35a1SLionel Sambuc
28711be35a1SLionel Sambuc if (sim_remain) {
28811be35a1SLionel Sambuc timespecsub(&tsb, &tsa, &tremain);
28911be35a1SLionel Sambuc timespecsub(&tslp, &tremain, &tremain);
29011be35a1SLionel Sambuc }
29111be35a1SLionel Sambuc
29211be35a1SLionel Sambuc delta1 = (int64_t)tsb.tv_sec - (int64_t)tsa.tv_sec;
29311be35a1SLionel Sambuc delta1 *= BILLION;
29411be35a1SLionel Sambuc delta1 += (int64_t)tsb.tv_nsec - (int64_t)tsa.tv_nsec;
29511be35a1SLionel Sambuc
29611be35a1SLionel Sambuc delta2 = (int64_t)tremain.tv_sec * BILLION;
29711be35a1SLionel Sambuc delta2 += (int64_t)tremain.tv_nsec;
29811be35a1SLionel Sambuc
29911be35a1SLionel Sambuc delta3 = (int64_t)tslp.tv_sec * BILLION;
30011be35a1SLionel Sambuc delta3 += (int64_t)tslp.tv_nsec - delta1 - delta2;
30111be35a1SLionel Sambuc
30211be35a1SLionel Sambuc delta3 /= round;
30311be35a1SLionel Sambuc delta3 *= round;
30411be35a1SLionel Sambuc
30511be35a1SLionel Sambuc if (delta3 > FUZZ || delta3 < -FUZZ) {
30611be35a1SLionel Sambuc if (!sim_remain)
30711be35a1SLionel Sambuc atf_tc_expect_fail("Long reschedule latency "
30811be35a1SLionel Sambuc "due to PR kern/43997");
30911be35a1SLionel Sambuc
31011be35a1SLionel Sambuc atf_tc_fail("Reschedule latency %"PRId64" exceeds "
31111be35a1SLionel Sambuc "allowable fuzz %lld", delta3, FUZZ);
31211be35a1SLionel Sambuc }
31311be35a1SLionel Sambuc delta3 = (int64_t)tslp.tv_sec * 2 * BILLION;
31411be35a1SLionel Sambuc delta3 += (int64_t)tslp.tv_nsec * 2;
31511be35a1SLionel Sambuc
31611be35a1SLionel Sambuc delta3 /= round;
31711be35a1SLionel Sambuc delta3 *= round;
31811be35a1SLionel Sambuc if (delta3 < FUZZ)
31911be35a1SLionel Sambuc break;
32011be35a1SLionel Sambuc tslp.tv_sec = delta3 / BILLION;
32111be35a1SLionel Sambuc tslp.tv_nsec = delta3 % BILLION;
32211be35a1SLionel Sambuc }
32311be35a1SLionel Sambuc ATF_REQUIRE_MSG(sig == 1, "Alarm did not fire!");
32411be35a1SLionel Sambuc
32511be35a1SLionel Sambuc atf_tc_pass();
32611be35a1SLionel Sambuc }
32711be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)32811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
32911be35a1SLionel Sambuc {
33011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, nanosleep);
33111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, select);
33211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, poll);
33311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sleep);
33411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, kevent);
33511be35a1SLionel Sambuc
33611be35a1SLionel Sambuc return atf_no_error();
33711be35a1SLionel Sambuc }
338