[LTP] [PATCH 1/3] libltpswap: Add get_maxswapfiles api

Cyril Hrubis chrubis@suse.cz
Wed Jan 3 15:53:49 CET 2024


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')
		...

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list