From 80765e187f04bd1fc7336c55b0186f7eb13f3e78 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 4 Jan 2024 13:59:21 -0700 Subject: [PATCH] test/defer-tw-timeout: test irq completions as well Signed-off-by: Jens Axboe --- test/defer-tw-timeout.c | 93 +++++++++++++++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/test/defer-tw-timeout.c b/test/defer-tw-timeout.c index cb504b883..1aad7b4fc 100644 --- a/test/defer-tw-timeout.c +++ b/test/defer-tw-timeout.c @@ -33,11 +33,10 @@ static void *thread_fn(void *data) return NULL; } -int main(int argc, char *argv[]) +static int test_poll(struct io_uring *ring) { struct io_uring_cqe *cqe; struct io_uring_sqe *sqe; - struct io_uring ring; struct __kernel_timespec ts; int ret, fds[2], i; pthread_t thread; @@ -45,20 +44,13 @@ int main(int argc, char *argv[]) struct d d; void *tret; - if (argc > 1) - return T_EXIT_SKIP; - if (pipe(fds) < 0) { perror("pipe"); return 1; } d.fd = fds[1]; - ret = io_uring_queue_init(2, &ring, IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN); - if (ret == -EINVAL) - return T_EXIT_SKIP; - - sqe = io_uring_get_sqe(&ring); + sqe = io_uring_get_sqe(ring); io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0); pthread_create(&thread, NULL, thread_fn, &d); @@ -66,17 +58,17 @@ int main(int argc, char *argv[]) ts.tv_sec = 1; ts.tv_nsec = 0; - ret = io_uring_submit_and_wait_timeout(&ring, &cqe, 2, &ts, NULL); + ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL); if (ret != 1) { fprintf(stderr, "unexpected wait ret %d\n", ret); return T_EXIT_FAIL; } for (i = 0; i < 2; i++) { - ret = io_uring_peek_cqe(&ring, &cqe); + ret = io_uring_peek_cqe(ring, &cqe); if (ret) break; - io_uring_cqe_seen(&ring, cqe); + io_uring_cqe_seen(ring, cqe); } if (i != 1) { @@ -87,3 +79,78 @@ int main(int argc, char *argv[]) pthread_join(thread, &tret); return T_EXIT_PASS; } + +static int test_file(struct io_uring *ring) +{ + struct io_uring_cqe *cqe; + struct io_uring_sqe *sqe; + struct __kernel_timespec ts; + char fname[64]; + int fd, ret, i; + void *buf; + + sprintf(fname, ".defer-tw-timeout.%d", getpid()); + t_create_file(fname, 128*1024); + + fd = open(fname, O_RDONLY | O_DIRECT); + if (fd < 0) { + perror("open"); + unlink(fname); + return T_EXIT_FAIL; + } + + unlink(fname); + + if (posix_memalign(&buf, 4096, 4096)) + return T_EXIT_FAIL; + + sqe = io_uring_get_sqe(ring); + io_uring_prep_read(sqe, fd, buf, 4096, 0); + + ts.tv_sec = 1; + ts.tv_nsec = 0; + + ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL); + if (ret != 1) { + fprintf(stderr, "unexpected wait ret %d\n", ret); + return T_EXIT_FAIL; + } + + for (i = 0; i < 2; i++) { + ret = io_uring_peek_cqe(ring, &cqe); + if (ret) + break; + io_uring_cqe_seen(ring, cqe); + } + + if (i != 1) { + fprintf(stderr, "Got %d request, expected 1\n", i); + return T_EXIT_FAIL; + } + + return T_EXIT_PASS; +} + +int main(int argc, char *argv[]) +{ + struct io_uring ring; + int ret; + + if (argc > 1) + return T_EXIT_SKIP; + + ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN); + if (ret == -EINVAL) + return T_EXIT_SKIP; + + ret = test_file(&ring); + if (ret != T_EXIT_PASS) + return ret; + + ret = test_poll(&ring); + if (ret != T_EXIT_PASS) + return ret; + + + return T_EXIT_PASS; +}