ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_iouring.c
(Generate patch)

Comparing libev/ev_iouring.c (file contents):
Revision 1.7 by root, Sun Dec 22 15:05:32 2019 UTC vs.
Revision 1.23 by root, Sun Jul 26 11:10:45 2020 UTC

1/* 1/*
2 * libev linux io_uring fd activity backend 2 * libev linux io_uring fd activity backend
3 * 3 *
4 * Copyright (c) 2019 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2019-2020 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
44 * b) best is not necessarily very good. 44 * b) best is not necessarily very good.
45 * c) it's better than the aio mess, doesn't suffer from the fork problems 45 * c) it's better than the aio mess, doesn't suffer from the fork problems
46 * of linux aio or epoll and so on and so on. and you could do event stuff 46 * of linux aio or epoll and so on and so on. and you could do event stuff
47 * without any syscalls. what's not to like? 47 * without any syscalls. what's not to like?
48 * d) ok, it's vastly more complex, but that's ok, really. 48 * d) ok, it's vastly more complex, but that's ok, really.
49 * e) why 3 mmaps instead of one? one would be more space-efficient, 49 * e) why two mmaps instead of one? one would be more space-efficient,
50 * and I can't see what benefit three would have (other than being 50 * and I can't see what benefit two would have (other than being
51 * somehow resizable/relocatable, but that's apparently not possible). 51 * somehow resizable/relocatable, but that's apparently not possible).
52 * (FIXME: newer kernels can use 2 mmaps only, need to look into this).
53 * f) hmm, it's practiclaly undebuggable (gdb can't access the memory, and 52 * f) hmm, it's practically undebuggable (gdb can't access the memory, and
54 * the bizarre way structure offsets are communicated makes it hard to 53 * the bizarre way structure offsets are communicated makes it hard to
55 * just print the ring buffer heads, even *iff* the memory were visible 54 * just print the ring buffer heads, even *iff* the memory were visible
56 * in gdb. but then, that's also ok, really. 55 * in gdb. but then, that's also ok, really.
57 * g) well, you cannot specify a timeout when waiting for events. no, 56 * g) well, you cannot specify a timeout when waiting for events. no,
58 * seriously, the interface doesn't support a timeout. never seen _that_ 57 * seriously, the interface doesn't support a timeout. never seen _that_
59 * before. sure, you can use a timerfd, but that's another syscall 58 * before. sure, you can use a timerfd, but that's another syscall
60 * you could have avoided. overall, this bizarre omission smells 59 * you could have avoided. overall, this bizarre omission smells
61 * like a µ-optimisation by the io_uring author for his personal 60 * like a µ-optimisation by the io_uring author for his personal
62 * applications, to the detriment of everybody else who just wants 61 * applications, to the detriment of everybody else who just wants
63 * an event loop. but, umm, ok, if that's all, it could be worse. 62 * an event loop. but, umm, ok, if that's all, it could be worse.
64 * (FIXME: jens mentioned timeout commands, need to investigate) 63 * (from what I gather from the author Jens Axboe, it simply didn't
64 * occur to him, and he made good on it by adding an unlimited number
65 * of timeouts later :).
65 * h) there is a hardcoded limit of 4096 outstanding events. okay, 66 * h) initially there was a hardcoded limit of 4096 outstanding events.
66 * at least there is no arbitrary low system-wide limit... 67 * later versions not only bump this to 32k, but also can handle
67 * (FIXME: apparently, this was increased to 32768 in later kernels( 68 * an unlimited amount of events, so this only affects the batch size.
68 * i) unlike linux aio, you *can* register more then the limit 69 * i) unlike linux aio, you *can* register more then the limit
69 * of fd events, and the kernel will "gracefully" signal an 70 * of fd events. while early verisons of io_uring signalled an overflow
70 * overflow, after which you could destroy and recreate the kernel 71 * and you ended up getting wet. 5.5+ does not do this anymore.
71 * state, a bit bigger, or fall back to e.g. poll. thats not
72 * totally insane, but kind of questions the point a high
73 * performance I/O framework when it doesn't really work
74 * under stress.
75 * (FIXME: iouring should no longer drop events, need to investigate)
76 * j) but, oh my! is has exactly the same bugs as the linux aio backend, 72 * j) but, oh my! it had exactly the same bugs as the linux aio backend,
77 * where some undocumented poll combinations just fail. 73 * where some undocumented poll combinations just fail. fortunately,
78 * so we need epoll AGAIN as a fallback. AGAIN! epoll!! and of course, 74 * after finally reaching the author, he was more than willing to fix
79 * this is completely undocumented, have I mantioned this already? 75 * this probably in 5.6+.
80 * k) overall, the *API* itself is, I dare to say, not a total trainwreck. 76 * k) overall, the *API* itself is, I dare to say, not a total trainwreck.
81 * the big isuess with it are the bugs requiring epoll, which might 77 * once the bugs ae fixed (probably in 5.6+), it will be without
82 * or might not get fixed (do I hold my breath?). 78 * competition.
83 */ 79 */
80
81/* TODO: use internal TIMEOUT */
82/* TODO: take advantage of single mmap, NODROP etc. */
83/* TODO: resize cq/sq size independently */
84 84
85#include <sys/timerfd.h> 85#include <sys/timerfd.h>
86#include <sys/mman.h> 86#include <sys/mman.h>
87#include <poll.h> 87#include <poll.h>
88#include <stdint.h>
88 89
89#define IOURING_INIT_ENTRIES 32 90#define IOURING_INIT_ENTRIES 32
90 91
91/*****************************************************************************/ 92/*****************************************************************************/
92/* syscall wrapdadoop - this section has the raw api/abi definitions */ 93/* syscall wrapdadoop - this section has the raw api/abi definitions */
100{ 101{
101 __u8 opcode; 102 __u8 opcode;
102 __u8 flags; 103 __u8 flags;
103 __u16 ioprio; 104 __u16 ioprio;
104 __s32 fd; 105 __s32 fd;
106 union {
105 __u64 off; 107 __u64 off;
108 __u64 addr2;
109 };
106 __u64 addr; 110 __u64 addr;
107 __u32 len; 111 __u32 len;
108 union { 112 union {
109 __kernel_rwf_t rw_flags; 113 __kernel_rwf_t rw_flags;
110 __u32 fsync_flags; 114 __u32 fsync_flags;
111 __u16 poll_events; 115 __u16 poll_events;
112 __u32 sync_range_flags; 116 __u32 sync_range_flags;
113 __u32 msg_flags; 117 __u32 msg_flags;
118 __u32 timeout_flags;
119 __u32 accept_flags;
120 __u32 cancel_flags;
121 __u32 open_flags;
122 __u32 statx_flags;
123 __u32 fadvise_advice;
114 }; 124 };
115 __u64 user_data; 125 __u64 user_data;
116 union { 126 union {
117 __u16 buf_index; 127 __u16 buf_index;
128 __u16 personality;
118 __u64 __pad2[3]; 129 __u64 __pad2[3];
119 }; 130 };
120}; 131};
121 132
122struct io_uring_cqe 133struct io_uring_cqe
155 __u32 sq_entries; 166 __u32 sq_entries;
156 __u32 cq_entries; 167 __u32 cq_entries;
157 __u32 flags; 168 __u32 flags;
158 __u32 sq_thread_cpu; 169 __u32 sq_thread_cpu;
159 __u32 sq_thread_idle; 170 __u32 sq_thread_idle;
171 __u32 features;
160 __u32 resv[5]; 172 __u32 resv[4];
161 struct io_sqring_offsets sq_off; 173 struct io_sqring_offsets sq_off;
162 struct io_cqring_offsets cq_off; 174 struct io_cqring_offsets cq_off;
163}; 175};
164 176
177#define IORING_FEAT_SINGLE_MMAP 0x00000001
178#define IORING_FEAT_NODROP 0x00000002
179#define IORING_FEAT_SUBMIT_STABLE 0x00000004
180
181#define IORING_SETUP_CQSIZE 0x00000008
182#define IORING_SETUP_CLAMP 0x00000010
183
165#define IORING_OP_POLL_ADD 6 184#define IORING_OP_POLL_ADD 6
166#define IORING_OP_POLL_REMOVE 7 185#define IORING_OP_POLL_REMOVE 7
186#define IORING_OP_TIMEOUT 11
187#define IORING_OP_TIMEOUT_REMOVE 12
188
189#define IORING_REGISTER_EVENTFD 4
190#define IORING_REGISTER_EVENTFD_ASYNC 7
191#define IORING_REGISTER_PROBE 8
192
193#define IO_URING_OP_SUPPORTED 1
194
195struct io_uring_probe_op {
196 __u8 op;
197 __u8 resv;
198 __u16 flags;
199 __u32 resv2;
200};
201
202struct io_uring_probe
203{
204 __u8 last_op;
205 __u8 ops_len;
206 __u16 resv;
207 __u32 resv2[3];
208 struct io_uring_probe_op ops[0];
209};
210
211/* relative or absolute, reference clock is CLOCK_MONOTONIC */
212struct iouring_kernel_timespec
213{
214 int64_t tv_sec;
215 long long tv_nsec;
216};
217
218#define IORING_TIMEOUT_ABS 0x00000001
167 219
168#define IORING_ENTER_GETEVENTS 0x01 220#define IORING_ENTER_GETEVENTS 0x01
169 221
170#define IORING_OFF_SQ_RING 0x00000000ULL 222#define IORING_OFF_SQ_RING 0x00000000ULL
171#define IORING_OFF_CQ_RING 0x08000000ULL
172#define IORING_OFF_SQES 0x10000000ULL 223#define IORING_OFF_SQES 0x10000000ULL
224
225#define IORING_FEAT_SINGLE_MMAP 0x00000001
226#define IORING_FEAT_NODROP 0x00000002
227#define IORING_FEAT_SUBMIT_STABLE 0x00000004
173 228
174inline_size 229inline_size
175int 230int
176evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) 231evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
177{ 232{
183evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz) 238evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz)
184{ 239{
185 return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz); 240 return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz);
186} 241}
187 242
243inline_size
244int
245evsys_io_uring_register (unsigned int fd, unsigned int opcode, void *arg, unsigned int nr_args)
246{
247 return ev_syscall4 (SYS_io_uring_register, fd, opcode, arg, nr_args);
248}
249
188/*****************************************************************************/ 250/*****************************************************************************/
189/* actual backed implementation */ 251/* actual backend implementation */
190 252
191/* we hope that volatile will make the compiler access this variables only once */ 253/* we hope that volatile will make the compiler access this variables only once */
192#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name)
193#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_cq_ring + iouring_cq_ ## name) 254#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_sq_ ## name)
255#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_cq_ ## name)
194 256
195/* the index array */ 257/* the index array */
196#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array)) 258#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))
197 259
198/* the submit/completion queue entries */ 260/* the submit/completion queue entries */
199#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) 261#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
200#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) 262#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes))
263
264inline_speed
265int
266iouring_enter (EV_P_ ev_tstamp timeout)
267{
268 int res;
269
270 EV_RELEASE_CB;
271
272 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
273 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
274
275 assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
276
277 iouring_to_submit = 0;
278
279 EV_ACQUIRE_CB;
280
281 return res;
282}
283
284/* TODO: can we move things around so we don't need this forward-reference? */
285static void
286iouring_poll (EV_P_ ev_tstamp timeout);
201 287
202static 288static
203struct io_uring_sqe * 289struct io_uring_sqe *
204iouring_sqe_get (EV_P) 290iouring_sqe_get (EV_P)
205{ 291{
292 unsigned tail;
293
294 for (;;)
295 {
206 unsigned tail = EV_SQ_VAR (tail); 296 tail = EV_SQ_VAR (tail);
207 297
208 if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) 298 if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
299 break; /* whats the problem, we have free sqes */
300
301 /* queue full, need to flush and possibly handle some events */
302
303#if EV_FEATURE_CODE
304 /* first we ask the kernel nicely, most often this frees up some sqes */
305 int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
306
307 ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
308
309 if (res >= 0)
310 continue; /* yes, it worked, try again */
311#endif
312
313 /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
314
315 iouring_poll (EV_A_ EV_TS_CONST (0.));
316 /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
209 { 317 }
210 /* queue full, flush */
211 evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0);
212 iouring_to_submit = 0;
213 }
214 318
215 assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); 319 /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
216 320
217 return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); 321 return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
218} 322}
219 323
220inline_size 324inline_size
221struct io_uring_sqe * 325struct io_uring_sqe *
222iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) 326iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
223{ 327{
224 unsigned idx = sqe - EV_SQES; 328 unsigned idx = sqe - EV_SQES;
329
330 printf ("submit idx %d, op %d, fd %d, us5r %p, poll %d\n", idx, sqe->opcode, sqe->fd, sqe->user_data, sqe->poll_events);
225 331
226 EV_SQ_ARRAY [idx] = idx; 332 EV_SQ_ARRAY [idx] = idx;
227 ECB_MEMORY_FENCE_RELEASE; 333 ECB_MEMORY_FENCE_RELEASE;
228 ++EV_SQ_VAR (tail); 334 ++EV_SQ_VAR (tail);
229 /*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */ 335 /*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */
240iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) 346iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
241{ 347{
242 iouring_tfd_to = EV_TSTAMP_HUGE; 348 iouring_tfd_to = EV_TSTAMP_HUGE;
243} 349}
244 350
245static void
246iouring_epoll_cb (EV_P_ struct ev_io *w, int revents)
247{
248 epoll_poll (EV_A_ 0);
249}
250
251/* called for full and partial cleanup */ 351/* called for full and partial cleanup */
252ecb_cold 352ecb_cold
253static int 353static int
254iouring_internal_destroy (EV_P) 354iouring_internal_destroy (EV_P)
255{ 355{
256 close (iouring_tfd); 356 close (iouring_tfd);
257 close (iouring_fd); 357 close (iouring_fd);
258 358
259 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); 359 if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
260 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size);
261 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); 360 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);
262 361
263 if (ev_is_active (&iouring_epoll_w)) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_epoll_w); 362 if (ev_is_active (&iouring_tfd_w))
264 if (ev_is_active (&iouring_tfd_w )) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_tfd_w ); 363 {
364 ev_ref (EV_A);
365 ev_io_stop (EV_A_ &iouring_tfd_w);
366 }
265} 367}
266 368
267ecb_cold 369ecb_cold
268static int 370static int
269iouring_internal_init (EV_P) 371iouring_internal_init (EV_P)
270{ 372{
271 struct io_uring_params params = { 0 }; 373 struct io_uring_params params = { 0 };
374 uint32_t sq_size, cq_size;
375
376 params.flags = IORING_SETUP_CLAMP;
272 377
273 iouring_to_submit = 0; 378 iouring_to_submit = 0;
274 379
275 iouring_tfd = -1; 380 iouring_tfd = -1;
276 iouring_sq_ring = MAP_FAILED; 381 iouring_ring = MAP_FAILED;
277 iouring_cq_ring = MAP_FAILED;
278 iouring_sqes = MAP_FAILED; 382 iouring_sqes = MAP_FAILED;
279 383
280 for (;;) 384 if (!have_monotonic) /* cannot really happen, but what if11 */
281 { 385 return -1;
386
282 iouring_fd = evsys_io_uring_setup (iouring_entries, &params); 387 iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
283 388
284 if (iouring_fd >= 0) 389 if (iouring_fd < 0)
285 break; /* yippie */
286
287 if (errno != EINVAL)
288 return -1; /* we failed */
289
290 /* EINVAL: lots of possible reasons, but maybe
291 * it is because we hit the unqueryable hardcoded size limit
292 */
293
294 /* we hit the limit already, give up */
295 if (iouring_max_entries)
296 return -1; 390 return -1;
297 391
298 /* first time we hit EINVAL? assume we hit the limit, so go back and retry */ 392 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
299 iouring_entries >>= 1; 393 return -1; /* we require the above features */
300 iouring_max_entries = iouring_entries;
301 }
302 394
395 /* TODO: remember somehow whether our queue size has been clamped */
396
303 iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); 397 sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
304 iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); 398 cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
399
400 iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
305 iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); 401 iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);
306 402
307 iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE, 403 iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE,
308 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); 404 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
309 iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE,
310 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING);
311 iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, 405 iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
312 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); 406 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);
313 407
314 if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) 408 if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
315 return -1; 409 return -1;
316 410
317 iouring_sq_head = params.sq_off.head; 411 iouring_sq_head = params.sq_off.head;
318 iouring_sq_tail = params.sq_off.tail; 412 iouring_sq_tail = params.sq_off.tail;
319 iouring_sq_ring_mask = params.sq_off.ring_mask; 413 iouring_sq_ring_mask = params.sq_off.ring_mask;
327 iouring_cq_ring_mask = params.cq_off.ring_mask; 421 iouring_cq_ring_mask = params.cq_off.ring_mask;
328 iouring_cq_ring_entries = params.cq_off.ring_entries; 422 iouring_cq_ring_entries = params.cq_off.ring_entries;
329 iouring_cq_overflow = params.cq_off.overflow; 423 iouring_cq_overflow = params.cq_off.overflow;
330 iouring_cq_cqes = params.cq_off.cqes; 424 iouring_cq_cqes = params.cq_off.cqes;
331 425
426 iouring_tfd_to = EV_TSTAMP_HUGE;
427
332 iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); 428 iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);
333 429
334 if (iouring_tfd < 0) 430 if (iouring_tfd < 0)
335 return iouring_tfd; 431 return -1;
336
337 iouring_tfd_to = EV_TSTAMP_HUGE;
338 432
339 return 0; 433 return 0;
340} 434}
341 435
342ecb_cold 436ecb_cold
346 iouring_internal_destroy (EV_A); 440 iouring_internal_destroy (EV_A);
347 441
348 while (iouring_internal_init (EV_A) < 0) 442 while (iouring_internal_init (EV_A) < 0)
349 ev_syserr ("(libev) io_uring_setup"); 443 ev_syserr ("(libev) io_uring_setup");
350 444
351 /* forking epoll should also effectively unregister all fds from the backend */
352 epoll_fork (EV_A);
353 /* epoll_fork already did this. hopefully */
354 /*fd_rearm_all (EV_A);*/ 445 fd_rearm_all (EV_A);
355
356 ev_io_stop (EV_A_ &iouring_epoll_w);
357 ev_io_set (EV_A_ &iouring_epoll_w, backend_fd, EV_READ);
358 ev_io_start (EV_A_ &iouring_epoll_w);
359 446
360 ev_io_stop (EV_A_ &iouring_tfd_w); 447 ev_io_stop (EV_A_ &iouring_tfd_w);
361 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); 448 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
362 ev_io_start (EV_A_ &iouring_tfd_w); 449 ev_io_start (EV_A_ &iouring_tfd_w);
363} 450}
365/*****************************************************************************/ 452/*****************************************************************************/
366 453
367static void 454static void
368iouring_modify (EV_P_ int fd, int oev, int nev) 455iouring_modify (EV_P_ int fd, int oev, int nev)
369{ 456{
370 if (ecb_expect_false (anfds [fd].eflags))
371 {
372 /* we handed this fd over to epoll, so undo this first */
373 /* we do it manually because the optimisations on epoll_modify won't do us any good */
374 epoll_ctl (iouring_fd, EPOLL_CTL_DEL, fd, 0);
375 anfds [fd].eflags = 0;
376 oev = 0;
377 }
378
379 if (oev) 457 if (oev)
380 { 458 {
381 /* we assume the sqe's are all "properly" initialised */ 459 /* we assume the sqe's are all "properly" initialised */
382 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 460 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
383 sqe->opcode = IORING_OP_POLL_REMOVE; 461 sqe->opcode = IORING_OP_POLL_REMOVE;
384 sqe->fd = fd; 462 sqe->fd = fd;
463 /* Jens Axboe notified me that user_data is not what is documented, but is
464 * some kind of unique ID that has to match, otherwise the request cannot
465 * be removed. Since we don't *really* have that, we pass in the old
466 * generation counter - if that fails, too bad, it will hopefully be removed
467 * at close time and then be ignored. */
468 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
385 sqe->user_data = -1; 469 sqe->user_data = (uint64_t)-1;
386 iouring_sqe_submit (EV_A_ sqe); 470 iouring_sqe_submit (EV_A_ sqe);
387 471
388 /* increment generation counter to avoid handling old events */ 472 /* increment generation counter to avoid handling old events */
389 ++anfds [fd].egen; 473 ++anfds [fd].egen;
390 } 474 }
392 if (nev) 476 if (nev)
393 { 477 {
394 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 478 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
395 sqe->opcode = IORING_OP_POLL_ADD; 479 sqe->opcode = IORING_OP_POLL_ADD;
396 sqe->fd = fd; 480 sqe->fd = fd;
481 sqe->addr = 0;
397 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); 482 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
398 sqe->poll_events = 483 sqe->poll_events =
399 (nev & EV_READ ? POLLIN : 0) 484 (nev & EV_READ ? POLLIN : 0)
400 | (nev & EV_WRITE ? POLLOUT : 0); 485 | (nev & EV_WRITE ? POLLOUT : 0);
401 iouring_sqe_submit (EV_A_ sqe); 486 iouring_sqe_submit (EV_A_ sqe);
431{ 516{
432 int fd = cqe->user_data & 0xffffffffU; 517 int fd = cqe->user_data & 0xffffffffU;
433 uint32_t gen = cqe->user_data >> 32; 518 uint32_t gen = cqe->user_data >> 32;
434 int res = cqe->res; 519 int res = cqe->res;
435 520
436 /* ignore fd removal events, if there are any. TODO: verify */ 521 /* user_data -1 is a remove that we are not atm. interested in */
437 if (cqe->user_data == (__u64)-1) 522 if (cqe->user_data == (uint64_t)-1)
438 abort ();//D 523 return;
439 524
440 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); 525 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
441 526
442 /* documentation lies, of course. the result value is NOT like 527 /* documentation lies, of course. the result value is NOT like
443 * normal syscalls, but like linux raw syscalls, i.e. negative 528 * normal syscalls, but like linux raw syscalls, i.e. negative
444 * error numbers. fortunate, as otherwise there would be no way 529 * error numbers. fortunate, as otherwise there would be no way
445 * to get error codes at all. still, why not document this? 530 * to get error codes at all. still, why not document this?
446 */ 531 */
447 532
448 /* ignore event if generation doesn't match */ 533 /* ignore event if generation doesn't match */
534 /* other than skipping removal events, */
449 /* this should actually be very rare */ 535 /* this should actually be very rare */
450 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) 536 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
451 return; 537 return;
452 538
453 if (ecb_expect_false (res < 0)) 539 if (ecb_expect_false (res < 0))
454 { 540 {
455 if (res == -EINVAL) 541 /*TODO: EINVAL handling (was something failed with this fd)*/
456 {
457 /* we assume this error code means the fd/poll combination is buggy
458 * and fall back to epoll.
459 * this error code might also indicate a bug, but the kernel doesn't
460 * distinguish between those two conditions, so... sigh...
461 */
462 542
463 epoll_modify (EV_A_ fd, 0, anfds [fd].events);
464 }
465 else if (res == -EBADF) 543 if (res == -EBADF)
466 { 544 {
467 assert (("libev: event loop rejected bad fd", res != -EBADF)); 545 assert (("libev: event loop rejected bad fd", res != -EBADF));
468 fd_kill (EV_A_ fd); 546 fd_kill (EV_A_ fd);
469 } 547 }
470 else 548 else
496iouring_overflow (EV_P) 574iouring_overflow (EV_P)
497{ 575{
498 /* we have two options, resize the queue (by tearing down 576 /* we have two options, resize the queue (by tearing down
499 * everything and recreating it, or living with it 577 * everything and recreating it, or living with it
500 * and polling. 578 * and polling.
501 * we implement this by resizing tghe queue, and, if that fails, 579 * we implement this by resizing the queue, and, if that fails,
502 * we just recreate the state on every failure, which 580 * we just recreate the state on every failure, which
503 * kind of is a very inefficient poll. 581 * kind of is a very inefficient poll.
504 * one danger is, due to the bios toward lower fds, 582 * one danger is, due to the bios toward lower fds,
505 * we will only really get events for those, so 583 * we will only really get events for those, so
506 * maybe we need a poll() fallback, after all. 584 * maybe we need a poll() fallback, after all.
518 else 596 else
519 { 597 {
520 /* we hit the kernel limit, we should fall back to something else. 598 /* we hit the kernel limit, we should fall back to something else.
521 * we can either poll() a few times and hope for the best, 599 * we can either poll() a few times and hope for the best,
522 * poll always, or switch to epoll. 600 * poll always, or switch to epoll.
523 * since we use epoll anyways, go epoll. 601 * TODO: is this necessary with newer kernels?
524 */ 602 */
525 603
526 iouring_internal_destroy (EV_A); 604 iouring_internal_destroy (EV_A);
527 605
528 /* this should make it so that on return, we don'T call any uring functions */ 606 /* this should make it so that on return, we don't call any uring functions */
529 iouring_to_submit = 0; 607 iouring_to_submit = 0;
530 608
531 for (;;) 609 for (;;)
532 { 610 {
533 backend = epoll_init (EV_A_ 0); 611 backend = epoll_init (EV_A_ 0);
574 652
575static void 653static void
576iouring_poll (EV_P_ ev_tstamp timeout) 654iouring_poll (EV_P_ ev_tstamp timeout)
577{ 655{
578 /* if we have events, no need for extra syscalls, but we might have to queue events */ 656 /* if we have events, no need for extra syscalls, but we might have to queue events */
657 /* we also clar the timeout if there are outstanding fdchanges */
658 /* the latter should only happen if both the sq and cq are full, most likely */
659 /* because we have a lot of event sources that immediately complete */
660 /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
579 if (iouring_handle_cq (EV_A)) 661 if (iouring_handle_cq (EV_A) || fdchangecnt)
580 timeout = EV_TS_CONST (0.); 662 timeout = EV_TS_CONST (0.);
581 else 663 else
582 /* no events, so maybe wait for some */ 664 /* no events, so maybe wait for some */
583 iouring_tfd_update (EV_A_ timeout); 665 iouring_tfd_update (EV_A_ timeout);
584 666
585 /* only enter the kernel if we have something to submit, or we need to wait */ 667 /* only enter the kernel if we have something to submit, or we need to wait */
586 if (timeout || iouring_to_submit) 668 if (timeout || iouring_to_submit)
587 { 669 {
588 int res; 670 int res = iouring_enter (EV_A_ timeout);
589
590 EV_RELEASE_CB;
591
592 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
593 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
594 iouring_to_submit = 0;
595
596 EV_ACQUIRE_CB;
597 671
598 if (ecb_expect_false (res < 0)) 672 if (ecb_expect_false (res < 0))
599 if (errno == EINTR) 673 if (errno == EINTR)
600 /* ignore */; 674 /* ignore */;
675 else if (errno == EBUSY)
676 /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
601 else 677 else
602 ev_syserr ("(libev) iouring setup"); 678 ev_syserr ("(libev) iouring setup");
603 else 679 else
604 iouring_handle_cq (EV_A); 680 iouring_handle_cq (EV_A);
605 } 681 }
607 683
608inline_size 684inline_size
609int 685int
610iouring_init (EV_P_ int flags) 686iouring_init (EV_P_ int flags)
611{ 687{
612 if (!epoll_init (EV_A_ 0))
613 return 0;
614
615 iouring_entries = IOURING_INIT_ENTRIES; 688 iouring_entries = IOURING_INIT_ENTRIES;
616 iouring_max_entries = 0; 689 iouring_max_entries = 0;
617 690
618 if (iouring_internal_init (EV_A) < 0) 691 if (iouring_internal_init (EV_A) < 0)
619 { 692 {
620 iouring_internal_destroy (EV_A); 693 iouring_internal_destroy (EV_A);
621 return 0; 694 return 0;
622 } 695 }
623 696
624 ev_io_init (&iouring_epoll_w, iouring_epoll_cb, backend_fd, EV_READ);
625 ev_set_priority (&iouring_epoll_w, EV_MAXPRI);
626
627 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); 697 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
628 ev_set_priority (&iouring_tfd_w, EV_MAXPRI); 698 ev_set_priority (&iouring_tfd_w, EV_MINPRI);
629
630 ev_io_start (EV_A_ &iouring_epoll_w);
631 ev_unref (EV_A); /* watcher should not keep loop alive */
632
633 ev_io_start (EV_A_ &iouring_tfd_w); 699 ev_io_start (EV_A_ &iouring_tfd_w);
634 ev_unref (EV_A); /* watcher should not keep loop alive */ 700 ev_unref (EV_A); /* watcher should not keep loop alive */
635 701
636 backend_modify = iouring_modify; 702 backend_modify = iouring_modify;
637 backend_poll = iouring_poll; 703 backend_poll = iouring_poll;
642inline_size 708inline_size
643void 709void
644iouring_destroy (EV_P) 710iouring_destroy (EV_P)
645{ 711{
646 iouring_internal_destroy (EV_A); 712 iouring_internal_destroy (EV_A);
647 epoll_destroy (EV_A);
648} 713}
649 714

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines