1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
5 * Selftests for a few posix timers interface.
7 * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
11 #include <sys/types.h>
18 #include <include/vdso/time64.h>
21 #include "../kselftest.h"
25 static void __fatal_error(const char *test
, const char *name
, const char *what
)
30 ret_str
= strerror_r(errno
, buf
, sizeof(buf
));
32 if (name
&& strlen(name
) && ret_str
)
33 ksft_exit_fail_msg("%s %s %s %s\n", test
, name
, what
, ret_str
);
35 ksft_exit_fail_msg("%s %s %s\n", test
, what
, ret_str
);
37 ksft_exit_fail_msg("%s %s\n", test
, what
);
41 #define fatal_error(name, what) __fatal_error(__func__, name, what)
43 static volatile int done
;
45 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
46 static void user_loop(void)
52 * Try to spend as much time as possible in kernelspace
53 * to elapse ITIMER_PROF.
55 static void kernel_loop(void)
60 while (!done
&& !err
) {
61 err
= brk(addr
+ 4096);
67 * Sleep until ITIMER_REAL expiration.
69 static void idle_loop(void)
74 static void sig_handler(int nr
)
80 * Check the expected timer expiration matches the GTOD elapsed delta since
81 * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
83 static int check_diff(struct timeval start
, struct timeval end
)
87 diff
= end
.tv_usec
- start
.tv_usec
;
88 diff
+= (end
.tv_sec
- start
.tv_sec
) * USEC_PER_SEC
;
90 if (llabs(diff
- DELAY
* USEC_PER_SEC
) > USEC_PER_SEC
/ 2) {
91 printf("Diff too high: %lld..", diff
);
98 static void check_itimer(int which
, const char *name
)
100 struct timeval start
, end
;
101 struct itimerval val
= {
102 .it_value
.tv_sec
= DELAY
,
107 if (which
== ITIMER_VIRTUAL
)
108 signal(SIGVTALRM
, sig_handler
);
109 else if (which
== ITIMER_PROF
)
110 signal(SIGPROF
, sig_handler
);
111 else if (which
== ITIMER_REAL
)
112 signal(SIGALRM
, sig_handler
);
114 if (gettimeofday(&start
, NULL
) < 0)
115 fatal_error(name
, "gettimeofday()");
117 if (setitimer(which
, &val
, NULL
) < 0)
118 fatal_error(name
, "setitimer()");
120 if (which
== ITIMER_VIRTUAL
)
122 else if (which
== ITIMER_PROF
)
124 else if (which
== ITIMER_REAL
)
127 if (gettimeofday(&end
, NULL
) < 0)
128 fatal_error(name
, "gettimeofday()");
130 ksft_test_result(check_diff(start
, end
) == 0, "%s\n", name
);
133 static void check_timer_create(int which
, const char *name
)
135 struct timeval start
, end
;
136 struct itimerspec val
= {
137 .it_value
.tv_sec
= DELAY
,
143 if (timer_create(which
, NULL
, &id
) < 0)
144 fatal_error(name
, "timer_create()");
146 if (signal(SIGALRM
, sig_handler
) == SIG_ERR
)
147 fatal_error(name
, "signal()");
149 if (gettimeofday(&start
, NULL
) < 0)
150 fatal_error(name
, "gettimeofday()");
152 if (timer_settime(id
, 0, &val
, NULL
) < 0)
153 fatal_error(name
, "timer_settime()");
157 if (gettimeofday(&end
, NULL
) < 0)
158 fatal_error(name
, "gettimeofday()");
160 ksft_test_result(check_diff(start
, end
) == 0,
161 "timer_create() per %s\n", name
);
164 static pthread_t ctd_thread
;
165 static volatile int ctd_count
, ctd_failed
;
167 static void ctd_sighandler(int sig
)
169 if (pthread_self() != ctd_thread
)
174 static void *ctd_thread_func(void *arg
)
176 struct itimerspec val
= {
177 .it_value
.tv_sec
= 0,
178 .it_value
.tv_nsec
= 1000 * 1000,
179 .it_interval
.tv_sec
= 0,
180 .it_interval
.tv_nsec
= 1000 * 1000,
184 /* 1/10 seconds to ensure the leader sleeps */
188 if (timer_create(CLOCK_PROCESS_CPUTIME_ID
, NULL
, &id
))
189 fatal_error(NULL
, "timer_create()");
190 if (timer_settime(id
, 0, &val
, NULL
))
191 fatal_error(NULL
, "timer_settime()");
192 while (ctd_count
> 0 && !ctd_failed
)
195 if (timer_delete(id
))
196 fatal_error(NULL
, "timer_delete()");
202 * Test that only the running thread receives the timer signal.
204 static void check_timer_distribution(void)
206 if (signal(SIGALRM
, ctd_sighandler
) == SIG_ERR
)
207 fatal_error(NULL
, "signal()");
209 if (pthread_create(&ctd_thread
, NULL
, ctd_thread_func
, NULL
))
210 fatal_error(NULL
, "pthread_create()");
212 if (pthread_join(ctd_thread
, NULL
))
213 fatal_error(NULL
, "pthread_join()");
216 ksft_test_result_pass("check signal distribution\n");
217 else if (ksft_min_kernel_version(6, 3))
218 ksft_test_result_fail("check signal distribution\n");
220 ksft_test_result_skip("check signal distribution (old kernel)\n");
228 static void siginfo_handler(int sig
, siginfo_t
*si
, void *uc
)
230 struct tmrsig
*tsig
= si
? si
->si_ptr
: NULL
;
234 tsig
->overruns
+= si
->si_overrun
;
238 static void *ignore_thread(void *arg
)
240 unsigned int *tid
= arg
;
244 sigaddset(&set
, SIGUSR1
);
245 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
246 fatal_error(NULL
, "sigprocmask(SIG_BLOCK)");
251 if (sigprocmask(SIG_UNBLOCK
, &set
, NULL
))
252 fatal_error(NULL
, "sigprocmask(SIG_UNBLOCK)");
256 static void check_sig_ign(int thread
)
258 struct tmrsig tsig
= { };
259 struct itimerspec its
;
260 unsigned int tid
= 0;
268 if (pthread_create(&pthread
, NULL
, ignore_thread
, &tid
))
269 fatal_error(NULL
, "pthread_create()");
273 sa
.sa_flags
= SA_SIGINFO
;
274 sa
.sa_sigaction
= siginfo_handler
;
275 sigemptyset(&sa
.sa_mask
);
276 if (sigaction(SIGUSR1
, &sa
, NULL
))
277 fatal_error(NULL
, "sigaction()");
279 /* Block the signal */
281 sigaddset(&set
, SIGUSR1
);
282 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
283 fatal_error(NULL
, "sigprocmask(SIG_BLOCK)");
285 memset(&sev
, 0, sizeof(sev
));
286 sev
.sigev_notify
= SIGEV_SIGNAL
;
287 sev
.sigev_signo
= SIGUSR1
;
288 sev
.sigev_value
.sival_ptr
= &tsig
;
290 sev
.sigev_notify
= SIGEV_THREAD_ID
;
291 sev
._sigev_un
._tid
= tid
;
294 if (timer_create(CLOCK_MONOTONIC
, &sev
, &timerid
))
295 fatal_error(NULL
, "timer_create()");
297 /* Start the timer to expire in 100ms and 100ms intervals */
298 its
.it_value
.tv_sec
= 0;
299 its
.it_value
.tv_nsec
= 100000000;
300 its
.it_interval
.tv_sec
= 0;
301 its
.it_interval
.tv_nsec
= 100000000;
302 timer_settime(timerid
, 0, &its
, NULL
);
306 /* Set the signal to be ignored */
307 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
)
308 fatal_error(NULL
, "signal(SIG_IGN)");
313 /* Stop the thread first. No signal should be delivered to it */
314 if (pthread_cancel(pthread
))
315 fatal_error(NULL
, "pthread_cancel()");
316 if (pthread_join(pthread
, NULL
))
317 fatal_error(NULL
, "pthread_join()");
320 /* Restore the handler */
321 if (sigaction(SIGUSR1
, &sa
, NULL
))
322 fatal_error(NULL
, "sigaction()");
326 /* Unblock it, which should deliver the signal in the !thread case*/
327 if (sigprocmask(SIG_UNBLOCK
, &set
, NULL
))
328 fatal_error(NULL
, "sigprocmask(SIG_UNBLOCK)");
330 if (timer_delete(timerid
))
331 fatal_error(NULL
, "timer_delete()");
334 ksft_test_result(tsig
.signals
== 1 && tsig
.overruns
== 29,
335 "check_sig_ign SIGEV_SIGNAL\n");
337 ksft_test_result(tsig
.signals
== 0 && tsig
.overruns
== 0,
338 "check_sig_ign SIGEV_THREAD_ID\n");
342 static void check_rearm(void)
344 struct tmrsig tsig
= { };
345 struct itimerspec its
;
351 sa
.sa_flags
= SA_SIGINFO
;
352 sa
.sa_sigaction
= siginfo_handler
;
353 sigemptyset(&sa
.sa_mask
);
354 if (sigaction(SIGUSR1
, &sa
, NULL
))
355 fatal_error(NULL
, "sigaction()");
357 /* Block the signal */
359 sigaddset(&set
, SIGUSR1
);
360 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
361 fatal_error(NULL
, "sigprocmask(SIG_BLOCK)");
363 memset(&sev
, 0, sizeof(sev
));
364 sev
.sigev_notify
= SIGEV_SIGNAL
;
365 sev
.sigev_signo
= SIGUSR1
;
366 sev
.sigev_value
.sival_ptr
= &tsig
;
367 if (timer_create(CLOCK_MONOTONIC
, &sev
, &timerid
))
368 fatal_error(NULL
, "timer_create()");
370 /* Start the timer to expire in 100ms and 100ms intervals */
371 its
.it_value
.tv_sec
= 0;
372 its
.it_value
.tv_nsec
= 100000000;
373 its
.it_interval
.tv_sec
= 0;
374 its
.it_interval
.tv_nsec
= 100000000;
375 if (timer_settime(timerid
, 0, &its
, NULL
))
376 fatal_error(NULL
, "timer_settime()");
380 /* Reprogram the timer to single shot */
381 its
.it_value
.tv_sec
= 10;
382 its
.it_value
.tv_nsec
= 0;
383 its
.it_interval
.tv_sec
= 0;
384 its
.it_interval
.tv_nsec
= 0;
385 if (timer_settime(timerid
, 0, &its
, NULL
))
386 fatal_error(NULL
, "timer_settime()");
388 /* Unblock it, which should not deliver a signal */
389 if (sigprocmask(SIG_UNBLOCK
, &set
, NULL
))
390 fatal_error(NULL
, "sigprocmask(SIG_UNBLOCK)");
392 if (timer_delete(timerid
))
393 fatal_error(NULL
, "timer_delete()");
395 ksft_test_result(!tsig
.signals
, "check_rearm\n");
398 static void check_delete(void)
400 struct tmrsig tsig
= { };
401 struct itimerspec its
;
407 sa
.sa_flags
= SA_SIGINFO
;
408 sa
.sa_sigaction
= siginfo_handler
;
409 sigemptyset(&sa
.sa_mask
);
410 if (sigaction(SIGUSR1
, &sa
, NULL
))
411 fatal_error(NULL
, "sigaction()");
413 /* Block the signal */
415 sigaddset(&set
, SIGUSR1
);
416 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
417 fatal_error(NULL
, "sigprocmask(SIG_BLOCK)");
419 memset(&sev
, 0, sizeof(sev
));
420 sev
.sigev_notify
= SIGEV_SIGNAL
;
421 sev
.sigev_signo
= SIGUSR1
;
422 sev
.sigev_value
.sival_ptr
= &tsig
;
423 if (timer_create(CLOCK_MONOTONIC
, &sev
, &timerid
))
424 fatal_error(NULL
, "timer_create()");
426 /* Start the timer to expire in 100ms and 100ms intervals */
427 its
.it_value
.tv_sec
= 0;
428 its
.it_value
.tv_nsec
= 100000000;
429 its
.it_interval
.tv_sec
= 0;
430 its
.it_interval
.tv_nsec
= 100000000;
431 if (timer_settime(timerid
, 0, &its
, NULL
))
432 fatal_error(NULL
, "timer_settime()");
436 if (timer_delete(timerid
))
437 fatal_error(NULL
, "timer_delete()");
439 /* Unblock it, which should not deliver a signal */
440 if (sigprocmask(SIG_UNBLOCK
, &set
, NULL
))
441 fatal_error(NULL
, "sigprocmask(SIG_UNBLOCK)");
443 ksft_test_result(!tsig
.signals
, "check_delete\n");
446 static inline int64_t calcdiff_ns(struct timespec t1
, struct timespec t2
)
450 diff
= NSEC_PER_SEC
* (int64_t)((int) t1
.tv_sec
- (int) t2
.tv_sec
);
451 diff
+= ((int) t1
.tv_nsec
- (int) t2
.tv_nsec
);
455 static void check_sigev_none(int which
, const char *name
)
457 struct timespec start
, now
;
458 struct itimerspec its
;
462 memset(&sev
, 0, sizeof(sev
));
463 sev
.sigev_notify
= SIGEV_NONE
;
465 if (timer_create(which
, &sev
, &timerid
))
466 fatal_error(name
, "timer_create()");
468 /* Start the timer to expire in 100ms and 100ms intervals */
469 its
.it_value
.tv_sec
= 0;
470 its
.it_value
.tv_nsec
= 100000000;
471 its
.it_interval
.tv_sec
= 0;
472 its
.it_interval
.tv_nsec
= 100000000;
473 timer_settime(timerid
, 0, &its
, NULL
);
475 if (clock_gettime(which
, &start
))
476 fatal_error(name
, "clock_gettime()");
479 if (clock_gettime(which
, &now
))
480 fatal_error(name
, "clock_gettime()");
481 } while (calcdiff_ns(now
, start
) < NSEC_PER_SEC
);
483 if (timer_gettime(timerid
, &its
))
484 fatal_error(name
, "timer_gettime()");
486 if (timer_delete(timerid
))
487 fatal_error(name
, "timer_delete()");
489 ksft_test_result(its
.it_value
.tv_sec
|| its
.it_value
.tv_nsec
,
490 "check_sigev_none %s\n", name
);
493 static void check_gettime(int which
, const char *name
)
495 struct itimerspec its
, prev
;
496 struct timespec start
, now
;
502 /* Block the signal */
504 sigaddset(&set
, SIGUSR1
);
505 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
506 fatal_error(name
, "sigprocmask(SIG_BLOCK)");
508 memset(&sev
, 0, sizeof(sev
));
509 sev
.sigev_notify
= SIGEV_SIGNAL
;
510 sev
.sigev_signo
= SIGUSR1
;
512 if (timer_create(which
, &sev
, &timerid
))
513 fatal_error(name
, "timer_create()");
515 /* Start the timer to expire in 100ms and 100ms intervals */
516 its
.it_value
.tv_sec
= 0;
517 its
.it_value
.tv_nsec
= 100000000;
518 its
.it_interval
.tv_sec
= 0;
519 its
.it_interval
.tv_nsec
= 100000000;
520 if (timer_settime(timerid
, 0, &its
, NULL
))
521 fatal_error(name
, "timer_settime()");
523 if (timer_gettime(timerid
, &prev
))
524 fatal_error(name
, "timer_gettime()");
526 if (clock_gettime(which
, &start
))
527 fatal_error(name
, "clock_gettime()");
530 if (clock_gettime(which
, &now
))
531 fatal_error(name
, "clock_gettime()");
532 if (timer_gettime(timerid
, &its
))
533 fatal_error(name
, "timer_gettime()");
534 if (its
.it_value
.tv_nsec
> prev
.it_value
.tv_nsec
)
538 } while (calcdiff_ns(now
, start
) < NSEC_PER_SEC
);
540 if (timer_delete(timerid
))
541 fatal_error(name
, "timer_delete()");
543 ksft_test_result(wraps
> 1, "check_gettime %s\n", name
);
546 static void check_overrun(int which
, const char *name
)
548 struct timespec start
, now
;
549 struct tmrsig tsig
= { };
550 struct itimerspec its
;
556 sa
.sa_flags
= SA_SIGINFO
;
557 sa
.sa_sigaction
= siginfo_handler
;
558 sigemptyset(&sa
.sa_mask
);
559 if (sigaction(SIGUSR1
, &sa
, NULL
))
560 fatal_error(name
, "sigaction()");
562 /* Block the signal */
564 sigaddset(&set
, SIGUSR1
);
565 if (sigprocmask(SIG_BLOCK
, &set
, NULL
))
566 fatal_error(name
, "sigprocmask(SIG_BLOCK)");
568 memset(&sev
, 0, sizeof(sev
));
569 sev
.sigev_notify
= SIGEV_SIGNAL
;
570 sev
.sigev_signo
= SIGUSR1
;
571 sev
.sigev_value
.sival_ptr
= &tsig
;
572 if (timer_create(which
, &sev
, &timerid
))
573 fatal_error(name
, "timer_create()");
575 /* Start the timer to expire in 100ms and 100ms intervals */
576 its
.it_value
.tv_sec
= 0;
577 its
.it_value
.tv_nsec
= 100000000;
578 its
.it_interval
.tv_sec
= 0;
579 its
.it_interval
.tv_nsec
= 100000000;
580 if (timer_settime(timerid
, 0, &its
, NULL
))
581 fatal_error(name
, "timer_settime()");
583 if (clock_gettime(which
, &start
))
584 fatal_error(name
, "clock_gettime()");
587 if (clock_gettime(which
, &now
))
588 fatal_error(name
, "clock_gettime()");
589 } while (calcdiff_ns(now
, start
) < NSEC_PER_SEC
);
591 /* Unblock it, which should deliver a signal */
592 if (sigprocmask(SIG_UNBLOCK
, &set
, NULL
))
593 fatal_error(name
, "sigprocmask(SIG_UNBLOCK)");
595 if (timer_delete(timerid
))
596 fatal_error(name
, "timer_delete()");
598 ksft_test_result(tsig
.signals
== 1 && tsig
.overruns
== 9,
599 "check_overrun %s\n", name
);
602 int main(int argc
, char **argv
)
607 ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
608 ksft_print_msg("based timers if other threads run on the CPU...\n");
610 check_itimer(ITIMER_VIRTUAL
, "ITIMER_VIRTUAL");
611 check_itimer(ITIMER_PROF
, "ITIMER_PROF");
612 check_itimer(ITIMER_REAL
, "ITIMER_REAL");
613 check_timer_create(CLOCK_THREAD_CPUTIME_ID
, "CLOCK_THREAD_CPUTIME_ID");
616 * It's unfortunately hard to reliably test a timer expiration
617 * on parallel multithread cputime. We could arm it to expire
618 * on DELAY * nr_threads, with nr_threads busy looping, then wait
619 * the normal DELAY since the time is elapsing nr_threads faster.
620 * But for that we need to ensure we have real physical free CPUs
621 * to ensure true parallelism. So test only one thread until we
622 * find a better solution.
624 check_timer_create(CLOCK_PROCESS_CPUTIME_ID
, "CLOCK_PROCESS_CPUTIME_ID");
625 check_timer_distribution();
631 check_sigev_none(CLOCK_MONOTONIC
, "CLOCK_MONOTONIC");
632 check_sigev_none(CLOCK_PROCESS_CPUTIME_ID
, "CLOCK_PROCESS_CPUTIME_ID");
633 check_gettime(CLOCK_MONOTONIC
, "CLOCK_MONOTONIC");
634 check_gettime(CLOCK_PROCESS_CPUTIME_ID
, "CLOCK_PROCESS_CPUTIME_ID");
635 check_gettime(CLOCK_THREAD_CPUTIME_ID
, "CLOCK_THREAD_CPUTIME_ID");
636 check_overrun(CLOCK_MONOTONIC
, "CLOCK_MONOTONIC");
637 check_overrun(CLOCK_PROCESS_CPUTIME_ID
, "CLOCK_PROCESS_CPUTIME_ID");
638 check_overrun(CLOCK_THREAD_CPUTIME_ID
, "CLOCK_THREAD_CPUTIME_ID");