blob: 1a685360bff6a83365e3f946a8d1bf432c6cc679 [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboef93c84e2019-01-08 06:51:07 -07002/*
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 Axboef0f42a12020-08-20 05:48:24 -060012#include <sys/types.h>
13#include <sys/stat.h>
Jens Axboef93c84e2019-01-08 06:51:07 -070014#include <unistd.h>
Stefan Hajnoczic31c7ec2019-07-24 09:24:50 +010015#include "liburing.h"
Jens Axboef93c84e2019-01-08 06:51:07 -070016
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070017#define QD 4
18
Jens Axboef93c84e2019-01-08 06:51:07 -070019int main(int argc, char *argv[])
20{
Jens Axboe7f7a66e2019-01-08 15:31:35 -070021 struct io_uring ring;
Jens Axboe66a7d052019-01-08 15:59:09 -070022 int i, fd, ret, pending, done;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070023 struct io_uring_sqe *sqe;
24 struct io_uring_cqe *cqe;
25 struct iovec *iovecs;
Jens Axboef0f42a12020-08-20 05:48:24 -060026 struct stat sb;
27 ssize_t fsize;
Jens Axboef93c84e2019-01-08 06:51:07 -070028 off_t offset;
29 void *buf;
30
31 if (argc < 2) {
32 printf("%s: file\n", argv[0]);
33 return 1;
34 }
35
Jens Axboea992ffa2019-01-10 15:11:07 -070036 ret = io_uring_queue_init(QD, &ring, 0);
Jens Axboe66a7d052019-01-08 15:59:09 -070037 if (ret < 0) {
38 fprintf(stderr, "queue_init: %s\n", strerror(-ret));
Jens Axboef93c84e2019-01-08 06:51:07 -070039 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 Axboef0f42a12020-08-20 05:48:24 -060048 if (fstat(fd, &sb) < 0) {
49 perror("fstat");
50 return 1;
51 }
52
53 fsize = 0;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070054 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 Axboef0f42a12020-08-20 05:48:24 -060060 fsize += 4096;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070061 }
Jens Axboef93c84e2019-01-08 06:51:07 -070062
63 offset = 0;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070064 i = 0;
Jens Axboef93c84e2019-01-08 06:51:07 -070065 do {
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070066 sqe = io_uring_get_sqe(&ring);
67 if (!sqe)
Jens Axboef93c84e2019-01-08 06:51:07 -070068 break;
Jens Axboe5789a632019-01-17 18:12:22 -070069 io_uring_prep_readv(sqe, fd, &iovecs[i], 1, offset);
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070070 offset += iovecs[i].iov_len;
Stephen Bates733223a2019-06-13 03:04:32 -060071 i++;
Jens Axboef0f42a12020-08-20 05:48:24 -060072 if (offset > sb.st_size)
73 break;
Jens Axboef93c84e2019-01-08 06:51:07 -070074 } while (1);
75
Jens Axboe66a7d052019-01-08 15:59:09 -070076 ret = io_uring_submit(&ring);
Jens Axboef93c84e2019-01-08 06:51:07 -070077 if (ret < 0) {
78 fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
79 return 1;
Jens Axboef0f42a12020-08-20 05:48:24 -060080 } else if (ret != i) {
81 fprintf(stderr, "io_uring_submit submitted less %d\n", ret);
82 return 1;
Jens Axboef93c84e2019-01-08 06:51:07 -070083 }
84
85 done = 0;
86 pending = ret;
Jens Axboef0f42a12020-08-20 05:48:24 -060087 fsize = 0;
Jens Axboef93c84e2019-01-08 06:51:07 -070088 for (i = 0; i < pending; i++) {
Jens Axboe39e0ebd2019-04-18 08:32:06 -060089 ret = io_uring_wait_cqe(&ring, &cqe);
Jens Axboef93c84e2019-01-08 06:51:07 -070090 if (ret < 0) {
Jens Axboe39e0ebd2019-04-18 08:32:06 -060091 fprintf(stderr, "io_uring_wait_cqe: %s\n", strerror(-ret));
Jens Axboef93c84e2019-01-08 06:51:07 -070092 return 1;
93 }
94
95 done++;
Jens Axboe76b61eb2019-04-17 09:25:32 -060096 ret = 0;
Jens Axboef0f42a12020-08-20 05:48:24 -060097 if (cqe->res != 4096 && cqe->res + fsize != sb.st_size) {
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070098 fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
Jens Axboe76b61eb2019-04-17 09:25:32 -060099 ret = 1;
Jens Axboef93c84e2019-01-08 06:51:07 -0700100 }
Jens Axboef0f42a12020-08-20 05:48:24 -0600101 fsize += cqe->res;
Jens Axboe76b61eb2019-04-17 09:25:32 -0600102 io_uring_cqe_seen(&ring, cqe);
103 if (ret)
104 break;
Jens Axboef93c84e2019-01-08 06:51:07 -0700105 }
106
Jens Axboef0f42a12020-08-20 05:48:24 -0600107 printf("Submitted=%d, completed=%d, bytes=%lu\n", pending, done,
108 (unsigned long) fsize);
Jens Axboef93c84e2019-01-08 06:51:07 -0700109 close(fd);
Jens Axboe66a7d052019-01-08 15:59:09 -0700110 io_uring_queue_exit(&ring);
Jens Axboef93c84e2019-01-08 06:51:07 -0700111 return 0;
112}