[LTP] [PATCH v4 2/7] libltpswap: alter tst_count_swaps api

Petr Vorel pvorel@suse.cz
Fri Feb 23 10:37:18 CET 2024


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;
> +}


More information about the ltp mailing list