* [PATCH] selftests: cachestat: add tests for mmap and /proc/cpuinfo
@ 2025-05-24 8:36 Suresh K C
2025-06-04 17:55 ` Nhat Pham
0 siblings, 1 reply; 2+ messages in thread
From: Suresh K C @ 2025-05-24 8:36 UTC (permalink / raw)
To: nphamcs, hannes, shuah
Cc: linux-mm, linux-kselftest, linux-kernel, Suresh K C
From: Suresh K C <suresh.k.chandrappa@gmail.com>
Add a test case to verify cachestat behavior with memory-mapped files
using mmap(). This ensures that pages accessed via mmap are correctly
accounted for in the page cache.
Also add a test for /proc/cpuinfo to validate cachestat's handling of
virtual files in pseudo-filesystems. This improves test coverage for
edge cases involving non-regular files.
Tested on x86_64 with default kernel config.
Signed-off-by: Suresh K C <suresh.k.chandrappa@gmail.com>
---
.../selftests/cachestat/test_cachestat.c | 69 ++++++++++++++++++-
1 file changed, 67 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
index 632ab44737ec..81e7f6dd2279 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -22,7 +22,7 @@
static const char * const dev_files[] = {
"/dev/zero", "/dev/null", "/dev/urandom",
- "/proc/version", "/proc"
+ "/proc/version","/proc/cpuinfo","/proc"
};
void print_cachestat(struct cachestat *cs)
@@ -202,6 +202,65 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
return ret;
}
+bool test_cachestat_mmap(void){
+
+ size_t PS = sysconf(_SC_PAGESIZE);
+ size_t filesize = PS * 512 * 2;;
+ int syscall_ret;
+ size_t compute_len = PS * 512;
+ struct cachestat_range cs_range = { PS, compute_len };
+ char *filename = "tmpshmcstat";
+ unsigned long num_pages = compute_len / PS;
+ struct cachestat cs;
+ bool ret = true;
+ int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
+ if (fd < 0) {
+ ksft_print_msg("Unable to create mmap file.\n");
+ ret = false;
+ goto out;
+ }
+ if (ftruncate(fd, filesize)) {
+ ksft_print_msg("Unable to truncate mmap file.\n");
+ ret = false;
+ goto close_fd;
+ }
+ if (!write_exactly(fd, filesize)) {
+ ksft_print_msg("Unable to write to mmap file.\n");
+ ret = false;
+ goto close_fd;
+ }
+ 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';
+ }
+ map[filesize - 1] = 'X';
+
+ syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
+
+ if (syscall_ret) {
+ ksft_print_msg("Cachestat returned non-zero.\n");
+ ret = false;
+ } else {
+ print_cachestat(&cs);
+ if (cs.nr_cache + cs.nr_evicted != num_pages) {
+ ksft_print_msg("Total number of cached and evicted pages is off.\n");
+ ret = false;
+ }
+ }
+
+close_fd:
+ close(fd);
+ unlink(filename);
+out:
+ return ret;
+}
+
bool test_cachestat_shmem(void)
{
size_t PS = sysconf(_SC_PAGESIZE);
@@ -274,7 +333,7 @@ int main(void)
ret = 1;
}
- for (int i = 0; i < 5; i++) {
+ for (int i = 0; i < 6; i++) {
const char *dev_filename = dev_files[i];
if (test_cachestat(dev_filename, false, false, false,
@@ -315,5 +374,11 @@ int main(void)
ret = 1;
}
+ if (test_cachestat_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;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] selftests: cachestat: add tests for mmap and /proc/cpuinfo
2025-05-24 8:36 [PATCH] selftests: cachestat: add tests for mmap and /proc/cpuinfo Suresh K C
@ 2025-06-04 17:55 ` Nhat Pham
0 siblings, 0 replies; 2+ messages in thread
From: Nhat Pham @ 2025-06-04 17:55 UTC (permalink / raw)
To: Suresh K C; +Cc: hannes, shuah, linux-mm, linux-kselftest, linux-kernel
On Sat, May 24, 2025 at 1:36 AM Suresh K C
<suresh.k.chandrappa@gmail.com> wrote:
>
> From: Suresh K C <suresh.k.chandrappa@gmail.com>
>
> Add a test case to verify cachestat behavior with memory-mapped files
> using mmap(). This ensures that pages accessed via mmap are correctly
> accounted for in the page cache.
>
> Also add a test for /proc/cpuinfo to validate cachestat's handling of
> virtual files in pseudo-filesystems. This improves test coverage for
Hmm, it's been awhile since I wrote these tests, but isn't there
already a test for /proc/* files?
> edge cases involving non-regular files.
>
> Tested on x86_64 with default kernel config.
>
> Signed-off-by: Suresh K C <suresh.k.chandrappa@gmail.com>
> ---
> .../selftests/cachestat/test_cachestat.c | 69 ++++++++++++++++++-
> 1 file changed, 67 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
> index 632ab44737ec..81e7f6dd2279 100644
> --- a/tools/testing/selftests/cachestat/test_cachestat.c
> +++ b/tools/testing/selftests/cachestat/test_cachestat.c
> @@ -22,7 +22,7 @@
>
> static const char * const dev_files[] = {
> "/dev/zero", "/dev/null", "/dev/urandom",
> - "/proc/version", "/proc"
> + "/proc/version","/proc/cpuinfo","/proc"
> };
>
> void print_cachestat(struct cachestat *cs)
> @@ -202,6 +202,65 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
> return ret;
> }
>
> +bool test_cachestat_mmap(void){
> +
> + size_t PS = sysconf(_SC_PAGESIZE);
> + size_t filesize = PS * 512 * 2;;
> + int syscall_ret;
> + size_t compute_len = PS * 512;
> + struct cachestat_range cs_range = { PS, compute_len };
> + char *filename = "tmpshmcstat";
> + unsigned long num_pages = compute_len / PS;
> + struct cachestat cs;
> + bool ret = true;
> + int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
> + if (fd < 0) {
> + ksft_print_msg("Unable to create mmap file.\n");
> + ret = false;
> + goto out;
> + }
> + if (ftruncate(fd, filesize)) {
> + ksft_print_msg("Unable to truncate mmap file.\n");
> + ret = false;
> + goto close_fd;
> + }
> + if (!write_exactly(fd, filesize)) {
> + ksft_print_msg("Unable to write to mmap file.\n");
> + ret = false;
> + goto close_fd;
> + }
> + 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';
> + }
> + map[filesize - 1] = 'X';
> +
> + syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
> +
> + if (syscall_ret) {
> + ksft_print_msg("Cachestat returned non-zero.\n");
> + ret = false;
> + } else {
> + print_cachestat(&cs);
> + if (cs.nr_cache + cs.nr_evicted != num_pages) {
> + ksft_print_msg("Total number of cached and evicted pages is off.\n");
> + ret = false;
> + }
> + }
> +
> +close_fd:
> + close(fd);
> + unlink(filename);
> +out:
> + return ret;
> +}
> +
This looks 90% the same as another test. Are we literally just adding
the mmap step to test_cachestat and call it a new test?
Can we at least refactor things?
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-06-04 17:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-24 8:36 [PATCH] selftests: cachestat: add tests for mmap and /proc/cpuinfo Suresh K C
2025-06-04 17:55 ` Nhat Pham
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).