2 * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4 * Licensed under the terms of the GNU GPL License version 2
6 * Selftests for a few posix timers interface.
8 * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
18 #include "../kselftest.h"
21 #define USECS_PER_SEC 1000000
23 static volatile int done;
25 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
26 static void user_loop(void)
32 * Try to spend as much time as possible in kernelspace
33 * to elapse ITIMER_PROF.
35 static void kernel_loop(void)
40 while (!done && !err) {
41 err = brk(addr + 4096);
47 * Sleep until ITIMER_REAL expiration.
49 static void idle_loop(void)
54 static void sig_handler(int nr)
60 * Check the expected timer expiration matches the GTOD elapsed delta since
61 * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
63 static int check_diff(struct timeval start, struct timeval end)
67 diff = end.tv_usec - start.tv_usec;
68 diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
70 if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
71 printf("Diff too high: %lld..", diff);
78 static int check_itimer(int which)
81 struct timeval start, end;
82 struct itimerval val = {
83 .it_value.tv_sec = DELAY,
86 printf("Check itimer ");
88 if (which == ITIMER_VIRTUAL)
89 printf("virtual... ");
90 else if (which == ITIMER_PROF)
92 else if (which == ITIMER_REAL)
99 if (which == ITIMER_VIRTUAL)
100 signal(SIGVTALRM, sig_handler);
101 else if (which == ITIMER_PROF)
102 signal(SIGPROF, sig_handler);
103 else if (which == ITIMER_REAL)
104 signal(SIGALRM, sig_handler);
106 err = gettimeofday(&start, NULL);
108 perror("Can't call gettimeofday()\n");
112 err = setitimer(which, &val, NULL);
114 perror("Can't set timer\n");
118 if (which == ITIMER_VIRTUAL)
120 else if (which == ITIMER_PROF)
122 else if (which == ITIMER_REAL)
125 err = gettimeofday(&end, NULL);
127 perror("Can't call gettimeofday()\n");
131 if (!check_diff(start, end))
139 static int check_timer_create(int which)
143 struct timeval start, end;
144 struct itimerspec val = {
145 .it_value.tv_sec = DELAY,
148 printf("Check timer_create() ");
149 if (which == CLOCK_THREAD_CPUTIME_ID) {
150 printf("per thread... ");
151 } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
152 printf("per process... ");
157 err = timer_create(which, NULL, &id);
159 perror("Can't create timer\n");
162 signal(SIGALRM, sig_handler);
164 err = gettimeofday(&start, NULL);
166 perror("Can't call gettimeofday()\n");
170 err = timer_settime(id, 0, &val, NULL);
172 perror("Can't set timer\n");
178 err = gettimeofday(&end, NULL);
180 perror("Can't call gettimeofday()\n");
184 if (!check_diff(start, end))
192 int main(int argc, char **argv)
194 printf("Testing posix timers. False negative may happen on CPU execution \n");
195 printf("based timers if other threads run on the CPU...\n");
197 if (check_itimer(ITIMER_VIRTUAL) < 0)
198 return ksft_exit_fail();
200 if (check_itimer(ITIMER_PROF) < 0)
201 return ksft_exit_fail();
203 if (check_itimer(ITIMER_REAL) < 0)
204 return ksft_exit_fail();
206 if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
207 return ksft_exit_fail();
210 * It's unfortunately hard to reliably test a timer expiration
211 * on parallel multithread cputime. We could arm it to expire
212 * on DELAY * nr_threads, with nr_threads busy looping, then wait
213 * the normal DELAY since the time is elapsing nr_threads faster.
214 * But for that we need to ensure we have real physical free CPUs
215 * to ensure true parallelism. So test only one thread until we
216 * find a better solution.
218 if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0)
219 return ksft_exit_fail();
221 return ksft_exit_pass();