[PATCH v2 2/2] selftest: improve mmap test clarity
From: Suresh K C
Date: Mon Jul 07 2025 - 11:27:12 EST
From: Suresh K C <suresh.k.chandrappa@xxxxxxxxx>
This patch refactors the mmap test logic to remove redundancy and improve
error reporting. It also removes leftover test code that is no longer needed.
Changes since v1:
- Refactored mmap logic into a switch statement as suggested
- Removed the last-character difference, which was only used for testing
- Added clearer error messages to indicate whether shmem or mmap failed
- Combined patches into a series for better context
- Addressed feedback on patch origin and versioning
Signed-off-by: Suresh K C <suresh.k.chandrappa@xxxxxxxxx>
---
.../selftests/cachestat/test_cachestat.c | 52 +++++++++++++------
1 file changed, 35 insertions(+), 17 deletions(-)
diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
index b6452978dae0..0549b7224ba1 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -206,6 +206,17 @@ 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 run_cachestat_test(enum file_type type)
{
@@ -225,34 +236,41 @@ bool run_cachestat_test(enum file_type type)
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
- ksft_print_msg("Unable to create 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 file.\n");
- ret = false;
- goto close_fd;
- }
-
- if (!write_exactly(fd, filesize)) {
- ksft_print_msg("Unable to write to file.\n");
+ ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type));
ret = false;
goto close_fd;
}
- if (type == 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");
+ 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;
- }
- for (int i = 0; i < filesize; i++) {
- map[i] = 'A';
- }
- map[filesize - 1] = 'X';
+ break;
}
syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
--
2.43.0