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