From 60b2c5d2c1a8605a8540d2ae6c65037ba1c4fa13 Mon Sep 17 00:00:00 2001 From: Hao Xu Date: Thu, 18 Nov 2021 15:51:54 +0800 Subject: [PATCH] add test for sqpoll sq_thread_idle_us mode Add a test for sqpoll sq_thread_idle_us mode Reviewed-by: Xiaoguang Wang Signed-off-by: Hao Xu --- test/Makefile | 4 +- test/sqpoll-idle-us.c | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 test/sqpoll-idle-us.c diff --git a/test/Makefile b/test/Makefile index 9e911e6..655c287 100644 --- a/test/Makefile +++ b/test/Makefile @@ -26,7 +26,7 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register send_recv eventfd-ring across-fork sq-poll-kthread splice \ lfs-openat lfs-openat-write iopoll d4ae271dfaae-test \ eventfd-disable close-opath ce593a6c480a-test cq-overflow-peek \ - timeout-new ioctl + timeout-new ioctl sqpoll-idle-us include ../Makefile.quiet @@ -67,7 +67,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \ personality.c eventfd.c eventfd-ring.c across-fork.c sq-poll-kthread.c \ splice.c lfs-openat.c lfs-openat-write.c iopoll.c d4ae271dfaae-test.c \ eventfd-disable.c close-opath.c ce593a6c480a-test.c cq-overflow-peek.c \ - timeout-new.c ioctl.c + timeout-new.c ioctl.c sqpoll-idle-us.c ifdef CONFIG_HAVE_STATX test_srcs += statx.c diff --git a/test/sqpoll-idle-us.c b/test/sqpoll-idle-us.c new file mode 100644 index 0000000..957ea7c --- /dev/null +++ b/test/sqpoll-idle-us.c @@ -0,0 +1,98 @@ +/* + * Test sqpoll sq_thread_idle in us mode + */ +#include +#include +#include +#include +#include "liburing.h" + +#define DEFAULT_IDLE_US 50 + +static int submit_single_nop(struct io_uring *ring, bool submit_on_idle) +{ + struct io_uring_sqe *sqe; + int ret; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + io_uring_prep_nop(sqe); + + if (submit_on_idle) + ret = io_uring_submit_on_idle(ring); + else + ret = io_uring_submit(ring); + if (ret <= 0) { + fprintf(stderr, "sqe submit failed: %d\n", ret); + goto err; + } + + return 0; +err: + return 1; +} + +static int reap_cqes(struct io_uring *ring, int count) +{ + int ret; + struct io_uring_cqe *cqe; + + while(count--) { + ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "wait completion %d\n", ret); + return 1; + } + io_uring_cqe_seen(ring, cqe); + } + return 0; +} + +static int test_sqpoll_idle_us(int nr) +{ + struct io_uring_params p = {}; + struct io_uring ring; + int ret, count = nr; + + srand((unsigned)time(NULL)); + p.flags = (IORING_SETUP_SQPOLL | IORING_SETUP_SQPOLL_PERCPU | IORING_SETUP_IDLE_US); + p.sq_thread_idle = DEFAULT_IDLE_US; + + ret = io_uring_queue_init_params(nr + 10, &ring, &p); + if (ret) { + fprintf(stderr, "queue_init=%d\n", ret); + return 1; + } + while (nr--) { + unsigned x = p.sq_thread_idle; + /* + * sleep some time to make sure sqthread often sleeps. + * [(1 / 2) * sq_thread_idle, (2 + 1 / 2) * sq_thread_idle) + */ + usleep((x >> 1) + rand() % (x << 1)); + ret = submit_single_nop(&ring, nr % 2); + if (ret) + return ret; + } + return reap_cqes(&ring, count); +} + +int main(int argc, char *argv[]) +{ + int ret; + + if (argc > 1) + return 0; + + ret = test_sqpoll_idle_us(10000); + if (ret) { + fprintf(stderr, "test failed: %d\n", ret); + return 1; + } + + return 0; +} -- Gitee