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)
46 * Sleep until ITIMER_REAL expiration.
48 static void idle_loop(void)
53 static void sig_handler(int nr)
59 * Check the expected timer expiration matches the GTOD elapsed delta since
60 * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
62 static int check_diff(struct timeval start, struct timeval end)
66 diff = end.tv_usec - start.tv_usec;
67 diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
69 if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
70 printf("Diff too high: %lld..", diff);
77 static int check_itimer(int which)
80 struct timeval start, end;
81 struct itimerval val = {
82 .it_value.tv_sec = DELAY,
85 printf("Check itimer ");
87 if (which == ITIMER_VIRTUAL)
88 printf("virtual... ");
89 else if (which == ITIMER_PROF)
91 else if (which == ITIMER_REAL)
98 if (which == ITIMER_VIRTUAL)
99 signal(SIGVTALRM, sig_handler);
100 else if (which == ITIMER_PROF)
101 signal(SIGPROF, sig_handler);
102 else if (which == ITIMER_REAL)
103 signal(SIGALRM, sig_handler);
105 err = gettimeofday(&start, NULL);
107 perror("Can't call gettimeofday()\n");
111 err = setitimer(which, &val, NULL);
113 perror("Can't set timer\n");
117 if (which == ITIMER_VIRTUAL)
119 else if (which == ITIMER_PROF)
121 else if (which == ITIMER_REAL)
124 gettimeofday(&end, NULL);
126 perror("Can't call gettimeofday()\n");
130 if (!check_diff(start, end))
138 static int check_timer_create(int which)
142 struct timeval start, end;
143 struct itimerspec val = {
144 .it_value.tv_sec = DELAY,
147 printf("Check timer_create() ");
148 if (which == CLOCK_THREAD_CPUTIME_ID) {
149 printf("per thread... ");
150 } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
151 printf("per process... ");
156 err = timer_create(which, NULL, &id);
158 perror("Can't create timer\n");
161 signal(SIGALRM, sig_handler);
163 err = gettimeofday(&start, NULL);
165 perror("Can't call gettimeofday()\n");
169 err = timer_settime(id, 0, &val, NULL);
171 perror("Can't set timer\n");
177 gettimeofday(&end, NULL);
179 perror("Can't call gettimeofday()\n");
183 if (!check_diff(start, end))
191 int main(int argc, char **argv)
195 printf("Testing posix timers. False negative may happen on CPU execution \n");
196 printf("based timers if other threads run on the CPU...\n");
198 if (check_itimer(ITIMER_VIRTUAL) < 0)
199 return ksft_exit_fail();
201 if (check_itimer(ITIMER_PROF) < 0)
202 return ksft_exit_fail();
204 if (check_itimer(ITIMER_REAL) < 0)
205 return ksft_exit_fail();
207 if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
208 return ksft_exit_fail();
211 * It's unfortunately hard to reliably test a timer expiration
212 * on parallel multithread cputime. We could arm it to expire
213 * on DELAY * nr_threads, with nr_threads busy looping, then wait
214 * the normal DELAY since the time is elapsing nr_threads faster.
215 * But for that we need to ensure we have real physical free CPUs
216 * to ensure true parallelism. So test only one thread until we
217 * find a better solution.
219 if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0)
220 return ksft_exit_fail();
222 return ksft_exit_pass();