Skip to contentSkip to conversation

Add support for cpu event term

PATCH Superseded Ian Rogers 2 participants 11 messages Feb 10, 2025 – Apr 3, 2025 perf events

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v5 cover Add support for cpu event term

Being able to set the cpu mask per event was discussed in the context of a sysfs event.cpus file is discussed here:
https://lore.kernel.org/lkml/CAP-5=fXXuWchzUK0n5KTH8kamr=DQoEni+bUoo8f-4j8Y+eMBg@mail.gmail.com/
Ultimately Kan preferred to have multiple PMUs with a cpumask each rather than an event.cpus file per event. It is still useful to have the cpu event term and so the sysfs part of the original patch series is dropped.

v5: Purely a rebase.
v4: Add the stat-display output change for zero counters Namhyung
requested as part of the series:
https://lore.kernel.org/lkml/[email protected]/
This skips zero values for CPUs not in the evsel's cpumask rather than the evsel's PMU's cpumask.
v3: Drop sysfs event.cpus file support patch from series. Reference
to using cpu to modify uncore events is dropped from the commit message. Reference counting issues on the cpumap are addressed.
v2: Add support for multiple cpu terms on an event that are
merged. For example, an event of "l1d-misses/cpu=4,cpu=5/" will now be opened on both CPU 4 and 5 rather than just CPU 4.

Ian Rogers (4):
libperf cpumap: Add ability to create CPU from a single CPU number perf stat: Use counter cpumask to skip zero values
perf parse-events: Set is_pmu_core for legacy hardware events perf parse-events: Add "cpu" term to set the CPU an event is recorded
on

 tools/lib/perf/cpumap.c                |  10 +++
 tools/lib/perf/include/perf/cpumap.h   |   2 +
 tools/perf/Documentation/perf-list.txt |   9 ++
 tools/perf/tests/event_update.c        |   1 +
 tools/perf/util/evsel_config.h         |   1 +
 tools/perf/util/parse-events.c         | 113 ++++++++++++++++++-------
 tools/perf/util/parse-events.h         |   3 +-
 tools/perf/util/parse-events.l         |   1 +
 tools/perf/util/pmu.c                  |   3 +-
 tools/perf/util/stat-display.c         |  21 +++--
 10 files changed, 125 insertions(+), 39 deletions(-)
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author reviewer
#1 lore raw

PATCH v5 1/4 libperf cpumap: Add ability to create CPU from a single CPU number

Add perf_cpu_map__new_int so that a CPU map can be created from a single integer.

---
 tools/lib/perf/cpumap.c              | 10 ++++++++++
 tools/lib/perf/include/perf/cpumap.h |  2 ++
 2 files changed, 12 insertions(+)
tools/lib/perf/cpumap.c +10 -0
index fcc47214062a..f3054894ff0c 100644
@@ -242,6 +242,16 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	return cpus;
 }
 
+struct perf_cpu_map *perf_cpu_map__new_int(int cpu)
+{
+	struct perf_cpu_map *cpus = perf_cpu_map__alloc(1);
+
+	if (cpus)
+		RC_CHK_ACCESS(cpus)->map[0].cpu = cpu;
+
+	return cpus;
+}
+
 static int __perf_cpu_map__nr(const struct perf_cpu_map *cpus)
 {
 	return RC_CHK_ACCESS(cpus)->nr;
tools/lib/perf/include/perf/cpumap.h +2 -0
index 188a667babc6..077133935bf4 100644
@@ -36,6 +36,8 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
  *                     perf_cpu_map__new_online_cpus is returned.
  */
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
+/** perf_cpu_map__new_int - create a map with the one given cpu. */
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_int(int cpu);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
 LIBPERF_API int perf_cpu_map__merge(struct perf_cpu_map **orig,
 				    struct perf_cpu_map *other);
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author reviewer
#2 lore raw

PATCH v5 2/4 perf stat: Use counter cpumask to skip zero values

In reply to Ian Rogers

When a counter is 0 it may or may not be skipped. For uncore counters it is common they are only valid on 1 logical CPU and all other CPUs should be skipped. The PMU's cpumask was used for the skip calculation, but that cpumask may not reflect user overrides. Similarly a counter on a core PMU may explicitly not request a CPU be gathered. If the counter on this CPU's value is 0 then the counter should be skipped as it wasn't requested. Switch from using the PMU cpumask to that associated with the evsel to support these cases.

Avoid potential crash with --per-thread mode where config->aggr_get_id is NULL. Add some examples for the tool event 0 counter skipping.

---
 tools/perf/util/stat-display.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
tools/perf/util/stat-display.c +13 -8
index e65c7e9f15d1..126fe6cc8893 100644
@@ -1022,8 +1022,16 @@ static bool should_skip_zero_counter(struct perf_stat_config *config,
 		return true;
 
 	/*
-	 * Many tool events are only gathered on the first index, skip other
-	 * zero values.
+	 * In per-thread mode the aggr_map and aggr_get_id functions may be
+	 * NULL, assume all 0 values should be output in that case.
+	 */
+	if (!config->aggr_map || !config->aggr_get_id)
+		return false;
+
+	/*
+	 * Tool events may be gathered on all logical CPUs, for example
+	 * system_time, but for many the first index is the only one used, for
+	 * example num_cores. Don't skip for the first index.
 	 */
 	if (evsel__is_tool(counter)) {
 		struct aggr_cpu_id own_id =
@@ -1031,15 +1039,12 @@ static bool should_skip_zero_counter(struct perf_stat_config *config,
 
 		return !aggr_cpu_id__equal(id, &own_id);
 	}
-
 	/*
-	 * Skip value 0 when it's an uncore event and the given aggr id
-	 * does not belong to the PMU cpumask.
+	 * Skip value 0 when the counter's cpumask doesn't match the given aggr
+	 * id.
 	 */
-	if (!counter->pmu || !counter->pmu->is_uncore)
-		return false;
 
-	perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
+	perf_cpu_map__for_each_cpu(cpu, idx, counter->core.cpus) {
 		struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
 
 		if (aggr_cpu_id__equal(id, &own_id))
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author reviewer
#3 lore raw

PATCH v5 3/4 perf parse-events: Set is_pmu_core for legacy hardware events

In reply to Ian Rogers

Also set the CPU map to all online CPU maps. This is done so the behavior of legacy hardware and hardware cache events better matches that of sysfs and json events during __perf_evlist__propagate_maps. Fix missing cpumap put in "Synthesize attr update" test.

---
 tools/perf/tests/event_update.c |  1 +
 tools/perf/util/parse-events.c  | 37 ++++++++++++++++++++-------------
 2 files changed, 24 insertions(+), 14 deletions(-)
tools/perf/tests/event_update.c +1 -0
index d6b4ce3ef4ee..9301fde11366 100644
@@ -109,6 +109,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
 	TEST_ASSERT_VAL("failed to synthesize attr update name",
 			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
+	perf_cpu_map__put(evsel->core.own_cpus);
 	evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
 
 	TEST_ASSERT_VAL("failed to synthesize attr update cpus",
tools/perf/util/parse-events.c +23 -14
index 6c36b98875bc..8cccf1e22cdf 100644
@@ -231,21 +231,30 @@ __add_event(struct list_head *list, int *idx,
 	    struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
 {
 	struct evsel *evsel;
-	struct perf_cpu_map *cpus = perf_cpu_map__is_empty(cpu_list) && pmu ? pmu->cpus : cpu_list;
+	bool is_pmu_core;
+	struct perf_cpu_map *cpus;
 
-	cpus = perf_cpu_map__get(cpus);
-	if (pmu)
+	if (pmu) {
+		is_pmu_core = pmu->is_core;
+		cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
 		perf_pmu__warn_invalid_formats(pmu);
-
-	if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
-		perf_pmu__warn_invalid_config(pmu, attr->config, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG, "config");
-		perf_pmu__warn_invalid_config(pmu, attr->config1, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
-		perf_pmu__warn_invalid_config(pmu, attr->config2, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
-		perf_pmu__warn_invalid_config(pmu, attr->config3, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+		if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
+			perf_pmu__warn_invalid_config(pmu, attr->config, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG, "config");
+			perf_pmu__warn_invalid_config(pmu, attr->config1, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
+			perf_pmu__warn_invalid_config(pmu, attr->config2, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
+			perf_pmu__warn_invalid_config(pmu, attr->config3, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+		}
+	} else {
+		is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
+			       attr->type == PERF_TYPE_HW_CACHE);
+		if (perf_cpu_map__is_empty(cpu_list))
+			cpus = is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL;
+		else
+			cpus = perf_cpu_map__get(cpu_list);
 	}
 	if (init_attr)
 		event_attr_init(attr);
@@ -260,7 +269,7 @@ __add_event(struct list_head *list, int *idx,
 	evsel->core.cpus = cpus;
 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
-	evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
+	evsel->core.is_pmu_core = is_pmu_core;
 	evsel->auto_merge_stats = auto_merge_stats;
 	evsel->pmu = pmu;
 	evsel->alternate_hw_config = alternate_hw_config;
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
#4 lore raw

Re: [PATCH v5 3/4] perf parse-events: Set is_pmu_core for legacy hardware events

Quoted 57 lines · Ian Rogers

Also set the CPU map to all online CPU maps. This is done so the behavior of legacy hardware and hardware cache events better matches that of sysfs and json events during __perf_evlist__propagate_maps. Fix missing cpumap put in "Synthesize attr update" test.

---
 tools/perf/tests/event_update.c |  1 +
 tools/perf/util/parse-events.c  | 37 ++++++++++++++++++++-------------
 2 files changed, 24 insertions(+), 14 deletions(-)
tools/perf/tests/event_update.c +1 -0
index d6b4ce3ef4ee..9301fde11366 100644
@@ -109,6 +109,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
 	TEST_ASSERT_VAL("failed to synthesize attr update name",
 			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
+	perf_cpu_map__put(evsel->core.own_cpus);
 	evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
 
 	TEST_ASSERT_VAL("failed to synthesize attr update cpus",
tools/perf/util/parse-events.c +10 -13
index 6c36b98875bc..8cccf1e22cdf 100644
@@ -231,21 +231,30 @@ __add_event(struct list_head *list, int *idx,
 	    struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
 {
 	struct evsel *evsel;
-	struct perf_cpu_map *cpus = perf_cpu_map__is_empty(cpu_list) && pmu ? pmu->cpus : cpu_list;
+	bool is_pmu_core;
+	struct perf_cpu_map *cpus;
 
-	cpus = perf_cpu_map__get(cpus);
-	if (pmu)
+	if (pmu) {
+		is_pmu_core = pmu->is_core;
+		cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
 		perf_pmu__warn_invalid_formats(pmu);
-
-	if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
-		perf_pmu__warn_invalid_config(pmu, attr->config, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG, "config");
-		perf_pmu__warn_invalid_config(pmu, attr->config1, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
-		perf_pmu__warn_invalid_config(pmu, attr->config2, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
-		perf_pmu__warn_invalid_config(pmu, attr->config3, name,
-					      PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+		if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
+			perf_pmu__warn_invalid_config(pmu, attr->config, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG, "config");
+			perf_pmu__warn_invalid_config(pmu, attr->config1, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");

tools/perf/util/parse-events.c __add_event(struct list_head *list, int *idx, · line 255

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+			perf_pmu__warn_invalid_config(pmu, attr->config2, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
+			perf_pmu__warn_invalid_config(pmu, attr->config3, name,
+						PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+		}
+	} else {
+		is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
+			       attr->type == PERF_TYPE_HW_CACHE);
+		if (perf_cpu_map__is_empty(cpu_list))
+			cpus = is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL;

All online CPUs? Is there a problem for hybrid?

Thanks,
Kan> + else

Quoted 13 lines
tools/perf/util/parse-events.c +2 -1
+			cpus = perf_cpu_map__get(cpu_list);
 	}
 	if (init_attr)
 		event_attr_init(attr);
@@ -260,7 +269,7 @@ __add_event(struct list_head *list, int *idx,
 	evsel->core.cpus = cpus;
 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
-	evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
+	evsel->core.is_pmu_core = is_pmu_core;
 	evsel->auto_merge_stats = auto_merge_stats;
 	evsel->pmu = pmu;
 	evsel->alternate_hw_config = alternate_hw_config;
author reviewer
#5 lore raw

Re: [PATCH v5 3/4] perf parse-events: Set is_pmu_core for legacy hardware events

Quoted 65 lines · Kan
Quoted 61 lines · Ian Rogers

Also set the CPU map to all online CPU maps. This is done so the behavior of legacy hardware and hardware cache events better matches that of sysfs and json events during __perf_evlist__propagate_maps. Fix missing cpumap put in "Synthesize attr update" test.

---
 tools/perf/tests/event_update.c |  1 +
 tools/perf/util/parse-events.c  | 37 ++++++++++++++++++++-------------
 2 files changed, 24 insertions(+), 14 deletions(-)
tools/perf/tests/event_update.c +1 -0
index d6b4ce3ef4ee..9301fde11366 100644
@@ -109,6 +109,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
      TEST_ASSERT_VAL("failed to synthesize attr update name",
                      !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
+     perf_cpu_map__put(evsel->core.own_cpus);
      evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
 
      TEST_ASSERT_VAL("failed to synthesize attr update cpus",
tools/perf/util/parse-events.c +14 -13
index 6c36b98875bc..8cccf1e22cdf 100644
@@ -231,21 +231,30 @@ __add_event(struct list_head *list, int *idx,
          struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
 {
      struct evsel *evsel;
-     struct perf_cpu_map *cpus = perf_cpu_map__is_empty(cpu_list) && pmu ? pmu->cpus : cpu_list;
+     bool is_pmu_core;
+     struct perf_cpu_map *cpus;
 
-     cpus = perf_cpu_map__get(cpus);
-     if (pmu)
+     if (pmu) {
+             is_pmu_core = pmu->is_core;
+             cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
              perf_pmu__warn_invalid_formats(pmu);
-
-     if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
-             perf_pmu__warn_invalid_config(pmu, attr->config, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG, "config");
-             perf_pmu__warn_invalid_config(pmu, attr->config1, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
-             perf_pmu__warn_invalid_config(pmu, attr->config2, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
-             perf_pmu__warn_invalid_config(pmu, attr->config3, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+             if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
+                     perf_pmu__warn_invalid_config(pmu, attr->config, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG, "config");
+                     perf_pmu__warn_invalid_config(pmu, attr->config1, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
+                     perf_pmu__warn_invalid_config(pmu, attr->config2, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
+                     perf_pmu__warn_invalid_config(pmu, attr->config3, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
Quoted 8 lines

tools/perf/util/parse-events.c __add_event(struct list_head *list, int *idx, · line 255

Quoted 6 lines
tools/perf/util/parse-events.c +6 -0
+             }
+     } else {
+             is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
+                            attr->type == PERF_TYPE_HW_CACHE);
+             if (perf_cpu_map__is_empty(cpu_list))
+                     cpus = is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL;

All online CPUs? Is there a problem for hybrid?

For hybrid if you do cpu_atom/cycles/ then the PMU is found by perf_pmus__find here:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1651

The wildcard case (e.g. perf stat -e cycles ...) scans all the PMUs passing the respective pmu into __add_event:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1382

For non-hybrid the PMU is given by perf_pmus__find_by_type which only returns a PMU if there is an exact match on the type, ie it won't return perf_pmus__find_core_pmu when asked to lookup the PMU for a legacy type, evsel__find_pmu will do this though.

Anyway, the assumption in this legacy configuration no PMU case is that the event is heading to all CPUs, which is true for software and tracepoint. For hybrid we could get into this kind of problem is sysfs isn't mounted, but I think we're pretty generally broken in that case anyway.

Fwiw, I'm carrying these changes in google's tree and test it on my hybrid desktop:
https://github.com/googleprodkernel/linux-perf

Thanks,
Ian

Quoted 16 lines

Thanks,
Kan> + else

Quoted 13 lines
tools/perf/util/parse-events.c +2 -1
+                     cpus = perf_cpu_map__get(cpu_list);
      }
      if (init_attr)
              event_attr_init(attr);
@@ -260,7 +269,7 @@ __add_event(struct list_head *list, int *idx,
      evsel->core.cpus = cpus;
      evsel->core.own_cpus = perf_cpu_map__get(cpus);
      evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
-     evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
+     evsel->core.is_pmu_core = is_pmu_core;
      evsel->auto_merge_stats = auto_merge_stats;
      evsel->pmu = pmu;
      evsel->alternate_hw_config = alternate_hw_config;
#6 lore raw

Re: [PATCH v5 3/4] perf parse-events: Set is_pmu_core for legacy hardware events

Quoted 25 lines · Ian Rogers
Quoted 24 lines · Kan
Quoted 20 lines · Ian Rogers

Also set the CPU map to all online CPU maps. This is done so the behavior of legacy hardware and hardware cache events better matches that of sysfs and json events during __perf_evlist__propagate_maps. Fix missing cpumap put in "Synthesize attr update" test.

---
 tools/perf/tests/event_update.c |  1 +
 tools/perf/util/parse-events.c  | 37 ++++++++++++++++++++-------------
 2 files changed, 24 insertions(+), 14 deletions(-)
tools/perf/tests/event_update.c +0 -0
index d6b4ce3ef4ee..9301fde11366 100644
@@ -109,6 +109,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
      TEST_ASSERT_VAL("failed to synthesize attr update name",
                      !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
Quoted 4 lines
Quoted 4 lines

tools/perf/tests/event_update.c static int test__event_update(struct test_suite *test __maybe_unused, int subtes · line 115

Quoted 4 lines
tools/perf/tests/event_update.c +1 -0
+     perf_cpu_map__put(evsel->core.own_cpus);
      evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
 
      TEST_ASSERT_VAL("failed to synthesize attr update cpus",

The above change should have been merged.
2c744f38da7a ("perf test: Fix leak in "Synthesize attr update" test").

It makes the patch failed to be applied to perf-tools-next.

Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
tools/perf/util/parse-events.c +14 -13
index 6c36b98875bc..8cccf1e22cdf 100644
@@ -231,21 +231,30 @@ __add_event(struct list_head *list, int *idx,
          struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
 {
      struct evsel *evsel;
-     struct perf_cpu_map *cpus = perf_cpu_map__is_empty(cpu_list) && pmu ? pmu->cpus : cpu_list;
+     bool is_pmu_core;
+     struct perf_cpu_map *cpus;
 
-     cpus = perf_cpu_map__get(cpus);
-     if (pmu)
+     if (pmu) {
+             is_pmu_core = pmu->is_core;
+             cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
              perf_pmu__warn_invalid_formats(pmu);
-
-     if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
-             perf_pmu__warn_invalid_config(pmu, attr->config, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG, "config");
-             perf_pmu__warn_invalid_config(pmu, attr->config1, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
-             perf_pmu__warn_invalid_config(pmu, attr->config2, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
-             perf_pmu__warn_invalid_config(pmu, attr->config3, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+             if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
+                     perf_pmu__warn_invalid_config(pmu, attr->config, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG, "config");
+                     perf_pmu__warn_invalid_config(pmu, attr->config1, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
+                     perf_pmu__warn_invalid_config(pmu, attr->config2, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
+                     perf_pmu__warn_invalid_config(pmu, attr->config3, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
Quoted 8 lines

tools/perf/util/parse-events.c __add_event(struct list_head *list, int *idx, · line 255

Quoted 6 lines
tools/perf/util/parse-events.c +6 -0
+             }
+     } else {
+             is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
+                            attr->type == PERF_TYPE_HW_CACHE);
+             if (perf_cpu_map__is_empty(cpu_list))
+                     cpus = is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL;

All online CPUs? Is there a problem for hybrid?

Quoted 5 lines

For hybrid if you do cpu_atom/cycles/ then the PMU is found by perf_pmus__find here:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1651

The wildcard case (e.g. perf stat -e cycles ...) scans all the PMUs>

passing the respective pmu into __add_event:

Quoted 1 line

https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1382

Yes, the pmu should always be available for hybrid case.

Quoted 12 lines

For non-hybrid the PMU is given by perf_pmus__find_by_type which only returns a PMU if there is an exact match on the type, ie it won't return perf_pmus__find_core_pmu when asked to lookup the PMU for a legacy type, evsel__find_pmu will do this though.

Anyway, the assumption in this legacy configuration no PMU case is that the event is heading to all CPUs, which is true for software and tracepoint. For hybrid we could get into this kind of problem is sysfs isn't mounted, but I think we're pretty generally broken in that case anyway.

Quoted 3 lines

Fwiw, I'm carrying these changes in google's tree and test it on my hybrid desktop:
https://github.com/googleprodkernel/linux-perf

OK. I will do some tests then.

Thanks,
Kan

Quoted 21 lines

Thanks,
Ian

Quoted 16 lines

Thanks,
Kan> + else

Quoted 13 lines
tools/perf/util/parse-events.c +2 -1
+                     cpus = perf_cpu_map__get(cpu_list);
      }
      if (init_attr)
              event_attr_init(attr);
@@ -260,7 +269,7 @@ __add_event(struct list_head *list, int *idx,
      evsel->core.cpus = cpus;
      evsel->core.own_cpus = perf_cpu_map__get(cpus);
      evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
-     evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
+     evsel->core.is_pmu_core = is_pmu_core;
      evsel->auto_merge_stats = auto_merge_stats;
      evsel->pmu = pmu;
      evsel->alternate_hw_config = alternate_hw_config;
author reviewer
#7 lore raw

Re: [PATCH v5 3/4] perf parse-events: Set is_pmu_core for legacy hardware events

Quoted 32 lines · Kan
Quoted 28 lines · Ian Rogers
Quoted 27 lines · Kan
Quoted 23 lines · Ian Rogers

Also set the CPU map to all online CPU maps. This is done so the behavior of legacy hardware and hardware cache events better matches that of sysfs and json events during __perf_evlist__propagate_maps. Fix missing cpumap put in "Synthesize attr update" test.

---
 tools/perf/tests/event_update.c |  1 +
 tools/perf/util/parse-events.c  | 37 ++++++++++++++++++++-------------
 2 files changed, 24 insertions(+), 14 deletions(-)
tools/perf/tests/event_update.c +1 -0
index d6b4ce3ef4ee..9301fde11366 100644
@@ -109,6 +109,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
      TEST_ASSERT_VAL("failed to synthesize attr update name",
                      !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
+     perf_cpu_map__put(evsel->core.own_cpus);
      evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
 
Quoted 7 lines
Quoted 1 line
Quoted 1 line

tools/perf/tests/event_update.c static int test__event_update(struct test_suite *test __maybe_unused, int subtes · line 115

Quoted 1 line
tools/perf/tests/event_update.c +0 -0
      TEST_ASSERT_VAL("failed to synthesize attr update cpus",

The above change should have been merged.
2c744f38da7a ("perf test: Fix leak in "Synthesize attr update" test").

It makes the patch failed to be applied to perf-tools-next.

Thanks Kan, I'll send out a rebase.

Ian

Quoted 99 lines
Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
tools/perf/util/parse-events.c +14 -13
index 6c36b98875bc..8cccf1e22cdf 100644
@@ -231,21 +231,30 @@ __add_event(struct list_head *list, int *idx,
          struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
 {
      struct evsel *evsel;
-     struct perf_cpu_map *cpus = perf_cpu_map__is_empty(cpu_list) && pmu ? pmu->cpus : cpu_list;
+     bool is_pmu_core;
+     struct perf_cpu_map *cpus;
 
-     cpus = perf_cpu_map__get(cpus);
-     if (pmu)
+     if (pmu) {
+             is_pmu_core = pmu->is_core;
+             cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
              perf_pmu__warn_invalid_formats(pmu);
-
-     if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
-             perf_pmu__warn_invalid_config(pmu, attr->config, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG, "config");
-             perf_pmu__warn_invalid_config(pmu, attr->config1, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
-             perf_pmu__warn_invalid_config(pmu, attr->config2, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
-             perf_pmu__warn_invalid_config(pmu, attr->config3, name,
-                                           PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
+             if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
+                     perf_pmu__warn_invalid_config(pmu, attr->config, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG, "config");
+                     perf_pmu__warn_invalid_config(pmu, attr->config1, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
+                     perf_pmu__warn_invalid_config(pmu, attr->config2, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
+                     perf_pmu__warn_invalid_config(pmu, attr->config3, name,
+                                             PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
Quoted 8 lines

tools/perf/util/parse-events.c __add_event(struct list_head *list, int *idx, · line 255

Quoted 6 lines
tools/perf/util/parse-events.c +6 -0
+             }
+     } else {
+             is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
+                            attr->type == PERF_TYPE_HW_CACHE);
+             if (perf_cpu_map__is_empty(cpu_list))
+                     cpus = is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL;

All online CPUs? Is there a problem for hybrid?

Quoted 5 lines

For hybrid if you do cpu_atom/cycles/ then the PMU is found by perf_pmus__find here:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1651

The wildcard case (e.g. perf stat -e cycles ...) scans all the PMUs>

passing the respective pmu into __add_event:

Quoted 1 line

https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n1382

Yes, the pmu should always be available for hybrid case.

Quoted 12 lines

For non-hybrid the PMU is given by perf_pmus__find_by_type which only returns a PMU if there is an exact match on the type, ie it won't return perf_pmus__find_core_pmu when asked to lookup the PMU for a legacy type, evsel__find_pmu will do this though.

Anyway, the assumption in this legacy configuration no PMU case is that the event is heading to all CPUs, which is true for software and tracepoint. For hybrid we could get into this kind of problem is sysfs isn't mounted, but I think we're pretty generally broken in that case anyway.

Quoted 3 lines

Fwiw, I'm carrying these changes in google's tree and test it on my hybrid desktop:
https://github.com/googleprodkernel/linux-perf

OK. I will do some tests then.

Thanks,
Kan

Quoted 21 lines

Thanks,
Ian

Quoted 16 lines

Thanks,
Kan> + else

Quoted 13 lines
tools/perf/util/parse-events.c +2 -1
+                     cpus = perf_cpu_map__get(cpu_list);
      }
      if (init_attr)
              event_attr_init(attr);
@@ -260,7 +269,7 @@ __add_event(struct list_head *list, int *idx,
      evsel->core.cpus = cpus;
      evsel->core.own_cpus = perf_cpu_map__get(cpus);
      evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
-     evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
+     evsel->core.is_pmu_core = is_pmu_core;
      evsel->auto_merge_stats = auto_merge_stats;
      evsel->pmu = pmu;
      evsel->alternate_hw_config = alternate_hw_config;
author reviewer
#8 lore raw

PATCH v5 4/4 perf parse-events: Add "cpu" term to set the CPU an event is recorded on

In reply to Ian Rogers

The -C option allows the CPUs for a list of events to be specified but its not possible to set the CPU for a single event. Add a term to allow this. The term isn't a general CPU list due to ',' already being a special character in event parsing instead multiple cpu= terms may be provided and they will be merged/unioned together.

An example of mixing different types of events counted on different CPUs:

```
$ perf stat -A -C 0,4-5,8 -e "instructions/cpu=0/,l1d-misses/cpu=4,cpu=5/,inst_retired.any/cpu=8/,cycles" -a sleep 0.1

 Performance counter stats for 'system wide':

CPU0            6,979,225      instructions/cpu=0/              #    0.89  insn per cycle
CPU4               75,138      cpu/l1d-misses/
CPU5            1,418,939      cpu/l1d-misses/
CPU8              797,553      cpu/inst_retired.any,cpu=8/
CPU0            7,845,302      cycles
CPU4            6,546,859      cycles
CPU5          185,915,438      cycles
CPU8            2,065,668      cycles

       0.112449242 seconds time elapsed
```
---
 tools/perf/Documentation/perf-list.txt |  9 +++
 tools/perf/util/evsel_config.h         |  1 +
 tools/perf/util/parse-events.c         | 76 +++++++++++++++++++++-----
 tools/perf/util/parse-events.h         |  3 +-
 tools/perf/util/parse-events.l         |  1 +
 tools/perf/util/pmu.c                  |  3 +-
 6 files changed, 76 insertions(+), 17 deletions(-)
tools/perf/Documentation/perf-list.txt +9 -0
index d0c65fad419a..d98de9adc515 100644
@@ -289,6 +289,15 @@ Sums up the event counts for all hardware threads in a core, e.g.:
 
   perf stat -e cpu/event=0,umask=0x3,percore=1/
 
+cpu:
+
+Specifies the CPU to open the event upon. The value may be repeated to
+specify opening the event on multiple CPUs:
+
+
+  perf stat -e instructions/cpu=0,cpu=2/,cycles/cpu=1,cpu=2/ -a sleep 1
+  perf stat -e data_read/cpu=0/,data_write/cpu=1/ -a sleep 1
+
 
 EVENT GROUPS
 ------------
tools/perf/util/evsel_config.h +1 -0
index af52a1516d0b..94a1e9cf73d6 100644
@@ -48,6 +48,7 @@ struct evsel_config_term {
 		u32	      aux_sample_size;
 		u64	      cfg_chg;
 		char	      *str;
+		int	      cpu;
 	} val;
 	bool weak;
 };
tools/perf/util/parse-events.c +61 -15

181 lines not on this page · raw message

tools/perf/util/parse-events.h +2 -1
index e176a34ab088..ab242f671031 100644
@@ -80,7 +80,8 @@ enum parse_events__term_type {
 	PARSE_EVENTS__TERM_TYPE_RAW,
 	PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
 	PARSE_EVENTS__TERM_TYPE_HARDWARE,
-#define	__PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_HARDWARE + 1)
+	PARSE_EVENTS__TERM_TYPE_CPU,
+#define	__PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_CPU + 1)
 };
 
 struct parse_events_term {
tools/perf/util/parse-events.l +1 -0
index bf7f73548605..14bff6f9731c 100644
@@ -324,6 +324,7 @@ aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
 aux-action		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_ACTION); }
 aux-sample-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
 metric-id		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
+cpu			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CPU); }
 cpu-cycles|cycles				{ return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); }
 stalled-cycles-frontend|idle-cycles-frontend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
 stalled-cycles-backend|idle-cycles-backend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
tools/perf/util/pmu.c +2 -1
index c2a15b0259cf..f84a9bf810bb 100644
@@ -1433,7 +1433,7 @@ static int pmu_config_term(const struct perf_pmu *pmu,
 			break;
 		case PARSE_EVENTS__TERM_TYPE_USER: /* Not hardcoded. */
 			return -EINVAL;
-		case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_HARDWARE:
+		case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_CPU:
 			/* Skip non-config terms. */
 			break;
 		default:
@@ -1808,6 +1808,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_call
 		"aux-output",
 		"aux-action=(pause|resume|start-paused)",
 		"aux-sample-size=number",
+		"cpu=number",
 	};
 	struct perf_pmu_format *format;
 	int ret;
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
author reviewer
#9 lore raw
In reply to Ian Rogers
Quoted 22 lines · Ian Rogers

Being able to set the cpu mask per event was discussed in the context of a sysfs event.cpus file is discussed here:
https://lore.kernel.org/lkml/CAP-5=fXXuWchzUK0n5KTH8kamr=DQoEni+bUoo8f-4j8Y+eMBg@mail.gmail.com/
Ultimately Kan preferred to have multiple PMUs with a cpumask each rather than an event.cpus file per event. It is still useful to have the cpu event term and so the sysfs part of the original patch series is dropped.

v5: Purely a rebase.
v4: Add the stat-display output change for zero counters Namhyung
requested as part of the series:
https://lore.kernel.org/lkml/[email protected]/
This skips zero values for CPUs not in the evsel's cpumask rather than the evsel's PMU's cpumask.
v3: Drop sysfs event.cpus file support patch from series. Reference
to using cpu to modify uncore events is dropped from the commit message. Reference counting issues on the cpumap are addressed.
v2: Add support for multiple cpu terms on an event that are
merged. For example, an event of "l1d-misses/cpu=4,cpu=5/" will now be opened on both CPU 4 and 5 rather than just CPU 4.

Quoted 5 lines

Ian Rogers (4):
libperf cpumap: Add ability to create CPU from a single CPU number perf stat: Use counter cpumask to skip zero values
perf parse-events: Set is_pmu_core for legacy hardware events perf parse-events: Add "cpu" term to set the CPU an event is recorded

Ping.

Thanks,
Ian

author reviewer
#10 lore raw
Quoted 24 lines · Ian Rogers
Quoted 22 lines · Ian Rogers

Being able to set the cpu mask per event was discussed in the context of a sysfs event.cpus file is discussed here:
https://lore.kernel.org/lkml/CAP-5=fXXuWchzUK0n5KTH8kamr=DQoEni+bUoo8f-4j8Y+eMBg@mail.gmail.com/
Ultimately Kan preferred to have multiple PMUs with a cpumask each rather than an event.cpus file per event. It is still useful to have the cpu event term and so the sysfs part of the original patch series is dropped.

v5: Purely a rebase.
v4: Add the stat-display output change for zero counters Namhyung
requested as part of the series:
https://lore.kernel.org/lkml/[email protected]/
This skips zero values for CPUs not in the evsel's cpumask rather than the evsel's PMU's cpumask.
v3: Drop sysfs event.cpus file support patch from series. Reference
to using cpu to modify uncore events is dropped from the commit message. Reference counting issues on the cpumap are addressed.
v2: Add support for multiple cpu terms on an event that are
merged. For example, an event of "l1d-misses/cpu=4,cpu=5/" will now be opened on both CPU 4 and 5 rather than just CPU 4.

Quoted 7 lines
Quoted 5 lines

Ian Rogers (4):
libperf cpumap: Add ability to create CPU from a single CPU number perf stat: Use counter cpumask to skip zero values
perf parse-events: Set is_pmu_core for legacy hardware events perf parse-events: Add "cpu" term to set the CPU an event is recorded

Ping.

Ping.

Thanks,
Ian

Quoted 2 lines

Thanks,
Ian