All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Yang Xu <xuyang2018.jy@fujitsu.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2 2/7] libltpswap: alter get_used_swapfiles api
Date: Wed, 3 Jan 2024 16:30:35 +0100	[thread overview]
Message-ID: <ZZV9m0C1dn72aKUY@yuki> (raw)
In-Reply-To: <20231222050006.148845-2-xuyang2018.jy@fujitsu.com>

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2024-01-03 15:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05  6:16 [LTP] [PATCH 1/3] libltpswap: Add get_maxswapfiles api Yang Xu
2023-12-05  6:16 ` [LTP] [PATCH 2/3] syscalls/swapon03: Use get_maxswapfiles() api instead of hard code Yang Xu
2023-12-22  1:39   ` Yang Xu (Fujitsu)
2023-12-22 11:42     ` Petr Vorel
2023-12-05  6:16 ` [LTP] [PATCH 3/3] swaponoff.h: Remove useless header Yang Xu
2023-12-22  5:00   ` [LTP] [PATCH v2 1/7] libltpswap: Add get_maxswapfiles api Yang Xu
2023-12-22  5:00     ` [LTP] [PATCH v2 2/7] libltpswap: alter get_used_swapfiles api Yang Xu
2024-01-03 15:30       ` Cyril Hrubis [this message]
2023-12-22  5:00     ` [LTP] [PATCH v2 3/7] syscalls/swapon03: use get_maxswapfiles() and GET_USED_SWAPFILES() api Yang Xu
2024-01-03 15:34       ` Cyril Hrubis
2023-12-22  5:00     ` [LTP] [PATCH v2 4/7] swaponoff.h: Remove useless header Yang Xu
2023-12-22  5:00     ` [LTP] [PATCH v2 5/7] swapon/Makefile: Remove useless section for MAX_SWAPFILES Yang Xu
2024-01-03 15:40       ` Cyril Hrubis
2023-12-22  5:00     ` [LTP] [PATCH v2 6/7] syscalls/swapon03: Simply this case Yang Xu
2023-12-22  5:00     ` [LTP] [PATCH v2 7/7] Add fallback for RHEL9 Yang Xu
2024-02-05 18:37       ` Petr Vorel
2024-01-02  2:07     ` [LTP] [PATCH v2 1/7] libltpswap: Add get_maxswapfiles api Yang Xu (Fujitsu)
2024-01-03 15:35     ` Cyril Hrubis
2024-02-05 18:43     ` Petr Vorel
2024-02-06  8:55       ` Yang Xu (Fujitsu) via ltp
2023-12-15  6:04 ` [LTP] [PATCH 1/3] " Yang Xu (Fujitsu)
2024-01-03 14:53 ` Cyril Hrubis

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=ZZV9m0C1dn72aKUY@yuki \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=xuyang2018.jy@fujitsu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.