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.12 by root, Fri Dec 27 22:16:10 2019 UTC vs.
Revision 1.22 by root, Mon Jun 8 11:15:59 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 */
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 */
228 224
229/* the submit/completion queue entries */ 225/* the submit/completion queue entries */
230#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) 226#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
231#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) 227#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes))
232 228
229inline_speed
230int
231iouring_enter (EV_P_ ev_tstamp timeout)
232{
233 int res;
234
235 EV_RELEASE_CB;
236
237 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
238 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
239
240 assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
241
242 iouring_to_submit = 0;
243
244 EV_ACQUIRE_CB;
245
246 return res;
247}
248
249/* TODO: can we move things around so we don't need this forward-reference? */
250static void
251iouring_poll (EV_P_ ev_tstamp timeout);
252
233static 253static
234struct io_uring_sqe * 254struct io_uring_sqe *
235iouring_sqe_get (EV_P) 255iouring_sqe_get (EV_P)
236{ 256{
257 unsigned tail;
258
259 for (;;)
260 {
237 unsigned tail = EV_SQ_VAR (tail); 261 tail = EV_SQ_VAR (tail);
238 262
239 if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) 263 if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
264 break; /* whats the problem, we have free sqes */
265
266 /* queue full, need to flush and possibly handle some events */
267
268#if EV_FEATURE_CODE
269 /* first we ask the kernel nicely, most often this frees up some sqes */
270 int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
271
272 ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
273
274 if (res >= 0)
275 continue; /* yes, it worked, try again */
276#endif
277
278 /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
279
280 iouring_poll (EV_A_ EV_TS_CONST (0.));
281 /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
240 { 282 }
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 283
246 assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); 284 /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
247 285
248 return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); 286 return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
249} 287}
250 288
251inline_size 289inline_size
317 355
318 if (errno != EINVAL) 356 if (errno != EINVAL)
319 return -1; /* we failed */ 357 return -1; /* we failed */
320 358
321#if TODO 359#if TODO
322 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP)) 360 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
323 return -1; /* we require the above features */ 361 return -1; /* we require the above features */
324#endif 362#endif
325 363
326 /* EINVAL: lots of possible reasons, but maybe 364 /* EINVAL: lots of possible reasons, but maybe
327 * it is because we hit the unqueryable hardcoded size limit 365 * it is because we hit the unqueryable hardcoded size limit
400 { 438 {
401 /* we assume the sqe's are all "properly" initialised */ 439 /* we assume the sqe's are all "properly" initialised */
402 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 440 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
403 sqe->opcode = IORING_OP_POLL_REMOVE; 441 sqe->opcode = IORING_OP_POLL_REMOVE;
404 sqe->fd = fd; 442 sqe->fd = fd;
443 /* Jens Axboe notified me that user_data is not what is documented, but is
444 * some kind of unique ID that has to match, otherwise the request cannot
445 * be removed. Since we don't *really* have that, we pass in the old
446 * generation counter - if that fails, too bad, it will hopefully be removed
447 * at close time and then be ignored. */
448 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
405 sqe->user_data = -1; 449 sqe->user_data = (uint64_t)-1;
406 iouring_sqe_submit (EV_A_ sqe); 450 iouring_sqe_submit (EV_A_ sqe);
407 451
408 /* increment generation counter to avoid handling old events */ 452 /* increment generation counter to avoid handling old events */
409 ++anfds [fd].egen; 453 ++anfds [fd].egen;
410 } 454 }
412 if (nev) 456 if (nev)
413 { 457 {
414 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 458 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
415 sqe->opcode = IORING_OP_POLL_ADD; 459 sqe->opcode = IORING_OP_POLL_ADD;
416 sqe->fd = fd; 460 sqe->fd = fd;
461 sqe->addr = 0;
417 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); 462 sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
418 sqe->poll_events = 463 sqe->poll_events =
419 (nev & EV_READ ? POLLIN : 0) 464 (nev & EV_READ ? POLLIN : 0)
420 | (nev & EV_WRITE ? POLLOUT : 0); 465 | (nev & EV_WRITE ? POLLOUT : 0);
421 iouring_sqe_submit (EV_A_ sqe); 466 iouring_sqe_submit (EV_A_ sqe);
451{ 496{
452 int fd = cqe->user_data & 0xffffffffU; 497 int fd = cqe->user_data & 0xffffffffU;
453 uint32_t gen = cqe->user_data >> 32; 498 uint32_t gen = cqe->user_data >> 32;
454 int res = cqe->res; 499 int res = cqe->res;
455 500
456 /* ignore fd removal events, if there are any. TODO: verify */ 501 /* user_data -1 is a remove that we are not atm. interested in */
457 /* TODO: yes, this triggers */
458 if (cqe->user_data == (__u64)-1) 502 if (cqe->user_data == (uint64_t)-1)
459 return; 503 return;
460 504
461 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); 505 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
462 506
463 /* documentation lies, of course. the result value is NOT like 507 /* documentation lies, of course. the result value is NOT like
465 * error numbers. fortunate, as otherwise there would be no way 509 * error numbers. fortunate, as otherwise there would be no way
466 * to get error codes at all. still, why not document this? 510 * to get error codes at all. still, why not document this?
467 */ 511 */
468 512
469 /* ignore event if generation doesn't match */ 513 /* ignore event if generation doesn't match */
514 /* other than skipping removal events, */
470 /* this should actually be very rare */ 515 /* this should actually be very rare */
471 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) 516 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
472 return; 517 return;
473 518
474 if (ecb_expect_false (res < 0)) 519 if (ecb_expect_false (res < 0))
475 { 520 {
476 /*TODO: EINVAL handling (was something failed with this fd)*/ 521 /*TODO: EINVAL handling (was something failed with this fd)*/
477 /*TODO: EBUSY happens when?*/
478 522
479 if (res == -EBADF) 523 if (res == -EBADF)
480 { 524 {
481 assert (("libev: event loop rejected bad fd", res != -EBADF)); 525 assert (("libev: event loop rejected bad fd", res != -EBADF));
482 fd_kill (EV_A_ fd); 526 fd_kill (EV_A_ fd);
588 632
589static void 633static void
590iouring_poll (EV_P_ ev_tstamp timeout) 634iouring_poll (EV_P_ ev_tstamp timeout)
591{ 635{
592 /* if we have events, no need for extra syscalls, but we might have to queue events */ 636 /* if we have events, no need for extra syscalls, but we might have to queue events */
637 /* we also clar the timeout if there are outstanding fdchanges */
638 /* the latter should only happen if both the sq and cq are full, most likely */
639 /* because we have a lot of event sources that immediately complete */
640 /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
593 if (iouring_handle_cq (EV_A)) 641 if (iouring_handle_cq (EV_A) || fdchangecnt)
594 timeout = EV_TS_CONST (0.); 642 timeout = EV_TS_CONST (0.);
595 else 643 else
596 /* no events, so maybe wait for some */ 644 /* no events, so maybe wait for some */
597 iouring_tfd_update (EV_A_ timeout); 645 iouring_tfd_update (EV_A_ timeout);
598 646
599 /* only enter the kernel if we have something to submit, or we need to wait */ 647 /* only enter the kernel if we have something to submit, or we need to wait */
600 if (timeout || iouring_to_submit) 648 if (timeout || iouring_to_submit)
601 { 649 {
602 int res; 650 int res = iouring_enter (EV_A_ timeout);
603
604 EV_RELEASE_CB;
605
606 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
607 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
608 iouring_to_submit = 0;
609
610 EV_ACQUIRE_CB;
611 651
612 if (ecb_expect_false (res < 0)) 652 if (ecb_expect_false (res < 0))
613 if (errno == EINTR) 653 if (errno == EINTR)
614 /* ignore */; 654 /* ignore */;
655 else if (errno == EBUSY)
656 /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
615 else 657 else
616 ev_syserr ("(libev) iouring setup"); 658 ev_syserr ("(libev) iouring setup");
617 else 659 else
618 iouring_handle_cq (EV_A); 660 iouring_handle_cq (EV_A);
619 } 661 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines