[LTP] [RFC][PATCH 1/2] libs: Add safe_get_nodemap()

Cyril Hrubis chrubis@suse.cz
Mon Mar 31 18:00:19 CEST 2025


On Fri, Mar 28, 2025 at 12:43:10PM +0100, Petr Vorel wrote:
> This requires to add also tst_numa_type_name().
> 
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>  include/tst_numa.h   | 41 +++++++++++++++++++++++++++++++++++++++--
>  libs/numa/tst_numa.c | 14 ++++++++++++++
>  2 files changed, 53 insertions(+), 2 deletions(-)
> 
> diff --git a/include/tst_numa.h b/include/tst_numa.h
> index a1f9616300..191a57fa6b 100644
> --- a/include/tst_numa.h
> +++ b/include/tst_numa.h
> @@ -106,8 +106,30 @@ enum tst_numa_types {
>  };
>  
>  /**
> - * tst_get_nodemap() - Allocates and returns numa node map, which is an array of numa nodes which
> - * contain desired resources e.g. memory.
> + * tst_numa_type_name() - Convert enum tst_numa_types to a description of a NUMA type.
> + *
> + * @type Bitflags of enum tst_numa_types specifying desired resources.
> + *
> + * @return a string describing a NUMA type.
> + */
> +static inline const char *tst_numa_type_name(int type)
> +{
> +	if (type & ~(TST_NUMA_MEM))
> +		tst_brk(TBROK, "Invalid type %i\n", type);
> +
> +	switch (type) {
> +	case TST_NUMA_MEM:
> +		return " memory";
> +	case TST_NUMA_ANY:
> +		return "";
> +	default:
> +		return " unknown";
> +	}
> +}
> +
> +/**
> + * tst_get_nodemap() - Allocates and returns numa node map, which is an array of
> + * NUMA nodes which contain desired resources e.g. memory.
>   *
>   * @type:       Bitflags of enum tst_numa_types specifying desired resources.
>   * @min_mem_kb: Minimal free RAM on memory nodes, if given node has less than
> @@ -119,4 +141,19 @@ enum tst_numa_types {
>   */
>  struct tst_nodemap *tst_get_nodemap(int type, size_t min_mem_kb);
>  
> +/**
> + * safe_get_nodemap() - Calls tst_get_nodemap() with check for minimal required
> + * NUMA nodes. Call tst_brk(TCONF) if not enough NUMA nodes.
> + *
> + * @type:       Bitflags of enum tst_numa_types specifying desired resources.
> + * @min_mem_kb: Minimal free RAM on memory nodes, if given node has less than
> + *             requested amount of free+buffers memory it's not included in
> + *             the resulting list of nodes.
> + * @required:   Minimal number of a required NUMA nodes.
> + *
> + * return: On success returns allocated and initialized struct tst_nodemap which contains
> + *         array of numa node ids that contains desired resources.
> + */
> +struct tst_nodemap *safe_get_nodemap(int type, size_t min_mem_kb, size_t required);
> +
>  #endif /* TST_NUMA_H__ */
> diff --git a/libs/numa/tst_numa.c b/libs/numa/tst_numa.c
> index c3297013be..f004e29014 100644
> --- a/libs/numa/tst_numa.c
> +++ b/libs/numa/tst_numa.c
> @@ -221,4 +221,18 @@ struct tst_nodemap *tst_get_nodemap(int type, size_t min_mem_kb)
>  	return nodes;
>  }
>  
> +struct tst_nodemap *safe_get_nodemap(int type, size_t min_mem_kb, size_t required)
> +{
> +	struct tst_nodemap *nodes;
> +
> +	nodes = tst_get_nodemap(type, min_mem_kb);
> +
> +	if (nodes->cnt < required) {
> +		tst_brk(TCONF, "Test requires at least %zi NUMA%s node%s",
> +				required, tst_numa_type_name(type), required > 1 ? "s" : "");
                                                                     ^
				     It does not make any sense to
				     request a NUMA with at least 1
				     node. Every computer has at least 1
				     node.

It may make sense to TBROK if the minimal number of nodes <= 1


> +	}
> +
> +	return nodes;
> +}
> +
>  #endif
> -- 
> 2.49.0
> 

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list