… | |
… | |
228 | |
228 | |
229 | /* the submit/completion queue entries */ |
229 | /* the submit/completion queue entries */ |
230 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
230 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
231 | #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) |
231 | #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) |
232 | |
232 | |
|
|
233 | inline_speed |
|
|
234 | int |
|
|
235 | iouring_enter (EV_P_ ev_tstamp timeout) |
|
|
236 | { |
|
|
237 | int res; |
|
|
238 | |
|
|
239 | EV_RELEASE_CB; |
|
|
240 | |
|
|
241 | res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
|
|
242 | timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
|
|
243 | |
|
|
244 | assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit))); |
|
|
245 | |
|
|
246 | iouring_to_submit = 0; |
|
|
247 | |
|
|
248 | EV_ACQUIRE_CB; |
|
|
249 | |
|
|
250 | return res; |
|
|
251 | } |
|
|
252 | |
|
|
253 | /* TODO: can we move things around so we don't need this forward-reference? */ |
|
|
254 | static void |
|
|
255 | iouring_poll (EV_P_ ev_tstamp timeout); |
|
|
256 | |
233 | static |
257 | static |
234 | struct io_uring_sqe * |
258 | struct io_uring_sqe * |
235 | iouring_sqe_get (EV_P) |
259 | iouring_sqe_get (EV_P) |
236 | { |
260 | { |
|
|
261 | unsigned tail; |
|
|
262 | |
|
|
263 | for (;;) |
|
|
264 | { |
237 | unsigned tail = EV_SQ_VAR (tail); |
265 | tail = EV_SQ_VAR (tail); |
238 | |
266 | |
239 | if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) |
267 | if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))) |
|
|
268 | break; /* whats the problem, we have free sqes */ |
|
|
269 | |
|
|
270 | /* queue full, need to flush and possibly handle some events */ |
|
|
271 | |
|
|
272 | #if EV_FEATURE_CODE |
|
|
273 | /* first we ask the kernel nicely, most often this frees up some sqes */ |
|
|
274 | int res = iouring_enter (EV_A_ EV_TS_CONST (0.)); |
|
|
275 | |
|
|
276 | ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */ |
|
|
277 | |
|
|
278 | if (res >= 0) |
|
|
279 | continue; /* yes, it worked, try again */ |
|
|
280 | #endif |
|
|
281 | |
|
|
282 | /* some problem, possibly EBUSY - do the full poll and let it handle any issues */ |
|
|
283 | |
|
|
284 | iouring_poll (EV_A_ EV_TS_CONST (0.)); |
|
|
285 | /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */ |
240 | { |
286 | } |
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 | |
287 | |
246 | assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); |
288 | /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/ |
247 | |
289 | |
248 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
290 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
249 | } |
291 | } |
250 | |
292 | |
251 | inline_size |
293 | inline_size |
… | |
… | |
405 | /* Jens Axboe notified me that user_data is not what is documented, but is |
447 | /* 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 |
448 | * 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 |
449 | * 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 |
450 | * generation counter - if that fails, too bad, it will hopefully be removed |
409 | * at close time and then be ignored. */ |
451 | * at close time and then be ignored. */ |
410 | sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
452 | sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
|
|
453 | sqe->user_data = (uint64_t)-1; |
411 | iouring_sqe_submit (EV_A_ sqe); |
454 | iouring_sqe_submit (EV_A_ sqe); |
412 | |
455 | |
413 | /* increment generation counter to avoid handling old events */ |
456 | /* increment generation counter to avoid handling old events */ |
414 | ++anfds [fd].egen; |
457 | ++anfds [fd].egen; |
415 | } |
458 | } |
… | |
… | |
455 | iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe) |
498 | iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe) |
456 | { |
499 | { |
457 | int fd = cqe->user_data & 0xffffffffU; |
500 | int fd = cqe->user_data & 0xffffffffU; |
458 | uint32_t gen = cqe->user_data >> 32; |
501 | uint32_t gen = cqe->user_data >> 32; |
459 | int res = cqe->res; |
502 | int res = cqe->res; |
|
|
503 | |
|
|
504 | /* user_data -1 is a remove that we are not atm. interested in */ |
|
|
505 | if (cqe->user_data == (uint64_t)-1) |
|
|
506 | return; |
460 | |
507 | |
461 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
508 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
462 | |
509 | |
463 | /* documentation lies, of course. the result value is NOT like |
510 | /* documentation lies, of course. the result value is NOT like |
464 | * normal syscalls, but like linux raw syscalls, i.e. negative |
511 | * normal syscalls, but like linux raw syscalls, i.e. negative |
… | |
… | |
589 | |
636 | |
590 | static void |
637 | static void |
591 | iouring_poll (EV_P_ ev_tstamp timeout) |
638 | iouring_poll (EV_P_ ev_tstamp timeout) |
592 | { |
639 | { |
593 | /* if we have events, no need for extra syscalls, but we might have to queue events */ |
640 | /* if we have events, no need for extra syscalls, but we might have to queue events */ |
|
|
641 | /* we also clar the timeout if there are outstanding fdchanges */ |
|
|
642 | /* the latter should only happen if both the sq and cq are full, most likely */ |
|
|
643 | /* because we have a lot of event sources that immediately complete */ |
|
|
644 | /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */ |
594 | if (iouring_handle_cq (EV_A)) |
645 | if (iouring_handle_cq (EV_A) || fdchangecnt) |
595 | timeout = EV_TS_CONST (0.); |
646 | timeout = EV_TS_CONST (0.); |
596 | else |
647 | else |
597 | /* no events, so maybe wait for some */ |
648 | /* no events, so maybe wait for some */ |
598 | iouring_tfd_update (EV_A_ timeout); |
649 | iouring_tfd_update (EV_A_ timeout); |
599 | |
650 | |
600 | /* only enter the kernel if we have something to submit, or we need to wait */ |
651 | /* only enter the kernel if we have something to submit, or we need to wait */ |
601 | if (timeout || iouring_to_submit) |
652 | if (timeout || iouring_to_submit) |
602 | { |
653 | { |
603 | int res; |
654 | 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 | |
655 | |
613 | if (ecb_expect_false (res < 0)) |
656 | if (ecb_expect_false (res < 0)) |
614 | if (errno == EINTR) |
657 | if (errno == EINTR) |
615 | /* ignore */; |
658 | /* ignore */; |
|
|
659 | else if (errno == EBUSY) |
|
|
660 | /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */; |
616 | else |
661 | else |
617 | ev_syserr ("(libev) iouring setup"); |
662 | ev_syserr ("(libev) iouring setup"); |
618 | else |
663 | else |
619 | iouring_handle_cq (EV_A); |
664 | iouring_handle_cq (EV_A); |
620 | } |
665 | } |