Skip to contentSkip to conversation

selftests: cachestat: add tests for mmap Refactor and enhance mmap test for cachestat validation

PATCH Applied Suresh K C 3 participants 3 messages Jul 9, 2025 – Jul 10, 2025

Subsystems (3)
lore
Share & tools
lore

Suresh K C

author
#0 lore raw

PATCH v3 1/1 selftests: cachestat: add tests for mmap Refactor and enhance mmap test for cachestat validation

From: Suresh K C <[email protected]>

This patch merges the previous two patches into a single,
cohesive test case that verifies cachestat behavior with memory-mapped files using mmap().
It also refactors the test logic to reduce redundancy, improve error reporting, and clarify failure messages for both shmem and mmap file types.

Changes since v2:

- Merged the two patches into one as suggested
- Improved error messages for better clarity

Tested on x86_64 with default kernel config.

---
 .../selftests/cachestat/test_cachestat.c      | 66 +++++++++++++++----
 1 file changed, 55 insertions(+), 11 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +55 -11
index 632ab44737ec..18188d7c639e 100644
@@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
 	cs->nr_evicted, cs->nr_recently_evicted);
 }
 
+enum file_type {
+	FILE_MMAP,
+	FILE_SHMEM
+};
+
 bool write_exactly(int fd, size_t filesize)
 {
 	int random_fd = open("/dev/urandom", O_RDONLY);
@@ -201,8 +206,19 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
 out:
 	return ret;
 }
+const char* file_type_str(enum file_type type) {
+	switch (type) {
+		case FILE_SHMEM:
+			return "shmem";
+		case FILE_MMAP:
+			return "mmap";
+		default:
+			return "unknown";
+	}
+}
 
-bool test_cachestat_shmem(void)
+
+bool run_cachestat_test(enum file_type type)
 {
 	size_t PS = sysconf(_SC_PAGESIZE);
 	size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
@@ -212,27 +228,49 @@ bool test_cachestat_shmem(void)
 	char *filename = "tmpshmcstat";
 	struct cachestat cs;
 	bool ret = true;
+	int fd;
 	unsigned long num_pages = compute_len / PS;
-	int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+	if (type == FILE_SHMEM)
+		fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+	else
+		fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
 
 	if (fd < 0) {
-		ksft_print_msg("Unable to create shmem file.\n");
+		ksft_print_msg("Unable to create %s file.\n",file_type_str(type));
 		ret = false;
 		goto out;
 	}
 
 	if (ftruncate(fd, filesize)) {
-		ksft_print_msg("Unable to truncate shmem file.\n");
+		ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type));
 		ret = false;
 		goto close_fd;
 	}
-
-	if (!write_exactly(fd, filesize)) {
-		ksft_print_msg("Unable to write to shmem file.\n");
-		ret = false;
-		goto close_fd;
+	switch (type){
+		case FILE_SHMEM:
+			if (!write_exactly(fd, filesize)) {
+				ksft_print_msg("Unable to write to file.\n");
+				ret = false;
+				goto close_fd;
+			}
+			break;
+		case FILE_MMAP:
+			char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+			if (map == MAP_FAILED) {
+				ksft_print_msg("mmap failed.\n");
+				ret = false;
+				goto close_fd;
+			}
+			for (int i = 0; i < filesize; i++) {
+				map[i] = 'A';
+			}
+			break;
+		default:
+			ksft_print_msg("Unsupported file type.\n");
+			ret = false;
+			goto close_fd;
+			break;
 	}
-
 	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
 	if (syscall_ret) {
@@ -308,12 +346,18 @@ int main(void)
 		break;
 	}
 
-	if (test_cachestat_shmem())
+	if (run_cachestat_test(FILE_SHMEM))
 		ksft_test_result_pass("cachestat works with a shmem file\n");
 	else {
 		ksft_test_result_fail("cachestat fails with a shmem file\n");
 		ret = 1;
 	}
 
+	if (run_cachestat_test(FILE_MMAP))
+		ksft_test_result_pass("cachestat works with a mmap file\n");
+	else {
+		ksft_test_result_fail("cachestat fails with a mmap file\n");
+		ret = 1;
+	}
 	return ret;
 }
Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 5 lines · On Wed, 9 Jul 2025 23:16:57 +0530 Suresh K C <[email protected]>…

From: Suresh K C <[email protected]>

This patch merges the previous two patches into a single,
cohesive test case that verifies cachestat behavior with memory-mapped files using mmap().
It also refactors the test logic to reduce redundancy, improve error reporting, and clarify failure messages for both shmem and mmap file types.

Hi Suresh,
LGTM, thank you for the iterations!

Quoted 130 lines

Changes since v2:

- Merged the two patches into one as suggested
- Improved error messages for better clarity

Tested on x86_64 with default kernel config.

---
 .../selftests/cachestat/test_cachestat.c      | 66 +++++++++++++++----
 1 file changed, 55 insertions(+), 11 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +55 -11
index 632ab44737ec..18188d7c639e 100644
@@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
 	cs->nr_evicted, cs->nr_recently_evicted);
 }
 
+enum file_type {
+	FILE_MMAP,
+	FILE_SHMEM
+};
+
 bool write_exactly(int fd, size_t filesize)
 {
 	int random_fd = open("/dev/urandom", O_RDONLY);
@@ -201,8 +206,19 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
 out:
 	return ret;
 }
+const char* file_type_str(enum file_type type) {
+	switch (type) {
+		case FILE_SHMEM:
+			return "shmem";
+		case FILE_MMAP:
+			return "mmap";
+		default:
+			return "unknown";
+	}
+}
 
-bool test_cachestat_shmem(void)
+
+bool run_cachestat_test(enum file_type type)
 {
 	size_t PS = sysconf(_SC_PAGESIZE);
 	size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
@@ -212,27 +228,49 @@ bool test_cachestat_shmem(void)
 	char *filename = "tmpshmcstat";
 	struct cachestat cs;
 	bool ret = true;
+	int fd;
 	unsigned long num_pages = compute_len / PS;
-	int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+	if (type == FILE_SHMEM)
+		fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+	else
+		fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
 
 	if (fd < 0) {
-		ksft_print_msg("Unable to create shmem file.\n");
+		ksft_print_msg("Unable to create %s file.\n",file_type_str(type));
 		ret = false;
 		goto out;
 	}
 
 	if (ftruncate(fd, filesize)) {
-		ksft_print_msg("Unable to truncate shmem file.\n");
+		ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type));
 		ret = false;
 		goto close_fd;
 	}
-
-	if (!write_exactly(fd, filesize)) {
-		ksft_print_msg("Unable to write to shmem file.\n");
-		ret = false;
-		goto close_fd;
+	switch (type){
+		case FILE_SHMEM:
+			if (!write_exactly(fd, filesize)) {
+				ksft_print_msg("Unable to write to file.\n");
+				ret = false;
+				goto close_fd;
+			}
+			break;
+		case FILE_MMAP:
+			char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+			if (map == MAP_FAILED) {
+				ksft_print_msg("mmap failed.\n");
+				ret = false;
+				goto close_fd;
+			}
+			for (int i = 0; i < filesize; i++) {
+				map[i] = 'A';
+			}
+			break;
+		default:
+			ksft_print_msg("Unsupported file type.\n");
+			ret = false;
+			goto close_fd;
+			break;
 	}
-
 	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
 	if (syscall_ret) {
@@ -308,12 +346,18 @@ int main(void)
 		break;
 	}
 
-	if (test_cachestat_shmem())
+	if (run_cachestat_test(FILE_SHMEM))
 		ksft_test_result_pass("cachestat works with a shmem file\n");
 	else {
 		ksft_test_result_fail("cachestat fails with a shmem file\n");
 		ret = 1;
 	}
 
+	if (run_cachestat_test(FILE_MMAP))
+		ksft_test_result_pass("cachestat works with a mmap file\n");
+	else {
+		ksft_test_result_fail("cachestat fails with a mmap file\n");
+		ret = 1;
+	}
 	return ret;
 }
Signature · 1 line
2.43.0

Sent using hkml (https://github.com/sjp38/hackermail)

maintainer Tested-by Acked-by on v3 1/1
#2 lore raw
In reply to Suresh K C
Quoted 136 lines · Suresh K C

From: Suresh K C <[email protected]>

This patch merges the previous two patches into a single,
cohesive test case that verifies cachestat behavior with memory-mapped files using mmap().
It also refactors the test logic to reduce redundancy, improve error reporting, and clarify failure messages for both shmem and mmap file types.

Changes since v2:

- Merged the two patches into one as suggested
- Improved error messages for better clarity

Tested on x86_64 with default kernel config.

---
 .../selftests/cachestat/test_cachestat.c      | 66 +++++++++++++++----
 1 file changed, 55 insertions(+), 11 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +55 -12
index 632ab44737ec..18188d7c639e 100644
@@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
        cs->nr_evicted, cs->nr_recently_evicted);
 }
 
+enum file_type {
+       FILE_MMAP,
+       FILE_SHMEM
+};
+
 bool write_exactly(int fd, size_t filesize)
 {
        int random_fd = open("/dev/urandom", O_RDONLY);
@@ -201,8 +206,19 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
 out:
        return ret;
 }
+const char* file_type_str(enum file_type type) {
+       switch (type) {
+               case FILE_SHMEM:
+                       return "shmem";
+               case FILE_MMAP:
+                       return "mmap";
+               default:
+                       return "unknown";
+       }
+}
 
-bool test_cachestat_shmem(void)
+
+bool run_cachestat_test(enum file_type type)
 {
        size_t PS = sysconf(_SC_PAGESIZE);
        size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
@@ -212,27 +228,49 @@ bool test_cachestat_shmem(void)
        char *filename = "tmpshmcstat";
        struct cachestat cs;
        bool ret = true;
+       int fd;
        unsigned long num_pages = compute_len / PS;
-       int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+       if (type == FILE_SHMEM)
+               fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
+       else
+               fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
 
        if (fd < 0) {
-               ksft_print_msg("Unable to create shmem file.\n");
+               ksft_print_msg("Unable to create %s file.\n",file_type_str(type));
                ret = false;
                goto out;
        }
 
        if (ftruncate(fd, filesize)) {
-               ksft_print_msg("Unable to truncate shmem file.\n");
+               ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type));
                ret = false;
                goto close_fd;
        }
-
-       if (!write_exactly(fd, filesize)) {
-               ksft_print_msg("Unable to write to shmem file.\n");
-               ret = false;
-               goto close_fd;
+       switch (type){
+               case FILE_SHMEM:
+                       if (!write_exactly(fd, filesize)) {
+                               ksft_print_msg("Unable to write to file.\n");
+                               ret = false;
+                               goto close_fd;
+                       }
+                       break;
+               case FILE_MMAP:
+                       char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+                       if (map == MAP_FAILED) {
+                               ksft_print_msg("mmap failed.\n");
+                               ret = false;
+                               goto close_fd;
+                       }
+                       for (int i = 0; i < filesize; i++) {
+                               map[i] = 'A';
+                       }
+                       break;
+               default:
+                       ksft_print_msg("Unsupported file type.\n");
+                       ret = false;
+                       goto close_fd;
+                       break;
        }
-
        syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
        if (syscall_ret) {
@@ -308,12 +346,18 @@ int main(void)
                break;
        }
 
-       if (test_cachestat_shmem())
+       if (run_cachestat_test(FILE_SHMEM))
                ksft_test_result_pass("cachestat works with a shmem file\n");
        else {
                ksft_test_result_fail("cachestat fails with a shmem file\n");
                ret = 1;
        }
 
+       if (run_cachestat_test(FILE_MMAP))
+               ksft_test_result_pass("cachestat works with a mmap file\n");
+       else {
+               ksft_test_result_fail("cachestat fails with a mmap file\n");
+               ret = 1;
+       }
        return ret;
 }
--
Quoted 2 lines

2.43.0

Code LGTM, and the test passed on my system (x86-64 too though). FWIW:

Thanks for the test contribution, Suresh! And thanks for the review help, Joshua :)