Skip to contentSkip to conversation

Fix num_events calculation in lazy loading

PATCH Applied Jia He 3 participants 8 messages May 10, 2024 – May 11, 2024 perf events

lore
Share & tools
lore

Jia He

author
#0 lore raw

cover Fix num_events calculation in lazy loading

I noticed "perf list" reported the error as follows on an Armv8 Neoverse N2 server:
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

The root cause is due to the incorrect calculation in perf_pmu__num_events().

Jia He (2):
perf pmu: Allow finishing loading json events when !events_table perf pmu: Fix num_events calculation

 tools/perf/util/pmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 perf pmu: Allow finishing loading json events when !events_table

Otherwise, cpu_aliases_added is never set to true on an Arm v8a Neoverse N2 server.

---
 tools/perf/util/pmu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/perf/util/pmu.c +2 -4
index f39cbbc1a7ec..a1eef7b2e389 100644
@@ -915,13 +915,11 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_tab
 
 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
 {
-	if (!pmu->events_table)
-		return;
-
 	if (pmu->cpu_aliases_added)
 		return;
 
-	pmu_add_cpu_aliases_table(pmu, pmu->events_table);
+	if (pmu->events_table)
+		pmu_add_cpu_aliases_table(pmu, pmu->events_table);
 	pmu->cpu_aliases_added = true;
 }
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 perf pmu: Fix num_events calculation

In reply to Jia He

When pe is NULL in the function perf_pmu__new_alias(), the total number of events is added to loaded_json_aliases. However, if pmu->events_table is NULL and cpu_aliases_added is false, the calculation for the events number in perf_pmu__num_events() is incorrect.

Then cause the error report after "perf list":
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

Fix it by adding loaded_json_aliases in the calculation under the mentioned conditions.

Test it also with "perf bench internals pmu-scan" and there is no regression.

Fixes e6ff1eed3584 ("perf pmu: Lazily add JSON events")
---
 tools/perf/util/pmu.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/pmu.c +2 -0
index a1eef7b2e389..a53224e2ce7e 100644
@@ -1639,6 +1639,8 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
 		 nr += pmu->loaded_json_aliases;
 	else if (pmu->events_table)
 		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
+	else
+		nr += pmu->loaded_json_aliases;
 
 	return pmu->selectable ? nr + 1 : nr;
 }
Signature · 1 line
2.34.1
reviewer
#3 lore raw

Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 26 lines · Jia He

When pe is NULL in the function perf_pmu__new_alias(), the total number of events is added to loaded_json_aliases. However, if pmu->events_table is NULL and cpu_aliases_added is false, the calculation for the events number in perf_pmu__num_events() is incorrect.

Then cause the error report after "perf list":
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

Fix it by adding loaded_json_aliases in the calculation under the mentioned conditions.

Test it also with "perf bench internals pmu-scan" and there is no regression.

Fixes e6ff1eed3584 ("perf pmu: Lazily add JSON events")
---
 tools/perf/util/pmu.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/pmu.c +0 -0
index a1eef7b2e389..a53224e2ce7e 100644

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1643

Quoted 5 lines
tools/perf/util/pmu.c +2 -0
@@ -1639,6 +1639,8 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
                 nr += pmu->loaded_json_aliases;
        else if (pmu->events_table)
                nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
+       else
+               nr += pmu->loaded_json_aliases;

Thanks for working on this! The "struct pmu_event *pe" in new_alias is an entry from the json data, and "pmu->events_table" should NULL if there is no json data. I believe the code is assuming that these lines aren't necessary as it shouldn't be possible to load json data if the json events table doesn't exist for the PMU - if there is no json data then loaded_json_aliases should be 0 and no addition is necessary. I'm wondering why this case isn't true for you.

Thanks,
Ian

Quoted 6 lines
tools/perf/util/pmu.c +0 -1
        return pmu->selectable ? nr + 1 : nr;
 }
--

2.34.1

author
#4 lore raw

RE: [PATCH 2/2] perf pmu: Fix num_events calculation

Hi, Ian

Quoted 50 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Friday, May 10, 2024 2:17 PM
To: Justin He <[email protected]>
Cc: Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Adrian Hunter <[email protected]>; Kan Liang <[email protected]>; James Clark <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 8 lines · Jia He

When pe is NULL in the function perf_pmu__new_alias(), the total number of events is added to loaded_json_aliases. However, if pmu->events_table is NULL and cpu_aliases_added is false, the calculation for the events number in perf_pmu__num_events() is incorrect.

Then cause the error report after "perf list":
Unexpected event

smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu

Quoted 18 lines

Fix it by adding loaded_json_aliases in the calculation under the mentioned conditions.

Test it also with "perf bench internals pmu-scan" and there is no regression.

Fixes e6ff1eed3584 ("perf pmu: Lazily add JSON events")
---
 tools/perf/util/pmu.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/pmu.c index renamed from tools/perf/util/pmu.c +0 -0

a1eef7b2e389..a53224e2ce7e 100644

tools/perf/util/pmu.c +0 -0
@@ -1639,6 +1639,8 @@ size_t perf_pmu__num_events(struct perf_pmu

*pmu)

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu)

Quoted 3 lines
tools/perf/util/pmu.c +0 -0
                 nr += pmu->loaded_json_aliases;
        else if (pmu->events_table)
                nr +=

pmu_events_table__num_events(pmu->events_table,

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1643

Quoted 3 lines
tools/perf/util/pmu.c +2 -0
 pmu) - pmu->loaded_json_aliases;
+       else
+               nr += pmu->loaded_json_aliases;
Quoted 6 lines

Thanks for working on this! The "struct pmu_event *pe" in new_alias is an entry from the json data, and "pmu->events_table" should NULL if there is no json data. I believe the code is assuming that these lines aren't necessary as it shouldn't be possible to load json data if the json events table doesn't exist for the PMU - if there is no json data then loaded_json_aliases should be 0 and no addition is necessary. I'm wondering why this case isn't true for you.

On my Armv8a N2 server, "pmu->events_table" is NULL because perf_pmu__find_events_table() return NULL.

I also noticed that pmu->loaded_json_aliases is *not* 0. The missing adding calculation will cause perf_pmu__num_events() less than normal case and will trigger latter check failure in perf_pmus__print_pmu_events__callback().
At last, perf list will report many lines similar as:
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

--
Cheers,
Justin (Jia He)

reviewer
#5 lore raw

Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 59 lines · Justin He

Hi, Ian

Quoted 50 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Friday, May 10, 2024 2:17 PM
To: Justin He <[email protected]>
Cc: Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Adrian Hunter <[email protected]>; Kan Liang <[email protected]>; James Clark <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 8 lines · Jia He

When pe is NULL in the function perf_pmu__new_alias(), the total number of events is added to loaded_json_aliases. However, if pmu->events_table is NULL and cpu_aliases_added is false, the calculation for the events number in perf_pmu__num_events() is incorrect.

Then cause the error report after "perf list":
Unexpected event

smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu

Quoted 18 lines

Fix it by adding loaded_json_aliases in the calculation under the mentioned conditions.

Test it also with "perf bench internals pmu-scan" and there is no regression.

Fixes e6ff1eed3584 ("perf pmu: Lazily add JSON events")
---
 tools/perf/util/pmu.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/pmu.c index renamed from tools/perf/util/pmu.c +0 -0

a1eef7b2e389..a53224e2ce7e 100644

tools/perf/util/pmu.c +0 -0
@@ -1639,6 +1639,8 @@ size_t perf_pmu__num_events(struct perf_pmu

*pmu)

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu)

Quoted 3 lines
tools/perf/util/pmu.c +0 -0
                 nr += pmu->loaded_json_aliases;
        else if (pmu->events_table)
                nr +=

pmu_events_table__num_events(pmu->events_table,

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1643

Quoted 3 lines
tools/perf/util/pmu.c +2 -0
 pmu) - pmu->loaded_json_aliases;
+       else
+               nr += pmu->loaded_json_aliases;
Quoted 6 lines

Thanks for working on this! The "struct pmu_event *pe" in new_alias is an entry from the json data, and "pmu->events_table" should NULL if there is no json data. I believe the code is assuming that these lines aren't necessary as it shouldn't be possible to load json data if the json events table doesn't exist for the PMU - if there is no json data then loaded_json_aliases should be 0 and no addition is necessary. I'm wondering why this case isn't true for you.

Quoted 8 lines

On my Armv8a N2 server, "pmu->events_table" is NULL because perf_pmu__find_events_table() return NULL.

I also noticed that pmu->loaded_json_aliases is *not* 0. The missing adding calculation will cause perf_pmu__num_events() less than normal case and will trigger latter check failure in perf_pmus__print_pmu_events__callback().
At last, perf list will report many lines similar as:
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

The issue is the sys events which currently are ARM only. Here is an lldb stack trace:

```
    frame #6: 0x0000aaaaabc9b49c
perf`__assert_fail(assertion="pmu->events_table",
file="tools/perf/util/pmu.c", line=522, function="int
perf_pmu__new_alias(struct perf_pmu *, const char *, const char *,
const char *, FILE *, const struct pmu_event *)")
    frame #7: 0x0000aaaaab41e018
perf`perf_pmu__new_alias(pmu=0x00007377e7e09b40,
name="hnf_brd_snoops_sent", desc="Counts number of multicast snoops
sent (not including SF back invalidation)", val="eventid=9,type=5",
val_fd=0x0000000000000000, pe=0x0000ffffffffde88) at pmu.c:522:3
    frame #8: 0x0000aaaaab4175cc
perf`pmu_add_sys_aliases_iter_fn(pe=0x0000ffffffffde88,
table=0x0000aaaaac299bb0, vdata=0x00007377e7e09b40) at pmu.c:939:3
    frame #9: 0x0000aaaaaaf6d000
perf`pmu_events_table__for_each_event_pmu(table=0x0000aaaaac299bb0,
pmu=0x0000aaaaac299238, fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:5994:23
    frame #10: 0x0000aaaaaaf6cd78
perf`pmu_events_table__for_each_event(table=0x0000aaaaac299bb0,
pmu=0x0000000000000000, fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:6057:23
    frame #11: 0x0000aaaaaaf6ec44
perf`pmu_for_each_sys_event(fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:6295:27
    frame #12: 0x0000aaaaab41738c
perf`pmu_add_sys_aliases(pmu=0x00007377e7e09b40) at pmu.c:955:2
    frame #13: 0x0000aaaaab4179fc
perf`perf_pmu__lookup(pmus=0x0000aaaaac500970, dirfd=34,
name="arm_cmn_0") at pmu.c:1037:2
    frame #14: 0x0000aaaaab4242d0 perf`perf_pmu__find2(dirfd=34,
name="arm_cmn_0") at pmus.c:161:9
    frame #15: 0x0000aaaaab421614 perf`pmu_read_sysfs(core_only=false)
at pmus.c:209:3
    frame #16: 0x0000aaaaab42278c
perf`perf_pmus__scan_skip_duplicates(pmu=0x0000000000000000) at
pmus.c:297:3
    frame #17: 0x0000aaaaab422078
perf`perf_pmus__print_pmu_events(print_cb=0x0000ffffffffec68,
print_state=0x0000ffffffffecd0) at pmus.c:462:16
    frame #18: 0x0000aaaaab425d9c
perf`print_events(print_cb=0x0000ffffffffec68,
print_state=0x0000ffffffffecd0) at print-events.c:409:2
    frame #19: 0x0000aaaaab093ef0 perf`cmd_list(argc=0,
argv=0x0000fffffffff340) at builtin-list.c:592:3
    frame #20: 0x0000aaaaaaf5b490
perf`run_builtin(p=0x0000aaaaac4e8220, argc=1,
argv=0x0000fffffffff340) at perf.c:349:11
    frame #21: 0x0000aaaaaaf5a3e0 perf`handle_internal_command(argc=1,
argv=0x0000fffffffff340) at perf.c:402:8
    frame #22: 0x0000aaaaaaf5b1a0
perf`run_argv(argcp=0x0000fffffffff1c8, argv=0x0000fffffffff1c0) at
perf.c:446:2
    frame #23: 0x0000aaaaaaf59f44 perf`main(argc=1,
argv=0x0000fffffffff340) at perf.c:562:3
```

As such the fix here is incomplete. There may be both sys json events (detected by PMU/id name) and CPU json events (detected by CPUID). I'm looking into a fix.

Thanks,
Ian

Quoted 3 lines

--
Cheers,
Justin (Jia He)

reviewer
#6 lore raw

Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 129 lines · Ian Rogers
Quoted 59 lines · Justin He

Hi, Ian

Quoted 50 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Friday, May 10, 2024 2:17 PM
To: Justin He <[email protected]>
Cc: Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Adrian Hunter <[email protected]>; Kan Liang <[email protected]>; James Clark <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 8 lines · Jia He

When pe is NULL in the function perf_pmu__new_alias(), the total number of events is added to loaded_json_aliases. However, if pmu->events_table is NULL and cpu_aliases_added is false, the calculation for the events number in perf_pmu__num_events() is incorrect.

Then cause the error report after "perf list":
Unexpected event

smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu

Quoted 18 lines

Fix it by adding loaded_json_aliases in the calculation under the mentioned conditions.

Test it also with "perf bench internals pmu-scan" and there is no regression.

Fixes e6ff1eed3584 ("perf pmu: Lazily add JSON events")
---
 tools/perf/util/pmu.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/pmu.c index renamed from tools/perf/util/pmu.c +0 -0

a1eef7b2e389..a53224e2ce7e 100644

tools/perf/util/pmu.c +0 -0
@@ -1639,6 +1639,8 @@ size_t perf_pmu__num_events(struct perf_pmu

*pmu)

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu)

Quoted 3 lines
tools/perf/util/pmu.c +0 -0
                 nr += pmu->loaded_json_aliases;
        else if (pmu->events_table)
                nr +=

pmu_events_table__num_events(pmu->events_table,

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1643

Quoted 3 lines
tools/perf/util/pmu.c +2 -0
 pmu) - pmu->loaded_json_aliases;
+       else
+               nr += pmu->loaded_json_aliases;
Quoted 6 lines

Thanks for working on this! The "struct pmu_event *pe" in new_alias is an entry from the json data, and "pmu->events_table" should NULL if there is no json data. I believe the code is assuming that these lines aren't necessary as it shouldn't be possible to load json data if the json events table doesn't exist for the PMU - if there is no json data then loaded_json_aliases should be 0 and no addition is necessary. I'm wondering why this case isn't true for you.

Quoted 8 lines

On my Armv8a N2 server, "pmu->events_table" is NULL because perf_pmu__find_events_table() return NULL.

I also noticed that pmu->loaded_json_aliases is *not* 0. The missing adding calculation will cause perf_pmu__num_events() less than normal case and will trigger latter check failure in perf_pmus__print_pmu_events__callback().
At last, perf list will report many lines similar as:
Unexpected event smmuv3_pmcg_3f062/smmuv3_pmcg_3f062/transaction//

The issue is the sys events which currently are ARM only. Here is an lldb stack trace:

```
    frame #6: 0x0000aaaaabc9b49c
perf`__assert_fail(assertion="pmu->events_table",
file="tools/perf/util/pmu.c", line=522, function="int
perf_pmu__new_alias(struct perf_pmu *, const char *, const char *,
const char *, FILE *, const struct pmu_event *)")
    frame #7: 0x0000aaaaab41e018
perf`perf_pmu__new_alias(pmu=0x00007377e7e09b40,
name="hnf_brd_snoops_sent", desc="Counts number of multicast snoops
sent (not including SF back invalidation)", val="eventid=9,type=5",
val_fd=0x0000000000000000, pe=0x0000ffffffffde88) at pmu.c:522:3
    frame #8: 0x0000aaaaab4175cc
perf`pmu_add_sys_aliases_iter_fn(pe=0x0000ffffffffde88,
table=0x0000aaaaac299bb0, vdata=0x00007377e7e09b40) at pmu.c:939:3
    frame #9: 0x0000aaaaaaf6d000
perf`pmu_events_table__for_each_event_pmu(table=0x0000aaaaac299bb0,
pmu=0x0000aaaaac299238, fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:5994:23
    frame #10: 0x0000aaaaaaf6cd78
perf`pmu_events_table__for_each_event(table=0x0000aaaaac299bb0,
pmu=0x0000000000000000, fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:6057:23
    frame #11: 0x0000aaaaaaf6ec44
perf`pmu_for_each_sys_event(fn=(perf`pmu_add_sys_aliases_iter_fn at
pmu.c:931), data=0x00007377e7e09b40) at pmu-events.c:6295:27
    frame #12: 0x0000aaaaab41738c
perf`pmu_add_sys_aliases(pmu=0x00007377e7e09b40) at pmu.c:955:2
    frame #13: 0x0000aaaaab4179fc
perf`perf_pmu__lookup(pmus=0x0000aaaaac500970, dirfd=34,
name="arm_cmn_0") at pmu.c:1037:2
    frame #14: 0x0000aaaaab4242d0 perf`perf_pmu__find2(dirfd=34,
name="arm_cmn_0") at pmus.c:161:9
    frame #15: 0x0000aaaaab421614 perf`pmu_read_sysfs(core_only=false)
at pmus.c:209:3
    frame #16: 0x0000aaaaab42278c
perf`perf_pmus__scan_skip_duplicates(pmu=0x0000000000000000) at
pmus.c:297:3
    frame #17: 0x0000aaaaab422078
perf`perf_pmus__print_pmu_events(print_cb=0x0000ffffffffec68,
print_state=0x0000ffffffffecd0) at pmus.c:462:16
    frame #18: 0x0000aaaaab425d9c
perf`print_events(print_cb=0x0000ffffffffec68,
print_state=0x0000ffffffffecd0) at print-events.c:409:2
    frame #19: 0x0000aaaaab093ef0 perf`cmd_list(argc=0,
argv=0x0000fffffffff340) at builtin-list.c:592:3
    frame #20: 0x0000aaaaaaf5b490
perf`run_builtin(p=0x0000aaaaac4e8220, argc=1,
argv=0x0000fffffffff340) at perf.c:349:11
    frame #21: 0x0000aaaaaaf5a3e0 perf`handle_internal_command(argc=1,
argv=0x0000fffffffff340) at perf.c:402:8
    frame #22: 0x0000aaaaaaf5b1a0
perf`run_argv(argcp=0x0000fffffffff1c8, argv=0x0000fffffffff1c0) at
perf.c:446:2
    frame #23: 0x0000aaaaaaf59f44 perf`main(argc=1,
argv=0x0000fffffffff340) at perf.c:562:3
```
Quoted 3 lines

As such the fix here is incomplete. There may be both sys json events (detected by PMU/id name) and CPU json events (detected by CPUID). I'm looking into a fix.

Patch sent:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 6 lines

Thanks,
Ian

Quoted 3 lines

--
Cheers,
Justin (Jia He)

maintainer Applied
#7 lore raw

Re: [PATCH 2/2] perf pmu: Fix num_events calculation

Quoted 7 lines · Ian Rogers
Quoted 3 lines · Ian Rogers

As such the fix here is incomplete. There may be both sys json events (detected by PMU/id name) and CPU json events (detected by CPUID). I'm looking into a fix.

Patch sent:
https://lore.kernel.org/lkml/[email protected]/

I applied the patch to tmp.perf-tools-next, will go to perf-tools-next soon, would be good for Justin to test and provide a Tested-by.

- Arnaldo