[LTP] [PATCH v1 3/3] Add cachestat02 test
Andrea Cervesato
andrea.cervesato@suse.de
Thu May 16 12:42:27 CEST 2024
From: Andrea Cervesato <andrea.cervesato@suse.com>
This test verifies that cachestat() syscall is properly counting cached
pages written inside a shared memory.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/syscalls | 1 +
.../kernel/syscalls/cachestat/.gitignore | 1 +
.../kernel/syscalls/cachestat/cachestat02.c | 82 +++++++++++++++++++
3 files changed, 84 insertions(+)
create mode 100644 testcases/kernel/syscalls/cachestat/cachestat02.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 961775cf7..1b0a3bb23 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -64,6 +64,7 @@ cacheflush01 cacheflush01
cachestat01 cachestat01
cachestat01A cachestat01 -s
+cachestat02 cachestat02
chdir01 chdir01
chdir01A symlink01 -T chdir01
diff --git a/testcases/kernel/syscalls/cachestat/.gitignore b/testcases/kernel/syscalls/cachestat/.gitignore
index daea1f4be..0f70fb801 100644
--- a/testcases/kernel/syscalls/cachestat/.gitignore
+++ b/testcases/kernel/syscalls/cachestat/.gitignore
@@ -1 +1,2 @@
cachestat01
+cachestat02
diff --git a/testcases/kernel/syscalls/cachestat/cachestat02.c b/testcases/kernel/syscalls/cachestat/cachestat02.c
new file mode 100644
index 000000000..0221cead4
--- /dev/null
+++ b/testcases/kernel/syscalls/cachestat/cachestat02.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that cachestat() syscall is properly counting cached pages
+ * written inside a shared memory.
+ *
+ * [Algorithm]
+ *
+ * * create a shared memory with a specific amount of pages
+ * * monitor file with cachestat()
+ * * check if the right amount of pages have been moved into cache
+ */
+
+#include "cachestat.h"
+
+#define FILENAME "myfile.bin"
+#define NUMPAGES 32
+
+static char *data;
+static int file_size;
+static struct cachestat *cs;
+static struct cachestat_range *cs_range;
+
+static void run(void)
+{
+ int fd;
+
+ memset(cs, 0, sizeof(struct cachestat));
+
+ fd = shm_open(FILENAME, O_RDWR | O_CREAT, 0600);
+ if (fd < 0)
+ tst_brk(TBROK | TERRNO, "shm_open error");
+
+ SAFE_FTRUNCATE(fd, file_size);
+ SAFE_WRITE(0, fd, data, file_size);
+
+ TST_EXP_PASS(cachestat(fd, cs_range, cs, 0));
+ print_cachestat(cs);
+
+ TST_EXP_EQ_LI(cs->nr_cache + cs->nr_evicted, NUMPAGES);
+
+ SAFE_CLOSE(fd);
+ shm_unlink(FILENAME);
+}
+
+static void setup(void)
+{
+ int page_size;
+
+ page_size = (int)sysconf(_SC_PAGESIZE);
+ file_size = page_size * NUMPAGES;
+
+ data = SAFE_MALLOC(file_size);
+ memset(data, 'a', file_size);
+
+ cs_range->off = 0;
+ cs_range->len = file_size;
+}
+
+static void cleanup(void)
+{
+ if (data)
+ free(data);
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_tmpdir = 1,
+ .min_kver = "6.5",
+ .bufs = (struct tst_buffers []) {
+ {&cs, .size = sizeof(struct cachestat)},
+ {&cs_range, .size = sizeof(struct cachestat_range)},
+ {}
+ },
+};
--
2.35.3
More information about the ltp
mailing list