[LTP] [PATCH v1] swapping01.c: Reporting /proc/meminfo before test and at the moment of the failure

Wei Gao wegao@suse.com
Sun Dec 10 10:43:39 CET 2023


Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/mem/swapping/swapping01.c | 24 +++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/mem/swapping/swapping01.c b/testcases/kernel/mem/swapping/swapping01.c
index fc225e4a6..07d89f44b 100644
--- a/testcases/kernel/mem/swapping/swapping01.c
+++ b/testcases/kernel/mem/swapping/swapping01.c
@@ -60,6 +60,17 @@ static long mem_over_max;
 static pid_t pid;
 static unsigned int start_runtime;
 
+static void print_meminfo(char *path)
+{
+	FILE *file;
+	char line[PATH_MAX];
+
+	file = SAFE_FOPEN(path, "r");
+	while (fgets(line, sizeof(line), file))
+		tst_res(TINFO, "%s", line);
+	SAFE_FCLOSE(file);
+}
+
 static void test_swapping(void)
 {
 #ifdef TST_ABI32
@@ -84,6 +95,8 @@ static void test_swapping(void)
 	switch (pid = SAFE_FORK()) {
 	case 0:
 		do_alloc(0);
+		tst_res(TINFO, "Meminfo before check:");
+		print_meminfo("/proc/meminfo");
 		do_alloc(1);
 		exit(0);
 	default:
@@ -108,9 +121,11 @@ static void do_alloc(int allow_raise)
 	long mem_count;
 	void *s;
 
-	if (allow_raise == 1)
+	if (allow_raise == 1) {
+		init_meminfo();
 		tst_res(TINFO, "available physical memory: %ld MB",
 				mem_available_init / 1024);
+	}
 	mem_count = mem_available_init + mem_over;
 	if (allow_raise == 1)
 		tst_res(TINFO, "try to allocate: %ld MB", mem_count / 1024);
@@ -127,6 +142,7 @@ static void check_swapping(void)
 {
 	int status;
 	long swap_free_now, swapped;
+	char proc_status_path[PATH_MAX];
 
 	/* wait child stop */
 	SAFE_WAITPID(pid, &status, WUNTRACED);
@@ -138,6 +154,7 @@ static void check_swapping(void)
 		swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
 		sleep(1);
 		long diff = labs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:"));
+
 		if (diff < 10)
 			break;
 
@@ -146,6 +163,11 @@ static void check_swapping(void)
 
 	swapped = SAFE_READ_PROC_STATUS(pid, "VmSwap:");
 	if (swapped > mem_over_max) {
+		tst_res(TINFO, "Heavy swapping detected, print meminfo:");
+		print_meminfo("/proc/meminfo");
+		tst_res(TINFO, "Heavy swapping detected, print proc status:");
+		sprintf(proc_status_path, "/proc/%d/status", pid);
+		print_meminfo(proc_status_path);
 		kill(pid, SIGCONT);
 		tst_brk(TFAIL, "heavy swapping detected: "
 				"%ld MB swapped.", swapped / 1024);
-- 
2.35.3



More information about the ltp mailing list