Skip to contentSkip to conversation

Clean up libperf cpumap's empty function

PATCH Applied Ian Rogers 5 participants 45 messages Nov 29, 2023 – Feb 1, 2024

Subsystems (5)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover Clean up libperf cpumap's empty function

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case libperf cpumap: Add any, empty and min helpers
perf arm-spe/cs-etm: Directly iterate CPU maps
perf intel-pt/intel-bts: Switch perf_cpu_map__has_any_cpu_or_is_empty
use
perf cpumap: Clean up use of perf_cpu_map__has_any_cpu_or_is_empty perf top: Avoid repeated function calls
perf arm64 header: Remove unnecessary CPU map get and put perf stat: Remove duplicate cpus_map_matched function
perf cpumap: Use perf_cpu_map__for_each_cpu when possible libperf cpumap: Document perf_cpu_map__nr's behavior

 .../perf/Documentation/examples/sampling.c    |  2 +-
 .../perf/Documentation/libperf-sampling.txt   |  2 +-
 tools/lib/perf/Documentation/libperf.txt      |  4 +-
 tools/lib/perf/cpumap.c                       | 92 +++++++++++++------
 tools/lib/perf/evlist.c                       |  6 +-
 tools/lib/perf/evsel.c                        |  2 +-
 tools/lib/perf/include/perf/cpumap.h          | 56 ++++++++++-
 tools/lib/perf/libperf.map                    | 10 +-
 tools/lib/perf/tests/test-cpumap.c            |  4 +-
 tools/lib/perf/tests/test-evlist.c            |  6 +-
 tools/lib/perf/tests/test-evsel.c             |  2 +-
 tools/perf/arch/arm/util/cs-etm.c             | 83 +++++++----------
 tools/perf/arch/arm64/util/arm-spe.c          |  4 +-
 tools/perf/arch/arm64/util/header.c           | 15 +--
 tools/perf/arch/x86/util/intel-bts.c          |  4 +-
 tools/perf/arch/x86/util/intel-pt.c           | 10 +-
 tools/perf/bench/epoll-ctl.c                  |  2 +-
 tools/perf/bench/epoll-wait.c                 |  2 +-
 tools/perf/bench/futex-hash.c                 |  2 +-
 tools/perf/bench/futex-lock-pi.c              |  2 +-
 tools/perf/bench/futex-requeue.c              |  2 +-
 tools/perf/bench/futex-wake-parallel.c        |  2 +-
 tools/perf/bench/futex-wake.c                 |  2 +-
 tools/perf/builtin-c2c.c                      |  6 +-
 tools/perf/builtin-ftrace.c                   |  2 +-
 tools/perf/builtin-record.c                   |  4 +-
 tools/perf/builtin-stat.c                     | 31 +------
 tools/perf/tests/bitmap.c                     | 13 +--
 tools/perf/tests/code-reading.c               |  2 +-
 tools/perf/tests/cpumap.c                     |  2 +-
 tools/perf/tests/keep-tracking.c              |  2 +-
 tools/perf/tests/mmap-basic.c                 |  2 +-
 tools/perf/tests/openat-syscall-all-cpus.c    |  2 +-
 tools/perf/tests/perf-time-to-tsc.c           |  2 +-
 tools/perf/tests/sw-clock.c                   |  2 +-
 tools/perf/tests/switch-tracking.c            |  2 +-
 tools/perf/tests/task-exit.c                  |  2 +-
 tools/perf/tests/topology.c                   | 48 +++++-----
 tools/perf/util/auxtrace.c                    |  4 +-
 tools/perf/util/bpf_counter.c                 |  2 +-
 tools/perf/util/bpf_kwork.c                   | 16 ++--
 tools/perf/util/bpf_kwork_top.c               | 12 +--
 tools/perf/util/cpumap.c                      | 14 ++-
 tools/perf/util/cputopo.c                     |  2 +-
 tools/perf/util/evlist.c                      |  4 +-
 tools/perf/util/evsel.c                       |  2 +-
 tools/perf/util/perf_api_probe.c              |  4 +-
 tools/perf/util/record.c                      |  4 +-
 .../scripting-engines/trace-event-python.c    | 12 ++-
 tools/perf/util/session.c                     |  5 +-
 tools/perf/util/stat.c                        |  2 +-
 tools/perf/util/svghelper.c                   | 20 ++--
 tools/perf/util/top.c                         |  9 +-
 53 files changed, 296 insertions(+), 254 deletions(-)
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
author reviewer
#1 lore raw

PATCH 1/14 libperf cpumap: Rename perf_cpu_map__dummy_new

Rename perf_cpu_map__dummy_new to perf_cpu_map__new_any_cpu to better indicate this is creating a CPU map for the perf_event_open "any" CPU case.

---
 tools/lib/perf/Documentation/libperf.txt | 2 +-
 tools/lib/perf/cpumap.c                  | 4 ++--
 tools/lib/perf/evsel.c                   | 2 +-
 tools/lib/perf/include/perf/cpumap.h     | 4 ++--
 tools/lib/perf/libperf.map               | 2 +-
 tools/lib/perf/tests/test-cpumap.c       | 2 +-
 tools/lib/perf/tests/test-evlist.c       | 2 +-
 tools/perf/tests/cpumap.c                | 2 +-
 tools/perf/tests/sw-clock.c              | 2 +-
 tools/perf/tests/task-exit.c             | 2 +-
 tools/perf/util/evlist.c                 | 2 +-
 tools/perf/util/evsel.c                  | 2 +-
 12 files changed, 14 insertions(+), 14 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -1
index a8f1a237931b..a256a26598b0 100644
@@ -37,7 +37,7 @@ SYNOPSIS
 
   struct perf_cpu_map;
 
-  struct perf_cpu_map *perf_cpu_map__dummy_new(void);
+  struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
   struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
   struct perf_cpu_map *perf_cpu_map__read(FILE *file);
   struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/cpumap.c +2 -2
index 2a5a29217374..2bd6aba3d8c9 100644
@@ -27,7 +27,7 @@ struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus)
 	return result;
 }
 
-struct perf_cpu_map *perf_cpu_map__dummy_new(void)
+struct perf_cpu_map *perf_cpu_map__new_any_cpu(void)
 {
 	struct perf_cpu_map *cpus = perf_cpu_map__alloc(1);
 
@@ -271,7 +271,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	else if (*cpu_list != '\0')
 		cpus = cpu_map__default_new();
 	else
-		cpus = perf_cpu_map__dummy_new();
+		cpus = perf_cpu_map__new_any_cpu();
 invalid:
 	free(tmp_cpus);
 out:
tools/lib/perf/evsel.c +1 -1
index 8b51b008a81f..c07160953224 100644
@@ -120,7 +120,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
 		static struct perf_cpu_map *empty_cpu_map;
 
 		if (empty_cpu_map == NULL) {
-			empty_cpu_map = perf_cpu_map__dummy_new();
+			empty_cpu_map = perf_cpu_map__new_any_cpu();
 			if (empty_cpu_map == NULL)
 				return -ENOMEM;
 		}
tools/lib/perf/include/perf/cpumap.h +2 -2
index e38d859a384d..d0bf218ada11 100644
@@ -19,9 +19,9 @@ struct perf_cache {
 struct perf_cpu_map;
 
 /**
- * perf_cpu_map__dummy_new - a map with a singular "any CPU"/dummy -1 value.
+ * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(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);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
tools/lib/perf/libperf.map +1 -1
index 190b56ae923a..a8ff64baea3e 100644
@@ -1,7 +1,7 @@
 LIBPERF_0.0.1 {
 	global:
 		libperf_init;
-		perf_cpu_map__dummy_new;
+		perf_cpu_map__new_any_cpu;
 		perf_cpu_map__default_new;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
tools/lib/perf/tests/test-cpumap.c +1 -1
index 87b0510a556f..2c359bdb951e 100644
@@ -21,7 +21,7 @@ int test_cpumap(int argc, char **argv)
 
 	libperf_init(libperf_print);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	if (!cpus)
 		return -1;
 
tools/lib/perf/tests/test-evlist.c +1 -1
index ed616fc19b4f..ab63878bacb9 100644
@@ -261,7 +261,7 @@ static int test_mmap_thread(void)
 	threads = perf_thread_map__new_dummy();
 	__T("failed to create threads", threads);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	__T("failed to create cpus", cpus);
 
 	perf_thread_map__set_pid(threads, 0, pid);
tools/perf/tests/cpumap.c +1 -1
index 7730fc2ab40b..bd8e396f3e57 100644
@@ -213,7 +213,7 @@ static int test__cpu_map_intersect(struct test_suite *test __maybe_unused,
 
 static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 {
-	struct perf_cpu_map *any = perf_cpu_map__dummy_new();
+	struct perf_cpu_map *any = perf_cpu_map__new_any_cpu();
 	struct perf_cpu_map *one = perf_cpu_map__new("1");
 	struct perf_cpu_map *two = perf_cpu_map__new("2");
 	struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two);
tools/perf/tests/sw-clock.c +1 -1
index 4d7493fa0105..290716783ac6 100644
@@ -62,7 +62,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 	}
 	evlist__add(evlist, evsel);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	threads = thread_map__new_by_tid(getpid());
 	if (!cpus || !threads) {
 		err = -ENOMEM;
tools/perf/tests/task-exit.c +1 -1
index 968dddde6dda..d33d0952025c 100644
@@ -70,7 +70,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _
 	 * evlist__prepare_workload we'll fill in the only thread
 	 * we're monitoring, the one forked there.
 	 */
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	threads = thread_map__new_by_tid(-1);
 	if (!cpus || !threads) {
 		err = -ENOMEM;
tools/perf/util/evlist.c +1 -1
index e36da58522ef..ff7f85ded89d 100644
@@ -1056,7 +1056,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)
 		return -1;
 
 	if (target__uses_dummy_map(target))
-		cpus = perf_cpu_map__dummy_new();
+		cpus = perf_cpu_map__new_any_cpu();
 	else
 		cpus = perf_cpu_map__new(target->cpu_list);
 
tools/perf/util/evsel.c +1 -1
index a5da74e3a517..76ef3ab488a2 100644
@@ -1801,7 +1801,7 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 	if (cpus == NULL) {
 		if (empty_cpu_map == NULL) {
-			empty_cpu_map = perf_cpu_map__dummy_new();
+			empty_cpu_map = perf_cpu_map__new_any_cpu();
 			if (empty_cpu_map == NULL)
 				return -ENOMEM;
 		}
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 1/14
#2 lore raw

Re: [PATCH v1 01/14] libperf cpumap: Rename perf_cpu_map__dummy_new

Quoted 5 lines · Ian Rogers

Rename perf_cpu_map__dummy_new to perf_cpu_map__new_any_cpu to better indicate this is creating a CPU map for the perf_event_open "any" CPU case.

Quoted 183 lines
---
 tools/lib/perf/Documentation/libperf.txt | 2 +-
 tools/lib/perf/cpumap.c                  | 4 ++--
 tools/lib/perf/evsel.c                   | 2 +-
 tools/lib/perf/include/perf/cpumap.h     | 4 ++--
 tools/lib/perf/libperf.map               | 2 +-
 tools/lib/perf/tests/test-cpumap.c       | 2 +-
 tools/lib/perf/tests/test-evlist.c       | 2 +-
 tools/perf/tests/cpumap.c                | 2 +-
 tools/perf/tests/sw-clock.c              | 2 +-
 tools/perf/tests/task-exit.c             | 2 +-
 tools/perf/util/evlist.c                 | 2 +-
 tools/perf/util/evsel.c                  | 2 +-
 12 files changed, 14 insertions(+), 14 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -1
index a8f1a237931b..a256a26598b0 100644
@@ -37,7 +37,7 @@ SYNOPSIS
 
   struct perf_cpu_map;
 
-  struct perf_cpu_map *perf_cpu_map__dummy_new(void);
+  struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
   struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
   struct perf_cpu_map *perf_cpu_map__read(FILE *file);
   struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/cpumap.c +2 -2
index 2a5a29217374..2bd6aba3d8c9 100644
@@ -27,7 +27,7 @@ struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus)
 	return result;
 }
 
-struct perf_cpu_map *perf_cpu_map__dummy_new(void)
+struct perf_cpu_map *perf_cpu_map__new_any_cpu(void)
 {
 	struct perf_cpu_map *cpus = perf_cpu_map__alloc(1);
 
@@ -271,7 +271,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	else if (*cpu_list != '\0')
 		cpus = cpu_map__default_new();
 	else
-		cpus = perf_cpu_map__dummy_new();
+		cpus = perf_cpu_map__new_any_cpu();
 invalid:
 	free(tmp_cpus);
 out:
tools/lib/perf/evsel.c +1 -1
index 8b51b008a81f..c07160953224 100644
@@ -120,7 +120,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
 		static struct perf_cpu_map *empty_cpu_map;
 
 		if (empty_cpu_map == NULL) {
-			empty_cpu_map = perf_cpu_map__dummy_new();
+			empty_cpu_map = perf_cpu_map__new_any_cpu();
 			if (empty_cpu_map == NULL)
 				return -ENOMEM;
 		}
tools/lib/perf/include/perf/cpumap.h +2 -2
index e38d859a384d..d0bf218ada11 100644
@@ -19,9 +19,9 @@ struct perf_cache {
 struct perf_cpu_map;
 
 /**
- * perf_cpu_map__dummy_new - a map with a singular "any CPU"/dummy -1 value.
+ * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(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);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
tools/lib/perf/libperf.map +1 -1
index 190b56ae923a..a8ff64baea3e 100644
@@ -1,7 +1,7 @@
 LIBPERF_0.0.1 {
 	global:
 		libperf_init;
-		perf_cpu_map__dummy_new;
+		perf_cpu_map__new_any_cpu;
 		perf_cpu_map__default_new;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
tools/lib/perf/tests/test-cpumap.c +1 -1
index 87b0510a556f..2c359bdb951e 100644
@@ -21,7 +21,7 @@ int test_cpumap(int argc, char **argv)
 
 	libperf_init(libperf_print);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	if (!cpus)
 		return -1;
 
tools/lib/perf/tests/test-evlist.c +1 -1
index ed616fc19b4f..ab63878bacb9 100644
@@ -261,7 +261,7 @@ static int test_mmap_thread(void)
 	threads = perf_thread_map__new_dummy();
 	__T("failed to create threads", threads);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	__T("failed to create cpus", cpus);
 
 	perf_thread_map__set_pid(threads, 0, pid);
tools/perf/tests/cpumap.c +1 -1
index 7730fc2ab40b..bd8e396f3e57 100644
@@ -213,7 +213,7 @@ static int test__cpu_map_intersect(struct test_suite *test __maybe_unused,
 
 static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 {
-	struct perf_cpu_map *any = perf_cpu_map__dummy_new();
+	struct perf_cpu_map *any = perf_cpu_map__new_any_cpu();
 	struct perf_cpu_map *one = perf_cpu_map__new("1");
 	struct perf_cpu_map *two = perf_cpu_map__new("2");
 	struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two);
tools/perf/tests/sw-clock.c +1 -1
index 4d7493fa0105..290716783ac6 100644
@@ -62,7 +62,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 	}
 	evlist__add(evlist, evsel);
 
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	threads = thread_map__new_by_tid(getpid());
 	if (!cpus || !threads) {
 		err = -ENOMEM;
tools/perf/tests/task-exit.c +1 -1
index 968dddde6dda..d33d0952025c 100644
@@ -70,7 +70,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _
 	 * evlist__prepare_workload we'll fill in the only thread
 	 * we're monitoring, the one forked there.
 	 */
-	cpus = perf_cpu_map__dummy_new();
+	cpus = perf_cpu_map__new_any_cpu();
 	threads = thread_map__new_by_tid(-1);
 	if (!cpus || !threads) {
 		err = -ENOMEM;
tools/perf/util/evlist.c +1 -1
index e36da58522ef..ff7f85ded89d 100644
@@ -1056,7 +1056,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)
 		return -1;
 
 	if (target__uses_dummy_map(target))
-		cpus = perf_cpu_map__dummy_new();
+		cpus = perf_cpu_map__new_any_cpu();
 	else
 		cpus = perf_cpu_map__new(target->cpu_list);
 
tools/perf/util/evsel.c +1 -1
index a5da74e3a517..76ef3ab488a2 100644
@@ -1801,7 +1801,7 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 	if (cpus == NULL) {
 		if (empty_cpu_map == NULL) {
-			empty_cpu_map = perf_cpu_map__dummy_new();
+			empty_cpu_map = perf_cpu_map__new_any_cpu();
 			if (empty_cpu_map == NULL)
 				return -ENOMEM;
 		}
author reviewer
#3 lore raw

PATCH 2/14 libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new

In reply to Ian Rogers

Rename perf_cpu_map__default_new to perf_cpu_map__new_online_cpus to better indicate what the implementation does. Read the online CPUs from /sys/devices/system/cpu/online first before using sysconf as sysconf can't accurately configure holes in the CPU map. If sysconf is used, warn when the configured and online processors disagree.

When reading from a file, if the read doesn't yield a CPU map then return an empty map rather than the default online. This avoids recursion but also better yields being able to detect failures.

Add more comments.

---
 tools/lib/perf/cpumap.c              | 59 +++++++++++++++++-----------
 tools/lib/perf/include/perf/cpumap.h | 15 ++++++-
 tools/lib/perf/libperf.map           |  2 +-
 tools/lib/perf/tests/test-cpumap.c   |  2 +-
 4 files changed, 51 insertions(+), 27 deletions(-)
tools/lib/perf/cpumap.c +35 -24
index 2bd6aba3d8c9..463ca8b37d45 100644
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <limits.h>
+#include "internal.h"
 
 void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus)
 {
@@ -66,15 +67,21 @@ void perf_cpu_map__put(struct perf_cpu_map *map)
 	}
 }
 
-static struct perf_cpu_map *cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_sysconf(void)
 {
 	struct perf_cpu_map *cpus;
-	int nr_cpus;
+	int nr_cpus, nr_cpus_conf;
 
 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	if (nr_cpus < 0)
 		return NULL;
 
+	nr_cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
+	if (nr_cpus != nr_cpus_conf) {
+		pr_warning("Number of online CPUs (%d) differs from the number configured (%d) the CPU map will only cover the first %d CPUs.",
+			nr_cpus, nr_cpus_conf, nr_cpus);
+	}
+
 	cpus = perf_cpu_map__alloc(nr_cpus);
 	if (cpus != NULL) {
 		int i;
@@ -86,9 +93,27 @@ static struct perf_cpu_map *cpu_map__default_new(void)
 	return cpus;
 }
 
-struct perf_cpu_map *perf_cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_syfs_online(void)
 {
-	return cpu_map__default_new();
+	struct perf_cpu_map *cpus = NULL;
+	FILE *onlnf;
+
+	onlnf = fopen("/sys/devices/system/cpu/online", "r");
+	if (onlnf) {
+		cpus = perf_cpu_map__read(onlnf);
+		fclose(onlnf);
+	}
+	return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__new_online_cpus(void)
+{
+	struct perf_cpu_map *cpus = cpu_map__new_syfs_online();
+
+	if (cpus)
+		return cpus;
+
+	return cpu_map__new_sysconf();
 }
 
 
@@ -180,27 +205,11 @@ struct perf_cpu_map *perf_cpu_map__read(FILE *file)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else
-		cpus = cpu_map__default_new();
 out_free_tmp:
 	free(tmp_cpus);
 	return cpus;
 }
 
-static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
-{
-	struct perf_cpu_map *cpus = NULL;
-	FILE *onlnf;
-
-	onlnf = fopen("/sys/devices/system/cpu/online", "r");
-	if (!onlnf)
-		return cpu_map__default_new();
-
-	cpus = perf_cpu_map__read(onlnf);
-	fclose(onlnf);
-	return cpus;
-}
-
 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 {
 	struct perf_cpu_map *cpus = NULL;
@@ -211,7 +220,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	int max_entries = 0;
 
 	if (!cpu_list)
-		return cpu_map__read_all_cpu_map();
+		return perf_cpu_map__new_online_cpus();
 
 	/*
 	 * must handle the case of empty cpumap to cover
@@ -268,9 +277,11 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else if (*cpu_list != '\0')
-		cpus = cpu_map__default_new();
-	else
+	else if (*cpu_list != '\0') {
+		pr_warning("Unexpected characters at end of cpu list ('%s'), using online CPUs.",
+			   cpu_list);
+		cpus = perf_cpu_map__new_online_cpus();
+	} else
 		cpus = perf_cpu_map__new_any_cpu();
 invalid:
 	free(tmp_cpus);
tools/lib/perf/include/perf/cpumap.h +14 -1
index d0bf218ada11..b24bd8b8f34e 100644
@@ -22,7 +22,20 @@ struct perf_cpu_map;
  * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
+/**
+ * perf_cpu_map__new_online_cpus - a map read from
+ *                                 /sys/devices/system/cpu/online if
+ *                                 available. If reading wasn't possible a map
+ *                                 is created using the online processors
+ *                                 assuming the first 'n' processors are all
+ *                                 online.
+ */
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
+/**
+ * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no
+ *                     cpu_list argument is provided then
+ *                     perf_cpu_map__new_online_cpus is returned.
+ */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/libperf.map +1 -1
index a8ff64baea3e..8a71f841498e 100644
@@ -2,7 +2,7 @@ LIBPERF_0.0.1 {
 	global:
 		libperf_init;
 		perf_cpu_map__new_any_cpu;
-		perf_cpu_map__default_new;
+		perf_cpu_map__new_online_cpus;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
 		perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +1 -1
index 2c359bdb951e..c998b1dae863 100644
@@ -29,7 +29,7 @@ int test_cpumap(int argc, char **argv)
 	perf_cpu_map__put(cpus);
 	perf_cpu_map__put(cpus);
 
-	cpus = perf_cpu_map__default_new();
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return -1;
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 2/14
#4 lore raw

Re: [PATCH v1 02/14] libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new

Quoted 6 lines · Ian Rogers

Rename perf_cpu_map__default_new to perf_cpu_map__new_online_cpus to better indicate what the implementation does. Read the online CPUs from /sys/devices/system/cpu/online first before using sysconf as sysconf can't accurately configure holes in the CPU map. If sysconf is used, warn when the configured and online processors disagree.

Quoted 7 lines

When reading from a file, if the read doesn't yield a CPU map then return an empty map rather than the default online. This avoids recursion but also better yields being able to detect failures.

Add more comments.

Quoted 177 lines
---
 tools/lib/perf/cpumap.c              | 59 +++++++++++++++++-----------
 tools/lib/perf/include/perf/cpumap.h | 15 ++++++-
 tools/lib/perf/libperf.map           |  2 +-
 tools/lib/perf/tests/test-cpumap.c   |  2 +-
 4 files changed, 51 insertions(+), 27 deletions(-)
tools/lib/perf/cpumap.c +35 -24
index 2bd6aba3d8c9..463ca8b37d45 100644
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <limits.h>
+#include "internal.h"
 
 void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus)
 {
@@ -66,15 +67,21 @@ void perf_cpu_map__put(struct perf_cpu_map *map)
 	}
 }
 
-static struct perf_cpu_map *cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_sysconf(void)
 {
 	struct perf_cpu_map *cpus;
-	int nr_cpus;
+	int nr_cpus, nr_cpus_conf;
 
 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	if (nr_cpus < 0)
 		return NULL;
 
+	nr_cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
+	if (nr_cpus != nr_cpus_conf) {
+		pr_warning("Number of online CPUs (%d) differs from the number configured (%d) the CPU map will only cover the first %d CPUs.",
+			nr_cpus, nr_cpus_conf, nr_cpus);
+	}
+
 	cpus = perf_cpu_map__alloc(nr_cpus);
 	if (cpus != NULL) {
 		int i;
@@ -86,9 +93,27 @@ static struct perf_cpu_map *cpu_map__default_new(void)
 	return cpus;
 }
 
-struct perf_cpu_map *perf_cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_syfs_online(void)
 {
-	return cpu_map__default_new();
+	struct perf_cpu_map *cpus = NULL;
+	FILE *onlnf;
+
+	onlnf = fopen("/sys/devices/system/cpu/online", "r");
+	if (onlnf) {
+		cpus = perf_cpu_map__read(onlnf);
+		fclose(onlnf);
+	}
+	return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__new_online_cpus(void)
+{
+	struct perf_cpu_map *cpus = cpu_map__new_syfs_online();
+
+	if (cpus)
+		return cpus;
+
+	return cpu_map__new_sysconf();
 }
 
 
@@ -180,27 +205,11 @@ struct perf_cpu_map *perf_cpu_map__read(FILE *file)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else
-		cpus = cpu_map__default_new();
 out_free_tmp:
 	free(tmp_cpus);
 	return cpus;
 }
 
-static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
-{
-	struct perf_cpu_map *cpus = NULL;
-	FILE *onlnf;
-
-	onlnf = fopen("/sys/devices/system/cpu/online", "r");
-	if (!onlnf)
-		return cpu_map__default_new();
-
-	cpus = perf_cpu_map__read(onlnf);
-	fclose(onlnf);
-	return cpus;
-}
-
 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 {
 	struct perf_cpu_map *cpus = NULL;
@@ -211,7 +220,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	int max_entries = 0;
 
 	if (!cpu_list)
-		return cpu_map__read_all_cpu_map();
+		return perf_cpu_map__new_online_cpus();
 
 	/*
 	 * must handle the case of empty cpumap to cover
@@ -268,9 +277,11 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else if (*cpu_list != '\0')
-		cpus = cpu_map__default_new();
-	else
+	else if (*cpu_list != '\0') {
+		pr_warning("Unexpected characters at end of cpu list ('%s'), using online CPUs.",
+			   cpu_list);
+		cpus = perf_cpu_map__new_online_cpus();
+	} else
 		cpus = perf_cpu_map__new_any_cpu();
 invalid:
 	free(tmp_cpus);
tools/lib/perf/include/perf/cpumap.h +14 -1
index d0bf218ada11..b24bd8b8f34e 100644
@@ -22,7 +22,20 @@ struct perf_cpu_map;
  * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
+/**
+ * perf_cpu_map__new_online_cpus - a map read from
+ *                                 /sys/devices/system/cpu/online if
+ *                                 available. If reading wasn't possible a map
+ *                                 is created using the online processors
+ *                                 assuming the first 'n' processors are all
+ *                                 online.
+ */
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
+/**
+ * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no
+ *                     cpu_list argument is provided then
+ *                     perf_cpu_map__new_online_cpus is returned.
+ */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/libperf.map +1 -1
index a8ff64baea3e..8a71f841498e 100644
@@ -2,7 +2,7 @@ LIBPERF_0.0.1 {
 	global:
 		libperf_init;
 		perf_cpu_map__new_any_cpu;
-		perf_cpu_map__default_new;
+		perf_cpu_map__new_online_cpus;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
 		perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +1 -1
index 2c359bdb951e..c998b1dae863 100644
@@ -29,7 +29,7 @@ int test_cpumap(int argc, char **argv)
 	perf_cpu_map__put(cpus);
 	perf_cpu_map__put(cpus);
 
-	cpus = perf_cpu_map__default_new();
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return -1;
 
#5 lore raw

Re: [PATCH v1 02/14] libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new

In reply to Ian Rogers

Em Tue, Nov 28, 2023 at 10:01:59PM -0800, Ian Rogers escreveu:

Quoted 57 lines

Rename perf_cpu_map__default_new to perf_cpu_map__new_online_cpus to better indicate what the implementation does. Read the online CPUs from /sys/devices/system/cpu/online first before using sysconf as sysconf can't accurately configure holes in the CPU map. If sysconf is used, warn when the configured and online processors disagree.

When reading from a file, if the read doesn't yield a CPU map then return an empty map rather than the default online. This avoids recursion but also better yields being able to detect failures.

Add more comments.

---
 tools/lib/perf/cpumap.c              | 59 +++++++++++++++++-----------
 tools/lib/perf/include/perf/cpumap.h | 15 ++++++-
 tools/lib/perf/libperf.map           |  2 +-
 tools/lib/perf/tests/test-cpumap.c   |  2 +-
 4 files changed, 51 insertions(+), 27 deletions(-)
tools/lib/perf/cpumap.c +9 -2
index 2bd6aba3d8c9..463ca8b37d45 100644
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <limits.h>
+#include "internal.h"
 
 void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus)
 {
@@ -66,15 +67,21 @@ void perf_cpu_map__put(struct perf_cpu_map *map)
 	}
 }
 
-static struct perf_cpu_map *cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_sysconf(void)
 {
 	struct perf_cpu_map *cpus;
-	int nr_cpus;
+	int nr_cpus, nr_cpus_conf;
 
 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	if (nr_cpus < 0)
 		return NULL;
 
+	nr_cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
+	if (nr_cpus != nr_cpus_conf) {
+		pr_warning("Number of online CPUs (%d) differs from the number configured (%d) the CPU map will only cover the first %d CPUs.",
+			nr_cpus, nr_cpus_conf, nr_cpus);
+	}
+
 	cpus = perf_cpu_map__alloc(nr_cpus);
 	if (cpus != NULL) {
 		int i;

tools/lib/perf/cpumap.c static struct perf_cpu_map *cpu_map__default_new(void) · line 96

Quoted 5 lines
tools/lib/perf/cpumap.c +1 -1
@@ -86,9 +93,27 @@ static struct perf_cpu_map *cpu_map__default_new(void)
 	return cpus;
 }
 
-struct perf_cpu_map *perf_cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_syfs_online(void)

I'm renaming this to cpu_map__new_sysfs_online(), ok?

- Arnaldo

Quoted 131 lines
tools/lib/perf/cpumap.c +25 -21
 {
-	return cpu_map__default_new();
+	struct perf_cpu_map *cpus = NULL;
+	FILE *onlnf;
+
+	onlnf = fopen("/sys/devices/system/cpu/online", "r");
+	if (onlnf) {
+		cpus = perf_cpu_map__read(onlnf);
+		fclose(onlnf);
+	}
+	return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__new_online_cpus(void)
+{
+	struct perf_cpu_map *cpus = cpu_map__new_syfs_online();
+
+	if (cpus)
+		return cpus;
+
+	return cpu_map__new_sysconf();
 }
 
 
@@ -180,27 +205,11 @@ struct perf_cpu_map *perf_cpu_map__read(FILE *file)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else
-		cpus = cpu_map__default_new();
 out_free_tmp:
 	free(tmp_cpus);
 	return cpus;
 }
 
-static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
-{
-	struct perf_cpu_map *cpus = NULL;
-	FILE *onlnf;
-
-	onlnf = fopen("/sys/devices/system/cpu/online", "r");
-	if (!onlnf)
-		return cpu_map__default_new();
-
-	cpus = perf_cpu_map__read(onlnf);
-	fclose(onlnf);
-	return cpus;
-}
-
 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 {
 	struct perf_cpu_map *cpus = NULL;
@@ -211,7 +220,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	int max_entries = 0;
 
 	if (!cpu_list)
-		return cpu_map__read_all_cpu_map();
+		return perf_cpu_map__new_online_cpus();
 
 	/*
 	 * must handle the case of empty cpumap to cover
@@ -268,9 +277,11 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 	if (nr_cpus > 0)
 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-	else if (*cpu_list != '\0')
-		cpus = cpu_map__default_new();
-	else
+	else if (*cpu_list != '\0') {
+		pr_warning("Unexpected characters at end of cpu list ('%s'), using online CPUs.",
+			   cpu_list);
+		cpus = perf_cpu_map__new_online_cpus();
+	} else
 		cpus = perf_cpu_map__new_any_cpu();
 invalid:
 	free(tmp_cpus);
tools/lib/perf/include/perf/cpumap.h +14 -1
index d0bf218ada11..b24bd8b8f34e 100644
@@ -22,7 +22,20 @@ struct perf_cpu_map;
  * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
+/**
+ * perf_cpu_map__new_online_cpus - a map read from
+ *                                 /sys/devices/system/cpu/online if
+ *                                 available. If reading wasn't possible a map
+ *                                 is created using the online processors
+ *                                 assuming the first 'n' processors are all
+ *                                 online.
+ */
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
+/**
+ * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no
+ *                     cpu_list argument is provided then
+ *                     perf_cpu_map__new_online_cpus is returned.
+ */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/libperf.map +1 -1
index a8ff64baea3e..8a71f841498e 100644
@@ -2,7 +2,7 @@ LIBPERF_0.0.1 {
 	global:
 		libperf_init;
 		perf_cpu_map__new_any_cpu;
-		perf_cpu_map__default_new;
+		perf_cpu_map__new_online_cpus;
 		perf_cpu_map__get;
 		perf_cpu_map__put;
 		perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +1 -1
index 2c359bdb951e..c998b1dae863 100644
@@ -29,7 +29,7 @@ int test_cpumap(int argc, char **argv)
 	perf_cpu_map__put(cpus);
 	perf_cpu_map__put(cpus);
 
-	cpus = perf_cpu_map__default_new();
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return -1;
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Signature · 1 line
- Arnaldo
author reviewer
#6 lore raw

Re: [PATCH v1 02/14] libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new

Quoted 62 lines · Arnaldo Carvalho de Melo

Em Tue, Nov 28, 2023 at 10:01:59PM -0800, Ian Rogers escreveu:

Quoted 60 lines

Rename perf_cpu_map__default_new to perf_cpu_map__new_online_cpus to better indicate what the implementation does. Read the online CPUs from /sys/devices/system/cpu/online first before using sysconf as sysconf can't accurately configure holes in the CPU map. If sysconf is used, warn when the configured and online processors disagree.

When reading from a file, if the read doesn't yield a CPU map then return an empty map rather than the default online. This avoids recursion but also better yields being able to detect failures.

Add more comments.

---
 tools/lib/perf/cpumap.c              | 59 +++++++++++++++++-----------
 tools/lib/perf/include/perf/cpumap.h | 15 ++++++-
 tools/lib/perf/libperf.map           |  2 +-
 tools/lib/perf/tests/test-cpumap.c   |  2 +-
 4 files changed, 51 insertions(+), 27 deletions(-)
tools/lib/perf/cpumap.c +9 -2
index 2bd6aba3d8c9..463ca8b37d45 100644
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <limits.h>
+#include "internal.h"
 
 void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus)
 {
@@ -66,15 +67,21 @@ void perf_cpu_map__put(struct perf_cpu_map *map)
      }
 }
 
-static struct perf_cpu_map *cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_sysconf(void)
 {
      struct perf_cpu_map *cpus;
-     int nr_cpus;
+     int nr_cpus, nr_cpus_conf;
 
      nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
      if (nr_cpus < 0)
              return NULL;
 
+     nr_cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
+     if (nr_cpus != nr_cpus_conf) {
+             pr_warning("Number of online CPUs (%d) differs from the number configured (%d) the CPU map will only cover the first %d CPUs.",
+                     nr_cpus, nr_cpus_conf, nr_cpus);
+     }
+
      cpus = perf_cpu_map__alloc(nr_cpus);
      if (cpus != NULL) {
              int i;
@@ -86,9 +93,27 @@ static struct perf_cpu_map *cpu_map__default_new(void)
      return cpus;
 }
 
Quoted 4 lines

tools/lib/perf/cpumap.c static struct perf_cpu_map *cpu_map__default_new(void) · line 96

Quoted 2 lines
tools/lib/perf/cpumap.c +1 -1
-struct perf_cpu_map *perf_cpu_map__default_new(void)
+static struct perf_cpu_map *cpu_map__new_syfs_online(void)

I'm renaming this to cpu_map__new_sysfs_online(), ok?

Yep, typo. Thanks!

Ian

Quoted 137 lines

- Arnaldo

Quoted 129 lines
tools/lib/perf/cpumap.c +25 -21
 {
-     return cpu_map__default_new();
+     struct perf_cpu_map *cpus = NULL;
+     FILE *onlnf;
+
+     onlnf = fopen("/sys/devices/system/cpu/online", "r");
+     if (onlnf) {
+             cpus = perf_cpu_map__read(onlnf);
+             fclose(onlnf);
+     }
+     return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__new_online_cpus(void)
+{
+     struct perf_cpu_map *cpus = cpu_map__new_syfs_online();
+
+     if (cpus)
+             return cpus;
+
+     return cpu_map__new_sysconf();
 }
 
 
@@ -180,27 +205,11 @@ struct perf_cpu_map *perf_cpu_map__read(FILE *file)
 
      if (nr_cpus > 0)
              cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-     else
-             cpus = cpu_map__default_new();
 out_free_tmp:
      free(tmp_cpus);
      return cpus;
 }
 
-static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
-{
-     struct perf_cpu_map *cpus = NULL;
-     FILE *onlnf;
-
-     onlnf = fopen("/sys/devices/system/cpu/online", "r");
-     if (!onlnf)
-             return cpu_map__default_new();
-
-     cpus = perf_cpu_map__read(onlnf);
-     fclose(onlnf);
-     return cpus;
-}
-
 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 {
      struct perf_cpu_map *cpus = NULL;
@@ -211,7 +220,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
      int max_entries = 0;
 
      if (!cpu_list)
-             return cpu_map__read_all_cpu_map();
+             return perf_cpu_map__new_online_cpus();
 
      /*
       * must handle the case of empty cpumap to cover
@@ -268,9 +277,11 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
      if (nr_cpus > 0)
              cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-     else if (*cpu_list != '\0')
-             cpus = cpu_map__default_new();
-     else
+     else if (*cpu_list != '\0') {
+             pr_warning("Unexpected characters at end of cpu list ('%s'), using online CPUs.",
+                        cpu_list);
+             cpus = perf_cpu_map__new_online_cpus();
+     } else
              cpus = perf_cpu_map__new_any_cpu();
 invalid:
      free(tmp_cpus);
tools/lib/perf/include/perf/cpumap.h +14 -1
index d0bf218ada11..b24bd8b8f34e 100644
@@ -22,7 +22,20 @@ struct perf_cpu_map;
  * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
  */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
-LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void);
+/**
+ * perf_cpu_map__new_online_cpus - a map read from
+ *                                 /sys/devices/system/cpu/online if
+ *                                 available. If reading wasn't possible a map
+ *                                 is created using the online processors
+ *                                 assuming the first 'n' processors are all
+ *                                 online.
+ */
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
+/**
+ * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no
+ *                     cpu_list argument is provided then
+ *                     perf_cpu_map__new_online_cpus is returned.
+ */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
tools/lib/perf/libperf.map +1 -1
index a8ff64baea3e..8a71f841498e 100644
@@ -2,7 +2,7 @@ LIBPERF_0.0.1 {
      global:
              libperf_init;
              perf_cpu_map__new_any_cpu;
-             perf_cpu_map__default_new;
+             perf_cpu_map__new_online_cpus;
              perf_cpu_map__get;
              perf_cpu_map__put;
              perf_cpu_map__new;
tools/lib/perf/tests/test-cpumap.c +1 -2
index 2c359bdb951e..c998b1dae863 100644
@@ -29,7 +29,7 @@ int test_cpumap(int argc, char **argv)
      perf_cpu_map__put(cpus);
      perf_cpu_map__put(cpus);
 
-     cpus = perf_cpu_map__default_new();
+     cpus = perf_cpu_map__new_online_cpus();
      if (!cpus)
              return -1;
 
--
Quoted 2 lines

2.43.0.rc1.413.gea7ed67945-goog

--

- Arnaldo

author reviewer
#7 lore raw

PATCH 3/14 libperf cpumap: Rename perf_cpu_map__empty

In reply to Ian Rogers

The name perf_cpu_map_empty is misleading as true is also returned when the map contains an "any" CPU (aka dummy) map. Rename to perf_cpu_map__has_any_cpu_or_is_empty, later changes will (re)introduce perf_cpu_map__empty and perf_cpu_map__has_any_cpu.

---
 tools/lib/perf/Documentation/libperf.txt |  2 +-
 tools/lib/perf/cpumap.c                  |  2 +-
 tools/lib/perf/evlist.c                  |  4 ++--
 tools/lib/perf/include/perf/cpumap.h     |  4 ++--
 tools/lib/perf/libperf.map               |  2 +-
 tools/perf/arch/arm/util/cs-etm.c        | 10 +++++-----
 tools/perf/arch/arm64/util/arm-spe.c     |  4 ++--
 tools/perf/arch/x86/util/intel-bts.c     |  4 ++--
 tools/perf/arch/x86/util/intel-pt.c      | 10 +++++-----
 tools/perf/builtin-c2c.c                 |  2 +-
 tools/perf/builtin-stat.c                |  6 +++---
 tools/perf/util/auxtrace.c               |  4 ++--
 tools/perf/util/record.c                 |  2 +-
 tools/perf/util/stat.c                   |  2 +-
 14 files changed, 29 insertions(+), 29 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -1
index a256a26598b0..fcfb9499ef9c 100644
@@ -46,7 +46,7 @@ SYNOPSIS
   void perf_cpu_map__put(struct perf_cpu_map *map);
   int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
   int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
-  bool perf_cpu_map__empty(const struct perf_cpu_map *map);
+  bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
   int perf_cpu_map__max(struct perf_cpu_map *map);
   bool perf_cpu_map__has(const struct perf_cpu_map *map, int cpu);
 
tools/lib/perf/cpumap.c +1 -1
index 463ca8b37d45..49fc98e16514 100644
@@ -311,7 +311,7 @@ int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
 	return cpus ? __perf_cpu_map__nr(cpus) : 1;
 }
 
-bool perf_cpu_map__empty(const struct perf_cpu_map *map)
+bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 {
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
tools/lib/perf/evlist.c +2 -2
index 3acbbccc1901..75f36218fdd9 100644
@@ -619,7 +619,7 @@ static int perf_evlist__nr_mmaps(struct perf_evlist *evlist)
 
 	/* One for each CPU */
 	nr_mmaps = perf_cpu_map__nr(evlist->all_cpus);
-	if (perf_cpu_map__empty(evlist->all_cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(evlist->all_cpus)) {
 		/* Plus one for each thread */
 		nr_mmaps += perf_thread_map__nr(evlist->threads);
 		/* Minus the per-thread CPU (-1) */
@@ -653,7 +653,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
 		return -ENOMEM;
 
-	if (perf_cpu_map__empty(cpus))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		return mmap_per_thread(evlist, ops, mp);
 
 	return mmap_per_cpu(evlist, ops, mp);
tools/lib/perf/include/perf/cpumap.h +2 -2
index b24bd8b8f34e..9cf361fc5edc 100644
@@ -47,9 +47,9 @@ LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
 /**
- * perf_cpu_map__empty - is map either empty or the "any CPU"/dummy value.
+ * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
-LIBPERF_API bool perf_cpu_map__empty(const struct perf_cpu_map *map);
+LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +1 -1
index 8a71f841498e..10b3f3722642 100644
@@ -9,7 +9,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__read;
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
-		perf_cpu_map__empty;
+		perf_cpu_map__has_any_cpu_or_is_empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
tools/perf/arch/arm/util/cs-etm.c +5 -5
index 2cf873d71dff..c6b7b3066324 100644
@@ -211,7 +211,7 @@ static int cs_etm_validate_config(struct auxtrace_record *itr,
 		 * program can run on any CPUs in this case, thus don't skip
 		 * validation.
 		 */
-		if (!perf_cpu_map__empty(event_cpus) &&
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
 		    !perf_cpu_map__has(event_cpus, cpu))
 			continue;
 
@@ -435,7 +435,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	 * Also the case of per-cpu mmaps, need the contextID in order to be notified
 	 * when a context switch happened.
 	 */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
 					   "timestamp", 1);
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +461,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__empty(cpus))
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		evsel__set_sample_bit(evsel, TIME);
 
 	err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -539,7 +539,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
 
 	/* cpu map is not empty, we have specific CPUs to work with */
-	if (!perf_cpu_map__empty(event_cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
 		for (i = 0; i < cpu__max_cpu().cpu; i++) {
 			struct perf_cpu cpu = { .cpu = i, };
 
@@ -814,7 +814,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 		return -EINVAL;
 
 	/* If the cpu_map is empty all online CPUs are involved */
-	if (perf_cpu_map__empty(event_cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
 		cpu_map = online_cpus;
 	} else {
 		/* Make sure all specified CPUs are online */
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index e3acc739bd00..51ccbfd3d246 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
 	 * also enable the timestamp tracing for samples correlation.
 	 */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(arm_spe_evsel, CPU);
 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
 					   "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	tracking_evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(tracking_evsel, TIME);
 		evsel__set_sample_bit(tracking_evsel, CPU);
 
tools/perf/arch/x86/util/intel-bts.c +2 -2
index d2c8cac11470..af8ae4647585 100644
@@ -143,7 +143,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	if (!opts->full_auxtrace)
 		return 0;
 
-	if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
+	if (opts->full_auxtrace && !perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
 		return -EINVAL;
 	}
@@ -224,7 +224,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_bts_evsel, CPU);
 	}
 
tools/perf/arch/x86/util/intel-pt.c +5 -5
index fa0c718b9e72..d199619df3ab 100644
@@ -369,7 +369,7 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 			ui__warning("Intel Processor Trace: TSC not available\n");
 	}
 
-	per_cpu_mmaps = !perf_cpu_map__empty(session->evlist->core.user_requested_cpus);
+	per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(session->evlist->core.user_requested_cpus);
 
 	auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
 	auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
@@ -774,7 +774,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Per-cpu recording needs sched_switch events to distinguish different
 	 * threads.
 	 */
-	if (have_timing_info && !perf_cpu_map__empty(cpus) &&
+	if (have_timing_info && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
 	    !record_opts__no_switch_events(opts)) {
 		if (perf_can_record_switch_events()) {
 			bool cpu_wide = !target__none(&opts->target) &&
@@ -832,7 +832,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_pt_evsel, CPU);
 	}
 
@@ -858,7 +858,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 			tracking_evsel->immediate = true;
 
 		/* In per-cpu case, always need the time of mmap events etc */
-		if (!perf_cpu_map__empty(cpus)) {
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 			evsel__set_sample_bit(tracking_evsel, TIME);
 			/* And the CPU for switch events */
 			evsel__set_sample_bit(tracking_evsel, CPU);
@@ -870,7 +870,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Warn the user when we do not have enough information to decode i.e.
 	 * per-cpu with no sched_switch (except workload-only).
 	 */
-	if (!ptr->have_sched_switch && !perf_cpu_map__empty(cpus) &&
+	if (!ptr->have_sched_switch && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
 	    !target__none(&opts->target) &&
 	    !intel_pt_evsel->core.attr.exclude_user)
 		ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
tools/perf/builtin-c2c.c +1 -1
index a4cf9de7a7b5..f78eea9e2153 100644
@@ -2320,7 +2320,7 @@ static int setup_nodes(struct perf_session *session)
 		nodes[node] = set;
 
 		/* empty node, skip */
-		if (perf_cpu_map__empty(map))
+		if (perf_cpu_map__has_any_cpu_or_is_empty(map))
 			continue;
 
 		perf_cpu_map__for_each_cpu(cpu, idx, map) {
tools/perf/builtin-stat.c +3 -3
index a3af805a1d57..3303aa20f326 100644
@@ -1316,7 +1316,7 @@ static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
 	 * be the first online CPU in the cache domain else use the
 	 * first online CPU of the cache domain as the ID.
 	 */
-	if (perf_cpu_map__empty(cpu_map))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpu_map))
 		id = cpu.cpu;
 	else
 		id = perf_cpu_map__cpu(cpu_map, 0).cpu;
@@ -1622,7 +1622,7 @@ static int perf_stat_init_aggr_mode(void)
 	 * taking the highest cpu number to be the size of
 	 * the aggregation translate cpumap.
 	 */
-	if (!perf_cpu_map__empty(evsel_list->core.user_requested_cpus))
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
 		nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu;
 	else
 		nr = 0;
@@ -2289,7 +2289,7 @@ int process_stat_config_event(struct perf_session *session,
 
 	perf_event__read_stat_config(&stat_config, &event->stat_config);
 
-	if (perf_cpu_map__empty(st->cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(st->cpus)) {
 		if (st->aggr_mode != AGGR_UNSET)
 			pr_warning("warning: processing task data, aggregation mode not set\n");
 	} else if (st->aggr_mode != AGGR_UNSET) {
tools/perf/util/auxtrace.c +2 -2
index f528c4364d23..3684e6009b63 100644
@@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
 				   struct evlist *evlist,
 				   struct evsel *evsel, int idx)
 {
-	bool per_cpu = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+	bool per_cpu = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
 
 	mp->mmap_needed = evsel->needs_auxtrace_mmap;
 
@@ -648,7 +648,7 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 
 static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
 {
-	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
 
 	if (per_cpu_mmaps) {
 		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
tools/perf/util/record.c +1 -1
index 9eb5c6a08999..40290382b2d7 100644
@@ -237,7 +237,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 
 	evsel = evlist__last(temp_evlist);
 
-	if (!evlist || perf_cpu_map__empty(evlist->core.user_requested_cpus)) {
+	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
 		struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
 
 		if (cpus)
tools/perf/util/stat.c +1 -1
index ec3506042217..012c4946b9c4 100644
@@ -315,7 +315,7 @@ static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
 	if (!counter->per_pkg)
 		return 0;
 
-	if (perf_cpu_map__empty(cpus))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		return 0;
 
 	if (!mask) {
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 3/14
#8 lore raw

Re: [PATCH v1 03/14] libperf cpumap: Rename perf_cpu_map__empty

Quoted 6 lines · Ian Rogers

The name perf_cpu_map_empty is misleading as true is also returned when the map contains an "any" CPU (aka dummy) map. Rename to perf_cpu_map__has_any_cpu_or_is_empty, later changes will (re)introduce perf_cpu_map__empty and perf_cpu_map__has_any_cpu.

Quoted 328 lines
---
 tools/lib/perf/Documentation/libperf.txt |  2 +-
 tools/lib/perf/cpumap.c                  |  2 +-
 tools/lib/perf/evlist.c                  |  4 ++--
 tools/lib/perf/include/perf/cpumap.h     |  4 ++--
 tools/lib/perf/libperf.map               |  2 +-
 tools/perf/arch/arm/util/cs-etm.c        | 10 +++++-----
 tools/perf/arch/arm64/util/arm-spe.c     |  4 ++--
 tools/perf/arch/x86/util/intel-bts.c     |  4 ++--
 tools/perf/arch/x86/util/intel-pt.c      | 10 +++++-----
 tools/perf/builtin-c2c.c                 |  2 +-
 tools/perf/builtin-stat.c                |  6 +++---
 tools/perf/util/auxtrace.c               |  4 ++--
 tools/perf/util/record.c                 |  2 +-
 tools/perf/util/stat.c                   |  2 +-
 14 files changed, 29 insertions(+), 29 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -1
index a256a26598b0..fcfb9499ef9c 100644
@@ -46,7 +46,7 @@ SYNOPSIS
   void perf_cpu_map__put(struct perf_cpu_map *map);
   int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
   int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
-  bool perf_cpu_map__empty(const struct perf_cpu_map *map);
+  bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
   int perf_cpu_map__max(struct perf_cpu_map *map);
   bool perf_cpu_map__has(const struct perf_cpu_map *map, int cpu);
 
tools/lib/perf/cpumap.c +1 -1
index 463ca8b37d45..49fc98e16514 100644
@@ -311,7 +311,7 @@ int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
 	return cpus ? __perf_cpu_map__nr(cpus) : 1;
 }
 
-bool perf_cpu_map__empty(const struct perf_cpu_map *map)
+bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 {
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
tools/lib/perf/evlist.c +2 -2
index 3acbbccc1901..75f36218fdd9 100644
@@ -619,7 +619,7 @@ static int perf_evlist__nr_mmaps(struct perf_evlist *evlist)
 
 	/* One for each CPU */
 	nr_mmaps = perf_cpu_map__nr(evlist->all_cpus);
-	if (perf_cpu_map__empty(evlist->all_cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(evlist->all_cpus)) {
 		/* Plus one for each thread */
 		nr_mmaps += perf_thread_map__nr(evlist->threads);
 		/* Minus the per-thread CPU (-1) */
@@ -653,7 +653,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
 		return -ENOMEM;
 
-	if (perf_cpu_map__empty(cpus))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		return mmap_per_thread(evlist, ops, mp);
 
 	return mmap_per_cpu(evlist, ops, mp);
tools/lib/perf/include/perf/cpumap.h +2 -2
index b24bd8b8f34e..9cf361fc5edc 100644
@@ -47,9 +47,9 @@ LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
 /**
- * perf_cpu_map__empty - is map either empty or the "any CPU"/dummy value.
+ * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
-LIBPERF_API bool perf_cpu_map__empty(const struct perf_cpu_map *map);
+LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +1 -1
index 8a71f841498e..10b3f3722642 100644
@@ -9,7 +9,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__read;
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
-		perf_cpu_map__empty;
+		perf_cpu_map__has_any_cpu_or_is_empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
tools/perf/arch/arm/util/cs-etm.c +5 -5
index 2cf873d71dff..c6b7b3066324 100644
@@ -211,7 +211,7 @@ static int cs_etm_validate_config(struct auxtrace_record *itr,
 		 * program can run on any CPUs in this case, thus don't skip
 		 * validation.
 		 */
-		if (!perf_cpu_map__empty(event_cpus) &&
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
 		    !perf_cpu_map__has(event_cpus, cpu))
 			continue;
 
@@ -435,7 +435,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	 * Also the case of per-cpu mmaps, need the contextID in order to be notified
 	 * when a context switch happened.
 	 */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
 					   "timestamp", 1);
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +461,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__empty(cpus))
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		evsel__set_sample_bit(evsel, TIME);
 
 	err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -539,7 +539,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
 
 	/* cpu map is not empty, we have specific CPUs to work with */
-	if (!perf_cpu_map__empty(event_cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
 		for (i = 0; i < cpu__max_cpu().cpu; i++) {
 			struct perf_cpu cpu = { .cpu = i, };
 
@@ -814,7 +814,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 		return -EINVAL;
 
 	/* If the cpu_map is empty all online CPUs are involved */
-	if (perf_cpu_map__empty(event_cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
 		cpu_map = online_cpus;
 	} else {
 		/* Make sure all specified CPUs are online */
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index e3acc739bd00..51ccbfd3d246 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
 	 * also enable the timestamp tracing for samples correlation.
 	 */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(arm_spe_evsel, CPU);
 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
 					   "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	tracking_evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__empty(cpus)) {
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(tracking_evsel, TIME);
 		evsel__set_sample_bit(tracking_evsel, CPU);
 
tools/perf/arch/x86/util/intel-bts.c +2 -2
index d2c8cac11470..af8ae4647585 100644
@@ -143,7 +143,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	if (!opts->full_auxtrace)
 		return 0;
 
-	if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
+	if (opts->full_auxtrace && !perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
 		return -EINVAL;
 	}
@@ -224,7 +224,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_bts_evsel, CPU);
 	}
 
tools/perf/arch/x86/util/intel-pt.c +5 -5
index fa0c718b9e72..d199619df3ab 100644
@@ -369,7 +369,7 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 			ui__warning("Intel Processor Trace: TSC not available\n");
 	}
 
-	per_cpu_mmaps = !perf_cpu_map__empty(session->evlist->core.user_requested_cpus);
+	per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(session->evlist->core.user_requested_cpus);
 
 	auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
 	auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
@@ -774,7 +774,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Per-cpu recording needs sched_switch events to distinguish different
 	 * threads.
 	 */
-	if (have_timing_info && !perf_cpu_map__empty(cpus) &&
+	if (have_timing_info && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
 	    !record_opts__no_switch_events(opts)) {
 		if (perf_can_record_switch_events()) {
 			bool cpu_wide = !target__none(&opts->target) &&
@@ -832,7 +832,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_pt_evsel, CPU);
 	}
 
@@ -858,7 +858,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 			tracking_evsel->immediate = true;
 
 		/* In per-cpu case, always need the time of mmap events etc */
-		if (!perf_cpu_map__empty(cpus)) {
+		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
 			evsel__set_sample_bit(tracking_evsel, TIME);
 			/* And the CPU for switch events */
 			evsel__set_sample_bit(tracking_evsel, CPU);
@@ -870,7 +870,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Warn the user when we do not have enough information to decode i.e.
 	 * per-cpu with no sched_switch (except workload-only).
 	 */
-	if (!ptr->have_sched_switch && !perf_cpu_map__empty(cpus) &&
+	if (!ptr->have_sched_switch && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
 	    !target__none(&opts->target) &&
 	    !intel_pt_evsel->core.attr.exclude_user)
 		ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
tools/perf/builtin-c2c.c +1 -1
index a4cf9de7a7b5..f78eea9e2153 100644
@@ -2320,7 +2320,7 @@ static int setup_nodes(struct perf_session *session)
 		nodes[node] = set;
 
 		/* empty node, skip */
-		if (perf_cpu_map__empty(map))
+		if (perf_cpu_map__has_any_cpu_or_is_empty(map))
 			continue;
 
 		perf_cpu_map__for_each_cpu(cpu, idx, map) {
tools/perf/builtin-stat.c +3 -3
index a3af805a1d57..3303aa20f326 100644
@@ -1316,7 +1316,7 @@ static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
 	 * be the first online CPU in the cache domain else use the
 	 * first online CPU of the cache domain as the ID.
 	 */
-	if (perf_cpu_map__empty(cpu_map))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpu_map))
 		id = cpu.cpu;
 	else
 		id = perf_cpu_map__cpu(cpu_map, 0).cpu;
@@ -1622,7 +1622,7 @@ static int perf_stat_init_aggr_mode(void)
 	 * taking the highest cpu number to be the size of
 	 * the aggregation translate cpumap.
 	 */
-	if (!perf_cpu_map__empty(evsel_list->core.user_requested_cpus))
+	if (!perf_cpu_map__has_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
 		nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu;
 	else
 		nr = 0;
@@ -2289,7 +2289,7 @@ int process_stat_config_event(struct perf_session *session,
 
 	perf_event__read_stat_config(&stat_config, &event->stat_config);
 
-	if (perf_cpu_map__empty(st->cpus)) {
+	if (perf_cpu_map__has_any_cpu_or_is_empty(st->cpus)) {
 		if (st->aggr_mode != AGGR_UNSET)
 			pr_warning("warning: processing task data, aggregation mode not set\n");
 	} else if (st->aggr_mode != AGGR_UNSET) {
tools/perf/util/auxtrace.c +2 -2
index f528c4364d23..3684e6009b63 100644
@@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
 				   struct evlist *evlist,
 				   struct evsel *evsel, int idx)
 {
-	bool per_cpu = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+	bool per_cpu = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
 
 	mp->mmap_needed = evsel->needs_auxtrace_mmap;
 
@@ -648,7 +648,7 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 
 static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
 {
-	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
 
 	if (per_cpu_mmaps) {
 		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
tools/perf/util/record.c +1 -1
index 9eb5c6a08999..40290382b2d7 100644
@@ -237,7 +237,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 
 	evsel = evlist__last(temp_evlist);
 
-	if (!evlist || perf_cpu_map__empty(evlist->core.user_requested_cpus)) {
+	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
 		struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
 
 		if (cpus)
tools/perf/util/stat.c +1 -1
index ec3506042217..012c4946b9c4 100644
@@ -315,7 +315,7 @@ static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
 	if (!counter->per_pkg)
 		return 0;
 
-	if (perf_cpu_map__empty(cpus))
+	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
 		return 0;
 
 	if (!mask) {
author reviewer
#9 lore raw

PATCH 4/14 libperf cpumap: Replace usage of perf_cpu_map__new(NULL)

In reply to Ian Rogers

Passing NULL to perf_cpu_map__new performs perf_cpu_map__new_online_cpus, just directly call perf_cpu_map__new_online_cpus to be more intention revealing.

---
 tools/lib/perf/Documentation/examples/sampling.c  | 2 +-
 tools/lib/perf/Documentation/libperf-sampling.txt | 2 +-
 tools/lib/perf/evlist.c                           | 2 +-
 tools/lib/perf/tests/test-evlist.c                | 4 ++--
 tools/lib/perf/tests/test-evsel.c                 | 2 +-
 tools/perf/arch/arm/util/cs-etm.c                 | 6 +++---
 tools/perf/arch/arm64/util/header.c               | 2 +-
 tools/perf/bench/epoll-ctl.c                      | 2 +-
 tools/perf/bench/epoll-wait.c                     | 2 +-
 tools/perf/bench/futex-hash.c                     | 2 +-
 tools/perf/bench/futex-lock-pi.c                  | 2 +-
 tools/perf/bench/futex-requeue.c                  | 2 +-
 tools/perf/bench/futex-wake-parallel.c            | 2 +-
 tools/perf/bench/futex-wake.c                     | 2 +-
 tools/perf/builtin-ftrace.c                       | 2 +-
 tools/perf/tests/code-reading.c                   | 2 +-
 tools/perf/tests/keep-tracking.c                  | 2 +-
 tools/perf/tests/mmap-basic.c                     | 2 +-
 tools/perf/tests/openat-syscall-all-cpus.c        | 2 +-
 tools/perf/tests/perf-time-to-tsc.c               | 2 +-
 tools/perf/tests/switch-tracking.c                | 2 +-
 tools/perf/tests/topology.c                       | 2 +-
 tools/perf/util/bpf_counter.c                     | 2 +-
 tools/perf/util/cpumap.c                          | 2 +-
 tools/perf/util/cputopo.c                         | 2 +-
 tools/perf/util/evlist.c                          | 2 +-
 tools/perf/util/perf_api_probe.c                  | 4 ++--
 tools/perf/util/record.c                          | 2 +-
 28 files changed, 32 insertions(+), 32 deletions(-)
tools/lib/perf/Documentation/examples/sampling.c +1 -1
index 8e1a926a9cfe..bc142f0664b5 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
 
 	libperf_init(libperf_print);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		fprintf(stderr, "failed to create cpus\n");
 		return -1;
tools/lib/perf/Documentation/libperf-sampling.txt +1 -1
index d6ca24f6ef78..2378980fab8a 100644
@@ -97,7 +97,7 @@ In this case we will monitor all the available CPUs:
 
 [source,c]
 --
- 42         cpus = perf_cpu_map__new(NULL);
+ 42         cpus = perf_cpu_map__new_online_cpus();
  43         if (!cpus) {
  44                 fprintf(stderr, "failed to create cpus\n");
  45                 return -1;
tools/lib/perf/evlist.c +1 -1
index 75f36218fdd9..058e3ff10f9b 100644
@@ -39,7 +39,7 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 	if (evsel->system_wide) {
 		/* System wide: set the cpu map of the evsel to all online CPUs. */
 		perf_cpu_map__put(evsel->cpus);
-		evsel->cpus = perf_cpu_map__new(NULL);
+		evsel->cpus = perf_cpu_map__new_online_cpus();
 	} else if (evlist->has_user_cpus && evsel->is_pmu_core) {
 		/*
 		 * User requested CPUs on a core PMU, ensure the requested CPUs
tools/lib/perf/tests/test-evlist.c +2 -2
index ab63878bacb9..10f70cb41ff1 100644
@@ -46,7 +46,7 @@ static int test_stat_cpu(void)
 	};
 	int err, idx;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evlist = perf_evlist__new();
@@ -350,7 +350,7 @@ static int test_mmap_cpus(void)
 
 	attr.config = id;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evlist = perf_evlist__new();
tools/lib/perf/tests/test-evsel.c +1 -1
index a11fc51bfb68..545ec3150546 100644
@@ -27,7 +27,7 @@ static int test_stat_cpu(void)
 	};
 	int err, idx;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evsel = perf_evsel__new(&attr);
tools/perf/arch/arm/util/cs-etm.c +3 -3
index c6b7b3066324..77e6663c1703 100644
@@ -199,7 +199,7 @@ static int cs_etm_validate_config(struct auxtrace_record *itr,
 {
 	int i, err = -EINVAL;
 	struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 
 	/* Set option of each CPU we have */
 	for (i = 0; i < cpu__max_cpu().cpu; i++) {
@@ -536,7 +536,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	int i;
 	int etmv3 = 0, etmv4 = 0, ete = 0;
 	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 
 	/* cpu map is not empty, we have specific CPUs to work with */
 	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
@@ -802,7 +802,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 	u64 nr_cpu, type;
 	struct perf_cpu_map *cpu_map;
 	struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 	struct cs_etm_recording *ptr =
 			container_of(itr, struct cs_etm_recording, itr);
 	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
tools/perf/arch/arm64/util/header.c +1 -1
index a2eef9ec5491..97037499152e 100644
@@ -57,7 +57,7 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 
 int get_cpuid(char *buf, size_t sz)
 {
-	struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 	int ret;
 
 	if (!cpus)
tools/perf/bench/epoll-ctl.c +1 -1
index 6bfffe83dde9..d3db73dac66a 100644
@@ -330,7 +330,7 @@ int bench_epoll_ctl(int argc, const char **argv)
 	act.sa_sigaction = toggle_done;
 	sigaction(SIGINT, &act, NULL);
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/epoll-wait.c +1 -1
index cb5174b53940..06bb3187660a 100644
@@ -444,7 +444,7 @@ int bench_epoll_wait(int argc, const char **argv)
 	act.sa_sigaction = toggle_done;
 	sigaction(SIGINT, &act, NULL);
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/futex-hash.c +1 -1
index 2005a3fa3026..0c69d20efa32 100644
@@ -138,7 +138,7 @@ int bench_futex_hash(int argc, const char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/futex-lock-pi.c +1 -1
index 092cbd52db82..7a4973346180 100644
@@ -172,7 +172,7 @@ int bench_futex_lock_pi(int argc, const char **argv)
 	if (argc)
 		goto err;
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/bench/futex-requeue.c +1 -1
index c0035990a33c..d9ad736c1a3e 100644
@@ -174,7 +174,7 @@ int bench_futex_requeue(int argc, const char **argv)
 	if (argc)
 		goto err;
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "cpu_map__new");
 
tools/perf/bench/futex-wake-parallel.c +1 -1
index 5ab0234d74e6..b66df553e561 100644
@@ -264,7 +264,7 @@ int bench_futex_wake_parallel(int argc, const char **argv)
 			err(EXIT_FAILURE, "mlockall");
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/bench/futex-wake.c +1 -1
index 18a5894af8bb..690fd6d3da13 100644
@@ -149,7 +149,7 @@ int bench_futex_wake(int argc, const char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/builtin-ftrace.c +1 -1
index ac2e6c75f912..eb30c8eca488 100644
@@ -333,7 +333,7 @@ static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
 
 static int reset_tracing_cpu(void)
 {
-	struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *cpumap = perf_cpu_map__new_online_cpus();
 	int ret;
 
 	ret = set_tracing_cpumask(cpumap);
tools/perf/tests/code-reading.c +1 -1
index 8620146d0378..7a3a7bbbec71 100644
@@ -610,7 +610,7 @@ static int do_test_code_reading(bool try_kcore)
 		goto out_put;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		pr_debug("perf_cpu_map__new failed\n");
 		goto out_put;
tools/perf/tests/keep-tracking.c +1 -1
index 8f4f9b632e1e..5a3b2bed07f3 100644
@@ -81,7 +81,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte
 	threads = thread_map__new(-1, getpid(), UINT_MAX);
 	CHECK_NOT_NULL__(threads);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	CHECK_NOT_NULL__(cpus);
 
 	evlist = evlist__new();
tools/perf/tests/mmap-basic.c +1 -1
index 886a13a77a16..012c8ae439fd 100644
@@ -52,7 +52,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
 		return -1;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (cpus == NULL) {
 		pr_debug("perf_cpu_map__new\n");
 		goto out_free_threads;
tools/perf/tests/openat-syscall-all-cpus.c +1 -1
index f3275be83a33..fb114118c876 100644
@@ -37,7 +37,7 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __mayb
 		return -1;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (cpus == NULL) {
 		pr_debug("perf_cpu_map__new\n");
 		goto out_thread_map_delete;
tools/perf/tests/perf-time-to-tsc.c +1 -1
index efcd71c2738a..bbe2ddeb9b74 100644
@@ -93,7 +93,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su
 	threads = thread_map__new(-1, getpid(), UINT_MAX);
 	CHECK_NOT_NULL__(threads);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	CHECK_NOT_NULL__(cpus);
 
 	evlist = evlist__new();
tools/perf/tests/switch-tracking.c +1 -1
index e52b031bedc5..5cab17a1942e 100644
@@ -351,7 +351,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub
 		goto out_err;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		pr_debug("perf_cpu_map__new failed!\n");
 		goto out_err;
tools/perf/tests/topology.c +1 -1
index 9dee63734e66..2a842f53fbb5 100644
@@ -215,7 +215,7 @@ static int test__session_topology(struct test_suite *test __maybe_unused, int su
 	if (session_write_header(path))
 		goto free_path;
 
-	map = perf_cpu_map__new(NULL);
+	map = perf_cpu_map__new_online_cpus();
 	if (map == NULL) {
 		pr_debug("failed to get system cpumap\n");
 		goto free_path;
tools/perf/util/bpf_counter.c +1 -1
index 7f9b0e46e008..7a8af60e0f51 100644
@@ -455,7 +455,7 @@ static int bperf__load(struct evsel *evsel, struct target *target)
 		return -1;
 
 	if (!all_cpu_map) {
-		all_cpu_map = perf_cpu_map__new(NULL);
+		all_cpu_map = perf_cpu_map__new_online_cpus();
 		if (!all_cpu_map)
 			return -1;
 	}
tools/perf/util/cpumap.c +1 -1
index 0e090e8bc334..0581ee0fa5f2 100644
@@ -672,7 +672,7 @@ struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
 	static struct perf_cpu_map *online;
 
 	if (!online)
-		online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
+		online = perf_cpu_map__new_online_cpus(); /* from /sys/devices/system/cpu/online */
 
 	return online;
 }
tools/perf/util/cputopo.c +1 -1
index 81cfc85f4668..8bbeb2dc76fd 100644
@@ -267,7 +267,7 @@ struct cpu_topology *cpu_topology__new(void)
 	ncpus = cpu__max_present_cpu().cpu;
 
 	/* build online CPU map */
-	map = perf_cpu_map__new(NULL);
+	map = perf_cpu_map__new_online_cpus();
 	if (map == NULL) {
 		pr_debug("failed to get system cpumap\n");
 		return NULL;
tools/perf/util/evlist.c +1 -1
index ff7f85ded89d..0ed3ce2aa8eb 100644
@@ -1352,7 +1352,7 @@ static int evlist__create_syswide_maps(struct evlist *evlist)
 	 * error, and we may not want to do that fallback to a
 	 * default cpu identity map :-\
 	 */
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		goto out;
 
tools/perf/util/perf_api_probe.c +2 -2
index e1e2d701599c..1de3b69cdf4a 100644
@@ -64,7 +64,7 @@ static bool perf_probe_api(setup_probe_fn_t fn)
 	struct perf_cpu cpu;
 	int ret, i = 0;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return false;
 	cpu = perf_cpu_map__cpu(cpus, 0);
@@ -140,7 +140,7 @@ bool perf_can_record_cpu_wide(void)
 	struct perf_cpu cpu;
 	int fd;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return false;
 
tools/perf/util/record.c +1 -1
index 40290382b2d7..87e817b3cf7e 100644
@@ -238,7 +238,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 	evsel = evlist__last(temp_evlist);
 
 	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
-		struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
+		struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 
 		if (cpus)
 			cpu =  perf_cpu_map__cpu(cpus, 0);
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 4/14
#10 lore raw

Re: [PATCH v1 04/14] libperf cpumap: Replace usage of perf_cpu_map__new(NULL)

Quoted 3 lines · Ian Rogers

Passing NULL to perf_cpu_map__new performs perf_cpu_map__new_online_cpus, just directly call perf_cpu_map__new_online_cpus to be more intention revealing.

If it's not too much effort I would make perf_cpu_map__new() assert if it's called with NULL now, to avoid any future divergance or hidden behavior again.

Either way:

Quoted 433 lines
---
 tools/lib/perf/Documentation/examples/sampling.c  | 2 +-
 tools/lib/perf/Documentation/libperf-sampling.txt | 2 +-
 tools/lib/perf/evlist.c                           | 2 +-
 tools/lib/perf/tests/test-evlist.c                | 4 ++--
 tools/lib/perf/tests/test-evsel.c                 | 2 +-
 tools/perf/arch/arm/util/cs-etm.c                 | 6 +++---
 tools/perf/arch/arm64/util/header.c               | 2 +-
 tools/perf/bench/epoll-ctl.c                      | 2 +-
 tools/perf/bench/epoll-wait.c                     | 2 +-
 tools/perf/bench/futex-hash.c                     | 2 +-
 tools/perf/bench/futex-lock-pi.c                  | 2 +-
 tools/perf/bench/futex-requeue.c                  | 2 +-
 tools/perf/bench/futex-wake-parallel.c            | 2 +-
 tools/perf/bench/futex-wake.c                     | 2 +-
 tools/perf/builtin-ftrace.c                       | 2 +-
 tools/perf/tests/code-reading.c                   | 2 +-
 tools/perf/tests/keep-tracking.c                  | 2 +-
 tools/perf/tests/mmap-basic.c                     | 2 +-
 tools/perf/tests/openat-syscall-all-cpus.c        | 2 +-
 tools/perf/tests/perf-time-to-tsc.c               | 2 +-
 tools/perf/tests/switch-tracking.c                | 2 +-
 tools/perf/tests/topology.c                       | 2 +-
 tools/perf/util/bpf_counter.c                     | 2 +-
 tools/perf/util/cpumap.c                          | 2 +-
 tools/perf/util/cputopo.c                         | 2 +-
 tools/perf/util/evlist.c                          | 2 +-
 tools/perf/util/perf_api_probe.c                  | 4 ++--
 tools/perf/util/record.c                          | 2 +-
 28 files changed, 32 insertions(+), 32 deletions(-)
tools/lib/perf/Documentation/examples/sampling.c +1 -1
index 8e1a926a9cfe..bc142f0664b5 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
 
 	libperf_init(libperf_print);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		fprintf(stderr, "failed to create cpus\n");
 		return -1;
tools/lib/perf/Documentation/libperf-sampling.txt +1 -1
index d6ca24f6ef78..2378980fab8a 100644
@@ -97,7 +97,7 @@ In this case we will monitor all the available CPUs:
 
 [source,c]
 --
- 42         cpus = perf_cpu_map__new(NULL);
+ 42         cpus = perf_cpu_map__new_online_cpus();
  43         if (!cpus) {
  44                 fprintf(stderr, "failed to create cpus\n");
  45                 return -1;
tools/lib/perf/evlist.c +1 -1
index 75f36218fdd9..058e3ff10f9b 100644
@@ -39,7 +39,7 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 	if (evsel->system_wide) {
 		/* System wide: set the cpu map of the evsel to all online CPUs. */
 		perf_cpu_map__put(evsel->cpus);
-		evsel->cpus = perf_cpu_map__new(NULL);
+		evsel->cpus = perf_cpu_map__new_online_cpus();
 	} else if (evlist->has_user_cpus && evsel->is_pmu_core) {
 		/*
 		 * User requested CPUs on a core PMU, ensure the requested CPUs
tools/lib/perf/tests/test-evlist.c +2 -2
index ab63878bacb9..10f70cb41ff1 100644
@@ -46,7 +46,7 @@ static int test_stat_cpu(void)
 	};
 	int err, idx;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evlist = perf_evlist__new();
@@ -350,7 +350,7 @@ static int test_mmap_cpus(void)
 
 	attr.config = id;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evlist = perf_evlist__new();
tools/lib/perf/tests/test-evsel.c +1 -1
index a11fc51bfb68..545ec3150546 100644
@@ -27,7 +27,7 @@ static int test_stat_cpu(void)
 	};
 	int err, idx;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	__T("failed to create cpus", cpus);
 
 	evsel = perf_evsel__new(&attr);
tools/perf/arch/arm/util/cs-etm.c +3 -3
index c6b7b3066324..77e6663c1703 100644
@@ -199,7 +199,7 @@ static int cs_etm_validate_config(struct auxtrace_record *itr,
 {
 	int i, err = -EINVAL;
 	struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 
 	/* Set option of each CPU we have */
 	for (i = 0; i < cpu__max_cpu().cpu; i++) {
@@ -536,7 +536,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	int i;
 	int etmv3 = 0, etmv4 = 0, ete = 0;
 	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 
 	/* cpu map is not empty, we have specific CPUs to work with */
 	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
@@ -802,7 +802,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 	u64 nr_cpu, type;
 	struct perf_cpu_map *cpu_map;
 	struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus;
-	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
 	struct cs_etm_recording *ptr =
 			container_of(itr, struct cs_etm_recording, itr);
 	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
tools/perf/arch/arm64/util/header.c +1 -1
index a2eef9ec5491..97037499152e 100644
@@ -57,7 +57,7 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 
 int get_cpuid(char *buf, size_t sz)
 {
-	struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 	int ret;
 
 	if (!cpus)
tools/perf/bench/epoll-ctl.c +1 -1
index 6bfffe83dde9..d3db73dac66a 100644
@@ -330,7 +330,7 @@ int bench_epoll_ctl(int argc, const char **argv)
 	act.sa_sigaction = toggle_done;
 	sigaction(SIGINT, &act, NULL);
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/epoll-wait.c +1 -1
index cb5174b53940..06bb3187660a 100644
@@ -444,7 +444,7 @@ int bench_epoll_wait(int argc, const char **argv)
 	act.sa_sigaction = toggle_done;
 	sigaction(SIGINT, &act, NULL);
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/futex-hash.c +1 -1
index 2005a3fa3026..0c69d20efa32 100644
@@ -138,7 +138,7 @@ int bench_futex_hash(int argc, const char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		goto errmem;
 
tools/perf/bench/futex-lock-pi.c +1 -1
index 092cbd52db82..7a4973346180 100644
@@ -172,7 +172,7 @@ int bench_futex_lock_pi(int argc, const char **argv)
 	if (argc)
 		goto err;
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/bench/futex-requeue.c +1 -1
index c0035990a33c..d9ad736c1a3e 100644
@@ -174,7 +174,7 @@ int bench_futex_requeue(int argc, const char **argv)
 	if (argc)
 		goto err;
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "cpu_map__new");
 
tools/perf/bench/futex-wake-parallel.c +1 -1
index 5ab0234d74e6..b66df553e561 100644
@@ -264,7 +264,7 @@ int bench_futex_wake_parallel(int argc, const char **argv)
 			err(EXIT_FAILURE, "mlockall");
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/bench/futex-wake.c +1 -1
index 18a5894af8bb..690fd6d3da13 100644
@@ -149,7 +149,7 @@ int bench_futex_wake(int argc, const char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	cpu = perf_cpu_map__new(NULL);
+	cpu = perf_cpu_map__new_online_cpus();
 	if (!cpu)
 		err(EXIT_FAILURE, "calloc");
 
tools/perf/builtin-ftrace.c +1 -1
index ac2e6c75f912..eb30c8eca488 100644
@@ -333,7 +333,7 @@ static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
 
 static int reset_tracing_cpu(void)
 {
-	struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
+	struct perf_cpu_map *cpumap = perf_cpu_map__new_online_cpus();
 	int ret;
 
 	ret = set_tracing_cpumask(cpumap);
tools/perf/tests/code-reading.c +1 -1
index 8620146d0378..7a3a7bbbec71 100644
@@ -610,7 +610,7 @@ static int do_test_code_reading(bool try_kcore)
 		goto out_put;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		pr_debug("perf_cpu_map__new failed\n");
 		goto out_put;
tools/perf/tests/keep-tracking.c +1 -1
index 8f4f9b632e1e..5a3b2bed07f3 100644
@@ -81,7 +81,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte
 	threads = thread_map__new(-1, getpid(), UINT_MAX);
 	CHECK_NOT_NULL__(threads);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	CHECK_NOT_NULL__(cpus);
 
 	evlist = evlist__new();
tools/perf/tests/mmap-basic.c +1 -1
index 886a13a77a16..012c8ae439fd 100644
@@ -52,7 +52,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
 		return -1;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (cpus == NULL) {
 		pr_debug("perf_cpu_map__new\n");
 		goto out_free_threads;
tools/perf/tests/openat-syscall-all-cpus.c +1 -1
index f3275be83a33..fb114118c876 100644
@@ -37,7 +37,7 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __mayb
 		return -1;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (cpus == NULL) {
 		pr_debug("perf_cpu_map__new\n");
 		goto out_thread_map_delete;
tools/perf/tests/perf-time-to-tsc.c +1 -1
index efcd71c2738a..bbe2ddeb9b74 100644
@@ -93,7 +93,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su
 	threads = thread_map__new(-1, getpid(), UINT_MAX);
 	CHECK_NOT_NULL__(threads);
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	CHECK_NOT_NULL__(cpus);
 
 	evlist = evlist__new();
tools/perf/tests/switch-tracking.c +1 -1
index e52b031bedc5..5cab17a1942e 100644
@@ -351,7 +351,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub
 		goto out_err;
 	}
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus) {
 		pr_debug("perf_cpu_map__new failed!\n");
 		goto out_err;
tools/perf/tests/topology.c +1 -1
index 9dee63734e66..2a842f53fbb5 100644
@@ -215,7 +215,7 @@ static int test__session_topology(struct test_suite *test __maybe_unused, int su
 	if (session_write_header(path))
 		goto free_path;
 
-	map = perf_cpu_map__new(NULL);
+	map = perf_cpu_map__new_online_cpus();
 	if (map == NULL) {
 		pr_debug("failed to get system cpumap\n");
 		goto free_path;
tools/perf/util/bpf_counter.c +1 -1
index 7f9b0e46e008..7a8af60e0f51 100644
@@ -455,7 +455,7 @@ static int bperf__load(struct evsel *evsel, struct target *target)
 		return -1;
 
 	if (!all_cpu_map) {
-		all_cpu_map = perf_cpu_map__new(NULL);
+		all_cpu_map = perf_cpu_map__new_online_cpus();
 		if (!all_cpu_map)
 			return -1;
 	}
tools/perf/util/cpumap.c +1 -1
index 0e090e8bc334..0581ee0fa5f2 100644
@@ -672,7 +672,7 @@ struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
 	static struct perf_cpu_map *online;
 
 	if (!online)
-		online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
+		online = perf_cpu_map__new_online_cpus(); /* from /sys/devices/system/cpu/online */
 
 	return online;
 }
tools/perf/util/cputopo.c +1 -1
index 81cfc85f4668..8bbeb2dc76fd 100644
@@ -267,7 +267,7 @@ struct cpu_topology *cpu_topology__new(void)
 	ncpus = cpu__max_present_cpu().cpu;
 
 	/* build online CPU map */
-	map = perf_cpu_map__new(NULL);
+	map = perf_cpu_map__new_online_cpus();
 	if (map == NULL) {
 		pr_debug("failed to get system cpumap\n");
 		return NULL;
tools/perf/util/evlist.c +1 -1
index ff7f85ded89d..0ed3ce2aa8eb 100644
@@ -1352,7 +1352,7 @@ static int evlist__create_syswide_maps(struct evlist *evlist)
 	 * error, and we may not want to do that fallback to a
 	 * default cpu identity map :-\
 	 */
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		goto out;
 
tools/perf/util/perf_api_probe.c +2 -2
index e1e2d701599c..1de3b69cdf4a 100644
@@ -64,7 +64,7 @@ static bool perf_probe_api(setup_probe_fn_t fn)
 	struct perf_cpu cpu;
 	int ret, i = 0;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return false;
 	cpu = perf_cpu_map__cpu(cpus, 0);
@@ -140,7 +140,7 @@ bool perf_can_record_cpu_wide(void)
 	struct perf_cpu cpu;
 	int fd;
 
-	cpus = perf_cpu_map__new(NULL);
+	cpus = perf_cpu_map__new_online_cpus();
 	if (!cpus)
 		return false;
 
tools/perf/util/record.c +1 -1
index 40290382b2d7..87e817b3cf7e 100644
@@ -238,7 +238,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 	evsel = evlist__last(temp_evlist);
 
 	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
-		struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
+		struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 
 		if (cpus)
 			cpu =  perf_cpu_map__cpu(cpus, 0);
author reviewer
#11 lore raw

PATCH 5/14 libperf cpumap: Add for_each_cpu that skips the "any CPU" case

In reply to Ian Rogers

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.

---
 tools/lib/perf/include/perf/cpumap.h | 6 ++++++
 tools/perf/builtin-record.c          | 4 +---
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/lib/perf/include/perf/cpumap.h +6 -0
index 9cf361fc5edc..dbe0a7352b64 100644
@@ -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)++)
 
tools/perf/builtin-record.c +1 -3
index 8ec818568662..066f9232e947 100644
@@ -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;
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 5/14
#12 lore raw

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

Quoted 5 lines · Ian Rogers

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.

Quoted 37 lines
---
 tools/lib/perf/include/perf/cpumap.h | 6 ++++++
 tools/perf/builtin-record.c          | 4 +---
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/lib/perf/include/perf/cpumap.h +6 -0
index 9cf361fc5edc..dbe0a7352b64 100644
@@ -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)++)
 
tools/perf/builtin-record.c +1 -3
index 8ec818568662..066f9232e947 100644
@@ -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;
author reviewer
#13 lore raw

PATCH 6/14 libperf cpumap: Add any, empty and min helpers

In reply to Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +27 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+	if (!map)
+		return true;
+
+	return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+	return map == NULL;
+}
+
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
 	int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
 	return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+	struct perf_cpu cpu, result = {
+		.cpu = -1
+	};
+	int idx;
+
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+		result = cpu;
+		break;
+	}
+	return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
 	struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
 		perf_cpu_map__has_any_cpu_or_is_empty;
+		perf_cpu_map__is_any_cpu_or_is_empty;
+		perf_cpu_map__is_empty;
+		perf_cpu_map__has_any_cpu;
+		perf_cpu_map__min;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#14 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

Quoted 21 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +3 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+	if (!map)

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 331

Quoted 10 lines
tools/lib/perf/cpumap.c +10 -0
+		return true;
+
+	return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+	return map == NULL;
+}
+

Maybe it doesn't currently happen, but it seems a bit weird that the 'new' function can create a map of length 0 which would return empty == false here.

Could we either make this check also return true for maps with length 0, or prevent the new function from returning a map of 0 length?

Quoted 66 lines
tools/lib/perf/cpumap.c +14 -0
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
 	int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
 	return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+	struct perf_cpu cpu, result = {
+		.cpu = -1
+	};
+	int idx;
+
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+		result = cpu;
+		break;
+	}
+	return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
 	struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
 		perf_cpu_map__has_any_cpu_or_is_empty;
+		perf_cpu_map__is_any_cpu_or_is_empty;
+		perf_cpu_map__is_empty;
+		perf_cpu_map__has_any_cpu;
+		perf_cpu_map__min;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
#15 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

Quoted 39 lines · James Clark
Quoted 21 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +3 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+	if (!map)

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 331

Quoted 10 lines
tools/lib/perf/cpumap.c +10 -0
+		return true;
+
+	return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+	return map == NULL;
+}
+

Maybe it doesn't currently happen, but it seems a bit weird that the 'new' function can create a map of length 0 which would return empty == false here.

Quoted 3 lines

Could we either make this check also return true for maps with length 0, or prevent the new function from returning a map of 0 length?

By 'new' I actually meant perf_cpu_map__alloc(). I see perf_cpu_map__new() actually takes a string, and returns all online CPUs if the string is null, but similarly I don't see a use case where that string is NULL.

Having something return either the parsed string, or all online CPUs if the string was null seems like unnecessary hidden behaviour so it would be good to remove that one too.

Quoted 67 lines
Quoted 66 lines
tools/lib/perf/cpumap.c +14 -0
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
 	int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
 	return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+	struct perf_cpu cpu, result = {
+		.cpu = -1
+	};
+	int idx;
+
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+		result = cpu;
+		break;
+	}
+	return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
 	struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
 		perf_cpu_map__has_any_cpu_or_is_empty;
+		perf_cpu_map__is_any_cpu_or_is_empty;
+		perf_cpu_map__is_empty;
+		perf_cpu_map__has_any_cpu;
+		perf_cpu_map__min;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
author reviewer
#16 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

In reply to James Clark
Quoted 40 lines · James Clark
Quoted 21 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +3 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
      return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+     if (!map)

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 331

Quoted 10 lines
tools/lib/perf/cpumap.c +10 -0
+             return true;
+
+     return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+     return map == NULL;
+}
+

Maybe it doesn't currently happen, but it seems a bit weird that the 'new' function can create a map of length 0 which would return empty == false here.

Quoted 2 lines

Could we either make this check also return true for maps with length 0, or prevent the new function from returning a map of 0 length?

We can add an assert or return NULL for size 0 in alloc. I think I prefer the return NULL option. It should never happen but it feels the right defensive thing to do.

Thanks,
Ian

Quoted 66 lines
Quoted 66 lines
tools/lib/perf/cpumap.c +14 -0
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
      int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
      return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+     struct perf_cpu cpu, result = {
+             .cpu = -1
+     };
+     int idx;
+
+     perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+             result = cpu;
+             break;
+     }
+     return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
      struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
              perf_cpu_map__nr;
              perf_cpu_map__cpu;
              perf_cpu_map__has_any_cpu_or_is_empty;
+             perf_cpu_map__is_any_cpu_or_is_empty;
+             perf_cpu_map__is_empty;
+             perf_cpu_map__has_any_cpu;
+             perf_cpu_map__min;
              perf_cpu_map__max;
              perf_cpu_map__has;
              perf_thread_map__new_array;
#17 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

In reply to Ian Rogers
Quoted 15 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +0 -0
index 49fc98e16514..7403819da8fd 100644

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 325

Quoted 10 lines
tools/lib/perf/cpumap.c +7 -0
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
 	return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+	if (!map)
+		return true;
+
+	return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}

I'm struggling to understand the relevance of the difference between has_any and is_any I see that there is a slight difference, but could it not be refactored out so we only need one?

Do you ever get an 'any' map that has more than 1 entry? It's quite a subtle difference that is_any returns false if the first one is 'any' but then there are subsequent entries. Whereas has_any would return true. I'm not sure if future readers would be able to appreciate that.

I see has_any is only used twice, both on evlist->all_cpus. Is there something about that member that means it could have a map that has an 'any' mixed with CPUs? Wouldn't that have the same result as a normal 'any' anyway?

Quoted 72 lines
tools/lib/perf/cpumap.c +20 -0
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+	return map == NULL;
+}
+
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
 	int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
 	return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+	struct perf_cpu cpu, result = {
+		.cpu = -1
+	};
+	int idx;
+
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+		result = cpu;
+		break;
+	}
+	return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
 	struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__nr;
 		perf_cpu_map__cpu;
 		perf_cpu_map__has_any_cpu_or_is_empty;
+		perf_cpu_map__is_any_cpu_or_is_empty;
+		perf_cpu_map__is_empty;
+		perf_cpu_map__has_any_cpu;
+		perf_cpu_map__min;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
 		perf_thread_map__new_array;
author reviewer
#18 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

Quoted 22 lines · James Clark
Quoted 18 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +0 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
      return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
Quoted 11 lines

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 325

Quoted 7 lines
tools/lib/perf/cpumap.c +7 -0
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+     if (!map)
+             return true;
+
+     return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}

I'm struggling to understand the relevance of the difference between has_any and is_any I see that there is a slight difference, but could it not be refactored out so we only need one?

Yep, that's what these changes are working toward. For has any the set {-1, 0, 1} would return true while is any will return false. Previously the has any behavior was called "empty" which I think is actively misleading.

Quoted 9 lines

Do you ever get an 'any' map that has more than 1 entry? It's quite a subtle difference that is_any returns false if the first one is 'any' but then there are subsequent entries. Whereas has_any would return true. I'm not sure if future readers would be able to appreciate that.

I see has_any is only used twice, both on evlist->all_cpus. Is there something about that member that means it could have a map that has an 'any' mixed with CPUs? Wouldn't that have the same result as a normal 'any' anyway?

The dummy event may be opened on any CPU but then a particular event may be opened on certain CPUs. We merge CPU maps in places like evlist so that we can iterate the appropriate CPUs for events and open/enable/disable/close all events on a certain CPU at the same time (we also set the affinity to that CPU to avoid IPIs). What I'm hoping to do in these changes is reduce the ambiguity, the corner cases are by their nature unusual.

An example of a corner case is, uncore events often get opened just on CPU 0 but on a multi-socket system you may have a CPU 32 that also needs to open the event. Previous code treated the CPU map index and value it contained pretty interchangeably. This is often fine for the core PMU but is clearly wrong in this uncore case, {0, 32} has indexes 0 and 1 but those indexes don't match the CPU numbers. The case of -1 has often previously been called dummy but I'm trying to call it the "any CPU" case to match the perf_event_open man page (I'm hoping it also makes it less ambiguous with any CPU being used with a particular event like cycles, calling it dummy makes the event sound like it may have sideband data). The difference between "all CPUs" and "any CPU" is that an evsel for all CPUs would need the event opening individually on each CPU, while any CPU events are a single open call. Any CPU is only valid to perf_event_open if a PID is specified. Depending on the set up there could be overlaps in what they count but hopefully it is clearer what the distinction is. I believe the case of "any CPU" and specific CPU numbers is more common with aux buffers and Adrian has mentioned needing it for intel-pt.

Thanks,
Ian

Quoted 72 lines
Quoted 72 lines
tools/lib/perf/cpumap.c +20 -0
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+     return map == NULL;
+}
+
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
      int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
      return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+     struct perf_cpu cpu, result = {
+             .cpu = -1
+     };
+     int idx;
+
+     perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+             result = cpu;
+             break;
+     }
+     return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
      struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
              perf_cpu_map__nr;
              perf_cpu_map__cpu;
              perf_cpu_map__has_any_cpu_or_is_empty;
+             perf_cpu_map__is_any_cpu_or_is_empty;
+             perf_cpu_map__is_empty;
+             perf_cpu_map__has_any_cpu;
+             perf_cpu_map__min;
              perf_cpu_map__max;
              perf_cpu_map__has;
              perf_thread_map__new_array;
#19 lore raw

Re: [PATCH v1 06/14] libperf cpumap: Add any, empty and min helpers

Quoted 77 lines · Ian Rogers
Quoted 22 lines · James Clark
Quoted 18 lines · Ian Rogers

Additional helpers to better replace
perf_cpu_map__has_any_cpu_or_is_empty.

---
 tools/lib/perf/cpumap.c              | 27 +++++++++++++++++++++++++++
 tools/lib/perf/include/perf/cpumap.h | 16 ++++++++++++++++
 tools/lib/perf/libperf.map           |  4 ++++
 3 files changed, 47 insertions(+)
tools/lib/perf/cpumap.c +0 -0
index 49fc98e16514..7403819da8fd 100644
@@ -316,6 +316,19 @@ bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map)
      return map ? __perf_cpu_map__cpu(map, 0).cpu == -1 : true;
 }
 
Quoted 11 lines

tools/lib/perf/cpumap.c bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map) · line 325

Quoted 7 lines
tools/lib/perf/cpumap.c +7 -0
+bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map)
+{
+     if (!map)
+             return true;
+
+     return __perf_cpu_map__nr(map) == 1 && __perf_cpu_map__cpu(map, 0).cpu == -1;
+}

I'm struggling to understand the relevance of the difference between has_any and is_any I see that there is a slight difference, but could it not be refactored out so we only need one?

Yep, that's what these changes are working toward. For has any the set {-1, 0, 1} would return true while is any will return false. Previously the has any behavior was called "empty" which I think is actively misleading.

Quoted 9 lines

Do you ever get an 'any' map that has more than 1 entry? It's quite a subtle difference that is_any returns false if the first one is 'any' but then there are subsequent entries. Whereas has_any would return true. I'm not sure if future readers would be able to appreciate that.

I see has_any is only used twice, both on evlist->all_cpus. Is there something about that member that means it could have a map that has an 'any' mixed with CPUs? Wouldn't that have the same result as a normal 'any' anyway?

The dummy event may be opened on any CPU but then a particular event may be opened on certain CPUs. We merge CPU maps in places like evlist so that we can iterate the appropriate CPUs for events and open/enable/disable/close all events on a certain CPU at the same time (we also set the affinity to that CPU to avoid IPIs). What I'm hoping to do in these changes is reduce the ambiguity, the corner cases are by their nature unusual.

An example of a corner case is, uncore events often get opened just on CPU 0 but on a multi-socket system you may have a CPU 32 that also needs to open the event. Previous code treated the CPU map index and value it contained pretty interchangeably. This is often fine for the core PMU but is clearly wrong in this uncore case, {0, 32} has indexes 0 and 1 but those indexes don't match the CPU numbers. The case of -1 has often previously been called dummy but I'm trying to call it the "any CPU" case to match the perf_event_open man page (I'm hoping it also makes it less ambiguous with any CPU being used with a particular event like cycles, calling it dummy makes the event sound like it may have sideband data). The difference between "all CPUs" and "any CPU" is that an evsel for all CPUs would need the event opening individually on each CPU, while any CPU events are a single open call. Any CPU is only valid to perf_event_open if a PID is specified. Depending on the set up there could be overlaps in what they count but hopefully it is clearer what the distinction is. I believe the case of "any CPU" and specific CPU numbers is more common with aux buffers and Adrian has mentioned needing it for intel-pt.

Quoted 3 lines

Thanks,
Ian

Thanks for explaining. I suppose I didn't realise that 'any' could be merged with per-cpu maps, but it makes sense.

Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
tools/lib/perf/cpumap.c +20 -0
+
+bool perf_cpu_map__is_empty(const struct perf_cpu_map *map)
+{
+     return map == NULL;
+}
+
 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
 {
      int low, high;
@@ -372,6 +385,20 @@ bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map)
      return map && __perf_cpu_map__cpu(map, 0).cpu == -1;
 }
 
+struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map)
+{
+     struct perf_cpu cpu, result = {
+             .cpu = -1
+     };
+     int idx;
+
+     perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
+             result = cpu;
+             break;
+     }
+     return result;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
      struct perf_cpu result = {
tools/lib/perf/include/perf/cpumap.h +16 -0
index dbe0a7352b64..523e4348fc96 100644
@@ -50,6 +50,22 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
  */
 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__is_empty - does the map contain no values and it doesn't
+ *                          contain the special "any CPU"/dummy value.
+ */
+LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
+LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
tools/lib/perf/libperf.map +4 -0
index 10b3f3722642..2aa79b696032 100644
@@ -10,6 +10,10 @@ LIBPERF_0.0.1 {
              perf_cpu_map__nr;
              perf_cpu_map__cpu;
              perf_cpu_map__has_any_cpu_or_is_empty;
+             perf_cpu_map__is_any_cpu_or_is_empty;
+             perf_cpu_map__is_empty;
+             perf_cpu_map__has_any_cpu;
+             perf_cpu_map__min;
              perf_cpu_map__max;
              perf_cpu_map__has;
              perf_thread_map__new_array;
author reviewer
#20 lore raw

PATCH 7/14 perf arm-spe/cs-etm: Directly iterate CPU maps

In reply to Ian Rogers

Rather than iterate all CPUs and see if they are in CPU maps, directly iterate the CPU map. Similarly make use of the intersect function. Switch perf_cpu_map__has_any_cpu_or_is_empty to more appropriate alternatives.

---
 tools/perf/arch/arm/util/cs-etm.c    | 77 ++++++++++++----------------
 tools/perf/arch/arm64/util/arm-spe.c |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +32 -45
index 77e6663c1703..a68a72f2f668 100644
@@ -197,38 +197,32 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,
 static int cs_etm_validate_config(struct auxtrace_record *itr,
 				  struct evsel *evsel)
 {
-	int i, err = -EINVAL;
+	int idx, err = -EINVAL;
 	struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu_map *intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+	struct perf_cpu cpu;
 
-	/* Set option of each CPU we have */
-	for (i = 0; i < cpu__max_cpu().cpu; i++) {
-		struct perf_cpu cpu = { .cpu = i, };
-
-		/*
-		 * In per-cpu case, do the validation for CPUs to work with.
-		 * In per-thread case, the CPU map is empty.  Since the traced
-		 * program can run on any CPUs in this case, thus don't skip
-		 * validation.
-		 */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
-		    !perf_cpu_map__has(event_cpus, cpu))
-			continue;
-
-		if (!perf_cpu_map__has(online_cpus, cpu))
-			continue;
+	perf_cpu_map__put(online_cpus);
 
-		err = cs_etm_validate_context_id(itr, evsel, i);
+	/*
+	 * Set option of each CPU we have. In per-cpu case, do the validation
+	 * for CPUs to work with.  In per-thread case, the CPU map is empty.
+	 * Since the traced program can run on any CPUs in this case, thus don't
+	 * skip validation.
+	 */
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+		err = cs_etm_validate_context_id(itr, evsel, cpu.cpu);
 		if (err)
 			goto out;
-		err = cs_etm_validate_timestamp(itr, evsel, i);
+		err = cs_etm_validate_timestamp(itr, evsel, idx);
 		if (err)
 			goto out;
 	}
 
 	err = 0;
 out:
-	perf_cpu_map__put(online_cpus);
+	perf_cpu_map__put(intersect_cpus);
 	return err;
 }
 
@@ -435,7 +429,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	 * Also the case of per-cpu mmaps, need the contextID in order to be notified
 	 * when a context switch happened.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
 					   "timestamp", 1);
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +455,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 		evsel__set_sample_bit(evsel, TIME);
 
 	err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -533,38 +527,32 @@ static size_t
 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 		      struct evlist *evlist __maybe_unused)
 {
-	int i;
+	int idx;
 	int etmv3 = 0, etmv4 = 0, ete = 0;
 	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu cpu;
 
 	/* cpu map is not empty, we have specific CPUs to work with */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(event_cpus, cpu) ||
-			    !perf_cpu_map__has(online_cpus, cpu))
-				continue;
+	if (!perf_cpu_map__is_empty(event_cpus)) {
+		struct perf_cpu_map *intersect_cpus =
+			perf_cpu_map__intersect(event_cpus, online_cpus);
 
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
 		}
+		perf_cpu_map__put(intersect_cpus);
 	} else {
 		/* get configuration for all CPUs in the system */
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(online_cpus, cpu))
-				continue;
-
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu(cpu, idx, online_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
@@ -814,15 +802,14 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 		return -EINVAL;
 
 	/* If the cpu_map is empty all online CPUs are involved */
-	if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
+	if (perf_cpu_map__is_empty(event_cpus)) {
 		cpu_map = online_cpus;
 	} else {
 		/* Make sure all specified CPUs are online */
-		for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
-			struct perf_cpu cpu = { .cpu = i, };
+		struct perf_cpu cpu;
 
-			if (perf_cpu_map__has(event_cpus, cpu) &&
-			    !perf_cpu_map__has(online_cpus, cpu))
+		perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+			if (!perf_cpu_map__has(online_cpus, cpu))
 				return -EINVAL;
 		}
 
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index 51ccbfd3d246..0b52e67edb3b 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
 	 * also enable the timestamp tracing for samples correlation.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(arm_spe_evsel, CPU);
 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
 					   "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	tracking_evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(tracking_evsel, TIME);
 		evsel__set_sample_bit(tracking_evsel, CPU);
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#21 lore raw

Re: [PATCH v1 07/14] perf arm-spe/cs-etm: Directly iterate CPU maps

Quoted 29 lines · Ian Rogers

Rather than iterate all CPUs and see if they are in CPU maps, directly iterate the CPU map. Similarly make use of the intersect function. Switch perf_cpu_map__has_any_cpu_or_is_empty to more appropriate alternatives.

---
 tools/perf/arch/arm/util/cs-etm.c    | 77 ++++++++++++----------------
 tools/perf/arch/arm64/util/arm-spe.c |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +3 -4
index 77e6663c1703..a68a72f2f668 100644
@@ -197,38 +197,32 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,
 static int cs_etm_validate_config(struct auxtrace_record *itr,
 				  struct evsel *evsel)
 {
-	int i, err = -EINVAL;
+	int idx, err = -EINVAL;
 	struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu_map *intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+	struct perf_cpu cpu;
 
-	/* Set option of each CPU we have */
-	for (i = 0; i < cpu__max_cpu().cpu; i++) {
-		struct perf_cpu cpu = { .cpu = i, };

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 216

Quoted 10 lines
tools/perf/arch/arm/util/cs-etm.c +0 -10
-
-		/*
-		 * In per-cpu case, do the validation for CPUs to work with.
-		 * In per-thread case, the CPU map is empty.  Since the traced
-		 * program can run on any CPUs in this case, thus don't skip
-		 * validation.
-		 */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
-		    !perf_cpu_map__has(event_cpus, cpu))
-			continue;

This has broken validation for per-thread sessions. perf_cpu_map__intersect() doesn't seem to be able to handle the case where an 'any' map intersected with an online map should return the online map. Or at least it should for this to work, and it seems to make sense for it to work that way.

At least that was my initial impression, but I only debugged it and saw that the loop is now skipped entirely.

Quoted 140 lines
tools/perf/arch/arm/util/cs-etm.c +29 -31
-
-		if (!perf_cpu_map__has(online_cpus, cpu))
-			continue;
+	perf_cpu_map__put(online_cpus);
 
-		err = cs_etm_validate_context_id(itr, evsel, i);
+	/*
+	 * Set option of each CPU we have. In per-cpu case, do the validation
+	 * for CPUs to work with.  In per-thread case, the CPU map is empty.
+	 * Since the traced program can run on any CPUs in this case, thus don't
+	 * skip validation.
+	 */
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+		err = cs_etm_validate_context_id(itr, evsel, cpu.cpu);
 		if (err)
 			goto out;
-		err = cs_etm_validate_timestamp(itr, evsel, i);
+		err = cs_etm_validate_timestamp(itr, evsel, idx);
 		if (err)
 			goto out;
 	}
 
 	err = 0;
 out:
-	perf_cpu_map__put(online_cpus);
+	perf_cpu_map__put(intersect_cpus);
 	return err;
 }
 
@@ -435,7 +429,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	 * Also the case of per-cpu mmaps, need the contextID in order to be notified
 	 * when a context switch happened.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
 					   "timestamp", 1);
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +455,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 		evsel__set_sample_bit(evsel, TIME);
 
 	err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -533,38 +527,32 @@ static size_t
 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 		      struct evlist *evlist __maybe_unused)
 {
-	int i;
+	int idx;
 	int etmv3 = 0, etmv4 = 0, ete = 0;
 	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu cpu;
 
 	/* cpu map is not empty, we have specific CPUs to work with */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(event_cpus, cpu) ||
-			    !perf_cpu_map__has(online_cpus, cpu))
-				continue;
+	if (!perf_cpu_map__is_empty(event_cpus)) {
+		struct perf_cpu_map *intersect_cpus =
+			perf_cpu_map__intersect(event_cpus, online_cpus);
 
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
 		}
+		perf_cpu_map__put(intersect_cpus);
 	} else {
 		/* get configuration for all CPUs in the system */
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(online_cpus, cpu))
-				continue;
-
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu(cpu, idx, online_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
@@ -814,15 +802,14 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 		return -EINVAL;
 
 	/* If the cpu_map is empty all online CPUs are involved */
-	if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
+	if (perf_cpu_map__is_empty(event_cpus)) {
 		cpu_map = online_cpus;
 	} else {
 		/* Make sure all specified CPUs are online */
-		for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
-			struct perf_cpu cpu = { .cpu = i, };
+		struct perf_cpu cpu;
 
-			if (perf_cpu_map__has(event_cpus, cpu) &&
-			    !perf_cpu_map__has(online_cpus, cpu))
+		perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+			if (!perf_cpu_map__has(online_cpus, cpu))
 				return -EINVAL;
 		}
 
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index 51ccbfd3d246..0b52e67edb3b 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
 	 * also enable the timestamp tracing for samples correlation.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(arm_spe_evsel, CPU);
 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
 					   "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	tracking_evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(tracking_evsel, TIME);
 		evsel__set_sample_bit(tracking_evsel, CPU);
 
#22 lore raw

Re: [PATCH v1 07/14] perf arm-spe/cs-etm: Directly iterate CPU maps

Quoted 122 lines · James Clark
Quoted 29 lines · Ian Rogers

Rather than iterate all CPUs and see if they are in CPU maps, directly iterate the CPU map. Similarly make use of the intersect function. Switch perf_cpu_map__has_any_cpu_or_is_empty to more appropriate alternatives.

---
 tools/perf/arch/arm/util/cs-etm.c    | 77 ++++++++++++----------------
 tools/perf/arch/arm64/util/arm-spe.c |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +3 -4
index 77e6663c1703..a68a72f2f668 100644
@@ -197,38 +197,32 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,
 static int cs_etm_validate_config(struct auxtrace_record *itr,
 				  struct evsel *evsel)
 {
-	int i, err = -EINVAL;
+	int idx, err = -EINVAL;
 	struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu_map *intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+	struct perf_cpu cpu;
 
-	/* Set option of each CPU we have */
-	for (i = 0; i < cpu__max_cpu().cpu; i++) {
-		struct perf_cpu cpu = { .cpu = i, };

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 216

Quoted 10 lines
tools/perf/arch/arm/util/cs-etm.c +0 -10
-
-		/*
-		 * In per-cpu case, do the validation for CPUs to work with.
-		 * In per-thread case, the CPU map is empty.  Since the traced
-		 * program can run on any CPUs in this case, thus don't skip
-		 * validation.
-		 */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
-		    !perf_cpu_map__has(event_cpus, cpu))
-			continue;

This has broken validation for per-thread sessions. perf_cpu_map__intersect() doesn't seem to be able to handle the case where an 'any' map intersected with an online map should return the online map. Or at least it should for this to work, and it seems to make sense for it to work that way.

At least that was my initial impression, but I only debugged it and saw that the loop is now skipped entirely.

Quoted 70 lines
tools/perf/arch/arm/util/cs-etm.c +18 -16
-
-		if (!perf_cpu_map__has(online_cpus, cpu))
-			continue;
+	perf_cpu_map__put(online_cpus);
 
-		err = cs_etm_validate_context_id(itr, evsel, i);
+	/*
+	 * Set option of each CPU we have. In per-cpu case, do the validation
+	 * for CPUs to work with.  In per-thread case, the CPU map is empty.
+	 * Since the traced program can run on any CPUs in this case, thus don't
+	 * skip validation.
+	 */
+	perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+		err = cs_etm_validate_context_id(itr, evsel, cpu.cpu);
 		if (err)
 			goto out;
-		err = cs_etm_validate_timestamp(itr, evsel, i);
+		err = cs_etm_validate_timestamp(itr, evsel, idx);
 		if (err)
 			goto out;
 	}
 
 	err = 0;
 out:
-	perf_cpu_map__put(online_cpus);
+	perf_cpu_map__put(intersect_cpus);
 	return err;
 }
 
@@ -435,7 +429,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	 * Also the case of per-cpu mmaps, need the contextID in order to be notified
 	 * when a context switch happened.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
 					   "timestamp", 1);
 		evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +455,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
 	evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 		evsel__set_sample_bit(evsel, TIME);
 
 	err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -533,38 +527,32 @@ static size_t
 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 		      struct evlist *evlist __maybe_unused)
 {
-	int i;
+	int idx;
 	int etmv3 = 0, etmv4 = 0, ete = 0;
 	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
 	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct perf_cpu cpu;
 
 	/* cpu map is not empty, we have specific CPUs to work with */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(event_cpus, cpu) ||
-			    !perf_cpu_map__has(online_cpus, cpu))
-				continue;
+	if (!perf_cpu_map__is_empty(event_cpus)) {
+		struct perf_cpu_map *intersect_cpus =
+			perf_cpu_map__intersect(event_cpus, online_cpus);
 
Quoted 3 lines

tools/perf/arch/arm/util/cs-etm.c static size_t · line 542

Quoted 3 lines
tools/perf/arch/arm/util/cs-etm.c +2 -1
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))

Similar problem here. For a per-thread session, the CPU map is not empty (it's an 'any' map, presumably length 1), so it comes into this first if, rather than the else below which is for the 'any' scenario.

Then the intersect with online CPUs results in an empty map, so no CPU metadata is recorded, then the session fails.

If you made the intersect work in the way I mentioned above we could also delete the else below, because that's just another way to convert from 'any' to 'all online'.

Quoted 67 lines
Quoted 67 lines
tools/perf/arch/arm/util/cs-etm.c +9 -14
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
 		}
+		perf_cpu_map__put(intersect_cpus);
 	} else {
 		/* get configuration for all CPUs in the system */
-		for (i = 0; i < cpu__max_cpu().cpu; i++) {
-			struct perf_cpu cpu = { .cpu = i, };
-
-			if (!perf_cpu_map__has(online_cpus, cpu))
-				continue;
-
-			if (cs_etm_is_ete(itr, i))
+		perf_cpu_map__for_each_cpu(cpu, idx, online_cpus) {
+			if (cs_etm_is_ete(itr, cpu.cpu))
 				ete++;
-			else if (cs_etm_is_etmv4(itr, i))
+			else if (cs_etm_is_etmv4(itr, cpu.cpu))
 				etmv4++;
 			else
 				etmv3++;
@@ -814,15 +802,14 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
 		return -EINVAL;
 
 	/* If the cpu_map is empty all online CPUs are involved */
-	if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
+	if (perf_cpu_map__is_empty(event_cpus)) {
 		cpu_map = online_cpus;
 	} else {
 		/* Make sure all specified CPUs are online */
-		for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
-			struct perf_cpu cpu = { .cpu = i, };
+		struct perf_cpu cpu;
 
-			if (perf_cpu_map__has(event_cpus, cpu) &&
-			    !perf_cpu_map__has(online_cpus, cpu))
+		perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+			if (!perf_cpu_map__has(online_cpus, cpu))
 				return -EINVAL;
 		}
 
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index 51ccbfd3d246..0b52e67edb3b 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
 	 * also enable the timestamp tracing for samples correlation.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(arm_spe_evsel, CPU);
 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
 					   "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
 	tracking_evsel->core.attr.sample_period = 1;
 
 	/* In per-cpu case, always need the time of mmap events etc */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		evsel__set_sample_bit(tracking_evsel, TIME);
 		evsel__set_sample_bit(tracking_evsel, CPU);
 
author reviewer
#23 lore raw

Re: [PATCH v1 07/14] perf arm-spe/cs-etm: Directly iterate CPU maps

Quoted 61 lines · James Clark
Quoted 57 lines · James Clark
Quoted 29 lines · Ian Rogers

Rather than iterate all CPUs and see if they are in CPU maps, directly iterate the CPU map. Similarly make use of the intersect function. Switch perf_cpu_map__has_any_cpu_or_is_empty to more appropriate alternatives.

---
 tools/perf/arch/arm/util/cs-etm.c    | 77 ++++++++++++----------------
 tools/perf/arch/arm64/util/arm-spe.c |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +3 -4
index 77e6663c1703..a68a72f2f668 100644
@@ -197,38 +197,32 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,
 static int cs_etm_validate_config(struct auxtrace_record *itr,
                               struct evsel *evsel)
 {
-    int i, err = -EINVAL;
+    int idx, err = -EINVAL;
     struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
     struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+    struct perf_cpu_map *intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+    struct perf_cpu cpu;
 
-    /* Set option of each CPU we have */
-    for (i = 0; i < cpu__max_cpu().cpu; i++) {
-            struct perf_cpu cpu = { .cpu = i, };

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 216

Quoted 10 lines
tools/perf/arch/arm/util/cs-etm.c +0 -10
-
-            /*
-             * In per-cpu case, do the validation for CPUs to work with.
-             * In per-thread case, the CPU map is empty.  Since the traced
-             * program can run on any CPUs in this case, thus don't skip
-             * validation.
-             */
-            if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
-                !perf_cpu_map__has(event_cpus, cpu))
-                    continue;

This has broken validation for per-thread sessions. perf_cpu_map__intersect() doesn't seem to be able to handle the case where an 'any' map intersected with an online map should return the online map. Or at least it should for this to work, and it seems to make sense for it to work that way.

At least that was my initial impression, but I only debugged it and saw that the loop is now skipped entirely.

Quoted 5 lines
tools/perf/arch/arm/util/cs-etm.c +1 -3
-
-            if (!perf_cpu_map__has(online_cpus, cpu))
-                    continue;
+    perf_cpu_map__put(online_cpus);
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/arch/arm/util/cs-etm.c +4 -1
-            err = cs_etm_validate_context_id(itr, evsel, i);
+    /*
+     * Set option of each CPU we have. In per-cpu case, do the validation
+     * for CPUs to work with.  In per-thread case, the CPU map is empty.
+     * Since the traced program can run on any CPUs in this case, thus don't
Quoted 8 lines

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 218

Quoted 8 lines
tools/perf/arch/arm/util/cs-etm.c +5 -1
+     * skip validation.
+     */
+    perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+            err = cs_etm_validate_context_id(itr, evsel, cpu.cpu);
             if (err)
                     goto out;
-            err = cs_etm_validate_timestamp(itr, evsel, i);
+            err = cs_etm_validate_timestamp(itr, evsel, idx);

I think this is an error, idx shouldn't be used here, cpu.cpu should.

Quoted 60 lines
Quoted 52 lines
Quoted 52 lines
tools/perf/arch/arm/util/cs-etm.c +8 -11
             if (err)
                     goto out;
     }
 
     err = 0;
 out:
-    perf_cpu_map__put(online_cpus);
+    perf_cpu_map__put(intersect_cpus);
     return err;
 }
 
@@ -435,7 +429,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
      * Also the case of per-cpu mmaps, need the contextID in order to be notified
      * when a context switch happened.
      */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
                                        "timestamp", 1);
             evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +455,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
     evsel->core.attr.sample_period = 1;
 
     /* In per-cpu case, always need the time of mmap events etc */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
             evsel__set_sample_bit(evsel, TIME);
 
     err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -533,38 +527,32 @@ static size_t
 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
                   struct evlist *evlist __maybe_unused)
 {
-    int i;
+    int idx;
     int etmv3 = 0, etmv4 = 0, ete = 0;
     struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
     struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+    struct perf_cpu cpu;
 
     /* cpu map is not empty, we have specific CPUs to work with */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
-            for (i = 0; i < cpu__max_cpu().cpu; i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
-
-                    if (!perf_cpu_map__has(event_cpus, cpu) ||
-                        !perf_cpu_map__has(online_cpus, cpu))
-                            continue;
+    if (!perf_cpu_map__is_empty(event_cpus)) {
+            struct perf_cpu_map *intersect_cpus =
+                    perf_cpu_map__intersect(event_cpus, online_cpus);
 
Quoted 3 lines

tools/perf/arch/arm/util/cs-etm.c static size_t · line 542

Quoted 3 lines
tools/perf/arch/arm/util/cs-etm.c +2 -1
-                    if (cs_etm_is_ete(itr, i))
+            perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+                    if (cs_etm_is_ete(itr, cpu.cpu))

Similar problem here. For a per-thread session, the CPU map is not empty (it's an 'any' map, presumably length 1), so it comes into this first if, rather than the else below which is for the 'any' scenario.

Quoted 6 lines

Then the intersect with online CPUs results in an empty map, so no CPU metadata is recorded, then the session fails.

If you made the intersect work in the way I mentioned above we could also delete the else below, because that's just another way to convert from 'any' to 'all online'.

I don't think intersect of "all online" with an "any CPU" should return "all online" as these would be quite different options to perf_event_open. Let's see if the issue above fixes this change otherwise I can revert it to a more mechanical translation of the existing code into the new APIs.

Thanks,
Ian

Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
tools/perf/arch/arm/util/cs-etm.c +9 -14
                             ete++;
-                    else if (cs_etm_is_etmv4(itr, i))
+                    else if (cs_etm_is_etmv4(itr, cpu.cpu))
                             etmv4++;
                     else
                             etmv3++;
             }
+            perf_cpu_map__put(intersect_cpus);
     } else {
             /* get configuration for all CPUs in the system */
-            for (i = 0; i < cpu__max_cpu().cpu; i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
-
-                    if (!perf_cpu_map__has(online_cpus, cpu))
-                            continue;
-
-                    if (cs_etm_is_ete(itr, i))
+            perf_cpu_map__for_each_cpu(cpu, idx, online_cpus) {
+                    if (cs_etm_is_ete(itr, cpu.cpu))
                             ete++;
-                    else if (cs_etm_is_etmv4(itr, i))
+                    else if (cs_etm_is_etmv4(itr, cpu.cpu))
                             etmv4++;
                     else
                             etmv3++;
@@ -814,15 +802,14 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
             return -EINVAL;
 
     /* If the cpu_map is empty all online CPUs are involved */
-    if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
+    if (perf_cpu_map__is_empty(event_cpus)) {
             cpu_map = online_cpus;
     } else {
             /* Make sure all specified CPUs are online */
-            for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
+            struct perf_cpu cpu;
 
-                    if (perf_cpu_map__has(event_cpus, cpu) &&
-                        !perf_cpu_map__has(online_cpus, cpu))
+            perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+                    if (!perf_cpu_map__has(online_cpus, cpu))
                             return -EINVAL;
             }
 
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index 51ccbfd3d246..0b52e67edb3b 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
      * In the case of per-cpu mmaps, sample CPU for AUX event;
      * also enable the timestamp tracing for samples correlation.
      */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_sample_bit(arm_spe_evsel, CPU);
             evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
                                        "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
     tracking_evsel->core.attr.sample_period = 1;
 
     /* In per-cpu case, always need the time of mmap events etc */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_sample_bit(tracking_evsel, TIME);
             evsel__set_sample_bit(tracking_evsel, CPU);
 
#24 lore raw

Re: [PATCH v1 07/14] perf arm-spe/cs-etm: Directly iterate CPU maps

Quoted 76 lines · Ian Rogers
Quoted 61 lines · James Clark
Quoted 57 lines · James Clark
Quoted 29 lines · Ian Rogers

Rather than iterate all CPUs and see if they are in CPU maps, directly iterate the CPU map. Similarly make use of the intersect function. Switch perf_cpu_map__has_any_cpu_or_is_empty to more appropriate alternatives.

---
 tools/perf/arch/arm/util/cs-etm.c    | 77 ++++++++++++----------------
 tools/perf/arch/arm64/util/arm-spe.c |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +3 -4
index 77e6663c1703..a68a72f2f668 100644
@@ -197,38 +197,32 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,
 static int cs_etm_validate_config(struct auxtrace_record *itr,
                               struct evsel *evsel)
 {
-    int i, err = -EINVAL;
+    int idx, err = -EINVAL;
     struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus;
     struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+    struct perf_cpu_map *intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+    struct perf_cpu cpu;
 
-    /* Set option of each CPU we have */
-    for (i = 0; i < cpu__max_cpu().cpu; i++) {
-            struct perf_cpu cpu = { .cpu = i, };

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 216

Quoted 10 lines
tools/perf/arch/arm/util/cs-etm.c +0 -10
-
-            /*
-             * In per-cpu case, do the validation for CPUs to work with.
-             * In per-thread case, the CPU map is empty.  Since the traced
-             * program can run on any CPUs in this case, thus don't skip
-             * validation.
-             */
-            if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus) &&
-                !perf_cpu_map__has(event_cpus, cpu))
-                    continue;

This has broken validation for per-thread sessions. perf_cpu_map__intersect() doesn't seem to be able to handle the case where an 'any' map intersected with an online map should return the online map. Or at least it should for this to work, and it seems to make sense for it to work that way.

At least that was my initial impression, but I only debugged it and saw that the loop is now skipped entirely.

Quoted 5 lines
tools/perf/arch/arm/util/cs-etm.c +1 -3
-
-            if (!perf_cpu_map__has(online_cpus, cpu))
-                    continue;
+    perf_cpu_map__put(online_cpus);
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/arch/arm/util/cs-etm.c +4 -1
-            err = cs_etm_validate_context_id(itr, evsel, i);
+    /*
+     * Set option of each CPU we have. In per-cpu case, do the validation
+     * for CPUs to work with.  In per-thread case, the CPU map is empty.
+     * Since the traced program can run on any CPUs in this case, thus don't
Quoted 8 lines

tools/perf/arch/arm/util/cs-etm.c static int cs_etm_validate_timestamp(struct auxtrace_record *itr, · line 218

Quoted 8 lines
tools/perf/arch/arm/util/cs-etm.c +5 -1
+     * skip validation.
+     */
+    perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+            err = cs_etm_validate_context_id(itr, evsel, cpu.cpu);
             if (err)
                     goto out;
-            err = cs_etm_validate_timestamp(itr, evsel, i);
+            err = cs_etm_validate_timestamp(itr, evsel, idx);
Quoted 2 lines

I think this is an error, idx shouldn't be used here, cpu.cpu should.

Yes I think you're right. But when I tested this it was on a machine with all CPUs online, and all traced, so I think idx == cpu.cpu. So although this might need to be fixed it didn't cause the breakage.

Also this line of code was also never hit because the issue was the intersect returning an empty map before here.

Quoted 67 lines
Quoted 60 lines
Quoted 52 lines
Quoted 52 lines
tools/perf/arch/arm/util/cs-etm.c +8 -11
             if (err)
                     goto out;
     }
 
     err = 0;
 out:
-    perf_cpu_map__put(online_cpus);
+    perf_cpu_map__put(intersect_cpus);
     return err;
 }
 
@@ -435,7 +429,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
      * Also the case of per-cpu mmaps, need the contextID in order to be notified
      * when a context switch happened.
      */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
                                        "timestamp", 1);
             evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel,
@@ -461,7 +455,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr,
     evsel->core.attr.sample_period = 1;
 
     /* In per-cpu case, always need the time of mmap events etc */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
             evsel__set_sample_bit(evsel, TIME);
 
     err = cs_etm_validate_config(itr, cs_etm_evsel);
@@ -533,38 +527,32 @@ static size_t
 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
                   struct evlist *evlist __maybe_unused)
 {
-    int i;
+    int idx;
     int etmv3 = 0, etmv4 = 0, ete = 0;
     struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
     struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+    struct perf_cpu cpu;
 
     /* cpu map is not empty, we have specific CPUs to work with */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
-            for (i = 0; i < cpu__max_cpu().cpu; i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
-
-                    if (!perf_cpu_map__has(event_cpus, cpu) ||
-                        !perf_cpu_map__has(online_cpus, cpu))
-                            continue;
+    if (!perf_cpu_map__is_empty(event_cpus)) {
+            struct perf_cpu_map *intersect_cpus =
+                    perf_cpu_map__intersect(event_cpus, online_cpus);
 
Quoted 3 lines

tools/perf/arch/arm/util/cs-etm.c static size_t · line 542

Quoted 3 lines
tools/perf/arch/arm/util/cs-etm.c +2 -1
-                    if (cs_etm_is_ete(itr, i))
+            perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) {
+                    if (cs_etm_is_ete(itr, cpu.cpu))

Similar problem here. For a per-thread session, the CPU map is not empty (it's an 'any' map, presumably length 1), so it comes into this first if, rather than the else below which is for the 'any' scenario.

Quoted 6 lines

Then the intersect with online CPUs results in an empty map, so no CPU metadata is recorded, then the session fails.

If you made the intersect work in the way I mentioned above we could also delete the else below, because that's just another way to convert from 'any' to 'all online'.

Quoted 6 lines

I don't think intersect of "all online" with an "any CPU" should return "all online" as these would be quite different options to perf_event_open. Let's see if the issue above fixes this change otherwise I can revert it to a more mechanical translation of the existing code into the new APIs.

If you're not ok with the intersection behavior that I suggested, then maybe we need a new function like perf_cpu_map__flatten() or perf_cpu_map__expand() which can be used in these two cases which does behave in that way. It doesn't look like it needs any more re-writing, it's just the perf_cpu_map__intersect() needs to behave slightly differently in this case.

It's because ETM cares about validating and saving the configuration per-core, even if you finally open the event with CPU==-1, because later we'll need all that info to decode the trace.

Although I can't see what scenario that you wouldn't want ('any' intersect '1,2') == '1,2'. Is there any code in Perf that's already doing that that would be broken by that change? It might be best to have that behavior earlier rather than later in case something does start depending on it.

As far as I know you can't do both per-thread and per-cpu sessions in Perf, so it doesn't really matter how the intersection behaves in that scenario.

Quoted 70 lines

Thanks,
Ian

Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
tools/perf/arch/arm/util/cs-etm.c +9 -14
                             ete++;
-                    else if (cs_etm_is_etmv4(itr, i))
+                    else if (cs_etm_is_etmv4(itr, cpu.cpu))
                             etmv4++;
                     else
                             etmv3++;
             }
+            perf_cpu_map__put(intersect_cpus);
     } else {
             /* get configuration for all CPUs in the system */
-            for (i = 0; i < cpu__max_cpu().cpu; i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
-
-                    if (!perf_cpu_map__has(online_cpus, cpu))
-                            continue;
-
-                    if (cs_etm_is_ete(itr, i))
+            perf_cpu_map__for_each_cpu(cpu, idx, online_cpus) {
+                    if (cs_etm_is_ete(itr, cpu.cpu))
                             ete++;
-                    else if (cs_etm_is_etmv4(itr, i))
+                    else if (cs_etm_is_etmv4(itr, cpu.cpu))
                             etmv4++;
                     else
                             etmv3++;
@@ -814,15 +802,14 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
             return -EINVAL;
 
     /* If the cpu_map is empty all online CPUs are involved */
-    if (perf_cpu_map__has_any_cpu_or_is_empty(event_cpus)) {
+    if (perf_cpu_map__is_empty(event_cpus)) {
             cpu_map = online_cpus;
     } else {
             /* Make sure all specified CPUs are online */
-            for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
-                    struct perf_cpu cpu = { .cpu = i, };
+            struct perf_cpu cpu;
 
-                    if (perf_cpu_map__has(event_cpus, cpu) &&
-                        !perf_cpu_map__has(online_cpus, cpu))
+            perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+                    if (!perf_cpu_map__has(online_cpus, cpu))
                             return -EINVAL;
             }
 
tools/perf/arch/arm64/util/arm-spe.c +2 -2
index 51ccbfd3d246..0b52e67edb3b 100644
@@ -232,7 +232,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
      * In the case of per-cpu mmaps, sample CPU for AUX event;
      * also enable the timestamp tracing for samples correlation.
      */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_sample_bit(arm_spe_evsel, CPU);
             evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
                                        "ts_enable", 1);
@@ -265,7 +265,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,
     tracking_evsel->core.attr.sample_period = 1;
 
     /* In per-cpu case, always need the time of mmap events etc */
-    if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+    if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
             evsel__set_sample_bit(tracking_evsel, TIME);
             evsel__set_sample_bit(tracking_evsel, CPU);
 
author reviewer
#25 lore raw

PATCH 8/14 perf intel-pt/intel-bts: Switch perf_cpu_map__has_any_cpu_or_is_empty use

In reply to Ian Rogers

Switch perf_cpu_map__has_any_cpu_or_is_empty to perf_cpu_map__is_any_cpu_or_is_empty as a CPU map may contain CPUs as well as the dummy event and perf_cpu_map__is_any_cpu_or_is_empty is a more correct alternative.

---
 tools/perf/arch/x86/util/intel-bts.c |  4 ++--
 tools/perf/arch/x86/util/intel-pt.c  | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)
tools/perf/arch/x86/util/intel-bts.c +2 -2
index af8ae4647585..34696f3d3d5d 100644
@@ -143,7 +143,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	if (!opts->full_auxtrace)
 		return 0;
 
-	if (opts->full_auxtrace && !perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+	if (opts->full_auxtrace && !perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
 		return -EINVAL;
 	}
@@ -224,7 +224,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+		if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_bts_evsel, CPU);
 	}
 
tools/perf/arch/x86/util/intel-pt.c +5 -5
index d199619df3ab..6de7e2d21075 100644
@@ -369,7 +369,7 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 			ui__warning("Intel Processor Trace: TSC not available\n");
 	}
 
-	per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(session->evlist->core.user_requested_cpus);
+	per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty(session->evlist->core.user_requested_cpus);
 
 	auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
 	auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
@@ -774,7 +774,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Per-cpu recording needs sched_switch events to distinguish different
 	 * threads.
 	 */
-	if (have_timing_info && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
+	if (have_timing_info && !perf_cpu_map__is_any_cpu_or_is_empty(cpus) &&
 	    !record_opts__no_switch_events(opts)) {
 		if (perf_can_record_switch_events()) {
 			bool cpu_wide = !target__none(&opts->target) &&
@@ -832,7 +832,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+		if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 			evsel__set_sample_bit(intel_pt_evsel, CPU);
 	}
 
@@ -858,7 +858,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 			tracking_evsel->immediate = true;
 
 		/* In per-cpu case, always need the time of mmap events etc */
-		if (!perf_cpu_map__has_any_cpu_or_is_empty(cpus)) {
+		if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
 			evsel__set_sample_bit(tracking_evsel, TIME);
 			/* And the CPU for switch events */
 			evsel__set_sample_bit(tracking_evsel, CPU);
@@ -870,7 +870,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Warn the user when we do not have enough information to decode i.e.
 	 * per-cpu with no sched_switch (except workload-only).
 	 */
-	if (!ptr->have_sched_switch && !perf_cpu_map__has_any_cpu_or_is_empty(cpus) &&
+	if (!ptr->have_sched_switch && !perf_cpu_map__is_any_cpu_or_is_empty(cpus) &&
 	    !target__none(&opts->target) &&
 	    !intel_pt_evsel->core.attr.exclude_user)
 		ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
author reviewer
#26 lore raw

PATCH 9/14 perf cpumap: Clean up use of perf_cpu_map__has_any_cpu_or_is_empty

In reply to Ian Rogers

Most uses of what was perf_cpu_map__empty but is now perf_cpu_map__has_any_cpu_or_is_empty want to do something with the CPU map if it contains CPUs. Replace uses of perf_cpu_map__has_any_cpu_or_is_empty with other helpers so that CPUs within the map can be handled.

---
 tools/perf/builtin-c2c.c   | 6 +-----
 tools/perf/builtin-stat.c  | 9 ++++-----
 tools/perf/util/auxtrace.c | 4 ++--
 tools/perf/util/record.c   | 2 +-
 tools/perf/util/stat.c     | 2 +-
 5 files changed, 9 insertions(+), 14 deletions(-)
tools/perf/builtin-c2c.c +1 -5
index f78eea9e2153..ef7ed53a4b4e 100644
@@ -2319,11 +2319,7 @@ static int setup_nodes(struct perf_session *session)
 
 		nodes[node] = set;
 
-		/* empty node, skip */
-		if (perf_cpu_map__has_any_cpu_or_is_empty(map))
-			continue;
-
-		perf_cpu_map__for_each_cpu(cpu, idx, map) {
+		perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
 			__set_bit(cpu.cpu, set);
 
 			if (WARN_ONCE(cpu2node[cpu.cpu] != -1, "node/cpu topology bug"))
tools/perf/builtin-stat.c +4 -5
index 3303aa20f326..f583027a0639 100644
@@ -1316,10 +1316,9 @@ static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
 	 * be the first online CPU in the cache domain else use the
 	 * first online CPU of the cache domain as the ID.
 	 */
-	if (perf_cpu_map__has_any_cpu_or_is_empty(cpu_map))
+	id = perf_cpu_map__min(cpu_map).cpu;
+	if (id == -1)
 		id = cpu.cpu;
-	else
-		id = perf_cpu_map__cpu(cpu_map, 0).cpu;
 
 	/* Free the perf_cpu_map used to find the cache ID */
 	perf_cpu_map__put(cpu_map);
@@ -1622,7 +1621,7 @@ static int perf_stat_init_aggr_mode(void)
 	 * taking the highest cpu number to be the size of
 	 * the aggregation translate cpumap.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
 		nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu;
 	else
 		nr = 0;
@@ -2289,7 +2288,7 @@ int process_stat_config_event(struct perf_session *session,
 
 	perf_event__read_stat_config(&stat_config, &event->stat_config);
 
-	if (perf_cpu_map__has_any_cpu_or_is_empty(st->cpus)) {
+	if (perf_cpu_map__is_empty(st->cpus)) {
 		if (st->aggr_mode != AGGR_UNSET)
 			pr_warning("warning: processing task data, aggregation mode not set\n");
 	} else if (st->aggr_mode != AGGR_UNSET) {
tools/perf/util/auxtrace.c +2 -2
index 3684e6009b63..6b1d4bafad59 100644
@@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
 				   struct evlist *evlist,
 				   struct evsel *evsel, int idx)
 {
-	bool per_cpu = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
+	bool per_cpu = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus);
 
 	mp->mmap_needed = evsel->needs_auxtrace_mmap;
 
@@ -648,7 +648,7 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 
 static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
 {
-	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
+	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus);
 
 	if (per_cpu_mmaps) {
 		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
tools/perf/util/record.c +1 -1
index 87e817b3cf7e..e867de8ddaaa 100644
@@ -237,7 +237,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 
 	evsel = evlist__last(temp_evlist);
 
-	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
+	if (!evlist || perf_cpu_map__is_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
 		struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 
 		if (cpus)
tools/perf/util/stat.c +1 -1
index 012c4946b9c4..915808a6211a 100644
@@ -315,7 +315,7 @@ static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
 	if (!counter->per_pkg)
 		return 0;
 
-	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+	if (perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 		return 0;
 
 	if (!mask) {
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 9/14
#27 lore raw

Re: [PATCH v1 09/14] perf cpumap: Clean up use of perf_cpu_map__has_any_cpu_or_is_empty

Quoted 7 lines · Ian Rogers

Most uses of what was perf_cpu_map__empty but is now perf_cpu_map__has_any_cpu_or_is_empty want to do something with the CPU map if it contains CPUs. Replace uses of perf_cpu_map__has_any_cpu_or_is_empty with other helpers so that CPUs within the map can be handled.

Quoted 108 lines
---
 tools/perf/builtin-c2c.c   | 6 +-----
 tools/perf/builtin-stat.c  | 9 ++++-----
 tools/perf/util/auxtrace.c | 4 ++--
 tools/perf/util/record.c   | 2 +-
 tools/perf/util/stat.c     | 2 +-
 5 files changed, 9 insertions(+), 14 deletions(-)
tools/perf/builtin-c2c.c +1 -5
index f78eea9e2153..ef7ed53a4b4e 100644
@@ -2319,11 +2319,7 @@ static int setup_nodes(struct perf_session *session)
 
 		nodes[node] = set;
 
-		/* empty node, skip */
-		if (perf_cpu_map__has_any_cpu_or_is_empty(map))
-			continue;
-
-		perf_cpu_map__for_each_cpu(cpu, idx, map) {
+		perf_cpu_map__for_each_cpu_skip_any(cpu, idx, map) {
 			__set_bit(cpu.cpu, set);
 
 			if (WARN_ONCE(cpu2node[cpu.cpu] != -1, "node/cpu topology bug"))
tools/perf/builtin-stat.c +4 -5
index 3303aa20f326..f583027a0639 100644
@@ -1316,10 +1316,9 @@ static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
 	 * be the first online CPU in the cache domain else use the
 	 * first online CPU of the cache domain as the ID.
 	 */
-	if (perf_cpu_map__has_any_cpu_or_is_empty(cpu_map))
+	id = perf_cpu_map__min(cpu_map).cpu;
+	if (id == -1)
 		id = cpu.cpu;
-	else
-		id = perf_cpu_map__cpu(cpu_map, 0).cpu;
 
 	/* Free the perf_cpu_map used to find the cache ID */
 	perf_cpu_map__put(cpu_map);
@@ -1622,7 +1621,7 @@ static int perf_stat_init_aggr_mode(void)
 	 * taking the highest cpu number to be the size of
 	 * the aggregation translate cpumap.
 	 */
-	if (!perf_cpu_map__has_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
 		nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu;
 	else
 		nr = 0;
@@ -2289,7 +2288,7 @@ int process_stat_config_event(struct perf_session *session,
 
 	perf_event__read_stat_config(&stat_config, &event->stat_config);
 
-	if (perf_cpu_map__has_any_cpu_or_is_empty(st->cpus)) {
+	if (perf_cpu_map__is_empty(st->cpus)) {
 		if (st->aggr_mode != AGGR_UNSET)
 			pr_warning("warning: processing task data, aggregation mode not set\n");
 	} else if (st->aggr_mode != AGGR_UNSET) {
tools/perf/util/auxtrace.c +2 -2
index 3684e6009b63..6b1d4bafad59 100644
@@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
 				   struct evlist *evlist,
 				   struct evsel *evsel, int idx)
 {
-	bool per_cpu = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
+	bool per_cpu = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus);
 
 	mp->mmap_needed = evsel->needs_auxtrace_mmap;
 
@@ -648,7 +648,7 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 
 static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
 {
-	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
+	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus);
 
 	if (per_cpu_mmaps) {
 		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
tools/perf/util/record.c +1 -1
index 87e817b3cf7e..e867de8ddaaa 100644
@@ -237,7 +237,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str)
 
 	evsel = evlist__last(temp_evlist);
 
-	if (!evlist || perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
+	if (!evlist || perf_cpu_map__is_any_cpu_or_is_empty(evlist->core.user_requested_cpus)) {
 		struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
 
 		if (cpus)
tools/perf/util/stat.c +1 -1
index 012c4946b9c4..915808a6211a 100644
@@ -315,7 +315,7 @@ static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
 	if (!counter->per_pkg)
 		return 0;
 
-	if (perf_cpu_map__has_any_cpu_or_is_empty(cpus))
+	if (perf_cpu_map__is_any_cpu_or_is_empty(cpus))
 		return 0;
 
 	if (!mask) {
author reviewer
#28 lore raw

PATCH 10/14 perf top: Avoid repeated function calls

In reply to Ian Rogers

Add a local variable to avoid repeated calls to perf_cpu_map__nr.

---
 tools/perf/util/top.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/perf/util/top.c +4 -5
index be7157de0451..4db3d1bd686c 100644
@@ -28,6 +28,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	struct record_opts *opts = &top->record_opts;
 	struct target *target = &opts->target;
 	size_t ret = 0;
+	int nr_cpus;
 
 	if (top->samples) {
 		samples_per_sec = top->samples / top->delay_secs;
@@ -93,19 +94,17 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	else
 		ret += SNPRINTF(bf + ret, size - ret, " (all");
 
+	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
 	if (target->cpu_list)
 		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
-				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-				? "s" : "",
+				nr_cpus > 1 ? "s" : "",
 				target->cpu_list);
 	else {
 		if (target->tid)
 			ret += SNPRINTF(bf + ret, size - ret, ")");
 		else
 			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-					? "s" : "");
+					nr_cpus, nr_cpus > 1 ? "s" : "");
 	}
 
 	perf_top__reset_sample_counters(top);
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 10/14
#29 lore raw

Re: [PATCH v1 10/14] perf top: Avoid repeated function calls

Quoted 3 lines · Ian Rogers

Add a local variable to avoid repeated calls to perf_cpu_map__nr.

Quoted 39 lines
---
 tools/perf/util/top.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/perf/util/top.c +4 -5
index be7157de0451..4db3d1bd686c 100644
@@ -28,6 +28,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	struct record_opts *opts = &top->record_opts;
 	struct target *target = &opts->target;
 	size_t ret = 0;
+	int nr_cpus;
 
 	if (top->samples) {
 		samples_per_sec = top->samples / top->delay_secs;
@@ -93,19 +94,17 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	else
 		ret += SNPRINTF(bf + ret, size - ret, " (all");
 
+	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
 	if (target->cpu_list)
 		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
-				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-				? "s" : "",
+				nr_cpus > 1 ? "s" : "",
 				target->cpu_list);
 	else {
 		if (target->tid)
 			ret += SNPRINTF(bf + ret, size - ret, ")");
 		else
 			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-					? "s" : "");
+					nr_cpus, nr_cpus > 1 ? "s" : "");
 	}
 
 	perf_top__reset_sample_counters(top);
maintainer Applied
#30 lore raw

Re: [PATCH v1 10/14] perf top: Avoid repeated function calls

Em Tue, Dec 12, 2023 at 03:11:53PM +0000, James Clark escreveu:

Quoted 8 lines · Ian Rogers
Quoted 3 lines

Add a local variable to avoid repeated calls to perf_cpu_map__nr.

Thanks, applied to perf-tools-next.

- Arnaldo

Quoted 40 lines
Quoted 39 lines
---
 tools/perf/util/top.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/perf/util/top.c +4 -5
index be7157de0451..4db3d1bd686c 100644
@@ -28,6 +28,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	struct record_opts *opts = &top->record_opts;
 	struct target *target = &opts->target;
 	size_t ret = 0;
+	int nr_cpus;
 
 	if (top->samples) {
 		samples_per_sec = top->samples / top->delay_secs;
@@ -93,19 +94,17 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
 	else
 		ret += SNPRINTF(bf + ret, size - ret, " (all");
 
+	nr_cpus = perf_cpu_map__nr(top->evlist->core.user_requested_cpus);
 	if (target->cpu_list)
 		ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
-				perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-				? "s" : "",
+				nr_cpus > 1 ? "s" : "",
 				target->cpu_list);
 	else {
 		if (target->tid)
 			ret += SNPRINTF(bf + ret, size - ret, ")");
 		else
 			ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus),
-					perf_cpu_map__nr(top->evlist->core.user_requested_cpus) > 1
-					? "s" : "");
+					nr_cpus, nr_cpus > 1 ? "s" : "");
 	}
 
 	perf_top__reset_sample_counters(top);
Signature · 1 line
- Arnaldo
author reviewer
#31 lore raw

PATCH 11/14 perf arm64 header: Remove unnecessary CPU map get and put

In reply to Ian Rogers

In both cases the CPU map is known owned by either the caller or a PMU.

---
 tools/perf/arch/arm64/util/header.c | 3 ---
 1 file changed, 3 deletions(-)
tools/perf/arch/arm64/util/header.c +0 -3
index 97037499152e..a9de0b5187dd 100644
@@ -25,8 +25,6 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 	if (!sysfs || sz < MIDR_SIZE)
 		return EINVAL;
 
-	cpus = perf_cpu_map__get(cpus);
-
 	for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {
 		char path[PATH_MAX];
 		FILE *file;
@@ -51,7 +49,6 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 		break;
 	}
 
-	perf_cpu_map__put(cpus);
 	return ret;
 }
 
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 11/14
#32 lore raw

Re: [PATCH v1 11/14] perf arm64 header: Remove unnecessary CPU map get and put

Quoted 4 lines · Ian Rogers

In both cases the CPU map is known owned by either the caller or a PMU.

Quoted 25 lines
---
 tools/perf/arch/arm64/util/header.c | 3 ---
 1 file changed, 3 deletions(-)
tools/perf/arch/arm64/util/header.c +0 -3
index 97037499152e..a9de0b5187dd 100644
@@ -25,8 +25,6 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 	if (!sysfs || sz < MIDR_SIZE)
 		return EINVAL;
 
-	cpus = perf_cpu_map__get(cpus);
-
 	for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {
 		char path[PATH_MAX];
 		FILE *file;
@@ -51,7 +49,6 @@ static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 		break;
 	}
 
-	perf_cpu_map__put(cpus);
 	return ret;
 }
 
author reviewer
#33 lore raw

PATCH 12/14 perf stat: Remove duplicate cpus_map_matched function

In reply to Ian Rogers

Use libperf's perf_cpu_map__equal that performs the same function.

---
 tools/perf/builtin-stat.c | 22 +---------------------
 1 file changed, 1 insertion(+), 21 deletions(-)
tools/perf/builtin-stat.c +1 -21
index f583027a0639..8e2f90b5c276 100644
@@ -164,26 +164,6 @@ static struct perf_stat_config stat_config = {
 	.iostat_run		= false,
 };
 
-static bool cpus_map_matched(struct evsel *a, struct evsel *b)
-{
-	if (!a->core.cpus && !b->core.cpus)
-		return true;
-
-	if (!a->core.cpus || !b->core.cpus)
-		return false;
-
-	if (perf_cpu_map__nr(a->core.cpus) != perf_cpu_map__nr(b->core.cpus))
-		return false;
-
-	for (int i = 0; i < perf_cpu_map__nr(a->core.cpus); i++) {
-		if (perf_cpu_map__cpu(a->core.cpus, i).cpu !=
-		    perf_cpu_map__cpu(b->core.cpus, i).cpu)
-			return false;
-	}
-
-	return true;
-}
-
 static void evlist__check_cpu_maps(struct evlist *evlist)
 {
 	struct evsel *evsel, *warned_leader = NULL;
@@ -194,7 +174,7 @@ static void evlist__check_cpu_maps(struct evlist *evlist)
 		/* Check that leader matches cpus with each member. */
 		if (leader == evsel)
 			continue;
-		if (cpus_map_matched(leader, evsel))
+		if (perf_cpu_map__equal(leader->core.cpus, evsel->core.cpus))
 			continue;
 
 		/* If there's mismatch disable the group and warn user. */
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 12/14
#34 lore raw

Re: [PATCH v1 12/14] perf stat: Remove duplicate cpus_map_matched function

Quoted 4 lines · Ian Rogers

Use libperf's perf_cpu_map__equal that performs the same function.

---

Quoted 43 lines
 tools/perf/builtin-stat.c | 22 +---------------------
 1 file changed, 1 insertion(+), 21 deletions(-)
tools/perf/builtin-stat.c +1 -21
index f583027a0639..8e2f90b5c276 100644
@@ -164,26 +164,6 @@ static struct perf_stat_config stat_config = {
 	.iostat_run		= false,
 };
 
-static bool cpus_map_matched(struct evsel *a, struct evsel *b)
-{
-	if (!a->core.cpus && !b->core.cpus)
-		return true;
-
-	if (!a->core.cpus || !b->core.cpus)
-		return false;
-
-	if (perf_cpu_map__nr(a->core.cpus) != perf_cpu_map__nr(b->core.cpus))
-		return false;
-
-	for (int i = 0; i < perf_cpu_map__nr(a->core.cpus); i++) {
-		if (perf_cpu_map__cpu(a->core.cpus, i).cpu !=
-		    perf_cpu_map__cpu(b->core.cpus, i).cpu)
-			return false;
-	}
-
-	return true;
-}
-
 static void evlist__check_cpu_maps(struct evlist *evlist)
 {
 	struct evsel *evsel, *warned_leader = NULL;
@@ -194,7 +174,7 @@ static void evlist__check_cpu_maps(struct evlist *evlist)
 		/* Check that leader matches cpus with each member. */
 		if (leader == evsel)
 			continue;
-		if (cpus_map_matched(leader, evsel))
+		if (perf_cpu_map__equal(leader->core.cpus, evsel->core.cpus))
 			continue;
 
 		/* If there's mismatch disable the group and warn user. */
author reviewer
#35 lore raw

PATCH 13/14 perf cpumap: Use perf_cpu_map__for_each_cpu when possible

In reply to Ian Rogers

Rather than manually iterating the CPU map, use perf_cpu_map__for_each_cpu. When possible tidy local variables.

---
 tools/perf/arch/arm64/util/header.c           | 10 ++--
 tools/perf/tests/bitmap.c                     | 13 +++---
 tools/perf/tests/topology.c                   | 46 +++++++++----------
 tools/perf/util/bpf_kwork.c                   | 16 ++++---
 tools/perf/util/bpf_kwork_top.c               | 12 ++---
 tools/perf/util/cpumap.c                      | 12 ++---
 .../scripting-engines/trace-event-python.c    | 12 +++--
 tools/perf/util/session.c                     |  5 +-
 tools/perf/util/svghelper.c                   | 20 ++++----
 9 files changed, 72 insertions(+), 74 deletions(-)
tools/perf/arch/arm64/util/header.c +4 -6
index a9de0b5187dd..741df3614a09 100644
@@ -4,8 +4,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <perf/cpumap.h>
-#include <util/cpumap.h>
-#include <internal/cpumap.h>
 #include <api/fs/fs.h>
 #include <errno.h>
 #include "debug.h"
@@ -19,18 +17,18 @@
 static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
 {
 	const char *sysfs = sysfs__mountpoint();
-	int cpu;
-	int ret = EINVAL;
+	struct perf_cpu cpu;
+	int idx, ret = EINVAL;
 
 	if (!sysfs || sz < MIDR_SIZE)
 		return EINVAL;
 
-	for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {
+	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
 		char path[PATH_MAX];
 		FILE *file;
 
 		scnprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d" MIDR,
-			  sysfs, RC_CHK_ACCESS(cpus)->map[cpu].cpu);
+			  sysfs, cpu.cpu);
 
 		file = fopen(path, "r");
 		if (!file) {
tools/perf/tests/bitmap.c +7 -6
index 0173f5402a35..98956e0e0765 100644
@@ -11,18 +11,19 @@
 static unsigned long *get_bitmap(const char *str, int nbits)
 {
 	struct perf_cpu_map *map = perf_cpu_map__new(str);
-	unsigned long *bm = NULL;
-	int i;
+	unsigned long *bm;
 
 	bm = bitmap_zalloc(nbits);
 
 	if (map && bm) {
-		for (i = 0; i < perf_cpu_map__nr(map); i++)
-			__set_bit(perf_cpu_map__cpu(map, i).cpu, bm);
+		int i;
+		struct perf_cpu cpu;
+
+		perf_cpu_map__for_each_cpu(cpu, i, map)
+			__set_bit(cpu.cpu, bm);
 	}
 
-	if (map)
-		perf_cpu_map__put(map);
+	perf_cpu_map__put(map);
 	return bm;
 }
 
tools/perf/tests/topology.c +23 -23

105 lines not on this page · raw message

tools/perf/util/bpf_kwork.c +9 -7
index 6eb2c78fd7f4..44f0f708a15d 100644
@@ -147,12 +147,12 @@ static bool valid_kwork_class_type(enum kwork_class_type type)
 
 static int setup_filters(struct perf_kwork *kwork)
 {
-	u8 val = 1;
-	int i, nr_cpus, key, fd;
-	struct perf_cpu_map *map;
-
 	if (kwork->cpu_list != NULL) {
-		fd = bpf_map__fd(skel->maps.perf_kwork_cpu_filter);
+		int idx, nr_cpus;
+		struct perf_cpu_map *map;
+		struct perf_cpu cpu;
+		int fd = bpf_map__fd(skel->maps.perf_kwork_cpu_filter);
+
 		if (fd < 0) {
 			pr_debug("Invalid cpu filter fd\n");
 			return -1;
@@ -165,8 +165,8 @@ static int setup_filters(struct perf_kwork *kwork)
 		}
 
 		nr_cpus = libbpf_num_possible_cpus();
-		for (i = 0; i < perf_cpu_map__nr(map); i++) {
-			struct perf_cpu cpu = perf_cpu_map__cpu(map, i);
+		perf_cpu_map__for_each_cpu(cpu, idx, map) {
+			u8 val = 1;
 
 			if (cpu.cpu >= nr_cpus) {
 				perf_cpu_map__put(map);
@@ -181,6 +181,8 @@ static int setup_filters(struct perf_kwork *kwork)
 	}
 
 	if (kwork->profile_name != NULL) {
+		int key, fd;
+
 		if (strlen(kwork->profile_name) >= MAX_KWORKNAME) {
 			pr_err("Requested name filter %s too large, limit to %d\n",
 			       kwork->profile_name, MAX_KWORKNAME - 1);
tools/perf/util/bpf_kwork_top.c +6 -6
index 035e02272790..22a3b00a1e23 100644
@@ -122,11 +122,11 @@ static bool valid_kwork_class_type(enum kwork_class_type type)
 
 static int setup_filters(struct perf_kwork *kwork)
 {
-	u8 val = 1;
-	int i, nr_cpus, fd;
-	struct perf_cpu_map *map;
-
 	if (kwork->cpu_list) {
+		int idx, nr_cpus, fd;
+		struct perf_cpu_map *map;
+		struct perf_cpu cpu;
+
 		fd = bpf_map__fd(skel->maps.kwork_top_cpu_filter);
 		if (fd < 0) {
 			pr_debug("Invalid cpu filter fd\n");
@@ -140,8 +140,8 @@ static int setup_filters(struct perf_kwork *kwork)
 		}
 
 		nr_cpus = libbpf_num_possible_cpus();
-		for (i = 0; i < perf_cpu_map__nr(map); i++) {
-			struct perf_cpu cpu = perf_cpu_map__cpu(map, i);
+		perf_cpu_map__for_each_cpu(cpu, idx, map) {
+			u8 val = 1;
 
 			if (cpu.cpu >= nr_cpus) {
 				perf_cpu_map__put(map);
tools/perf/util/cpumap.c +5 -7
index 0581ee0fa5f2..e2287187babd 100644
@@ -629,10 +629,10 @@ static char hex_char(unsigned char val)
 
 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
 {
-	int i, cpu;
+	int idx;
 	char *ptr = buf;
 	unsigned char *bitmap;
-	struct perf_cpu last_cpu = perf_cpu_map__cpu(map, perf_cpu_map__nr(map) - 1);
+	struct perf_cpu c, last_cpu = perf_cpu_map__max(map);
 
 	if (buf == NULL)
 		return 0;
@@ -643,12 +643,10 @@ size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
 		return 0;
 	}
 
-	for (i = 0; i < perf_cpu_map__nr(map); i++) {
-		cpu = perf_cpu_map__cpu(map, i).cpu;
-		bitmap[cpu / 8] |= 1 << (cpu % 8);
-	}
+	perf_cpu_map__for_each_cpu(c, idx, map)
+		bitmap[c.cpu / 8] |= 1 << (c.cpu % 8);
 
-	for (cpu = last_cpu.cpu / 4 * 4; cpu >= 0; cpu -= 4) {
+	for (int cpu = last_cpu.cpu / 4 * 4; cpu >= 0; cpu -= 4) {
 		unsigned char bits = bitmap[cpu / 8];
 
 		if (cpu % 8)
tools/perf/util/scripting-engines/trace-event-python.c +7 -5
index 860e1837ba96..8ef0e5ac03c2 100644
@@ -1693,13 +1693,15 @@ static void python_process_stat(struct perf_stat_config *config,
 {
 	struct perf_thread_map *threads = counter->core.threads;
 	struct perf_cpu_map *cpus = counter->core.cpus;
-	int cpu, thread;
 
-	for (thread = 0; thread < perf_thread_map__nr(threads); thread++) {
-		for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {
-			process_stat(counter, perf_cpu_map__cpu(cpus, cpu),
+	for (int thread = 0; thread < perf_thread_map__nr(threads); thread++) {
+		int idx;
+		struct perf_cpu cpu;
+
+		perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
+			process_stat(counter, cpu,
 				     perf_thread_map__pid(threads, thread), tstamp,
-				     perf_counts(counter->counts, cpu, thread));
+				     perf_counts(counter->counts, idx, thread));
 		}
 	}
 }
tools/perf/util/session.c +2 -3
index 199d3e8df315..d52b58344dbc 100644
@@ -2738,6 +2738,7 @@ int perf_session__cpu_bitmap(struct perf_session *session,
 	int i, err = -1;
 	struct perf_cpu_map *map;
 	int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS);
+	struct perf_cpu cpu;
 
 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
 		struct evsel *evsel;
@@ -2759,9 +2760,7 @@ int perf_session__cpu_bitmap(struct perf_session *session,
 		return -1;
 	}
 
-	for (i = 0; i < perf_cpu_map__nr(map); i++) {
-		struct perf_cpu cpu = perf_cpu_map__cpu(map, i);
-
+	perf_cpu_map__for_each_cpu(cpu, i, map) {
 		if (cpu.cpu >= nr_cpus) {
 			pr_err("Requested CPU %d too large. "
 			       "Consider raising MAX_NR_CPUS\n", cpu.cpu);
tools/perf/util/svghelper.c +9 -11
index 1892e9b6aa7f..2b04f47f4db0 100644
@@ -725,26 +725,24 @@ static void scan_core_topology(int *map, struct topology *t, int nr_cpus)
 
 static int str_to_bitmap(char *s, cpumask_t *b, int nr_cpus)
 {
-	int i;
-	int ret = 0;
-	struct perf_cpu_map *m;
-	struct perf_cpu c;
+	int idx, ret = 0;
+	struct perf_cpu_map *map;
+	struct perf_cpu cpu;
 
-	m = perf_cpu_map__new(s);
-	if (!m)
+	map = perf_cpu_map__new(s);
+	if (!map)
 		return -1;
 
-	for (i = 0; i < perf_cpu_map__nr(m); i++) {
-		c = perf_cpu_map__cpu(m, i);
-		if (c.cpu >= nr_cpus) {
+	perf_cpu_map__for_each_cpu(cpu, idx, map) {
+		if (cpu.cpu >= nr_cpus) {
 			ret = -1;
 			break;
 		}
 
-		__set_bit(c.cpu, cpumask_bits(b));
+		__set_bit(cpu.cpu, cpumask_bits(b));
 	}
 
-	perf_cpu_map__put(m);
+	perf_cpu_map__put(map);
 
 	return ret;
 }
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 13/14
#36 lore raw

Re: [PATCH v1 13/14] perf cpumap: Use perf_cpu_map__for_each_cpu when possible

Quoted 16 lines · Ian Rogers

Rather than manually iterating the CPU map, use perf_cpu_map__for_each_cpu. When possible tidy local variables.

---
 tools/perf/arch/arm64/util/header.c           | 10 ++--
 tools/perf/tests/bitmap.c                     | 13 +++---
 tools/perf/tests/topology.c                   | 46 +++++++++----------
 tools/perf/util/bpf_kwork.c                   | 16 ++++---
 tools/perf/util/bpf_kwork_top.c               | 12 ++---
 tools/perf/util/cpumap.c                      | 12 ++---
 .../scripting-engines/trace-event-python.c    | 12 +++--
 tools/perf/util/session.c                     |  5 +-
 tools/perf/util/svghelper.c                   | 20 ++++----
 9 files changed, 72 insertions(+), 74 deletions(-)

[...]

Quoted 12 lines
tools/perf/util/scripting-engines/trace-event-python.c +0 -3
index 860e1837ba96..8ef0e5ac03c2 100644
@@ -1693,13 +1693,15 @@ static void python_process_stat(struct perf_stat_config *config,
 {
 	struct perf_thread_map *threads = counter->core.threads;
 	struct perf_cpu_map *cpus = counter->core.cpus;
-	int cpu, thread;
 
-	for (thread = 0; thread < perf_thread_map__nr(threads); thread++) {
-		for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) {

tools/perf/util/scripting-engines/trace-event-python.c static void python_process_stat(struct perf_stat_config *config, · line 1704

Quoted 10 lines
tools/perf/util/scripting-engines/trace-event-python.c +7 -2
-			process_stat(counter, perf_cpu_map__cpu(cpus, cpu),
+	for (int thread = 0; thread < perf_thread_map__nr(threads); thread++) {
+		int idx;
+		struct perf_cpu cpu;
+
+		perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
+			process_stat(counter, cpu,
 				     perf_thread_map__pid(threads, thread), tstamp,
-				     perf_counts(counter->counts, cpu, thread));
+				     perf_counts(counter->counts, idx, thread));

I thought changing cpu to idx was fixing a bug, but it was actually just hard to read before where cpu was actually idx and not cpu, so this cleanup is pretty good.

author reviewer
#37 lore raw

PATCH 14/14 libperf cpumap: Document perf_cpu_map__nr's behavior

In reply to Ian Rogers

perf_cpu_map__nr's behavior around an empty CPU map is strange as it returns that there is 1 CPU. Changing code that may rely on this behavior is hard, we can at least document the behavior.

---
 tools/lib/perf/include/perf/cpumap.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/lib/perf/include/perf/cpumap.h +11 -0
index 523e4348fc96..90457d17fb2f 100644
@@ -44,7 +44,18 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig,
 							 struct perf_cpu_map *other);
 LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
+/**
+ * perf_cpu_map__cpu - get the CPU value at the given index. Returns -1 if index
+ *                     is invalid.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
+/**
+ * perf_cpu_map__nr - for an empty map returns 1, as perf_cpu_map__cpu returns a
+ *                    cpu of -1 for an invalid index, this makes an empty map
+ *                    look like it contains the "any CPU"/dummy value. Otherwise
+ *                    the result is the number CPUs in the map plus one if the
+ *                    "any CPU"/dummy value is present.
+ */
 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
 /**
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Reviewed-by on v1 14/14
#38 lore raw

Re: [PATCH v1 14/14] libperf cpumap: Document perf_cpu_map__nr's behavior

Quoted 18 lines · Ian Rogers

perf_cpu_map__nr's behavior around an empty CPU map is strange as it returns that there is 1 CPU. Changing code that may rely on this behavior is hard, we can at least document the behavior.

---
 tools/lib/perf/include/perf/cpumap.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/lib/perf/include/perf/cpumap.h +1 -0
index 523e4348fc96..90457d17fb2f 100644
@@ -44,7 +44,18 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig,
 							 struct perf_cpu_map *other);
 LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
+/**

tools/lib/perf/include/perf/cpumap.h LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, · line 57

Quoted 10 lines
tools/lib/perf/include/perf/cpumap.h +9 -0
+ * perf_cpu_map__cpu - get the CPU value at the given index. Returns -1 if index
+ *                     is invalid.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
+/**
+ * perf_cpu_map__nr - for an empty map returns 1, as perf_cpu_map__cpu returns a
+ *                    cpu of -1 for an invalid index, this makes an empty map
+ *                    look like it contains the "any CPU"/dummy value. Otherwise
+ *                    the result is the number CPUs in the map plus one if the
+ *                    "any CPU"/dummy value is present.

Hmmm... I'm not sure whether to laugh or cry at that API.

Quoted 4 lines
tools/lib/perf/include/perf/cpumap.h +1 -0
+ */
 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
 /**
  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
maintainer Applied
#39 lore raw

Re: [PATCH v1 14/14] libperf cpumap: Document perf_cpu_map__nr's behavior

Em Tue, Dec 12, 2023 at 03:20:47PM +0000, James Clark escreveu:

Quoted 16 lines · Ian Rogers

tools/lib/perf/include/perf/cpumap.h LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, · line 57

Quoted 12 lines

LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);

tools/lib/perf/include/perf/cpumap.h +10 -0
+/**
+ * perf_cpu_map__cpu - get the CPU value at the given index. Returns -1 if index
+ *                     is invalid.
+ */
 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
+/**
+ * perf_cpu_map__nr - for an empty map returns 1, as perf_cpu_map__cpu returns a
+ *                    cpu of -1 for an invalid index, this makes an empty map
+ *                    look like it contains the "any CPU"/dummy value. Otherwise
+ *                    the result is the number CPUs in the map plus one if the
+ *                    "any CPU"/dummy value is present.

Hmmm... I'm not sure whether to laugh or cry at that API.

Quoted 1 line

Thanks, applied to perf-tools-next.

- Arnaldo

author reviewer
#40 lore raw
In reply to Ian Rogers
Quoted 9 lines · Ian Rogers

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Quoted 16 lines

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case libperf cpumap: Add any, empty and min helpers
perf arm-spe/cs-etm: Directly iterate CPU maps
perf intel-pt/intel-bts: Switch perf_cpu_map__has_any_cpu_or_is_empty
use
perf cpumap: Clean up use of perf_cpu_map__has_any_cpu_or_is_empty perf top: Avoid repeated function calls
perf arm64 header: Remove unnecessary CPU map get and put perf stat: Remove duplicate cpus_map_matched function
perf cpumap: Use perf_cpu_map__for_each_cpu when possible libperf cpumap: Document perf_cpu_map__nr's behavior

Ping. Thanks,
Ian

Quoted 58 lines
 .../perf/Documentation/examples/sampling.c    |  2 +-
 .../perf/Documentation/libperf-sampling.txt   |  2 +-
 tools/lib/perf/Documentation/libperf.txt      |  4 +-
 tools/lib/perf/cpumap.c                       | 92 +++++++++++++------
 tools/lib/perf/evlist.c                       |  6 +-
 tools/lib/perf/evsel.c                        |  2 +-
 tools/lib/perf/include/perf/cpumap.h          | 56 ++++++++++-
 tools/lib/perf/libperf.map                    | 10 +-
 tools/lib/perf/tests/test-cpumap.c            |  4 +-
 tools/lib/perf/tests/test-evlist.c            |  6 +-
 tools/lib/perf/tests/test-evsel.c             |  2 +-
 tools/perf/arch/arm/util/cs-etm.c             | 83 +++++++----------
 tools/perf/arch/arm64/util/arm-spe.c          |  4 +-
 tools/perf/arch/arm64/util/header.c           | 15 +--
 tools/perf/arch/x86/util/intel-bts.c          |  4 +-
 tools/perf/arch/x86/util/intel-pt.c           | 10 +-
 tools/perf/bench/epoll-ctl.c                  |  2 +-
 tools/perf/bench/epoll-wait.c                 |  2 +-
 tools/perf/bench/futex-hash.c                 |  2 +-
 tools/perf/bench/futex-lock-pi.c              |  2 +-
 tools/perf/bench/futex-requeue.c              |  2 +-
 tools/perf/bench/futex-wake-parallel.c        |  2 +-
 tools/perf/bench/futex-wake.c                 |  2 +-
 tools/perf/builtin-c2c.c                      |  6 +-
 tools/perf/builtin-ftrace.c                   |  2 +-
 tools/perf/builtin-record.c                   |  4 +-
 tools/perf/builtin-stat.c                     | 31 +------
 tools/perf/tests/bitmap.c                     | 13 +--
 tools/perf/tests/code-reading.c               |  2 +-
 tools/perf/tests/cpumap.c                     |  2 +-
 tools/perf/tests/keep-tracking.c              |  2 +-
 tools/perf/tests/mmap-basic.c                 |  2 +-
 tools/perf/tests/openat-syscall-all-cpus.c    |  2 +-
 tools/perf/tests/perf-time-to-tsc.c           |  2 +-
 tools/perf/tests/sw-clock.c                   |  2 +-
 tools/perf/tests/switch-tracking.c            |  2 +-
 tools/perf/tests/task-exit.c                  |  2 +-
 tools/perf/tests/topology.c                   | 48 +++++-----
 tools/perf/util/auxtrace.c                    |  4 +-
 tools/perf/util/bpf_counter.c                 |  2 +-
 tools/perf/util/bpf_kwork.c                   | 16 ++--
 tools/perf/util/bpf_kwork_top.c               | 12 +--
 tools/perf/util/cpumap.c                      | 14 ++-
 tools/perf/util/cputopo.c                     |  2 +-
 tools/perf/util/evlist.c                      |  4 +-
 tools/perf/util/evsel.c                       |  2 +-
 tools/perf/util/perf_api_probe.c              |  4 +-
 tools/perf/util/record.c                      |  4 +-
 .../scripting-engines/trace-event-python.c    | 12 ++-
 tools/perf/util/session.c                     |  5 +-
 tools/perf/util/stat.c                        |  2 +-
 tools/perf/util/svghelper.c                   | 20 ++--
 tools/perf/util/top.c                         |  9 +-
 53 files changed, 296 insertions(+), 254 deletions(-)

--
2.43.0.rc1.413.gea7ed67945-goog

#41 lore raw
In reply to Ian Rogers

Em Tue, Nov 28, 2023 at 10:01:57PM -0800, Ian Rogers escreveu:

Quoted 8 lines

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Quoted 6 lines

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case

Applied 1-6, with James Reviewed-by tags, would be good to have Adrian check the PT and BTS parts, testing the end result if he things its all ok.

- Arnaldo

reviewer Acked-by
#42 lore raw
Quoted 16 lines · Arnaldo Carvalho de Melo

Em Tue, Nov 28, 2023 at 10:01:57PM -0800, Ian Rogers escreveu:

Quoted 8 lines

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Quoted 6 lines

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case

Quoted 4 lines

Applied 1-6, with James Reviewed-by tags, would be good to have Adrian check the PT and BTS parts, testing the end result if he things its all ok.

Changing the same lines of code twice in the same patch set is not really kernel style.

Some of the churn could be reduced by applying and rebasing on the patch below.

Ideally the patches should be reordered so that the lines only change once i.e.

	perf_cpu_map__empty -> <replacement>

instead of

	perf_cpu_map__empty -> <rename> -> <replacement>

If that is too much trouble, please accept my ack instead:

From: Adrian Hunter <[email protected]>

Factor out perf_cpu_map__empty() use to reduce the occurrences and make the code more readable.

---
 tools/perf/arch/x86/util/intel-bts.c | 11 ++++++++---
 tools/perf/arch/x86/util/intel-pt.c  | 21 ++++++++++++---------
 2 files changed, 20 insertions(+), 12 deletions(-)
tools/perf/arch/x86/util/intel-bts.c +8 -3
index d2c8cac11470..cebe994eb9db 100644
@@ -59,6 +59,11 @@ intel_bts_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	return INTEL_BTS_AUXTRACE_PRIV_SIZE;
 }
 
+static bool intel_bts_per_cpu(struct evlist *evlist)
+{
+	return !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+}
+
 static int intel_bts_info_fill(struct auxtrace_record *itr,
 			       struct perf_session *session,
 			       struct perf_record_auxtrace_info *auxtrace_info,
@@ -109,8 +114,8 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	struct intel_bts_recording *btsr =
 			container_of(itr, struct intel_bts_recording, itr);
 	struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
+	bool per_cpu_mmaps = intel_bts_per_cpu(evlist);
 	struct evsel *evsel, *intel_bts_evsel = NULL;
-	const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
 	bool privileged = perf_event_paranoid_check(-1);
 
 	if (opts->auxtrace_sample_mode) {
@@ -143,7 +148,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	if (!opts->full_auxtrace)
 		return 0;
 
-	if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
+	if (opts->full_auxtrace && per_cpu_mmaps) {
 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
 		return -EINVAL;
 	}
@@ -224,7 +229,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (per_cpu_mmaps)
 			evsel__set_sample_bit(intel_bts_evsel, CPU);
 	}
 
tools/perf/arch/x86/util/intel-pt.c +12 -9
index fa0c718b9e72..0ff9147c75da 100644
@@ -312,6 +312,11 @@ static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
 	*d = eax;
 }
 
+static bool intel_pt_per_cpu(struct evlist *evlist)
+{
+	return !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+}
+
 static int intel_pt_info_fill(struct auxtrace_record *itr,
 			      struct perf_session *session,
 			      struct perf_record_auxtrace_info *auxtrace_info,
@@ -322,7 +327,8 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 	struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
 	struct perf_event_mmap_page *pc;
 	struct perf_tsc_conversion tc = { .time_mult = 0, };
-	bool cap_user_time_zero = false, per_cpu_mmaps;
+	bool per_cpu_mmaps = intel_pt_per_cpu(session->evlist);
+	bool cap_user_time_zero = false;
 	u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
 	u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
 	unsigned long max_non_turbo_ratio;
@@ -369,8 +375,6 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 			ui__warning("Intel Processor Trace: TSC not available\n");
 	}
 
-	per_cpu_mmaps = !perf_cpu_map__empty(session->evlist->core.user_requested_cpus);
-
 	auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
 	auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
 	auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
@@ -604,8 +608,8 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
 	bool have_timing_info, need_immediate = false;
 	struct evsel *evsel, *intel_pt_evsel = NULL;
-	const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
 	bool privileged = perf_event_paranoid_check(-1);
+	bool per_cpu_mmaps = intel_pt_per_cpu(evlist);
 	u64 tsc_bit;
 	int err;
 
@@ -774,8 +778,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Per-cpu recording needs sched_switch events to distinguish different
 	 * threads.
 	 */
-	if (have_timing_info && !perf_cpu_map__empty(cpus) &&
-	    !record_opts__no_switch_events(opts)) {
+	if (have_timing_info && per_cpu_mmaps && !record_opts__no_switch_events(opts)) {
 		if (perf_can_record_switch_events()) {
 			bool cpu_wide = !target__none(&opts->target) &&
 					!target__has_task(&opts->target);
@@ -832,7 +835,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (per_cpu_mmaps)
 			evsel__set_sample_bit(intel_pt_evsel, CPU);
 	}
 
@@ -858,7 +861,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 			tracking_evsel->immediate = true;
 
 		/* In per-cpu case, always need the time of mmap events etc */
-		if (!perf_cpu_map__empty(cpus)) {
+		if (per_cpu_mmaps) {
 			evsel__set_sample_bit(tracking_evsel, TIME);
 			/* And the CPU for switch events */
 			evsel__set_sample_bit(tracking_evsel, CPU);
@@ -870,7 +873,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Warn the user when we do not have enough information to decode i.e.
 	 * per-cpu with no sched_switch (except workload-only).
 	 */
-	if (!ptr->have_sched_switch && !perf_cpu_map__empty(cpus) &&
+	if (!ptr->have_sched_switch && per_cpu_mmaps &&
 	    !target__none(&opts->target) &&
 	    !intel_pt_evsel->core.attr.exclude_user)
 		ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
Signature · 1 line
2.34.1
#43 lore raw

Em Wed, Dec 13, 2023 at 02:48:15PM +0200, Adrian Hunter escreveu:

Quoted 17 lines · Arnaldo Carvalho de Melo
Quoted 16 lines

Em Tue, Nov 28, 2023 at 10:01:57PM -0800, Ian Rogers escreveu:

Quoted 14 lines

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case

Quoted 3 lines
Quoted 3 lines

Applied 1-6, with James Reviewed-by tags, would be good to have Adrian check the PT and BTS parts, testing the end result if he things its all ok.

Ian,

1-6 is in perf-tools-next now, can you please consider Adrian's
suggestion to reduce patch size and rebase the remaining patches?

- Arnaldo

Quoted 164 lines

Changing the same lines of code twice in the same patch set is not really kernel style.

Some of the churn could be reduced by applying and rebasing on the patch below.

Ideally the patches should be reordered so that the lines only change once i.e.

	perf_cpu_map__empty -> <replacement>

instead of

	perf_cpu_map__empty -> <rename> -> <replacement>

If that is too much trouble, please accept my ack instead:

From: Adrian Hunter <[email protected]>

Factor out perf_cpu_map__empty() use to reduce the occurrences and make the code more readable.

---
 tools/perf/arch/x86/util/intel-bts.c | 11 ++++++++---
 tools/perf/arch/x86/util/intel-pt.c  | 21 ++++++++++++---------
 2 files changed, 20 insertions(+), 12 deletions(-)
tools/perf/arch/x86/util/intel-bts.c +8 -3
index d2c8cac11470..cebe994eb9db 100644
@@ -59,6 +59,11 @@ intel_bts_info_priv_size(struct auxtrace_record *itr __maybe_unused,
 	return INTEL_BTS_AUXTRACE_PRIV_SIZE;
 }
 
+static bool intel_bts_per_cpu(struct evlist *evlist)
+{
+	return !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+}
+
 static int intel_bts_info_fill(struct auxtrace_record *itr,
 			       struct perf_session *session,
 			       struct perf_record_auxtrace_info *auxtrace_info,
@@ -109,8 +114,8 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	struct intel_bts_recording *btsr =
 			container_of(itr, struct intel_bts_recording, itr);
 	struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
+	bool per_cpu_mmaps = intel_bts_per_cpu(evlist);
 	struct evsel *evsel, *intel_bts_evsel = NULL;
-	const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
 	bool privileged = perf_event_paranoid_check(-1);
 
 	if (opts->auxtrace_sample_mode) {
@@ -143,7 +148,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 	if (!opts->full_auxtrace)
 		return 0;
 
-	if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
+	if (opts->full_auxtrace && per_cpu_mmaps) {
 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
 		return -EINVAL;
 	}
@@ -224,7 +229,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (per_cpu_mmaps)
 			evsel__set_sample_bit(intel_bts_evsel, CPU);
 	}
 
tools/perf/arch/x86/util/intel-pt.c +12 -9
index fa0c718b9e72..0ff9147c75da 100644
@@ -312,6 +312,11 @@ static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
 	*d = eax;
 }
 
+static bool intel_pt_per_cpu(struct evlist *evlist)
+{
+	return !perf_cpu_map__empty(evlist->core.user_requested_cpus);
+}
+
 static int intel_pt_info_fill(struct auxtrace_record *itr,
 			      struct perf_session *session,
 			      struct perf_record_auxtrace_info *auxtrace_info,
@@ -322,7 +327,8 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 	struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
 	struct perf_event_mmap_page *pc;
 	struct perf_tsc_conversion tc = { .time_mult = 0, };
-	bool cap_user_time_zero = false, per_cpu_mmaps;
+	bool per_cpu_mmaps = intel_pt_per_cpu(session->evlist);
+	bool cap_user_time_zero = false;
 	u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
 	u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
 	unsigned long max_non_turbo_ratio;
@@ -369,8 +375,6 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,
 			ui__warning("Intel Processor Trace: TSC not available\n");
 	}
 
-	per_cpu_mmaps = !perf_cpu_map__empty(session->evlist->core.user_requested_cpus);
-
 	auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
 	auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
 	auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
@@ -604,8 +608,8 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
 	bool have_timing_info, need_immediate = false;
 	struct evsel *evsel, *intel_pt_evsel = NULL;
-	const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
 	bool privileged = perf_event_paranoid_check(-1);
+	bool per_cpu_mmaps = intel_pt_per_cpu(evlist);
 	u64 tsc_bit;
 	int err;
 
@@ -774,8 +778,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Per-cpu recording needs sched_switch events to distinguish different
 	 * threads.
 	 */
-	if (have_timing_info && !perf_cpu_map__empty(cpus) &&
-	    !record_opts__no_switch_events(opts)) {
+	if (have_timing_info && per_cpu_mmaps && !record_opts__no_switch_events(opts)) {
 		if (perf_can_record_switch_events()) {
 			bool cpu_wide = !target__none(&opts->target) &&
 					!target__has_task(&opts->target);
@@ -832,7 +835,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		 * In the case of per-cpu mmaps, we need the CPU on the
 		 * AUX event.
 		 */
-		if (!perf_cpu_map__empty(cpus))
+		if (per_cpu_mmaps)
 			evsel__set_sample_bit(intel_pt_evsel, CPU);
 	}
 
@@ -858,7 +861,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 			tracking_evsel->immediate = true;
 
 		/* In per-cpu case, always need the time of mmap events etc */
-		if (!perf_cpu_map__empty(cpus)) {
+		if (per_cpu_mmaps) {
 			evsel__set_sample_bit(tracking_evsel, TIME);
 			/* And the CPU for switch events */
 			evsel__set_sample_bit(tracking_evsel, CPU);
@@ -870,7 +873,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 	 * Warn the user when we do not have enough information to decode i.e.
 	 * per-cpu with no sched_switch (except workload-only).
 	 */
-	if (!ptr->have_sched_switch && !perf_cpu_map__empty(cpus) &&
+	if (!ptr->have_sched_switch && per_cpu_mmaps &&
 	    !target__none(&opts->target) &&
 	    !intel_pt_evsel->core.attr.exclude_user)
 		ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
Signature · 1 line
2.34.1
Signature · 1 line
- Arnaldo
maintainer Acked-by on the v1 series
#44 lore raw
In reply to Ian Rogers
Quoted 9 lines · Ian Rogers

Rename and clean up the use of libperf CPU map functions particularly focussing on perf_cpu_map__empty that may return true for maps containing CPUs but also with an "any CPU"/dummy value.

perf_cpu_map__nr is also troubling in that iterating an empty CPU map will yield the "any CPU"/dummy value. Reduce the appearance of some calls to this by using the perf_cpu_map__for_each_cpu macro.

Quoted 16 lines

Ian Rogers (14):
libperf cpumap: Rename perf_cpu_map__dummy_new
libperf cpumap: Rename and prefer sysfs for perf_cpu_map__default_new libperf cpumap: Rename perf_cpu_map__empty
libperf cpumap: Replace usage of perf_cpu_map__new(NULL) libperf cpumap: Add for_each_cpu that skips the "any CPU" case libperf cpumap: Add any, empty and min helpers
perf arm-spe/cs-etm: Directly iterate CPU maps
perf intel-pt/intel-bts: Switch perf_cpu_map__has_any_cpu_or_is_empty
use
perf cpumap: Clean up use of perf_cpu_map__has_any_cpu_or_is_empty perf top: Avoid repeated function calls
perf arm64 header: Remove unnecessary CPU map get and put perf stat: Remove duplicate cpus_map_matched function
perf cpumap: Use perf_cpu_map__for_each_cpu when possible libperf cpumap: Document perf_cpu_map__nr's behavior

Thanks,
Namhyung