Skip to contentSkip to conversation

zcrx: Get the page size at runtime

PATCH In review Haiyue Wang 2 participants 3 messages Apr 19, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zcrx: Get the page size at runtime

Use the API sysconf() to query page size at runtime, instead of using hard code number 4096.

---
 examples/zcrx.c | 20 +++++++++++++-------
 test/zcrx.c     | 11 +++++++++--
 2 files changed, 22 insertions(+), 9 deletions(-)
examples/zcrx.c +13 -7
index 8393cfe..9a9b8b9 100644
@@ -39,8 +39,8 @@
 #include "liburing.h"
 #include "helpers.h"
 
-#define PAGE_SIZE (4096)
-#define AREA_SIZE (8192 * PAGE_SIZE)
+static long page_size;
+#define AREA_SIZE (8192 * page_size)
 #define SEND_SIZE (512 * 4096)
 
 static int cfg_port = 8000;
@@ -65,8 +65,8 @@ static inline size_t get_refill_ring_size(unsigned int rq_entries)
 {
 	ring_size = rq_entries * sizeof(struct io_uring_zcrx_rqe);
 	/* add space for the header (head/tail/etc.) */
-	ring_size += PAGE_SIZE;
-	return T_ALIGN_UP(ring_size, 4096);
+	ring_size += page_size;
+	return T_ALIGN_UP(ring_size, page_size);
 }
 
 static void setup_zcrx(struct io_uring *ring)
@@ -169,7 +169,7 @@ static void process_accept(struct io_uring *ring, struct io_uring_cqe *cqe)
 
 	connfd = cqe->res;
 	if (cfg_oneshot)
-		add_recvzc_oneshot(ring, connfd, PAGE_SIZE);
+		add_recvzc_oneshot(ring, connfd, page_size);
 	else
 		add_recvzc(ring, connfd);
 }
@@ -207,7 +207,7 @@ static void process_recvzc(struct io_uring *ring, struct io_uring_cqe *cqe)
 
 	if (cfg_oneshot) {
 		if (cqe->res == 0 && cqe->flags == 0 && cfg_oneshot_recvs) {
-			add_recvzc_oneshot(ring, connfd, PAGE_SIZE);
+			add_recvzc_oneshot(ring, connfd, page_size);
 			cfg_oneshot_recvs--;
 		}
 	} else if (!(cqe->flags & IORING_CQE_F_MORE)) {
@@ -329,7 +329,13 @@ static void parse_opts(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
-	parse_opts(argc, argv);
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size < 0) {
+		perror("sysconf(_SC_PAGESIZE)");
+		return 1;
+	}
+
+        parse_opts(argc, argv);
 	run_server();
 	return 0;
 }
test/zcrx.c +9 -2
index b60462a..61c984d 100644
@@ -19,10 +19,11 @@
 
 static unsigned int ifidx, rxq;
 
+static long page_size;
+
 /* the hw rxq must consume 128 of these pages, leaving 4 left */
 #define AREA_PAGES	132
-#define PAGE_SIZE	4096
-#define AREA_SZ		AREA_PAGES * PAGE_SIZE
+#define AREA_SZ		(AREA_PAGES * page_size)
 #define RQ_ENTRIES	128
 /* this is one more than the # of free pages after filling hw rxq */
 #define LOOP_COUNT	5
@@ -822,6 +823,12 @@ int main(int argc, char *argv[])
 	if (argc > 1)
 		return T_EXIT_SKIP;
 
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size < 0) {
+		perror("sysconf(_SC_PAGESIZE)");
+		return T_EXIT_FAIL;
+	}
+
 	area_outer = mmap(NULL, AREA_SZ + 8192, PROT_NONE,
 		MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
 	if (area_outer == MAP_FAILED) {
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 2 lines · Haiyue Wang

Use the API sysconf() to query page size at runtime, instead of using hard code number 4096.

This is v2, no? It's customary to include a "changes since the last version" when posting a v2. JFYI.

examples/zcrx.c static void parse_opts(int argc, char **argv) · line 338

Quoted 12 lines
examples/zcrx.c +7 -1
@@ -329,7 +329,13 @@ static void parse_opts(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
-	parse_opts(argc, argv);
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size < 0) {
+		perror("sysconf(_SC_PAGESIZE)");
+		return 1;
+	}
+
+        parse_opts(argc, argv);

Whitespace damage here, it's using spaces rather than tabs.

Outside of that, I think this looks fine. liburing helpers should probably have something for this going forward, so every test that uses the correct page size (or still hardcodes 4096...) would get it right without needing to know about this. But that's beyond the scope of this change.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 7 lines · Jens Axboe
Quoted 2 lines · Haiyue Wang

Use the API sysconf() to query page size at runtime, instead of using hard code number 4096.

This is v2, no? It's customary to include a "changes since the last version" when posting a v2. JFYI.

Got it. Will send v3, fix the tab issue.

Quoted 21 lines

examples/zcrx.c static void parse_opts(int argc, char **argv) · line 338

Quoted 12 lines
examples/zcrx.c +7 -1
@@ -329,7 +329,13 @@ static void parse_opts(int argc, char **argv)
 
  int main(int argc, char **argv)
  {
-	parse_opts(argc, argv);
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size < 0) {
+		perror("sysconf(_SC_PAGESIZE)");
+		return 1;
+	}
+
+        parse_opts(argc, argv);

Whitespace damage here, it's using spaces rather than tabs.

Outside of that, I think this looks fine. liburing helpers should probably have something for this going forward, so every test that uses the correct page size (or still hardcodes 4096...) would get it right without needing to know about this. But that's beyond the scope of this change.