t_sleep.c revision 1.3 1 /* $NetBSD: t_sleep.c,v 1.3 2012/11/08 16:33:26 pgoyette 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 <atf-c.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <poll.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38
39 #include <sys/cdefs.h>
40 #include <sys/event.h>
41 #include <sys/signal.h>
42
43 #define ALARM 11 /* SIGALRM after this many seconds */
44 #define KEVNT_TIMEOUT 13200 /* measured in milli-seconds */
45 #define BILLION 1000000000LL /* nano-seconds per second */
46 #define MILLION 1000000LL /* nano-seconds per milli-second */
47 #define FUZZ (50 * MILLION) /* scheduling fuzz accepted - 50 ms */
48 #define MAXSLEEP (32 * BILLION) /* 32 seconds */
49
50 static volatile int sig, sigs;
51
52 int sleeptest(int (*)(struct timespec *, struct timespec *), bool, bool);
53 int do_nanosleep(struct timespec *, struct timespec *);
54 int do_select(struct timespec *, struct timespec *);
55 int do_poll(struct timespec *, struct timespec *);
56 int do_sleep(struct timespec *, struct timespec *);
57 int do_kevent(struct timespec *, struct timespec *);
58 void sigalrm(int);
59
60 void
61 sigalrm(int s)
62 {
63 sig++;
64 }
65
66 int
67 do_nanosleep(struct timespec *delay, struct timespec *remain)
68 {
69 int ret;
70
71 if (nanosleep(delay, remain) == -1)
72 ret = (errno == EINTR ? 0 : errno);
73 else
74 ret = 0;
75 return ret;
76 }
77
78 int
79 do_select(struct timespec *delay, struct timespec *remain)
80 {
81 int ret;
82 struct timeval tv;
83
84 TIMESPEC_TO_TIMEVAL(&tv, delay);
85 if (select(0, NULL, NULL, NULL, &tv) == -1)
86 ret = (errno == EINTR ? 0 : errno);
87 else
88 ret = 0;
89 return ret;
90 }
91
92 int
93 do_poll(struct timespec *delay, struct timespec *remain)
94 {
95 int ret;
96 struct timeval tv;
97
98 TIMESPEC_TO_TIMEVAL(&tv, delay);
99 if (pollts(NULL, 0, delay, NULL) == -1)
100 ret = (errno == EINTR ? 0 : errno);
101 else
102 ret = 0;
103 return ret;
104 }
105
106 int
107 do_sleep(struct timespec *delay, struct timespec *remain)
108 {
109 struct timeval tv;
110
111 TIMESPEC_TO_TIMEVAL(&tv, delay);
112 remain->tv_sec = sleep(delay->tv_sec);
113 remain->tv_nsec = 0;
114
115 return 0;
116 }
117
118 int
119 do_kevent(struct timespec *delay, struct timespec *remain)
120 {
121 struct kevent ktimer;
122 struct kevent kresult;
123 int rtc, kq, kerrno;
124 int tmo = KEVNT_TIMEOUT;
125
126 ATF_REQUIRE_MSG((kq = kqueue()) != -1, "kqueue: %s", strerror(errno));
127
128 EV_SET(&ktimer, 1, EVFILT_TIMER, EV_ADD, 0, tmo, 0);
129
130 rtc = kevent(kq, &ktimer, 1, &kresult, 1, delay);
131 kerrno = errno;
132
133 (void)close(kq);
134
135 ATF_REQUIRE_MSG(rtc != -1 || kerrno == EINTR, "kevent: %s",
136 strerror(errno));
137
138 if (delay->tv_sec * BILLION + delay->tv_nsec > tmo * MILLION)
139 ATF_REQUIRE_MSG(rtc > 0,
140 "kevent: ALARM did not cause EVFILT_TIMER event");
141
142 return 0;
143 }
144
145 ATF_TC(nanosleep);
146 ATF_TC_HEAD(nanosleep, tc)
147 {
148
149 atf_tc_set_md_var(tc, "descr", "Test nanosleep(2) timing");
150 atf_tc_set_md_var(tc, "timeout", "65");
151 }
152
153 ATF_TC_BODY(nanosleep, tc)
154 {
155
156 sleeptest(do_nanosleep, true, false);
157 }
158
159 ATF_TC(select);
160 ATF_TC_HEAD(select, tc)
161 {
162
163 atf_tc_set_md_var(tc, "descr", "Test select(2) timing");
164 atf_tc_set_md_var(tc, "timeout", "65");
165 }
166
167 ATF_TC_BODY(select, tc)
168 {
169
170 sleeptest(do_select, true, true);
171 }
172
173 ATF_TC(poll);
174 ATF_TC_HEAD(poll, tc)
175 {
176
177 atf_tc_set_md_var(tc, "descr", "Test poll(2) timing");
178 atf_tc_set_md_var(tc, "timeout", "65");
179 }
180
181 ATF_TC_BODY(poll, tc)
182 {
183
184 sleeptest(do_poll, true, true);
185 }
186
187 ATF_TC(sleep);
188 ATF_TC_HEAD(sleep, tc)
189 {
190
191 atf_tc_set_md_var(tc, "descr", "Test sleep(3) timing");
192 atf_tc_set_md_var(tc, "timeout", "65");
193 }
194
195 ATF_TC_BODY(sleep, tc)
196 {
197
198 sleeptest(do_sleep, false, false);
199 }
200
201 ATF_TC(kevent);
202 ATF_TC_HEAD(kevent, tc)
203 {
204
205 atf_tc_set_md_var(tc, "descr", "Test kevent(2) timing");
206 atf_tc_set_md_var(tc, "timeout", "65");
207 }
208
209 ATF_TC_BODY(kevent, tc)
210 {
211
212 sleeptest(do_kevent, true, true);
213 }
214
215 int
216 sleeptest(int (*test)(struct timespec *, struct timespec *),
217 bool subsec, bool sim_remain)
218 {
219 struct timespec tsa, tsb, tslp, tremain;
220 int64_t delta1, delta2, delta3, round;
221
222 sig = 0;
223 signal(SIGALRM, sigalrm);
224
225 if (subsec) {
226 round = 1;
227 delta3 = FUZZ;
228 } else {
229 round = 1000000000;
230 delta3 = round;
231 }
232
233 tslp.tv_sec = delta3 / 1000000000;
234 tslp.tv_nsec = delta3 % 1000000000;
235
236 while (delta3 <= MAXSLEEP) {
237 /*
238 * disturb sleep by signal on purpose
239 */
240 if (delta3 > ALARM * BILLION && sig == 0)
241 alarm(ALARM);
242
243 clock_gettime(CLOCK_REALTIME, &tsa);
244 (*test)(&tslp, &tremain);
245 clock_gettime(CLOCK_REALTIME, &tsb);
246
247 if (sim_remain) {
248 timespecsub(&tsb, &tsa, &tremain);
249 timespecsub(&tslp, &tremain, &tremain);
250 }
251
252 delta1 = (int64_t)tsb.tv_sec - (int64_t)tsa.tv_sec;
253 delta1 *= BILLION;
254 delta1 += (int64_t)tsb.tv_nsec - (int64_t)tsa.tv_nsec;
255
256 delta2 = (int64_t)tremain.tv_sec * BILLION;
257 delta2 += (int64_t)tremain.tv_nsec;
258
259 delta3 = (int64_t)tslp.tv_sec * BILLION;
260 delta3 += (int64_t)tslp.tv_nsec - delta1 - delta2;
261
262 delta3 /= round;
263 delta3 *= round;
264
265 ATF_REQUIRE_MSG(delta3 <= FUZZ && delta3 >= -FUZZ,
266 "Reschedule latency %"PRId64" exceeds allowable fuzz %lld",
267 delta3, FUZZ);
268
269 delta3 = (int64_t)tslp.tv_sec * 2 * BILLION;
270 delta3 += (int64_t)tslp.tv_nsec * 2;
271
272 delta3 /= round;
273 delta3 *= round;
274 if (delta3 < FUZZ)
275 break;
276 tslp.tv_sec = delta3 / BILLION;
277 tslp.tv_nsec = delta3 % BILLION;
278 }
279 ATF_REQUIRE_MSG(sig == 1, "Alarm did not fire!");
280
281 atf_tc_pass();
282 }
283
284 ATF_TP_ADD_TCS(tp)
285 {
286 ATF_TP_ADD_TC(tp, nanosleep);
287 ATF_TP_ADD_TC(tp, select);
288 ATF_TP_ADD_TC(tp, poll);
289 ATF_TP_ADD_TC(tp, sleep);
290 ATF_TP_ADD_TC(tp, kevent);
291
292 return atf_no_error();
293 }
294