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>
19 #define USECS_PER_SEC 1000000
21 static volatile int done
;
23 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
24 static void user_loop(void)
30 * Try to spend as much time as possible in kernelspace
31 * to elapse ITIMER_PROF.
33 static void kernel_loop(void)
44 * Sleep until ITIMER_REAL expiration.
46 static void idle_loop(void)
51 static void sig_handler(int nr
)
57 * Check the expected timer expiration matches the GTOD elapsed delta since
58 * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
60 static int check_diff(struct timeval start
, struct timeval end
)
64 diff
= end
.tv_usec
- start
.tv_usec
;
65 diff
+= (end
.tv_sec
- start
.tv_sec
) * USECS_PER_SEC
;
67 if (abs(diff
- DELAY
* USECS_PER_SEC
) > USECS_PER_SEC
/ 2) {
68 printf("Diff too high: %lld..", diff
);
75 static int check_itimer(int which
)
78 struct timeval start
, end
;
79 struct itimerval val
= {
80 .it_value
.tv_sec
= DELAY
,
83 printf("Check itimer ");
85 if (which
== ITIMER_VIRTUAL
)
86 printf("virtual... ");
87 else if (which
== ITIMER_PROF
)
89 else if (which
== ITIMER_REAL
)
96 if (which
== ITIMER_VIRTUAL
)
97 signal(SIGVTALRM
, sig_handler
);
98 else if (which
== ITIMER_PROF
)
99 signal(SIGPROF
, sig_handler
);
100 else if (which
== ITIMER_REAL
)
101 signal(SIGALRM
, sig_handler
);
103 err
= gettimeofday(&start
, NULL
);
105 perror("Can't call gettimeofday()\n");
109 err
= setitimer(which
, &val
, NULL
);
111 perror("Can't set timer\n");
115 if (which
== ITIMER_VIRTUAL
)
117 else if (which
== ITIMER_PROF
)
119 else if (which
== ITIMER_REAL
)
122 gettimeofday(&end
, NULL
);
124 perror("Can't call gettimeofday()\n");
128 if (!check_diff(start
, end
))
136 static int check_timer_create(int which
)
140 struct timeval start
, end
;
141 struct itimerspec val
= {
142 .it_value
.tv_sec
= DELAY
,
145 printf("Check timer_create() ");
146 if (which
== CLOCK_THREAD_CPUTIME_ID
) {
147 printf("per thread... ");
148 } else if (which
== CLOCK_PROCESS_CPUTIME_ID
) {
149 printf("per process... ");
154 timer_create(which
, NULL
, &id
);
156 perror("Can't create timer\n");
159 signal(SIGALRM
, sig_handler
);
161 err
= gettimeofday(&start
, NULL
);
163 perror("Can't call gettimeofday()\n");
167 err
= timer_settime(id
, 0, &val
, NULL
);
169 perror("Can't set timer\n");
175 gettimeofday(&end
, NULL
);
177 perror("Can't call gettimeofday()\n");
181 if (!check_diff(start
, end
))
189 int main(int argc
, char **argv
)
193 printf("Testing posix timers. False negative may happen on CPU execution \n");
194 printf("based timers if other threads run on the CPU...\n");
196 if (check_itimer(ITIMER_VIRTUAL
) < 0)
199 if (check_itimer(ITIMER_PROF
) < 0)
202 if (check_itimer(ITIMER_REAL
) < 0)
205 if (check_timer_create(CLOCK_THREAD_CPUTIME_ID
) < 0)
209 * It's unfortunately hard to reliably test a timer expiration
210 * on parallel multithread cputime. We could arm it to expire
211 * on DELAY * nr_threads, with nr_threads busy looping, then wait
212 * the normal DELAY since the time is elapsing nr_threads faster.
213 * But for that we need to ensure we have real physical free CPUs
214 * to ensure true parallelism. So test only one thread until we
215 * find a better solution.
217 if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID
) < 0)