1 /* Measure nanosleep timer latency
2 * by: john stultz (john.stultz@linaro.org)
3 * (C) Copyright Linaro 2013
4 * Licensed under the GPLv2
7 * $ gcc nsleep-lat.c -o nsleep-lat -lrt
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
24 #include <sys/timex.h>
27 #include "../kselftest.h"
29 #define NSEC_PER_SEC 1000000000ULL
31 #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
34 #define CLOCK_REALTIME 0
35 #define CLOCK_MONOTONIC 1
36 #define CLOCK_PROCESS_CPUTIME_ID 2
37 #define CLOCK_THREAD_CPUTIME_ID 3
38 #define CLOCK_MONOTONIC_RAW 4
39 #define CLOCK_REALTIME_COARSE 5
40 #define CLOCK_MONOTONIC_COARSE 6
41 #define CLOCK_BOOTTIME 7
42 #define CLOCK_REALTIME_ALARM 8
43 #define CLOCK_BOOTTIME_ALARM 9
44 #define CLOCK_HWSPECIFIC 10
46 #define NR_CLOCKIDS 12
48 #define UNSUPPORTED 0xf00f
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 struct timespec
timespec_add(struct timespec ts
, unsigned long long ns
)
82 while (ts
.tv_nsec
>= NSEC_PER_SEC
) {
83 ts
.tv_nsec
-= NSEC_PER_SEC
;
90 long long timespec_sub(struct timespec a
, struct timespec b
)
92 long long ret
= NSEC_PER_SEC
* b
.tv_sec
+ b
.tv_nsec
;
94 ret
-= NSEC_PER_SEC
* a
.tv_sec
+ a
.tv_nsec
;
98 int nanosleep_lat_test(int clockid
, long long ns
)
100 struct timespec start
, end
, target
;
101 long long latency
= 0;
104 target
.tv_sec
= ns
/NSEC_PER_SEC
;
105 target
.tv_nsec
= ns
%NSEC_PER_SEC
;
107 if (clock_gettime(clockid
, &start
))
109 if (clock_nanosleep(clockid
, 0, &target
, NULL
))
114 /* First check relative latency */
115 clock_gettime(clockid
, &start
);
116 for (i
= 0; i
< count
; i
++)
117 clock_nanosleep(clockid
, 0, &target
, NULL
);
118 clock_gettime(clockid
, &end
);
120 if (((timespec_sub(start
, end
)/count
)-ns
) > UNRESONABLE_LATENCY
) {
121 printf("Large rel latency: %lld ns :", (timespec_sub(start
, end
)/count
)-ns
);
125 /* Next check absolute latency */
126 for (i
= 0; i
< count
; i
++) {
127 clock_gettime(clockid
, &start
);
128 target
= timespec_add(start
, ns
);
129 clock_nanosleep(clockid
, TIMER_ABSTIME
, &target
, NULL
);
130 clock_gettime(clockid
, &end
);
131 latency
+= timespec_sub(target
, end
);
134 if (latency
/count
> UNRESONABLE_LATENCY
) {
135 printf("Large abs latency: %lld ns :", latency
/count
);
144 int main(int argc
, char **argv
)
149 for (clockid
= CLOCK_REALTIME
; clockid
< NR_CLOCKIDS
; clockid
++) {
151 /* Skip cputime clockids since nanosleep won't increment cputime */
152 if (clockid
== CLOCK_PROCESS_CPUTIME_ID
||
153 clockid
== CLOCK_THREAD_CPUTIME_ID
||
154 clockid
== CLOCK_HWSPECIFIC
)
157 printf("nsleep latency %-26s ", clockstring(clockid
));
161 while (length
<= (NSEC_PER_SEC
* 10)) {
162 ret
= nanosleep_lat_test(clockid
, length
);
169 if (ret
== UNSUPPORTED
) {
170 printf("[UNSUPPORTED]\n");
174 printf("[FAILED]\n");
175 return ksft_exit_fail();
179 return ksft_exit_pass();