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

Cyril Hrubis chrubis@suse.cz
Wed Jan 3 16:30:35 CET 2024


Hi!
> +/*
> + * Get the used swapfiles number
> + */
> +int get_used_swapfiles(const char *file, const int lineno);
> +#define GET_USED_SWAPFILES() \
> +	get_used_swapfiles(__FILE__, __LINE__)

This has to be prefixed with tst_

Also I wouldn't bother withg the macro and passing down FILE and LINE,
it's going to be called just once in the test setup anyways.

So I would just add:

	int tst_count_swaps(void);

>  #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");

I suppose that that we may want to check if the file exists and return 0
if it does not, otherwise we will possibly TBROK here if CONFIG_SWAP is
not set. Or do all the tests that call this function have CONFIG_SWAP in
.needs_kconfig?

> +	while (fgets(buf, BUFSIZE, fp) != NULL)
> +		used++;

We can do this even simpler:

	int c;

	while ((c = fgetc(f)) != EOF) {
		if (c == '\n')
			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
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list