From 8202c46439c9b6099d6968545d65adfa6d1ef214 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Fri, 1 Nov 2024 10:49:23 -0600 Subject: [PATCH] test/sqpoll-sleep: ensure WAKEUP flag is read correctly Rewrite this test to be a bit better: - Read wakeup properly with IO_URING_READ_ONCE() - Check if wakeup has been seen - Check elapsed time before wakeup flag is seen - Prepare and push a nop request first, to ensure the thread is up and running Hopefully this will fix the quirks with this test. Link: https://github.com/axboe/liburing/issues/1207 Signed-off-by: Jens Axboe --- test/sqpoll-sleep.c | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/test/sqpoll-sleep.c b/test/sqpoll-sleep.c index 2f1ead2c0..6e93231f2 100644 --- a/test/sqpoll-sleep.c +++ b/test/sqpoll-sleep.c @@ -14,32 +14,63 @@ int main(int argc, char *argv[]) { struct io_uring_params p = {}; + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; struct timeval tv; struct io_uring ring; + unsigned long elapsed; + bool seen_wakeup; int ret; if (argc > 1) - return 0; + return T_EXIT_SKIP; p.flags = IORING_SETUP_SQPOLL; - p.sq_thread_idle = 10000; + p.sq_thread_idle = 100; ret = io_uring_queue_init_params(1, &ring, &p); if (ret) { if (geteuid()) { printf("%s: skipped, not root\n", argv[0]); - return 0; + return T_EXIT_SKIP; } fprintf(stderr, "queue_init=%d\n", ret); - return 1; + return T_EXIT_FAIL; } + sqe = io_uring_get_sqe(&ring); + io_uring_prep_nop(sqe); + io_uring_submit(&ring); + + ret = io_uring_wait_cqe(&ring, &cqe); + if (ret) { + fprintf(stderr, "wait_cqe: %d\n", ret); + return T_EXIT_FAIL; + } + io_uring_cqe_seen(&ring, cqe); + + elapsed = 0; + seen_wakeup = false; gettimeofday(&tv, NULL); do { - usleep(100000); - if ((*ring.sq.kflags) & IORING_SQ_NEED_WAKEUP) - return 0; - } while (mtime_since_now(&tv) < 1000); + usleep(100); + if (IO_URING_READ_ONCE(*ring.sq.kflags) & IORING_SQ_NEED_WAKEUP) { + seen_wakeup = true; + break; + } + elapsed = mtime_since_now(&tv); + } while (elapsed < 1000); + + if (!seen_wakeup) { + fprintf(stderr, "SQPOLL didn't flag wakeup\n"); + return T_EXIT_FAIL; + } + + /* should be around 100 msec */ + if (elapsed < 90 || elapsed > 110) { + fprintf(stderr, "SQPOLL wakeup timing off %lu\n", elapsed); + return T_EXIT_FAIL; + } - return 1; + return T_EXIT_PASS; }