summaryrefslogtreecommitdiff
path: root/examples/io_uring-test.c
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2020-08-20 05:48:24 -0600
committerJens Axboe <axboe@kernel.dk>2020-08-20 05:48:24 -0600
commitf0f42a1b1df22f0a1d4a5b6ee1c1ab5be754f36a (patch)
tree2860eb1712afdbbe5dc8deaed97121b13894a383 /examples/io_uring-test.c
parent515604dc3c5e7528df2325a0219af0bd394e4294 (diff)
downloadliburing-f0f42a1b1df22f0a1d4a5b6ee1c1ab5be754f36a.tar.gz
liburing-f0f42a1b1df22f0a1d4a5b6ee1c1ab5be754f36a.tar.bz2
examples/io_uring-test: don't error on shorter files
The example currently assumes the file is at least 4*4096 bytes, and errors when we get 0 reads on EOF. Fix this up. Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'examples/io_uring-test.c')
-rw-r--r--examples/io_uring-test.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/examples/io_uring-test.c b/examples/io_uring-test.c
index 1da8407..1a68536 100644
--- a/examples/io_uring-test.c
+++ b/examples/io_uring-test.c
@@ -9,6 +9,8 @@
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include "liburing.h"
@@ -21,6 +23,8 @@ int main(int argc, char *argv[])
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct iovec *iovecs;
+ struct stat sb;
+ ssize_t fsize;
off_t offset;
void *buf;
@@ -41,12 +45,19 @@ int main(int argc, char *argv[])
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;
@@ -58,16 +69,22 @@ int main(int argc, char *argv[])
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) {
@@ -77,16 +94,18 @@ int main(int argc, char *argv[])
done++;
ret = 0;
- if (cqe->res != 4096) {
+ 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\n", pending, done);
+ printf("Submitted=%d, completed=%d, bytes=%lu\n", pending, done,
+ (unsigned long) fsize);
close(fd);
io_uring_queue_exit(&ring);
return 0;