Skip to contentSkip to conversation

perf tools: Clean up cpu map handling for a system-wide evsel (v1)

PATCH In review Namhyung Kim 4 participants 22 messages Sep 24, 2022 – Sep 30, 2022 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Clean up cpu map handling for a system-wide evsel (v1)

Hello,

The system-wide evsel has a cpu map for all (online) cpus regardless of user requested cpus. But the cpu map handling code has some special case for it and I think we can cleanup the code by making sure that such a evsel has a proper cpu/thread maps from the beginning. This patches should not cause any change in the behavior.

You can get the code from 'perf/cpumap-update-v1' branch in

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung

Namhyung Kim (5):
libperf: Populate system-wide evsel maps
libperf: Propagate maps only if necessary
perf tools: Get rid of evlist__add_on_all_cpus() perf tools: Add evlist__add_sched_switch()
perf tools: Remove special handling of system-wide evsel

 tools/lib/perf/evlist.c             | 23 ++++++++-------
 tools/lib/perf/evsel.c              |  3 --
 tools/perf/arch/x86/util/intel-pt.c | 15 ++++------
 tools/perf/builtin-script.c         |  3 --
 tools/perf/tests/switch-tracking.c  | 15 ++++------
 tools/perf/util/evlist.c            | 46 ++++++++++++-----------------
 tools/perf/util/evlist.h            |  1 +
 tools/perf/util/evsel.c             | 12 ++------
 tools/perf/util/stat.c              |  3 --
 9 files changed, 44 insertions(+), 77 deletions(-)
Signature · 1 line
2.37.3.998.g577e59143f-goog
author maintainer
#1 lore raw

PATCH 1/5 libperf: Populate system-wide evsel maps

Setting proper cpu and thread maps for system wide evsels regardless of user requested cpu in __perf_evlist__propagate_maps(). Those evsels need to be active on all cpus always. Do it in the libperf so that we can guarantee it has proper maps.

---
 tools/lib/perf/evlist.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/lib/perf/evlist.c +9 -6
index 6b1bafe267a4..187129652ab6 100644
@@ -40,11 +40,11 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 	 * We already have cpus for evsel (via PMU sysfs) so
 	 * keep it, if there's no target cpu list defined.
 	 */
-	if (!evsel->own_cpus ||
-	    (!evsel->system_wide && evlist->has_user_cpus) ||
-	    (!evsel->system_wide &&
-	     !evsel->requires_cpu &&
-	     perf_cpu_map__empty(evlist->user_requested_cpus))) {
+	if (evsel->system_wide) {
+		perf_cpu_map__put(evsel->cpus);
+		evsel->cpus = perf_cpu_map__new(NULL);
+	} else if (!evsel->own_cpus || evlist->has_user_cpus ||
+		   (!evsel->requires_cpu && perf_cpu_map__empty(evlist->user_requested_cpus))) {
 		perf_cpu_map__put(evsel->cpus);
 		evsel->cpus = perf_cpu_map__get(evlist->user_requested_cpus);
 	} else if (evsel->cpus != evsel->own_cpus) {
@@ -52,7 +52,10 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 		evsel->cpus = perf_cpu_map__get(evsel->own_cpus);
 	}
 
-	if (!evsel->system_wide) {
+	if (evsel->system_wide) {
+		perf_thread_map__put(evsel->threads);
+		evsel->threads = perf_thread_map__new_dummy();
+	} else {
 		perf_thread_map__put(evsel->threads);
 		evsel->threads = perf_thread_map__get(evlist->threads);
 	}
Signature · 1 line
2.37.3.998.g577e59143f-goog
reviewer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] libperf: Populate system-wide evsel maps

Quoted 6 lines · Namhyung Kim

Setting proper cpu and thread maps for system wide evsels regardless of user requested cpu in __perf_evlist__propagate_maps(). Those evsels need to be active on all cpus always. Do it in the libperf so that we can guarantee it has proper maps.

Quoted 37 lines
---
 tools/lib/perf/evlist.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/lib/perf/evlist.c +9 -6
index 6b1bafe267a4..187129652ab6 100644
@@ -40,11 +40,11 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 	 * We already have cpus for evsel (via PMU sysfs) so
 	 * keep it, if there's no target cpu list defined.
 	 */
-	if (!evsel->own_cpus ||
-	    (!evsel->system_wide && evlist->has_user_cpus) ||
-	    (!evsel->system_wide &&
-	     !evsel->requires_cpu &&
-	     perf_cpu_map__empty(evlist->user_requested_cpus))) {
+	if (evsel->system_wide) {
+		perf_cpu_map__put(evsel->cpus);
+		evsel->cpus = perf_cpu_map__new(NULL);
+	} else if (!evsel->own_cpus || evlist->has_user_cpus ||
+		   (!evsel->requires_cpu && perf_cpu_map__empty(evlist->user_requested_cpus))) {
 		perf_cpu_map__put(evsel->cpus);
 		evsel->cpus = perf_cpu_map__get(evlist->user_requested_cpus);
 	} else if (evsel->cpus != evsel->own_cpus) {
@@ -52,7 +52,10 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 		evsel->cpus = perf_cpu_map__get(evsel->own_cpus);
 	}
 
-	if (!evsel->system_wide) {
+	if (evsel->system_wide) {
+		perf_thread_map__put(evsel->threads);
+		evsel->threads = perf_thread_map__new_dummy();
+	} else {
 		perf_thread_map__put(evsel->threads);
 		evsel->threads = perf_thread_map__get(evlist->threads);
 	}
author maintainer
#3 lore raw

PATCH 2/5 libperf: Propagate maps only if necessary

In reply to Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

---
 tools/lib/perf/evlist.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
tools/lib/perf/evlist.c +3 -5
index 187129652ab6..cc070c3a134d 100644
@@ -67,10 +67,6 @@ static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
 {
 	struct perf_evsel *evsel;
 
-	/* Recomputing all_cpus, so start with a blank slate. */
-	perf_cpu_map__put(evlist->all_cpus);
-	evlist->all_cpus = NULL;
-
 	perf_evlist__for_each_evsel(evlist, evsel)
 		__perf_evlist__propagate_maps(evlist, evsel);
 }
@@ -81,7 +77,9 @@ void perf_evlist__add(struct perf_evlist *evlist,
 	evsel->idx = evlist->nr_entries;
 	list_add_tail(&evsel->node, &evlist->entries);
 	evlist->nr_entries += 1;
-	__perf_evlist__propagate_maps(evlist, evsel);
+
+	if (evlist->all_cpus)
+		__perf_evlist__propagate_maps(evlist, evsel);
 }
 
 void perf_evlist__remove(struct perf_evlist *evlist,
Signature · 1 line
2.37.3.998.g577e59143f-goog
reviewer
#4 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 8 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

There might be other cases like that, but that was just one that stuck out.

Quoted 32 lines
---
 tools/lib/perf/evlist.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
tools/lib/perf/evlist.c +3 -5
index 187129652ab6..cc070c3a134d 100644
@@ -67,10 +67,6 @@ static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
 {
 	struct perf_evsel *evsel;
 
-	/* Recomputing all_cpus, so start with a blank slate. */
-	perf_cpu_map__put(evlist->all_cpus);
-	evlist->all_cpus = NULL;
-
 	perf_evlist__for_each_evsel(evlist, evsel)
 		__perf_evlist__propagate_maps(evlist, evsel);
 }
@@ -81,7 +77,9 @@ void perf_evlist__add(struct perf_evlist *evlist,
 	evsel->idx = evlist->nr_entries;
 	list_add_tail(&evsel->node, &evlist->entries);
 	evlist->nr_entries += 1;
-	__perf_evlist__propagate_maps(evlist, evsel);
+
+	if (evlist->all_cpus)
+		__perf_evlist__propagate_maps(evlist, evsel);
 }
 
 void perf_evlist__remove(struct perf_evlist *evlist,
author maintainer
#5 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 3 lines

There might be other cases like that, but that was just one that stuck out.

Thanks for sharing your concern. Please let me know if you could come up with another.

Namhyung

reviewer
#6 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 9 lines
Quoted 4 lines

There might be other cases like that, but that was just one that stuck out.

Thanks for sharing your concern. Please let me know if you could come up with another.

Namhyung

author maintainer
#7 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

reviewer
#8 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Thanks,
Ian

author maintainer
#9 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 50 lines · Ian Rogers
Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Thanks,
Namhyung

reviewer
#10 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 60 lines · Namhyung Kim
Quoted 50 lines · Ian Rogers
Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Quoted 6 lines

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Thanks,
Namhyung

Perhaps an alternative is to be explicit about deferring map propagation e.g.

tools/lib/perf/evlist.c +3 -4
index 19eaea99aa4f..5ce19e62397d 100644
@@ -70,6 +70,7 @@ static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
 	/* Recomputing all_cpus, so start with a blank slate. */
 	perf_cpu_map__put(evlist->all_cpus);
 	evlist->all_cpus = NULL;
+	evlist->defer_map_propagation = false;
 
 	perf_evlist__for_each_evsel(evlist, evsel)
 		__perf_evlist__propagate_maps(evlist, evsel);
@@ -81,7 +82,8 @@ void perf_evlist__add(struct perf_evlist *evlist,
 	evsel->idx = evlist->nr_entries;
 	list_add_tail(&evsel->node, &evlist->entries);
 	evlist->nr_entries += 1;
-	__perf_evlist__propagate_maps(evlist, evsel);
+	if (!evlist->defer_map_propagation)
+		__perf_evlist__propagate_maps(evlist, evsel);
 }
 
 void perf_evlist__remove(struct perf_evlist *evlist,
@@ -177,9 +179,6 @@ void perf_evlist__set_maps(struct perf_evlist *evlist,
 		evlist->threads = perf_thread_map__get(threads);
 	}
 
-	if (!evlist->all_cpus && cpus)
-		evlist->all_cpus = perf_cpu_map__get(cpus);
-
 	perf_evlist__propagate_maps(evlist);
 }
 
tools/lib/perf/include/internal/evlist.h +1 -0
index 6f89aec3e608..dbe0b763f597 100644
@@ -19,6 +19,7 @@ struct perf_evlist {
 	int			 nr_entries;
 	int			 nr_groups;
 	bool			 has_user_cpus;
+	bool			 defer_map_propagation;
 	/**
 	 * The cpus passed from the command line or all online CPUs by
 	 * default.
tools/perf/builtin-record.c +1 -0
index 52d254b1530c..1c2523d66a14 100644
@@ -3975,6 +3975,7 @@ int cmd_record(int argc, const char **argv)
 	rec->evlist = evlist__new();
 	if (rec->evlist == NULL)
 		return -ENOMEM;
+	rec->evlist->core.defer_map_propagation = true;
 
 	err = perf_config(perf_record_config, rec);
 	if (err)
author maintainer
#11 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 66 lines · Adrian Hunter
Quoted 64 lines · Namhyung Kim
Quoted 50 lines · Ian Rogers
Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Quoted 5 lines
Quoted 2 lines

Thanks,
Namhyung

Perhaps an alternative is to be explicit about deferring map propagation e.g.

Thanks for your patch. Yeah, we can use this.

But I still think it'd be better doing it unconditionally since any propagation before perf_evlist__set_maps will be discarded anyway. With this change, other than perf record will collect all cpus before _set_maps and then discard it. It seems like a waste, no?

Or else, we can have allow_map_propagation initialized to false and set it to true in perf_evlist__set_maps().

Thanks,
Namhyung

Quoted 58 lines
tools/lib/perf/evlist.c +3 -4
index 19eaea99aa4f..5ce19e62397d 100644
@@ -70,6 +70,7 @@ static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
        /* Recomputing all_cpus, so start with a blank slate. */
        perf_cpu_map__put(evlist->all_cpus);
        evlist->all_cpus = NULL;
+       evlist->defer_map_propagation = false;
 
        perf_evlist__for_each_evsel(evlist, evsel)
                __perf_evlist__propagate_maps(evlist, evsel);
@@ -81,7 +82,8 @@ void perf_evlist__add(struct perf_evlist *evlist,
        evsel->idx = evlist->nr_entries;
        list_add_tail(&evsel->node, &evlist->entries);
        evlist->nr_entries += 1;
-       __perf_evlist__propagate_maps(evlist, evsel);
+       if (!evlist->defer_map_propagation)
+               __perf_evlist__propagate_maps(evlist, evsel);
 }
 
 void perf_evlist__remove(struct perf_evlist *evlist,
@@ -177,9 +179,6 @@ void perf_evlist__set_maps(struct perf_evlist *evlist,
                evlist->threads = perf_thread_map__get(threads);
        }
 
-       if (!evlist->all_cpus && cpus)
-               evlist->all_cpus = perf_cpu_map__get(cpus);
-
        perf_evlist__propagate_maps(evlist);
 }
 
tools/lib/perf/include/internal/evlist.h +1 -0
index 6f89aec3e608..dbe0b763f597 100644
@@ -19,6 +19,7 @@ struct perf_evlist {
        int                      nr_entries;
        int                      nr_groups;
        bool                     has_user_cpus;
+       bool                     defer_map_propagation;
        /**
         * The cpus passed from the command line or all online CPUs by
         * default.
tools/perf/builtin-record.c +1 -0
index 52d254b1530c..1c2523d66a14 100644
@@ -3975,6 +3975,7 @@ int cmd_record(int argc, const char **argv)
        rec->evlist = evlist__new();
        if (rec->evlist == NULL)
                return -ENOMEM;
+       rec->evlist->core.defer_map_propagation = true;
 
        err = perf_config(perf_record_config, rec);
        if (err)
reviewer
#12 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 75 lines · Namhyung Kim
Quoted 66 lines · Adrian Hunter
Quoted 64 lines · Namhyung Kim
Quoted 50 lines · Ian Rogers
Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Quoted 5 lines
Quoted 2 lines

Thanks,
Namhyung

Perhaps an alternative is to be explicit about deferring map propagation e.g.

Thanks for your patch. Yeah, we can use this.

Quoted 9 lines

But I still think it'd be better doing it unconditionally since any propagation before perf_evlist__set_maps will be discarded anyway. With this change, other than perf record will collect all cpus before _set_maps and then discard it. It seems like a waste, no?

Or else, we can have allow_map_propagation initialized to false and set it to true in perf_evlist__set_maps().

That sounds fine.

author maintainer
#13 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Quoted 83 lines · Adrian Hunter
Quoted 81 lines · Namhyung Kim
Quoted 66 lines · Adrian Hunter
Quoted 64 lines · Namhyung Kim
Quoted 50 lines · Ian Rogers
Quoted 40 lines · Namhyung Kim
Quoted 29 lines · Adrian Hunter
Quoted 18 lines · Namhyung Kim

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Quoted 5 lines
Quoted 2 lines

Thanks,
Namhyung

Perhaps an alternative is to be explicit about deferring map propagation e.g.

Thanks for your patch. Yeah, we can use this.

But I still think it'd be better doing it unconditionally since any propagation before perf_evlist__set_maps will be discarded anyway. With this change, other than perf record will collect all cpus before _set_maps and then discard it. It seems like a waste, no?

Quoted 5 lines
Quoted 3 lines

Or else, we can have allow_map_propagation initialized to false and set it to true in perf_evlist__set_maps().

That sounds fine.

Thanks!

Arnaldo, how do you want to handle it? I can send v2 for the whole series, but I think you already applied it. Then do you want me to send this change on top?

Namhyung

#14 lore raw

Re: [PATCH 2/5] libperf: Propagate maps only if necessary

Em Fri, Sep 30, 2022 at 09:44:49AM -0700, Namhyung Kim escreveu:

Quoted 88 lines · Adrian Hunter
Quoted 87 lines · Namhyung Kim
Quoted 84 lines · Adrian Hunter
Quoted 66 lines · Namhyung Kim
Quoted 64 lines · Ian Rogers
Quoted 50 lines · Namhyung Kim
Quoted 40 lines · Adrian Hunter
Quoted 29 lines · Namhyung Kim
Quoted 18 lines

Hi Adrian,

Quoted 7 lines · Adrian Hunter
Quoted 5 lines · Namhyung Kim

The current code propagate evsel's cpu map settings to evlist when it's added to an evlist. But the evlist->all_cpus and each evsel's cpus will be updated in perf_evlist__set_maps() later. No need to do it before evlist's cpus are set actually.

Quoted 7 lines
Quoted 3 lines

Actually we discarded this intermediate all_cpus maps at the beginning of perf_evlist__set_maps(). Let's not do this. It's only needed when an evsel is added after the evlist cpu maps are set.

That might not be true. Consider evlist__fix_hybrid_cpus() which fiddles with evsel->core.cpus and evsel->core.own_cpus after the evsel has been added to the evlist. It can also remove an evsel from the evlist.

Quoted 8 lines

Thanks for your review. I think it's fine to change evsel cpus or to remove an evsel from evlist before calling evlist__create_maps(). The function will take care of setting evlist's all_cpus from the evsels in the evlist. So previous changes in evsel/cpus wouldn't be any special.

After this point, adding a new evsel needs to update evlist all cpus by propagating cpu maps. So I think hybrid cpus should be fine. Did I miss something?

Quoted 8 lines

I wondered how it might play out if evlist__fix_hybrid_cpus() reduced the cpus from the target->cpu_list (using perf record -C) , since after this patch all_cpus always starts with the target->cpu_list instead of an empty list. But then, in the hybrid case, it puts a dummy event that uses the target cpu list anyway, so the result is the same.

I don't know if there are any cases where all_cpus would actually need to exclude some of the cpus from target->cpu_list.

Quoted 7 lines

I'm not aware of other cases to reduce cpu list. I think it'd be fine if it has a cpu in the evlist->all_cpus even if it's not used. The evsel should have a correct list anyway and we mostly use the evsel cpus to do the real work.

Thanks,
Namhyung

Quoted 8 lines

The affinity changes made it so that we use all_cpus probably more often than the evsel CPU maps for real work. The reason being we want to avoid IPIs so we do all the work on 1 CPU and then move to the next CPU in evlist all_cpus. evsel CPU maps are used to make sure the indices are kept accurate - for example, if an uncore event is measured with a CPU event:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.h?h=perf/core#n366
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/util/evlist.c?h=perf/core#n404

Right, I meant it'd check the evsel cpus eventually even if it iterates on the evlist all_cpus. The evlist_cpu_iterator__next() will skip a CPU if it's not in the evsel cpus.

Quoted 5 lines
Quoted 2 lines

Thanks,
Namhyung

Perhaps an alternative is to be explicit about deferring map propagation e.g.

Thanks for your patch. Yeah, we can use this.

But I still think it'd be better doing it unconditionally since any propagation before perf_evlist__set_maps will be discarded anyway. With this change, other than perf record will collect all cpus before _set_maps and then discard it. It seems like a waste, no?

Or else, we can have allow_map_propagation initialized to false and set it to true in perf_evlist__set_maps().

Quoted 7 lines
Quoted 1 line

That sounds fine.

Thanks!

Arnaldo, how do you want to handle it? I can send v2 for the whole series, but I think you already applied it. Then do you want me to send this change on top?

Send v2 for the whole series, I haven't yet published it so I can replace.

- Arnaldo

author maintainer
#15 lore raw

PATCH 3/5 perf tools: Get rid of evlist__add_on_all_cpus()

In reply to Namhyung Kim

The cpu and thread maps are properly handled in libperf now. No need to do it in the perf tools anymore. Let's remove the logic.

---
 tools/perf/util/evlist.c | 29 ++---------------------------
 1 file changed, 2 insertions(+), 27 deletions(-)
tools/perf/util/evlist.c +2 -27
index fcfe5bcc0bcf..dcf57b271ff1 100644
@@ -268,28 +268,6 @@ int evlist__add_dummy(struct evlist *evlist)
 	return 0;
 }
 
-static void evlist__add_on_all_cpus(struct evlist *evlist, struct evsel *evsel)
-{
-	evsel->core.system_wide = true;
-
-	/*
-	 * All CPUs.
-	 *
-	 * Note perf_event_open() does not accept CPUs that are not online, so
-	 * in fact this CPU list will include only all online CPUs.
-	 */
-	perf_cpu_map__put(evsel->core.own_cpus);
-	evsel->core.own_cpus = perf_cpu_map__new(NULL);
-	perf_cpu_map__put(evsel->core.cpus);
-	evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
-
-	/* No threads */
-	perf_thread_map__put(evsel->core.threads);
-	evsel->core.threads = perf_thread_map__new_dummy();
-
-	evlist__add(evlist, evsel);
-}
-
 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 {
 	struct evsel *evsel = evlist__dummy_event(evlist);
@@ -302,14 +280,11 @@ struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 	evsel->core.attr.exclude_hv = 1;
 	evsel->core.attr.freq = 0;
 	evsel->core.attr.sample_period = 1;
+	evsel->core.system_wide = system_wide;
 	evsel->no_aux_samples = true;
 	evsel->name = strdup("dummy:u");
 
-	if (system_wide)
-		evlist__add_on_all_cpus(evlist, evsel);
-	else
-		evlist__add(evlist, evsel);
-
+	evlist__add(evlist, evsel);
 	return evsel;
 }
 
Signature · 1 line
2.37.3.998.g577e59143f-goog
reviewer Reviewed-by on v1 3/5
#16 lore raw

Re: [PATCH 3/5] perf tools: Get rid of evlist__add_on_all_cpus()

Quoted 4 lines · Namhyung Kim

The cpu and thread maps are properly handled in libperf now. No need to do it in the perf tools anymore. Let's remove the logic.

Quoted 54 lines
---
 tools/perf/util/evlist.c | 29 ++---------------------------
 1 file changed, 2 insertions(+), 27 deletions(-)
tools/perf/util/evlist.c +2 -27
index fcfe5bcc0bcf..dcf57b271ff1 100644
@@ -268,28 +268,6 @@ int evlist__add_dummy(struct evlist *evlist)
 	return 0;
 }
 
-static void evlist__add_on_all_cpus(struct evlist *evlist, struct evsel *evsel)
-{
-	evsel->core.system_wide = true;
-
-	/*
-	 * All CPUs.
-	 *
-	 * Note perf_event_open() does not accept CPUs that are not online, so
-	 * in fact this CPU list will include only all online CPUs.
-	 */
-	perf_cpu_map__put(evsel->core.own_cpus);
-	evsel->core.own_cpus = perf_cpu_map__new(NULL);
-	perf_cpu_map__put(evsel->core.cpus);
-	evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
-
-	/* No threads */
-	perf_thread_map__put(evsel->core.threads);
-	evsel->core.threads = perf_thread_map__new_dummy();
-
-	evlist__add(evlist, evsel);
-}
-
 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 {
 	struct evsel *evsel = evlist__dummy_event(evlist);
@@ -302,14 +280,11 @@ struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 	evsel->core.attr.exclude_hv = 1;
 	evsel->core.attr.freq = 0;
 	evsel->core.attr.sample_period = 1;
+	evsel->core.system_wide = system_wide;
 	evsel->no_aux_samples = true;
 	evsel->name = strdup("dummy:u");
 
-	if (system_wide)
-		evlist__add_on_all_cpus(evlist, evsel);
-	else
-		evlist__add(evlist, evsel);
-
+	evlist__add(evlist, evsel);
 	return evsel;
 }
 
author maintainer
#17 lore raw

PATCH 4/5 perf tools: Add evlist__add_sched_switch()

In reply to Namhyung Kim

Add a help to create a system-wide sched_switch event. One merit is that it sets the system-wide bit before adding it to evlist so that the libperf can handle the cpu and thread maps correctly.

---
 tools/perf/arch/x86/util/intel-pt.c | 15 +++++----------
 tools/perf/tests/switch-tracking.c  | 15 +++++----------
 tools/perf/util/evlist.c            | 17 +++++++++++++++++
 tools/perf/util/evlist.h            |  1 +
 4 files changed, 28 insertions(+), 20 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +5 -10
index 13933020a79e..793b35f2221a 100644
@@ -11,6 +11,7 @@
 #include <linux/bitops.h>
 #include <linux/log2.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <cpuid.h>
 
 #include "../../../util/session.h"
@@ -426,20 +427,14 @@ static int intel_pt_track_switches(struct evlist *evlist)
 	if (!evlist__can_select_event(evlist, sched_switch))
 		return -EPERM;
 
-	err = parse_event(evlist, sched_switch);
-	if (err) {
-		pr_debug2("%s: failed to parse %s, error %d\n",
+	evsel = evlist__add_sched_switch(evlist, true);
+	if (IS_ERR(evsel)) {
+		err = PTR_ERR(evsel);
+		pr_debug2("%s: failed to create %s, error = %d\n",
 			  __func__, sched_switch, err);
 		return err;
 	}
 
-	evsel = evlist__last(evlist);
-
-	evsel__set_sample_bit(evsel, CPU);
-	evsel__set_sample_bit(evsel, TIME);
-
-	evsel->core.system_wide = true;
-	evsel->no_aux_samples = true;
 	evsel->immediate = true;
 
 	return 0;
tools/perf/tests/switch-tracking.c +5 -10
index 2d46af9ef935..87f565c7f650 100644
@@ -6,6 +6,7 @@
 #include <time.h>
 #include <stdlib.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <perf/cpumap.h>
 #include <perf/evlist.h>
 #include <perf/mmap.h>
@@ -398,19 +399,13 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub
 		goto out;
 	}
 
-	err = parse_event(evlist, sched_switch);
-	if (err) {
-		pr_debug("Failed to parse event %s\n", sched_switch);
+	switch_evsel = evlist__add_sched_switch(evlist, true);
+	if (IS_ERR(switch_evsel)) {
+		err = PTR_ERR(switch_evsel);
+		pr_debug("Failed to create event %s\n", sched_switch);
 		goto out_err;
 	}
 
-	switch_evsel = evlist__last(evlist);
-
-	evsel__set_sample_bit(switch_evsel, CPU);
-	evsel__set_sample_bit(switch_evsel, TIME);
-
-	switch_evsel->core.system_wide = true;
-	switch_evsel->no_aux_samples = true;
 	switch_evsel->immediate = true;
 
 	/* Test moving an event to the front */
tools/perf/util/evlist.c +17 -0
index dcf57b271ff1..6612b00949e7 100644
@@ -288,6 +288,23 @@ struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 	return evsel;
 }
 
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
+{
+	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
+
+	if (IS_ERR(evsel))
+		return evsel;
+
+	evsel__set_sample_bit(evsel, CPU);
+	evsel__set_sample_bit(evsel, TIME);
+
+	evsel->core.system_wide = system_wide;
+	evsel->no_aux_samples = true;
+
+	evlist__add(evlist, evsel);
+	return evsel;
+};
+
 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
 {
 	struct evsel *evsel, *n;
tools/perf/util/evlist.h +1 -0
index 9d967fe3953a..16734c6756b3 100644
@@ -127,6 +127,7 @@ static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
 {
 	return evlist__add_aux_dummy(evlist, true);
 }
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
 
 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
 			 evsel__sb_cb_t cb, void *data);
Signature · 1 line
2.37.3.998.g577e59143f-goog
reviewer Reviewed-by on v1 4/5
#18 lore raw

Re: [PATCH 4/5] perf tools: Add evlist__add_sched_switch()

Quoted 5 lines · Namhyung Kim

Add a help to create a system-wide sched_switch event. One merit is that it sets the system-wide bit before adding it to evlist so that the libperf can handle the cpu and thread maps correctly.

Quoted 120 lines
---
 tools/perf/arch/x86/util/intel-pt.c | 15 +++++----------
 tools/perf/tests/switch-tracking.c  | 15 +++++----------
 tools/perf/util/evlist.c            | 17 +++++++++++++++++
 tools/perf/util/evlist.h            |  1 +
 4 files changed, 28 insertions(+), 20 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +5 -10
index 13933020a79e..793b35f2221a 100644
@@ -11,6 +11,7 @@
 #include <linux/bitops.h>
 #include <linux/log2.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <cpuid.h>
 
 #include "../../../util/session.h"
@@ -426,20 +427,14 @@ static int intel_pt_track_switches(struct evlist *evlist)
 	if (!evlist__can_select_event(evlist, sched_switch))
 		return -EPERM;
 
-	err = parse_event(evlist, sched_switch);
-	if (err) {
-		pr_debug2("%s: failed to parse %s, error %d\n",
+	evsel = evlist__add_sched_switch(evlist, true);
+	if (IS_ERR(evsel)) {
+		err = PTR_ERR(evsel);
+		pr_debug2("%s: failed to create %s, error = %d\n",
 			  __func__, sched_switch, err);
 		return err;
 	}
 
-	evsel = evlist__last(evlist);
-
-	evsel__set_sample_bit(evsel, CPU);
-	evsel__set_sample_bit(evsel, TIME);
-
-	evsel->core.system_wide = true;
-	evsel->no_aux_samples = true;
 	evsel->immediate = true;
 
 	return 0;
tools/perf/tests/switch-tracking.c +5 -10
index 2d46af9ef935..87f565c7f650 100644
@@ -6,6 +6,7 @@
 #include <time.h>
 #include <stdlib.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <perf/cpumap.h>
 #include <perf/evlist.h>
 #include <perf/mmap.h>
@@ -398,19 +399,13 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub
 		goto out;
 	}
 
-	err = parse_event(evlist, sched_switch);
-	if (err) {
-		pr_debug("Failed to parse event %s\n", sched_switch);
+	switch_evsel = evlist__add_sched_switch(evlist, true);
+	if (IS_ERR(switch_evsel)) {
+		err = PTR_ERR(switch_evsel);
+		pr_debug("Failed to create event %s\n", sched_switch);
 		goto out_err;
 	}
 
-	switch_evsel = evlist__last(evlist);
-
-	evsel__set_sample_bit(switch_evsel, CPU);
-	evsel__set_sample_bit(switch_evsel, TIME);
-
-	switch_evsel->core.system_wide = true;
-	switch_evsel->no_aux_samples = true;
 	switch_evsel->immediate = true;
 
 	/* Test moving an event to the front */
tools/perf/util/evlist.c +17 -0
index dcf57b271ff1..6612b00949e7 100644
@@ -288,6 +288,23 @@ struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
 	return evsel;
 }
 
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
+{
+	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
+
+	if (IS_ERR(evsel))
+		return evsel;
+
+	evsel__set_sample_bit(evsel, CPU);
+	evsel__set_sample_bit(evsel, TIME);
+
+	evsel->core.system_wide = system_wide;
+	evsel->no_aux_samples = true;
+
+	evlist__add(evlist, evsel);
+	return evsel;
+};
+
 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
 {
 	struct evsel *evsel, *n;
tools/perf/util/evlist.h +1 -0
index 9d967fe3953a..16734c6756b3 100644
@@ -127,6 +127,7 @@ static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
 {
 	return evlist__add_aux_dummy(evlist, true);
 }
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
 
 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
 			 evsel__sb_cb_t cb, void *data);
author maintainer
#19 lore raw

PATCH 5/5 perf tools: Remove special handling of system-wide evsel

In reply to Namhyung Kim

For system-wide evsels, the thread map should be dummy - i.e. it has a single entry of -1. But the code guarantees such a thread map, so no need to handle it specially.

No functional change intended.

---
 tools/lib/perf/evsel.c      |  3 ---
 tools/perf/builtin-script.c |  3 ---
 tools/perf/util/evsel.c     | 12 ++----------
 tools/perf/util/stat.c      |  3 ---
 4 files changed, 2 insertions(+), 19 deletions(-)
tools/lib/perf/evsel.c +0 -3
index 8ce5bbd09666..8b51b008a81f 100644
@@ -515,9 +515,6 @@ int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
 	if (ncpus == 0 || nthreads == 0)
 		return 0;
 
-	if (evsel->system_wide)
-		nthreads = 1;
-
 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
 	if (evsel->sample_id == NULL)
 		return -ENOMEM;
tools/perf/builtin-script.c +0 -3
index 886f53cfa257..7fa467ed91dc 100644
@@ -2243,9 +2243,6 @@ static void __process_stat(struct evsel *counter, u64 tstamp)
 	struct perf_cpu cpu;
 	static int header_printed;
 
-	if (counter->core.system_wide)
-		nthreads = 1;
-
 	if (!header_printed) {
 		printf("%3s %8s %15s %15s %15s %15s %s\n",
 		       "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
tools/perf/util/evsel.c +2 -10
index 5776bfa70f11..e319bb17d10d 100644
@@ -1813,7 +1813,7 @@ static struct perf_thread_map *empty_thread_map;
 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads)
 {
-	int nthreads;
+	int nthreads = perf_thread_map__nr(threads);
 
 	if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
 	    (perf_missing_features.aux_output     && evsel->core.attr.aux_output))
@@ -1839,11 +1839,6 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		threads = empty_thread_map;
 	}
 
-	if (evsel->core.system_wide)
-		nthreads = 1;
-	else
-		nthreads = threads->nr;
-
 	if (evsel->core.fd == NULL &&
 	    perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0)
 		return -ENOMEM;
@@ -2061,10 +2056,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	if (threads == NULL)
 		threads = empty_thread_map;
 
-	if (evsel->core.system_wide)
-		nthreads = 1;
-	else
-		nthreads = threads->nr;
+	nthreads = perf_thread_map__nr(threads);
 
 	if (evsel->cgrp)
 		pid = evsel->cgrp->fd;
tools/perf/util/stat.c +0 -3
index ce5e9e372fc4..cef943377ad7 100644
@@ -420,9 +420,6 @@ static int process_counter_maps(struct perf_stat_config *config,
 	int ncpus = evsel__nr_cpus(counter);
 	int idx, thread;
 
-	if (counter->core.system_wide)
-		nthreads = 1;
-
 	for (thread = 0; thread < nthreads; thread++) {
 		for (idx = 0; idx < ncpus; idx++) {
 			if (process_counter_values(config, counter, idx, thread,
Signature · 1 line
2.37.3.998.g577e59143f-goog
reviewer Reviewed-by on v1 5/5
#20 lore raw

Re: [PATCH 5/5] perf tools: Remove special handling of system-wide evsel

Quoted 7 lines · Namhyung Kim

For system-wide evsels, the thread map should be dummy - i.e. it has a single entry of -1. But the code guarantees such a thread map, so no need to handle it specially.

No functional change intended.

Quoted 86 lines
---
 tools/lib/perf/evsel.c      |  3 ---
 tools/perf/builtin-script.c |  3 ---
 tools/perf/util/evsel.c     | 12 ++----------
 tools/perf/util/stat.c      |  3 ---
 4 files changed, 2 insertions(+), 19 deletions(-)
tools/lib/perf/evsel.c +0 -3
index 8ce5bbd09666..8b51b008a81f 100644
@@ -515,9 +515,6 @@ int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
 	if (ncpus == 0 || nthreads == 0)
 		return 0;
 
-	if (evsel->system_wide)
-		nthreads = 1;
-
 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
 	if (evsel->sample_id == NULL)
 		return -ENOMEM;
tools/perf/builtin-script.c +0 -3
index 886f53cfa257..7fa467ed91dc 100644
@@ -2243,9 +2243,6 @@ static void __process_stat(struct evsel *counter, u64 tstamp)
 	struct perf_cpu cpu;
 	static int header_printed;
 
-	if (counter->core.system_wide)
-		nthreads = 1;
-
 	if (!header_printed) {
 		printf("%3s %8s %15s %15s %15s %15s %s\n",
 		       "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
tools/perf/util/evsel.c +2 -10
index 5776bfa70f11..e319bb17d10d 100644
@@ -1813,7 +1813,7 @@ static struct perf_thread_map *empty_thread_map;
 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads)
 {
-	int nthreads;
+	int nthreads = perf_thread_map__nr(threads);
 
 	if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
 	    (perf_missing_features.aux_output     && evsel->core.attr.aux_output))
@@ -1839,11 +1839,6 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		threads = empty_thread_map;
 	}
 
-	if (evsel->core.system_wide)
-		nthreads = 1;
-	else
-		nthreads = threads->nr;
-
 	if (evsel->core.fd == NULL &&
 	    perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0)
 		return -ENOMEM;
@@ -2061,10 +2056,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	if (threads == NULL)
 		threads = empty_thread_map;
 
-	if (evsel->core.system_wide)
-		nthreads = 1;
-	else
-		nthreads = threads->nr;
+	nthreads = perf_thread_map__nr(threads);
 
 	if (evsel->cgrp)
 		pid = evsel->cgrp->fd;
tools/perf/util/stat.c +0 -3
index ce5e9e372fc4..cef943377ad7 100644
@@ -420,9 +420,6 @@ static int process_counter_maps(struct perf_stat_config *config,
 	int ncpus = evsel__nr_cpus(counter);
 	int idx, thread;
 
-	if (counter->core.system_wide)
-		nthreads = 1;
-
 	for (thread = 0; thread < nthreads; thread++) {
 		for (idx = 0; idx < ncpus; idx++) {
 			if (process_counter_values(config, counter, idx, thread,
#21 lore raw
In reply to Namhyung Kim

Em Sat, Sep 24, 2022 at 09:57:32AM -0700, Namhyung Kim escreveu:

Quoted 8 lines

Hello,

The system-wide evsel has a cpu map for all (online) cpus regardless of user requested cpus. But the cpu map handling code has some special case for it and I think we can cleanup the code by making sure that such a evsel has a proper cpu/thread maps from the beginning. This patches should not cause any change in the behavior.

Quoted 6 lines

You can get the code from 'perf/cpumap-update-v1' branch in

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung

Looks sane, applied locally for testing.

- Arnaldo

Quoted 21 lines

Namhyung Kim (5):
libperf: Populate system-wide evsel maps
libperf: Propagate maps only if necessary
perf tools: Get rid of evlist__add_on_all_cpus() perf tools: Add evlist__add_sched_switch()
perf tools: Remove special handling of system-wide evsel

 tools/lib/perf/evlist.c             | 23 ++++++++-------
 tools/lib/perf/evsel.c              |  3 --
 tools/perf/arch/x86/util/intel-pt.c | 15 ++++------
 tools/perf/builtin-script.c         |  3 --
 tools/perf/tests/switch-tracking.c  | 15 ++++------
 tools/perf/util/evlist.c            | 46 ++++++++++++-----------------
 tools/perf/util/evlist.h            |  1 +
 tools/perf/util/evsel.c             | 12 ++------
 tools/perf/util/stat.c              |  3 --
 9 files changed, 44 insertions(+), 77 deletions(-)
Signature · 1 line
2.37.3.998.g577e59143f-goog
Signature · 1 line
- Arnaldo