perf pmu: Unbreak perf record for arm/arm64 with events with explicit PMU
Subsystems (2)
Mark Rutland
reviewerPATCH 1/1 perf pmu: Unbreak perf record for arm/arm64 with events with explicit PMU
Commit 66ec11919a0f96e936bb731fdbc2851316077d26 upstream.
Currently, perf record is broken on arm/arm64 systems when the PMU is specified explicitly as part of the event, e.g.
$ ./perf record -e armv8_cortex_a53/cpu_cycles/u true
In such cases, perf record fails to open events unless perf_event_paranoid is set to -1, even if the PMU in question supports mode exclusion. Further, even when perf_event_paranoid is toggled, no samples are recorded.
This is an unintended side effect of commit:
e3ba76deef23064f ("perf tools: Force uncore events to system wide monitoring)
... which assumes that if a PMU has an associated cpu_map, it is an uncore PMU, and forces events for such PMUs to be system-wide.
This is not true for arm/arm64 systems, which can have heterogeneous CPUs. To account for this, multiple CPU PMUs are exposed, each with a "cpus" field under sysfs, which the perf tool parses into a cpu_map. ARM PMUs do not have a "cpumask" file, and only have a "cpus" file. For the gory details as to why, see commit:
7e3fcffe95544010 ("perf pmu: Support alternative sysfs cpumask")
Given all of this, we can instead identify uncore PMUs by explicitly checking for a "cpumask" file, and restore arm/arm64 PMU support back to a working state. This patch does so, adding a new perf_pmu::is_uncore field, and splitting the existing cpumask parsing so that it can be reused.
Tested-by Will Deacon <[email protected]>
--- tools/perf/util/parse-events.c | 9 ++++--- tools/perf/util/pmu.c | 56 +++++++++++++++++++++++++++++++----------- tools/perf/util/pmu.h | 1 + 3 files changed, 47 insertions(+), 19 deletions(-)
tools/perf/util/parse-events.c +5 -4
@@ -309,10 +309,11 @@ static char *get_config_name(struct list_head *head_terms)static struct perf_evsel *__add_event (struct list_head *list,int *idx,struct perf_event_attr *attr, -char *name,struct cpu_map *cpus, +char *name,struct perf_pmu *pmu,struct list_head *config_terms) {struct perf_evsel *evsel; +struct cpu_map *cpus = pmu ? pmu->cpus :NULL ;event_attr_init (attr);
@@ -323,7 +324,7 @@ __add_event(struct list_head *list, int *idx, (*idx)++; evsel->cpus =cpu_map__get (cpus); evsel->own_cpus =cpu_map__get (cpus); - evsel->system_wide = !!cpus; + evsel->system_wide = pmu ? pmu->is_uncore : false;if (name) evsel->name =strdup (name);
@@ -1232,7 +1233,7 @@ int parse_events_add_pmu(struct parse_events_evlist *data,if (!head_config) { attr.type = pmu->type; - evsel =__add_event (list, &data->idx, &attr,NULL , pmu->cpus,NULL ); + evsel =__add_event (list, &data->idx, &attr,NULL , pmu,NULL );return evsel ?0 : -ENOMEM ; }
@@ -1253,7 +1254,7 @@ int parse_events_add_pmu(struct parse_events_evlist *data,return -EINVAL ; evsel =__add_event (list, &data->idx, &attr, -get_config_name (head_config), pmu->cpus, +get_config_name (head_config), pmu, &config_terms);if (evsel) { evsel->unit = info.unit;
tools/perf/util/pmu.c +41 -15
84 lines not on this page · raw message
tools/perf/util/pmu.h +1 -0
@@ -21,6 +21,7 @@ struct perf_pmu {char *name;__u32 type;bool selectable; +bool is_uncore;struct perf_event_attr *default_config;struct cpu_map *cpus;struct list_head format;/* HEAD struct perf_pmu_format -> list */
Signature · 1 line
2.11.0
Mark Rutland
reviewerSorry all, I messed up, and this was generated from my v4.12 backports branch.
There's a small difference in the context, so I will send the correct patch shortly.
Sorry for the noise.
Thanks,
Mark.
Quoted 201 lines · Mark Rutland
Commit
66ec11919a0f96e936bb731fdbc2851316077d26upstream.Currently, perf record is broken on arm/arm64 systems when the PMU is specified explicitly as part of the event, e.g.
$ ./perf record -e armv8_cortex_a53/cpu_cycles/u trueIn such cases, perf record fails to open events unless
perf_event_paranoidis set to -1, even if the PMU in question supports mode exclusion. Further, even whenperf_event_paranoidis toggled, no samples are recorded.This is an unintended side effect of commit:
e3ba76deef23064f("perf tools: Force uncore events to system wide monitoring)... which assumes that if a PMU has an associated
cpu_map, it is an uncore PMU, and forces events for such PMUs to be system-wide.This is not true for arm/arm64 systems, which can have heterogeneous CPUs. To account for this, multiple CPU PMUs are exposed, each with a "cpus" field under sysfs, which the perf tool parses into a
cpu_map. ARM PMUs do not have a "cpumask" file, and only have a "cpus" file. For the gory details as to why, see commit:
7e3fcffe95544010("perf pmu: Support alternative sysfs cpumask")Given all of this, we can instead identify uncore PMUs by explicitly checking for a "cpumask" file, and restore arm/arm64 PMU support back to a working state. This patch does so, adding a new
perf_pmu::is_uncorefield, and splitting the existing cpumask parsing so that it can be reused.Signed-off-by Mark Rutland <[email protected]>Tested-by Will Deacon <[email protected]>
Acked-by Jiri Olsa <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc David Ahern <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc 4.12+ <[email protected]>Fixes e3ba76deef23064f ("perf tools: Force uncore events to system wide monitoring)Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/parse-events.c | 9 ++++--- tools/perf/util/pmu.c | 56 +++++++++++++++++++++++++++++++----------- tools/perf/util/pmu.h | 1 + 3 files changed, 47 insertions(+), 19 deletions(-)tools/perf/util/parse-events.c +5
-4@@ -309,10 +309,11 @@ static char *get_config_name(struct list_head *head_terms)static struct perf_evsel *__add_event (struct list_head *list,int *idx,struct perf_event_attr *attr, -char *name,struct cpu_map *cpus, +char *name,struct perf_pmu *pmu,struct list_head *config_terms) {struct perf_evsel *evsel; +struct cpu_map *cpus = pmu ? pmu->cpus :NULL ;event_attr_init (attr);@@ -323,7 +324,7 @@ __add_event(struct list_head *list, int *idx, (*idx)++; evsel->cpus =cpu_map__get (cpus); evsel->own_cpus =cpu_map__get (cpus); - evsel->system_wide = !!cpus; + evsel->system_wide = pmu ? pmu->is_uncore : false;if (name) evsel->name =strdup (name);@@ -1232,7 +1233,7 @@ int parse_events_add_pmu(struct parse_events_evlist *data,if (!head_config) { attr.type = pmu->type; - evsel =__add_event (list, &data->idx, &attr,NULL , pmu->cpus,NULL ); + evsel =__add_event (list, &data->idx, &attr,NULL , pmu,NULL );return evsel ?0 : -ENOMEM ; }@@ -1253,7 +1254,7 @@ int parse_events_add_pmu(struct parse_events_evlist *data,return -EINVAL ; evsel =__add_event (list, &data->idx, &attr, -get_config_name (head_config), pmu->cpus, +get_config_name (head_config), pmu, &config_terms);if (evsel) { evsel->unit = info.unit;tools/perf/util/pmu.c +41
-15@@ -470,17 +470,36 @@ static void pmu_read_sysfs(void)closedir (dir); } +static struct cpu_map *__pmu_cpumask (const char *path) +{ +FILE *file; +struct cpu_map *cpus; + + file =fopen (path,"r" ); +if (!file) +return NULL ; + + cpus =cpu_map__read (file); +fclose (file); +return cpus; +} + +/* +* Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) +* may have a "cpus" file. +*/ +#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" +#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus" +static struct cpu_map *pmu_cpumask (const char *name) { -struct stat st;char path[PATH_MAX ]; -FILE *file;struct cpu_map *cpus;const char *sysfs =sysfs__mountpoint ();const char *templates[] = { -"%s/bus/event_source/devices/%s/cpumask" , -"%s/bus/event_source/devices/%s/cpus" , -NULL +CPUS_TEMPLATE_UNCORE , +CPUS_TEMPLATE_CPU , +NULL };const char **template;@@ -489,20 +508,25 @@ static struct cpu_map *pmu_cpumask(const char *name)for (template = templates; *template; template++) {snprintf (path,PATH_MAX , *template, sysfs, name); -if (stat (path, &st) ==0 ) -break ; + cpus =__pmu_cpumask (path); +if (cpus) +return cpus; } -if (!*template) -return NULL ; +return NULL ; +} - file =fopen (path,"r" ); -if (!file) -return NULL ; +static bool pmu_is_uncore (const char *name) +{ +char path[PATH_MAX ]; +struct cpu_map *cpus; +const char *sysfs =sysfs__mountpoint (); - cpus =cpu_map__read (file); -fclose (file); -return cpus; +snprintf (path,PATH_MAX ,CPUS_TEMPLATE_UNCORE , sysfs, name); + cpus =__pmu_cpumask (path); +cpu_map__put (cpus); + +return !!cpus; }/* @@ -617,6 +641,8 @@ static struct perf_pmu *pmu_lookup(const char *name) pmu->cpus =pmu_cpumask (name); + pmu->is_uncore =pmu_is_uncore (name); +INIT_LIST_HEAD (&pmu->format);INIT_LIST_HEAD (&pmu->aliases);list_splice (&format, &pmu->format);tools/perf/util/pmu.h +1
-0@@ -21,6 +21,7 @@ struct perf_pmu {char *name;__u32 type;bool selectable; +bool is_uncore;struct perf_event_attr *default_config;struct cpu_map *cpus;struct list_head format;/* HEAD struct perf_pmu_format -> list */ Signature · 1 line
2.11.0