xref: /linux/tools/testing/selftests/timers/set-timer-lat.c (revision 3b4128b9f374b4219eb716f4ad8a307bc7eb3d84)
1 /* set_timer latency test
2  *		John Stultz (john.stultz@linaro.org)
3  *              (C) Copyright Linaro 2014
4  *              Licensed under the GPLv2
5  *
6  *   This test makes sure the set_timer api is correct
7  *
8  *  To build:
9  *	$ gcc set-timer-lat.c -o set-timer-lat -lrt
10  *
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.
15  *
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.
20  */
21 
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <pthread.h>
31 #include "clock-helpers.h"
32 #include "kselftest.h"
33 
34 /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
35 #define CLOCK_HWSPECIFIC		10
36 
37 #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
38 
39 #define TIMER_SECS 1
40 int alarmcount;
41 int clock_id;
42 struct timespec start_time;
43 long long max_latency_ns;
44 int timer_fired_early;
45 
46 long long timespec_sub(struct timespec a, struct timespec b)
47 {
48 	long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
49 
50 	ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
51 	return ret;
52 }
53 
54 
55 void sigalarm(int signo)
56 {
57 	long long delta_ns;
58 	struct timespec ts;
59 
60 	clock_gettime(clock_id, &ts);
61 	alarmcount++;
62 
63 	delta_ns = timespec_sub(start_time, ts);
64 	delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount;
65 
66 	if (delta_ns < 0)
67 		timer_fired_early = 1;
68 
69 	if (delta_ns > max_latency_ns)
70 		max_latency_ns = delta_ns;
71 }
72 
73 void describe_timer(int flags, int interval)
74 {
75 	printf("%-22s %s %s ",
76 			clock_name(clock_id),
77 			flags ? "ABSTIME":"RELTIME",
78 			interval ? "PERIODIC":"ONE-SHOT");
79 }
80 
81 int setup_timer(int clock_id, int flags, int interval, timer_t *tm1)
82 {
83 	struct sigevent se;
84 	struct itimerspec its1, its2;
85 	int err;
86 
87 	/* Set up timer: */
88 	memset(&se, 0, sizeof(se));
89 	se.sigev_notify = SIGEV_SIGNAL;
90 	se.sigev_signo = SIGRTMAX;
91 	se.sigev_value.sival_int = 0;
92 
93 	max_latency_ns = 0;
94 	alarmcount = 0;
95 	timer_fired_early = 0;
96 
97 	err = timer_create(clock_id, &se, tm1);
98 	if (err) {
99 		if ((clock_id == CLOCK_REALTIME_ALARM) ||
100 		    (clock_id == CLOCK_BOOTTIME_ALARM)) {
101 			printf("%-22s %s missing CAP_WAKE_ALARM?    : [UNSUPPORTED]\n",
102 					clock_name(clock_id),
103 					flags ? "ABSTIME":"RELTIME");
104 			/* Indicate timer isn't set, so caller doesn't wait */
105 			return 1;
106 		}
107 		printf("%s - timer_create() failed\n", clock_name(clock_id));
108 		return -1;
109 	}
110 
111 	clock_gettime(clock_id, &start_time);
112 	if (flags) {
113 		its1.it_value = start_time;
114 		its1.it_value.tv_sec += TIMER_SECS;
115 	} else {
116 		its1.it_value.tv_sec = TIMER_SECS;
117 		its1.it_value.tv_nsec = 0;
118 	}
119 	its1.it_interval.tv_sec = interval;
120 	its1.it_interval.tv_nsec = 0;
121 
122 	err = timer_settime(*tm1, flags, &its1, &its2);
123 	if (err) {
124 		printf("%s - timer_settime() failed\n", clock_name(clock_id));
125 		return -1;
126 	}
127 
128 	return 0;
129 }
130 
131 int check_timer_latency(int flags, int interval)
132 {
133 	int err = 0;
134 
135 	describe_timer(flags, interval);
136 	printf("timer fired early: %7d : ", timer_fired_early);
137 	if (!timer_fired_early) {
138 		printf("[OK]\n");
139 	} else {
140 		printf("[FAILED]\n");
141 		err = -1;
142 	}
143 
144 	describe_timer(flags, interval);
145 	printf("max latency: %10lld ns : ", max_latency_ns);
146 
147 	if (max_latency_ns < UNRESONABLE_LATENCY) {
148 		printf("[OK]\n");
149 	} else {
150 		printf("[FAILED]\n");
151 		err = -1;
152 	}
153 	return err;
154 }
155 
156 int check_alarmcount(int flags, int interval)
157 {
158 	describe_timer(flags, interval);
159 	printf("count: %19d : ", alarmcount);
160 	if (alarmcount == 1) {
161 		printf("[OK]\n");
162 		return 0;
163 	}
164 	printf("[FAILED]\n");
165 	return -1;
166 }
167 
168 int do_timer(int clock_id, int flags)
169 {
170 	timer_t tm1;
171 	const int interval = TIMER_SECS;
172 	int err;
173 
174 	err = setup_timer(clock_id, flags, interval, &tm1);
175 	/* Unsupported case - return 0 to not fail the test */
176 	if (err)
177 		return err == 1 ? 0 : err;
178 
179 	while (alarmcount < 5)
180 		sleep(1);
181 
182 	timer_delete(tm1);
183 	return check_timer_latency(flags, interval);
184 }
185 
186 int do_timer_oneshot(int clock_id, int flags)
187 {
188 	timer_t tm1;
189 	const int interval = 0;
190 	struct timeval timeout;
191 	int err;
192 
193 	err = setup_timer(clock_id, flags, interval, &tm1);
194 	/* Unsupported case - return 0 to not fail the test */
195 	if (err)
196 		return err == 1 ? 0 : err;
197 
198 	memset(&timeout, 0, sizeof(timeout));
199 	timeout.tv_sec = 5;
200 	do {
201 		err = select(0, NULL, NULL, NULL, &timeout);
202 	} while (err == -1 && errno == EINTR);
203 
204 	timer_delete(tm1);
205 	err = check_timer_latency(flags, interval);
206 	err |= check_alarmcount(flags, interval);
207 	return err;
208 }
209 
210 int main(void)
211 {
212 	struct sigaction act;
213 	int signum = SIGRTMAX;
214 	int ret = 0;
215 	int max_clocks = CLOCK_TAI + 1;
216 
217 	/* Set up signal handler: */
218 	sigfillset(&act.sa_mask);
219 	act.sa_flags = 0;
220 	act.sa_handler = sigalarm;
221 	sigaction(signum, &act, NULL);
222 
223 	printf("Setting timers for every %i seconds\n", TIMER_SECS);
224 	for (clock_id = 0; clock_id < max_clocks; clock_id++) {
225 
226 		if ((clock_id == CLOCK_PROCESS_CPUTIME_ID) ||
227 				(clock_id == CLOCK_THREAD_CPUTIME_ID) ||
228 				(clock_id == CLOCK_MONOTONIC_RAW) ||
229 				(clock_id == CLOCK_REALTIME_COARSE) ||
230 				(clock_id == CLOCK_MONOTONIC_COARSE) ||
231 				(clock_id == CLOCK_HWSPECIFIC))
232 			continue;
233 
234 		ret |= do_timer(clock_id, TIMER_ABSTIME);
235 		ret |= do_timer(clock_id, 0);
236 		ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME);
237 		ret |= do_timer_oneshot(clock_id, 0);
238 	}
239 	if (ret)
240 		ksft_exit_fail();
241 	ksft_exit_pass();
242 }
243