1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4 */
5
6 /*\
7 * [Description]
8 *
9 * This test verifies that cachestat() syscall is properly counting cached pages
10 * written inside a file. If storage device synchronization is requested, test
11 * will check if the number of dirty pages is zero.
12 *
13 * [Algorithm]
14 *
15 * - create a file with specific amount of pages
16 * - synchronize storage device, if needed
17 * - monitor file with cachestat()
18 * - check if the right amount of pages have been moved into cache
19 * - if storage device synchronization is requested, check that dirty pages is
20 * zero
21 */
22
23 #include <stdlib.h>
24 #include "cachestat.h"
25
26 #define MNTPOINT "mntpoint"
27 #define FILENAME MNTPOINT "/myfile.bin"
28
29 static int page_size, num_shift;
30 static char *page_data;
31 static struct cachestat *cs;
32 static struct cachestat_range *cs_range;
33
test_cached_pages(const unsigned int use_sync,const int num_pages)34 static void test_cached_pages(const unsigned int use_sync, const int num_pages)
35 {
36 int fd;
37
38 tst_res(TINFO, "%s file synchronization", use_sync ? "Enable" : "Disable");
39 tst_res(TINFO, "Number of pages: %d", num_pages);
40
41 memset(cs, 0, sizeof(struct cachestat));
42
43 fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, 0600);
44
45 for (int i = 0; i < num_pages; i++)
46 SAFE_WRITE(0, fd, page_data, page_size);
47
48 if (use_sync)
49 fsync(fd);
50
51 cs_range->off = 0;
52 cs_range->len = page_size * num_pages;
53
54 memset(cs, 0xff, sizeof(*cs));
55
56 TST_EXP_PASS(cachestat(fd, cs_range, cs, 0));
57 print_cachestat(cs);
58
59 TST_EXP_EQ_LI(cs->nr_cache + cs->nr_evicted, num_pages);
60
61 if (use_sync)
62 TST_EXP_EQ_LI(cs->nr_dirty, 0);
63
64 SAFE_CLOSE(fd);
65 SAFE_UNLINK(FILENAME);
66 }
67
run(unsigned int use_sync)68 static void run(unsigned int use_sync)
69 {
70 for (int i = 0; i < num_shift; i++)
71 test_cached_pages(use_sync, 1 << i);
72 }
73
setup(void)74 static void setup(void)
75 {
76 page_size = (int)sysconf(_SC_PAGESIZE);
77 num_shift = MIN(tst_device->size*1024*2.5/page_size, 15);
78 page_data = SAFE_MALLOC(page_size);
79 memset(page_data, 'a', page_size);
80 }
81
cleanup(void)82 static void cleanup(void)
83 {
84 free(page_data);
85 }
86
87 static struct tst_test test = {
88 .timeout = 13,
89 .test = run,
90 .tcnt = 2,
91 .setup = setup,
92 .cleanup = cleanup,
93 .mount_device = 1,
94 .mntpoint = MNTPOINT,
95 .all_filesystems = 1,
96 .skip_filesystems = (const char *const []) {
97 "fuse",
98 "tmpfs",
99 NULL
100 },
101 .bufs = (struct tst_buffers []) {
102 {&cs, .size = sizeof(struct cachestat)},
103 {&cs_range, .size = sizeof(struct cachestat_range)},
104 {}
105 },
106 };
107