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>
31 #include "../kselftest.h"
33 #define CALLS_PER_LOOP 64
34 #define NSEC_PER_SEC 1000000000ULL
36 #define CLOCK_REALTIME 0
37 #define CLOCK_MONOTONIC 1
38 #define CLOCK_PROCESS_CPUTIME_ID 2
39 #define CLOCK_THREAD_CPUTIME_ID 3
40 #define CLOCK_MONOTONIC_RAW 4
41 #define CLOCK_REALTIME_COARSE 5
42 #define CLOCK_MONOTONIC_COARSE 6
43 #define CLOCK_BOOTTIME 7
44 #define CLOCK_REALTIME_ALARM 8
45 #define CLOCK_BOOTTIME_ALARM 9
46 #define CLOCK_HWSPECIFIC 10
48 #define NR_CLOCKIDS 12
50 char *clockstring(int clockid
)
54 return "CLOCK_REALTIME";
56 return "CLOCK_MONOTONIC";
57 case CLOCK_PROCESS_CPUTIME_ID
:
58 return "CLOCK_PROCESS_CPUTIME_ID";
59 case CLOCK_THREAD_CPUTIME_ID
:
60 return "CLOCK_THREAD_CPUTIME_ID";
61 case CLOCK_MONOTONIC_RAW
:
62 return "CLOCK_MONOTONIC_RAW";
63 case CLOCK_REALTIME_COARSE
:
64 return "CLOCK_REALTIME_COARSE";
65 case CLOCK_MONOTONIC_COARSE
:
66 return "CLOCK_MONOTONIC_COARSE";
68 return "CLOCK_BOOTTIME";
69 case CLOCK_REALTIME_ALARM
:
70 return "CLOCK_REALTIME_ALARM";
71 case CLOCK_BOOTTIME_ALARM
:
72 return "CLOCK_BOOTTIME_ALARM";
76 return "UNKNOWN_CLOCKID";
79 /* returns 1 if a <= b, 0 otherwise */
80 static inline int in_order(struct timespec a
, struct timespec b
)
82 /* use unsigned to avoid false positives on 2038 rollover */
83 if ((unsigned long)a
.tv_sec
< (unsigned long)b
.tv_sec
)
85 if ((unsigned long)a
.tv_sec
> (unsigned long)b
.tv_sec
)
87 if (a
.tv_nsec
> b
.tv_nsec
)
94 int consistency_test(int clock_type
, unsigned long seconds
)
96 struct timespec list
[CALLS_PER_LOOP
];
102 clock_gettime(clock_type
, &list
[0]);
103 now
= then
= list
[0].tv_sec
;
105 /* timestamp start of test */
107 start_str
= ctime(&t
);
109 while (seconds
== -1 || now
- then
< seconds
) {
113 for (i
= 0; i
< CALLS_PER_LOOP
; i
++)
114 clock_gettime(clock_type
, &list
[i
]);
116 /* Check for inconsistencies */
117 for (i
= 0; i
< CALLS_PER_LOOP
- 1; i
++)
118 if (!in_order(list
[i
], list
[i
+1]))
121 /* display inconsistency */
122 if (inconsistent
>= 0) {
123 unsigned long long delta
;
125 printf("\%s\n", start_str
);
126 for (i
= 0; i
< CALLS_PER_LOOP
; i
++) {
127 if (i
== inconsistent
)
128 printf("--------------------\n");
129 printf("%lu:%lu\n", list
[i
].tv_sec
,
131 if (i
== inconsistent
+ 1)
132 printf("--------------------\n");
134 delta
= list
[inconsistent
].tv_sec
* NSEC_PER_SEC
;
135 delta
+= list
[inconsistent
].tv_nsec
;
136 delta
-= list
[inconsistent
+1].tv_sec
* NSEC_PER_SEC
;
137 delta
-= list
[inconsistent
+1].tv_nsec
;
138 printf("Delta: %llu ns\n", delta
);
140 /* timestamp inconsistency*/
142 printf("%s\n", ctime(&t
));
143 printf("[FAILED]\n");
146 now
= list
[0].tv_sec
;
153 int main(int argc
, char *argv
[])
156 int userclock
= CLOCK_REALTIME
;
157 int maxclocks
= NR_CLOCKIDS
;
161 /* Process arguments */
162 while ((opt
= getopt(argc
, argv
, "t:c:")) != -1) {
165 runtime
= atoi(optarg
);
168 userclock
= atoi(optarg
);
169 maxclocks
= userclock
+ 1;
172 printf("Usage: %s [-t <secs>] [-c <clockid>]\n", argv
[0]);
173 printf(" -t: Number of seconds to run\n");
174 printf(" -c: clockid to use (default, all clockids)\n");
179 setbuf(stdout
, NULL
);
181 for (clockid
= userclock
; clockid
< maxclocks
; clockid
++) {
183 if (clockid
== CLOCK_HWSPECIFIC
)
186 if (!clock_gettime(clockid
, &ts
)) {
187 printf("Consistent %-30s ", clockstring(clockid
));
188 if (consistency_test(clockid
, runtime
))
189 return ksft_exit_fail();
192 return ksft_exit_pass();