From e31b82888efc4986d89f3609e9edbc7205642bc8 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Mon, 7 Jan 2019 21:35:15 -0700 Subject: [PATCH] Rename t/aio-ring to t/io_uring The new API is completely discoupled from the aio/libaio interface. Rename it while adopting the new API. Signed-off-by: Jens Axboe --- Makefile | 10 +-- os/io_uring.h | 99 ++++++++++++++++++++++++++++ t/{aio-ring.c => io_uring.c} | 121 +++++++++++------------------------ 3 files changed, 143 insertions(+), 87 deletions(-) create mode 100644 os/io_uring.h rename t/{aio-ring.c => io_uring.c} (81%) diff --git a/Makefile b/Makefile index f111ae6a..9492da6c 100644 --- a/Makefile +++ b/Makefile @@ -266,8 +266,8 @@ T_VS_PROGS = t/fio-verify-state T_PIPE_ASYNC_OBJS = t/read-to-pipe-async.o T_PIPE_ASYNC_PROGS = t/read-to-pipe-async -T_AIO_RING_OBJS = t/aio-ring.o -T_AIO_RING_PROGS = t/aio-ring +T_IOU_RING_OBJS = t/io_uring.o +T_IOU_RING_PROGS = t/io_uring T_MEMLOCK_OBJS = t/memlock.o T_MEMLOCK_PROGS = t/memlock @@ -287,7 +287,7 @@ T_OBJS += $(T_VS_OBJS) T_OBJS += $(T_PIPE_ASYNC_OBJS) T_OBJS += $(T_MEMLOCK_OBJS) T_OBJS += $(T_TT_OBJS) -T_OBJS += $(T_AIO_RING_OBJS) +T_OBJS += $(T_IOU_RING_OBJS) ifneq (,$(findstring CYGWIN,$(CONFIG_TARGET_OS))) T_DEDUPE_OBJS += os/windows/posix.o lib/hweight.o @@ -447,8 +447,8 @@ cairo_text_helpers.o: cairo_text_helpers.c cairo_text_helpers.h printing.o: printing.c printing.h $(QUIET_CC)$(CC) $(CFLAGS) $(GTK_CFLAGS) $(CPPFLAGS) -c $< -t/aio-ring: $(T_AIO_RING_OBJS) - $(QUIET_LINK)$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(T_AIO_RING_OBJS) $(LIBS) +t/io_uring: $(T_IOU_RING_OBJS) + $(QUIET_LINK)$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(T_IOU_RING_OBJS) $(LIBS) t/read-to-pipe-async: $(T_PIPE_ASYNC_OBJS) $(QUIET_LINK)$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(T_PIPE_ASYNC_OBJS) $(LIBS) diff --git a/os/io_uring.h b/os/io_uring.h new file mode 100644 index 00000000..61a37531 --- /dev/null +++ b/os/io_uring.h @@ -0,0 +1,99 @@ +#ifndef IO_URING_H +#define IO_URING_H + +/* + * IO submission data structure + */ +struct io_uring_iocb { + u8 opcode; + u8 flags; + u16 ioprio; + s32 fd; + u64 off; + union { + void *addr; + u64 __pad; + }; + u32 len; + union { + __kernel_rwf_t rw_flags; + u32 __resv; + }; +}; + +/* + * io_uring_setup() flags + */ +#define IORING_SETUP_IOPOLL (1 << 0) /* io_context is polled */ +#define IORING_SETUP_FIXEDBUFS (1 << 1) /* IO buffers are fixed */ +#define IORING_SETUP_SQTHREAD (1 << 2) /* Use SQ thread */ +#define IORING_SETUP_SQWQ (1 << 3) /* Use SQ workqueue */ +#define IORING_SETUP_SQPOLL (1 << 4) /* SQ thread polls */ + +#define IORING_OP_READ 1 +#define IORING_OP_WRITE 2 +#define IORING_OP_FSYNC 3 +#define IORING_OP_FDSYNC 4 +#define IORING_OP_READ_FIXED 5 +#define IORING_OP_WRITE_FIXED 6 + +/* + * IO completion data structure + */ +struct io_uring_event { + __u64 index; /* what iocb this event came from */ + s32 res; /* result code for this event */ + u32 flags; +}; + +#define IOEV_FLAG_CACHEHIT (1 << 0) /* IO did not hit media */ + +/* + * Magic offsets for the application to mmap the data it needs + */ +#define IORING_OFF_SQ_RING 0ULL +#define IORING_OFF_CQ_RING 0x8000000ULL +#define IORING_OFF_IOCB 0x10000000ULL + +/* + * Filled with the offset for mmap(2) + */ +struct io_sqring_offsets { + u32 head; + u32 tail; + u32 ring_mask; + u32 ring_entries; + u32 flags; + u32 dropped; + u32 array; + u32 resv[3]; +}; + +#define IORING_SQ_NEED_WAKEUP (1 << 0) /* needs io_uring_enter wakeup */ + +struct io_cqring_offsets { + u32 head; + u32 tail; + u32 ring_mask; + u32 ring_entries; + u32 overflow; + u32 events; + u32 resv[4]; +}; + +#define IORING_ENTER_GETEVENTS (1 << 0) + +/* + * Passed in for io_uring_setup(2). Copied back with updated info on success + */ +struct io_uring_params { + u32 sq_entries; + u32 cq_entries; + u32 flags; + u16 sq_thread_cpu; + u16 resv[9]; + struct io_sqring_offsets sq_off; + struct io_cqring_offsets cq_off; +}; + +#endif diff --git a/t/aio-ring.c b/t/io_uring.c similarity index 81% rename from t/aio-ring.c rename to t/io_uring.c index 1a4fe44b..83d723f9 100644 --- a/t/aio-ring.c +++ b/t/io_uring.c @@ -1,6 +1,3 @@ -/* - * gcc -D_GNU_SOURCE -Wall -O2 -o aio-ring aio-ring.c -lpthread -laio - */ #include #include #include @@ -15,69 +12,29 @@ #include #include #include +#include #include #include #include -#include #include #include #include #include "../arch/arch.h" -#define IOCTX_FLAG_SCQRING (1 << 0) /* Use SQ/CQ rings */ -#define IOCTX_FLAG_IOPOLL (1 << 1) -#define IOCTX_FLAG_FIXEDBUFS (1 << 2) -#define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */ -#define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */ -#define IOCTX_FLAG_SQPOLL (1 << 5) - -#define IOEV_RES2_CACHEHIT (1 << 0) - -#define barrier() __asm__ __volatile__("": : :"memory") - -#define min(a, b) ((a < b) ? (a) : (b)) - typedef uint64_t u64; typedef uint32_t u32; +typedef int32_t s32; typedef uint16_t u16; +typedef uint8_t u8; -#define IORING_OFF_SQ_RING 0ULL -#define IORING_OFF_CQ_RING 0x8000000ULL -#define IORING_OFF_IOCB 0x10000000ULL - -struct aio_sqring_offsets { - u32 head; - u32 tail; - u32 ring_mask; - u32 ring_entries; - u32 flags; - u32 dropped; - u32 array; - u32 resv[3]; -}; +#include "../os/io_uring.h" -struct aio_cqring_offsets { - u32 head; - u32 tail; - u32 ring_mask; - u32 ring_entries; - u32 overflow; - u32 events; - u32 resv[4]; -}; +#define barrier() __asm__ __volatile__("": : :"memory") -struct aio_uring_params { - u32 sq_entries; - u32 cq_entries; - u32 flags; - u16 sq_thread_cpu; - u16 resv[9]; - struct aio_sqring_offsets sq_off; - struct aio_cqring_offsets cq_off; -}; +#define min(a, b) ((a < b) ? (a) : (b)) -struct aio_sq_ring { +struct io_sq_ring { u32 *head; u32 *tail; u32 *ring_mask; @@ -85,16 +42,14 @@ struct aio_sq_ring { u32 *array; }; -struct aio_cq_ring { +struct io_cq_ring { u32 *head; u32 *tail; u32 *ring_mask; u32 *ring_entries; - struct io_event *events; + struct io_uring_event *events; }; -#define IORING_ENTER_GETEVENTS (1 << 0) - #define DEPTH 32 #define BATCH_SUBMIT 8 @@ -109,10 +64,10 @@ struct submitter { unsigned long max_blocks; int ring_fd; struct drand48_data rand; - struct aio_sq_ring sq_ring; - struct iocb *iocbs; + struct io_sq_ring sq_ring; + struct io_uring_iocb *iocbs; struct iovec iovecs[DEPTH]; - struct aio_cq_ring cq_ring; + struct io_cq_ring cq_ring; int inflight; unsigned long reaps; unsigned long done; @@ -132,7 +87,7 @@ static int sq_thread = 0; /* use kernel submission thread */ static int sq_thread_cpu = 0; /* pin above thread to this CPU */ static int io_uring_setup(unsigned entries, struct iovec *iovecs, - struct aio_uring_params *p) + struct io_uring_params *p) { return syscall(__NR_sys_io_uring_setup, entries, iovecs, p); } @@ -151,23 +106,25 @@ static int gettid(void) static void init_io(struct submitter *s, int fd, unsigned index) { - struct iocb *iocb = &s->iocbs[index]; + struct io_uring_iocb *iocb = &s->iocbs[index]; unsigned long offset; long r; lrand48_r(&s->rand, &r); offset = (r % (s->max_blocks - 1)) * BS; - iocb->aio_fildes = fd; - iocb->aio_lio_opcode = IO_CMD_PREAD; - iocb->u.c.buf = s->iovecs[index].iov_base; - iocb->u.c.nbytes = BS; - iocb->u.c.offset = offset; + iocb->opcode = IORING_OP_READ; + iocb->flags = 0; + iocb->ioprio = 0; + iocb->fd = fd; + iocb->off = offset; + iocb->addr = s->iovecs[index].iov_base; + iocb->len = BS; } static int prep_more_ios(struct submitter *s, int fd, int max_ios) { - struct aio_sq_ring *ring = &s->sq_ring; + struct io_sq_ring *ring = &s->sq_ring; u32 index, tail, next_tail, prepped = 0; next_tail = tail = *ring->tail; @@ -217,8 +174,8 @@ static int get_file_size(int fd, unsigned long *blocks) static int reap_events(struct submitter *s) { - struct aio_cq_ring *ring = &s->cq_ring; - struct io_event *ev; + struct io_cq_ring *ring = &s->cq_ring; + struct io_uring_event *ev; u32 head, reaped = 0; head = *ring->head; @@ -228,15 +185,15 @@ static int reap_events(struct submitter *s) break; ev = &ring->events[head & cq_ring_mask]; if (ev->res != BS) { - struct iocb *iocb = ev->obj; + struct io_uring_iocb *iocb = &s->iocbs[ev->index]; - printf("io: unexpected ret=%ld\n", ev->res); + printf("io: unexpected ret=%d\n", ev->res); printf("offset=%lu, size=%lu\n", - (unsigned long) iocb->u.c.offset, - (unsigned long) iocb->u.c.nbytes); + (unsigned long) iocb->off, + (unsigned long) iocb->len); return -1; } - if (ev->res2 & IOEV_RES2_CACHEHIT) + if (ev->flags & IOEV_FLAG_CACHEHIT) s->cachehit++; else s->cachemiss++; @@ -359,23 +316,22 @@ static void arm_sig_int(void) static int setup_ring(struct submitter *s) { - struct aio_sq_ring *sring = &s->sq_ring; - struct aio_cq_ring *cring = &s->cq_ring; - struct aio_uring_params p; + struct io_sq_ring *sring = &s->sq_ring; + struct io_cq_ring *cring = &s->cq_ring; + struct io_uring_params p; void *ptr; int fd; memset(&p, 0, sizeof(p)); - p.flags = IOCTX_FLAG_SCQRING; if (polled) - p.flags |= IOCTX_FLAG_IOPOLL; + p.flags |= IORING_SETUP_IOPOLL; if (fixedbufs) - p.flags |= IOCTX_FLAG_FIXEDBUFS; + p.flags |= IORING_SETUP_FIXEDBUFS; if (buffered) - p.flags |= IOCTX_FLAG_SQWQ; + p.flags |= IORING_SETUP_SQWQ; else if (sq_thread) { - p.flags |= IOCTX_FLAG_SQTHREAD; + p.flags |= IORING_SETUP_SQTHREAD; p.sq_thread_cpu = sq_thread_cpu; } @@ -400,12 +356,12 @@ static int setup_ring(struct submitter *s) sring->array = ptr + p.sq_off.array; sq_ring_mask = *sring->ring_mask; - s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb), + s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_IOCB); printf("iocbs ptr = 0x%p\n", s->iocbs); - ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_event), + ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_uring_event), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); printf("cq_ring ptr = 0x%p\n", ptr); @@ -501,5 +457,6 @@ int main(int argc, char *argv[]) } while (!finish); pthread_join(s->thread, &ret); + close(s->ring_fd); return 0; } -- 2.25.1