Browse Source

§

master
Marc Alexander Lehmann 2 weeks ago
parent
commit
51713929eb
4 changed files with 85 additions and 71 deletions
  1. +10
    -10
      ev.3
  2. +69
    -49
      ev_iouring.c
  3. +2
    -4
      ev_vars.h
  4. +4
    -8
      ev_wrap.h

+ 10
- 10
ev.3 View File

@@ -133,7 +133,7 @@
.\" ========================================================================
.\"
.IX Title "LIBEV 3"
.TH LIBEV 3 "2020-04-19" "libev-4.33" "libev - high performance full featured event loop"
.TH LIBEV 3 "2020-07-12" "libev-4.33" "libev - high performance full featured event loop"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@@ -4004,10 +4004,10 @@ First, you need to associate some data with the event loop:
.PP
.Vb 6
\& typedef struct {
\& mutex_t lock; /* global loop lock */
\& pthread_mutex_t lock; /* global loop lock */
\& pthread_t tid;
\& pthread_cond_t invoke_cv;
\& ev_async async_w;
\& thread_t tid;
\& cond_t invoke_cv;
\& } userdata;
\&
\& void prepare_loop (EV_P)
@@ -4015,19 +4015,19 @@ First, you need to associate some data with the event loop:
\& // for simplicity, we use a static userdata struct.
\& static userdata u;
\&
\& ev_async_init (&u\->async_w, async_cb);
\& ev_async_start (EV_A_ &u\->async_w);
\& ev_async_init (&u.async_w, async_cb);
\& ev_async_start (EV_A_ &u.async_w);
\&
\& pthread_mutex_init (&u\->lock, 0);
\& pthread_cond_init (&u\->invoke_cv, 0);
\& pthread_mutex_init (&u.lock, 0);
\& pthread_cond_init (&u.invoke_cv, 0);
\&
\& // now associate this with the loop
\& ev_set_userdata (EV_A_ u);
\& ev_set_userdata (EV_A_ &u);
\& ev_set_invoke_pending_cb (EV_A_ l_invoke);
\& ev_set_loop_release_cb (EV_A_ l_release, l_acquire);
\&
\& // then create the thread running ev_run
\& pthread_create (&u\->tid, 0, l_run, EV_A);
\& pthread_create (&u.tid, 0, l_run, EV_A);
\& }
.Ve
.PP


+ 69
- 49
ev_iouring.c View File

@@ -120,10 +120,12 @@ struct io_uring_sqe
__u32 cancel_flags;
__u32 open_flags;
__u32 statx_flags;
__u32 fadvise_advice;
};
__u64 user_data;
union {
__u16 buf_index;
__u16 personality;
__u64 __pad2[3];
};
};
@@ -172,13 +174,40 @@ struct io_uring_params
struct io_cqring_offsets cq_off;
};

#define IORING_FEAT_SINGLE_MMAP 0x00000001
#define IORING_FEAT_NODROP 0x00000002
#define IORING_FEAT_SUBMIT_STABLE 0x00000004

#define IORING_SETUP_CQSIZE 0x00000008
#define IORING_SETUP_CLAMP 0x00000010

#define IORING_OP_POLL_ADD 6
#define IORING_OP_POLL_REMOVE 7
#define IORING_OP_TIMEOUT 11
#define IORING_OP_TIMEOUT_REMOVE 12

#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];
};

/* relative or absolute, reference clock is CLOCK_MONOTONIC */
struct iouring_kernel_timespec
{
@@ -191,7 +220,6 @@ struct iouring_kernel_timespec
#define IORING_ENTER_GETEVENTS 0x01

#define IORING_OFF_SQ_RING 0x00000000ULL
#define IORING_OFF_CQ_RING 0x08000000ULL
#define IORING_OFF_SQES 0x10000000ULL

#define IORING_FEAT_SINGLE_MMAP 0x00000001
@@ -212,19 +240,26 @@ evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigne
return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz);
}

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);
}

/*****************************************************************************/
/* actual backed implementation */
/* actual backend implementation */

/* we hope that volatile will make the compiler access this variables only once */
#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name)
#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_cq_ring + iouring_cq_ ## name)
#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)

/* the index array */
#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array))
#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))

/* the submit/completion queue entries */
#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes))
#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes))

inline_speed
int
@@ -292,6 +327,8 @@ iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
{
unsigned idx = sqe - EV_SQES;

printf ("submit idx %d, op %d, fd %d, us5r %p, poll %d\n", idx, sqe->opcode, sqe->fd, sqe->user_data, sqe->poll_events);

EV_SQ_ARRAY [idx] = idx;
ECB_MEMORY_FENCE_RELEASE;
++EV_SQ_VAR (tail);
@@ -319,9 +356,8 @@ iouring_internal_destroy (EV_P)
close (iouring_tfd);
close (iouring_fd);

if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size);
if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size);
if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size );
if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);

if (ev_is_active (&iouring_tfd_w))
{
@@ -335,57 +371,41 @@ static int
iouring_internal_init (EV_P)
{
struct io_uring_params params = { 0 };
uint32_t sq_size, cq_size;

params.flags = IORING_SETUP_CLAMP;

iouring_to_submit = 0;

iouring_tfd = -1;
iouring_sq_ring = MAP_FAILED;
iouring_cq_ring = MAP_FAILED;
iouring_sqes = MAP_FAILED;
iouring_tfd = -1;
iouring_ring = MAP_FAILED;
iouring_sqes = MAP_FAILED;

if (!have_monotonic) /* cannot really happen, but what if11 */
return -1;

for (;;)
{
iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
iouring_fd = evsys_io_uring_setup (iouring_entries, &params);

if (iouring_fd >= 0)
break; /* yippie */
if (iouring_fd < 0)
return -1;

if (errno != EINVAL)
return -1; /* we failed */
if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
return -1; /* we require the above features */

#if TODO
if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
return -1; /* we require the above features */
#endif
/* TODO: remember somehow whether our queue size has been clamped */

/* EINVAL: lots of possible reasons, but maybe
* it is because we hit the unqueryable hardcoded size limit
*/
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);

/* we hit the limit already, give up */
if (iouring_max_entries)
return -1;
iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);

/* first time we hit EINVAL? assume we hit the limit, so go back and retry */
iouring_entries >>= 1;
iouring_max_entries = iouring_entries;
}
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);

iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);

iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING);
iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);

if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
return -1;

iouring_sq_head = params.sq_off.head;
@@ -403,12 +423,12 @@ iouring_internal_init (EV_P)
iouring_cq_overflow = params.cq_off.overflow;
iouring_cq_cqes = params.cq_off.cqes;

iouring_tfd_to = EV_TSTAMP_HUGE;

iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);

if (iouring_tfd < 0)
return iouring_tfd;

iouring_tfd_to = EV_TSTAMP_HUGE;
return -1;

return 0;
}


+ 2
- 4
ev_vars.h View File

@@ -123,11 +123,9 @@ VARx(int, iouring_fd)
VARx(unsigned, iouring_to_submit);
VARx(int, iouring_entries)
VARx(int, iouring_max_entries)
VARx(void *, iouring_sq_ring)
VARx(void *, iouring_cq_ring)
VARx(void *, iouring_ring)
VARx(void *, iouring_sqes)
VARx(uint32_t, iouring_sq_ring_size)
VARx(uint32_t, iouring_cq_ring_size)
VARx(uint32_t, iouring_ring_size)
VARx(uint32_t, iouring_sqes_size)
VARx(uint32_t, iouring_sq_head)
VARx(uint32_t, iouring_sq_tail)


+ 4
- 8
ev_wrap.h View File

@@ -47,22 +47,20 @@
#define iouring_cq_cqes ((loop)->iouring_cq_cqes)
#define iouring_cq_head ((loop)->iouring_cq_head)
#define iouring_cq_overflow ((loop)->iouring_cq_overflow)
#define iouring_cq_ring ((loop)->iouring_cq_ring)
#define iouring_cq_ring_entries ((loop)->iouring_cq_ring_entries)
#define iouring_cq_ring_mask ((loop)->iouring_cq_ring_mask)
#define iouring_cq_ring_size ((loop)->iouring_cq_ring_size)
#define iouring_cq_tail ((loop)->iouring_cq_tail)
#define iouring_entries ((loop)->iouring_entries)
#define iouring_fd ((loop)->iouring_fd)
#define iouring_max_entries ((loop)->iouring_max_entries)
#define iouring_ring ((loop)->iouring_ring)
#define iouring_ring_size ((loop)->iouring_ring_size)
#define iouring_sq_array ((loop)->iouring_sq_array)
#define iouring_sq_dropped ((loop)->iouring_sq_dropped)
#define iouring_sq_flags ((loop)->iouring_sq_flags)
#define iouring_sq_head ((loop)->iouring_sq_head)
#define iouring_sq_ring ((loop)->iouring_sq_ring)
#define iouring_sq_ring_entries ((loop)->iouring_sq_ring_entries)
#define iouring_sq_ring_mask ((loop)->iouring_sq_ring_mask)
#define iouring_sq_ring_size ((loop)->iouring_sq_ring_size)
#define iouring_sq_tail ((loop)->iouring_sq_tail)
#define iouring_sqes ((loop)->iouring_sqes)
#define iouring_sqes_size ((loop)->iouring_sqes_size)
@@ -182,22 +180,20 @@
#undef iouring_cq_cqes
#undef iouring_cq_head
#undef iouring_cq_overflow
#undef iouring_cq_ring
#undef iouring_cq_ring_entries
#undef iouring_cq_ring_mask
#undef iouring_cq_ring_size
#undef iouring_cq_tail
#undef iouring_entries
#undef iouring_fd
#undef iouring_max_entries
#undef iouring_ring
#undef iouring_ring_size
#undef iouring_sq_array
#undef iouring_sq_dropped
#undef iouring_sq_flags
#undef iouring_sq_head
#undef iouring_sq_ring
#undef iouring_sq_ring_entries
#undef iouring_sq_ring_mask
#undef iouring_sq_ring_size
#undef iouring_sq_tail
#undef iouring_sqes
#undef iouring_sqes_size


Loading…
Cancel
Save