Re: [PATCH v1 05/14] libperf cpumap: Add for_each_cpu that skips the "any CPU" case

From: James Clark
Date: Tue Dec 12 2023 - 08:54:34 EST




On 29/11/2023 06:02, Ian Rogers wrote:
> When iterating CPUs in a CPU map it is often desirable to skip the
> "any CPU" (aka dummy) case. Add a helper for this and use in
> builtin-record.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>

Reviewed-by: James Clark <james.clark@xxxxxxx>

> ---
> tools/lib/perf/include/perf/cpumap.h | 6 ++++++
> tools/perf/builtin-record.c | 4 +---
> 2 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h
> index 9cf361fc5edc..dbe0a7352b64 100644
> --- a/tools/lib/perf/include/perf/cpumap.h
> +++ b/tools/lib/perf/include/perf/cpumap.h
> @@ -64,6 +64,12 @@ LIBPERF_API bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map);
> (idx) < perf_cpu_map__nr(cpus); \
> (idx)++, (cpu) = perf_cpu_map__cpu(cpus, idx))
>
> +#define perf_cpu_map__for_each_cpu_skip_any(_cpu, idx, cpus) \
> + for ((idx) = 0, (_cpu) = perf_cpu_map__cpu(cpus, idx); \
> + (idx) < perf_cpu_map__nr(cpus); \
> + (idx)++, (_cpu) = perf_cpu_map__cpu(cpus, idx)) \
> + if ((_cpu).cpu != -1)
> +
> #define perf_cpu_map__for_each_idx(idx, cpus) \
> for ((idx) = 0; (idx) < perf_cpu_map__nr(cpus); (idx)++)
>
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 8ec818568662..066f9232e947 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -3580,9 +3580,7 @@ static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cp
> if (cpu_map__is_dummy(cpus))
> return 0;
>
> - perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
> - if (cpu.cpu == -1)
> - continue;
> + perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpus) {
> /* Return ENODEV is input cpu is greater than max cpu */
> if ((unsigned long)cpu.cpu > mask->nbits)
> return -ENODEV;