1 /* $OpenBSD: t_clock_gettime.c,v 1.2 2021/09/02 12:40:44 mbuhl Exp $ */ 2 /* $NetBSD: t_clock_gettime.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 2008 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Frank Kardel. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 2006 Frank Kardel 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 56 * POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 #include "macros.h" 60 61 #include <sys/cdefs.h> 62 __COPYRIGHT("@(#) Copyright (c) 2008\ 63 The NetBSD Foundation, inc. All rights reserved."); 64 __RCSID("$NetBSD: t_clock_gettime.c,v 1.3 2017/01/13 21:30:41 christos Exp $"); 65 66 #include <sys/param.h> 67 #include <sys/sysctl.h> 68 69 70 #include "atf-c.h" 71 #include <errno.h> 72 #include <limits.h> 73 #include <stdio.h> 74 #include <stdint.h> 75 #include <stdlib.h> 76 #include <string.h> 77 #include <time.h> 78 #include <unistd.h> 79 80 #include "h_macros.h" 81 82 #define MINPOSDIFF 15000000 /* 15 ms for now */ 83 #define TIMEOUT 5 84 85 #define TC_HARDWARE "kern.timecounter.hardware" 86 #define TC_CHOICE "kern.timecounter.choice" 87 88 static void 89 check_timecounter(void) 90 { 91 struct timespec tsa, tsb, tsl, res; 92 long long mindiff = INTMAX_MAX; 93 time_t endlimit; 94 95 #define CL(x) \ 96 do { \ 97 if ((x) != -1) \ 98 break; \ 99 atf_tc_fail_nonfatal("%s: %s", #x, strerror(errno)); \ 100 return; \ 101 } while (0) 102 103 CL(clock_gettime(CLOCK_REALTIME, &tsa)); 104 tsl = tsa; 105 106 CL(time(&endlimit)); 107 endlimit += TIMEOUT + 1; 108 109 while ((time_t)tsa.tv_sec < endlimit) { 110 long long diff; 111 112 CL(clock_gettime(CLOCK_REALTIME, &tsb)); 113 diff = 1000000000LL * (tsb.tv_sec - tsa.tv_sec) 114 + tsb.tv_nsec - tsa.tv_nsec; 115 116 if (diff > 0 && mindiff > diff) 117 mindiff = diff; 118 119 if (diff < 0 || diff > MINPOSDIFF) { 120 long long elapsed; 121 (void)printf("%stime TSA: 0x%jx.%08jx, TSB: 0x%jx.%08jx, " 122 "diff = %lld nsec, ", (diff < 0) ? "BAD " : "", 123 (uintmax_t)tsa.tv_sec, (uintmax_t)tsa.tv_nsec, 124 (uintmax_t)tsb.tv_sec, (uintmax_t)tsb.tv_nsec, diff); 125 126 elapsed = 1000000000LL * (tsb.tv_sec - tsl.tv_sec) 127 + tsb.tv_nsec - tsl.tv_nsec; 128 129 130 (void)printf("%lld nsec\n", elapsed); 131 tsl = tsb; 132 133 ATF_CHECK(diff >= 0); 134 if (diff < 0) 135 return; 136 } 137 138 tsa.tv_sec = tsb.tv_sec; 139 tsa.tv_nsec = tsb.tv_nsec; 140 } 141 142 if (clock_getres(CLOCK_REALTIME, &res) == 0) { 143 long long r = res.tv_sec * 1000000000 + res.tv_nsec; 144 145 (void)printf("Claimed resolution: %lld nsec (%f Hz) or " 146 "better\n", r, 1.0 / r * 1e9); 147 (void)printf("Observed minimum non zero delta: %lld " 148 "nsec\n", mindiff); 149 } 150 151 #undef CL 152 } 153 154 ATF_TC(clock_gettime_real); 155 ATF_TC_HEAD(clock_gettime_real, tc) 156 { 157 atf_tc_set_md_var(tc, "require.user", "root"); 158 atf_tc_set_md_var(tc, "descr", 159 "Checks the monotonicity of the CLOCK_REALTIME implementation"); 160 atf_tc_set_md_var(tc, "timeout", "300"); 161 } 162 163 ATF_TC_BODY(clock_gettime_real, tc) 164 { 165 char name[128], cbuf[512], ctrbuf[10240]; 166 size_t cbufsiz = sizeof(cbuf); 167 size_t ctrbufsiz = sizeof(ctrbuf); 168 const char *p; 169 char *save; 170 int quality, n; 171 172 if (sysctlbyname(TC_HARDWARE, cbuf, &cbufsiz, NULL, 0) != 0) { 173 (void)printf("\nChecking legacy time implementation " 174 "for %d seconds\n", TIMEOUT); 175 check_timecounter(); 176 return; 177 /* NOTREACHED */ 178 } 179 (void)printf("%s = %s\n", TC_HARDWARE, cbuf); 180 REQUIRE_LIBC(save = strdup(cbuf), NULL); 181 182 RL(sysctlbyname(TC_CHOICE, ctrbuf, &ctrbufsiz, NULL, 0)); 183 (void)printf("%s = %s\n", TC_CHOICE, ctrbuf); 184 185 for (p = ctrbuf, n = 0; sscanf(p, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n", 186 name, &quality, &n) == 2; p += n) { 187 struct timespec ts; 188 int ret; 189 190 if (quality < 0) 191 continue; 192 193 (void)printf("\nChecking %s for %d seconds\n", name, TIMEOUT); 194 CHECK_LIBC(ret = sysctlbyname(TC_HARDWARE, NULL, 0, 195 name, strlen(name)), -1); 196 if (ret == -1) 197 continue; 198 199 /* wait a bit to select new counter in clockinterrupt */ 200 ts.tv_sec = 0; 201 ts.tv_nsec = 100000000; 202 (void)nanosleep(&ts, NULL); 203 204 check_timecounter(); 205 } 206 207 RL(sysctlbyname(TC_HARDWARE, NULL, 0, save, strlen(save))); 208 } 209 210 ATF_TP_ADD_TCS(tp) 211 { 212 213 ATF_TP_ADD_TC(tp, clock_gettime_real); 214 215 return atf_no_error(); 216 } 217