- mkdir dir
- f2fs_io test_lookup_perf -i /mnt/f2fs/dir 50000
- sync
- echo 3 > /proc/sys/vm/drop_caches
- f2fs_io test_lookup_perf /mnt/f2fs/dir

Output:
Measure readdir performance
Measure stat performance
Operation: total_files, total_time_s, throughput_files_per_sec
readdir: 50000, 1.7781, 28120.08
stat: 50000, 2.1665, 23079.19

- f2fs_io test_lookup_perf -v /mnt/f2fs/dir 50000

Output:
inode    file type  d_reclen  d_off    d_name
6176     directory  24        1        .
3        directory  24        2        ..
6276     regular    32        4        test_file_0
6285     regular    32        6        test_file_1
6291     regular    32        8        test_file_2
6295     regular    32        10       test_file_3
....

Signed-off-by: Chao Yu <c...@kernel.org>
---
v3:
- add doc entry
- fix parameter in usage description
 man/f2fs_io.8           |   3 +
 tools/f2fs_io/f2fs_io.c | 150 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+)

diff --git a/man/f2fs_io.8 b/man/f2fs_io.8
index e0f659e..0d69b5f 100644
--- a/man/f2fs_io.8
+++ b/man/f2fs_io.8
@@ -184,6 +184,9 @@ Get i_advise value and info in file
 .TP
 \fBioprio\fR \fI[hint] [file]\fR
 Set ioprio to the file. The ioprio can be ioprio_write.
+.TP
+\fBtest_lookup_perf\fR \fI[-i] [-v] <dir> <num_files>\fR
+Measure readdir/stat speed
 .SH AUTHOR
 This version of
 .B f2fs_io
diff --git a/tools/f2fs_io/f2fs_io.c b/tools/f2fs_io/f2fs_io.c
index f6649f1..f282190 100644
--- a/tools/f2fs_io/f2fs_io.c
+++ b/tools/f2fs_io/f2fs_io.c
@@ -36,6 +36,8 @@
 #include <time.h>
 #include <unistd.h>
 #include <sys/xattr.h>
+#define _GNU_SOURCE
+#include <dirent.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -2237,6 +2239,153 @@ static void do_test_create_perf(int argc, char **argv, 
const struct cmd_desc *cm
        exit(0);
 }
 
+#define test_lookup_perf_desc "measure readdir/stat speed"
+#define test_lookup_perf_help                                          \
+"f2fs_io test_lookup_perf [-i] [-v] <dir> <num_files>\n\n"             \
+"Measures readdir/stat performance.\n"                         \
+"  <dir>          The target directory in where it will test on.\n"    \
+"  <num_files>    The total number of files the test will initialize or 
test.\n"\
+"  [-i]           Initialized to create files only.\n"\
+"  [-v]           Verbose mode.\n"
+
+static void do_test_lookup_perf(int argc, char **argv, const struct cmd_desc 
*cmd)
+{
+       struct timespec readdir_start, readdir_end;
+       struct timespec stat_start, stat_end;
+       DIR *dirp;
+       struct dirent *dp;
+       int opt;
+       char *dir;
+       int num_files;
+       bool need_initialize = false;
+       bool verb = false;
+       int i;
+
+       while ((opt = getopt(argc, argv, "iv")) != -1) {
+               switch (opt) {
+               case 'i':
+                       need_initialize = true;
+                       break;
+               case 'v':
+                       verb = true;
+                       break;
+               default:
+                       fputs(cmd->cmd_help, stderr);
+                       exit(1);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 2) {
+               fputs("Excess arguments\n\n", stderr);
+               fputs(cmd->cmd_help, stderr);
+               exit(1);
+       }
+
+       dir = argv[0];
+       num_files = atoi(argv[1]);
+
+       if (num_files <= 0) {
+               fprintf(stderr, "Error: Number of files must be positive.\n");
+               exit(1);
+       }
+
+       if (need_initialize) {
+               int fd;
+
+               // Initialization Phase
+               printf("Starting test: Creating %d files in %s\n", num_files, 
dir);
+
+               for (i = 0; i < num_files; i++) {
+                       char path[1024];
+
+                       snprintf(path, sizeof(path), "%s/test_file_%d", dir, i);
+
+                       fd = xopen(path, O_WRONLY | O_CREAT, 0644);
+                       if (fd < 0)
+                               exit(1);
+                       close(fd);
+               }
+
+               exit(0);
+       }
+
+       // Measure readdir performance
+       printf("Measure readdir performance\n");
+       clock_gettime(CLOCK_MONOTONIC, &readdir_start);
+
+       dirp = opendir(dir);
+       if (dirp == NULL) {
+               perror("opendir failed");
+               exit(1);
+       }
+
+       if (verb)
+               printf("inode    file type  d_reclen  d_off    d_name\n");
+
+       while ((dp = readdir(dirp)) != NULL) {
+               if (!verb)
+                       continue;
+
+               printf("%-8lu %-10s %-9d %-8jd %s\n",
+                       dp->d_ino,
+                       (dp->d_type == DT_REG) ?  "regular" :
+                       (dp->d_type == DT_DIR) ?  "directory" :
+                       (dp->d_type == DT_FIFO) ? "FIFO" :
+                       (dp->d_type == DT_SOCK) ? "socket" :
+                       (dp->d_type == DT_LNK) ?  "symlink" :
+                       (dp->d_type == DT_BLK) ?  "block dev" :
+                       (dp->d_type == DT_CHR) ?  "char dev" : "unknown",
+                       dp->d_reclen, dp->d_off, dp->d_name);
+       }
+
+       closedir(dirp);
+
+       clock_gettime(CLOCK_MONOTONIC, &readdir_end);
+
+       // Measure stat performance
+       printf("Measure stat performance\n");
+
+       clock_gettime(CLOCK_MONOTONIC, &stat_start);
+
+       for (i = 0; i < num_files; i++) {
+               char path[1024];
+               struct stat st;
+
+               snprintf(path, sizeof(path), "%s/test_file_%d", dir, i);
+               if (stat(path, &st) != 0)
+                       die_errno("stat failed");
+       }
+
+       clock_gettime(CLOCK_MONOTONIC, &stat_end);
+
+       long readdir_seconds = readdir_end.tv_sec - readdir_start.tv_sec;
+       long readdir_ns = readdir_end.tv_nsec - readdir_start.tv_nsec;
+       double readdir_time_s = (double)readdir_seconds + (double)readdir_ns / 
1000000000.0;
+       double readdir_throughput = (readdir_time_s > 0) ? (num_files / 
readdir_time_s) : 0;
+
+       long stat_seconds = stat_end.tv_sec - stat_start.tv_sec;
+       long stat_ns = stat_end.tv_nsec - stat_start.tv_nsec;
+       double stat_time_s = (double)stat_seconds + (double)stat_ns / 
1000000000.0;
+       double stat_throughput = (stat_time_s > 0) ? (num_files / stat_time_s) 
: 0;
+
+       printf("Operation: total_files, total_time_s, 
throughput_files_per_sec\n");
+
+       printf("readdir: %d, %.4f, %.2f\n",
+                  num_files,
+                  readdir_time_s,
+                  readdir_throughput);
+
+       printf("stat: %d, %.4f, %.2f\n",
+                  num_files,
+                  stat_time_s,
+                  stat_throughput);
+
+       exit(0);
+}
+
 #define CMD_HIDDEN     0x0001
 #define CMD(name) { #name, do_##name, name##_desc, name##_help, 0 }
 #define _CMD(name) { #name, do_##name, NULL, NULL, CMD_HIDDEN }
@@ -2286,6 +2435,7 @@ const struct cmd_desc cmd_list[] = {
        CMD(ioprio),
        CMD(ftruncate),
        CMD(test_create_perf),
+       CMD(test_lookup_perf),
        { NULL, NULL, NULL, NULL, 0 }
 };
 
-- 
2.49.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to