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