Home | History | Annotate | Line # | Download | only in sys
      1 /* $NetBSD: t_clock_gettime.c,v 1.10 2025/03/25 19:51:32 riastradh 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.10 2025/03/25 19:51:32 riastradh 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"
    121 			    " 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,
    125 			    diff);
    126 
    127 			elapsed = 1000000000LL * (tsb.tv_sec - tsl.tv_sec)
    128 			    + tsb.tv_nsec - tsl.tv_nsec;
    129 
    130 
    131 			(void)printf("%lld nsec\n", elapsed);
    132 			tsl = tsb;
    133 
    134 			ATF_CHECK(diff >= 0);
    135 			if (diff < 0)
    136 				return;
    137 		}
    138 
    139 		tsa.tv_sec = tsb.tv_sec;
    140 		tsa.tv_nsec = tsb.tv_nsec;
    141 	}
    142 
    143 	if (clock_getres(CLOCK_REALTIME, &res) == 0) {
    144 		long long r = res.tv_sec * 1000000000 + res.tv_nsec;
    145 
    146 		(void)printf("Claimed resolution: %lld nsec (%f Hz) or "
    147 		    "better\n", r, 1.0 / r * 1e9);
    148 		(void)printf("Observed minimum non zero delta: %lld "
    149 		    "nsec\n", mindiff);
    150 	}
    151 
    152 #undef CL
    153 }
    154 
    155 ATF_TC(clock_gettime_real);
    156 ATF_TC_HEAD(clock_gettime_real, tc)
    157 {
    158 	atf_tc_set_md_var(tc, "require.user", "root");
    159 	atf_tc_set_md_var(tc, "descr",
    160 	    "Checks the monotonicity of the CLOCK_REALTIME implementation");
    161 	atf_tc_set_md_var(tc, "timeout", "300");
    162 }
    163 
    164 ATF_TC_BODY(clock_gettime_real, tc)
    165 {
    166 	char name[128], cbuf[512], ctrbuf[10240];
    167 	size_t cbufsiz = sizeof(cbuf);
    168 	size_t ctrbufsiz = sizeof(ctrbuf);
    169 	const char *p;
    170 	char *save;
    171 	int quality, n;
    172 
    173 	if (sysctlbyname(TC_HARDWARE, cbuf, &cbufsiz, NULL, 0) != 0) {
    174 		(void)printf("\nChecking legacy time implementation "
    175 		    "for %d seconds\n", TIMEOUT);
    176 		check_timecounter();
    177 		return;
    178 		/* NOTREACHED */
    179 	}
    180 	(void)printf("%s = %s\n", TC_HARDWARE, cbuf);
    181 	REQUIRE_LIBC(save = strdup(cbuf), NULL);
    182 
    183 	RL(sysctlbyname(TC_CHOICE, ctrbuf, &ctrbufsiz, NULL, 0));
    184 	(void)printf("%s = %s\n", TC_CHOICE, ctrbuf);
    185 
    186 	for (p = ctrbuf, n = 0; sscanf(p, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n",
    187 	    name, &quality, &n) == 2; p += n) {
    188 		struct timespec ts;
    189 		int ret;
    190 
    191 		if (quality < 0)
    192 			continue;
    193 
    194 		(void)printf("\nChecking %s for %d seconds\n", name, TIMEOUT);
    195 		CHECK_LIBC(ret = sysctlbyname(TC_HARDWARE, NULL, 0,
    196 		    name, strlen(name)), -1);
    197 		if (ret == -1)
    198 			continue;
    199 
    200 		/* wait a bit to select new counter in clockinterrupt */
    201 		ts.tv_sec = 0;
    202 		ts.tv_nsec = 100000000;
    203 		(void)nanosleep(&ts, NULL);
    204 
    205 		check_timecounter();
    206 	}
    207 
    208 	RL(sysctlbyname(TC_HARDWARE, NULL, 0, save, strlen(save)));
    209 }
    210 
    211 static void
    212 waste_user_time(void)
    213 {
    214 	static char buf[4*4096];
    215 
    216 	arc4random_buf(buf, sizeof(buf));
    217 }
    218 
    219 static void __unused
    220 waste_system_time(void)
    221 {
    222 	static char buf[4*4096];
    223 	int fd[2];
    224 	int i, n;
    225 
    226 	RL(pipe2(fd, O_NONBLOCK));
    227 	RL(n = ioctl(fd[1], FIONSPACE));
    228 	n = MIN((unsigned)MAX(0, n), sizeof(buf));
    229 	for (i = 0; i < 16; i++) {
    230 		RL(write(fd[1], buf, n));
    231 		RL(read(fd[0], buf, n));
    232 	}
    233 	RL(close(fd[0]));
    234 	RL(close(fd[1]));
    235 }
    236 
    237 static void
    238 check_monotonicity(const char *clockname, clockid_t clockid,
    239     void (*waste_time)(void))
    240 {
    241 	static const struct timespec maxtime = {5, 0};
    242 	struct timespec mono_t0, t0, mono_d;
    243 
    244 	RL(clock_gettime(CLOCK_MONOTONIC, &mono_t0));
    245 	RL(clock_gettime(clockid, &t0));
    246 
    247 	do {
    248 		struct timespec t1, mono_t1;
    249 
    250 		(*waste_time)();
    251 
    252 		RL(clock_gettime(clockid, &t1));
    253 		ATF_CHECK_MSG(timespeccmp(&t0, &t1, <=),
    254 		    "clock %s=0x%jx went backwards t0=%jd.%09ld t1=%jd.%09ld",
    255 		    clockname, (uintmax_t)clockid,
    256 		    (intmax_t)t0.tv_sec, t0.tv_nsec,
    257 		    (intmax_t)t1.tv_sec, t1.tv_nsec);
    258 
    259 		t0 = t1;
    260 
    261 		RL(clock_gettime(CLOCK_MONOTONIC, &mono_t1));
    262 		timespecsub(&mono_t1, &mono_t0, &mono_d);
    263 	} while (timespeccmp(&mono_d, &maxtime, <));
    264 }
    265 
    266 ATF_TC(clock_gettime_process_cputime_is_monotonic);
    267 ATF_TC_HEAD(clock_gettime_process_cputime_is_monotonic, tc)
    268 {
    269 	atf_tc_set_md_var(tc, "descr",
    270 	    "Checks that CLOCK_PROCESS_CPUTIME_ID is monotonic");
    271 }
    272 ATF_TC_BODY(clock_gettime_process_cputime_is_monotonic, tc)
    273 {
    274 	check_monotonicity("CLOCK_PROCESS_CPUTIME_ID",
    275 	    CLOCK_PROCESS_CPUTIME_ID, &waste_user_time);
    276 }
    277 
    278 ATF_TC(clock_gettime_thread_cputime_is_monotonic);
    279 ATF_TC_HEAD(clock_gettime_thread_cputime_is_monotonic, tc)
    280 {
    281 	atf_tc_set_md_var(tc, "descr",
    282 	    "Checks that CLOCK_THREAD_CPUTIME_ID is monotonic");
    283 }
    284 ATF_TC_BODY(clock_gettime_thread_cputime_is_monotonic, tc)
    285 {
    286 	check_monotonicity("CLOCK_THREAD_CPUTIME_ID",
    287 	    CLOCK_THREAD_CPUTIME_ID, &waste_user_time);
    288 }
    289 
    290 static void
    291 check_resolution(const char *clockname, clockid_t clockid)
    292 {
    293 	struct timespec ts;
    294 	int rv;
    295 
    296 	RLF(rv = clock_getres(clockid, &ts), "%s", clockname);
    297 	if (rv != -1) {
    298 		ATF_CHECK_MSG(ts.tv_sec == 0,
    299 		    "The resolution of the clock %s is reported as %jd.%09ld"
    300 		    " which is lower than a second; most likely a wrong value",
    301 		    clockname, ts.tv_sec, ts.tv_nsec);
    302 	}
    303 }
    304 
    305 ATF_TC(clock_getres);
    306 ATF_TC_HEAD(clock_getres, tc)
    307 {
    308 	atf_tc_set_md_var(tc, "descr",
    309 	    "Checks that clock_getres(2) returns some reasonable resolution"
    310 	    " for all supported clocks");
    311 }
    312 ATF_TC_BODY(clock_getres, tc)
    313 {
    314 	check_resolution("CLOCK_REALTIME", CLOCK_REALTIME);
    315 	check_resolution("CLOCK_MONOTONIC", CLOCK_MONOTONIC);
    316 	atf_tc_expect_fail("These clocks aren't supported but are documented"
    317 	    " in clock_gettime(2) for some reason");
    318 	check_resolution("CLOCK_VIRTUAL", CLOCK_VIRTUAL);
    319 	check_resolution("CLOCK_PROF", CLOCK_PROF);
    320 	atf_tc_expect_pass();
    321 	check_resolution("CLOCK_PROCESS_CPUTIME_ID", CLOCK_PROCESS_CPUTIME_ID);
    322 	check_resolution("CLOCK_THREAD_CPUTIME_ID", CLOCK_THREAD_CPUTIME_ID);
    323 }
    324 
    325 ATF_TP_ADD_TCS(tp)
    326 {
    327 
    328 	ATF_TP_ADD_TC(tp, clock_gettime_real);
    329 	ATF_TP_ADD_TC(tp, clock_gettime_process_cputime_is_monotonic);
    330 	ATF_TP_ADD_TC(tp, clock_gettime_thread_cputime_is_monotonic);
    331 	ATF_TP_ADD_TC(tp, clock_getres);
    332 
    333 	return atf_no_error();
    334 }
    335