t_clock_gettime.c revision 1.1 1 1.1 jruoho /* $NetBSD: t_clock_gettime.c,v 1.1 2011/10/15 06:42:16 jruoho 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.1 jruoho __RCSID("$NetBSD: t_clock_gettime.c,v 1.1 2011/10/15 06:42:16 jruoho 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 #include <machine/int_limits.h>
67 1.1 jruoho
68 1.1 jruoho #include <atf-c.h>
69 1.1 jruoho #include <errno.h>
70 1.1 jruoho #include <stdio.h>
71 1.1 jruoho #include <stdlib.h>
72 1.1 jruoho #include <string.h>
73 1.1 jruoho #include <time.h>
74 1.1 jruoho #include <unistd.h>
75 1.1 jruoho
76 1.1 jruoho #include "../../../h_macros.h"
77 1.1 jruoho
78 1.1 jruoho #define MINPOSDIFF 15000000 /* 15 ms for now */
79 1.1 jruoho #define TIMEOUT 5
80 1.1 jruoho
81 1.1 jruoho #define TC_HARDWARE "kern.timecounter.hardware"
82 1.1 jruoho #define TC_CHOICE "kern.timecounter.choice"
83 1.1 jruoho
84 1.1 jruoho static void
85 1.1 jruoho check_timecounter(void)
86 1.1 jruoho {
87 1.1 jruoho struct timespec tsa, tsb, tsl, res;
88 1.1 jruoho long long mindiff = INTMAX_MAX;
89 1.1 jruoho time_t endlimit;
90 1.1 jruoho
91 1.1 jruoho #define CL(x) \
92 1.1 jruoho do { \
93 1.1 jruoho if ((x) != -1) \
94 1.1 jruoho break; \
95 1.1 jruoho atf_tc_fail_nonfatal("%s: %s", #x, strerror(errno)); \
96 1.1 jruoho return; \
97 1.1 jruoho } while (0)
98 1.1 jruoho
99 1.1 jruoho CL(clock_gettime(CLOCK_REALTIME, &tsa));
100 1.1 jruoho tsl = tsa;
101 1.1 jruoho
102 1.1 jruoho CL(time(&endlimit));
103 1.1 jruoho endlimit += TIMEOUT + 1;
104 1.1 jruoho
105 1.1 jruoho while ((time_t)tsa.tv_sec < endlimit) {
106 1.1 jruoho long long diff;
107 1.1 jruoho
108 1.1 jruoho CL(clock_gettime(CLOCK_REALTIME, &tsb));
109 1.1 jruoho diff = 1000000000LL * (tsb.tv_sec - tsa.tv_sec)
110 1.1 jruoho + tsb.tv_nsec - tsa.tv_nsec;
111 1.1 jruoho
112 1.1 jruoho if (diff > 0 && mindiff > diff)
113 1.1 jruoho mindiff = diff;
114 1.1 jruoho
115 1.1 jruoho if (diff < 0 || diff > MINPOSDIFF) {
116 1.1 jruoho long long elapsed;
117 1.1 jruoho (void)printf("%stime TSA: 0x%jx.%08jx, TSB: 0x%jx.%08jx, "
118 1.1 jruoho "diff = %lld nsec, ", (diff < 0) ? "BAD " : "",
119 1.1 jruoho (uintmax_t)tsa.tv_sec, (uintmax_t)tsa.tv_nsec,
120 1.1 jruoho (uintmax_t)tsb.tv_sec, (uintmax_t)tsb.tv_nsec, diff);
121 1.1 jruoho
122 1.1 jruoho elapsed = 1000000000LL * (tsb.tv_sec - tsl.tv_sec)
123 1.1 jruoho + tsb.tv_nsec - tsl.tv_nsec;
124 1.1 jruoho
125 1.1 jruoho
126 1.1 jruoho (void)printf("%lld nsec\n", elapsed);
127 1.1 jruoho tsl = tsb;
128 1.1 jruoho
129 1.1 jruoho ATF_CHECK(diff >= 0);
130 1.1 jruoho if (diff < 0)
131 1.1 jruoho return;
132 1.1 jruoho }
133 1.1 jruoho
134 1.1 jruoho tsa.tv_sec = tsb.tv_sec;
135 1.1 jruoho tsa.tv_nsec = tsb.tv_nsec;
136 1.1 jruoho }
137 1.1 jruoho
138 1.1 jruoho if (clock_getres(CLOCK_REALTIME, &res) == 0) {
139 1.1 jruoho long long r = res.tv_sec * 1000000000 + res.tv_nsec;
140 1.1 jruoho
141 1.1 jruoho (void)printf("Claimed resolution: %lld nsec (%f Hz) or "
142 1.1 jruoho "better\n", r, 1.0 / r * 1e9);
143 1.1 jruoho (void)printf("Observed minimum non zero delta: %lld "
144 1.1 jruoho "nsec\n", mindiff);
145 1.1 jruoho }
146 1.1 jruoho
147 1.1 jruoho #undef CL
148 1.1 jruoho }
149 1.1 jruoho
150 1.1 jruoho ATF_TC(clock_gettime_real);
151 1.1 jruoho ATF_TC_HEAD(clock_gettime_real, tc)
152 1.1 jruoho {
153 1.1 jruoho atf_tc_set_md_var(tc, "require.user", "root");
154 1.1 jruoho atf_tc_set_md_var(tc, "descr",
155 1.1 jruoho "Checks the monotonicity of the CLOCK_REALTIME implementation");
156 1.1 jruoho atf_tc_set_md_var(tc, "timeout", "300");
157 1.1 jruoho }
158 1.1 jruoho
159 1.1 jruoho ATF_TC_BODY(clock_gettime_real, tc)
160 1.1 jruoho {
161 1.1 jruoho char name[128], cbuf[512], ctrbuf[10240];
162 1.1 jruoho size_t cbufsiz = sizeof(cbuf);
163 1.1 jruoho size_t ctrbufsiz = sizeof(ctrbuf);
164 1.1 jruoho const char *p;
165 1.1 jruoho char *save;
166 1.1 jruoho int quality, n;
167 1.1 jruoho
168 1.1 jruoho if (sysctlbyname(TC_HARDWARE, cbuf, &cbufsiz, NULL, 0) != 0) {
169 1.1 jruoho (void)printf("\nChecking legacy time implementation "
170 1.1 jruoho "for %d seconds\n", TIMEOUT);
171 1.1 jruoho check_timecounter();
172 1.1 jruoho return;
173 1.1 jruoho /* NOTREACHED */
174 1.1 jruoho }
175 1.1 jruoho (void)printf("%s = %s\n", TC_HARDWARE, cbuf);
176 1.1 jruoho REQUIRE_LIBC(save = strdup(cbuf), NULL);
177 1.1 jruoho
178 1.1 jruoho RL(sysctlbyname(TC_CHOICE, ctrbuf, &ctrbufsiz, NULL, 0));
179 1.1 jruoho (void)printf("%s = %s\n", TC_CHOICE, ctrbuf);
180 1.1 jruoho
181 1.1 jruoho for (p = ctrbuf, n = 0; sscanf(p, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n",
182 1.1 jruoho name, &quality, &n) == 2; p += n) {
183 1.1 jruoho struct timespec ts;
184 1.1 jruoho int ret;
185 1.1 jruoho
186 1.1 jruoho if (quality < 0)
187 1.1 jruoho continue;
188 1.1 jruoho
189 1.1 jruoho (void)printf("\nChecking %s for %d seconds\n", name, TIMEOUT);
190 1.1 jruoho CHECK_LIBC(ret = sysctlbyname(TC_HARDWARE, NULL, 0,
191 1.1 jruoho name, strlen(name)), -1);
192 1.1 jruoho if (ret == -1)
193 1.1 jruoho continue;
194 1.1 jruoho
195 1.1 jruoho /* wait a bit to select new counter in clockinterrupt */
196 1.1 jruoho ts.tv_sec = 0;
197 1.1 jruoho ts.tv_nsec = 100000000;
198 1.1 jruoho (void)nanosleep(&ts, NULL);
199 1.1 jruoho
200 1.1 jruoho check_timecounter();
201 1.1 jruoho }
202 1.1 jruoho
203 1.1 jruoho RL(sysctlbyname(TC_HARDWARE, NULL, 0, save, strlen(save)));
204 1.1 jruoho }
205 1.1 jruoho
206 1.1 jruoho ATF_TP_ADD_TCS(tp)
207 1.1 jruoho {
208 1.1 jruoho
209 1.1 jruoho ATF_TP_ADD_TC(tp, clock_gettime_real);
210 1.1 jruoho
211 1.1 jruoho return atf_no_error();
212 1.1 jruoho }
213