summaryrefslogtreecommitdiff
path: root/test/timeout.c
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-09-20 17:48:27 -0600
committerJens Axboe <axboe@kernel.dk>2019-09-20 19:49:30 -0600
commit3ad9074b7962e1ce97f744534650fbf14d32f465 (patch)
tree91cdaf29669158cddc0e03f257287cde93107de8 /test/timeout.c
parent5109ed3ec733968cc1aa6457020d8cc1389135bb (diff)
downloadliburing-3ad9074b7962e1ce97f744534650fbf14d32f465.tar.gz
liburing-3ad9074b7962e1ce97f744534650fbf14d32f465.tar.bz2
Add TIMEOUT support
Add support for the current TIMEOUT variant. This allows passing in a timeout and event count, first of which will trigger the completion of the timeout command. If the timeout is hit, the command completes with cqe->res == -ETIME. If the asked number of events complete first, the command completes with cqe->res == 0. test/timeout.c tests a variety of timeout conditions. Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'test/timeout.c')
-rw-r--r--test/timeout.c306
1 files changed, 306 insertions, 0 deletions
diff --git a/test/timeout.c b/test/timeout.c
new file mode 100644
index 0000000..641f4ac
--- /dev/null
+++ b/test/timeout.c
@@ -0,0 +1,306 @@
+/*
+ * Description: run various timeout tests
+ *
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/time.h>
+
+#include "liburing.h"
+
+#define TIMEOUT_MSEC 1000
+static int not_supported;
+
+static unsigned long long mtime_since(const struct timeval *s,
+ const struct timeval *e)
+{
+ long long sec, usec;
+
+ sec = e->tv_sec - s->tv_sec;
+ usec = (e->tv_usec - s->tv_usec);
+ if (sec > 0 && usec < 0) {
+ sec--;
+ usec += 1000000;
+ }
+
+ sec *= 1000;
+ usec /= 1000;
+ return sec + usec;
+}
+
+static unsigned long long mtime_since_now(struct timeval *tv)
+{
+ struct timeval end;
+
+ gettimeofday(&end, NULL);
+ return mtime_since(tv, &end);
+}
+
+/*
+ * Test that we return to userspace if a timeout triggers, even if we
+ * don't satisfy the number of events asked for.
+ */
+static int test_single_timeout_many(struct io_uring *ring)
+{
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ unsigned long long exp;
+ struct timespec ts;
+ struct timeval tv;
+ int ret;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = TIMEOUT_MSEC / 1000;
+ ts.tv_nsec = 0;
+ io_uring_prep_timeout(sqe, &ts, 0);
+
+ ret = io_uring_submit(ring);
+ if (ret <= 0) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ gettimeofday(&tv, NULL);
+ ret = io_uring_enter(ring->ring_fd, 0, 4, IORING_ENTER_GETEVENTS, NULL);
+ if (ret < 0) {
+ printf("io_uring_enter %d\n", ret);
+ goto err;
+ }
+
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ if (cqe->res == -EINVAL) {
+ printf("Timeout not supported, ignored\n");
+ not_supported = 1;
+ return 0;
+ } else if (cqe->res != -ETIME) {
+ printf("Timeout: %s\n", strerror(-cqe->res));
+ goto err;
+ }
+ io_uring_cqe_seen(ring, cqe);
+
+ exp = mtime_since_now(&tv);
+ if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
+ return 0;
+ printf("Timeout seems wonky (got %llu)\n", exp);
+err:
+ return 1;
+}
+
+/*
+ * Test numbered trigger of timeout
+ */
+static int test_single_timeout_nr(struct io_uring *ring)
+{
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ struct timespec ts;
+ int i, ret;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = TIMEOUT_MSEC / 1000;
+ ts.tv_nsec = 0;
+ io_uring_prep_timeout(sqe, &ts, 2);
+
+ sqe = io_uring_get_sqe(ring);
+ io_uring_prep_nop(sqe);
+ io_uring_sqe_set_data(sqe, (void *) 1);
+ sqe = io_uring_get_sqe(ring);
+ io_uring_prep_nop(sqe);
+ io_uring_sqe_set_data(sqe, (void *) 1);
+
+ ret = io_uring_submit_and_wait(ring, 4);
+ if (ret <= 0) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ i = 0;
+ while (i < 3) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+
+ /*
+ * NOP commands have user_data as 1. Check that we get the
+ * two NOPs first, then the successfully removed timout as
+ * the last one.
+ */
+ switch (i) {
+ case 0:
+ case 1:
+ if (io_uring_cqe_get_data(cqe) != (void *) 1) {
+ printf("nop not seen as 1 or 2\n");
+ goto err;
+ }
+ break;
+ case 2:
+ if (io_uring_cqe_get_data(cqe) != NULL) {
+ printf("timeout not last\n");
+ goto err;
+ }
+ break;
+ }
+
+ if (cqe->res < 0) {
+ printf("Timeout: %s\n", strerror(-cqe->res));
+ goto err;
+ } else if (cqe->res) {
+ printf("res: %d\n", cqe->res);
+ goto err;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ i++;
+ };
+
+ return 0;
+err:
+ return 1;
+}
+
+/*
+ * Test single timeout waking us up
+ */
+static int test_single_timeout(struct io_uring *ring)
+{
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ unsigned long long exp;
+ struct timespec ts;
+ struct timeval tv;
+ int ret;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = TIMEOUT_MSEC / 1000;
+ ts.tv_nsec = 0;
+ io_uring_prep_timeout(sqe, &ts, 0);
+
+ ret = io_uring_submit(ring);
+ if (ret <= 0) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ gettimeofday(&tv, NULL);
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ if (cqe->res == -EINVAL) {
+ printf("Timeout not supported, ignored\n");
+ not_supported = 1;
+ return 0;
+ } else if (cqe->res != -ETIME) {
+ printf("Timeout: %s\n", strerror(-cqe->res));
+ goto err;
+ }
+ io_uring_cqe_seen(ring, cqe);
+
+ exp = mtime_since_now(&tv);
+ if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
+ return 0;
+ printf("Timeout seems wonky (got %llu)\n", exp);
+err:
+ return 1;
+}
+
+/*
+ * Test that timeout is canceled on exit
+ */
+static int test_single_timeout_exit(struct io_uring *ring)
+{
+ struct io_uring_sqe *sqe;
+ struct timespec ts;
+ int ret;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = 30;
+ ts.tv_nsec = 0;
+ io_uring_prep_timeout(sqe, &ts, 0);
+
+ ret = io_uring_submit(ring);
+ if (ret <= 0) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ io_uring_queue_exit(ring);
+ return 0;
+err:
+ io_uring_queue_exit(ring);
+ return 1;
+}
+
+int main(int argc, char *argv[])
+{
+ struct io_uring ring;
+ int ret;
+
+ ret = io_uring_queue_init(8, &ring, 0);
+ if (ret) {
+ printf("ring setup failed\n");
+ return 1;
+
+ }
+
+ ret = test_single_timeout(&ring);
+ if (ret) {
+ printf("test_single_timeout failed\n");
+ return ret;
+ }
+ if (not_supported)
+ return 0;
+
+ ret = test_single_timeout_many(&ring);
+ if (ret) {
+ printf("test_single_timeout_many failed\n");
+ return ret;
+ }
+
+ ret = test_single_timeout_nr(&ring);
+ if (ret) {
+ printf("test_single_timeout_nr failed\n");
+ return ret;
+ }
+
+ /*
+ * this test must go last, it kills the ring
+ */
+ ret = test_single_timeout_exit(&ring);
+ if (ret) {
+ printf("test_single_timeout_nr failed\n");
+ return ret;
+ }
+
+ return 0;
+}