xref: /linux/tools/testing/selftests/timers/posix_timers.c (revision c94cd9508b1335b949fd13ebd269313c65492df0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4  *
5  * Selftests for a few posix timers interface.
6  *
7  * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
8  */
9 #define _GNU_SOURCE
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <stdint.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <time.h>
18 #include <pthread.h>
19 
20 #include "../kselftest.h"
21 
22 #define DELAY 2
23 #define USECS_PER_SEC 1000000
24 #define NSECS_PER_SEC 1000000000
25 
26 static void __fatal_error(const char *test, const char *name, const char *what)
27 {
28 	char buf[64];
29 
30 	strerror_r(errno, buf, sizeof(buf));
31 
32 	if (name && strlen(name))
33 		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf);
34 	else
35 		ksft_exit_fail_msg("%s %s %s\n", test, what, buf);
36 }
37 
38 #define fatal_error(name, what)	__fatal_error(__func__, name, what)
39 
40 static volatile int done;
41 
42 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
43 static void user_loop(void)
44 {
45 	while (!done);
46 }
47 
48 /*
49  * Try to spend as much time as possible in kernelspace
50  * to elapse ITIMER_PROF.
51  */
52 static void kernel_loop(void)
53 {
54 	void *addr = sbrk(0);
55 	int err = 0;
56 
57 	while (!done && !err) {
58 		err = brk(addr + 4096);
59 		err |= brk(addr);
60 	}
61 }
62 
63 /*
64  * Sleep until ITIMER_REAL expiration.
65  */
66 static void idle_loop(void)
67 {
68 	pause();
69 }
70 
71 static void sig_handler(int nr)
72 {
73 	done = 1;
74 }
75 
76 /*
77  * Check the expected timer expiration matches the GTOD elapsed delta since
78  * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
79  */
80 static int check_diff(struct timeval start, struct timeval end)
81 {
82 	long long diff;
83 
84 	diff = end.tv_usec - start.tv_usec;
85 	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
86 
87 	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
88 		printf("Diff too high: %lld..", diff);
89 		return -1;
90 	}
91 
92 	return 0;
93 }
94 
95 static void check_itimer(int which, const char *name)
96 {
97 	struct timeval start, end;
98 	struct itimerval val = {
99 		.it_value.tv_sec = DELAY,
100 	};
101 
102 	done = 0;
103 
104 	if (which == ITIMER_VIRTUAL)
105 		signal(SIGVTALRM, sig_handler);
106 	else if (which == ITIMER_PROF)
107 		signal(SIGPROF, sig_handler);
108 	else if (which == ITIMER_REAL)
109 		signal(SIGALRM, sig_handler);
110 
111 	if (gettimeofday(&start, NULL) < 0)
112 		fatal_error(name, "gettimeofday()");
113 
114 	if (setitimer(which, &val, NULL) < 0)
115 		fatal_error(name, "setitimer()");
116 
117 	if (which == ITIMER_VIRTUAL)
118 		user_loop();
119 	else if (which == ITIMER_PROF)
120 		kernel_loop();
121 	else if (which == ITIMER_REAL)
122 		idle_loop();
123 
124 	if (gettimeofday(&end, NULL) < 0)
125 		fatal_error(name, "gettimeofday()");
126 
127 	ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
128 }
129 
130 static void check_timer_create(int which, const char *name)
131 {
132 	struct timeval start, end;
133 	struct itimerspec val = {
134 		.it_value.tv_sec = DELAY,
135 	};
136 	timer_t id;
137 
138 	done = 0;
139 
140 	if (timer_create(which, NULL, &id) < 0)
141 		fatal_error(name, "timer_create()");
142 
143 	if (signal(SIGALRM, sig_handler) == SIG_ERR)
144 		fatal_error(name, "signal()");
145 
146 	if (gettimeofday(&start, NULL) < 0)
147 		fatal_error(name, "gettimeofday()");
148 
149 	if (timer_settime(id, 0, &val, NULL) < 0)
150 		fatal_error(name, "timer_settime()");
151 
152 	user_loop();
153 
154 	if (gettimeofday(&end, NULL) < 0)
155 		fatal_error(name, "gettimeofday()");
156 
157 	ksft_test_result(check_diff(start, end) == 0,
158 			 "timer_create() per %s\n", name);
159 }
160 
161 static pthread_t ctd_thread;
162 static volatile int ctd_count, ctd_failed;
163 
164 static void ctd_sighandler(int sig)
165 {
166 	if (pthread_self() != ctd_thread)
167 		ctd_failed = 1;
168 	ctd_count--;
169 }
170 
171 static void *ctd_thread_func(void *arg)
172 {
173 	struct itimerspec val = {
174 		.it_value.tv_sec = 0,
175 		.it_value.tv_nsec = 1000 * 1000,
176 		.it_interval.tv_sec = 0,
177 		.it_interval.tv_nsec = 1000 * 1000,
178 	};
179 	timer_t id;
180 
181 	/* 1/10 seconds to ensure the leader sleeps */
182 	usleep(10000);
183 
184 	ctd_count = 100;
185 	if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id))
186 		fatal_error(NULL, "timer_create()");
187 	if (timer_settime(id, 0, &val, NULL))
188 		fatal_error(NULL, "timer_settime()");
189 	while (ctd_count > 0 && !ctd_failed)
190 		;
191 
192 	if (timer_delete(id))
193 		fatal_error(NULL, "timer_delete()");
194 
195 	return NULL;
196 }
197 
198 /*
199  * Test that only the running thread receives the timer signal.
200  */
201 static void check_timer_distribution(void)
202 {
203 	if (signal(SIGALRM, ctd_sighandler) == SIG_ERR)
204 		fatal_error(NULL, "signal()");
205 
206 	if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL))
207 		fatal_error(NULL, "pthread_create()");
208 
209 	if (pthread_join(ctd_thread, NULL))
210 		fatal_error(NULL, "pthread_join()");
211 
212 	if (!ctd_failed)
213 		ksft_test_result_pass("check signal distribution\n");
214 	else if (ksft_min_kernel_version(6, 3))
215 		ksft_test_result_fail("check signal distribution\n");
216 	else
217 		ksft_test_result_skip("check signal distribution (old kernel)\n");
218 }
219 
220 struct tmrsig {
221 	int	signals;
222 	int	overruns;
223 };
224 
225 static void siginfo_handler(int sig, siginfo_t *si, void *uc)
226 {
227 	struct tmrsig *tsig = si ? si->si_ptr : NULL;
228 
229 	if (tsig) {
230 		tsig->signals++;
231 		tsig->overruns += si->si_overrun;
232 	}
233 }
234 
235 static void *ignore_thread(void *arg)
236 {
237 	unsigned int *tid = arg;
238 	sigset_t set;
239 
240 	sigemptyset(&set);
241 	sigaddset(&set, SIGUSR1);
242 	if (sigprocmask(SIG_BLOCK, &set, NULL))
243 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
244 
245 	*tid = gettid();
246 	sleep(100);
247 
248 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
249 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
250 	return NULL;
251 }
252 
253 static void check_sig_ign(int thread)
254 {
255 	struct tmrsig tsig = { };
256 	struct itimerspec its;
257 	unsigned int tid = 0;
258 	struct sigaction sa;
259 	struct sigevent sev;
260 	pthread_t pthread;
261 	timer_t timerid;
262 	sigset_t set;
263 
264 	if (thread) {
265 		if (pthread_create(&pthread, NULL, ignore_thread, &tid))
266 			fatal_error(NULL, "pthread_create()");
267 		sleep(1);
268 	}
269 
270 	sa.sa_flags = SA_SIGINFO;
271 	sa.sa_sigaction = siginfo_handler;
272 	sigemptyset(&sa.sa_mask);
273 	if (sigaction(SIGUSR1, &sa, NULL))
274 		fatal_error(NULL, "sigaction()");
275 
276 	/* Block the signal */
277 	sigemptyset(&set);
278 	sigaddset(&set, SIGUSR1);
279 	if (sigprocmask(SIG_BLOCK, &set, NULL))
280 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
281 
282 	memset(&sev, 0, sizeof(sev));
283 	sev.sigev_notify = SIGEV_SIGNAL;
284 	sev.sigev_signo = SIGUSR1;
285 	sev.sigev_value.sival_ptr = &tsig;
286 	if (thread) {
287 		sev.sigev_notify = SIGEV_THREAD_ID;
288 		sev._sigev_un._tid = tid;
289 	}
290 
291 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
292 		fatal_error(NULL, "timer_create()");
293 
294 	/* Start the timer to expire in 100ms and 100ms intervals */
295 	its.it_value.tv_sec = 0;
296 	its.it_value.tv_nsec = 100000000;
297 	its.it_interval.tv_sec = 0;
298 	its.it_interval.tv_nsec = 100000000;
299 	timer_settime(timerid, 0, &its, NULL);
300 
301 	sleep(1);
302 
303 	/* Set the signal to be ignored */
304 	if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
305 		fatal_error(NULL, "signal(SIG_IGN)");
306 
307 	sleep(1);
308 
309 	if (thread) {
310 		/* Stop the thread first. No signal should be delivered to it */
311 		if (pthread_cancel(pthread))
312 			fatal_error(NULL, "pthread_cancel()");
313 		if (pthread_join(pthread, NULL))
314 			fatal_error(NULL, "pthread_join()");
315 	}
316 
317 	/* Restore the handler */
318 	if (sigaction(SIGUSR1, &sa, NULL))
319 		fatal_error(NULL, "sigaction()");
320 
321 	sleep(1);
322 
323 	/* Unblock it, which should deliver the signal in the !thread case*/
324 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
325 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
326 
327 	if (timer_delete(timerid))
328 		fatal_error(NULL, "timer_delete()");
329 
330 	if (!thread) {
331 		ksft_test_result(tsig.signals == 1 && tsig.overruns == 29,
332 				 "check_sig_ign SIGEV_SIGNAL\n");
333 	} else {
334 		ksft_test_result(tsig.signals == 0 && tsig.overruns == 0,
335 				 "check_sig_ign SIGEV_THREAD_ID\n");
336 	}
337 }
338 
339 static void check_rearm(void)
340 {
341 	struct tmrsig tsig = { };
342 	struct itimerspec its;
343 	struct sigaction sa;
344 	struct sigevent sev;
345 	timer_t timerid;
346 	sigset_t set;
347 
348 	sa.sa_flags = SA_SIGINFO;
349 	sa.sa_sigaction = siginfo_handler;
350 	sigemptyset(&sa.sa_mask);
351 	if (sigaction(SIGUSR1, &sa, NULL))
352 		fatal_error(NULL, "sigaction()");
353 
354 	/* Block the signal */
355 	sigemptyset(&set);
356 	sigaddset(&set, SIGUSR1);
357 	if (sigprocmask(SIG_BLOCK, &set, NULL))
358 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
359 
360 	memset(&sev, 0, sizeof(sev));
361 	sev.sigev_notify = SIGEV_SIGNAL;
362 	sev.sigev_signo = SIGUSR1;
363 	sev.sigev_value.sival_ptr = &tsig;
364 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
365 		fatal_error(NULL, "timer_create()");
366 
367 	/* Start the timer to expire in 100ms and 100ms intervals */
368 	its.it_value.tv_sec = 0;
369 	its.it_value.tv_nsec = 100000000;
370 	its.it_interval.tv_sec = 0;
371 	its.it_interval.tv_nsec = 100000000;
372 	if (timer_settime(timerid, 0, &its, NULL))
373 		fatal_error(NULL, "timer_settime()");
374 
375 	sleep(1);
376 
377 	/* Reprogram the timer to single shot */
378 	its.it_value.tv_sec = 10;
379 	its.it_value.tv_nsec = 0;
380 	its.it_interval.tv_sec = 0;
381 	its.it_interval.tv_nsec = 0;
382 	if (timer_settime(timerid, 0, &its, NULL))
383 		fatal_error(NULL, "timer_settime()");
384 
385 	/* Unblock it, which should not deliver a signal */
386 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
387 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
388 
389 	if (timer_delete(timerid))
390 		fatal_error(NULL, "timer_delete()");
391 
392 	ksft_test_result(!tsig.signals, "check_rearm\n");
393 }
394 
395 static void check_delete(void)
396 {
397 	struct tmrsig tsig = { };
398 	struct itimerspec its;
399 	struct sigaction sa;
400 	struct sigevent sev;
401 	timer_t timerid;
402 	sigset_t set;
403 
404 	sa.sa_flags = SA_SIGINFO;
405 	sa.sa_sigaction = siginfo_handler;
406 	sigemptyset(&sa.sa_mask);
407 	if (sigaction(SIGUSR1, &sa, NULL))
408 		fatal_error(NULL, "sigaction()");
409 
410 	/* Block the signal */
411 	sigemptyset(&set);
412 	sigaddset(&set, SIGUSR1);
413 	if (sigprocmask(SIG_BLOCK, &set, NULL))
414 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
415 
416 	memset(&sev, 0, sizeof(sev));
417 	sev.sigev_notify = SIGEV_SIGNAL;
418 	sev.sigev_signo = SIGUSR1;
419 	sev.sigev_value.sival_ptr = &tsig;
420 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
421 		fatal_error(NULL, "timer_create()");
422 
423 	/* Start the timer to expire in 100ms and 100ms intervals */
424 	its.it_value.tv_sec = 0;
425 	its.it_value.tv_nsec = 100000000;
426 	its.it_interval.tv_sec = 0;
427 	its.it_interval.tv_nsec = 100000000;
428 	if (timer_settime(timerid, 0, &its, NULL))
429 		fatal_error(NULL, "timer_settime()");
430 
431 	sleep(1);
432 
433 	if (timer_delete(timerid))
434 		fatal_error(NULL, "timer_delete()");
435 
436 	/* Unblock it, which should not deliver a signal */
437 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
438 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
439 
440 	ksft_test_result(!tsig.signals, "check_delete\n");
441 }
442 
443 static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2)
444 {
445 	int64_t diff;
446 
447 	diff = NSECS_PER_SEC * (int64_t)((int) t1.tv_sec - (int) t2.tv_sec);
448 	diff += ((int) t1.tv_nsec - (int) t2.tv_nsec);
449 	return diff;
450 }
451 
452 static void check_sigev_none(int which, const char *name)
453 {
454 	struct timespec start, now;
455 	struct itimerspec its;
456 	struct sigevent sev;
457 	timer_t timerid;
458 
459 	memset(&sev, 0, sizeof(sev));
460 	sev.sigev_notify = SIGEV_NONE;
461 
462 	if (timer_create(which, &sev, &timerid))
463 		fatal_error(name, "timer_create()");
464 
465 	/* Start the timer to expire in 100ms and 100ms intervals */
466 	its.it_value.tv_sec = 0;
467 	its.it_value.tv_nsec = 100000000;
468 	its.it_interval.tv_sec = 0;
469 	its.it_interval.tv_nsec = 100000000;
470 	timer_settime(timerid, 0, &its, NULL);
471 
472 	if (clock_gettime(which, &start))
473 		fatal_error(name, "clock_gettime()");
474 
475 	do {
476 		if (clock_gettime(which, &now))
477 			fatal_error(name, "clock_gettime()");
478 	} while (calcdiff_ns(now, start) < NSECS_PER_SEC);
479 
480 	if (timer_gettime(timerid, &its))
481 		fatal_error(name, "timer_gettime()");
482 
483 	if (timer_delete(timerid))
484 		fatal_error(name, "timer_delete()");
485 
486 	ksft_test_result(its.it_value.tv_sec || its.it_value.tv_nsec,
487 			 "check_sigev_none %s\n", name);
488 }
489 
490 static void check_gettime(int which, const char *name)
491 {
492 	struct itimerspec its, prev;
493 	struct timespec start, now;
494 	struct sigevent sev;
495 	timer_t timerid;
496 	int wraps = 0;
497 	sigset_t set;
498 
499 	/* Block the signal */
500 	sigemptyset(&set);
501 	sigaddset(&set, SIGUSR1);
502 	if (sigprocmask(SIG_BLOCK, &set, NULL))
503 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
504 
505 	memset(&sev, 0, sizeof(sev));
506 	sev.sigev_notify = SIGEV_SIGNAL;
507 	sev.sigev_signo = SIGUSR1;
508 
509 	if (timer_create(which, &sev, &timerid))
510 		fatal_error(name, "timer_create()");
511 
512 	/* Start the timer to expire in 100ms and 100ms intervals */
513 	its.it_value.tv_sec = 0;
514 	its.it_value.tv_nsec = 100000000;
515 	its.it_interval.tv_sec = 0;
516 	its.it_interval.tv_nsec = 100000000;
517 	if (timer_settime(timerid, 0, &its, NULL))
518 		fatal_error(name, "timer_settime()");
519 
520 	if (timer_gettime(timerid, &prev))
521 		fatal_error(name, "timer_gettime()");
522 
523 	if (clock_gettime(which, &start))
524 		fatal_error(name, "clock_gettime()");
525 
526 	do {
527 		if (clock_gettime(which, &now))
528 			fatal_error(name, "clock_gettime()");
529 		if (timer_gettime(timerid, &its))
530 			fatal_error(name, "timer_gettime()");
531 		if (its.it_value.tv_nsec > prev.it_value.tv_nsec)
532 			wraps++;
533 		prev = its;
534 
535 	} while (calcdiff_ns(now, start) < NSECS_PER_SEC);
536 
537 	if (timer_delete(timerid))
538 		fatal_error(name, "timer_delete()");
539 
540 	ksft_test_result(wraps > 1, "check_gettime %s\n", name);
541 }
542 
543 static void check_overrun(int which, const char *name)
544 {
545 	struct timespec start, now;
546 	struct tmrsig tsig = { };
547 	struct itimerspec its;
548 	struct sigaction sa;
549 	struct sigevent sev;
550 	timer_t timerid;
551 	sigset_t set;
552 
553 	sa.sa_flags = SA_SIGINFO;
554 	sa.sa_sigaction = siginfo_handler;
555 	sigemptyset(&sa.sa_mask);
556 	if (sigaction(SIGUSR1, &sa, NULL))
557 		fatal_error(name, "sigaction()");
558 
559 	/* Block the signal */
560 	sigemptyset(&set);
561 	sigaddset(&set, SIGUSR1);
562 	if (sigprocmask(SIG_BLOCK, &set, NULL))
563 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
564 
565 	memset(&sev, 0, sizeof(sev));
566 	sev.sigev_notify = SIGEV_SIGNAL;
567 	sev.sigev_signo = SIGUSR1;
568 	sev.sigev_value.sival_ptr = &tsig;
569 	if (timer_create(which, &sev, &timerid))
570 		fatal_error(name, "timer_create()");
571 
572 	/* Start the timer to expire in 100ms and 100ms intervals */
573 	its.it_value.tv_sec = 0;
574 	its.it_value.tv_nsec = 100000000;
575 	its.it_interval.tv_sec = 0;
576 	its.it_interval.tv_nsec = 100000000;
577 	if (timer_settime(timerid, 0, &its, NULL))
578 		fatal_error(name, "timer_settime()");
579 
580 	if (clock_gettime(which, &start))
581 		fatal_error(name, "clock_gettime()");
582 
583 	do {
584 		if (clock_gettime(which, &now))
585 			fatal_error(name, "clock_gettime()");
586 	} while (calcdiff_ns(now, start) < NSECS_PER_SEC);
587 
588 	/* Unblock it, which should deliver a signal */
589 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
590 		fatal_error(name, "sigprocmask(SIG_UNBLOCK)");
591 
592 	if (timer_delete(timerid))
593 		fatal_error(name, "timer_delete()");
594 
595 	ksft_test_result(tsig.signals == 1 && tsig.overruns == 9,
596 			 "check_overrun %s\n", name);
597 }
598 
599 int main(int argc, char **argv)
600 {
601 	ksft_print_header();
602 	ksft_set_plan(18);
603 
604 	ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
605 	ksft_print_msg("based timers if other threads run on the CPU...\n");
606 
607 	check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL");
608 	check_itimer(ITIMER_PROF, "ITIMER_PROF");
609 	check_itimer(ITIMER_REAL, "ITIMER_REAL");
610 	check_timer_create(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID");
611 
612 	/*
613 	 * It's unfortunately hard to reliably test a timer expiration
614 	 * on parallel multithread cputime. We could arm it to expire
615 	 * on DELAY * nr_threads, with nr_threads busy looping, then wait
616 	 * the normal DELAY since the time is elapsing nr_threads faster.
617 	 * But for that we need to ensure we have real physical free CPUs
618 	 * to ensure true parallelism. So test only one thread until we
619 	 * find a better solution.
620 	 */
621 	check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID");
622 	check_timer_distribution();
623 
624 	check_sig_ign(0);
625 	check_sig_ign(1);
626 	check_rearm();
627 	check_delete();
628 	check_sigev_none(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
629 	check_sigev_none(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID");
630 	check_gettime(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
631 	check_gettime(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID");
632 	check_gettime(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID");
633 	check_overrun(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
634 	check_overrun(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID");
635 	check_overrun(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID");
636 
637 	ksft_finished();
638 }
639