libev/ev_iouring.c

713 lines
21 KiB
C
Raw Normal View History

2019-07-02 06:07:54 +00:00
/*
* libev linux io_uring fd activity backend
*
2020-01-22 02:20:47 +00:00
* Copyright (c) 2019-2020 Marc Alexander Lehmann <libev@schmorp.de>
2019-07-02 06:07:54 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
/*
* general notes about linux io_uring:
*
* a) it's the best interface I have seen so far. on linux.
* b) best is not necessarily very good.
* c) it's better than the aio mess, doesn't suffer from the fork problems
* of linux aio or epoll and so on and so on. and you could do event stuff
* without any syscalls. what's not to like?
* d) ok, it's vastly more complex, but that's ok, really.
2019-12-28 07:57:48 +00:00
* e) why two mmaps instead of one? one would be more space-efficient,
* and I can't see what benefit two would have (other than being
2019-07-02 06:07:54 +00:00
* somehow resizable/relocatable, but that's apparently not possible).
2019-12-28 07:57:48 +00:00
* f) hmm, it's practically undebuggable (gdb can't access the memory, and
2019-12-22 15:05:31 +00:00
* the bizarre way structure offsets are communicated makes it hard to
2019-07-02 06:07:54 +00:00
* just print the ring buffer heads, even *iff* the memory were visible
* in gdb. but then, that's also ok, really.
* g) well, you cannot specify a timeout when waiting for events. no,
* seriously, the interface doesn't support a timeout. never seen _that_
* before. sure, you can use a timerfd, but that's another syscall
* you could have avoided. overall, this bizarre omission smells
* like a µ-optimisation by the io_uring author for his personal
* applications, to the detriment of everybody else who just wants
* an event loop. but, umm, ok, if that's all, it could be worse.
2019-12-28 07:58:51 +00:00
* (from what I gather from the author Jens Axboe, it simply didn't
2020-06-08 11:15:59 +00:00
* occur to him, and he made good on it by adding an unlimited number
2019-12-28 07:58:51 +00:00
* of timeouts later :).
2019-12-28 07:57:48 +00:00
* h) initially there was a hardcoded limit of 4096 outstanding events.
2019-12-28 07:58:51 +00:00
* later versions not only bump this to 32k, but also can handle
2019-12-28 07:57:48 +00:00
* an unlimited amount of events, so this only affects the batch size.
2019-07-02 06:07:54 +00:00
* i) unlike linux aio, you *can* register more then the limit
2019-12-28 07:57:48 +00:00
* of fd events. while early verisons of io_uring signalled an overflow
* and you ended up getting wet. 5.5+ does not do this anymore.
* j) but, oh my! it had exactly the same bugs as the linux aio backend,
* where some undocumented poll combinations just fail. fortunately,
* after finally reaching the author, he was more than willing to fix
* this probably in 5.6+.
2019-07-02 06:07:54 +00:00
* k) overall, the *API* itself is, I dare to say, not a total trainwreck.
2019-12-28 07:57:48 +00:00
* once the bugs ae fixed (probably in 5.6+), it will be without
* competition.
2019-07-02 06:07:54 +00:00
*/
2019-12-27 16:15:58 +00:00
/* TODO: use internal TIMEOUT */
/* TODO: take advantage of single mmap, NODROP etc. */
/* TODO: resize cq/sq size independently */
2019-07-02 06:07:54 +00:00
#include <sys/timerfd.h>
#include <sys/mman.h>
#include <poll.h>
2019-12-27 22:16:10 +00:00
#include <stdint.h>
2019-07-02 06:07:54 +00:00
#define IOURING_INIT_ENTRIES 32
/*****************************************************************************/
/* syscall wrapdadoop - this section has the raw api/abi definitions */
#include <linux/fs.h>
#include <linux/types.h>
/* mostly directly taken from the kernel or documentation */
struct io_uring_sqe
{
__u8 opcode;
__u8 flags;
__u16 ioprio;
__s32 fd;
2019-12-27 16:08:24 +00:00
union {
__u64 off;
__u64 addr2;
};
2019-07-02 06:07:54 +00:00
__u64 addr;
__u32 len;
union {
__kernel_rwf_t rw_flags;
__u32 fsync_flags;
__u16 poll_events;
__u32 sync_range_flags;
__u32 msg_flags;
2019-12-27 16:08:24 +00:00
__u32 timeout_flags;
__u32 accept_flags;
__u32 cancel_flags;
__u32 open_flags;
__u32 statx_flags;
2020-07-26 11:10:45 +00:00
__u32 fadvise_advice;
2019-07-02 06:07:54 +00:00
};
__u64 user_data;
union {
__u16 buf_index;
2020-07-26 11:10:45 +00:00
__u16 personality;
2019-07-02 06:07:54 +00:00
__u64 __pad2[3];
};
};
struct io_uring_cqe
{
__u64 user_data;
__s32 res;
__u32 flags;
};
struct io_sqring_offsets
{
__u32 head;
__u32 tail;
__u32 ring_mask;
__u32 ring_entries;
__u32 flags;
__u32 dropped;
__u32 array;
__u32 resv1;
__u64 resv2;
};
struct io_cqring_offsets
{
__u32 head;
__u32 tail;
__u32 ring_mask;
__u32 ring_entries;
__u32 overflow;
__u32 cqes;
__u64 resv[2];
};
struct io_uring_params
{
__u32 sq_entries;
__u32 cq_entries;
__u32 flags;
__u32 sq_thread_cpu;
__u32 sq_thread_idle;
2019-12-27 16:08:24 +00:00
__u32 features;
__u32 resv[4];
2019-07-02 06:07:54 +00:00
struct io_sqring_offsets sq_off;
struct io_cqring_offsets cq_off;
};
2020-07-26 11:10:45 +00:00
#define IORING_FEAT_SINGLE_MMAP 0x00000001
#define IORING_FEAT_NODROP 0x00000002
#define IORING_FEAT_SUBMIT_STABLE 0x00000004
2019-12-27 22:16:10 +00:00
#define IORING_SETUP_CQSIZE 0x00000008
2020-07-26 11:10:45 +00:00
#define IORING_SETUP_CLAMP 0x00000010
2019-12-27 22:16:10 +00:00
#define IORING_OP_POLL_ADD 6
#define IORING_OP_POLL_REMOVE 7
#define IORING_OP_TIMEOUT 11
#define IORING_OP_TIMEOUT_REMOVE 12
2020-07-26 11:10:45 +00:00
#define IORING_REGISTER_EVENTFD 4
#define IORING_REGISTER_EVENTFD_ASYNC 7
#define IORING_REGISTER_PROBE 8
#define IO_URING_OP_SUPPORTED 1
struct io_uring_probe_op {
__u8 op;
__u8 resv;
__u16 flags;
__u32 resv2;
};
struct io_uring_probe
{
__u8 last_op;
__u8 ops_len;
__u16 resv;
__u32 resv2[3];
struct io_uring_probe_op ops[0];
};
2019-12-27 22:16:10 +00:00
/* relative or absolute, reference clock is CLOCK_MONOTONIC */
struct iouring_kernel_timespec
{
int64_t tv_sec;
long long tv_nsec;
};
#define IORING_TIMEOUT_ABS 0x00000001
2019-07-02 06:07:54 +00:00
#define IORING_ENTER_GETEVENTS 0x01
#define IORING_OFF_SQ_RING 0x00000000ULL
#define IORING_OFF_SQES 0x10000000ULL
2019-12-27 22:16:10 +00:00
#define IORING_FEAT_SINGLE_MMAP 0x00000001
#define IORING_FEAT_NODROP 0x00000002
#define IORING_FEAT_SUBMIT_STABLE 0x00000004
2019-12-27 16:08:24 +00:00
2019-07-02 06:07:54 +00:00
inline_size
int
evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
{
return ev_syscall2 (SYS_io_uring_setup, entries, params);
}
inline_size
int
evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz)
{
return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz);
}
2020-07-26 11:10:45 +00:00
inline_size
int
evsys_io_uring_register (unsigned int fd, unsigned int opcode, void *arg, unsigned int nr_args)
{
return ev_syscall4 (SYS_io_uring_register, fd, opcode, arg, nr_args);
}
2019-07-02 06:07:54 +00:00
/*****************************************************************************/
2020-07-26 11:10:45 +00:00
/* actual backend implementation */
2019-07-02 06:07:54 +00:00
/* we hope that volatile will make the compiler access this variables only once */
2020-07-26 11:10:45 +00:00
#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_sq_ ## name)
#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_cq_ ## name)
2019-07-02 06:07:54 +00:00
/* the index array */
2020-07-26 11:10:45 +00:00
#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))
2019-07-02 06:07:54 +00:00
/* the submit/completion queue entries */
#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
2020-07-26 11:10:45 +00:00
#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes))
2019-07-02 06:07:54 +00:00
2019-12-28 07:37:07 +00:00
inline_speed
int
2019-12-28 05:20:17 +00:00
iouring_enter (EV_P_ ev_tstamp timeout)
{
int res;
EV_RELEASE_CB;
res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
iouring_to_submit = 0;
EV_ACQUIRE_CB;
return res;
}
2019-12-28 07:37:07 +00:00
/* TODO: can we move things around so we don't need this forward-reference? */
static void
iouring_poll (EV_P_ ev_tstamp timeout);
2019-07-02 06:07:54 +00:00
static
struct io_uring_sqe *
iouring_sqe_get (EV_P)
{
2019-12-28 07:37:07 +00:00
unsigned tail;
for (;;)
2019-07-02 06:07:54 +00:00
{
2019-12-28 07:37:07 +00:00
tail = EV_SQ_VAR (tail);
2019-12-28 05:20:17 +00:00
2019-12-28 07:37:07 +00:00
if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
break; /* whats the problem, we have free sqes */
/* queue full, need to flush and possibly handle some events */
#if EV_FEATURE_CODE
/* first we ask the kernel nicely, most often this frees up some sqes */
2019-12-28 05:20:17 +00:00
int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
2019-12-28 07:37:07 +00:00
ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
2019-12-28 05:20:17 +00:00
2019-12-28 07:37:07 +00:00
if (res >= 0)
continue; /* yes, it worked, try again */
#endif
/* some problem, possibly EBUSY - do the full poll and let it handle any issues */
iouring_poll (EV_A_ EV_TS_CONST (0.));
/* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
2019-07-02 06:07:54 +00:00
}
2019-12-28 05:20:17 +00:00
/*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
2019-07-02 06:07:54 +00:00
return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
}
inline_size
2021-05-09 18:41:06 +00:00
void
2019-07-02 06:07:54 +00:00
iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
{
unsigned idx = sqe - EV_SQES;
EV_SQ_ARRAY [idx] = idx;
ECB_MEMORY_FENCE_RELEASE;
++EV_SQ_VAR (tail);
/*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */
++iouring_to_submit;
}
/*****************************************************************************/
/* when the timerfd expires we simply note the fact,
* as the purpose of the timerfd is to wake us up, nothing else.
* the next iteration should re-set it.
*/
static void
iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
{
iouring_tfd_to = EV_TSTAMP_HUGE;
}
/* called for full and partial cleanup */
ecb_cold
static void
2019-07-02 06:07:54 +00:00
iouring_internal_destroy (EV_P)
{
close (iouring_tfd);
close (iouring_fd);
2020-07-26 11:10:45 +00:00
if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);
2019-07-02 06:07:54 +00:00
2019-12-27 16:08:24 +00:00
if (ev_is_active (&iouring_tfd_w))
{
ev_ref (EV_A);
ev_io_stop (EV_A_ &iouring_tfd_w);
}
2019-07-02 06:07:54 +00:00
}
ecb_cold
static int
iouring_internal_init (EV_P)
{
struct io_uring_params params = { 0 };
2020-07-26 11:10:45 +00:00
uint32_t sq_size, cq_size;
params.flags = IORING_SETUP_CLAMP;
2019-07-02 06:07:54 +00:00
iouring_to_submit = 0;
2020-07-26 11:10:45 +00:00
iouring_tfd = -1;
iouring_ring = MAP_FAILED;
iouring_sqes = MAP_FAILED;
2019-07-02 06:07:54 +00:00
2019-12-27 22:16:10 +00:00
if (!have_monotonic) /* cannot really happen, but what if11 */
return -1;
2020-07-26 11:10:45 +00:00
iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
if (iouring_fd < 0)
return -1;
2019-12-27 21:56:29 +00:00
2020-07-26 11:10:45 +00:00
if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
return -1; /* we require the above features */
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
/* TODO: remember somehow whether our queue size has been clamped */
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);
2019-07-02 06:07:54 +00:00
2020-07-26 11:10:45 +00:00
if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
2019-07-02 06:07:54 +00:00
return -1;
iouring_sq_head = params.sq_off.head;
iouring_sq_tail = params.sq_off.tail;
iouring_sq_ring_mask = params.sq_off.ring_mask;
iouring_sq_ring_entries = params.sq_off.ring_entries;
iouring_sq_flags = params.sq_off.flags;
iouring_sq_dropped = params.sq_off.dropped;
iouring_sq_array = params.sq_off.array;
iouring_cq_head = params.cq_off.head;
iouring_cq_tail = params.cq_off.tail;
iouring_cq_ring_mask = params.cq_off.ring_mask;
iouring_cq_ring_entries = params.cq_off.ring_entries;
iouring_cq_overflow = params.cq_off.overflow;
iouring_cq_cqes = params.cq_off.cqes;
2020-07-26 11:10:45 +00:00
iouring_tfd_to = EV_TSTAMP_HUGE;
2019-07-02 06:07:54 +00:00
iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);
if (iouring_tfd < 0)
2020-07-26 11:10:45 +00:00
return -1;
2019-07-02 06:07:54 +00:00
return 0;
}
ecb_cold
static void
iouring_fork (EV_P)
{
iouring_internal_destroy (EV_A);
while (iouring_internal_init (EV_A) < 0)
ev_syserr ("(libev) io_uring_setup");
2019-12-27 16:08:24 +00:00
fd_rearm_all (EV_A);
2019-07-02 06:07:54 +00:00
ev_io_stop (EV_A_ &iouring_tfd_w);
ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
ev_io_start (EV_A_ &iouring_tfd_w);
}
/*****************************************************************************/
static void
iouring_modify (EV_P_ int fd, int oev, int nev)
{
if (oev)
{
/* we assume the sqe's are all "properly" initialised */
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
sqe->opcode = IORING_OP_POLL_REMOVE;
sqe->fd = fd;
2019-12-28 03:29:50 +00:00
/* Jens Axboe notified me that user_data is not what is documented, but is
* some kind of unique ID that has to match, otherwise the request cannot
* be removed. Since we don't *really* have that, we pass in the old
* generation counter - if that fails, too bad, it will hopefully be removed
* at close time and then be ignored. */
2019-12-28 05:53:48 +00:00
sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
sqe->user_data = (uint64_t)-1;
2019-07-02 06:07:54 +00:00
iouring_sqe_submit (EV_A_ sqe);
2019-07-09 00:04:35 +00:00
/* increment generation counter to avoid handling old events */
++anfds [fd].egen;
}
2019-07-02 06:07:54 +00:00
if (nev)
{
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
sqe->opcode = IORING_OP_POLL_ADD;
sqe->fd = fd;
2019-12-28 08:11:20 +00:00
sqe->addr = 0;
2019-07-09 00:04:35 +00:00
sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
2019-07-02 06:07:54 +00:00
sqe->poll_events =
(nev & EV_READ ? POLLIN : 0)
| (nev & EV_WRITE ? POLLOUT : 0);
iouring_sqe_submit (EV_A_ sqe);
}
}
inline_size
void
iouring_tfd_update (EV_P_ ev_tstamp timeout)
{
ev_tstamp tfd_to = mn_now + timeout;
/* we assume there will be many iterations per timer change, so
* we only re-set the timerfd when we have to because its expiry
* is too late.
*/
if (ecb_expect_false (tfd_to < iouring_tfd_to))
{
struct itimerspec its;
iouring_tfd_to = tfd_to;
EV_TS_SET (its.it_interval, 0.);
EV_TS_SET (its.it_value, tfd_to);
if (timerfd_settime (iouring_tfd, TFD_TIMER_ABSTIME, &its, 0) < 0)
assert (("libev: iouring timerfd_settime failed", 0));
}
}
inline_size
void
iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe)
{
int fd = cqe->user_data & 0xffffffffU;
uint32_t gen = cqe->user_data >> 32;
int res = cqe->res;
2019-12-28 05:53:48 +00:00
/* user_data -1 is a remove that we are not atm. interested in */
if (cqe->user_data == (uint64_t)-1)
return;
2019-07-02 06:07:54 +00:00
assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
/* documentation lies, of course. the result value is NOT like
* normal syscalls, but like linux raw syscalls, i.e. negative
* error numbers. fortunate, as otherwise there would be no way
* to get error codes at all. still, why not document this?
*/
/* ignore event if generation doesn't match */
2019-12-28 03:29:50 +00:00
/* other than skipping removal events, */
2019-07-02 06:07:54 +00:00
/* this should actually be very rare */
2019-07-09 00:04:35 +00:00
if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
2019-07-02 06:07:54 +00:00
return;
if (ecb_expect_false (res < 0))
{
2019-12-27 21:56:29 +00:00
/*TODO: EINVAL handling (was something failed with this fd)*/
2019-07-02 06:07:54 +00:00
2019-12-27 16:08:24 +00:00
if (res == -EBADF)
2019-07-02 06:07:54 +00:00
{
assert (("libev: event loop rejected bad fd", res != -EBADF));
fd_kill (EV_A_ fd);
}
else
{
errno = -res;
ev_syserr ("(libev) IORING_OP_POLL_ADD");
}
return;
}
/* feed events, we do not expect or handle POLLNVAL */
fd_event (
EV_A_
fd,
(res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
| (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
);
/* io_uring is oneshot, so we need to re-arm the fd next iteration */
/* this also means we usually have to do at least one syscall per iteration */
anfds [fd].events = 0;
fd_change (EV_A_ fd, EV_ANFD_REIFY);
}
/* called when the event queue overflows */
ecb_cold
static void
iouring_overflow (EV_P)
{
/* we have two options, resize the queue (by tearing down
* everything and recreating it, or living with it
* and polling.
2019-12-27 21:17:11 +00:00
* we implement this by resizing the queue, and, if that fails,
2019-07-02 06:07:54 +00:00
* we just recreate the state on every failure, which
* kind of is a very inefficient poll.
* one danger is, due to the bios toward lower fds,
* we will only really get events for those, so
* maybe we need a poll() fallback, after all.
*/
/*EV_CQ_VAR (overflow) = 0;*/ /* need to do this if we keep the state and poll manually */
fd_rearm_all (EV_A);
/* we double the size until we hit the hard-to-probe maximum */
if (!iouring_max_entries)
{
iouring_entries <<= 1;
iouring_fork (EV_A);
}
else
{
/* we hit the kernel limit, we should fall back to something else.
* we can either poll() a few times and hope for the best,
* poll always, or switch to epoll.
2019-12-27 21:17:11 +00:00
* TODO: is this necessary with newer kernels?
2019-07-02 06:07:54 +00:00
*/
iouring_internal_destroy (EV_A);
2019-12-27 21:17:11 +00:00
/* this should make it so that on return, we don't call any uring functions */
2019-07-02 06:07:54 +00:00
iouring_to_submit = 0;
for (;;)
{
backend = epoll_init (EV_A_ 0);
if (backend)
break;
ev_syserr ("(libev) iouring switch to epoll");
}
}
}
/* handle any events in the completion queue, return true if there were any */
static int
iouring_handle_cq (EV_P)
{
unsigned head, tail, mask;
head = EV_CQ_VAR (head);
ECB_MEMORY_FENCE_ACQUIRE;
tail = EV_CQ_VAR (tail);
if (head == tail)
return 0;
/* it can only overflow if we have events, yes, yes? */
if (ecb_expect_false (EV_CQ_VAR (overflow)))
{
iouring_overflow (EV_A);
return 1;
}
mask = EV_CQ_VAR (ring_mask);
do
iouring_process_cqe (EV_A_ &EV_CQES [head++ & mask]);
while (head != tail);
EV_CQ_VAR (head) = head;
ECB_MEMORY_FENCE_RELEASE;
return 1;
}
static void
iouring_poll (EV_P_ ev_tstamp timeout)
{
/* if we have events, no need for extra syscalls, but we might have to queue events */
2019-12-28 07:37:07 +00:00
/* we also clar the timeout if there are outstanding fdchanges */
/* the latter should only happen if both the sq and cq are full, most likely */
/* because we have a lot of event sources that immediately complete */
/* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
if (iouring_handle_cq (EV_A) || fdchangecnt)
2019-08-17 05:30:16 +00:00
timeout = EV_TS_CONST (0.);
2019-07-02 06:07:54 +00:00
else
/* no events, so maybe wait for some */
iouring_tfd_update (EV_A_ timeout);
2019-12-20 05:20:23 +00:00
/* only enter the kernel if we have something to submit, or we need to wait */
2019-07-02 06:07:54 +00:00
if (timeout || iouring_to_submit)
{
2019-12-28 05:20:17 +00:00
int res = iouring_enter (EV_A_ timeout);
2019-07-02 06:07:54 +00:00
if (ecb_expect_false (res < 0))
if (errno == EINTR)
/* ignore */;
2019-12-28 05:20:17 +00:00
else if (errno == EBUSY)
/* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
2019-07-02 06:07:54 +00:00
else
ev_syserr ("(libev) iouring setup");
else
iouring_handle_cq (EV_A);
}
}
inline_size
int
iouring_init (EV_P_ int flags)
{
iouring_entries = IOURING_INIT_ENTRIES;
iouring_max_entries = 0;
if (iouring_internal_init (EV_A) < 0)
{
iouring_internal_destroy (EV_A);
return 0;
}
2019-12-20 05:20:23 +00:00
ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
2019-12-27 16:08:24 +00:00
ev_set_priority (&iouring_tfd_w, EV_MINPRI);
2019-07-02 06:07:54 +00:00
ev_io_start (EV_A_ &iouring_tfd_w);
ev_unref (EV_A); /* watcher should not keep loop alive */
backend_modify = iouring_modify;
backend_poll = iouring_poll;
return EVBACKEND_IOURING;
}
inline_size
void
iouring_destroy (EV_P)
{
iouring_internal_destroy (EV_A);
}