Add a test file for hugepage registered buffers, to make sure the fixed buffer coalescing feature works safe and soundly. Testcases include reading/writing with single/multiple hugepage buffers. Signed-off-by: Chenliang Li <cliang01.li@xxxxxxxxxxx> --- test/Makefile | 1 + test/fixed-hugepage.c | 236 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 test/fixed-hugepage.c diff --git a/test/Makefile b/test/Makefile index 94bdc25..364514d 100644 --- a/test/Makefile +++ b/test/Makefile @@ -88,6 +88,7 @@ test_srcs := \ file-update.c \ file-verify.c \ fixed-buf-iter.c \ + fixed-hugepage.c \ fixed-link.c \ fixed-reuse.c \ fpos.c \ diff --git a/test/fixed-hugepage.c b/test/fixed-hugepage.c new file mode 100644 index 0000000..4fe45a2 --- /dev/null +++ b/test/fixed-hugepage.c @@ -0,0 +1,236 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Test fixed buffers consisting of hugepages. + */ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <stdlib.h> +#include <sys/mman.h> + +#include "liburing.h" +#include "helpers.h" + +#define PAGE_SIZE 4096 +#define HUGEPAGE_SIZE (512 * PAGE_SIZE) +#define NR_BUFS 1 +#define IN_FD "/dev/urandom" +#define OUT_FD "/dev/zero" + +static int open_files(int *fd_in, int *fd_out) +{ + *fd_in = open(IN_FD, O_RDONLY, 0644); + if (*fd_in < 0) { + perror("open in"); + return 1; + } + + *fd_out = open(OUT_FD, O_RDWR, 0644); + if (*fd_out < 0) { + perror("open out"); + return 1; + } + + return 0; +} + +static int mmap_hugebufs(struct iovec *iov, int nr_bufs, size_t buf_size) +{ + int i; + + for (i = 0; i < nr_bufs; i++) { + void *base = NULL; + + base = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); + if (!base || base == MAP_FAILED) { + fprintf(stderr, "Error in mmapping the %dth buffer", i); + return 1; + } + + iov[i].iov_base = base; + iov[i].iov_len = buf_size; + memset(iov[i].iov_base, 0, buf_size); + } + + return 0; +} + +static int do_read(struct io_uring *ring, int fd, struct iovec *iov, + int nr_bufs, size_t buf_size) +{ + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + int i, ret; + + for (i = 0; i < nr_bufs; i++) { + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + + io_uring_prep_read_fixed(sqe, fd, iov[i].iov_base, buf_size, 0, i); + io_uring_submit(ring); + + ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret)); + return 1; + } + + if (cqe->res < 0) { + fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res)); + return 1; + } + + io_uring_cqe_seen(ring, cqe); + } + + return 0; +} + +static int do_write(struct io_uring *ring, int fd, struct iovec *iov, + int nr_bufs, size_t buf_size) +{ + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + int i, ret; + + for (i = 0; i < nr_bufs; i++) { + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + + io_uring_prep_write_fixed(sqe, fd, iov[i].iov_base, buf_size, 0, i); + io_uring_submit(ring); + + ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret)); + return 1; + } + + if (cqe->res < 0) { + fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res)); + return 1; + } + + io_uring_cqe_seen(ring, cqe); + } + + return 0; +} + +static int test_one_hugepage(struct io_uring *ring, int fd_in, int fd_out) +{ + struct iovec iov[NR_BUFS]; + int ret; + size_t buf_size = HUGEPAGE_SIZE; + + ret = mmap_hugebufs(iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "Buffer allocating for one hugepage failed."); + return 1; + } + + ret = io_uring_register_buffers(ring, iov, NR_BUFS); + if (ret) { + fprintf(stderr, "Error registering buffers for one hugepage test: %s", strerror(-ret)); + return 1; + } + + ret = do_read(ring, fd_in, iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "One hugepage read test failed"); + return ret; + } + + ret = do_write(ring, fd_out, iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "One hugepages write test failed"); + return ret; + } + + ret = io_uring_unregister_buffers(ring); + if (ret) { + fprintf(stderr, "Error unregistering buffers for one hugepage test: %s", strerror(-ret)); + return 1; + } + + return 0; +} + +static int test_multi_hugepages(struct io_uring *ring, int fd_in, int fd_out) +{ + struct iovec iov[NR_BUFS]; + int ret; + size_t buf_size = 4 * HUGEPAGE_SIZE; + + ret = mmap_hugebufs(iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "mmap multi hugepages failed."); + return 1; + } + + ret = io_uring_register_buffers(ring, iov, NR_BUFS); + if (ret) { + fprintf(stderr, "Error registering buffers for multi hugepages test: %s", strerror(-ret)); + return 1; + } + + ret = do_read(ring, fd_in, iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "multi hugepages read test failed"); + return ret; + } + + ret = do_write(ring, fd_out, iov, NR_BUFS, buf_size); + if (ret) { + fprintf(stderr, "multi hugepages write test failed"); + return ret; + } + + ret = io_uring_unregister_buffers(ring); + if (ret) { + fprintf(stderr, "Error unregistering buffers for multi hugepages test: %s", strerror(-ret)); + return 1; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct io_uring ring; + int ret, fd_in, fd_out; + + if (argc > 1) + return T_EXIT_SKIP; + + if (open_files(&fd_in, &fd_out)) + return T_EXIT_FAIL; + + ret = t_create_ring(8, &ring, 0); + if (ret == T_SETUP_SKIP) + return T_EXIT_SKIP; + else if (ret < 0) + return T_EXIT_FAIL; + + ret = test_one_hugepage(&ring, fd_in, fd_out); + if (ret) { + fprintf(stderr, "Test one hugepage failed"); + return T_EXIT_FAIL; + } + + ret = test_multi_hugepages(&ring, fd_in, fd_out); + if (ret) { + fprintf(stderr, "Test multi hugepages failed"); + return T_EXIT_FAIL; + } + + io_uring_queue_exit(&ring); + return T_EXIT_PASS; +} base-commit: 23b43f44f882e48a725ad87f8f22722c40743dec -- 2.34.1