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