1 /* Make sure timers don't return early
2 * by: john stultz (johnstul@us.ibm.com)
3 * John Stultz (john.stultz@linaro.org)
4 * (C) Copyright IBM 2012
5 * (C) Copyright Linaro 2013 2015
6 * Licensed under the GPLv2
9 * $ gcc nanosleep.c -o nanosleep -lrt
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
26 #include <sys/timex.h>
30 #include "../kselftest.h"
32 static inline int ksft_exit_pass(void)
36 static inline int ksft_exit_fail(void)
42 #define NSEC_PER_SEC 1000000000ULL
44 #define CLOCK_REALTIME 0
45 #define CLOCK_MONOTONIC 1
46 #define CLOCK_PROCESS_CPUTIME_ID 2
47 #define CLOCK_THREAD_CPUTIME_ID 3
48 #define CLOCK_MONOTONIC_RAW 4
49 #define CLOCK_REALTIME_COARSE 5
50 #define CLOCK_MONOTONIC_COARSE 6
51 #define CLOCK_BOOTTIME 7
52 #define CLOCK_REALTIME_ALARM 8
53 #define CLOCK_BOOTTIME_ALARM 9
54 #define CLOCK_HWSPECIFIC 10
56 #define NR_CLOCKIDS 12
58 #define UNSUPPORTED 0xf00f
60 char *clockstring(int clockid
)
64 return "CLOCK_REALTIME";
66 return "CLOCK_MONOTONIC";
67 case CLOCK_PROCESS_CPUTIME_ID
:
68 return "CLOCK_PROCESS_CPUTIME_ID";
69 case CLOCK_THREAD_CPUTIME_ID
:
70 return "CLOCK_THREAD_CPUTIME_ID";
71 case CLOCK_MONOTONIC_RAW
:
72 return "CLOCK_MONOTONIC_RAW";
73 case CLOCK_REALTIME_COARSE
:
74 return "CLOCK_REALTIME_COARSE";
75 case CLOCK_MONOTONIC_COARSE
:
76 return "CLOCK_MONOTONIC_COARSE";
78 return "CLOCK_BOOTTIME";
79 case CLOCK_REALTIME_ALARM
:
80 return "CLOCK_REALTIME_ALARM";
81 case CLOCK_BOOTTIME_ALARM
:
82 return "CLOCK_BOOTTIME_ALARM";
86 return "UNKNOWN_CLOCKID";
89 /* returns 1 if a <= b, 0 otherwise */
90 static inline int in_order(struct timespec a
, struct timespec b
)
92 if (a
.tv_sec
< b
.tv_sec
)
94 if (a
.tv_sec
> b
.tv_sec
)
96 if (a
.tv_nsec
> b
.tv_nsec
)
101 struct timespec
timespec_add(struct timespec ts
, unsigned long long ns
)
104 while (ts
.tv_nsec
>= NSEC_PER_SEC
) {
105 ts
.tv_nsec
-= NSEC_PER_SEC
;
111 int nanosleep_test(int clockid
, long long ns
)
113 struct timespec now
, target
, rel
;
115 /* First check abs time */
116 if (clock_gettime(clockid
, &now
))
118 target
= timespec_add(now
, ns
);
120 if (clock_nanosleep(clockid
, TIMER_ABSTIME
, &target
, NULL
))
122 clock_gettime(clockid
, &now
);
124 if (!in_order(target
, now
))
127 /* Second check reltime */
128 clock_gettime(clockid
, &now
);
131 rel
= timespec_add(rel
, ns
);
132 target
= timespec_add(now
, ns
);
133 clock_nanosleep(clockid
, 0, &rel
, NULL
);
134 clock_gettime(clockid
, &now
);
136 if (!in_order(target
, now
))
141 int main(int argc
, char **argv
)
146 for (clockid
= CLOCK_REALTIME
; clockid
< NR_CLOCKIDS
; clockid
++) {
148 /* Skip cputime clockids since nanosleep won't increment cputime */
149 if (clockid
== CLOCK_PROCESS_CPUTIME_ID
||
150 clockid
== CLOCK_THREAD_CPUTIME_ID
||
151 clockid
== CLOCK_HWSPECIFIC
)
154 printf("Nanosleep %-31s ", clockstring(clockid
));
157 while (length
<= (NSEC_PER_SEC
* 10)) {
158 ret
= nanosleep_test(clockid
, length
);
159 if (ret
== UNSUPPORTED
) {
160 printf("[UNSUPPORTED]\n");
164 printf("[FAILED]\n");
165 return ksft_exit_fail();
173 return ksft_exit_pass();