[PATCH] fsstress: support direct IO

Huang Jianan huangjianan at oppo.com
Tue Dec 15 01:04:28 AEDT 2020


From: huangjianan <huangjianan at oppo.com>

add direct IO test for the stress tool which was mentioned here:
https://lore.kernel.org/linux-erofs/20200206135631.1491-1-hsiangkao@aol.com/

Signed-off-by: Huang Jianan <huangjianan at oppo.com>
Signed-off-by: Guo Weichao <guoweichao at oppo.com>
---
 stress.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/stress.c b/stress.c
index f4bf874..7e7cc93 100644
--- a/stress.c
+++ b/stress.c
@@ -4,12 +4,14 @@
  *
  * Copyright (C) 2019-2020 Gao Xiang <hsiangkao at aol.com>
  */
+#define _GNU_SOURCE
 #define _LARGEFILE64_SOURCE
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -21,6 +23,7 @@
 #define MAX_CHUNKSIZE	(4 * 1024 * 1024)
 #define MAX_SCAN_CHUNKSIZE	(256 * 1024)
 
+bool direct_io = false;
 unsigned int nprocs = 512;
 sig_atomic_t should_stop = 0;
 
@@ -98,7 +101,7 @@ int drop_file_cache(int fd, int mode)
 
 int tryopen(char *filename)
 {
-	int fd = open(filename, O_RDONLY);
+	int fd = open(filename, direct_io ? O_RDONLY : O_RDONLY | O_DIRECT);
 
 	if (fd < 0)
 		return -errno;
@@ -166,6 +169,13 @@ int randread(int fd, int chkfd, uint64_t filesize)
 	if (start + length > filesize)
 		length = filesize - start;
 
+	if (direct_io) {
+		length = (((length - 1) >> PAGE_SHIFT) + 1)
+			<< PAGE_SHIFT;
+		if (!length || start + length > filesize)
+			return 0;
+	}
+
 	printf("randread(%u): %llu bytes @ %llu\n",
 	       getpid(), (unsigned long long)length,
 	       (unsigned long long)start);
@@ -212,7 +222,7 @@ int testfd(int fd, int chkfd, int mode)
 		err = doscan(fd, chkfd, filesize, chunksize);
 		if (err)
 			return err;
-	} else if (mode == RANDSCAN_UNALIGNED) {
+	} else if (mode == RANDSCAN_UNALIGNED && !direct_io) {
 		chunksize = (random() * random() % MAX_SCAN_CHUNKSIZE) + 1;
 		err = doscan(fd, chkfd, filesize, chunksize);
 		if (err)
@@ -252,8 +262,11 @@ static int parse_options(int argc, char *argv[])
 {
 	int opt;
 
-	while ((opt = getopt(argc, argv, "p:")) != -1) {
+	while ((opt = getopt(argc, argv, "dp:")) != -1) {
 		switch (opt) {
+		case 'd':
+			direct_io = true;
+			break;
 		case 'p':
 			nprocs = atoi(optarg);
 			if (nprocs < 0) {
@@ -281,6 +294,7 @@ void usage(void)
 {
 	fputs("usage: [options] TESTFILE [COMPRFILE]\n\n"
 	      "stress tester for read-only filesystems\n"
+	      " -d      use direct io\n"
 	      " -p#     set workers to #\n", stderr);
 }
 
-- 
2.7.4



More information about the Linux-erofs mailing list