diff options
author | Jens Axboe <axboe@kernel.dk> | 2019-11-06 11:34:18 -0700 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2019-11-06 15:15:38 -0700 |
commit | 570d79ab6485cb94aad931553d95bab7f8d19d11 (patch) | |
tree | 3df6ffcc58ccd7bcaa2993d8f31f07ff730005d6 | |
parent | ba97eaf8f9ef2030d581be10df9d875dbfadc91d (diff) | |
download | liburing-570d79ab6485cb94aad931553d95bab7f8d19d11.tar.gz liburing-570d79ab6485cb94aad931553d95bab7f8d19d11.tar.bz2 |
Add test case for IORING_SETUP_CQ_NODROPcqring-nodrop
Tests overflows without CQ_NODROP, and correct behavior on a ring
setup with CQ_NODROP. The latter backlogs events that otherwise
would have been dropped, and returns -EBUSY to an application trying
to submit new IO with a backlog pending.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r-- | src/include/liburing/io_uring.h | 1 | ||||
-rw-r--r-- | test/Makefile | 5 | ||||
-rw-r--r-- | test/cq-overflow.c | 198 |
3 files changed, 202 insertions, 2 deletions
diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h index 6877cf8..7ae91dc 100644 --- a/src/include/liburing/io_uring.h +++ b/src/include/liburing/io_uring.h @@ -56,6 +56,7 @@ struct io_uring_sqe { #define IORING_SETUP_SQPOLL (1U << 1) /* SQ poll thread */ #define IORING_SETUP_SQ_AFF (1U << 2) /* sq_thread_cpu is valid */ #define IORING_SETUP_CQSIZE (1U << 3) /* app defines CQ size */ +#define IORING_SETUP_CQ_NODROP (1U << 4) /* no CQ drops */ #define IORING_OP_NOP 0 #define IORING_OP_READV 1 diff --git a/test/Makefile b/test/Makefile index 25cf4d9..d99bb15 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,7 +8,8 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register send_recvmsg a4c0b3decb33-test 500f9fbadef8-test timeout \ sq-space_left stdout cq-ready cq-peek-batch file-register \ cq-size 8a9973408177-test a0908ae19763-test 232c93d07b74-test \ - socket-rw accept timeout-overflow defer read-write io-cancel + socket-rw accept timeout-overflow defer read-write io-cancel \ + cq-overflow include ../Makefile.quiet @@ -24,7 +25,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \ 500f9fbadef8-test.c timeout.c sq-space_left.c stdout.c cq-ready.c\ cq-peek-batch.c file-register.c cq-size.c 8a9973408177-test.c \ a0908ae19763-test.c 232c93d07b74-test.c socket-rw.c accept.c \ - timeout-overflow.c defer.c read-write.c io-cancel.c + timeout-overflow.c defer.c read-write.c io-cancel.c cq-overflow.c test_objs := $(patsubst %.c,%.ol,$(test_srcs)) diff --git a/test/cq-overflow.c b/test/cq-overflow.c new file mode 100644 index 0000000..368e328 --- /dev/null +++ b/test/cq-overflow.c @@ -0,0 +1,198 @@ +/* + * Description: run various CQ ring overflow tests + * + */ +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> + +#include "liburing.h" + +static int reap_events(struct io_uring *ring, unsigned nr_events, int do_wait) +{ + struct io_uring_cqe *cqe; + int i, ret = 0, seq = 0; + + for (i = 0; i < nr_events; i++) { + if (do_wait) + ret = io_uring_wait_cqe(ring, &cqe); + else + ret = io_uring_peek_cqe(ring, &cqe); + if (ret) { + if (ret != -EAGAIN) + fprintf(stderr, "cqe peek failed: %d\n", ret); + break; + } + if (cqe->user_data != seq) { + fprintf(stderr, "cqe sequence out-of-order\n"); + fprintf(stderr, "got %d, wanted %d\n", (int) cqe->user_data, + seq); + return -EINVAL; + } + seq++; + io_uring_cqe_seen(ring, cqe); + } + + return i ? i : ret; +} + +/* + * Setup ring with CQ_NODROP and check we get -EBUSY on trying to submit new IO + * on an overflown ring, and that we get all the events (even overflows) when + * we finally reap them. + */ +static int test_overflow_nodrop(void) +{ + struct __kernel_timespec ts; + struct io_uring_sqe *sqe; + struct io_uring ring; + unsigned pending; + int ret, i, j; + + ret = io_uring_queue_init(4, &ring, IORING_SETUP_CQ_NODROP); + if (ret) { + if (ret == -EINVAL) { + fprintf(stdout, "CQ_NODROP not supported, skipped\n"); + return 0; + } + fprintf(stderr, "io_uring_queue_init failed %d\n", ret); + return 1; + } + + ts.tv_sec = 0; + ts.tv_nsec = 10000000; + + /* submit 4x4 SQEs, should overflow the ring by 8 */ + pending = 0; + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + sqe = io_uring_get_sqe(&ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + io_uring_prep_timeout(sqe, &ts, -1U, 0); + sqe->user_data = (i * 4) + j; + } + + ret = io_uring_submit(&ring); + if (ret != 4) { + fprintf(stderr, "sqe submit failed: %d, %d\n", ret, pending); + goto err; + } + } + + /* wait for timers to fire */ + usleep(2 * 10000); + + /* + * We should have 16 pending CQEs now, 8 of them in the overflow list. Any + * attempt to queue more IO should return -EBUSY + */ + sqe = io_uring_get_sqe(&ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + io_uring_prep_nop(sqe); + ret = io_uring_submit(&ring); + if (ret != -EBUSY) { + fprintf(stderr, "expected sqe submit busy: %d\n", ret); + goto err; + } + + /* reap the 16 events we should have available */ + ret = reap_events(&ring, 16, 1); + if (ret < 0) { + fprintf(stderr, "ret=%d\n", ret); + goto err; + } + + if (*ring.cq.koverflow) { + fprintf(stderr, "cq ring overflow %d, expected 0\n", + *ring.cq.koverflow); + goto err; + } + + io_uring_queue_exit(&ring); + return 0; +err: + io_uring_queue_exit(&ring); + return 1; +} + +/* + * Submit some NOPs and watch if the overflow is correct + */ +static int test_overflow(void) +{ + struct io_uring ring; + struct io_uring_sqe *sqe; + int ret, i, j; + + ret = io_uring_queue_init(4, &ring, 0); + if (ret) { + fprintf(stderr, "io_uring_queue_init failed %d\n", ret); + return 1; + } + + /* submit 4x4 SQEs, should overflow the ring by 8 */ + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + sqe = io_uring_get_sqe(&ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + io_uring_prep_nop(sqe); + sqe->user_data = (i * 4) + j; + } + + ret = io_uring_submit(&ring); + if (ret != 4) { + fprintf(stderr, "sqe submit failed: %d\n", ret); + goto err; + } + } + + /* we should now have 8 completions ready */ + ret = reap_events(&ring, 8, 0); + if (ret < 0) + goto err; + + if (*ring.cq.koverflow != 8) { + fprintf(stderr, "cq ring overflow %d, expected 8\n", + *ring.cq.koverflow); + goto err; + } + io_uring_queue_exit(&ring); + return 0; +err: + io_uring_queue_exit(&ring); + return 1; +} + +int main(int argc, char *argv[]) +{ + int ret; + + ret = test_overflow(); + if (ret) { + printf("test_overflow failed\n"); + return ret; + } + + ret = test_overflow_nodrop(); + if (ret) { + printf("test_overflow_nodrop failed\n"); + return ret; + } + + return 0; +} |