Skip to content

Commit 948b3b2

Browse files
SureshChandrappaakpm00
authored andcommitted
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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Suresh K C <[email protected]> Reviewed-by: Joshua Hahn <[email protected]> Tested-by: Nhat Pham <[email protected]> Acked-by: Nhat Pham <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Shuah Khan <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent aec2d49 commit 948b3b2

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

tools/testing/selftests/cachestat/test_cachestat.c

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
3333
cs->nr_evicted, cs->nr_recently_evicted);
3434
}
3535

36+
enum file_type {
37+
FILE_MMAP,
38+
FILE_SHMEM
39+
};
40+
3641
bool write_exactly(int fd, size_t filesize)
3742
{
3843
int random_fd = open("/dev/urandom", O_RDONLY);
@@ -201,8 +206,19 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
201206
out:
202207
return ret;
203208
}
209+
const char* file_type_str(enum file_type type) {
210+
switch (type) {
211+
case FILE_SHMEM:
212+
return "shmem";
213+
case FILE_MMAP:
214+
return "mmap";
215+
default:
216+
return "unknown";
217+
}
218+
}
204219

205-
bool test_cachestat_shmem(void)
220+
221+
bool run_cachestat_test(enum file_type type)
206222
{
207223
size_t PS = sysconf(_SC_PAGESIZE);
208224
size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
@@ -212,27 +228,49 @@ bool test_cachestat_shmem(void)
212228
char *filename = "tmpshmcstat";
213229
struct cachestat cs;
214230
bool ret = true;
231+
int fd;
215232
unsigned long num_pages = compute_len / PS;
216-
int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
233+
if (type == FILE_SHMEM)
234+
fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
235+
else
236+
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
217237

218238
if (fd < 0) {
219-
ksft_print_msg("Unable to create shmem file.\n");
239+
ksft_print_msg("Unable to create %s file.\n",file_type_str(type));
220240
ret = false;
221241
goto out;
222242
}
223243

224244
if (ftruncate(fd, filesize)) {
225-
ksft_print_msg("Unable to truncate shmem file.\n");
245+
ksft_print_msg("Unable to truncate %s file.\n",file_type_str(type));
226246
ret = false;
227247
goto close_fd;
228248
}
229-
230-
if (!write_exactly(fd, filesize)) {
231-
ksft_print_msg("Unable to write to shmem file.\n");
232-
ret = false;
233-
goto close_fd;
249+
switch (type){
250+
case FILE_SHMEM:
251+
if (!write_exactly(fd, filesize)) {
252+
ksft_print_msg("Unable to write to file.\n");
253+
ret = false;
254+
goto close_fd;
255+
}
256+
break;
257+
case FILE_MMAP:
258+
char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
259+
if (map == MAP_FAILED) {
260+
ksft_print_msg("mmap failed.\n");
261+
ret = false;
262+
goto close_fd;
263+
}
264+
for (int i = 0; i < filesize; i++) {
265+
map[i] = 'A';
266+
}
267+
break;
268+
default:
269+
ksft_print_msg("Unsupported file type.\n");
270+
ret = false;
271+
goto close_fd;
272+
break;
234273
}
235-
236274
syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
237275

238276
if (syscall_ret) {
@@ -308,12 +346,18 @@ int main(void)
308346
break;
309347
}
310348

311-
if (test_cachestat_shmem())
349+
if (run_cachestat_test(FILE_SHMEM))
312350
ksft_test_result_pass("cachestat works with a shmem file\n");
313351
else {
314352
ksft_test_result_fail("cachestat fails with a shmem file\n");
315353
ret = 1;
316354
}
317355

356+
if (run_cachestat_test(FILE_MMAP))
357+
ksft_test_result_pass("cachestat works with a mmap file\n");
358+
else {
359+
ksft_test_result_fail("cachestat fails with a mmap file\n");
360+
ret = 1;
361+
}
318362
return ret;
319363
}

0 commit comments

Comments
 (0)