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