1 /* $NetBSD: t_clock_gettime.c,v 1.1 2011/10/15 06:42:16 jruoho Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 2006 Frank Kardel
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
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 $");
63 #include <sys/param.h>
64 #include <sys/sysctl.h>
66 #include <machine/int_limits.h>
76 #include "../../../h_macros.h"
78 #define MINPOSDIFF 15000000 /* 15 ms for now */
81 #define TC_HARDWARE "kern.timecounter.hardware"
82 #define TC_CHOICE "kern.timecounter.choice"
85 check_timecounter(void)
87 struct timespec tsa
, tsb
, tsl
, res
;
88 long long mindiff
= INTMAX_MAX
;
95 atf_tc_fail_nonfatal("%s: %s", #x, strerror(errno)); \
99 CL(clock_gettime(CLOCK_REALTIME
, &tsa
));
103 endlimit
+= TIMEOUT
+ 1;
105 while ((time_t)tsa
.tv_sec
< endlimit
) {
108 CL(clock_gettime(CLOCK_REALTIME
, &tsb
));
109 diff
= 1000000000LL * (tsb
.tv_sec
- tsa
.tv_sec
)
110 + tsb
.tv_nsec
- tsa
.tv_nsec
;
112 if (diff
> 0 && mindiff
> diff
)
115 if (diff
< 0 || diff
> MINPOSDIFF
) {
117 (void)printf("%stime TSA: 0x%jx.%08jx, TSB: 0x%jx.%08jx, "
118 "diff = %lld nsec, ", (diff
< 0) ? "BAD " : "",
119 (uintmax_t)tsa
.tv_sec
, (uintmax_t)tsa
.tv_nsec
,
120 (uintmax_t)tsb
.tv_sec
, (uintmax_t)tsb
.tv_nsec
, diff
);
122 elapsed
= 1000000000LL * (tsb
.tv_sec
- tsl
.tv_sec
)
123 + tsb
.tv_nsec
- tsl
.tv_nsec
;
126 (void)printf("%lld nsec\n", elapsed
);
129 ATF_CHECK(diff
>= 0);
134 tsa
.tv_sec
= tsb
.tv_sec
;
135 tsa
.tv_nsec
= tsb
.tv_nsec
;
138 if (clock_getres(CLOCK_REALTIME
, &res
) == 0) {
139 long long r
= res
.tv_sec
* 1000000000 + res
.tv_nsec
;
141 (void)printf("Claimed resolution: %lld nsec (%f Hz) or "
142 "better\n", r
, 1.0 / r
* 1e9
);
143 (void)printf("Observed minimum non zero delta: %lld "
150 ATF_TC(clock_gettime_real
);
151 ATF_TC_HEAD(clock_gettime_real
, tc
)
153 atf_tc_set_md_var(tc
, "require.user", "root");
154 atf_tc_set_md_var(tc
, "descr",
155 "Checks the monotonicity of the CLOCK_REALTIME implementation");
156 atf_tc_set_md_var(tc
, "timeout", "300");
159 ATF_TC_BODY(clock_gettime_real
, tc
)
161 char name
[128], cbuf
[512], ctrbuf
[10240];
162 size_t cbufsiz
= sizeof(cbuf
);
163 size_t ctrbufsiz
= sizeof(ctrbuf
);
168 if (sysctlbyname(TC_HARDWARE
, cbuf
, &cbufsiz
, NULL
, 0) != 0) {
169 (void)printf("\nChecking legacy time implementation "
170 "for %d seconds\n", TIMEOUT
);
175 (void)printf("%s = %s\n", TC_HARDWARE
, cbuf
);
176 REQUIRE_LIBC(save
= strdup(cbuf
), NULL
);
178 RL(sysctlbyname(TC_CHOICE
, ctrbuf
, &ctrbufsiz
, NULL
, 0));
179 (void)printf("%s = %s\n", TC_CHOICE
, ctrbuf
);
181 for (p
= ctrbuf
, n
= 0; sscanf(p
, "%127[^(](q=%d, f=%*u Hz)%*[ ]%n",
182 name
, &quality
, &n
) == 2; p
+= n
) {
189 (void)printf("\nChecking %s for %d seconds\n", name
, TIMEOUT
);
190 CHECK_LIBC(ret
= sysctlbyname(TC_HARDWARE
, NULL
, 0,
191 name
, strlen(name
)), -1);
195 /* wait a bit to select new counter in clockinterrupt */
197 ts
.tv_nsec
= 100000000;
198 (void)nanosleep(&ts
, NULL
);
203 RL(sysctlbyname(TC_HARDWARE
, NULL
, 0, save
, strlen(save
)));
209 ATF_TP_ADD_TC(tp
, clock_gettime_real
);
211 return atf_no_error();