public inbox for [email protected]
 help / color / mirror / Atom feed
From: Chenliang Li <[email protected]>
To: [email protected], [email protected]
Cc: [email protected], [email protected],
	[email protected], [email protected],
	[email protected], Chenliang Li <[email protected]>
Subject: [PATCH v3 5/5] liburing: add test cases for hugepage registered buffers
Date: Mon, 13 May 2024 16:23:00 +0800	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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


  parent reply	other threads:[~2024-05-13  8:29 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20240513082306epcas5p2fd8ea6fd88b2c4ab1d17b1508fe2af97@epcas5p2.samsung.com>
2024-05-13  8:22 ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Chenliang Li
     [not found]   ` <CGME20240513082308epcas5p3c38ce4d44fa1613988bbae84eaec41b9@epcas5p3.samsung.com>
2024-05-13  8:22     ` [PATCH v3 1/5] io_uring/rsrc: add hugepage buffer coalesce helpers Chenliang Li
     [not found]   ` <CGME20240513082310epcas5p27576a80eae3ee09e40102b179ce46fa9@epcas5p2.samsung.com>
2024-05-13  8:22     ` [PATCH v3 2/5] io_uring/rsrc: store folio shift and mask into imu Chenliang Li
     [not found]   ` <CGME20240513082311epcas5p3556d301a1f1faca0c6b613555324861e@epcas5p3.samsung.com>
2024-05-13  8:22     ` [PATCH v3 3/5] io_uring/rsrc: add init and account functions for coalesced imus Chenliang Li
     [not found]   ` <CGME20240513082313epcas5p2a781d3393e4bf92d13d04ac62bb28fb7@epcas5p2.samsung.com>
2024-05-13  8:22     ` [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing Chenliang Li
2024-05-13 12:11       ` Anuj gupta
     [not found]   ` <CGME20240513082314epcas5p309fa70575596792b5c9923ce76a3778f@epcas5p3.samsung.com>
2024-05-13  8:23     ` Chenliang Li [this message]
2024-05-13 12:09   ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Anuj gupta
2024-05-13 13:40     ` Jens Axboe
     [not found]       ` <CGME20240514001813epcas5p455c8a3dd6f2164626a526c05f7fd92c4@epcas5p4.samsung.com>
2024-05-14  0:18         ` Chenliang Li
     [not found]     ` <CGME20240514001443epcas5p362c60c233ed2aecdb5e144099b44d9be@epcas5p3.samsung.com>
2024-05-14  0:14       ` [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing Chenliang Li
     [not found]     ` <CGME20240514001620epcas5p10d8c08ffc3dbd746213df21e47df19f7@epcas5p1.samsung.com>
2024-05-14  0:16       ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Chenliang Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox