[PATCH v3] selftests:proc Add /proc/$(pid)/statm output validation

From: Swarup Laxman Kotiaklapudi
Date: Mon Oct 09 2023 - 15:20:36 EST


Add /proc/${pid}/statm validation

/proc/$(pid)/statm output is expected to be:
"0 0 0 * 0 0 0\n"
Here * can be any value

Read output of /proc/$(pid)/statm
and check except for 4th position,
all other positions have value zero.

Fixes: 5bc73bb3451b (proc: test how it holds up with mapping'less process)
Signed-off-by: Swarup Laxman Kotiaklapudi <swarupkotikalapudi@xxxxxxxxx>
---

Changes in V3:
- Remove wrong comment "/proc/${pid}/statm is under
CONFIG_PROC_PAGE_MONITOR"
- read() function usage is change

Changes in V2:
- 4th field if zero it will assert
- field other than 4th field is checked for zero value
- clean up of function test_proc_pid_statm()

tools/testing/selftests/proc/proc-empty-vm.c | 60 ++++++++++++++++++--
1 file changed, 55 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/proc/proc-empty-vm.c b/tools/testing/selftests/proc/proc-empty-vm.c
index b16c13688b88..bc76e8bba7e4 100644
--- a/tools/testing/selftests/proc/proc-empty-vm.c
+++ b/tools/testing/selftests/proc/proc-empty-vm.c
@@ -38,6 +38,8 @@
#include <sys/wait.h>
#include <unistd.h>

+#include "../kselftest.h"
+
#ifdef __amd64__
#define TEST_VSYSCALL
#endif
@@ -302,6 +304,57 @@ static int test_proc_pid_smaps_rollup(pid_t pid)
}
}

+static int test_proc_pid_statm(pid_t pid)
+{
+ char buf[4096];
+ char *tok;
+ char *string, *p;
+ int non_zero_value_indx = 4;
+ int i = 1, len;
+ size_t left;
+
+ snprintf(buf, sizeof(buf), "/proc/%u/statm", pid);
+
+ /*
+ * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything.
+ */
+ int fd = open(buf, O_RDONLY);
+
+ if (fd == -1) {
+ if (errno == ENOENT) {
+ return EXIT_SUCCESS;
+ }
+ perror("open /proc/${pid}/statm");
+ return EXIT_FAILURE;
+ } else {
+ memset(buf, 0, sizeof(buf));
+ left = ARRAY_SIZE(buf);
+ p = buf;
+ while ((len = read(fd, p, left)) > 0) {
+ p += len;
+ left -= len;
+ }
+ close(fd);
+ string = buf;
+
+ while ((tok = strsep(&string, " ")) != NULL) {
+ if (i == non_zero_value_indx) {
+ if (!strncmp(tok, "0", 1))
+ goto err_statm;
+ } else {
+ if (strncmp(tok, "0", 1))
+ goto err_statm;
+ }
+ i++;
+ }
+ }
+
+ return EXIT_SUCCESS;
+
+err_statm:
+ assert(0);
+}
+
int main(void)
{
int rv = EXIT_SUCCESS;
@@ -388,11 +441,8 @@ int main(void)
if (rv == EXIT_SUCCESS) {
rv = test_proc_pid_smaps_rollup(pid);
}
- /*
- * TODO test /proc/${pid}/statm, task_statm()
- * ->start_code, ->end_code aren't updated by munmap().
- * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything.
- */
+ if (rv == EXIT_SUCCESS)
+ rv = test_proc_pid_statm(pid);

/* Cut the rope. */
int wstatus;
--
2.34.1