diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | src/include/liburing.h | 7 | ||||
-rw-r--r-- | src/queue.c | 23 | ||||
-rw-r--r-- | test/Makefile | 5 | ||||
-rw-r--r-- | test/cq-peek-batch.c | 96 | ||||
-rw-r--r-- | test/cq-ready.c | 90 |
6 files changed, 222 insertions, 2 deletions
@@ -20,6 +20,8 @@ /test/a4c0b3decb33-test /test/b19062a56726-test /test/cq-full +/test/cq-ready +/test/cq-peek-batch /test/eeed8b54e0df-test /test/fsync /test/io_uring_enter @@ -33,6 +35,7 @@ /test/send_recvmsg /test/sq-full /test/sq-space_left +/test/stdout /test/timeout config-host.h diff --git a/src/include/liburing.h b/src/include/liburing.h index cfce895..6367307 100644 --- a/src/include/liburing.h +++ b/src/include/liburing.h @@ -72,6 +72,8 @@ extern int io_uring_queue_mmap(int fd, struct io_uring_params *p, extern void io_uring_queue_exit(struct io_uring *ring); extern int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr); +unsigned io_uring_peek_batch_cqe(struct io_uring *ring, + struct io_uring_cqe **cqes, unsigned count); extern int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr); extern int io_uring_wait_cqes_timeout(struct io_uring *ring, @@ -248,6 +250,11 @@ static inline unsigned io_uring_sq_space_left(struct io_uring *ring) return *ring->sq.kring_entries - (ring->sq.sqe_tail - ring->sq.sqe_head); } +static inline unsigned io_uring_cq_ready(struct io_uring *ring) +{ + return io_uring_smp_load_acquire(ring->cq.ktail) - *ring->cq.khead; +} + #ifdef __cplusplus } #endif diff --git a/src/queue.c b/src/queue.c index 18040d5..470c3a2 100644 --- a/src/queue.c +++ b/src/queue.c @@ -53,6 +53,29 @@ int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr) } /* + * Fill in an array of IO completions up to count, if any are available. + * Returns the amount of IO completions filled. + */ +unsigned io_uring_peek_batch_cqe(struct io_uring *ring, struct io_uring_cqe **cqes, unsigned count) +{ + unsigned ready = io_uring_cq_ready(ring); + if (ready) { + count = count > ready ? ready : count; + unsigned head = *ring->cq.khead; + unsigned last = head + count; + unsigned mask = *ring->cq.kring_mask; + int i = 0; + for (;head!=last;head++,i++) { + cqes[i] = &ring->cq.cqes[head & mask]; + } + + return count; + } + + return 0; +} + +/* * Return an IO completion, waiting for it if necessary. Returns 0 with * cqe_ptr filled in on success, -errno on failure. */ diff --git a/test/Makefile b/test/Makefile index 54052a7..4710b30 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,7 +6,7 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register io_uring_enter nop sq-full cq-full 35fa71a030ca-test \ 917257daa0fe-test b19062a56726-test eeed8b54e0df-test link \ send_recvmsg a4c0b3decb33-test 500f9fbadef8-test timeout \ - sq-space_left stdout + sq-space_left stdout cq-ready cq-peek-batch include ../Makefile.quiet @@ -19,7 +19,8 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \ io_uring_register.c io_uring_enter.c nop.c sq-full.c cq-full.c \ 35fa71a030ca-test.c 917257daa0fe-test.c b19062a56726-test.c \ eeed8b54e0df-test.c link.c send_recvmsg.c a4c0b3decb33-test.c \ - 500f9fbadef8-test.c timeout.c sq-space_left.c stdout.c + 500f9fbadef8-test.c timeout.c sq-space_left.c stdout.c cq-ready.c\ + cq-peek-batch.c test_objs := $(patsubst %.c,%.ol,$(test_srcs)) diff --git a/test/cq-peek-batch.c b/test/cq-peek-batch.c new file mode 100644 index 0000000..e0a765e --- /dev/null +++ b/test/cq-peek-batch.c @@ -0,0 +1,96 @@ +/* + * Description: test CQ peek-batch + * + */ +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> + +#include "liburing.h" + +static int queue_n_nops(struct io_uring *ring, int n, int offset) +{ + struct io_uring_sqe *sqe; + int i, ret; + + for (i = 0; i < n; i++) { + sqe = io_uring_get_sqe(ring); + if (!sqe) { + printf("get sqe failed\n"); + goto err; + } + + io_uring_prep_nop(sqe); + sqe->user_data = i + offset; + } + + ret = io_uring_submit(ring); + if (ret < n) { + printf("Submitted only %d\n", ret); + goto err; + } else if (ret < 0) { + printf("sqe submit failed: %d\n", ret); + goto err; + } + + return 0; +err: + return 1; +} + +#define CHECK_BATCH(ring, got, cqes, count, expected) do {\ + got = io_uring_peek_batch_cqe((ring), cqes, count);\ + if (got != expected) {\ + printf("Got %d CQs, expected %d\n", got, expected);\ + goto err;\ + }\ +} while(0) + +int main(int argc, char *argv[]) +{ + struct io_uring_cqe *cqes[8]; + struct io_uring ring; + int ret, i; + unsigned got; + + ret = io_uring_queue_init(4, &ring, 0); + if (ret) { + printf("ring setup failed\n"); + return 1; + + } + + CHECK_BATCH(&ring, got, cqes, 4, 0); + if (queue_n_nops(&ring, 4, 0)) + goto err; + + CHECK_BATCH(&ring, got, cqes, 4, 4); + for (i=0;i<4;i++) { + if (i != cqes[i]->user_data) { + printf("Got user_data %lld, expected %d\n", cqes[i]->user_data, i); + goto err; + } + } + + if (queue_n_nops(&ring, 4, 4)) + goto err; + + io_uring_cq_advance(&ring, 4); + CHECK_BATCH(&ring, got, cqes, 4, 4); + for (i=0;i<4;i++) { + if (i + 4 != cqes[i]->user_data) { + printf("Got user_data %lld, expected %d\n", cqes[i]->user_data, i + 4); + goto err; + } + } + + io_uring_cq_advance(&ring, 8); + io_uring_queue_exit(&ring); + return 0; +err: + io_uring_queue_exit(&ring); + return 1; +} diff --git a/test/cq-ready.c b/test/cq-ready.c new file mode 100644 index 0000000..1885c0c --- /dev/null +++ b/test/cq-ready.c @@ -0,0 +1,90 @@ +/* + * Description: test CQ ready + * + */ +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> + +#include "liburing.h" + +static int queue_n_nops(struct io_uring *ring, int n) +{ + struct io_uring_sqe *sqe; + int i, ret; + + for (i = 0; i < n; i++) { + sqe = io_uring_get_sqe(ring); + if (!sqe) { + printf("get sqe failed\n"); + goto err; + } + + io_uring_prep_nop(sqe); + } + + ret = io_uring_submit(ring); + if (ret < n) { + printf("Submitted only %d\n", ret); + goto err; + } else if (ret < 0) { + printf("sqe submit failed: %d\n", ret); + goto err; + } + + return 0; +err: + return 1; +} + +#define CHECK_READY(ring, expected) do {\ + ready = io_uring_cq_ready((ring));\ + if (ready != expected) {\ + printf("Got %d CQs ready, expected %d\n", ready, expected);\ + goto err;\ + }\ +} while(0) + +int main(int argc, char *argv[]) +{ + struct io_uring ring; + int ret; + unsigned ready; + + ret = io_uring_queue_init(4, &ring, 0); + if (ret) { + printf("ring setup failed\n"); + return 1; + + } + + CHECK_READY(&ring, 0); + if (queue_n_nops(&ring, 4)) + goto err; + + CHECK_READY(&ring, 4); + io_uring_cq_advance(&ring, 4); + CHECK_READY(&ring, 0); + if (queue_n_nops(&ring, 4)) + goto err; + + CHECK_READY(&ring, 4); + + io_uring_cq_advance(&ring, 1); + CHECK_READY(&ring, 3); + + io_uring_cq_advance(&ring, 2); + CHECK_READY(&ring, 1); + + io_uring_cq_advance(&ring, 1); + CHECK_READY(&ring, 0); + + io_uring_queue_exit(&ring); + return 0; +err: + io_uring_queue_exit(&ring); + return 1; +} |