1 /* Time inconsistency check test
2 * by: john stultz (johnstul@us.ibm.com)
3 * (C) Copyright IBM 2003, 2004, 2005, 2012
4 * (C) Copyright Linaro Limited 2015
5 * Licensed under the GPLv2
8 * $ gcc inconsistency-check.c -o inconsistency-check -lrt
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
28 #include <sys/timex.h>
32 #include "../kselftest.h"
34 static inline int ksft_exit_pass(void)
38 static inline int ksft_exit_fail(void)
44 #define CALLS_PER_LOOP 64
45 #define NSEC_PER_SEC 1000000000ULL
47 #define CLOCK_REALTIME 0
48 #define CLOCK_MONOTONIC 1
49 #define CLOCK_PROCESS_CPUTIME_ID 2
50 #define CLOCK_THREAD_CPUTIME_ID 3
51 #define CLOCK_MONOTONIC_RAW 4
52 #define CLOCK_REALTIME_COARSE 5
53 #define CLOCK_MONOTONIC_COARSE 6
54 #define CLOCK_BOOTTIME 7
55 #define CLOCK_REALTIME_ALARM 8
56 #define CLOCK_BOOTTIME_ALARM 9
57 #define CLOCK_HWSPECIFIC 10
59 #define NR_CLOCKIDS 12
61 char *clockstring(int clockid
)
65 return "CLOCK_REALTIME";
67 return "CLOCK_MONOTONIC";
68 case CLOCK_PROCESS_CPUTIME_ID
:
69 return "CLOCK_PROCESS_CPUTIME_ID";
70 case CLOCK_THREAD_CPUTIME_ID
:
71 return "CLOCK_THREAD_CPUTIME_ID";
72 case CLOCK_MONOTONIC_RAW
:
73 return "CLOCK_MONOTONIC_RAW";
74 case CLOCK_REALTIME_COARSE
:
75 return "CLOCK_REALTIME_COARSE";
76 case CLOCK_MONOTONIC_COARSE
:
77 return "CLOCK_MONOTONIC_COARSE";
79 return "CLOCK_BOOTTIME";
80 case CLOCK_REALTIME_ALARM
:
81 return "CLOCK_REALTIME_ALARM";
82 case CLOCK_BOOTTIME_ALARM
:
83 return "CLOCK_BOOTTIME_ALARM";
87 return "UNKNOWN_CLOCKID";
90 /* returns 1 if a <= b, 0 otherwise */
91 static inline int in_order(struct timespec a
, struct timespec b
)
93 /* use unsigned to avoid false positives on 2038 rollover */
94 if ((unsigned long)a
.tv_sec
< (unsigned long)b
.tv_sec
)
96 if ((unsigned long)a
.tv_sec
> (unsigned long)b
.tv_sec
)
98 if (a
.tv_nsec
> b
.tv_nsec
)
105 int consistency_test(int clock_type
, unsigned long seconds
)
107 struct timespec list
[CALLS_PER_LOOP
];
113 clock_gettime(clock_type
, &list
[0]);
114 now
= then
= list
[0].tv_sec
;
116 /* timestamp start of test */
118 start_str
= ctime(&t
);
120 while (seconds
== -1 || now
- then
< seconds
) {
124 for (i
= 0; i
< CALLS_PER_LOOP
; i
++)
125 clock_gettime(clock_type
, &list
[i
]);
127 /* Check for inconsistencies */
128 for (i
= 0; i
< CALLS_PER_LOOP
- 1; i
++)
129 if (!in_order(list
[i
], list
[i
+1]))
132 /* display inconsistency */
134 unsigned long long delta
;
136 printf("\%s\n", start_str
);
137 for (i
= 0; i
< CALLS_PER_LOOP
; i
++) {
138 if (i
== inconsistent
)
139 printf("--------------------\n");
140 printf("%lu:%lu\n", list
[i
].tv_sec
,
142 if (i
== inconsistent
+ 1)
143 printf("--------------------\n");
145 delta
= list
[inconsistent
].tv_sec
* NSEC_PER_SEC
;
146 delta
+= list
[inconsistent
].tv_nsec
;
147 delta
-= list
[inconsistent
+1].tv_sec
* NSEC_PER_SEC
;
148 delta
-= list
[inconsistent
+1].tv_nsec
;
149 printf("Delta: %llu ns\n", delta
);
151 /* timestamp inconsistency*/
153 printf("%s\n", ctime(&t
));
154 printf("[FAILED]\n");
157 now
= list
[0].tv_sec
;
164 int main(int argc
, char *argv
[])
167 int userclock
= CLOCK_REALTIME
;
168 int maxclocks
= NR_CLOCKIDS
;
172 /* Process arguments */
173 while ((opt
= getopt(argc
, argv
, "t:c:")) != -1) {
176 runtime
= atoi(optarg
);
179 userclock
= atoi(optarg
);
180 maxclocks
= userclock
+ 1;
183 printf("Usage: %s [-t <secs>] [-c <clockid>]\n", argv
[0]);
184 printf(" -t: Number of seconds to run\n");
185 printf(" -c: clockid to use (default, all clockids)\n");
190 setbuf(stdout
, NULL
);
192 for (clockid
= userclock
; clockid
< maxclocks
; clockid
++) {
194 if (clockid
== CLOCK_HWSPECIFIC
)
197 if (!clock_gettime(clockid
, &ts
)) {
198 printf("Consistent %-30s ", clockstring(clockid
));
199 if (consistency_test(clockid
, runtime
))
200 return ksft_exit_fail();
203 return ksft_exit_pass();