[LTP] [PATCH v1] lib: add .min_swap_avail in tst_test struct

Andrea Cervesato andrea.cervesato@suse.de
Mon Sep 4 09:44:43 CEST 2023


From: Andrea Cervesato <andrea.cervesato@suse.com>

This new field is mainly to set the minimum size of SwapFree for
LTP testcase. If system available free swap memory is less than
.min_swap_avail, test will be exit with TCONF.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 include/tst_memutils.h | 5 +++++
 include/tst_test.h     | 3 +++
 lib/tst_memutils.c     | 9 +++++++++
 lib/tst_test.c         | 3 +++
 4 files changed, 20 insertions(+)

diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index 45dec55bc..19b593430 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -25,6 +25,11 @@ void tst_pollute_memory(size_t maxsize, int fillchar);
  */
 long long tst_available_mem(void);
 
+/*
+ * Read the value of SwapFree from /proc/meminfo.
+ */
+long long tst_available_swap(void);
+
 /*
  * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
  *   echo -1000 >/proc/$PID/oom_score_adj
diff --git a/include/tst_test.h b/include/tst_test.h
index 0ac492a80..75c2109b9 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -210,6 +210,9 @@ struct tst_test {
 	/* Minimum size(MB) of MemAvailable required by the test */
 	unsigned long min_mem_avail;
 
+	/* Minimum size(MB) of SwapFree required by the test */
+	unsigned long min_swap_avail;
+
 	/*
 	 * Two policies for reserving hugepage:
 	 *
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index 6fc9f6a93..c5382ff10 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -95,6 +95,15 @@ long long tst_available_mem(void)
 	return mem_available;
 }
 
+long long tst_available_swap(void)
+{
+	unsigned long long swap_available = 0;
+
+	FILE_LINES_SCANF("/proc/meminfo", "SwapFree: %llu", &swap_available);
+
+	return swap_available;
+}
+
 static int has_caps(void)
 {
 	struct tst_cap_user_header hdr = {
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 0efabe467..3cc4aee0a 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -1198,6 +1198,9 @@ static void do_setup(int argc, char *argv[])
 	if (tst_test->min_mem_avail > (unsigned long)(tst_available_mem() / 1024))
 		tst_brk(TCONF, "Test needs at least %luMB MemAvailable", tst_test->min_mem_avail);
 
+	if (tst_test->min_swap_avail > (unsigned long)(tst_available_swap() / 1024))
+		tst_brk(TCONF, "Test needs at least %luMB SwapFree", tst_test->min_swap_avail);
+
 	if (tst_test->hugepages.number)
 		tst_reserve_hugepages(&tst_test->hugepages);
 
-- 
2.35.3



More information about the ltp mailing list