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

From: Swarup Laxman Kotiaklapudi
Date: Tue Oct 03 2023 - 15:43: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.

Signed-off-by: Swarup Laxman Kotiaklapudi <swarupkotikalapudi@xxxxxxxxx>
---
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()

Changes in V1:
- added new function test_proc_pid_statm()

tools/testing/selftests/proc/proc-empty-vm.c | 57 ++++++++++++++++++--
1 file changed, 52 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..1b5b48445e0f 100644
--- a/tools/testing/selftests/proc/proc-empty-vm.c
+++ b/tools/testing/selftests/proc/proc-empty-vm.c
@@ -302,6 +302,56 @@ 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;
+ int non_zero_value_indx = 4;
+ int i = 1;
+
+ 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) {
+ /*
+ * /proc/${pid}/statm is under CONFIG_PROC_PAGE_MONITOR,
+ * it doesn't necessarily exist.
+ */
+ return EXIT_SUCCESS;
+ }
+ perror("open /proc/${pid}/statm");
+ return EXIT_FAILURE;
+ } else {
+ ssize_t rv = read(fd, buf, sizeof(buf));
+
+ close(fd);
+ assert(rv);
+ 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 +438,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