14e40d0a2SJohn Stultz /* set_timer latency test
24e40d0a2SJohn Stultz * John Stultz (john.stultz@linaro.org)
34e40d0a2SJohn Stultz * (C) Copyright Linaro 2014
44e40d0a2SJohn Stultz * Licensed under the GPLv2
54e40d0a2SJohn Stultz *
64e40d0a2SJohn Stultz * This test makes sure the set_timer api is correct
74e40d0a2SJohn Stultz *
84e40d0a2SJohn Stultz * To build:
94e40d0a2SJohn Stultz * $ gcc set-timer-lat.c -o set-timer-lat -lrt
104e40d0a2SJohn Stultz *
114e40d0a2SJohn Stultz * This program is free software: you can redistribute it and/or modify
124e40d0a2SJohn Stultz * it under the terms of the GNU General Public License as published by
134e40d0a2SJohn Stultz * the Free Software Foundation, either version 2 of the License, or
144e40d0a2SJohn Stultz * (at your option) any later version.
154e40d0a2SJohn Stultz *
164e40d0a2SJohn Stultz * This program is distributed in the hope that it will be useful,
174e40d0a2SJohn Stultz * but WITHOUT ANY WARRANTY; without even the implied warranty of
184e40d0a2SJohn Stultz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
194e40d0a2SJohn Stultz * GNU General Public License for more details.
204e40d0a2SJohn Stultz */
214e40d0a2SJohn Stultz
224e40d0a2SJohn Stultz
2328be3f8fSGreg Hackmann #include <errno.h>
244e40d0a2SJohn Stultz #include <stdio.h>
254e40d0a2SJohn Stultz #include <unistd.h>
264e40d0a2SJohn Stultz #include <time.h>
274e40d0a2SJohn Stultz #include <string.h>
284e40d0a2SJohn Stultz #include <signal.h>
294e40d0a2SJohn Stultz #include <stdlib.h>
304e40d0a2SJohn Stultz #include <pthread.h>
314e40d0a2SJohn Stultz #include "../kselftest.h"
324e40d0a2SJohn Stultz
334e40d0a2SJohn Stultz #define CLOCK_REALTIME 0
344e40d0a2SJohn Stultz #define CLOCK_MONOTONIC 1
354e40d0a2SJohn Stultz #define CLOCK_PROCESS_CPUTIME_ID 2
364e40d0a2SJohn Stultz #define CLOCK_THREAD_CPUTIME_ID 3
374e40d0a2SJohn Stultz #define CLOCK_MONOTONIC_RAW 4
384e40d0a2SJohn Stultz #define CLOCK_REALTIME_COARSE 5
394e40d0a2SJohn Stultz #define CLOCK_MONOTONIC_COARSE 6
404e40d0a2SJohn Stultz #define CLOCK_BOOTTIME 7
414e40d0a2SJohn Stultz #define CLOCK_REALTIME_ALARM 8
424e40d0a2SJohn Stultz #define CLOCK_BOOTTIME_ALARM 9
434e40d0a2SJohn Stultz #define CLOCK_HWSPECIFIC 10
444e40d0a2SJohn Stultz #define CLOCK_TAI 11
454e40d0a2SJohn Stultz #define NR_CLOCKIDS 12
464e40d0a2SJohn Stultz
474e40d0a2SJohn Stultz
484e40d0a2SJohn Stultz #define NSEC_PER_SEC 1000000000ULL
494e40d0a2SJohn Stultz #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
504e40d0a2SJohn Stultz
51e48f284dSJohn Stultz #define TIMER_SECS 1
524e40d0a2SJohn Stultz int alarmcount;
534e40d0a2SJohn Stultz int clock_id;
544e40d0a2SJohn Stultz struct timespec start_time;
554e40d0a2SJohn Stultz long long max_latency_ns;
56a524b118SGreg Hackmann int timer_fired_early;
574e40d0a2SJohn Stultz
clockstring(int clockid)584e40d0a2SJohn Stultz char *clockstring(int clockid)
594e40d0a2SJohn Stultz {
604e40d0a2SJohn Stultz switch (clockid) {
614e40d0a2SJohn Stultz case CLOCK_REALTIME:
624e40d0a2SJohn Stultz return "CLOCK_REALTIME";
634e40d0a2SJohn Stultz case CLOCK_MONOTONIC:
644e40d0a2SJohn Stultz return "CLOCK_MONOTONIC";
654e40d0a2SJohn Stultz case CLOCK_PROCESS_CPUTIME_ID:
664e40d0a2SJohn Stultz return "CLOCK_PROCESS_CPUTIME_ID";
674e40d0a2SJohn Stultz case CLOCK_THREAD_CPUTIME_ID:
684e40d0a2SJohn Stultz return "CLOCK_THREAD_CPUTIME_ID";
694e40d0a2SJohn Stultz case CLOCK_MONOTONIC_RAW:
704e40d0a2SJohn Stultz return "CLOCK_MONOTONIC_RAW";
714e40d0a2SJohn Stultz case CLOCK_REALTIME_COARSE:
724e40d0a2SJohn Stultz return "CLOCK_REALTIME_COARSE";
734e40d0a2SJohn Stultz case CLOCK_MONOTONIC_COARSE:
744e40d0a2SJohn Stultz return "CLOCK_MONOTONIC_COARSE";
754e40d0a2SJohn Stultz case CLOCK_BOOTTIME:
764e40d0a2SJohn Stultz return "CLOCK_BOOTTIME";
774e40d0a2SJohn Stultz case CLOCK_REALTIME_ALARM:
784e40d0a2SJohn Stultz return "CLOCK_REALTIME_ALARM";
794e40d0a2SJohn Stultz case CLOCK_BOOTTIME_ALARM:
804e40d0a2SJohn Stultz return "CLOCK_BOOTTIME_ALARM";
814e40d0a2SJohn Stultz case CLOCK_TAI:
824e40d0a2SJohn Stultz return "CLOCK_TAI";
834e40d0a2SJohn Stultz };
844e40d0a2SJohn Stultz return "UNKNOWN_CLOCKID";
854e40d0a2SJohn Stultz }
864e40d0a2SJohn Stultz
874e40d0a2SJohn Stultz
timespec_sub(struct timespec a,struct timespec b)884e40d0a2SJohn Stultz long long timespec_sub(struct timespec a, struct timespec b)
894e40d0a2SJohn Stultz {
904e40d0a2SJohn Stultz long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
914e40d0a2SJohn Stultz
924e40d0a2SJohn Stultz ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
934e40d0a2SJohn Stultz return ret;
944e40d0a2SJohn Stultz }
954e40d0a2SJohn Stultz
964e40d0a2SJohn Stultz
sigalarm(int signo)974e40d0a2SJohn Stultz void sigalarm(int signo)
984e40d0a2SJohn Stultz {
994e40d0a2SJohn Stultz long long delta_ns;
1004e40d0a2SJohn Stultz struct timespec ts;
1014e40d0a2SJohn Stultz
1024e40d0a2SJohn Stultz clock_gettime(clock_id, &ts);
1034e40d0a2SJohn Stultz alarmcount++;
1044e40d0a2SJohn Stultz
1054e40d0a2SJohn Stultz delta_ns = timespec_sub(start_time, ts);
1064e40d0a2SJohn Stultz delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount;
1074e40d0a2SJohn Stultz
1084e40d0a2SJohn Stultz if (delta_ns < 0)
109a524b118SGreg Hackmann timer_fired_early = 1;
1104e40d0a2SJohn Stultz
1114e40d0a2SJohn Stultz if (delta_ns > max_latency_ns)
1124e40d0a2SJohn Stultz max_latency_ns = delta_ns;
1134e40d0a2SJohn Stultz }
1144e40d0a2SJohn Stultz
describe_timer(int flags,int interval)11528be3f8fSGreg Hackmann void describe_timer(int flags, int interval)
116a524b118SGreg Hackmann {
11728be3f8fSGreg Hackmann printf("%-22s %s %s ",
118a524b118SGreg Hackmann clockstring(clock_id),
11928be3f8fSGreg Hackmann flags ? "ABSTIME":"RELTIME",
12028be3f8fSGreg Hackmann interval ? "PERIODIC":"ONE-SHOT");
121a524b118SGreg Hackmann }
122a524b118SGreg Hackmann
setup_timer(int clock_id,int flags,int interval,timer_t * tm1)12328be3f8fSGreg Hackmann int setup_timer(int clock_id, int flags, int interval, timer_t *tm1)
1244e40d0a2SJohn Stultz {
1254e40d0a2SJohn Stultz struct sigevent se;
1264e40d0a2SJohn Stultz struct itimerspec its1, its2;
1274e40d0a2SJohn Stultz int err;
1284e40d0a2SJohn Stultz
1294e40d0a2SJohn Stultz /* Set up timer: */
1304e40d0a2SJohn Stultz memset(&se, 0, sizeof(se));
1314e40d0a2SJohn Stultz se.sigev_notify = SIGEV_SIGNAL;
1324e40d0a2SJohn Stultz se.sigev_signo = SIGRTMAX;
1334e40d0a2SJohn Stultz se.sigev_value.sival_int = 0;
1344e40d0a2SJohn Stultz
1354e40d0a2SJohn Stultz max_latency_ns = 0;
1364e40d0a2SJohn Stultz alarmcount = 0;
137a524b118SGreg Hackmann timer_fired_early = 0;
1384e40d0a2SJohn Stultz
13928be3f8fSGreg Hackmann err = timer_create(clock_id, &se, tm1);
1404e40d0a2SJohn Stultz if (err) {
14148fd77d1SJohn Stultz if ((clock_id == CLOCK_REALTIME_ALARM) ||
14248fd77d1SJohn Stultz (clock_id == CLOCK_BOOTTIME_ALARM)) {
14348fd77d1SJohn Stultz printf("%-22s %s missing CAP_WAKE_ALARM? : [UNSUPPORTED]\n",
14448fd77d1SJohn Stultz clockstring(clock_id),
14548fd77d1SJohn Stultz flags ? "ABSTIME":"RELTIME");
146*eefd95e1SShuah Khan /* Indicate timer isn't set, so caller doesn't wait */
147*eefd95e1SShuah Khan return 1;
14848fd77d1SJohn Stultz }
1494e40d0a2SJohn Stultz printf("%s - timer_create() failed\n", clockstring(clock_id));
1504e40d0a2SJohn Stultz return -1;
1514e40d0a2SJohn Stultz }
1524e40d0a2SJohn Stultz
1534e40d0a2SJohn Stultz clock_gettime(clock_id, &start_time);
1544e40d0a2SJohn Stultz if (flags) {
1554e40d0a2SJohn Stultz its1.it_value = start_time;
1564e40d0a2SJohn Stultz its1.it_value.tv_sec += TIMER_SECS;
1574e40d0a2SJohn Stultz } else {
1584e40d0a2SJohn Stultz its1.it_value.tv_sec = TIMER_SECS;
1594e40d0a2SJohn Stultz its1.it_value.tv_nsec = 0;
1604e40d0a2SJohn Stultz }
16128be3f8fSGreg Hackmann its1.it_interval.tv_sec = interval;
1624e40d0a2SJohn Stultz its1.it_interval.tv_nsec = 0;
1634e40d0a2SJohn Stultz
16428be3f8fSGreg Hackmann err = timer_settime(*tm1, flags, &its1, &its2);
1654e40d0a2SJohn Stultz if (err) {
1664e40d0a2SJohn Stultz printf("%s - timer_settime() failed\n", clockstring(clock_id));
1674e40d0a2SJohn Stultz return -1;
1684e40d0a2SJohn Stultz }
1694e40d0a2SJohn Stultz
17028be3f8fSGreg Hackmann return 0;
17128be3f8fSGreg Hackmann }
1724e40d0a2SJohn Stultz
check_timer_latency(int flags,int interval)17328be3f8fSGreg Hackmann int check_timer_latency(int flags, int interval)
17428be3f8fSGreg Hackmann {
17528be3f8fSGreg Hackmann int err = 0;
17628be3f8fSGreg Hackmann
17728be3f8fSGreg Hackmann describe_timer(flags, interval);
178a524b118SGreg Hackmann printf("timer fired early: %7d : ", timer_fired_early);
179a524b118SGreg Hackmann if (!timer_fired_early) {
180a524b118SGreg Hackmann printf("[OK]\n");
181a524b118SGreg Hackmann } else {
182a524b118SGreg Hackmann printf("[FAILED]\n");
183a524b118SGreg Hackmann err = -1;
184a524b118SGreg Hackmann }
185a524b118SGreg Hackmann
18628be3f8fSGreg Hackmann describe_timer(flags, interval);
187a524b118SGreg Hackmann printf("max latency: %10lld ns : ", max_latency_ns);
1884e40d0a2SJohn Stultz
1894e40d0a2SJohn Stultz if (max_latency_ns < UNRESONABLE_LATENCY) {
1904e40d0a2SJohn Stultz printf("[OK]\n");
191a524b118SGreg Hackmann } else {
1924e40d0a2SJohn Stultz printf("[FAILED]\n");
193a524b118SGreg Hackmann err = -1;
194a524b118SGreg Hackmann }
195a524b118SGreg Hackmann return err;
1964e40d0a2SJohn Stultz }
1974e40d0a2SJohn Stultz
check_alarmcount(int flags,int interval)19828be3f8fSGreg Hackmann int check_alarmcount(int flags, int interval)
19928be3f8fSGreg Hackmann {
20028be3f8fSGreg Hackmann describe_timer(flags, interval);
20128be3f8fSGreg Hackmann printf("count: %19d : ", alarmcount);
20228be3f8fSGreg Hackmann if (alarmcount == 1) {
20328be3f8fSGreg Hackmann printf("[OK]\n");
20428be3f8fSGreg Hackmann return 0;
20528be3f8fSGreg Hackmann }
20628be3f8fSGreg Hackmann printf("[FAILED]\n");
20728be3f8fSGreg Hackmann return -1;
20828be3f8fSGreg Hackmann }
20928be3f8fSGreg Hackmann
do_timer(int clock_id,int flags)21028be3f8fSGreg Hackmann int do_timer(int clock_id, int flags)
21128be3f8fSGreg Hackmann {
21228be3f8fSGreg Hackmann timer_t tm1;
21328be3f8fSGreg Hackmann const int interval = TIMER_SECS;
21428be3f8fSGreg Hackmann int err;
21528be3f8fSGreg Hackmann
21628be3f8fSGreg Hackmann err = setup_timer(clock_id, flags, interval, &tm1);
217*eefd95e1SShuah Khan /* Unsupported case - return 0 to not fail the test */
21828be3f8fSGreg Hackmann if (err)
219*eefd95e1SShuah Khan return err == 1 ? 0 : err;
22028be3f8fSGreg Hackmann
22128be3f8fSGreg Hackmann while (alarmcount < 5)
22228be3f8fSGreg Hackmann sleep(1);
22328be3f8fSGreg Hackmann
22428be3f8fSGreg Hackmann timer_delete(tm1);
22528be3f8fSGreg Hackmann return check_timer_latency(flags, interval);
22628be3f8fSGreg Hackmann }
22728be3f8fSGreg Hackmann
do_timer_oneshot(int clock_id,int flags)22828be3f8fSGreg Hackmann int do_timer_oneshot(int clock_id, int flags)
22928be3f8fSGreg Hackmann {
23028be3f8fSGreg Hackmann timer_t tm1;
23128be3f8fSGreg Hackmann const int interval = 0;
23228be3f8fSGreg Hackmann struct timeval timeout;
23328be3f8fSGreg Hackmann int err;
23428be3f8fSGreg Hackmann
23528be3f8fSGreg Hackmann err = setup_timer(clock_id, flags, interval, &tm1);
236*eefd95e1SShuah Khan /* Unsupported case - return 0 to not fail the test */
23728be3f8fSGreg Hackmann if (err)
238*eefd95e1SShuah Khan return err == 1 ? 0 : err;
23928be3f8fSGreg Hackmann
24028be3f8fSGreg Hackmann memset(&timeout, 0, sizeof(timeout));
24128be3f8fSGreg Hackmann timeout.tv_sec = 5;
24228be3f8fSGreg Hackmann do {
24301db7fbfSShuah Khan err = select(0, NULL, NULL, NULL, &timeout);
24428be3f8fSGreg Hackmann } while (err == -1 && errno == EINTR);
24528be3f8fSGreg Hackmann
24628be3f8fSGreg Hackmann timer_delete(tm1);
24728be3f8fSGreg Hackmann err = check_timer_latency(flags, interval);
24828be3f8fSGreg Hackmann err |= check_alarmcount(flags, interval);
24928be3f8fSGreg Hackmann return err;
25028be3f8fSGreg Hackmann }
25128be3f8fSGreg Hackmann
main(void)2524e40d0a2SJohn Stultz int main(void)
2534e40d0a2SJohn Stultz {
2544e40d0a2SJohn Stultz struct sigaction act;
2554e40d0a2SJohn Stultz int signum = SIGRTMAX;
2564e40d0a2SJohn Stultz int ret = 0;
2574e40d0a2SJohn Stultz
2584e40d0a2SJohn Stultz /* Set up signal handler: */
2594e40d0a2SJohn Stultz sigfillset(&act.sa_mask);
2604e40d0a2SJohn Stultz act.sa_flags = 0;
2614e40d0a2SJohn Stultz act.sa_handler = sigalarm;
2624e40d0a2SJohn Stultz sigaction(signum, &act, NULL);
2634e40d0a2SJohn Stultz
2644e40d0a2SJohn Stultz printf("Setting timers for every %i seconds\n", TIMER_SECS);
2654e40d0a2SJohn Stultz for (clock_id = 0; clock_id < NR_CLOCKIDS; clock_id++) {
2664e40d0a2SJohn Stultz
2674e40d0a2SJohn Stultz if ((clock_id == CLOCK_PROCESS_CPUTIME_ID) ||
2684e40d0a2SJohn Stultz (clock_id == CLOCK_THREAD_CPUTIME_ID) ||
2694e40d0a2SJohn Stultz (clock_id == CLOCK_MONOTONIC_RAW) ||
2704e40d0a2SJohn Stultz (clock_id == CLOCK_REALTIME_COARSE) ||
2714e40d0a2SJohn Stultz (clock_id == CLOCK_MONOTONIC_COARSE) ||
2724e40d0a2SJohn Stultz (clock_id == CLOCK_HWSPECIFIC))
2734e40d0a2SJohn Stultz continue;
2744e40d0a2SJohn Stultz
2754e40d0a2SJohn Stultz ret |= do_timer(clock_id, TIMER_ABSTIME);
2764e40d0a2SJohn Stultz ret |= do_timer(clock_id, 0);
27728be3f8fSGreg Hackmann ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME);
27828be3f8fSGreg Hackmann ret |= do_timer_oneshot(clock_id, 0);
2794e40d0a2SJohn Stultz }
2804e40d0a2SJohn Stultz if (ret)
2814e40d0a2SJohn Stultz return ksft_exit_fail();
2824e40d0a2SJohn Stultz return ksft_exit_pass();
2834e40d0a2SJohn Stultz }
284