Home | History | Annotate | Line # | Download | only in sys
t_clock_gettime.c revision 1.7
      1 /* $NetBSD: t_clock_gettime.c,v 1.7 2025/03/19 14:27:05 pho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank Kardel.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2006 Frank Kardel
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     55  * POSSIBILITY OF SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __COPYRIGHT("@(#) Copyright (c) 2008\
     60  The NetBSD Foundation, inc. All rights reserved.");
     61 __RCSID("$NetBSD: t_clock_gettime.c,v 1.7 2025/03/19 14:27:05 pho Exp $");
     62 
     63 #include <sys/param.h>
     64 
     65 #include <sys/ioctl.h>
     66 #include <sys/sysctl.h>
     67 
     68 #include <atf-c.h>
     69 #include <errno.h>
     70 #include <fcntl.h>
     71 #include <limits.h>
     72 #include <stdint.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <time.h>
     77 #include <unistd.h>
     78 
     79 #include "h_macros.h"
     80 
     81 #define MINPOSDIFF	15000000	/* 15 ms for now */
     82 #define TIMEOUT		5
     83 
     84 #define TC_HARDWARE	"kern.timecounter.hardware"
     85 #define TC_CHOICE	"kern.timecounter.choice"
     86 
     87 static void
     88 check_timecounter(void)
     89 {
     90 	struct timespec tsa, tsb, tsl, res;
     91 	long long mindiff = INTMAX_MAX;
     92 	time_t endlimit;
     93 
     94 #define CL(x) \
     95 	do { \
     96 		if ((x) != -1) \
     97 			break; \
     98 		atf_tc_fail_nonfatal("%s: %s", #x, strerror(errno)); \
     99 		return; \
    100 	} while (0)
    101 
    102 	CL(clock_gettime(CLOCK_REALTIME, &tsa));
    103 	tsl = tsa;
    104 
    105 	CL(time(&endlimit));
    106 	endlimit += TIMEOUT + 1;
    107 
    108 	while ((time_t)tsa.tv_sec < endlimit) {
    109 		long long diff;
    110 
    111 		CL(clock_gettime(CLOCK_REALTIME, &tsb));
    112 		diff = 1000000000LL * (tsb.tv_sec - tsa.tv_sec)
    113 		    + tsb.tv_nsec - tsa.tv_nsec;
    114 
    115 		if (diff > 0 && mindiff > diff)
    116 			mindiff = diff;
    117 
    118 		if (diff < 0 || diff > MINPOSDIFF) {
    119 			long long elapsed;
    120 			(void)printf("%stime TSA: 0x%jx.%08jx, TSB: 0x%jx.%08jx, "
    121 			    "diff = %lld nsec, ", (diff < 0) ? "BAD " : "",
    122 			    (uintmax_t)tsa.tv_sec, (uintmax_t)tsa.tv_nsec,
    123 			    (uintmax_t)tsb.tv_sec, (uintmax_t)tsb.tv_nsec, diff);
    124 
    125 			elapsed = 1000000000LL * (tsb.tv_sec - tsl.tv_sec)
    126 			    + tsb.tv_nsec - tsl.tv_nsec;
    127 
    128 
    129 			(void)printf("%lld nsec\n", elapsed);
    130 			tsl = tsb;
    131 
    132 			ATF_CHECK(diff >= 0);
    133 			if (diff < 0)
    134 				return;
    135 		}
    136 
    137 		tsa.tv_sec = tsb.tv_sec;
    138 		tsa.tv_nsec = tsb.tv_nsec;
    139 	}
    140 
    141 	if (clock_getres(CLOCK_REALTIME, &res) == 0) {
    142 		long long r = res.tv_sec * 1000000000 + res.tv_nsec;
    143 
    144 		(void)printf("Claimed resolution: %lld nsec (%f Hz) or "
    145 		    "better\n", r, 1.0 / r * 1e9);
    146 		(void)printf("Observed minimum non zero delta: %lld "
    147 		    "nsec\n", mindiff);
    148 	}
    149 
    150 #undef CL
    151 }
    152 
    153 ATF_TC(clock_gettime_real);
    154 ATF_TC_HEAD(clock_gettime_real, tc)
    155 {
    156 	atf_tc_set_md_var(tc, "require.user", "root");
    157 	atf_tc_set_md_var(tc, "descr",
    158 	    "Checks the monotonicity of the CLOCK_REALTIME implementation");
    159 	atf_tc_set_md_var(tc, "timeout", "300");
    160 }
    161 
    162 ATF_TC_BODY(clock_gettime_real, tc)
    163 {
    164 	char name[128], cbuf[512], ctrbuf[10240];
    165 	size_t cbufsiz = sizeof(cbuf);
    166 	size_t ctrbufsiz = sizeof(ctrbuf);
    167 	const char *p;
    168 	char *save;
    169 	int quality, n;
    170 
    171 	if (sysctlbyname(TC_HARDWARE, cbuf, &cbufsiz, NULL, 0) != 0) {
    172 		(void)printf("\nChecking legacy time implementation "
    173 		    "for %d seconds\n", TIMEOUT);
    174 		check_timecounter();
    175 		return;
    176 		/* NOTREACHED */
    177 	}
    178 	(void)printf("%s = %s\n", TC_HARDWARE, cbuf);
    179 	REQUIRE_LIBC(save = strdup(cbuf), NULL);
    180 
    181 	RL(sysctlbyname(TC_CHOICE, ctrbuf, &ctrbufsiz, NULL, 0));
    182 	(void)printf("%s = %s\n", TC_CHOICE, ctrbuf);
    183 
    184 	for (p = ctrbuf, n = 0; sscanf(p, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n",
    185 	    name, &quality, &n) == 2; p += n) {
    186 		struct timespec ts;
    187 		int ret;
    188 
    189 		if (quality < 0)
    190 			continue;
    191 
    192 		(void)printf("\nChecking %s for %d seconds\n", name, TIMEOUT);
    193 		CHECK_LIBC(ret = sysctlbyname(TC_HARDWARE, NULL, 0,
    194 		    name, strlen(name)), -1);
    195 		if (ret == -1)
    196 			continue;
    197 
    198 		/* wait a bit to select new counter in clockinterrupt */
    199 		ts.tv_sec = 0;
    200 		ts.tv_nsec = 100000000;
    201 		(void)nanosleep(&ts, NULL);
    202 
    203 		check_timecounter();
    204 	}
    205 
    206 	RL(sysctlbyname(TC_HARDWARE, NULL, 0, save, strlen(save)));
    207 }
    208 
    209 static void
    210 waste_user_time(void)
    211 {
    212 	static char buf[4*4096];
    213 
    214 	arc4random_buf(buf, sizeof(buf));
    215 }
    216 
    217 static void __unused
    218 waste_system_time(void)
    219 {
    220 	static char buf[4*4096];
    221 	int fd[2];
    222 	int i, n;
    223 
    224 	RL(pipe2(fd, O_NONBLOCK));
    225 	RL(n = ioctl(fd[1], FIONSPACE));
    226 	n = MIN((unsigned)MAX(0, n), sizeof(buf));
    227 	for (i = 0; i < 16; i++) {
    228 		RL(write(fd[1], buf, n));
    229 		RL(read(fd[0], buf, n));
    230 	}
    231 	RL(close(fd[0]));
    232 	RL(close(fd[1]));
    233 }
    234 
    235 static void
    236 check_monotonicity(const char *clockname, clockid_t clockid,
    237     void (*waste_time)(void))
    238 {
    239 	static const struct timespec maxtime = {5, 0};
    240 	struct timespec mono_t0, t0, mono_d;
    241 
    242 	RL(clock_gettime(CLOCK_MONOTONIC, &mono_t0));
    243 	RL(clock_gettime(clockid, &t0));
    244 
    245 	do {
    246 		struct timespec t1, mono_t1;
    247 
    248 		(*waste_time)();
    249 
    250 		RL(clock_gettime(clockid, &t1));
    251 		ATF_CHECK_MSG(timespeccmp(&t0, &t1, <=),
    252 		    "clock %s=0x%jx went backwards t0=%jd.%09ld t1=%jd.%09ld",
    253 		    clockname, (uintmax_t)clockid,
    254 		    (intmax_t)t0.tv_sec, t0.tv_nsec,
    255 		    (intmax_t)t1.tv_sec, t1.tv_nsec);
    256 
    257 		t0 = t1;
    258 
    259 		RL(clock_gettime(CLOCK_MONOTONIC, &mono_t1));
    260 		timespecsub(&mono_t1, &mono_t0, &mono_d);
    261 	} while (timespeccmp(&mono_d, &maxtime, <));
    262 }
    263 
    264 ATF_TC(clock_gettime_process_cputime_is_monotonic);
    265 ATF_TC_HEAD(clock_gettime_process_cputime_is_monotonic, tc)
    266 {
    267 	atf_tc_set_md_var(tc, "descr",
    268 	    "Checks that CLOCK_PROCESS_CPUTIME_ID is monotonic");
    269 }
    270 ATF_TC_BODY(clock_gettime_process_cputime_is_monotonic, tc)
    271 {
    272 	check_monotonicity("CLOCK_PROCESS_CPUTIME_ID",
    273 	    CLOCK_PROCESS_CPUTIME_ID, &waste_user_time);
    274 }
    275 
    276 ATF_TC(clock_gettime_thread_cputime_is_monotonic);
    277 ATF_TC_HEAD(clock_gettime_thread_cputime_is_monotonic, tc)
    278 {
    279 	atf_tc_set_md_var(tc, "descr",
    280 	    "Checks that CLOCK_THREAD_CPUTIME_ID is monotonic");
    281 }
    282 ATF_TC_BODY(clock_gettime_thread_cputime_is_monotonic, tc)
    283 {
    284 	check_monotonicity("CLOCK_THREAD_CPUTIME_ID",
    285 	    CLOCK_THREAD_CPUTIME_ID, &waste_user_time);
    286 }
    287 
    288 static void
    289 check_resolution(const char *clockname, clockid_t clockid)
    290 {
    291 	struct timespec ts;
    292 	int rv;
    293 
    294 	RLF(rv = clock_getres(clockid, &ts), "%s", clockname);
    295 	if (rv != -1) {
    296 		ATF_CHECK_MSG(ts.tv_sec == 0,
    297 		    "The resolution of the clock %s is reported as %jd.%09jd which is"
    298 		    " lower than a second; most likely an wrong value",
    299 		    clockname, ts.tv_sec, ts.tv_nsec);
    300 	}
    301 }
    302 
    303 ATF_TC(clock_getres);
    304 ATF_TC_HEAD(clock_getres, tc)
    305 {
    306 	atf_tc_set_md_var(tc, "descr",
    307 	    "Checks that clock_getres(2) returns some reasonable resolution for all supported clocks");
    308 }
    309 ATF_TC_BODY(clock_getres, tc)
    310 {
    311 	check_resolution("CLOCK_REALTIME", CLOCK_REALTIME);
    312 	check_resolution("CLOCK_MONOTONIC", CLOCK_MONOTONIC);
    313 	atf_tc_expect_fail("These clocks aren't supported but are documented in clock_gettime(2) for some reason");
    314 	check_resolution("CLOCK_VIRTUAL", CLOCK_VIRTUAL);
    315 	check_resolution("CLOCK_PROF", CLOCK_PROF);
    316 	atf_tc_expect_pass();
    317 	check_resolution("CLOCK_PROCESS_CPUTIME_ID", CLOCK_PROCESS_CPUTIME_ID);
    318 	check_resolution("CLOCK_THREAD_CPUTIME_ID", CLOCK_THREAD_CPUTIME_ID);
    319 }
    320 
    321 ATF_TP_ADD_TCS(tp)
    322 {
    323 
    324 	ATF_TP_ADD_TC(tp, clock_gettime_real);
    325 	ATF_TP_ADD_TC(tp, clock_gettime_process_cputime_is_monotonic);
    326 	ATF_TP_ADD_TC(tp, clock_gettime_thread_cputime_is_monotonic);
    327 	ATF_TP_ADD_TC(tp, clock_getres);
    328 
    329 	return atf_no_error();
    330 }
    331