Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Simple app that demonstrates how to setup an io_uring interface, |
| 4 | * submit and complete IO against it, and then tear it down. |
| 5 | * |
| 6 | * gcc -Wall -O2 -D_GNU_SOURCE -o io_uring-test io_uring-test.c -luring |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <string.h> |
| 11 | #include <stdlib.h> |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 14 | #include <unistd.h> |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 15 | #include "liburing.h" |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 16 | |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 17 | #define QD 4 |
| 18 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 19 | int main(int argc, char *argv[]) |
| 20 | { |
Jens Axboe | 7f7a66e | 2019-01-08 15:31:35 -0700 | [diff] [blame] | 21 | struct io_uring ring; |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 22 | int i, fd, ret, pending, done; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 23 | struct io_uring_sqe *sqe; |
| 24 | struct io_uring_cqe *cqe; |
| 25 | struct iovec *iovecs; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 26 | struct stat sb; |
| 27 | ssize_t fsize; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 28 | off_t offset; |
| 29 | void *buf; |
| 30 | |
| 31 | if (argc < 2) { |
| 32 | printf("%s: file\n", argv[0]); |
| 33 | return 1; |
| 34 | } |
| 35 | |
Jens Axboe | a992ffa | 2019-01-10 15:11:07 -0700 | [diff] [blame] | 36 | ret = io_uring_queue_init(QD, &ring, 0); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 37 | if (ret < 0) { |
| 38 | fprintf(stderr, "queue_init: %s\n", strerror(-ret)); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | fd = open(argv[1], O_RDONLY | O_DIRECT); |
| 43 | if (fd < 0) { |
| 44 | perror("open"); |
| 45 | return 1; |
| 46 | } |
| 47 | |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 48 | if (fstat(fd, &sb) < 0) { |
| 49 | perror("fstat"); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | fsize = 0; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 54 | iovecs = calloc(QD, sizeof(struct iovec)); |
| 55 | for (i = 0; i < QD; i++) { |
| 56 | if (posix_memalign(&buf, 4096, 4096)) |
| 57 | return 1; |
| 58 | iovecs[i].iov_base = buf; |
| 59 | iovecs[i].iov_len = 4096; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 60 | fsize += 4096; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 61 | } |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 62 | |
| 63 | offset = 0; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 64 | i = 0; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 65 | do { |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 66 | sqe = io_uring_get_sqe(&ring); |
| 67 | if (!sqe) |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 68 | break; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 69 | io_uring_prep_readv(sqe, fd, &iovecs[i], 1, offset); |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 70 | offset += iovecs[i].iov_len; |
Stephen Bates | 733223a | 2019-06-13 03:04:32 -0600 | [diff] [blame] | 71 | i++; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 72 | if (offset > sb.st_size) |
| 73 | break; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 74 | } while (1); |
| 75 | |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 76 | ret = io_uring_submit(&ring); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 77 | if (ret < 0) { |
| 78 | fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret)); |
| 79 | return 1; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 80 | } else if (ret != i) { |
| 81 | fprintf(stderr, "io_uring_submit submitted less %d\n", ret); |
| 82 | return 1; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | done = 0; |
| 86 | pending = ret; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 87 | fsize = 0; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 88 | for (i = 0; i < pending; i++) { |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 89 | ret = io_uring_wait_cqe(&ring, &cqe); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 90 | if (ret < 0) { |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 91 | fprintf(stderr, "io_uring_wait_cqe: %s\n", strerror(-ret)); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | done++; |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 96 | ret = 0; |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 97 | if (cqe->res != 4096 && cqe->res + fsize != sb.st_size) { |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 98 | fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res); |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 99 | ret = 1; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 100 | } |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 101 | fsize += cqe->res; |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 102 | io_uring_cqe_seen(&ring, cqe); |
| 103 | if (ret) |
| 104 | break; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jens Axboe | f0f42a1 | 2020-08-20 05:48:24 -0600 | [diff] [blame] | 107 | printf("Submitted=%d, completed=%d, bytes=%lu\n", pending, done, |
| 108 | (unsigned long) fsize); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 109 | close(fd); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 110 | io_uring_queue_exit(&ring); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |