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.13 by root, Sat Dec 28 03:29:50 2019 UTC vs.
Revision 1.27 by sf-exg, Sun May 14 19:02:31 2023 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 versions 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 */
84 80
85/* TODO: use internal TIMEOUT */ 81/* TODO: use internal TIMEOUT */
86/* TODO: take advantage of single mmap, NODROP etc. */ 82/* TODO: take advantage of single mmap, NODROP etc. */
87/* TODO: resize cq/sq size independently */ 83/* TODO: resize cq/sq size independently */
122 __u32 timeout_flags; 118 __u32 timeout_flags;
123 __u32 accept_flags; 119 __u32 accept_flags;
124 __u32 cancel_flags; 120 __u32 cancel_flags;
125 __u32 open_flags; 121 __u32 open_flags;
126 __u32 statx_flags; 122 __u32 statx_flags;
123 __u32 fadvise_advice;
127 }; 124 };
128 __u64 user_data; 125 __u64 user_data;
129 union { 126 union {
130 __u16 buf_index; 127 __u16 buf_index;
128 __u16 personality;
131 __u64 __pad2[3]; 129 __u64 __pad2[3];
132 }; 130 };
133}; 131};
134 132
135struct io_uring_cqe 133struct io_uring_cqe
174 __u32 resv[4]; 172 __u32 resv[4];
175 struct io_sqring_offsets sq_off; 173 struct io_sqring_offsets sq_off;
176 struct io_cqring_offsets cq_off; 174 struct io_cqring_offsets cq_off;
177}; 175};
178 176
177#define IORING_FEAT_SINGLE_MMAP 0x00000001
178#define IORING_FEAT_NODROP 0x00000002
179#define IORING_FEAT_SUBMIT_STABLE 0x00000004
180
179#define IORING_SETUP_CQSIZE 0x00000008 181#define IORING_SETUP_CQSIZE 0x00000008
182#define IORING_SETUP_CLAMP 0x00000010
180 183
181#define IORING_OP_POLL_ADD 6 184#define IORING_OP_POLL_ADD 6
182#define IORING_OP_POLL_REMOVE 7 185#define IORING_OP_POLL_REMOVE 7
183#define IORING_OP_TIMEOUT 11 186#define IORING_OP_TIMEOUT 11
184#define IORING_OP_TIMEOUT_REMOVE 12 187#define IORING_OP_TIMEOUT_REMOVE 12
185 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
186/* relative or absolute, reference clock is CLOCK_MONOTONIC */ 211/* relative or absolute, reference clock is CLOCK_MONOTONIC */
187struct iouring_kernel_timespec 212struct iouring_kernel_timespec
188{ 213{
189 int64_t tv_sec; 214 int64_t tv_sec;
190 long long tv_nsec; 215 long long tv_nsec;
193#define IORING_TIMEOUT_ABS 0x00000001 218#define IORING_TIMEOUT_ABS 0x00000001
194 219
195#define IORING_ENTER_GETEVENTS 0x01 220#define IORING_ENTER_GETEVENTS 0x01
196 221
197#define IORING_OFF_SQ_RING 0x00000000ULL 222#define IORING_OFF_SQ_RING 0x00000000ULL
198#define IORING_OFF_CQ_RING 0x08000000ULL
199#define IORING_OFF_SQES 0x10000000ULL 223#define IORING_OFF_SQES 0x10000000ULL
200 224
201#define IORING_FEAT_SINGLE_MMAP 0x00000001 225#define IORING_FEAT_SINGLE_MMAP 0x00000001
202#define IORING_FEAT_NODROP 0x00000002 226#define IORING_FEAT_NODROP 0x00000002
203#define IORING_FEAT_SUBMIT_STABLE 0x00000004 227#define IORING_FEAT_SUBMIT_STABLE 0x00000004
214evsys_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)
215{ 239{
216 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);
217} 241}
218 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
219/*****************************************************************************/ 250/*****************************************************************************/
220/* actual backed implementation */ 251/* actual backend implementation */
221 252
222/* 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 */
223#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name)
224#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)
225 256
226/* the index array */ 257/* the index array */
227#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array)) 258#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))
228 259
229/* the submit/completion queue entries */ 260/* the submit/completion queue entries */
230#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) 261#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
231#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);
232 287
233static 288static
234struct io_uring_sqe * 289struct io_uring_sqe *
235iouring_sqe_get (EV_P) 290iouring_sqe_get (EV_P)
236{ 291{
292 unsigned tail;
293
294 for (;;)
295 {
237 unsigned tail = EV_SQ_VAR (tail); 296 tail = EV_SQ_VAR (tail);
238 297
239 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 */
240 { 317 }
241 /* queue full, flush */
242 evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0);
243 iouring_to_submit = 0;
244 }
245 318
246 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)));*/
247 320
248 return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); 321 return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
249} 322}
250 323
251inline_size 324inline_size
252struct io_uring_sqe * 325void
253iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) 326iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
254{ 327{
255 unsigned idx = sqe - EV_SQES; 328 unsigned idx = sqe - EV_SQES;
256 329
257 EV_SQ_ARRAY [idx] = idx; 330 EV_SQ_ARRAY [idx] = idx;
273 iouring_tfd_to = EV_TSTAMP_HUGE; 346 iouring_tfd_to = EV_TSTAMP_HUGE;
274} 347}
275 348
276/* called for full and partial cleanup */ 349/* called for full and partial cleanup */
277ecb_cold 350ecb_cold
278static int 351static void
279iouring_internal_destroy (EV_P) 352iouring_internal_destroy (EV_P)
280{ 353{
281 close (iouring_tfd); 354 close (iouring_tfd);
282 close (iouring_fd); 355 close (iouring_fd);
283 356
284 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); 357 if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
285 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size);
286 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); 358 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);
287 359
288 if (ev_is_active (&iouring_tfd_w)) 360 if (ev_is_active (&iouring_tfd_w))
289 { 361 {
290 ev_ref (EV_A); 362 ev_ref (EV_A);
291 ev_io_stop (EV_A_ &iouring_tfd_w); 363 ev_io_stop (EV_A_ &iouring_tfd_w);
295ecb_cold 367ecb_cold
296static int 368static int
297iouring_internal_init (EV_P) 369iouring_internal_init (EV_P)
298{ 370{
299 struct io_uring_params params = { 0 }; 371 struct io_uring_params params = { 0 };
372 uint32_t sq_size, cq_size;
373
374 params.flags = IORING_SETUP_CLAMP;
300 375
301 iouring_to_submit = 0; 376 iouring_to_submit = 0;
302 377
303 iouring_tfd = -1; 378 iouring_tfd = -1;
304 iouring_sq_ring = MAP_FAILED; 379 iouring_ring = MAP_FAILED;
305 iouring_cq_ring = MAP_FAILED;
306 iouring_sqes = MAP_FAILED; 380 iouring_sqes = MAP_FAILED;
307 381
308 if (!have_monotonic) /* cannot really happen, but what if11 */ 382 if (!have_monotonic) /* cannot really happen, but what if11 */
309 return -1; 383 return -1;
310 384
311 for (;;)
312 {
313 iouring_fd = evsys_io_uring_setup (iouring_entries, &params); 385 iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
314 386
315 if (iouring_fd >= 0) 387 if (iouring_fd < 0)
316 break; /* yippie */ 388 return -1;
317 389
318 if (errno != EINVAL)
319 return -1; /* we failed */
320
321#if TODO
322 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP)) 390 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
323 return -1; /* we require the above features */ 391 return -1; /* we require the above features */
324#endif
325 392
326 /* EINVAL: lots of possible reasons, but maybe 393 /* TODO: remember somehow whether our queue size has been clamped */
327 * it is because we hit the unqueryable hardcoded size limit
328 */
329 394
330 /* we hit the limit already, give up */
331 if (iouring_max_entries)
332 return -1;
333
334 /* first time we hit EINVAL? assume we hit the limit, so go back and retry */
335 iouring_entries >>= 1;
336 iouring_max_entries = iouring_entries;
337 }
338
339 iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); 395 sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
340 iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); 396 cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
397
398 iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
341 iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); 399 iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);
342 400
343 iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE, 401 iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE,
344 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); 402 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
345 iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE,
346 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING);
347 iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, 403 iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
348 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); 404 MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);
349 405
350 if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) 406 if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
351 return -1; 407 return -1;
352 408
353 iouring_sq_head = params.sq_off.head; 409 iouring_sq_head = params.sq_off.head;
354 iouring_sq_tail = params.sq_off.tail; 410 iouring_sq_tail = params.sq_off.tail;
355 iouring_sq_ring_mask = params.sq_off.ring_mask; 411 iouring_sq_ring_mask = params.sq_off.ring_mask;
363 iouring_cq_ring_mask = params.cq_off.ring_mask; 419 iouring_cq_ring_mask = params.cq_off.ring_mask;
364 iouring_cq_ring_entries = params.cq_off.ring_entries; 420 iouring_cq_ring_entries = params.cq_off.ring_entries;
365 iouring_cq_overflow = params.cq_off.overflow; 421 iouring_cq_overflow = params.cq_off.overflow;
366 iouring_cq_cqes = params.cq_off.cqes; 422 iouring_cq_cqes = params.cq_off.cqes;
367 423
424 iouring_tfd_to = EV_TSTAMP_HUGE;
425
368 iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); 426 iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);
369 427
370 if (iouring_tfd < 0) 428 if (iouring_tfd < 0)
371 return iouring_tfd; 429 return -1;
372
373 iouring_tfd_to = EV_TSTAMP_HUGE;
374 430
375 return 0; 431 return 0;
376} 432}
377 433
378ecb_cold 434ecb_cold
405 /* Jens Axboe notified me that user_data is not what is documented, but is 461 /* Jens Axboe notified me that user_data is not what is documented, but is
406 * some kind of unique ID that has to match, otherwise the request cannot 462 * some kind of unique ID that has to match, otherwise the request cannot
407 * be removed. Since we don't *really* have that, we pass in the old 463 * be removed. Since we don't *really* have that, we pass in the old
408 * generation counter - if that fails, too bad, it will hopefully be removed 464 * generation counter - if that fails, too bad, it will hopefully be removed
409 * at close time and then be ignored. */ 465 * at close time and then be ignored. */
410 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); 466 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
467 sqe->user_data = (uint64_t)-1;
411 iouring_sqe_submit (EV_A_ sqe); 468 iouring_sqe_submit (EV_A_ sqe);
412 469
413 /* increment generation counter to avoid handling old events */ 470 /* increment generation counter to avoid handling old events */
414 ++anfds [fd].egen; 471 ++anfds [fd].egen;
415 } 472 }
417 if (nev) 474 if (nev)
418 { 475 {
419 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 476 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
420 sqe->opcode = IORING_OP_POLL_ADD; 477 sqe->opcode = IORING_OP_POLL_ADD;
421 sqe->fd = fd; 478 sqe->fd = fd;
479 sqe->addr = 0;
422 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); 480 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
423 sqe->poll_events = 481 sqe->poll_events =
424 (nev & EV_READ ? POLLIN : 0) 482 (nev & EV_READ ? POLLIN : 0)
425 | (nev & EV_WRITE ? POLLOUT : 0); 483 | (nev & EV_WRITE ? POLLOUT : 0);
426 iouring_sqe_submit (EV_A_ sqe); 484 iouring_sqe_submit (EV_A_ sqe);
456{ 514{
457 int fd = cqe->user_data & 0xffffffffU; 515 int fd = cqe->user_data & 0xffffffffU;
458 uint32_t gen = cqe->user_data >> 32; 516 uint32_t gen = cqe->user_data >> 32;
459 int res = cqe->res; 517 int res = cqe->res;
460 518
519 /* user_data -1 is a remove that we are not atm. interested in */
520 if (cqe->user_data == (uint64_t)-1)
521 return;
522
461 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); 523 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
462 524
463 /* documentation lies, of course. the result value is NOT like 525 /* documentation lies, of course. the result value is NOT like
464 * normal syscalls, but like linux raw syscalls, i.e. negative 526 * normal syscalls, but like linux raw syscalls, i.e. negative
465 * error numbers. fortunate, as otherwise there would be no way 527 * error numbers. fortunate, as otherwise there would be no way
473 return; 535 return;
474 536
475 if (ecb_expect_false (res < 0)) 537 if (ecb_expect_false (res < 0))
476 { 538 {
477 /*TODO: EINVAL handling (was something failed with this fd)*/ 539 /*TODO: EINVAL handling (was something failed with this fd)*/
478 /*TODO: EBUSY happens when?*/
479 540
480 if (res == -EBADF) 541 if (res == -EBADF)
481 { 542 {
482 assert (("libev: event loop rejected bad fd", res != -EBADF)); 543 assert (("libev: event loop rejected bad fd", res != -EBADF));
483 fd_kill (EV_A_ fd); 544 fd_kill (EV_A_ fd);
589 650
590static void 651static void
591iouring_poll (EV_P_ ev_tstamp timeout) 652iouring_poll (EV_P_ ev_tstamp timeout)
592{ 653{
593 /* if we have events, no need for extra syscalls, but we might have to queue events */ 654 /* if we have events, no need for extra syscalls, but we might have to queue events */
655 /* we also clar the timeout if there are outstanding fdchanges */
656 /* the latter should only happen if both the sq and cq are full, most likely */
657 /* because we have a lot of event sources that immediately complete */
658 /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
594 if (iouring_handle_cq (EV_A)) 659 if (iouring_handle_cq (EV_A) || fdchangecnt)
595 timeout = EV_TS_CONST (0.); 660 timeout = EV_TS_CONST (0.);
596 else 661 else
597 /* no events, so maybe wait for some */ 662 /* no events, so maybe wait for some */
598 iouring_tfd_update (EV_A_ timeout); 663 iouring_tfd_update (EV_A_ timeout);
599 664
600 /* only enter the kernel if we have something to submit, or we need to wait */ 665 /* only enter the kernel if we have something to submit, or we need to wait */
601 if (timeout || iouring_to_submit) 666 if (timeout || iouring_to_submit)
602 { 667 {
603 int res; 668 int res = iouring_enter (EV_A_ timeout);
604
605 EV_RELEASE_CB;
606
607 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
608 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
609 iouring_to_submit = 0;
610
611 EV_ACQUIRE_CB;
612 669
613 if (ecb_expect_false (res < 0)) 670 if (ecb_expect_false (res < 0))
614 if (errno == EINTR) 671 if (errno == EINTR)
615 /* ignore */; 672 /* ignore */;
673 else if (errno == EBUSY)
674 /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
616 else 675 else
617 ev_syserr ("(libev) iouring setup"); 676 ev_syserr ("(libev) iouring setup");
618 else 677 else
619 iouring_handle_cq (EV_A); 678 iouring_handle_cq (EV_A);
620 } 679 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines