summaryrefslogtreecommitdiff
path: root/examples/io_uring-test.c
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-04-17 17:38:14 +0000
committerJens Axboe <axboe@kernel.dk>2019-04-17 11:39:39 -0600
commit4916320ec374cc5d2b4062c48d2e4c576472b739 (patch)
tree5f6fd8bdc735da53000e3a03bedf9b564e148f0f /examples/io_uring-test.c
parent76b61ebf1bd17d3a31c3bf2d8236b9bd50d0f9a8 (diff)
downloadliburing-4916320ec374cc5d2b4062c48d2e4c576472b739.tar.gz
liburing-4916320ec374cc5d2b4062c48d2e4c576472b739.tar.bz2
Separate test cases from examples
Also adds a runtests makefile target.
Diffstat (limited to 'examples/io_uring-test.c')
-rw-r--r--examples/io_uring-test.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/examples/io_uring-test.c b/examples/io_uring-test.c
new file mode 100644
index 0000000..fe0098c
--- /dev/null
+++ b/examples/io_uring-test.c
@@ -0,0 +1,91 @@
+/*
+ * Simple app that demonstrates how to setup an io_uring interface,
+ * submit and complete IO against it, and then tear it down.
+ *
+ * gcc -Wall -O2 -D_GNU_SOURCE -o io_uring-test io_uring-test.c -luring
+ */
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "../src/liburing.h"
+
+#define QD 4
+
+int main(int argc, char *argv[])
+{
+ struct io_uring ring;
+ int i, fd, ret, pending, done;
+ struct io_uring_sqe *sqe;
+ struct io_uring_cqe *cqe;
+ struct iovec *iovecs;
+ off_t offset;
+ void *buf;
+
+ if (argc < 2) {
+ printf("%s: file\n", argv[0]);
+ return 1;
+ }
+
+ ret = io_uring_queue_init(QD, &ring, 0);
+ if (ret < 0) {
+ fprintf(stderr, "queue_init: %s\n", strerror(-ret));
+ return 1;
+ }
+
+ fd = open(argv[1], O_RDONLY | O_DIRECT);
+ if (fd < 0) {
+ perror("open");
+ return 1;
+ }
+
+ iovecs = calloc(QD, sizeof(struct iovec));
+ for (i = 0; i < QD; i++) {
+ if (posix_memalign(&buf, 4096, 4096))
+ return 1;
+ iovecs[i].iov_base = buf;
+ iovecs[i].iov_len = 4096;
+ }
+
+ offset = 0;
+ i = 0;
+ do {
+ sqe = io_uring_get_sqe(&ring);
+ if (!sqe)
+ break;
+ io_uring_prep_readv(sqe, fd, &iovecs[i], 1, offset);
+ offset += iovecs[i].iov_len;
+ } while (1);
+
+ ret = io_uring_submit(&ring);
+ if (ret < 0) {
+ fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
+ return 1;
+ }
+
+ done = 0;
+ pending = ret;
+ for (i = 0; i < pending; i++) {
+ ret = io_uring_wait_completion(&ring, &cqe);
+ if (ret < 0) {
+ fprintf(stderr, "io_uring_get_completion: %s\n", strerror(-ret));
+ return 1;
+ }
+
+ done++;
+ ret = 0;
+ if (cqe->res != 4096) {
+ fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
+ ret = 1;
+ }
+ io_uring_cqe_seen(&ring, cqe);
+ if (ret)
+ break;
+ }
+
+ printf("Submitted=%d, completed=%d\n", pending, done);
+ close(fd);
+ io_uring_queue_exit(&ring);
+ return 0;
+}