diff mbox series

[1/3] libltpswap: Add get_maxswapfiles api

Message ID 20231205061639.68656-1-xuyang2018.jy@fujitsu.com
State Superseded
Headers show
Series [1/3] libltpswap: Add get_maxswapfiles api | expand

Commit Message

Yang Xu \(Fujitsu\) Dec. 5, 2023, 6:16 a.m. UTC
Current, the kernel constant MAX_SWAPFILES value is calculated as below
------------------------------------
//#ifdef CONFIG_DEVICE_PRIVATE
//#define SWP_DEVICE_NUM 4
//#else
//#define SWP_DEVICE_NUM 0
//#endif

//#ifdef CONFIG_MIGRATION
//#define SWP_MIGRATION_NUM 3
//#else
//#define SWP_MIGRATION_NUM 0

//#ifdef CONFIG_MEMORY_FAILURE
//#define SWP_HWPOISON_NUM 1
//#else
//#define SWP_HWPOISON_NUM 0
//#endif

//#define SWP_PTE_MARKER_NUM 1
//#define MAX_SWAPFILES_SHIFT	5

//#define MAX_SWAPFILES \
//	((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
//	SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \
//	SWP_PTE_MARKER_NUM)
------------------------------------

Also, man-pages missed something after 5.14 kernel. I have sent two patches to
add it.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 include/libswap.h         |  6 ++++++
 libs/libltpswap/libswap.c | 41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

Comments

Yang Xu \(Fujitsu\) Dec. 15, 2023, 6:04 a.m. UTC | #1
Hi all,

Ping

Best Regards
Yang Xu

> Current, the kernel constant MAX_SWAPFILES value is calculated as below
> ------------------------------------
> //#ifdef CONFIG_DEVICE_PRIVATE
> //#define SWP_DEVICE_NUM 4
> //#else
> //#define SWP_DEVICE_NUM 0
> //#endif
> 
> //#ifdef CONFIG_MIGRATION
> //#define SWP_MIGRATION_NUM 3
> //#else
> //#define SWP_MIGRATION_NUM 0
> 
> //#ifdef CONFIG_MEMORY_FAILURE
> //#define SWP_HWPOISON_NUM 1
> //#else
> //#define SWP_HWPOISON_NUM 0
> //#endif
> 
> //#define SWP_PTE_MARKER_NUM 1
> //#define MAX_SWAPFILES_SHIFT	5
> 
> //#define MAX_SWAPFILES \
> //	((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
> //	SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \
> //	SWP_PTE_MARKER_NUM)
> ------------------------------------
> 
> Also, man-pages missed something after 5.14 kernel. I have sent two patches to
> add it.
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
>   include/libswap.h         |  6 ++++++
>   libs/libltpswap/libswap.c | 41 +++++++++++++++++++++++++++++++++++++++
>   2 files changed, 47 insertions(+)
> 
> diff --git a/include/libswap.h b/include/libswap.h
> index d4b5301a5..2cab1047d 100644
> --- a/include/libswap.h
> +++ b/include/libswap.h
> @@ -21,4 +21,10 @@ int make_swapfile(const char *swapfile, int safe);
>    * we are testing on.
>    */
>   void is_swap_supported(const char *filename);
> +
> +/*
> + * Get kernel constant MAX_SWAPFILES value
> + */
> +unsigned int get_maxswapfiles(void);
> +
>   #endif /* __LIBSWAP_H__ */
> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index a4427736f..54317c627 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -11,6 +11,7 @@
>   #include "tst_test.h"
>   #include "libswap.h"
>   #include "lapi/syscalls.h"
> +#include "tst_kconfig.h"
>   
>   /*
>    * Make a swap file
> @@ -63,3 +64,43 @@ void is_swap_supported(const char *filename)
>   	if (TST_RET == -1)
>   		tst_brk(TFAIL | TTERRNO, "swapoff on %s failed", fstype);
>   }
> +
> +/*
> + * Get kernel constant MAX_SWAPFILES value
> + */
> +unsigned int get_maxswapfiles(void)
> +{
> +	unsigned int max_swapfile = 32;
> +	unsigned int swp_migration_num = 0, swp_hwpoison_num = 0, swp_device_num = 0, swp_pte_marker_num = 0;
> +	struct tst_kconfig_var migration_kconfig = TST_KCONFIG_INIT("CONFIG_MIGRATION");
> +	struct tst_kconfig_var memory_kconfig = TST_KCONFIG_INIT("CONFIG_MEMORY_FAILURE");
> +	struct tst_kconfig_var device_kconfig = TST_KCONFIG_INIT("CONFIG_DEVICE_PRIVATE");
> +	struct tst_kconfig_var marker_kconfig = TST_KCONFIG_INIT("CONFIG_PTE_MARKER");
> +
> +	tst_kconfig_read(&migration_kconfig, 1);
> +	tst_kconfig_read(&memory_kconfig, 1);
> +	tst_kconfig_read(&device_kconfig, 1);
> +	tst_kconfig_read(&marker_kconfig, 1);
> +
> +	if (migration_kconfig.choice == 'y') {
> +		if (tst_kvercmp(5, 19, 0) < 0)
> +			swp_migration_num = 2;
> +		else
> +			swp_migration_num = 3;
> +	}
> +
> +	if (memory_kconfig.choice == 'y')
> +		swp_hwpoison_num = 1;
> +
> +	if (device_kconfig.choice == 'y') {
> +		if (tst_kvercmp(5, 14, 0) >= 0)
> +			swp_device_num = 4;
> +	}
> +
> +	if (marker_kconfig.choice == 'y') {
> +		if (tst_kvercmp(5, 19, 0) < 0)
> +			swp_pte_marker_num = 1;
> +	}
> +
> +	return max_swapfile - swp_migration_num - swp_hwpoison_num - swp_device_num - swp_pte_marker_num;
> +}
Cyril Hrubis Jan. 3, 2024, 2:53 p.m. UTC | #2
Hi!
> +unsigned int get_maxswapfiles(void)
> +{
> +	unsigned int max_swapfile = 32;
> +	unsigned int swp_migration_num = 0, swp_hwpoison_num = 0, swp_device_num = 0, swp_pte_marker_num = 0;
> +	struct tst_kconfig_var migration_kconfig = TST_KCONFIG_INIT("CONFIG_MIGRATION");
> +	struct tst_kconfig_var memory_kconfig = TST_KCONFIG_INIT("CONFIG_MEMORY_FAILURE");
> +	struct tst_kconfig_var device_kconfig = TST_KCONFIG_INIT("CONFIG_DEVICE_PRIVATE");
> +	struct tst_kconfig_var marker_kconfig = TST_KCONFIG_INIT("CONFIG_PTE_MARKER");
> +
> +	tst_kconfig_read(&migration_kconfig, 1);
> +	tst_kconfig_read(&memory_kconfig, 1);
> +	tst_kconfig_read(&device_kconfig, 1);
> +	tst_kconfig_read(&marker_kconfig, 1);

This API is designed so that we can pass an array and parse all values
in a single call. So this should be done as:

	struct tst_kconfig_var kconfig[] = {
		TST_KCONFIG_INIT("CONFIG_MIGRATION"),
		TST_KCONFIG_INIT("CONFIG_MEMORY_FAILURE"),
		...
	};

	tst_kconfig_read(kconfig, ARRAY_SIZE(kconfigs));

If you want to have a nice indexes into that array, you can create an
enum as:

	enum cfg_idx {
		CFG_MIGRATION,
		CFG_MEMORY_FAILURE,
		...
	};

Then use them in the array initialization to make sure they match:

	struct tst_kconfig_var kconfig[] = {
		[CFG_MIGRATION] = TST_KCONFIG_INIT("CONFIG_MIGRATION"),
		...
	};

And finally we can use these as:

	if (kconfig[CFG_MIGRATION].choice == 'y')


I guess that this is quite cumbersome to use, maybe we need optional
pointer in the tst_kconfig_var structure so we can pass a pointer to a
char that would be set to the value of choice then we could do:

	char migration_choice;

	struct tst_kconfig_var kconfig[] = {
		TST_KCONFIG_INIT2("CONFIG_MIGRATION", &migration_choice),
		...
	};

	if (migration_choice == 'y')
		...
diff mbox series

Patch

diff --git a/include/libswap.h b/include/libswap.h
index d4b5301a5..2cab1047d 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -21,4 +21,10 @@  int make_swapfile(const char *swapfile, int safe);
  * we are testing on.
  */
 void is_swap_supported(const char *filename);
+
+/*
+ * Get kernel constant MAX_SWAPFILES value
+ */
+unsigned int get_maxswapfiles(void);
+
 #endif /* __LIBSWAP_H__ */
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index a4427736f..54317c627 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -11,6 +11,7 @@ 
 #include "tst_test.h"
 #include "libswap.h"
 #include "lapi/syscalls.h"
+#include "tst_kconfig.h"
 
 /*
  * Make a swap file
@@ -63,3 +64,43 @@  void is_swap_supported(const char *filename)
 	if (TST_RET == -1)
 		tst_brk(TFAIL | TTERRNO, "swapoff on %s failed", fstype);
 }
+
+/*
+ * Get kernel constant MAX_SWAPFILES value
+ */
+unsigned int get_maxswapfiles(void)
+{
+	unsigned int max_swapfile = 32;
+	unsigned int swp_migration_num = 0, swp_hwpoison_num = 0, swp_device_num = 0, swp_pte_marker_num = 0;
+	struct tst_kconfig_var migration_kconfig = TST_KCONFIG_INIT("CONFIG_MIGRATION");
+	struct tst_kconfig_var memory_kconfig = TST_KCONFIG_INIT("CONFIG_MEMORY_FAILURE");
+	struct tst_kconfig_var device_kconfig = TST_KCONFIG_INIT("CONFIG_DEVICE_PRIVATE");
+	struct tst_kconfig_var marker_kconfig = TST_KCONFIG_INIT("CONFIG_PTE_MARKER");
+
+	tst_kconfig_read(&migration_kconfig, 1);
+	tst_kconfig_read(&memory_kconfig, 1);
+	tst_kconfig_read(&device_kconfig, 1);
+	tst_kconfig_read(&marker_kconfig, 1);
+
+	if (migration_kconfig.choice == 'y') {
+		if (tst_kvercmp(5, 19, 0) < 0)
+			swp_migration_num = 2;
+		else
+			swp_migration_num = 3;
+	}
+
+	if (memory_kconfig.choice == 'y')
+		swp_hwpoison_num = 1;
+
+	if (device_kconfig.choice == 'y') {
+		if (tst_kvercmp(5, 14, 0) >= 0)
+			swp_device_num = 4;
+	}
+
+	if (marker_kconfig.choice == 'y') {
+		if (tst_kvercmp(5, 19, 0) < 0)
+			swp_pte_marker_num = 1;
+	}
+
+	return max_swapfile - swp_migration_num - swp_hwpoison_num - swp_device_num - swp_pte_marker_num;
+}