Merge tag 'platform-drivers-x86-v6.9-3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / tools / testing / selftests / timers / posix_timers.c
CommitLineData
4f19048f 1// SPDX-License-Identifier: GPL-2.0-only
0bc4b0cf
FW
2/*
3 * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4 *
0bc4b0cf
FW
5 * Selftests for a few posix timers interface.
6 *
7 * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
8 */
9
10#include <sys/time.h>
11#include <stdio.h>
12#include <signal.h>
13#include <unistd.h>
14#include <time.h>
15#include <pthread.h>
16
88471649
SK
17#include "../kselftest.h"
18
0bc4b0cf
FW
19#define DELAY 2
20#define USECS_PER_SEC 1000000
21
22static volatile int done;
23
24/* Busy loop in userspace to elapse ITIMER_VIRTUAL */
25static void user_loop(void)
26{
27 while (!done);
28}
29
30/*
31 * Try to spend as much time as possible in kernelspace
32 * to elapse ITIMER_PROF.
33 */
34static void kernel_loop(void)
35{
36 void *addr = sbrk(0);
2430ec65 37 int err = 0;
0bc4b0cf 38
2430ec65
JS
39 while (!done && !err) {
40 err = brk(addr + 4096);
41 err |= brk(addr);
0bc4b0cf
FW
42 }
43}
44
45/*
46 * Sleep until ITIMER_REAL expiration.
47 */
48static void idle_loop(void)
49{
50 pause();
51}
52
53static void sig_handler(int nr)
54{
55 done = 1;
56}
57
58/*
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.
61 */
62static int check_diff(struct timeval start, struct timeval end)
63{
64 long long diff;
65
66 diff = end.tv_usec - start.tv_usec;
67 diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
68
ed366de8 69 if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
0bc4b0cf
FW
70 printf("Diff too high: %lld..", diff);
71 return -1;
72 }
73
74 return 0;
75}
76
77static int check_itimer(int which)
78{
071af0c9 79 const char *name;
0bc4b0cf
FW
80 int err;
81 struct timeval start, end;
82 struct itimerval val = {
83 .it_value.tv_sec = DELAY,
84 };
85
0bc4b0cf 86 if (which == ITIMER_VIRTUAL)
071af0c9 87 name = "ITIMER_VIRTUAL";
0bc4b0cf 88 else if (which == ITIMER_PROF)
071af0c9 89 name = "ITIMER_PROF";
0bc4b0cf 90 else if (which == ITIMER_REAL)
071af0c9
MB
91 name = "ITIMER_REAL";
92 else
93 return -1;
0bc4b0cf
FW
94
95 done = 0;
96
97 if (which == ITIMER_VIRTUAL)
98 signal(SIGVTALRM, sig_handler);
99 else if (which == ITIMER_PROF)
100 signal(SIGPROF, sig_handler);
101 else if (which == ITIMER_REAL)
102 signal(SIGALRM, sig_handler);
103
104 err = gettimeofday(&start, NULL);
105 if (err < 0) {
071af0c9 106 ksft_perror("Can't call gettimeofday()");
0bc4b0cf
FW
107 return -1;
108 }
109
110 err = setitimer(which, &val, NULL);
111 if (err < 0) {
071af0c9 112 ksft_perror("Can't set timer");
0bc4b0cf
FW
113 return -1;
114 }
115
116 if (which == ITIMER_VIRTUAL)
117 user_loop();
118 else if (which == ITIMER_PROF)
119 kernel_loop();
120 else if (which == ITIMER_REAL)
121 idle_loop();
122
c3cb8356 123 err = gettimeofday(&end, NULL);
0bc4b0cf 124 if (err < 0) {
071af0c9 125 ksft_perror("Can't call gettimeofday()");
0bc4b0cf
FW
126 return -1;
127 }
128
071af0c9 129 ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
0bc4b0cf
FW
130
131 return 0;
132}
133
134static int check_timer_create(int which)
135{
071af0c9 136 const char *type;
0bc4b0cf
FW
137 int err;
138 timer_t id;
139 struct timeval start, end;
140 struct itimerspec val = {
141 .it_value.tv_sec = DELAY,
142 };
143
0bc4b0cf 144 if (which == CLOCK_THREAD_CPUTIME_ID) {
071af0c9 145 type = "thread";
0bc4b0cf 146 } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
071af0c9
MB
147 type = "process";
148 } else {
149 ksft_print_msg("Unknown timer_create() type %d\n", which);
150 return -1;
0bc4b0cf 151 }
0bc4b0cf
FW
152
153 done = 0;
c88b05b2 154 err = timer_create(which, NULL, &id);
0bc4b0cf 155 if (err < 0) {
071af0c9 156 ksft_perror("Can't create timer");
0bc4b0cf
FW
157 return -1;
158 }
159 signal(SIGALRM, sig_handler);
160
161 err = gettimeofday(&start, NULL);
162 if (err < 0) {
071af0c9 163 ksft_perror("Can't call gettimeofday()");
0bc4b0cf
FW
164 return -1;
165 }
166
167 err = timer_settime(id, 0, &val, NULL);
168 if (err < 0) {
071af0c9 169 ksft_perror("Can't set timer");
0bc4b0cf
FW
170 return -1;
171 }
172
173 user_loop();
174
c3cb8356 175 err = gettimeofday(&end, NULL);
0bc4b0cf 176 if (err < 0) {
071af0c9 177 ksft_perror("Can't call gettimeofday()");
0bc4b0cf
FW
178 return -1;
179 }
180
071af0c9
MB
181 ksft_test_result(check_diff(start, end) == 0,
182 "timer_create() per %s\n", type);
0bc4b0cf
FW
183
184 return 0;
185}
186
6d029c25
ON
187static pthread_t ctd_thread;
188static volatile int ctd_count, ctd_failed;
e797203f 189
6d029c25 190static void ctd_sighandler(int sig)
e797203f 191{
6d029c25
ON
192 if (pthread_self() != ctd_thread)
193 ctd_failed = 1;
194 ctd_count--;
e797203f
DV
195}
196
6d029c25 197static void *ctd_thread_func(void *arg)
e797203f 198{
e797203f
DV
199 struct itimerspec val = {
200 .it_value.tv_sec = 0,
201 .it_value.tv_nsec = 1000 * 1000,
202 .it_interval.tv_sec = 0,
203 .it_interval.tv_nsec = 1000 * 1000,
204 };
6d029c25 205 timer_t id;
e797203f 206
6d029c25
ON
207 /* 1/10 seconds to ensure the leader sleeps */
208 usleep(10000);
e797203f 209
6d029c25
ON
210 ctd_count = 100;
211 if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id))
212 return "Can't create timer\n";
213 if (timer_settime(id, 0, &val, NULL))
214 return "Can't set timer\n";
e797203f 215
6d029c25
ON
216 while (ctd_count > 0 && !ctd_failed)
217 ;
e797203f 218
6d029c25
ON
219 if (timer_delete(id))
220 return "Can't delete timer\n";
e797203f 221
6d029c25
ON
222 return NULL;
223}
224
225/*
226 * Test that only the running thread receives the timer signal.
227 */
228static int check_timer_distribution(void)
229{
230 const char *errmsg;
e797203f 231
6d029c25
ON
232 signal(SIGALRM, ctd_sighandler);
233
234 errmsg = "Can't create thread\n";
235 if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL))
236 goto err;
237
238 errmsg = "Can't join thread\n";
239 if (pthread_join(ctd_thread, (void **)&errmsg) || errmsg)
240 goto err;
241
242 if (!ctd_failed)
243 ksft_test_result_pass("check signal distribution\n");
244 else if (ksft_min_kernel_version(6, 3))
245 ksft_test_result_fail("check signal distribution\n");
246 else
247 ksft_test_result_skip("check signal distribution (old kernel)\n");
e797203f 248 return 0;
6d029c25 249err:
e4a6bcea 250 ksft_print_msg("%s", errmsg);
6d029c25 251 return -1;
e797203f
DV
252}
253
0bc4b0cf
FW
254int main(int argc, char **argv)
255{
071af0c9
MB
256 ksft_print_header();
257 ksft_set_plan(6);
258
259 ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
260 ksft_print_msg("based timers if other threads run on the CPU...\n");
0bc4b0cf
FW
261
262 if (check_itimer(ITIMER_VIRTUAL) < 0)
88471649 263 return ksft_exit_fail();
0bc4b0cf
FW
264
265 if (check_itimer(ITIMER_PROF) < 0)
88471649 266 return ksft_exit_fail();
0bc4b0cf
FW
267
268 if (check_itimer(ITIMER_REAL) < 0)
88471649 269 return ksft_exit_fail();
0bc4b0cf
FW
270
271 if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
88471649 272 return ksft_exit_fail();
0bc4b0cf
FW
273
274 /*
275 * It's unfortunately hard to reliably test a timer expiration
276 * on parallel multithread cputime. We could arm it to expire
277 * on DELAY * nr_threads, with nr_threads busy looping, then wait
278 * the normal DELAY since the time is elapsing nr_threads faster.
279 * But for that we need to ensure we have real physical free CPUs
280 * to ensure true parallelism. So test only one thread until we
281 * find a better solution.
282 */
283 if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0)
88471649 284 return ksft_exit_fail();
0bc4b0cf 285
e797203f
DV
286 if (check_timer_distribution() < 0)
287 return ksft_exit_fail();
288
071af0c9 289 ksft_finished();
0bc4b0cf 290}