t_sleep.c revision 313632
1/* $NetBSD: t_sleep.c,v 1.11 2017/01/10 15:43:59 maya 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#ifdef __FreeBSD__
30/* kqueue(2) on FreeBSD requires sys/types.h for uintptr_t; NetBSD doesn't. */
31#include <sys/types.h>
32#endif
33#include <sys/cdefs.h>
34#include <sys/event.h>
35#include <sys/signal.h>
36#include <sys/time.h>		/* for TIMESPEC_TO_TIMEVAL on FreeBSD */
37
38#include <atf-c.h>
39#include <errno.h>
40#include <inttypes.h>
41#include <poll.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46#include <unistd.h>
47
48#include "isqemu.h"
49
50#define BILLION		1000000000LL	/* nano-seconds per second */
51#define MILLION		1000000LL	/* nano-seconds per milli-second */
52
53#define ALARM		6		/* SIGALRM after this many seconds */
54#define MAXSLEEP	22		/* Maximum delay in seconds */
55#define KEVNT_TIMEOUT	10300		/* measured in milli-seconds */
56#define FUZZ		(40 * MILLION)	/* scheduling fuzz accepted - 40 ms */
57
58/*
59 * Timer notes
60 *
61 * Most tests use FUZZ as their initial delay value, but 'sleep'
62 * starts at 1sec (since it cannot handle sub-second intervals).
63 * Subsequent passes double the previous interval, up to MAXSLEEP.
64 *
65 * The current values result in 5 passes for the 'sleep' test (at 1,
66 * 2, 4, 8, and 16 seconds) and 10 passes for the other tests (at
67 * 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, and 20.48
68 * seconds).
69 *
70 * The ALARM is only set if the current pass's delay is longer, and
71 * only if the ALARM has not already been triggered.
72 *
73 * The 'kevent' test needs the ALARM to be set on a different pass
74 * from when the KEVNT_TIMEOUT fires.  So set ALARM to fire on the
75 * penultimate pass, and the KEVNT_TIMEOUT on the final pass.  We
76 * set KEVNT_TIMEOUT just barely long enough to put it into the
77 * last test pass, and set MAXSLEEP a couple seconds longer than
78 * necessary, in order to avoid a QEMU bug which nearly doubles
79 * some timers.
80 */
81
82static volatile int sig;
83
84int sleeptest(int (*)(struct timespec *, struct timespec *), bool, bool);
85int do_nanosleep(struct timespec *, struct timespec *);
86int do_select(struct timespec *, struct timespec *);
87#ifdef __NetBSD__
88int do_poll(struct timespec *, struct timespec *);
89#endif
90int do_sleep(struct timespec *, struct timespec *);
91int do_kevent(struct timespec *, struct timespec *);
92void sigalrm(int);
93
94void
95sigalrm(int s)
96{
97
98	sig++;
99}
100
101int
102do_nanosleep(struct timespec *delay, struct timespec *remain)
103{
104	int ret;
105
106	if (nanosleep(delay, remain) == -1)
107		ret = (errno == EINTR ? 0 : errno);
108	else
109		ret = 0;
110	return ret;
111}
112
113int
114do_select(struct timespec *delay, struct timespec *remain)
115{
116	int ret;
117	struct timeval tv;
118
119	TIMESPEC_TO_TIMEVAL(&tv, delay);
120	if (select(0, NULL, NULL, NULL, &tv) == -1)
121		ret = (errno == EINTR ? 0 : errno);
122	else
123		ret = 0;
124	return ret;
125}
126
127#ifdef __NetBSD__
128int
129do_poll(struct timespec *delay, struct timespec *remain)
130{
131	int ret;
132	struct timeval tv;
133
134	TIMESPEC_TO_TIMEVAL(&tv, delay);
135	if (pollts(NULL, 0, delay, NULL) == -1)
136		ret = (errno == EINTR ? 0 : errno);
137	else
138		ret = 0;
139	return ret;
140}
141#endif
142
143int
144do_sleep(struct timespec *delay, struct timespec *remain)
145{
146	struct timeval tv;
147
148	TIMESPEC_TO_TIMEVAL(&tv, delay);
149	remain->tv_sec = sleep(delay->tv_sec);
150	remain->tv_nsec = 0;
151
152	return 0;
153}
154
155int
156do_kevent(struct timespec *delay, struct timespec *remain)
157{
158	struct kevent ktimer;
159	struct kevent kresult;
160	int rtc, kq, kerrno;
161	int tmo;
162
163	ATF_REQUIRE_MSG((kq = kqueue()) != -1, "kqueue: %s", strerror(errno));
164
165	tmo = KEVNT_TIMEOUT;
166
167	/*
168	 * If we expect the KEVNT_TIMEOUT to fire, and we're running
169	 * under QEMU, make sure the delay is long enough to account
170	 * for the effects of PR kern/43997 !
171	 */
172	if (isQEMU() &&
173	    tmo/1000 < delay->tv_sec && tmo/500 > delay->tv_sec)
174		delay->tv_sec = MAXSLEEP;
175
176	EV_SET(&ktimer, 1, EVFILT_TIMER, EV_ADD, 0, tmo, 0);
177
178	rtc = kevent(kq, &ktimer, 1, &kresult, 1, delay);
179	kerrno = errno;
180
181	(void)close(kq);
182
183	if (rtc == -1) {
184		ATF_REQUIRE_MSG(kerrno == EINTR, "kevent: %s",
185		    strerror(kerrno));
186		return 0;
187	}
188
189	if (delay->tv_sec * BILLION + delay->tv_nsec > tmo * MILLION)
190		ATF_REQUIRE_MSG(rtc > 0,
191		    "kevent: KEVNT_TIMEOUT did not cause EVFILT_TIMER event");
192
193	return 0;
194}
195
196ATF_TC(nanosleep);
197ATF_TC_HEAD(nanosleep, tc)
198{
199
200	atf_tc_set_md_var(tc, "descr", "Test nanosleep(2) timing");
201	atf_tc_set_md_var(tc, "timeout", "65");
202}
203
204ATF_TC_BODY(nanosleep, tc)
205{
206
207	sleeptest(do_nanosleep, true, false);
208}
209
210ATF_TC(select);
211ATF_TC_HEAD(select, tc)
212{
213
214	atf_tc_set_md_var(tc, "descr", "Test select(2) timing");
215	atf_tc_set_md_var(tc, "timeout", "65");
216}
217
218ATF_TC_BODY(select, tc)
219{
220
221	sleeptest(do_select, true, true);
222}
223
224#ifdef __NetBSD__
225ATF_TC(poll);
226ATF_TC_HEAD(poll, tc)
227{
228
229	atf_tc_set_md_var(tc, "descr", "Test poll(2) timing");
230	atf_tc_set_md_var(tc, "timeout", "65");
231}
232
233ATF_TC_BODY(poll, tc)
234{
235
236	sleeptest(do_poll, true, true);
237}
238#endif
239
240ATF_TC(sleep);
241ATF_TC_HEAD(sleep, tc)
242{
243
244	atf_tc_set_md_var(tc, "descr", "Test sleep(3) timing");
245	atf_tc_set_md_var(tc, "timeout", "65");
246}
247
248ATF_TC_BODY(sleep, tc)
249{
250
251	sleeptest(do_sleep, false, false);
252}
253
254ATF_TC(kevent);
255ATF_TC_HEAD(kevent, tc)
256{
257
258	atf_tc_set_md_var(tc, "descr", "Test kevent(2) timing");
259	atf_tc_set_md_var(tc, "timeout", "65");
260}
261
262ATF_TC_BODY(kevent, tc)
263{
264
265	sleeptest(do_kevent, true, true);
266}
267
268int
269sleeptest(int (*test)(struct timespec *, struct timespec *),
270	   bool subsec, bool sim_remain)
271{
272	struct timespec tsa, tsb, tslp, tremain;
273	int64_t delta1, delta2, delta3, round;
274
275	sig = 0;
276	signal(SIGALRM, sigalrm);
277
278	if (subsec) {
279		round = 1;
280		delta3 = FUZZ;
281	} else {
282		round = 1000000000;
283		delta3 = round;
284	}
285
286	tslp.tv_sec = delta3 / 1000000000;
287	tslp.tv_nsec = delta3 % 1000000000;
288
289	while (tslp.tv_sec <= MAXSLEEP) {
290		/*
291		 * disturb sleep by signal on purpose
292		 */
293		if (tslp.tv_sec > ALARM && sig == 0)
294			alarm(ALARM);
295
296		clock_gettime(CLOCK_REALTIME, &tsa);
297		(*test)(&tslp, &tremain);
298		clock_gettime(CLOCK_REALTIME, &tsb);
299
300		if (sim_remain) {
301			timespecsub(&tsb, &tsa, &tremain);
302			timespecsub(&tslp, &tremain, &tremain);
303		}
304
305		delta1 = (int64_t)tsb.tv_sec - (int64_t)tsa.tv_sec;
306		delta1 *= BILLION;
307		delta1 += (int64_t)tsb.tv_nsec - (int64_t)tsa.tv_nsec;
308
309		delta2 = (int64_t)tremain.tv_sec * BILLION;
310		delta2 += (int64_t)tremain.tv_nsec;
311
312		delta3 = (int64_t)tslp.tv_sec * BILLION;
313		delta3 += (int64_t)tslp.tv_nsec - delta1 - delta2;
314
315		delta3 /= round;
316		delta3 *= round;
317
318		if (delta3 > FUZZ || delta3 < -FUZZ) {
319			if (!sim_remain)
320				atf_tc_expect_fail("Long reschedule latency "
321				    "due to PR kern/43997");
322
323			atf_tc_fail("Reschedule latency %"PRId64" exceeds "
324			    "allowable fuzz %lld", delta3, FUZZ);
325		}
326		delta3 = (int64_t)tslp.tv_sec * 2 * BILLION;
327		delta3 += (int64_t)tslp.tv_nsec * 2;
328
329		delta3 /= round;
330		delta3 *= round;
331		if (delta3 < FUZZ)
332			break;
333		tslp.tv_sec = delta3 / BILLION;
334		tslp.tv_nsec = delta3 % BILLION;
335	}
336	ATF_REQUIRE_MSG(sig == 1, "Alarm did not fire!");
337
338	atf_tc_pass();
339}
340
341ATF_TP_ADD_TCS(tp)
342{
343	ATF_TP_ADD_TC(tp, nanosleep);
344	ATF_TP_ADD_TC(tp, select);
345#ifdef __NetBSD__
346	ATF_TP_ADD_TC(tp, poll);
347#endif
348	ATF_TP_ADD_TC(tp, sleep);
349	ATF_TP_ADD_TC(tp, kevent);
350
351	return atf_no_error();
352}
353