From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it, Wei Gao <wegao@suse.com>
Subject: Re: [LTP] [PATCH 1/3] libswap: add two method to create swapfile
Date: Tue, 19 Mar 2024 16:38:31 +0800 [thread overview]
Message-ID: <CAEemH2c+gLkjP-3j+SnN=qvnLKZF4LMjCMxWtPd-6TpUM2i55w@mail.gmail.com> (raw)
In-Reply-To: <20240319070040.3239539-1-liwang@redhat.com>
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
prev parent reply other threads:[~2024-03-19 8:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-19 7:00 [LTP] [PATCH 1/3] libswap: add two method to create swapfile Li Wang
2024-03-19 7:00 ` [LTP] [PATCH 2/3] swapon/off: replace make_swapfile by new macro Li Wang
2024-03-19 7:00 ` [LTP] [PATCH 3/3] swapon: create 128MB swapfile Li Wang
2024-03-19 8:38 ` Li Wang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAEemH2c+gLkjP-3j+SnN=qvnLKZF4LMjCMxWtPd-6TpUM2i55w@mail.gmail.com' \
--to=liwang@redhat.com \
--cc=ltp@lists.linux.it \
--cc=wegao@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).