Skip to contentSkip to conversation

libperf: Fix perf_cpu_map__for_each_cpu macro

PATCH Applied Jiri Olsa 3 participants 3 messages Feb 15, 2022 perf events

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 libperf: Fix perf_cpu_map__for_each_cpu macro

Tzvetomir Stoyanov reported an issue with using macro perf_cpu_map__for_each_cpu using private perf_cpu object.

The issue is caused by recent change that wrapped cpu in struct perf_cpu to distinguish it from cpu indexes. We need to make struct perf_cpu public.

Adding simple test for using perf_cpu_map__for_each_cpu macro.

Fixes 6d18804b963b ("perf cpumap: Give CPUs their own type")
---
 tools/lib/perf/include/internal/cpumap.h |  6 +-----
 tools/lib/perf/include/perf/cpumap.h     |  5 +++++
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-cpumap.c       | 11 +++++++++++
 4 files changed, 18 insertions(+), 5 deletions(-)
tools/lib/perf/include/internal/cpumap.h +1 -5
index 581f9ffb4237..1973a18c096b 100644
@@ -3,11 +3,7 @@
 #define __LIBPERF_INTERNAL_CPUMAP_H
 
 #include <linux/refcount.h>
-
-/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
-struct perf_cpu {
-	int cpu;
-};
+#include <perf/cpumap.h>
 
 /**
  * A sized, reference counted, sorted array of integers representing CPU
tools/lib/perf/include/perf/cpumap.h +5 -0
index 15b8faafd615..4a2edbdb5e2b 100644
@@ -7,6 +7,11 @@
 #include <stdio.h>
 #include <stdbool.h>
 
+/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
+struct perf_cpu {
+	int cpu;
+};
+
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
tools/lib/perf/libperf.map +1 -0
index 93696affda2e..6fa0d651576b 100644
@@ -2,6 +2,7 @@ LIBPERF_0.0.1 {
 	global:
 		libperf_init;
 		perf_cpu_map__dummy_new;
+		perf_cpu_map__default_new;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
 		perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +11 -0
index d39378eaf897..87b0510a556f 100644
@@ -14,6 +14,8 @@ static int libperf_print(enum libperf_print_level level,
 int test_cpumap(int argc, char **argv)
 {
 	struct perf_cpu_map *cpus;
+	struct perf_cpu cpu;
+	int idx;
 
 	__T_START;
 
@@ -27,6 +29,15 @@ int test_cpumap(int argc, char **argv)
 	perf_cpu_map__put(cpus);
 	perf_cpu_map__put(cpus);
 
+	cpus = perf_cpu_map__default_new();
+	if (!cpus)
+		return -1;
+
+	perf_cpu_map__for_each_cpu(cpu, idx, cpus)
+		__T("wrong cpu number", cpu.cpu != -1);
+
+	perf_cpu_map__put(cpus);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
Signature · 1 line
2.35.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jiri Olsa

Tzvetomir Stoyanov reported an issue with using macro perf_cpu_map__for_each_cpu using private perf_cpu object.

The issue is caused by recent change that wrapped cpu in struct perf_cpu to distinguish it from cpu indexes. We need to make struct perf_cpu public.

Quoted 6 lines

Adding simple test for using perf_cpu_map__for_each_cpu macro.

Fixes 6d18804b963b ("perf cpumap: Give CPUs their own type")

Thanks,
Ian

Quoted 84 lines
---
 tools/lib/perf/include/internal/cpumap.h |  6 +-----
 tools/lib/perf/include/perf/cpumap.h     |  5 +++++
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-cpumap.c       | 11 +++++++++++
 4 files changed, 18 insertions(+), 5 deletions(-)
tools/lib/perf/include/internal/cpumap.h +1 -5
index 581f9ffb4237..1973a18c096b 100644
@@ -3,11 +3,7 @@
 #define __LIBPERF_INTERNAL_CPUMAP_H
 
 #include <linux/refcount.h>
-
-/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
-struct perf_cpu {
-       int cpu;
-};
+#include <perf/cpumap.h>
 
 /**
  * A sized, reference counted, sorted array of integers representing CPU
tools/lib/perf/include/perf/cpumap.h +5 -0
index 15b8faafd615..4a2edbdb5e2b 100644
@@ -7,6 +7,11 @@
 #include <stdio.h>
 #include <stdbool.h>
 
+/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
+struct perf_cpu {
+       int cpu;
+};
+
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
tools/lib/perf/libperf.map +1 -0
index 93696affda2e..6fa0d651576b 100644
@@ -2,6 +2,7 @@ LIBPERF_0.0.1 {
        global:
                libperf_init;
                perf_cpu_map__dummy_new;
+               perf_cpu_map__default_new;
                perf_cpu_map__get;
                perf_cpu_map__put;
                perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +11 -1
index d39378eaf897..87b0510a556f 100644
@@ -14,6 +14,8 @@ static int libperf_print(enum libperf_print_level level,
 int test_cpumap(int argc, char **argv)
 {
        struct perf_cpu_map *cpus;
+       struct perf_cpu cpu;
+       int idx;
 
        __T_START;
 
@@ -27,6 +29,15 @@ int test_cpumap(int argc, char **argv)
        perf_cpu_map__put(cpus);
        perf_cpu_map__put(cpus);
 
+       cpus = perf_cpu_map__default_new();
+       if (!cpus)
+               return -1;
+
+       perf_cpu_map__for_each_cpu(cpu, idx, cpus)
+               __T("wrong cpu number", cpu.cpu != -1);
+
+       perf_cpu_map__put(cpus);
+
        __T_END;
        return tests_failed == 0 ? 0 : -1;
 }
--

2.35.1

maintainer Applied
#2 lore raw

Em Tue, Feb 15, 2022 at 09:12:57AM -0800, Ian Rogers escreveu:

Quoted 9 lines · Jiri Olsa
Quoted 8 lines

Tzvetomir Stoyanov reported an issue with using macro perf_cpu_map__for_each_cpu using private perf_cpu object.

The issue is caused by recent change that wrapped cpu in struct perf_cpu to distinguish it from cpu indexes. We need to make struct perf_cpu public.

Quoted 8 lines
Quoted 6 lines

Adding simple test for using perf_cpu_map__for_each_cpu macro.

Fixes 6d18804b963b ("perf cpumap: Give CPUs their own type")

Thanks, applied.

- Arnaldo

Quoted 87 lines

Thanks,
Ian

Quoted 84 lines
---
 tools/lib/perf/include/internal/cpumap.h |  6 +-----
 tools/lib/perf/include/perf/cpumap.h     |  5 +++++
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-cpumap.c       | 11 +++++++++++
 4 files changed, 18 insertions(+), 5 deletions(-)
tools/lib/perf/include/internal/cpumap.h +1 -5
index 581f9ffb4237..1973a18c096b 100644
@@ -3,11 +3,7 @@
 #define __LIBPERF_INTERNAL_CPUMAP_H
 
 #include <linux/refcount.h>
-
-/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
-struct perf_cpu {
-       int cpu;
-};
+#include <perf/cpumap.h>
 
 /**
  * A sized, reference counted, sorted array of integers representing CPU
tools/lib/perf/include/perf/cpumap.h +5 -0
index 15b8faafd615..4a2edbdb5e2b 100644
@@ -7,6 +7,11 @@
 #include <stdio.h>
 #include <stdbool.h>
 
+/** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
+struct perf_cpu {
+       int cpu;
+};
+
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
tools/lib/perf/libperf.map +1 -0
index 93696affda2e..6fa0d651576b 100644
@@ -2,6 +2,7 @@ LIBPERF_0.0.1 {
        global:
                libperf_init;
                perf_cpu_map__dummy_new;
+               perf_cpu_map__default_new;
                perf_cpu_map__get;
                perf_cpu_map__put;
                perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +11 -1
index d39378eaf897..87b0510a556f 100644
@@ -14,6 +14,8 @@ static int libperf_print(enum libperf_print_level level,
 int test_cpumap(int argc, char **argv)
 {
        struct perf_cpu_map *cpus;
+       struct perf_cpu cpu;
+       int idx;
 
        __T_START;
 
@@ -27,6 +29,15 @@ int test_cpumap(int argc, char **argv)
        perf_cpu_map__put(cpus);
        perf_cpu_map__put(cpus);
 
+       cpus = perf_cpu_map__default_new();
+       if (!cpus)
+               return -1;
+
+       perf_cpu_map__for_each_cpu(cpu, idx, cpus)
+               __T("wrong cpu number", cpu.cpu != -1);
+
+       perf_cpu_map__put(cpus);
+
        __T_END;
        return tests_failed == 0 ? 0 : -1;
 }
--

2.35.1

Signature · 1 line
- Arnaldo