Skip to content

Commit

Permalink
Add example of closing a ring fd and using it via registered index
Browse files Browse the repository at this point in the history
Based on io_uring-test, modified to use io_uring_close_ring_fd.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
  • Loading branch information
joshtriplett committed Feb 16, 2023
1 parent b90be56 commit 6117393
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/src/include/liburing/compat.h
/src/include/liburing/io_uring_version.h

/examples/io_uring-close-test
/examples/io_uring-cp
/examples/io_uring-test
/examples/io_uring-udp
Expand Down
1 change: 1 addition & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include ../config-host.mak
endif

example_srcs := \
io_uring-close-test.c \
io_uring-cp.c \
io_uring-test.c \
io_uring-udp.c \
Expand Down
123 changes: 123 additions & 0 deletions examples/io_uring-close-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* SPDX-License-Identifier: MIT */
/*
* Simple app that demonstrates how to setup an io_uring interface, and use it
* via a registered ring fd, without leaving the original fd open.
*
* gcc -Wall -O2 -D_GNU_SOURCE -o io_uring-close-test io_uring-close-test.c -luring
*/
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "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;
struct stat sb;
ssize_t fsize;
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;
}

ret = io_uring_register_ring_fd(&ring);
if (ret < 0) {
fprintf(stderr, "register_ring_fd: %s\n", strerror(-ret));
return 1;
}
ret = io_uring_close_ring_fd(&ring);
if (ret < 0) {
fprintf(stderr, "close_ring_fd: %s\n", strerror(-ret));
return 1;
}

fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
return 1;
}

if (fstat(fd, &sb) < 0) {
perror("fstat");
return 1;
}

fsize = 0;
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;
fsize += 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;
i++;
if (offset > sb.st_size)
break;
} while (1);

ret = io_uring_submit(&ring);
if (ret < 0) {
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
return 1;
} else if (ret != i) {
fprintf(stderr, "io_uring_submit submitted less %d\n", ret);
return 1;
}

done = 0;
pending = ret;
fsize = 0;
for (i = 0; i < pending; i++) {
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret < 0) {
fprintf(stderr, "io_uring_wait_cqe: %s\n", strerror(-ret));
return 1;
}

done++;
ret = 0;
if (cqe->res != 4096 && cqe->res + fsize != sb.st_size) {
fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
ret = 1;
}
fsize += cqe->res;
io_uring_cqe_seen(&ring, cqe);
if (ret)
break;
}

printf("Submitted=%d, completed=%d, bytes=%lu\n", pending, done,
(unsigned long) fsize);
close(fd);
io_uring_queue_exit(&ring);
return 0;
}

0 comments on commit 6117393

Please sign in to comment.