Skip to contentSkip to conversation

perf tools: Remove needless affinity setup/cleanup for pid targets

PATCH In review Arnaldo Carvalho de Melo 2 participants 6 messages Jan 17, 2022 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

cover perf tools: Remove needless affinity setup/cleanup for pid targets

From: Arnaldo Carvalho de Melo <[email protected]>

Hi,

Continuing with the removal of needless affinity setup/cleanup
for pid targets, please ack.

- Arnaldo

Arnaldo Carvalho de Melo (4):
perf affinity: Allow passing a NULL arg to affinity__cleanup() perf stat: No need to setup affinities when starting a workload perf evlist: No need to setup affinities when enabling events for pid
targets
perf evlist: No need to setup affinities when disabling events for pid
targets

 tools/perf/builtin-stat.c  | 17 ++++++++++-------
 tools/perf/util/affinity.c |  8 +++++++-
 tools/perf/util/evlist.c   | 28 ++++++++++++++++++----------
 3 files changed, 35 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/4 perf affinity: Allow passing a NULL arg to affinity__cleanup()

From: Arnaldo Carvalho de Melo <[email protected]>

Just like with free(), NULL is checked to avoid having all callers do it.

Its convenient for when not using affinity setup/cleanup for dummy CPU maps, i.e. CPU maps for pid targets.

Cc Adrian Hunter <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/util/affinity.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/affinity.c +7 -1
index f1e30d566db3c835..4d216c0dc4259b9e 100644
@@ -62,7 +62,7 @@ void affinity__set(struct affinity *a, int cpu)
 	clear_bit(cpu, a->sched_cpus);
 }
 
-void affinity__cleanup(struct affinity *a)
+static void __affinity__cleanup(struct affinity *a)
 {
 	int cpu_set_size = get_cpu_set_size();
 
@@ -71,3 +71,9 @@ void affinity__cleanup(struct affinity *a)
 	zfree(&a->sched_cpus);
 	zfree(&a->orig_cpus);
 }
+
+void affinity__cleanup(struct affinity *a)
+{
+	if (a != NULL)
+		__affinity__cleanup(a);
+}
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH 2/4 perf stat: No need to setup affinities when starting a workload

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

I.e. the simple:

  $ perf stat sleep 1

Uses a dummy CPU map and thus there is no need to setup/cleanup affinities to avoid IPIs, etc.

With this we're down to a sched_getaffinity() call, in the libnuma initialization, that probably can be removed in a followup patch.

Cc Adrian Hunter <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/builtin-stat.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/perf/builtin-stat.c +10 -7
index 973ade18b72a9479..934e992c966fb950 100644
@@ -788,7 +788,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	const bool forks = (argc > 0);
 	bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
 	struct evlist_cpu_iterator evlist_cpu_itr;
-	struct affinity affinity;
+	struct affinity saved_affinity, *affinity = NULL;
 	int err;
 	bool second_pass = false;
 
@@ -803,8 +803,11 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	if (group)
 		evlist__set_leader(evsel_list);
 
-	if (affinity__setup(&affinity) < 0)
-		return -1;
+	if (!cpu_map__is_dummy(evsel_list->core.cpus)) {
+		if (affinity__setup(&saved_affinity) < 0)
+			return -1;
+		affinity = &saved_affinity;
+	}
 
 	evlist__for_each_entry(evsel_list, counter) {
 		if (bpf_counter__load(counter, &target))
@@ -813,7 +816,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 			all_counters_use_bpf = false;
 	}
 
-	evlist__for_each_cpu(evlist_cpu_itr, evsel_list, &affinity) {
+	evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
 		counter = evlist_cpu_itr.evsel;
 
 		/*
@@ -869,7 +872,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		 */
 
 		/* First close errored or weak retry */
-		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, &affinity) {
+		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
 			counter = evlist_cpu_itr.evsel;
 
 			if (!counter->reset_group && !counter->errored)
@@ -878,7 +881,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 			perf_evsel__close_cpu(&counter->core, evlist_cpu_itr.cpu_map_idx);
 		}
 		/* Now reopen weak */
-		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, &affinity) {
+		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
 			counter = evlist_cpu_itr.evsel;
 
 			if (!counter->reset_group && !counter->errored)
@@ -904,7 +907,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 			counter->supported = true;
 		}
 	}
-	affinity__cleanup(&affinity);
+	affinity__cleanup(affinity);
 
 	evlist__for_each_entry(evsel_list, counter) {
 		if (!counter->supported) {
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH 3/4 perf evlist: No need to setup affinities when enabling events for pid targets

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When the target is a pid, not started by 'perf stat' we need to enable the events, and in that case there is no need to setup affinities as we use a dummy CPU map, with just one entry set to -1.

So stop doing it to avoid this needless call to sched_getaffinity():

  # strace -ke sched_getaffinity perf stat -e cycles -p 241957 sleep 1
  <SNIP>
  sched_getaffinity(0, 512, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]) = 8
Quoted 9 lines

/usr/lib64/libc-2.33.so(sched_getaffinity@@GLIBC_2.3.4+0x1a) [0xe6eea] /var/home/acme/bin/perf(affinity__setup+0x6a) [0x5329ca] /var/home/acme/bin/perf(__evlist__enable.constprop.0+0x23) [0x4b9693] /var/home/acme/bin/perf(enable_counters+0x14d) [0x42de5d] /var/home/acme/bin/perf(cmd_stat+0x2358) [0x4310c8] /var/home/acme/bin/perf(run_builtin+0x6a) [0x4a2cfa] /var/home/acme/bin/perf(main+0x612) [0x40f8c2] /usr/lib64/libc-2.33.so(__libc_start_main+0xd4) [0x27b74] /var/home/acme/bin/perf(_start+0x2d) [0x40fadd]

<SNIP>

Cc Adrian Hunter <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/util/evlist.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/evlist.c +9 -5
index 6e88d404b5b3e96f..ae6d4363da76ec56 100644
@@ -487,12 +487,16 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct evlist_cpu_iterator evlist_cpu_itr;
-	struct affinity affinity;
+	struct affinity saved_affinity, *affinity = NULL;
 
-	if (affinity__setup(&affinity) < 0)
-		return;
+	// See explanation in evlist__close()
+	if (!cpu_map__is_dummy(evlist->core.cpus)) {
+		if (affinity__setup(&saved_affinity) < 0)
+			return;
+		affinity = &saved_affinity;
+	}
 
-	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
+	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
 		pos = evlist_cpu_itr.evsel;
 		if (evsel__strcmp(pos, evsel_name))
 			continue;
@@ -500,7 +504,7 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
 			continue;
 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
 	}
-	affinity__cleanup(&affinity);
+	affinity__cleanup(affinity);
 	evlist__for_each_entry(evlist, pos) {
 		if (evsel__strcmp(pos, evsel_name))
 			continue;
Signature · 1 line
2.34.1
author maintainer
#4 lore raw

PATCH 4/4 perf evlist: No need to setup affinities when disabling events for pid targets

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When the target is a pid, not started by 'perf stat' we need to disable the events, and in that case there is no need to setup affinities as we use a dummy CPU map, with just one entry set to -1.

So stop doing it to avoid this needless call to sched_getaffinity():

  # strace -ke sched_getaffinity perf stat -e cycles -p 241957 sleep 1
  <SNIP>
  sched_getaffinity(0, 512, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]) = 8
Quoted 8 lines

/usr/lib64/libc-2.33.so(sched_getaffinity@@GLIBC_2.3.4+0x1a) [0xe6eea] /var/home/acme/bin/perf(affinity__setup+0x6a) [0x532a2a] /var/home/acme/bin/perf(__evlist__disable.constprop.0+0x27) [0x4b9827] /var/home/acme/bin/perf(cmd_stat+0x29b5) [0x431725] /var/home/acme/bin/perf(run_builtin+0x6a) [0x4a2cfa] /var/home/acme/bin/perf(main+0x612) [0x40f8c2] /usr/lib64/libc-2.33.so(__libc_start_main+0xd4) [0x27b74] /var/home/acme/bin/perf(_start+0x2d) [0x40fadd]

<SNIP>

Cc Adrian Hunter <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/util/evlist.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/evlist.c +9 -5
index ae6d4363da76ec56..eaad04e1672a4752 100644
@@ -430,15 +430,19 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct evlist_cpu_iterator evlist_cpu_itr;
-	struct affinity affinity;
+	struct affinity saved_affinity, *affinity = NULL;
 	bool has_imm = false;
 
-	if (affinity__setup(&affinity) < 0)
-		return;
+	// See explanation in evlist__close()
+	if (!cpu_map__is_dummy(evlist->core.cpus)) {
+		if (affinity__setup(&saved_affinity) < 0)
+			return;
+		affinity = &saved_affinity;
+	}
 
 	/* Disable 'immediate' events last */
 	for (int imm = 0; imm <= 1; imm++) {
-		evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
+		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
 			pos = evlist_cpu_itr.evsel;
 			if (evsel__strcmp(pos, evsel_name))
 				continue;
@@ -454,7 +458,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 			break;
 	}
 
-	affinity__cleanup(&affinity);
+	affinity__cleanup(affinity);
 	evlist__for_each_entry(evlist, pos) {
 		if (evsel__strcmp(pos, evsel_name))
 			continue;
Signature · 1 line
2.34.1
reviewer Acked-by on the v1 series
#5 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 7 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

Hi,

Continuing with the removal of needless affinity setup/cleanup
for pid targets, please ack.

The whole set:

Thanks!
Ian

Quoted 18 lines

- Arnaldo

Arnaldo Carvalho de Melo (4):
perf affinity: Allow passing a NULL arg to affinity__cleanup() perf stat: No need to setup affinities when starting a workload perf evlist: No need to setup affinities when enabling events for pid
targets
perf evlist: No need to setup affinities when disabling events for pid
targets

 tools/perf/builtin-stat.c  | 17 ++++++++++-------
 tools/perf/util/affinity.c |  8 +++++++-
 tools/perf/util/evlist.c   | 28 ++++++++++++++++++----------
 3 files changed, 35 insertions(+), 18 deletions(-)

--
2.34.1