diff mbox series

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

Message ID 20231129060211.1890454-6-irogers@google.com (mailing list archive)
State New, archived
Headers show
Series Clean up libperf cpumap's empty function | expand

Commit Message

Ian Rogers Nov. 29, 2023, 6:02 a.m. UTC
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@google.com>
---
 tools/lib/perf/include/perf/cpumap.h | 6 ++++++
 tools/perf/builtin-record.c          | 4 +---
 2 files changed, 7 insertions(+), 3 deletions(-)

Comments

James Clark Dec. 12, 2023, 1:54 p.m. UTC | #1
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@google.com>

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

> ---
>  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;
diff mbox series

Patch

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;