1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: run various timeout tests
4 *
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/time.h>
13 #include <sys/wait.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <assert.h>
17
18 #include "helpers.h"
19 #include "liburing.h"
20 #include "../src/syscall.h"
21
22 #define IO_NSEC_PER_SEC 1000000000ULL
23
24 static bool support_abs = false;
25 static bool support_clock = false;
26
timespec_to_ns(struct timespec * ts)27 static unsigned long long timespec_to_ns(struct timespec *ts)
28 {
29 return ts->tv_nsec + ts->tv_sec * IO_NSEC_PER_SEC;
30 }
ns_to_timespec(unsigned long long t)31 static struct timespec ns_to_timespec(unsigned long long t)
32 {
33 struct timespec ts;
34
35 ts.tv_sec = t / IO_NSEC_PER_SEC;
36 ts.tv_nsec = t - ts.tv_sec * IO_NSEC_PER_SEC;
37 return ts;
38 }
39
ns_since(struct timespec * ts)40 static long long ns_since(struct timespec *ts)
41 {
42 struct timespec now;
43 int ret;
44
45 ret = clock_gettime(CLOCK_MONOTONIC, &now);
46 if (ret) {
47 fprintf(stderr, "clock_gettime failed\n");
48 exit(T_EXIT_FAIL);
49 }
50
51 return timespec_to_ns(&now) - timespec_to_ns(ts);
52
53 }
54
t_io_uring_wait(struct io_uring * ring,int nr,unsigned enter_flags,struct timespec * ts)55 static int t_io_uring_wait(struct io_uring *ring, int nr, unsigned enter_flags,
56 struct timespec *ts)
57 {
58 struct __kernel_timespec kts = {
59 .tv_sec = ts->tv_sec,
60 .tv_nsec = ts->tv_nsec
61 };
62 struct io_uring_getevents_arg arg = {
63 .sigmask = 0,
64 .sigmask_sz = _NSIG / 8,
65 .ts = (unsigned long) &kts
66 };
67 int ret;
68
69 enter_flags |= IORING_ENTER_GETEVENTS | IORING_ENTER_EXT_ARG;
70 ret = io_uring_enter2(ring->ring_fd, 0, nr, enter_flags,
71 (void *)&arg, sizeof(arg));
72 return ret;
73 }
74
probe_timers(void)75 static int probe_timers(void)
76 {
77 struct io_uring_clock_register cr = { .clockid = CLOCK_MONOTONIC, };
78 struct io_uring ring;
79 struct timespec ts;
80 int ret;
81
82 ret = io_uring_queue_init(8, &ring, 0);
83 if (ret) {
84 fprintf(stderr, "probe ring setup failed: %d\n", ret);
85 return ret;
86 }
87
88 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
89 if (ret) {
90 fprintf(stderr, "clock_gettime failed\n");
91 return ret;
92 }
93
94 ret = t_io_uring_wait(&ring, 0, IORING_ENTER_ABS_TIMER, &ts);
95 if (!ret) {
96 support_abs = true;
97 } else if (ret != -EINVAL) {
98 fprintf(stderr, "wait failed %i\n", ret);
99 return ret;
100 }
101
102 ret = io_uring_register_clock(&ring, &cr);
103 if (!ret) {
104 support_clock = true;
105 } else if (ret != -EINVAL) {
106 fprintf(stderr, "io_uring_register_clock %i\n", ret);
107 return ret;
108 }
109
110 io_uring_queue_exit(&ring);
111 return 0;
112 }
113
test_timeout(bool abs,bool set_clock)114 static int test_timeout(bool abs, bool set_clock)
115 {
116 unsigned enter_flags = abs ? IORING_ENTER_ABS_TIMER : 0;
117 struct io_uring ring;
118 struct timespec start, end, ts;
119 long long dt;
120 int ret;
121
122 ret = io_uring_queue_init(8, &ring, 0);
123 if (ret) {
124 fprintf(stderr, "ring setup failed: %d\n", ret);
125 return 1;
126 }
127
128 if (set_clock) {
129 struct io_uring_clock_register cr = {};
130
131 cr.clockid = CLOCK_BOOTTIME;
132 ret = io_uring_register_clock(&ring, &cr);
133 if (ret) {
134 fprintf(stderr, "io_uring_register_clock failed\n");
135 return 1;
136 }
137 }
138
139 /* pass current time */
140 ret = clock_gettime(CLOCK_MONOTONIC, &start);
141 assert(ret == 0);
142
143 ts = abs ? start : ns_to_timespec(0);
144 ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
145 if (ret != -ETIME) {
146 fprintf(stderr, "wait current time failed, %i\n", ret);
147 return 1;
148 }
149
150 if (ns_since(&start) >= IO_NSEC_PER_SEC) {
151 fprintf(stderr, "current time test failed\n");
152 return 1;
153 }
154
155 if (abs) {
156 /* expired time */
157 ret = clock_gettime(CLOCK_MONOTONIC, &start);
158 assert(ret == 0);
159 ts = ns_to_timespec(timespec_to_ns(&start) - IO_NSEC_PER_SEC);
160
161 ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
162 if (ret != -ETIME) {
163 fprintf(stderr, "expired timeout wait failed, %i\n", ret);
164 return 1;
165 }
166
167 ret = clock_gettime(CLOCK_MONOTONIC, &end);
168 assert(ret == 0);
169
170 if (ns_since(&start) >= IO_NSEC_PER_SEC) {
171 fprintf(stderr, "expired timer test failed\n");
172 return 1;
173 }
174 }
175
176 /* 1s wait */
177 ret = clock_gettime(CLOCK_MONOTONIC, &start);
178 assert(ret == 0);
179
180 dt = 2 * IO_NSEC_PER_SEC + (abs ? timespec_to_ns(&start) : 0);
181 ts = ns_to_timespec(dt);
182 ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
183 if (ret != -ETIME) {
184 fprintf(stderr, "wait timeout failed, %i\n", ret);
185 return 1;
186 }
187
188 dt = ns_since(&start);
189 if (dt < IO_NSEC_PER_SEC || dt > 3 * IO_NSEC_PER_SEC) {
190 fprintf(stderr, "early wake up, %lld\n", dt);
191 return 1;
192 }
193 return 0;
194 }
195
test_clock_setup(void)196 static int test_clock_setup(void)
197 {
198 struct io_uring ring;
199 struct io_uring_clock_register cr = {};
200 int ret;
201
202 ret = io_uring_queue_init(8, &ring, 0);
203 if (ret) {
204 fprintf(stderr, "ring setup failed: %d\n", ret);
205 return T_EXIT_FAIL;
206 }
207
208 ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, NULL, 0);
209 if (!ret) {
210 fprintf(stderr, "invalid null clock registration %i\n", ret);
211 return T_EXIT_FAIL;
212 }
213
214 cr.clockid = -1;
215 ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
216 if (ret != -EINVAL) {
217 fprintf(stderr, "invalid clockid registration %i\n", ret);
218 return T_EXIT_FAIL;
219 }
220
221 cr.clockid = CLOCK_MONOTONIC;
222 ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
223 if (ret) {
224 fprintf(stderr, "clock monotonic registration failed %i\n", ret);
225 return T_EXIT_FAIL;
226 }
227
228 cr.clockid = CLOCK_BOOTTIME;
229 ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
230 if (ret) {
231 fprintf(stderr, "clock boottime registration failed %i\n", ret);
232 return T_EXIT_FAIL;
233 }
234
235 cr.clockid = CLOCK_MONOTONIC;
236 ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
237 if (ret) {
238 fprintf(stderr, "2nd clock monotonic registration failed %i\n", ret);
239 return T_EXIT_FAIL;
240 }
241
242 io_uring_queue_exit(&ring);
243 return 0;
244 }
245
main(int argc,char * argv[])246 int main(int argc, char *argv[])
247 {
248 int ret, i;
249
250 if (argc > 1)
251 return 0;
252
253 ret = probe_timers();
254 if (ret) {
255 fprintf(stderr, "probe failed\n");
256 return T_EXIT_FAIL;
257 }
258 if (!support_abs && !support_clock)
259 return T_EXIT_SKIP;
260
261 if (support_clock) {
262 ret = test_clock_setup();
263 if (ret) {
264 fprintf(stderr, "test_clock_setup failed\n");
265 return T_EXIT_FAIL;
266 }
267 }
268
269 for (i = 0; i < 4; i++) {
270 bool abs = i & 1;
271 bool clock = i & 2;
272
273 if (abs && !support_abs)
274 continue;
275 if (clock && !support_clock)
276 continue;
277
278 ret = test_timeout(abs, clock);
279 if (ret) {
280 fprintf(stderr, "test_timeout failed %i %i\n",
281 abs, clock);
282 return ret;
283 }
284 }
285
286 return 0;
287 }
288