aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/timers/set-timer-lat.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/timers/set-timer-lat.c')
-rw-r--r--tools/testing/selftests/timers/set-timer-lat.c103
1 files changed, 90 insertions, 13 deletions
diff --git a/tools/testing/selftests/timers/set-timer-lat.c b/tools/testing/selftests/timers/set-timer-lat.c
index 4fc98c5b0899..15434da23b04 100644
--- a/tools/testing/selftests/timers/set-timer-lat.c
+++ b/tools/testing/selftests/timers/set-timer-lat.c
@@ -20,6 +20,7 @@
20 */ 20 */
21 21
22 22
23#include <errno.h>
23#include <stdio.h> 24#include <stdio.h>
24#include <unistd.h> 25#include <unistd.h>
25#include <time.h> 26#include <time.h>
@@ -63,6 +64,7 @@ int alarmcount;
63int clock_id; 64int clock_id;
64struct timespec start_time; 65struct timespec start_time;
65long long max_latency_ns; 66long long max_latency_ns;
67int timer_fired_early;
66 68
67char *clockstring(int clockid) 69char *clockstring(int clockid)
68{ 70{
@@ -115,16 +117,23 @@ void sigalarm(int signo)
115 delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount; 117 delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount;
116 118
117 if (delta_ns < 0) 119 if (delta_ns < 0)
118 printf("%s timer fired early: FAIL\n", clockstring(clock_id)); 120 timer_fired_early = 1;
119 121
120 if (delta_ns > max_latency_ns) 122 if (delta_ns > max_latency_ns)
121 max_latency_ns = delta_ns; 123 max_latency_ns = delta_ns;
122} 124}
123 125
124int do_timer(int clock_id, int flags) 126void describe_timer(int flags, int interval)
127{
128 printf("%-22s %s %s ",
129 clockstring(clock_id),
130 flags ? "ABSTIME":"RELTIME",
131 interval ? "PERIODIC":"ONE-SHOT");
132}
133
134int setup_timer(int clock_id, int flags, int interval, timer_t *tm1)
125{ 135{
126 struct sigevent se; 136 struct sigevent se;
127 timer_t tm1;
128 struct itimerspec its1, its2; 137 struct itimerspec its1, its2;
129 int err; 138 int err;
130 139
@@ -136,8 +145,9 @@ int do_timer(int clock_id, int flags)
136 145
137 max_latency_ns = 0; 146 max_latency_ns = 0;
138 alarmcount = 0; 147 alarmcount = 0;
148 timer_fired_early = 0;
139 149
140 err = timer_create(clock_id, &se, &tm1); 150 err = timer_create(clock_id, &se, tm1);
141 if (err) { 151 if (err) {
142 if ((clock_id == CLOCK_REALTIME_ALARM) || 152 if ((clock_id == CLOCK_REALTIME_ALARM) ||
143 (clock_id == CLOCK_BOOTTIME_ALARM)) { 153 (clock_id == CLOCK_BOOTTIME_ALARM)) {
@@ -158,32 +168,97 @@ int do_timer(int clock_id, int flags)
158 its1.it_value.tv_sec = TIMER_SECS; 168 its1.it_value.tv_sec = TIMER_SECS;
159 its1.it_value.tv_nsec = 0; 169 its1.it_value.tv_nsec = 0;
160 } 170 }
161 its1.it_interval.tv_sec = TIMER_SECS; 171 its1.it_interval.tv_sec = interval;
162 its1.it_interval.tv_nsec = 0; 172 its1.it_interval.tv_nsec = 0;
163 173
164 err = timer_settime(tm1, flags, &its1, &its2); 174 err = timer_settime(*tm1, flags, &its1, &its2);
165 if (err) { 175 if (err) {
166 printf("%s - timer_settime() failed\n", clockstring(clock_id)); 176 printf("%s - timer_settime() failed\n", clockstring(clock_id));
167 return -1; 177 return -1;
168 } 178 }
169 179
170 while (alarmcount < 5) 180 return 0;
171 sleep(1); 181}
172 182
173 printf("%-22s %s max latency: %10lld ns : ", 183int check_timer_latency(int flags, int interval)
174 clockstring(clock_id), 184{
175 flags ? "ABSTIME":"RELTIME", 185 int err = 0;
176 max_latency_ns); 186
187 describe_timer(flags, interval);
188 printf("timer fired early: %7d : ", timer_fired_early);
189 if (!timer_fired_early) {
190 printf("[OK]\n");
191 } else {
192 printf("[FAILED]\n");
193 err = -1;
194 }
195
196 describe_timer(flags, interval);
197 printf("max latency: %10lld ns : ", max_latency_ns);
177 198
178 timer_delete(tm1);
179 if (max_latency_ns < UNRESONABLE_LATENCY) { 199 if (max_latency_ns < UNRESONABLE_LATENCY) {
180 printf("[OK]\n"); 200 printf("[OK]\n");
201 } else {
202 printf("[FAILED]\n");
203 err = -1;
204 }
205 return err;
206}
207
208int check_alarmcount(int flags, int interval)
209{
210 describe_timer(flags, interval);
211 printf("count: %19d : ", alarmcount);
212 if (alarmcount == 1) {
213 printf("[OK]\n");
181 return 0; 214 return 0;
182 } 215 }
183 printf("[FAILED]\n"); 216 printf("[FAILED]\n");
184 return -1; 217 return -1;
185} 218}
186 219
220int do_timer(int clock_id, int flags)
221{
222 timer_t tm1;
223 const int interval = TIMER_SECS;
224 int err;
225
226 err = setup_timer(clock_id, flags, interval, &tm1);
227 if (err)
228 return err;
229
230 while (alarmcount < 5)
231 sleep(1);
232
233 timer_delete(tm1);
234 return check_timer_latency(flags, interval);
235}
236
237int do_timer_oneshot(int clock_id, int flags)
238{
239 timer_t tm1;
240 const int interval = 0;
241 struct timeval timeout;
242 fd_set fds;
243 int err;
244
245 err = setup_timer(clock_id, flags, interval, &tm1);
246 if (err)
247 return err;
248
249 memset(&timeout, 0, sizeof(timeout));
250 timeout.tv_sec = 5;
251 FD_ZERO(&fds);
252 do {
253 err = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
254 } while (err == -1 && errno == EINTR);
255
256 timer_delete(tm1);
257 err = check_timer_latency(flags, interval);
258 err |= check_alarmcount(flags, interval);
259 return err;
260}
261
187int main(void) 262int main(void)
188{ 263{
189 struct sigaction act; 264 struct sigaction act;
@@ -209,6 +284,8 @@ int main(void)
209 284
210 ret |= do_timer(clock_id, TIMER_ABSTIME); 285 ret |= do_timer(clock_id, TIMER_ABSTIME);
211 ret |= do_timer(clock_id, 0); 286 ret |= do_timer(clock_id, 0);
287 ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME);
288 ret |= do_timer_oneshot(clock_id, 0);
212 } 289 }
213 if (ret) 290 if (ret)
214 return ksft_exit_fail(); 291 return ksft_exit_fail();