[LTP] [PATCH v2 3/3] syscalls/madvise11: new test for madvise(MADV_DONTNEED)

Zhao Gongyi zhaogongyi@huawei.com
Tue Oct 11 14:16:07 CEST 2022


Test cases for madvise(2) system call, verify that when MADV_DONTNEED
applied to shared mappings, it will lead to the resident set size(RSS)
of the calling process reduced immediately.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/madvise/.gitignore  |  1 +
 testcases/kernel/syscalls/madvise/madvise11.c | 78 +++++++++++++++++++
 3 files changed, 80 insertions(+)
 create mode 100644 testcases/kernel/syscalls/madvise/madvise11.c

diff --git a/runtest/syscalls b/runtest/syscalls
index eb1910cec..296af9f9d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -948,6 +948,7 @@ madvise07 madvise07
 madvise08 madvise08
 madvise09 madvise09
 madvise10 madvise10
+madvise11 madvise11

 newuname01 newuname01

diff --git a/testcases/kernel/syscalls/madvise/.gitignore b/testcases/kernel/syscalls/madvise/.gitignore
index db8ce47c1..ffd8823d1 100644
--- a/testcases/kernel/syscalls/madvise/.gitignore
+++ b/testcases/kernel/syscalls/madvise/.gitignore
@@ -8,3 +8,4 @@
 /madvise08
 /madvise09
 /madvise10
+/madvise11
diff --git a/testcases/kernel/syscalls/madvise/madvise11.c b/testcases/kernel/syscalls/madvise/madvise11.c
new file mode 100644
index 000000000..77f0b91ac
--- /dev/null
+++ b/testcases/kernel/syscalls/madvise/madvise11.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * Author: Zhao Gongyi <zhaogongyi@huawei.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test cases for madvise(2) system call, advise value as "MADV_MADV_DONTNEED":
+ *   When MADV_DONTNEED applied to shared mappings, it will lead to the
+ *   resident set size(RSS) of the calling process reduced immediately.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "tst_test.h"
+
+#define MAP_SIZE (8 * 1024)
+#define BUF_SIZE 1024
+
+static FILE *fp;
+static char *addr;
+
+static void run(void)
+{
+	char cmd[BUF_SIZE];
+	char line[BUF_SIZE];
+	char vm_area_addr[128];
+
+	TEST(madvise(addr, MAP_SIZE, MADV_DONTNEED));
+	if (TST_RET == -1) {
+		tst_brk(TBROK | TTERRNO, "madvise(%p, %d, 0x%x)",
+			addr, MAP_SIZE, MADV_DONTNEED);
+	}
+
+	sprintf(vm_area_addr, "%p", addr);
+	sprintf(cmd,
+		"cat /proc/%d/smaps | grep %s -A 4 | grep Rss: | grep '0 kB'",
+		getpid(), &(vm_area_addr[2]));
+
+	fp = popen(cmd, "r");
+	if (!fp)
+		tst_brk(TBROK, "popen failed");
+
+	if (fgets(line, sizeof(line), fp) != NULL) {
+		if (strstr(line, " 0 kB"))
+			tst_res(TPASS, "RSS is released");
+		else
+			tst_res(TFAIL, "RSS is not released");
+
+	} else
+		tst_brk(TBROK, "There is no 'Rss:' in smaps?");
+}
+
+static void setup(void)
+{
+	addr = SAFE_MMAP(NULL, MAP_SIZE,
+			PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS,
+			-1, 0);
+	memset(addr, 1, MAP_SIZE);
+}
+
+static void cleanup(void)
+{
+	if (addr)
+		SAFE_MUNMAP(addr, MAP_SIZE);
+	if (fp)
+		pclose(fp);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
+
--
2.17.1



More information about the ltp mailing list