Created
July 20, 2025 10:48
-
-
Save kawaii-ghost/cdc43833df63861a3da8db036a73ee2f to your computer and use it in GitHub Desktop.
Trying to handle timeout with io_uring_submit_and_wait_timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <liburing.h> | |
| #include <unistd.h> | |
| #include <linux/futex.h> | |
| #define QUEUE_DEPTH 64 | |
| #define BUFFER_SIZE (64 * 1024) | |
| #define BLOCK_SZ 4096 | |
| #define WAIT_NR 64 | |
| int main(int argc, char **argv) { | |
| struct io_uring ring; | |
| struct io_uring_params params; | |
| int ret; | |
| ret = io_uring_queue_init(QUEUE_DEPTH, &ring, IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN); | |
| if (ret < 0) { | |
| fprintf(stderr, "io_uring_queue_init_mem: %s\n", strerror(-ret)); | |
| return 1; | |
| } | |
| _Atomic uint32_t vec[64] = {0}; | |
| for (int i = 0; i < QUEUE_DEPTH; i++) { | |
| struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); | |
| if (!sqe) { | |
| fprintf(stderr, "io_uring_get_sqe failed\n"); | |
| io_uring_queue_exit(&ring); | |
| return 1; | |
| } | |
| io_uring_prep_futex_wait(sqe, (uint32_t *)&vec[i], 0, FUTEX_BITSET_MATCH_ANY, FUTEX2_PRIVATE | FUTEX2_SIZE_U32, 0); | |
| } | |
| struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; | |
| struct io_uring_cqe *cqe; | |
| ret = io_uring_submit_and_wait_timeout(&ring, &cqe, WAIT_NR, (struct __kernel_timespec *)&ts, NULL); | |
| if (ret < 0) { | |
| if (ret == -ETIME) { | |
| printf("io_uring_submit_and_wait_timeout timed out\n"); | |
| } else { | |
| fprintf(stderr, "io_uring_submit_and_wait_timeout: %s\n", strerror(-ret)); | |
| } | |
| } else { | |
| printf("Submitted %d SQEs\n", ret); | |
| } | |
| unsigned cqe_count = 0; | |
| while (1) { | |
| ret = io_uring_peek_cqe(&ring, &cqe); | |
| if (ret == -EAGAIN) { | |
| break; | |
| } else if (ret < 0) { | |
| fprintf(stderr, "io_uring_peek_cqe: %s\n", strerror(-ret)); | |
| break; | |
| } | |
| if (cqe->res < 0) { | |
| fprintf(stderr, "CQE %u error: %d\n", cqe_count, cqe->res); | |
| } else { | |
| printf("CQE %u completed, read %d bytes\n", cqe_count, cqe->res); | |
| } | |
| io_uring_cqe_seen(&ring, cqe); | |
| cqe_count++; | |
| } | |
| if (cqe_count == WAIT_NR) { | |
| printf("All %u CQEs completed successfully\n", WAIT_NR); | |
| } else { | |
| printf("Only %u of %u CQEs completed, likely due to timeout\n", cqe_count, WAIT_NR); | |
| } | |
| io_uring_queue_exit(&ring); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment