aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-07-06 17:09:38 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-07-06 17:09:38 -0400
commit21884a83b2192a00885d7244a1dda32debd2fbc7 (patch)
treee00267bbcac32f0114b3d21a20c61107d2314c40 /tools
parent8b70a90cabafb6a6e1a0d3f838b38355fe48337e (diff)
parent73b0cd674ccc64c921e25bd7154f26d342116539 (diff)
Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timer core updates from Thomas Gleixner: "The timer changes contain: - posix timer code consolidation and fixes for odd corner cases - sched_clock implementation moved from ARM to core code to avoid duplication by other architectures - alarm timer updates - clocksource and clockevents unregistration facilities - clocksource/events support for new hardware - precise nanoseconds RTC readout (Xen feature) - generic support for Xen suspend/resume oddities - the usual lot of fixes and cleanups all over the place The parts which touch other areas (ARM/XEN) have been coordinated with the relevant maintainers. Though this results in an handful of trivial to solve merge conflicts, which we preferred over nasty cross tree merge dependencies. The patches which have been committed in the last few days are bug fixes plus the posix timer lot. The latter was in akpms queue and next for quite some time; they just got forgotten and Frederic collected them last minute." * 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (59 commits) hrtimer: Remove unused variable hrtimers: Move SMP function call to thread context clocksource: Reselect clocksource when watchdog validated high-res capability posix-cpu-timers: don't account cpu timer after stopped thread runtime accounting posix_timers: fix racy timer delta caching on task exit posix-timers: correctly get dying task time sample in posix_cpu_timer_schedule() selftests: add basic posix timers selftests posix_cpu_timers: consolidate expired timers check posix_cpu_timers: consolidate timer list cleanups posix_cpu_timer: consolidate expiry time type tick: Sanitize broadcast control logic tick: Prevent uncontrolled switch to oneshot mode tick: Make oneshot broadcast robust vs. CPU offlining x86: xen: Sync the CMOS RTC as well as the Xen wallclock x86: xen: Sync the wallclock when the system time is set timekeeping: Indicate that clock was set in the pvclock gtod notifier timekeeping: Pass flags instead of multiple bools to timekeeping_update() xen: Remove clock_was_set() call in the resume path hrtimers: Support resuming with two or more CPUs online (but stopped) timer: Fix jiffies wrap behavior of round_jiffies_common() ...
Diffstat (limited to 'tools')
-rw-r--r--tools/testing/selftests/Makefile1
-rw-r--r--tools/testing/selftests/timers/Makefile8
-rw-r--r--tools/testing/selftests/timers/posix_timers.c221
3 files changed, 230 insertions, 0 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 0a63658065f0..4cb14cae3791 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -6,6 +6,7 @@ TARGETS += memory-hotplug
6TARGETS += mqueue 6TARGETS += mqueue
7TARGETS += net 7TARGETS += net
8TARGETS += ptrace 8TARGETS += ptrace
9TARGETS += timers
9TARGETS += vm 10TARGETS += vm
10 11
11all: 12all:
diff --git a/tools/testing/selftests/timers/Makefile b/tools/testing/selftests/timers/Makefile
new file mode 100644
index 000000000000..eb2859f4ad21
--- /dev/null
+++ b/tools/testing/selftests/timers/Makefile
@@ -0,0 +1,8 @@
1all:
2 gcc posix_timers.c -o posix_timers -lrt
3
4run_tests: all
5 ./posix_timers
6
7clean:
8 rm -f ./posix_timers
diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
new file mode 100644
index 000000000000..4fa655d68a81
--- /dev/null
+++ b/tools/testing/selftests/timers/posix_timers.c
@@ -0,0 +1,221 @@
1/*
2 * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
3 *
4 * Licensed under the terms of the GNU GPL License version 2
5 *
6 * Selftests for a few posix timers interface.
7 *
8 * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
9 */
10
11#include <sys/time.h>
12#include <stdio.h>
13#include <signal.h>
14#include <unistd.h>
15#include <time.h>
16#include <pthread.h>
17
18#define DELAY 2
19#define USECS_PER_SEC 1000000
20
21static volatile int done;
22
23/* Busy loop in userspace to elapse ITIMER_VIRTUAL */
24static void user_loop(void)
25{
26 while (!done);
27}
28
29/*
30 * Try to spend as much time as possible in kernelspace
31 * to elapse ITIMER_PROF.
32 */
33static void kernel_loop(void)
34{
35 void *addr = sbrk(0);
36
37 while (!done) {
38 brk(addr + 4096);
39 brk(addr);
40 }
41}
42
43/*
44 * Sleep until ITIMER_REAL expiration.
45 */
46static void idle_loop(void)
47{
48 pause();
49}
50
51static void sig_handler(int nr)
52{
53 done = 1;
54}
55
56/*
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.
59 */
60static int check_diff(struct timeval start, struct timeval end)
61{
62 long long diff;
63
64 diff = end.tv_usec - start.tv_usec;
65 diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
66
67 if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
68 printf("Diff too high: %lld..", diff);
69 return -1;
70 }
71
72 return 0;
73}
74
75static int check_itimer(int which)
76{
77 int err;
78 struct timeval start, end;
79 struct itimerval val = {
80 .it_value.tv_sec = DELAY,
81 };
82
83 printf("Check itimer ");
84
85 if (which == ITIMER_VIRTUAL)
86 printf("virtual... ");
87 else if (which == ITIMER_PROF)
88 printf("prof... ");
89 else if (which == ITIMER_REAL)
90 printf("real... ");
91
92 fflush(stdout);
93
94 done = 0;
95
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);
102
103 err = gettimeofday(&start, NULL);
104 if (err < 0) {
105 perror("Can't call gettimeofday()\n");
106 return -1;
107 }
108
109 err = setitimer(which, &val, NULL);
110 if (err < 0) {
111 perror("Can't set timer\n");
112 return -1;
113 }
114
115 if (which == ITIMER_VIRTUAL)
116 user_loop();
117 else if (which == ITIMER_PROF)
118 kernel_loop();
119 else if (which == ITIMER_REAL)
120 idle_loop();
121
122 gettimeofday(&end, NULL);
123 if (err < 0) {
124 perror("Can't call gettimeofday()\n");
125 return -1;
126 }
127
128 if (!check_diff(start, end))
129 printf("[OK]\n");
130 else
131 printf("[FAIL]\n");
132
133 return 0;
134}
135
136static int check_timer_create(int which)
137{
138 int err;
139 timer_t id;
140 struct timeval start, end;
141 struct itimerspec val = {
142 .it_value.tv_sec = DELAY,
143 };
144
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... ");
150 }
151 fflush(stdout);
152
153 done = 0;
154 timer_create(which, NULL, &id);
155 if (err < 0) {
156 perror("Can't create timer\n");
157 return -1;
158 }
159 signal(SIGALRM, sig_handler);
160
161 err = gettimeofday(&start, NULL);
162 if (err < 0) {
163 perror("Can't call gettimeofday()\n");
164 return -1;
165 }
166
167 err = timer_settime(id, 0, &val, NULL);
168 if (err < 0) {
169 perror("Can't set timer\n");
170 return -1;
171 }
172
173 user_loop();
174
175 gettimeofday(&end, NULL);
176 if (err < 0) {
177 perror("Can't call gettimeofday()\n");
178 return -1;
179 }
180
181 if (!check_diff(start, end))
182 printf("[OK]\n");
183 else
184 printf("[FAIL]\n");
185
186 return 0;
187}
188
189int main(int argc, char **argv)
190{
191 int err;
192
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");
195
196 if (check_itimer(ITIMER_VIRTUAL) < 0)
197 return -1;
198
199 if (check_itimer(ITIMER_PROF) < 0)
200 return -1;
201
202 if (check_itimer(ITIMER_REAL) < 0)
203 return -1;
204
205 if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
206 return -1;
207
208 /*
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.
216 */
217 if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0)
218 return -1;
219
220 return 0;
221}