2019-01-08 06:51:07 -07:00
|
|
|
/*
|
|
|
|
|
* 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"
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
#define QD 4
|
|
|
|
|
|
2019-01-08 06:51:07 -07:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
struct io_uring_params p;
|
2019-01-08 15:31:35 -07:00
|
|
|
struct io_uring ring;
|
2019-01-08 15:59:09 -07:00
|
|
|
int i, fd, ret, pending, done;
|
2019-01-09 15:26:20 -07:00
|
|
|
struct io_uring_sqe *sqe;
|
|
|
|
|
struct io_uring_cqe *cqe;
|
|
|
|
|
struct iovec *iovecs;
|
2019-01-08 06:51:07 -07:00
|
|
|
off_t offset;
|
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
|
printf("%s: file\n", argv[0]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
|
p.flags = IORING_SETUP_IOPOLL;
|
|
|
|
|
|
2019-01-10 09:32:41 -07:00
|
|
|
ret = io_uring_queue_init(QD, &p, NULL, 0, &ring);
|
2019-01-08 15:59:09 -07:00
|
|
|
if (ret < 0) {
|
|
|
|
|
fprintf(stderr, "queue_init: %s\n", strerror(-ret));
|
2019-01-08 06:51:07 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd = open(argv[1], O_RDONLY | O_DIRECT);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
perror("open");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 15:26:20 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2019-01-08 06:51:07 -07:00
|
|
|
|
|
|
|
|
offset = 0;
|
2019-01-09 15:26:20 -07:00
|
|
|
i = 0;
|
2019-01-08 06:51:07 -07:00
|
|
|
do {
|
2019-01-09 15:26:20 -07:00
|
|
|
sqe = io_uring_get_sqe(&ring);
|
|
|
|
|
if (!sqe)
|
2019-01-08 06:51:07 -07:00
|
|
|
break;
|
2019-01-09 15:26:20 -07:00
|
|
|
sqe->opcode = IORING_OP_READV;
|
|
|
|
|
sqe->flags = 0;
|
|
|
|
|
sqe->ioprio = 0;
|
|
|
|
|
sqe->fd = fd;
|
|
|
|
|
sqe->off = offset;
|
|
|
|
|
sqe->addr = &iovecs[i];
|
|
|
|
|
sqe->len = 1;
|
|
|
|
|
offset += iovecs[i].iov_len;
|
2019-01-08 06:51:07 -07:00
|
|
|
} while (1);
|
|
|
|
|
|
2019-01-08 15:59:09 -07:00
|
|
|
ret = io_uring_submit(&ring);
|
2019-01-08 06:51:07 -07:00
|
|
|
if (ret < 0) {
|
|
|
|
|
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done = 0;
|
|
|
|
|
pending = ret;
|
|
|
|
|
for (i = 0; i < pending; i++) {
|
2019-01-09 15:26:20 -07:00
|
|
|
ret = io_uring_get_completion(&ring, &cqe);
|
2019-01-08 06:51:07 -07:00
|
|
|
if (ret < 0) {
|
|
|
|
|
fprintf(stderr, "io_uring_get_completion: %s\n", strerror(-ret));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done++;
|
2019-01-09 15:26:20 -07:00
|
|
|
if (cqe->res != 4096) {
|
|
|
|
|
fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
|
2019-01-08 06:51:07 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Submitted=%d, completed=%d\n", pending, done);
|
|
|
|
|
close(fd);
|
2019-01-08 15:59:09 -07:00
|
|
|
io_uring_queue_exit(&ring);
|
2019-01-08 06:51:07 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|