Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: cachestat: add tests for mmap, refactor and enhance mmap test for cachestat validation

Add a cohesive test case that verifies cachestat behavior with
memory-mapped files using mmap(). Also refactor the test logic to reduce
redundancy, improve error reporting, and clarify failure messages for both
shmem and mmap file types.

[akpm@linux-foundation.org: coding-style cleanups]
Link: https://lkml.kernel.org/r/20250709174657.6916-1-suresh.k.chandrappa@gmail.com
Signed-off-by: Suresh K C <suresh.k.chandrappa@gmail.com>
Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
Tested-by: Nhat Pham <nphamcs@gmail.com>
Acked-by: Nhat Pham <nphamcs@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Suresh K C and committed by
Andrew Morton
d6a511de 881388f3

+54 -8
+54 -8
tools/testing/selftests/cachestat/test_cachestat.c
··· 33 33 cs->nr_evicted, cs->nr_recently_evicted); 34 34 } 35 35 36 + enum file_type { 37 + FILE_MMAP, 38 + FILE_SHMEM 39 + }; 40 + 36 41 bool write_exactly(int fd, size_t filesize) 37 42 { 38 43 int random_fd = open("/dev/urandom", O_RDONLY); ··· 206 201 out: 207 202 return ret; 208 203 } 204 + const char *file_type_str(enum file_type type) 205 + { 206 + switch (type) { 207 + case FILE_SHMEM: 208 + return "shmem"; 209 + case FILE_MMAP: 210 + return "mmap"; 211 + default: 212 + return "unknown"; 213 + } 214 + } 209 215 210 - bool test_cachestat_shmem(void) 216 + 217 + bool run_cachestat_test(enum file_type type) 211 218 { 212 219 size_t PS = sysconf(_SC_PAGESIZE); 213 220 size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */ ··· 229 212 char *filename = "tmpshmcstat"; 230 213 struct cachestat cs; 231 214 bool ret = true; 215 + int fd; 232 216 unsigned long num_pages = compute_len / PS; 233 - int fd = shm_open(filename, O_CREAT | O_RDWR, 0600); 217 + if (type == FILE_SHMEM) 218 + fd = shm_open(filename, O_CREAT | O_RDWR, 0600); 219 + else 220 + fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666); 234 221 235 222 if (fd < 0) { 236 - ksft_print_msg("Unable to create shmem file.\n"); 223 + ksft_print_msg("Unable to create %s file.\n", 224 + file_type_str(type)); 237 225 ret = false; 238 226 goto out; 239 227 } 240 228 241 229 if (ftruncate(fd, filesize)) { 242 - ksft_print_msg("Unable to truncate shmem file.\n"); 230 + ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type)); 243 231 ret = false; 244 232 goto close_fd; 245 233 } 234 + switch (type) { 235 + case FILE_SHMEM: 236 + if (!write_exactly(fd, filesize)) { 237 + ksft_print_msg("Unable to write to file.\n"); 238 + ret = false; 239 + goto close_fd; 240 + } 241 + break; 242 + case FILE_MMAP: 243 + char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, 244 + MAP_SHARED, fd, 0); 246 245 247 - if (!write_exactly(fd, filesize)) { 248 - ksft_print_msg("Unable to write to shmem file.\n"); 246 + if (map == MAP_FAILED) { 247 + ksft_print_msg("mmap failed.\n"); 248 + ret = false; 249 + goto close_fd; 250 + } 251 + for (int i = 0; i < filesize; i++) 252 + map[i] = 'A'; 253 + break; 254 + default: 255 + ksft_print_msg("Unsupported file type.\n"); 249 256 ret = false; 250 257 goto close_fd; 251 258 } 252 - 253 259 syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0); 254 260 255 261 if (syscall_ret) { ··· 348 308 break; 349 309 } 350 310 351 - if (test_cachestat_shmem()) 311 + if (run_cachestat_test(FILE_SHMEM)) 352 312 ksft_test_result_pass("cachestat works with a shmem file\n"); 353 313 else { 354 314 ksft_test_result_fail("cachestat fails with a shmem file\n"); 355 315 ret = 1; 356 316 } 357 317 318 + if (run_cachestat_test(FILE_MMAP)) 319 + ksft_test_result_pass("cachestat works with a mmap file\n"); 320 + else { 321 + ksft_test_result_fail("cachestat fails with a mmap file\n"); 322 + ret = 1; 323 + } 358 324 return ret; 359 325 }