[LTP] [PATCH 1/3] libswap: add two method to create swapfile
Li Wang
liwang@redhat.com
Tue Mar 19 09:38:31 CET 2024
On Tue, Mar 19, 2024 at 3:01 PM Li Wang <liwang@redhat.com> wrote:
> This introduces new functionality to the libswap library by adding
> two methods for creating a swapfile: SWAPFILE_BY_SIZE and
> SWAPFILE_BY_BLOCKS.
> The make_swapfile function is updated to accept an additional enum
> swapfile_method parameter to specify the creation method.
>
> Two macros, MAKE_SWAPFILE_SIZE and MAKE_SWAPFILE_BLKS, are defined
> to simplify the interface for creating swapfiles by size and by
> blocks respectively.
> ---
> include/libswap.h | 16 ++++++++++++++--
> libs/libltpswap/libswap.c | 30 ++++++++++++++++++++++--------
> 2 files changed, 36 insertions(+), 10 deletions(-)
>
> diff --git a/include/libswap.h b/include/libswap.h
> index 8c75e20ae..1dee907f0 100644
> --- a/include/libswap.h
> +++ b/include/libswap.h
> @@ -11,10 +11,22 @@
> #ifndef __LIBSWAP_H__
> #define __LIBSWAP_H__
>
> +enum swapfile_method {
> + SWAPFILE_BY_SIZE,
> + SWAPFILE_BY_BLOCKS
> +};
> +
> /*
> - * Make a swap file
> + * Create a swapfile of a specified size or number of blocks.
> */
> -int make_swapfile(const char *swapfile, int blocks, int safe);
> +int make_swapfile(const char *swapfile, unsigned int num,
> + int safe, enum swapfile_method method);
> +
> +#define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
> + make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> +
> +#define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
> + make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
>
> /*
> * Check swapon/swapoff support status of filesystems or files
> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index a26ea25e4..6a0f24ed2 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -133,23 +133,37 @@ out:
> return contiguous;
> }
>
> -int make_swapfile(const char *swapfile, int blocks, int safe)
> +int make_swapfile(const char *swapfile, unsigned int num, int safe, enum
> swapfile_method method)
> {
> struct statvfs fs_info;
> unsigned long blk_size, bs;
> size_t pg_size = sysconf(_SC_PAGESIZE);
> char mnt_path[100];
> + unsigned int blocks;
>
> if (statvfs(".", &fs_info) == -1)
> return -1;
>
> blk_size = fs_info.f_bsize;
>
> - /* To guarantee at least one page can be swapped out */
> - if (blk_size * blocks < pg_size)
> - bs = pg_size;
> - else
> - bs = blk_size;
>
> + switch (method) {
> + case SWAPFILE_BY_BLOCKS:
> + blocks = num;
> + if (blk_size * blocks < pg_size)
> + bs = pg_size;
> + else
> + bs = blk_size;
> + break;
> + case SWAPFILE_BY_SIZE:
> + if (num * 1024 * 1024 < pg_size)
> + bs = pg_size;
> + else
> + bs = blk_size;
> + blocks = (num * 1024 * 1024) / blk_size;
> + break;
> + default:
> + return -1;
> + }
>
Obviously, this part is too ugly, I would take the way Wei posted in
another thread, thanks.
Something like:
+ if (method == SWAPFILE_BY_SIZE)
+ blocks = num * 1024 * 1024 / blk_size;
+ else if (method == SWAPFILE_BY_BLOCKS)
+ blocks = num;
+ else
+ tst_brk(TBROK, "Invalide method.");
>
> if (sscanf(swapfile, "%[^/]", mnt_path) != 1)
> tst_brk(TBROK, "sscanf failed");
> @@ -175,13 +189,13 @@ int make_swapfile(const char *swapfile, int blocks,
> int safe)
> argv[2] = NULL;
>
> return tst_cmd(argv, "/dev/null", "/dev/null", safe ?
> - TST_CMD_PASS_RETVAL |
> TST_CMD_TCONF_ON_MISSING : 0);
> + TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING :
> 0);
> }
>
> bool is_swap_supported(const char *filename)
> {
> int i, sw_support = 0;
> - int ret = make_swapfile(filename, 10, 1);
> + int ret = MAKE_SWAPFILE_BLKS(filename, 10, 1);
> int fi_contiguous = file_is_contiguous(filename);
> long fs_type = tst_fs_type(filename);
> const char *fstype = tst_fs_type_name(fs_type);
> --
> 2.40.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>
--
Regards,
Li Wang
More information about the ltp
mailing list