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