Skip to contentSkip to conversation

f2fs_io: add mlock to measure the read speed

PATCH In review Jaegeuk Kim via Linux-f2fs-devel 1 participant 5 messages Sep 18, 2025 – Sep 23, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 f2fs_io: add mlock to measure the read speed

---
 tools/f2fs_io/f2fs_io.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
tools/f2fs_io/f2fs_io.c +26 -6
index b9bf9bc5f797..2d64eda81706 100644
@@ -931,6 +931,7 @@ static void do_write_advice(int argc, char **argv, const struct cmd_desc *cmd)
 "  buffered : buffered IO\n"				\
 "  dio      : direct IO\n"				\
 "  mmap     : mmap IO\n"				\
+"  mlock    : mmap + mlock\n"				\
 "advice can be\n"					\
 " 1 : set sequential|willneed\n"			\
 " 0 : none\n"						\
@@ -946,6 +947,7 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	u64 total_time = 0;
 	int flags = 0;
 	int do_mmap = 0;
+	int do_mlock = 0;
 	int fd, advice;
 
 	if (argc != 8) {
@@ -968,6 +970,8 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		flags |= O_DIRECT;
 	else if (!strcmp(argv[4], "mmap"))
 		do_mmap = 1;
+	else if (!strcmp(argv[4], "mlock"))
+		do_mlock = 1;
 	else if (strcmp(argv[4], "buffered"))
 		die("Wrong IO type");
 
@@ -993,11 +997,24 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	total_time = get_current_us();
 	if (do_mmap) {
 		data = mmap(NULL, count * buf_size, PROT_READ,
-						MAP_SHARED | MAP_POPULATE, fd, offset);
+				MAP_SHARED | MAP_POPULATE, fd, offset);
 		if (data == MAP_FAILED)
 			die("Mmap failed");
-	}
-	if (!do_mmap) {
+
+		read_cnt = count * buf_size;
+		memcpy(print_buf, data, print_bytes);
+	} else if (do_mlock) {
+		data = mmap(NULL, count * buf_size, PROT_READ,
+				MAP_SHARED, fd, offset);
+		if (data == MAP_FAILED)
+			die("Mmap failed");
+		if (posix_fadvise(fd, offset, count * buf_size,
+					POSIX_FADV_WILLNEED) != 0)
+			die_errno("fadvise failed");
+		if (mlock(data, count * buf_size))
+			die_errno("mlock failed");
+		read_cnt = count * buf_size;
+	} else {
 		for (i = 0; i < count; i++) {
 			ret = pread(fd, buf, buf_size, offset + buf_size * i);
 			if (ret != buf_size) {
@@ -1014,9 +1031,6 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 			if (i == 0)
 				memcpy(print_buf, buf, print_bytes);
 		}
-	} else {
-		read_cnt = count * buf_size;
-		memcpy(print_buf, data, print_bytes);
 	}
 	printf("Read %"PRIu64" bytes total_time = %"PRIu64" us, BW = %.Lf MB/s print %u bytes:\n",
 		read_cnt, get_current_us() - total_time,
@@ -1029,6 +1043,12 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		else if (i % 2 == 0)
 			printf(" ");
 	}
+	if (do_mmap) {
+		munmap(data, count * buf_size);
+	} else if (do_mlock) {
+		munlock(data, count * buf_size);
+		munmap(data, count * buf_size);
+	}
 	printf("\n");
 	exit(0);
 }
Signature · 8 lines
2.51.0.470.ga7dc726c21-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH 2/3 f2fs_io: add dontcache to measure RWF_DONTCACHE speed

It only measures the read performance.

---
 tools/f2fs_io/f2fs_io.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/f2fs_io/f2fs_io.c +12 -1
index 2d64eda81706..2ed9cb4184b8 100644
@@ -24,6 +24,8 @@
 #include <linux/fs.h>
 #include <signal.h>
 #include <stdarg.h>
+#include <sys/uio.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -929,6 +931,7 @@ static void do_write_advice(int argc, char **argv, const struct cmd_desc *cmd)
 "Read data in file_path and print nbytes\n"		\
 "IO can be\n"						\
 "  buffered : buffered IO\n"				\
+"  dontcache: buffered IO + dontcache\n"		\
 "  dio      : direct IO\n"				\
 "  mmap     : mmap IO\n"				\
 "  mlock    : mmap + mlock\n"				\
@@ -948,6 +951,7 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	int flags = 0;
 	int do_mmap = 0;
 	int do_mlock = 0;
+	int do_dontcache = 0;
 	int fd, advice;
 
 	if (argc != 8) {
@@ -972,6 +976,8 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		do_mmap = 1;
 	else if (!strcmp(argv[4], "mlock"))
 		do_mlock = 1;
+	else if (!strcmp(argv[4], "dontcache"))
+		do_dontcache = 1;
 	else if (strcmp(argv[4], "buffered"))
 		die("Wrong IO type");
 
@@ -1016,7 +1022,12 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		read_cnt = count * buf_size;
 	} else {
 		for (i = 0; i < count; i++) {
-			ret = pread(fd, buf, buf_size, offset + buf_size * i);
+			if (!do_dontcache) {
+				ret = pread(fd, buf, buf_size, offset + buf_size * i);
+			} else {
+				struct iovec iov = { .iov_base = buf, .iov_len = buf_size };
+				ret = preadv2(fd, &iov, 1, offset + buf_size * i, RWF_DONTCACHE);
+			}
 			if (ret != buf_size) {
 				printf("pread expected: %"PRIu64", readed: %"PRIu64"\n",
 						buf_size, ret);
Signature · 8 lines
2.51.0.470.ga7dc726c21-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs_io: add dontcache to measure RWF_DONTCACHE speed

Ditto, need to manual entry.

Quoted 49 lines · On 9/18/25 12:53, Jaegeuk Kim via Linux-f2fs-devel wrote:

It only measures the read performance.

---
 tools/f2fs_io/f2fs_io.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/f2fs_io/f2fs_io.c +6 -0
index 2d64eda81706..2ed9cb4184b8 100644
@@ -24,6 +24,8 @@
 #include <linux/fs.h>
 #include <signal.h>
 #include <stdarg.h>
+#include <sys/uio.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -929,6 +931,7 @@ static void do_write_advice(int argc, char **argv, const struct cmd_desc *cmd)
 "Read data in file_path and print nbytes\n"		\
 "IO can be\n"						\
 "  buffered : buffered IO\n"				\
+"  dontcache: buffered IO + dontcache\n"		\
 "  dio      : direct IO\n"				\
 "  mmap     : mmap IO\n"				\
 "  mlock    : mmap + mlock\n"				\
@@ -948,6 +951,7 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	int flags = 0;
 	int do_mmap = 0;
 	int do_mlock = 0;
+	int do_dontcache = 0;
 	int fd, advice;
 
 	if (argc != 8) {
@@ -972,6 +976,8 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		do_mmap = 1;
 	else if (!strcmp(argv[4], "mlock"))
 		do_mlock = 1;
+	else if (!strcmp(argv[4], "dontcache"))
+		do_dontcache = 1;
 	else if (strcmp(argv[4], "buffered"))
 		die("Wrong IO type");
 
@@ -1016,7 +1022,12 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		read_cnt = count * buf_size;
 	} else {
 		for (i = 0; i < count; i++) {

tools/f2fs_io/f2fs_io.c static void do_read(int argc, char **argv, const struct cmd_desc *cmd) · line 1033

Quoted 10 lines
tools/f2fs_io/f2fs_io.c +6 -1
-			ret = pread(fd, buf, buf_size, offset + buf_size * i);
+			if (!do_dontcache) {
+				ret = pread(fd, buf, buf_size, offset + buf_size * i);
+			} else {
+				struct iovec iov = { .iov_base = buf, .iov_len = buf_size };
+				ret = preadv2(fd, &iov, 1, offset + buf_size * i, RWF_DONTCACHE);
+			}
 			if (ret != buf_size) {
 				printf("pread expected: %"PRIu64", readed: %"PRIu64"\n",
 						buf_size, ret);

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw

PATCH 3/3 f2fs_io: let's try to get contigous memory if possible

In reply to Jaegeuk Kim via Linux-f2fs-devel
---
 tools/f2fs_io/f2fs_io.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/f2fs_io/f2fs_io.c +4 -0
index 2ed9cb4184b8..248cf7a44a34 100644
@@ -101,6 +101,10 @@ static void *aligned_xalloc(size_t alignment, size_t size)
 
 	if (!p)
 		die("Memory alloc failed (requested %zu bytes)", size);
+
+	if (madvise(p, size, MADV_HUGEPAGE))
+		die("Madvise failed (requested %zu bytes)", size);
+
 	return p;
 }
 
Signature · 8 lines
2.51.0.470.ga7dc726c21-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#4 lore raw
In reply to Jaegeuk Kim via Linux-f2fs-devel

Need to update manual entry?

Quoted 75 lines · On 9/18/25 12:53, Jaegeuk Kim via Linux-f2fs-devel wrote:
---
 tools/f2fs_io/f2fs_io.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
tools/f2fs_io/f2fs_io.c +20 -6
index b9bf9bc5f797..2d64eda81706 100644
@@ -931,6 +931,7 @@ static void do_write_advice(int argc, char **argv, const struct cmd_desc *cmd)
 "  buffered : buffered IO\n"				\
 "  dio      : direct IO\n"				\
 "  mmap     : mmap IO\n"				\
+"  mlock    : mmap + mlock\n"				\
 "advice can be\n"					\
 " 1 : set sequential|willneed\n"			\
 " 0 : none\n"						\
@@ -946,6 +947,7 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	u64 total_time = 0;
 	int flags = 0;
 	int do_mmap = 0;
+	int do_mlock = 0;
 	int fd, advice;
 
 	if (argc != 8) {
@@ -968,6 +970,8 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		flags |= O_DIRECT;
 	else if (!strcmp(argv[4], "mmap"))
 		do_mmap = 1;
+	else if (!strcmp(argv[4], "mlock"))
+		do_mlock = 1;
 	else if (strcmp(argv[4], "buffered"))
 		die("Wrong IO type");
 
@@ -993,11 +997,24 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 	total_time = get_current_us();
 	if (do_mmap) {
 		data = mmap(NULL, count * buf_size, PROT_READ,
-						MAP_SHARED | MAP_POPULATE, fd, offset);
+				MAP_SHARED | MAP_POPULATE, fd, offset);
 		if (data == MAP_FAILED)
 			die("Mmap failed");
-	}
-	if (!do_mmap) {
+
+		read_cnt = count * buf_size;
+		memcpy(print_buf, data, print_bytes);
+	} else if (do_mlock) {
+		data = mmap(NULL, count * buf_size, PROT_READ,
+				MAP_SHARED, fd, offset);
+		if (data == MAP_FAILED)
+			die("Mmap failed");
+		if (posix_fadvise(fd, offset, count * buf_size,
+					POSIX_FADV_WILLNEED) != 0)
+			die_errno("fadvise failed");
+		if (mlock(data, count * buf_size))
+			die_errno("mlock failed");
+		read_cnt = count * buf_size;
+	} else {
 		for (i = 0; i < count; i++) {
 			ret = pread(fd, buf, buf_size, offset + buf_size * i);
 			if (ret != buf_size) {
@@ -1014,9 +1031,6 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 			if (i == 0)
 				memcpy(print_buf, buf, print_bytes);
 		}
-	} else {
-		read_cnt = count * buf_size;
-		memcpy(print_buf, data, print_bytes);
 	}
 	printf("Read %"PRIu64" bytes total_time = %"PRIu64" us, BW = %.Lf MB/s print %u bytes:\n",
 		read_cnt, get_current_us() - total_time,
@@ -1029,6 +1043,12 @@ static void do_read(int argc, char **argv, const struct cmd_desc *cmd)
 		else if (i % 2 == 0)
 			printf(" ");

tools/f2fs_io/f2fs_io.c static void do_read(int argc, char **argv, const struct cmd_desc *cmd) · line 1054

Quoted 10 lines
tools/f2fs_io/f2fs_io.c +6 -0
 	}
+	if (do_mmap) {
+		munmap(data, count * buf_size);
+	} else if (do_mlock) {
+		munlock(data, count * buf_size);
+		munmap(data, count * buf_size);
+	}
 	printf("\n");
 	exit(0);
 }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel