Re: [bpf-next v1 1/2] libbpf: introduce new API libbpf_num_online_cpus

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2023-01-17 12:49 UTC+0800 ~ tong@xxxxxxxxxxxxx
> From: Tonghao Zhang <tong@xxxxxxxxxxxxx>
> 
> Adding a new API libbpf_num_online_cpus() that helps user with
> fetching online CPUs number.
> 
> It's useful in system which number of online CPUs is different with
> possible CPUs.
> 
> Signed-off-by: Tonghao Zhang <tong@xxxxxxxxxxxxx>
> Cc: Quentin Monnet <quentin@xxxxxxxxxxxxx>
> Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
> Cc: Daniel Borkmann <daniel@xxxxxxxxxxxxx>
> Cc: Andrii Nakryiko <andrii@xxxxxxxxxx>
> Cc: Martin KaFai Lau <martin.lau@xxxxxxxxx>
> Cc: Song Liu <song@xxxxxxxxxx>
> Cc: Yonghong Song <yhs@xxxxxx>
> Cc: John Fastabend <john.fastabend@xxxxxxxxx>
> Cc: KP Singh <kpsingh@xxxxxxxxxx>
> Cc: Stanislav Fomichev <sdf@xxxxxxxxxx>
> Cc: Hao Luo <haoluo@xxxxxxxxxx>
> Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
> ---
>  tools/lib/bpf/libbpf.c   | 47 ++++++++++++++++++++++++++++++----------
>  tools/lib/bpf/libbpf.h   |  7 ++++++
>  tools/lib/bpf/libbpf.map |  1 +
>  3 files changed, 43 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 27d9faa80471..b84904f79ffd 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -12192,30 +12192,53 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
>  	return parse_cpu_mask_str(buf, mask, mask_sz);
>  }
>  
> -int libbpf_num_possible_cpus(void)
> +static int num_cpus(const char *fcpu)
>  {
> -	static const char *fcpu = "/sys/devices/system/cpu/possible";
> -	static int cpus;
> -	int err, n, i, tmp_cpus;
> +	int err, n, i, cpus;
>  	bool *mask;
>  
> -	tmp_cpus = READ_ONCE(cpus);
> -	if (tmp_cpus > 0)
> -		return tmp_cpus;
> -
>  	err = parse_cpu_mask_file(fcpu, &mask, &n);
>  	if (err)
>  		return libbpf_err(err);
>  
> -	tmp_cpus = 0;
> +	cpus = 0;
>  	for (i = 0; i < n; i++) {
>  		if (mask[i])
> -			tmp_cpus++;
> +			cpus++;
>  	}
>  	free(mask);
>  
> -	WRITE_ONCE(cpus, tmp_cpus);
> -	return tmp_cpus;
> +	return cpus;
> +}
> +
> +int libbpf_num_online_cpus(void)
> +{
> +	static int online_cpus;
> +	int cpus;
> +
> +	cpus = READ_ONCE(online_cpus);
> +	if (cpus > 0)
> +		return cpus;

The number of online CPUs can change over time, I don't think you can
READ_ONCE()/WRITE_ONCE().

> +
> +	cpus = num_cpus("/sys/devices/system/cpu/online");
> +
> +	WRITE_ONCE(online_cpus, cpus);
> +	return cpus;
> +}
> +
> +int libbpf_num_possible_cpus(void)
> +{
> +	static int possible_cpus;
> +	int cpus;
> +
> +	cpus = READ_ONCE(possible_cpus);
> +	if (cpus > 0)
> +		return cpus;
> +
> +	cpus = num_cpus("/sys/devices/system/cpu/possible");
> +
> +	WRITE_ONCE(possible_cpus, cpus);
> +	return cpus;
>  }
>  
>  static int populate_skeleton_maps(const struct bpf_object *obj,
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 898db26e42e9..e433575ff865 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -1332,6 +1332,13 @@ LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
>   */
>  LIBBPF_API int libbpf_num_possible_cpus(void);
>  
> +/**
> + * @brief **libbpf_num_online_cpus()** is a helper function to get the
> + * number of online CPUs that the host kernel supports and expects.
> + * @return number of online CPUs; or error code on failure
> + */
> +LIBBPF_API int libbpf_num_online_cpus(void);
> +
>  struct bpf_map_skeleton {
>  	const char *name;
>  	struct bpf_map **map;
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 11c36a3c1a9f..384fb6333f3f 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -381,6 +381,7 @@ LIBBPF_1.1.0 {
>  		user_ring_buffer__reserve;
>  		user_ring_buffer__reserve_blocking;
>  		user_ring_buffer__submit;
> +		libbpf_num_online_cpus;
>  } LIBBPF_1.0.0;

Libbpf v1.1.0 has shipped, so this would now go to the 1.2.0 block below.

>  
>  LIBBPF_1.2.0 {

Thanks, but I'm not sure retrieving the number of online CPUs, without
the list of those in use, is enough for fixing bpftool profiling
feature. Please see my answer on the second patch.

Quentin



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux