1 /* set_timer latency test
2 * John Stultz (john.stultz@linaro.org)
3 * (C) Copyright Linaro 2014
4 * Licensed under the GPLv2
6 * This test makes sure the set_timer api is correct
9 * $ gcc set-timer-lat.c -o set-timer-lat -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.
32 #include "../kselftest.h"
34 static inline int ksft_exit_pass(void)
38 static inline int ksft_exit_fail(void)
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
59 #define NSEC_PER_SEC 1000000000ULL
60 #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
65 struct timespec start_time;
66 long long max_latency_ns;
67 int timer_fired_early;
69 char *clockstring(int clockid)
73 return "CLOCK_REALTIME";
75 return "CLOCK_MONOTONIC";
76 case CLOCK_PROCESS_CPUTIME_ID:
77 return "CLOCK_PROCESS_CPUTIME_ID";
78 case CLOCK_THREAD_CPUTIME_ID:
79 return "CLOCK_THREAD_CPUTIME_ID";
80 case CLOCK_MONOTONIC_RAW:
81 return "CLOCK_MONOTONIC_RAW";
82 case CLOCK_REALTIME_COARSE:
83 return "CLOCK_REALTIME_COARSE";
84 case CLOCK_MONOTONIC_COARSE:
85 return "CLOCK_MONOTONIC_COARSE";
87 return "CLOCK_BOOTTIME";
88 case CLOCK_REALTIME_ALARM:
89 return "CLOCK_REALTIME_ALARM";
90 case CLOCK_BOOTTIME_ALARM:
91 return "CLOCK_BOOTTIME_ALARM";
95 return "UNKNOWN_CLOCKID";
99 long long timespec_sub(struct timespec a, struct timespec b)
101 long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
103 ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
108 void sigalarm(int signo)
113 clock_gettime(clock_id, &ts);
116 delta_ns = timespec_sub(start_time, ts);
117 delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount;
120 timer_fired_early = 1;
122 if (delta_ns > max_latency_ns)
123 max_latency_ns = delta_ns;
126 void describe_timer(int flags, int interval)
128 printf("%-22s %s %s ",
129 clockstring(clock_id),
130 flags ? "ABSTIME":"RELTIME",
131 interval ? "PERIODIC":"ONE-SHOT");
134 int setup_timer(int clock_id, int flags, int interval, timer_t *tm1)
137 struct itimerspec its1, its2;
141 memset(&se, 0, sizeof(se));
142 se.sigev_notify = SIGEV_SIGNAL;
143 se.sigev_signo = SIGRTMAX;
144 se.sigev_value.sival_int = 0;
148 timer_fired_early = 0;
150 err = timer_create(clock_id, &se, tm1);
152 if ((clock_id == CLOCK_REALTIME_ALARM) ||
153 (clock_id == CLOCK_BOOTTIME_ALARM)) {
154 printf("%-22s %s missing CAP_WAKE_ALARM? : [UNSUPPORTED]\n",
155 clockstring(clock_id),
156 flags ? "ABSTIME":"RELTIME");
159 printf("%s - timer_create() failed\n", clockstring(clock_id));
163 clock_gettime(clock_id, &start_time);
165 its1.it_value = start_time;
166 its1.it_value.tv_sec += TIMER_SECS;
168 its1.it_value.tv_sec = TIMER_SECS;
169 its1.it_value.tv_nsec = 0;
171 its1.it_interval.tv_sec = interval;
172 its1.it_interval.tv_nsec = 0;
174 err = timer_settime(*tm1, flags, &its1, &its2);
176 printf("%s - timer_settime() failed\n", clockstring(clock_id));
183 int check_timer_latency(int flags, int interval)
187 describe_timer(flags, interval);
188 printf("timer fired early: %7d : ", timer_fired_early);
189 if (!timer_fired_early) {
192 printf("[FAILED]\n");
196 describe_timer(flags, interval);
197 printf("max latency: %10lld ns : ", max_latency_ns);
199 if (max_latency_ns < UNRESONABLE_LATENCY) {
202 printf("[FAILED]\n");
208 int check_alarmcount(int flags, int interval)
210 describe_timer(flags, interval);
211 printf("count: %19d : ", alarmcount);
212 if (alarmcount == 1) {
216 printf("[FAILED]\n");
220 int do_timer(int clock_id, int flags)
223 const int interval = TIMER_SECS;
226 err = setup_timer(clock_id, flags, interval, &tm1);
230 while (alarmcount < 5)
234 return check_timer_latency(flags, interval);
237 int do_timer_oneshot(int clock_id, int flags)
240 const int interval = 0;
241 struct timeval timeout;
245 err = setup_timer(clock_id, flags, interval, &tm1);
249 memset(&timeout, 0, sizeof(timeout));
253 err = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
254 } while (err == -1 && errno == EINTR);
257 err = check_timer_latency(flags, interval);
258 err |= check_alarmcount(flags, interval);
264 struct sigaction act;
265 int signum = SIGRTMAX;
268 /* Set up signal handler: */
269 sigfillset(&act.sa_mask);
271 act.sa_handler = sigalarm;
272 sigaction(signum, &act, NULL);
274 printf("Setting timers for every %i seconds\n", TIMER_SECS);
275 for (clock_id = 0; clock_id < NR_CLOCKIDS; clock_id++) {
277 if ((clock_id == CLOCK_PROCESS_CPUTIME_ID) ||
278 (clock_id == CLOCK_THREAD_CPUTIME_ID) ||
279 (clock_id == CLOCK_MONOTONIC_RAW) ||
280 (clock_id == CLOCK_REALTIME_COARSE) ||
281 (clock_id == CLOCK_MONOTONIC_COARSE) ||
282 (clock_id == CLOCK_HWSPECIFIC))
285 ret |= do_timer(clock_id, TIMER_ABSTIME);
286 ret |= do_timer(clock_id, 0);
287 ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME);
288 ret |= do_timer_oneshot(clock_id, 0);
291 return ksft_exit_fail();
292 return ksft_exit_pass();