[LTP] [PATCH v2 2/7] libltpswap: alter get_used_swapfiles api

Yang Xu xuyang2018.jy@fujitsu.com
Fri Dec 22 06:00:01 CET 2023


Like we count the ipc resouce total, we can also add a similar api
for swapfiles, so we can use it for swapon03 case.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 include/libswap.h         |  7 +++++++
 libs/libltpswap/libswap.c | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/include/libswap.h b/include/libswap.h
index 2cab1047d..7f8df1032 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -27,4 +27,11 @@ void is_swap_supported(const char *filename);
  */
 unsigned int get_maxswapfiles(void);
 
+/*
+ * Get the used swapfiles number
+ */
+int get_used_swapfiles(const char *file, const int lineno);
+#define GET_USED_SWAPFILES() \
+	get_used_swapfiles(__FILE__, __LINE__)
+
 #endif /* __LIBSWAP_H__ */
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 658ecede7..e10a6f5b2 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -14,6 +14,8 @@
 #include "tst_kconfig.h"
 #include "tst_safe_stdio.h"
 
+#define BUFSIZE 1024
+
 /*
  * Make a swap file
  */
@@ -109,3 +111,27 @@ unsigned int get_maxswapfiles(void)
 
 	return max_swapfile - swp_migration_num - swp_hwpoison_num - swp_device_num - swp_pte_marker_num;
 }
+
+/*
+ * Get the used swapfiles number
+ */
+int get_used_swapfiles(const char *file, const int lineno)
+{
+	FILE *fp;
+	int used = -1;
+	char buf[BUFSIZE];
+
+	fp = safe_fopen(file, lineno, NULL, "/proc/swaps", "r");
+
+	while (fgets(buf, BUFSIZE, fp) != NULL)
+		used++;
+
+	fclose(fp);
+
+	if (used < 0) {
+		tst_brk(TBROK, "can't read /proc/swaps to get used swapfiles resource total "
+			"at %s:%d", file, lineno);
+	}
+
+	return used;
+}
-- 
2.27.0



More information about the ltp mailing list