[LTP] [PATCH v5 2/7] libltpswap: alter tst_count_swaps API

Yang Xu xuyang2018.jy@fujitsu.com
Mon Feb 26 14:53:31 CET 2024


Like we count the IPC resource total, we can also add a
similar API for swapfiles, so we can use it for swapon03 case.

Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 include/libswap.h         |  5 +++++
 libs/libltpswap/libswap.c | 25 +++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/include/libswap.h b/include/libswap.h
index cc68d5e6b..8c75e20ae 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -28,4 +28,9 @@ bool is_swap_supported(const char *filename);
  */
 int tst_max_swapfiles(void);
 
+/*
+ * Get the used swapfiles number.
+ */
+int tst_count_swaps(void);
+
 #endif /* __LIBSWAP_H__ */
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 3528a3fb9..5e0c79b8f 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -261,3 +261,28 @@ int tst_max_swapfiles(void)
 
 	return DEFAULT_MAX_SWAPFILE - swp_migration_num - swp_hwpoison_num - swp_device_num - swp_pte_marker_num;
 }
+
+/*
+ * Get the used swapfiles number.
+ */
+int tst_count_swaps(void)
+{
+	FILE *fp;
+	int used = -1;
+	char c;
+
+	fp = SAFE_FOPEN("/proc/swaps", "r");
+	if (fp == NULL)
+		return -1;
+
+	while ((c = fgetc(fp)) != EOF) {
+		if (c == '\n')
+			used++;
+	}
+
+	SAFE_FCLOSE(fp);
+	if (used < 0)
+		tst_brk(TBROK, "can't read /proc/swaps to get used swapfiles resource total");
+
+	return used;
+}
-- 
2.39.3



More information about the ltp mailing list