Skip to content

Commit 267ed5d

Browse files
Andi Kleenacmel
authored andcommitted
perf affinity: Add infrastructure to save/restore affinity
The kernel perf subsystem has to IPI to the target CPU for many operations. On systems with many CPUs and when managing many events the overhead can be dominated by lots of IPIs. An alternative is to set up CPU affinity in the perf tool, then set up all the events for that CPU, and then move on to the next CPU. Add some affinity management infrastructure to enable such a model. Used in followon patches. Committer notes: Use zfree() in some places, add missing stdbool.h header, some minor coding style changes. Signed-off-by: Andi Kleen <ak@linux.intel.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: http://lore.kernel.org/lkml/20191121001522.180827-3-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent d966458 commit 267ed5d

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎tools/perf/util/Build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ perf-y += sort.o
7777
perf-y += hist.o
7878
perf-y += util.o
7979
perf-y += cpumap.o
80+
perf-y += affinity.o
8081
perf-y += cputopo.o
8182
perf-y += cgroup.o
8283
perf-y += target.o

‎tools/perf/util/affinity.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Manage affinity to optimize IPIs inside the kernel perf API. */
3+
#define _GNU_SOURCE 1
4+
#include <sched.h>
5+
#include <stdlib.h>
6+
#include <linux/bitmap.h>
7+
#include <linux/zalloc.h>
8+
#include "perf.h"
9+
#include "cpumap.h"
10+
#include "affinity.h"
11+
12+
static int get_cpu_set_size(void)
13+
{
14+
int sz = cpu__max_cpu() + 8 - 1;
15+
/*
16+
* sched_getaffinity doesn't like masks smaller than the kernel.
17+
* Hopefully that's big enough.
18+
*/
19+
if (sz < 4096)
20+
sz = 4096;
21+
return sz / 8;
22+
}
23+
24+
int affinity__setup(struct affinity *a)
25+
{
26+
int cpu_set_size = get_cpu_set_size();
27+
28+
a->orig_cpus = bitmap_alloc(cpu_set_size * 8);
29+
if (!a->orig_cpus)
30+
return -1;
31+
sched_getaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus);
32+
a->sched_cpus = bitmap_alloc(cpu_set_size * 8);
33+
if (!a->sched_cpus) {
34+
zfree(&a->orig_cpus);
35+
return -1;
36+
}
37+
bitmap_zero((unsigned long *)a->sched_cpus, cpu_set_size);
38+
a->changed = false;
39+
return 0;
40+
}
41+
42+
/*
43+
* perf_event_open does an IPI internally to the target CPU.
44+
* It is more efficient to change perf's affinity to the target
45+
* CPU and then set up all events on that CPU, so we amortize
46+
* CPU communication.
47+
*/
48+
void affinity__set(struct affinity *a, int cpu)
49+
{
50+
int cpu_set_size = get_cpu_set_size();
51+
52+
if (cpu == -1)
53+
return;
54+
a->changed = true;
55+
set_bit(cpu, a->sched_cpus);
56+
/*
57+
* We ignore errors because affinity is just an optimization.
58+
* This could happen for example with isolated CPUs or cpusets.
59+
* In this case the IPIs inside the kernel's perf API still work.
60+
*/
61+
sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->sched_cpus);
62+
clear_bit(cpu, a->sched_cpus);
63+
}
64+
65+
void affinity__cleanup(struct affinity *a)
66+
{
67+
int cpu_set_size = get_cpu_set_size();
68+
69+
if (a->changed)
70+
sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus);
71+
zfree(&a->sched_cpus);
72+
zfree(&a->orig_cpus);
73+
}

‎tools/perf/util/affinity.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#ifndef PERF_AFFINITY_H
3+
#define PERF_AFFINITY_H 1
4+
5+
#include <stdbool.h>
6+
7+
struct affinity {
8+
unsigned long *orig_cpus;
9+
unsigned long *sched_cpus;
10+
bool changed;
11+
};
12+
13+
void affinity__cleanup(struct affinity *a);
14+
void affinity__set(struct affinity *a, int cpu);
15+
int affinity__setup(struct affinity *a);
16+
17+
#endif // PERF_AFFINITY_H

‎tools/perf/util/python-ext-sources‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ util/trace-event.c
3333
util/string.c
3434
util/symbol_fprintf.c
3535
util/units.c
36+
util/affinity.c

0 commit comments

Comments
 (0)