diff mbox series

[v4,2/7] libltpswap: alter tst_count_swaps api

Message ID 20240220074218.13487-2-xuyang2018.jy@fujitsu.com
State Superseded
Headers show
Series [v4,1/7] libltpswap: Add tst_max_swapfiles api | expand

Commit Message

Yang Xu Feb. 20, 2024, 7:42 a.m. UTC
Like we count the ipc resource 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         |  5 +++++
 libs/libltpswap/libswap.c | 27 +++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

Comments

Petr Vorel Feb. 23, 2024, 9:37 a.m. UTC | #1
Hi Yang Xu,

> Like we count the ipc resource total, we can also add a
nit: s/ipc/IPC/

> similar api for swapfiles, so we can use it for swapon03 case.
nit: s/api/API/

Reviewed-by: Petr Vorel <pvorel@suse.cz>

...
> +
> +/*
> + * Get the used swapfiles number
> + */
> +int tst_count_swaps(void)
> +{
> +	FILE *fp;
> +	int used = -1;
> +	char c;
> +
> +	fp = fopen("/proc/swaps", "r");
> +	if (fp == NULL) {
> +		return -1;
Shouldn't we tst_brk(TCONF, "missing /proc/swaps, no swap support?");
This can happen if CONFIG_SWAP is not configured. We have in swapon03.c:

	if (access("/proc/swaps", F_OK))
		tst_brk(TCONF, "swap not supported by kernel");

Once this patchset is solved, I'll add to this code to a function
is_swap_supported in libswap.c, which will be used in all swapo{n,ff}* tests.
(That will be better than require CONFIG_SWAP kconfig.)
Then we can use SAFE_FOPEN() here.

> +	}
> +
> +	while ((c = fgetc(fp)) != EOF) {
> +		if (c == '\n')
> +			used++;
So you read number of lines in /proc/swaps, -1 because file has header.
> +	}
> +
> +	fclose(fp);
Maybe SAFE_FCLOSE() ?

> +	if (used < 0) {
> +		tst_brk(TBROK, "can't read /proc/swaps to get used swapfiles resource total");
> +	}
nit: no need for { }

Kind regards,
Petr

> +
> +	return used;
> +}
diff mbox series

Patch

diff --git a/include/libswap.h b/include/libswap.h
index 361d73175..76a3ed0c0 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -27,4 +27,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 a404a4ada..1f9235f17 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -265,3 +265,30 @@  int tst_max_swapfiles(void)
 
 	return 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 = fopen("/proc/swaps", "r");
+	if (fp == NULL) {
+		return -1;
+	}
+
+	while ((c = fgetc(fp)) != EOF) {
+		if (c == '\n')
+			used++;
+	}
+
+	fclose(fp);
+	if (used < 0) {
+		tst_brk(TBROK, "can't read /proc/swaps to get used swapfiles resource total");
+	}
+
+	return used;
+}