perf header: Fix missing PMU caps recording in write_pmu_caps
PATCH 1/1 perf header: Fix missing PMU caps recording in write_pmu_caps
An error occurred when executing "perf script syscall-counts" on arm64:
Press control+C to stop and show the summary
0x6a7c [0x14]: failed to process type: 80
After debugging i found that it failed to process HEADER_PMU_CAPS, looking back on write_pmu_caps(), it get all pmus with caps except "cpu" first, then handle the hybrid core pmus, and the uncore pmus finally.
But on arm64 platforms, the core pmu is named "armv8_pmuv3" etc, not "cpu". So it's counted but no caps recorded here, this is broken since commit 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps"), as the logic for determining hybrid is changed from name matching to "core" attribute judging.
To fix this, change the logic of recording pmu caps to be consistent with the preceding counting, also make the code clear. The compatibility with old perf tool is ignored since it has been already broken.
--- tools/perf/util/header.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
tools/perf/util/header.c +2 -14
@@ -1618,22 +1618,10 @@ static int write_pmu_caps(struct feat_fd *ff,if (!nr_pmu)return 0 ; -/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. -*/ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - } - } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (!pmu->name || !strcmp (pmu->name,"cpu" ) || + !pmu->nr_caps)continue ; ret =__write_pmu_caps (ff, pmu, true);
Signature · 1 line
2.25.1
Ian Rogers
reviewerQuoted 44 lines · Wei Li
An error occurred when executing "perf script syscall-counts" on arm64:
Press control+C to stop and show the summary
0x6a7c[0x14]: failed to process type: 80After debugging i found that it failed to process
HEADER_PMU_CAPS, looking back onwrite_pmu_caps(), it get all pmus with caps except "cpu" first, then handle the hybrid core pmus, and the uncore pmus finally.But on arm64 platforms, the core pmu is named "
armv8_pmuv3" etc, not "cpu". So it's counted but no caps recorded here, this is broken since commit178ddf3bad98("perf header: Avoid hybrid PMU list inwrite_pmu_caps"), as the logic for determining hybrid is changed from name matching to "core" attribute judging.To fix this, change the logic of recording pmu caps to be consistent with the preceding counting, also make the code clear. The compatibility with old perf tool is ignored since it has been already broken.
Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")Signed-off-by Wei Li <[email protected]>--- tools/perf/util/header.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)tools/perf/util/header.c +0
-11@@ -1618,22 +1618,10 @@ static int write_pmu_caps(struct feat_fd *ff,if (!nr_pmu)return 0 ; -/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. -*/ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - }
tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1627
Quoted 10 lines
tools/perf/util/header.c +2
-3- } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (!pmu->name || !strcmp (pmu->name,"cpu" ) || + !pmu->nr_caps)continue ; ret =__write_pmu_caps (ff, pmu, true);
Sorry for the breakage. I think all of the branches are interfering here, I think a simpler solution is:
```
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -1605,8 +1605,7 @@ static int write_pmu_caps(struct feat_fd *ff,
int ret;
while ((pmu = perf_pmus__scan(pmu))) {
- if (!pmu->name || !strcmp(pmu->name, "cpu") ||
- perf_pmu__caps_parse(pmu) <= 0)
+ if (perf_pmu__caps_parse(pmu) <= 0)
continue;
nr_pmu++;
}
@@ -1619,23 +1618,13 @@ static int write_pmu_caps(struct feat_fd *ff,
return 0;
/*
- * Write hybrid pmu caps first to maintain compatibility with
- * older perf tool.
+ * Note older perf tools assume core PMUs come first, this is a property
+ * of perf_pmus__scan.
*/
- if (perf_pmus__num_core_pmus() > 1) {
- pmu = NULL;
- while ((pmu = perf_pmus__scan_core(pmu))) {
- ret = __write_pmu_caps(ff, pmu, true);
- if (ret < 0)
- return ret;
- }
- }
-
pmu = NULL;
while ((pmu = perf_pmus__scan(pmu))) {
- if (pmu->is_core || !pmu->nr_caps)
+ if (perf_pmu__caps_parse(pmu) <= 0)
continue;
-
ret = __write_pmu_caps(ff, pmu, true);
if (ret < 0)
return ret;
```Could you try it out?
Thanks,
Ian
Quoted 3 lines
--
2.25.1
Hi Ian:
On 2023/8/17 11:31, Ian Rogers wrote:
(SNIP)
Quoted 76 lines
Quoted 20 lines
tools/perf/util/header.c +0
-11@@ -1618,22 +1618,10 @@ static int write_pmu_caps(struct feat_fd *ff,if (!nr_pmu)return 0 ; -/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. -*/ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - }tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1627
Quoted 10 lines
tools/perf/util/header.c +2
-3- } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (!pmu->name || !strcmp (pmu->name,"cpu" ) || + !pmu->nr_caps)continue ; ret =__write_pmu_caps (ff, pmu, true);Sorry for the breakage. I think all of the branches are interfering here, I think a simpler solution is:
```
tools/perf/util/header.c +4
-15@@ -1605,8 +1605,7 @@ static int write_pmu_caps(struct feat_fd *ff,int ret;while ((pmu =perf_pmus__scan (pmu))) { -if (!pmu->name || !strcmp (pmu->name,"cpu" ) || -perf_pmu__caps_parse (pmu) <=0 ) +if (perf_pmu__caps_parse (pmu) <=0 )continue ; nr_pmu++; }@@ -1619,23 +1618,13 @@ static int write_pmu_caps(struct feat_fd *ff,return 0 ;/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. +* Note older perf tools assume core PMUs come first, this is a property +* of perf_pmus__scan. */ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - } - } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (perf_pmu__caps_parse (pmu) <=0 )continue ; - ret =__write_pmu_caps (ff, pmu, true);if (ret <0 )return ret;```
Quoted 2 lines
Could you try it out?
I have tested your patch, it does make "perf script" work, but your modification records all the pmus with caps, including "cpu" on x86. This does not match the spec in tools/perf/Documentation/perf.data-file-format.txt:
HEADER_PMU_CAPS = 31,
List of pmu capabilities (except cpu pmu which is already covered by HEADER_CPU_PMU_CAPS). Note that hybrid cpu pmu capabilities are also stored here.
---
The header info of perf.data:
# bpf_prog_info 5809: bpf_prog_6deef7357e7b4530 addr 0xffffffffc11a10e3 size 61 # cpu pmu capabilities: branches=32, max_precise=3, pmu_name=skylake # cpu pmu capabilities: branches=32, max_precise=3, pmu_name=skylake # intel_pt pmu capabilities: topa_multiple_entries=1, psb_cyc=1, single_range_output=1, mtc_periods=249, ip_filtering=1, output_subsys=0, cr3_filtering=1, psb_periods=3f, cycle_thresholds=3fff, power_event_t> # missing features: TRACING_DATA BRANCH_STACK GROUP_DESC AUXTRACE STAT CLOCKID DIR_FORMAT COMPRESSED CLOCK_DATA HYBRID_TOPOLOGY
Thanks,
Wei
Ian Rogers
reviewerQuoted 104 lines · liwei (GF)
Hi Ian:
On 2023/8/17 11:31, Ian Rogers wrote:
(SNIP)Quoted 76 lines
Quoted 20 lines
tools/perf/util/header.c +0
-11@@ -1618,22 +1618,10 @@ static int write_pmu_caps(struct feat_fd *ff,if (!nr_pmu)return 0 ; -/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. -*/ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - }tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1627
Quoted 10 lines
tools/perf/util/header.c +2
-3- } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (!pmu->name || !strcmp (pmu->name,"cpu" ) || + !pmu->nr_caps)continue ; ret =__write_pmu_caps (ff, pmu, true);Sorry for the breakage. I think all of the branches are interfering here, I think a simpler solution is:
```
tools/perf/util/header.c +4
-15@@ -1605,8 +1605,7 @@ static int write_pmu_caps(struct feat_fd *ff,int ret;while ((pmu =perf_pmus__scan (pmu))) { -if (!pmu->name || !strcmp (pmu->name,"cpu" ) || -perf_pmu__caps_parse (pmu) <=0 ) +if (perf_pmu__caps_parse (pmu) <=0 )continue ; nr_pmu++; }@@ -1619,23 +1618,13 @@ static int write_pmu_caps(struct feat_fd *ff,return 0 ;/* -* Write hybrid pmu caps first to maintain compatibility with -* older perf tool. +* Note older perf tools assume core PMUs come first, this is a property +* of perf_pmus__scan. */ -if (perf_pmus__num_core_pmus () >1 ) { - pmu =NULL ; -while ((pmu =perf_pmus__scan_core (pmu))) { - ret =__write_pmu_caps (ff, pmu, true); -if (ret <0 ) -return ret; - } - } - pmu =NULL ;while ((pmu =perf_pmus__scan (pmu))) { -if (pmu->is_core || !pmu->nr_caps) +if (perf_pmu__caps_parse (pmu) <=0 )continue ; - ret =__write_pmu_caps (ff, pmu, true);if (ret <0 )return ret;```
Quoted 2 lines
Could you try it out?
I have tested your patch, it does make "perf script" work, but your modification records all the pmus with caps, including "cpu" on x86. This does not match the spec in
tools/perf/Documentation/perf.data-file-format.txt:
HEADER_PMU_CAPS= 31,List of pmu capabilities (except cpu pmu which is already covered by
HEADER_CPU_PMU_CAPS). Note that hybrid cpu pmu capabilities are also stored here.---
The header info of perf.data:# bpf_prog_info 5809: bpf_prog_6deef7357e7b4530 addr 0xffffffffc11a10e3 size 61 # cpu pmu capabilities: branches=32, max_precise=3, pmu_name=skylake # cpu pmu capabilities: branches=32, max_precise=3, pmu_name=skylake # intel_pt pmu capabilities: topa_multiple_entries=1, psb_cyc=1, single_range_output=1, mtc_periods=249, ip_filtering=1, output_subsys=0, cr3_filtering=1, psb_periods=3f, cycle_thresholds=3fff, power_event_t> # missing features: TRACING_DATA BRANCH_STACK GROUP_DESC AUXTRACE STAT CLOCKID DIR_FORMAT COMPRESSED CLOCK_DATA HYBRID_TOPOLOGY
Quoted 2 lines
Thanks,
Wei
Thanks Wei! I'll update the patch I wrote and skip "cpu" with the comment that it is already covered by HEADER_CPU_PMU_CAPS. It feels that caps should really cover hybrid, but I was trying to refactor and cleanup not change the perf.data format.
Thanks,
Ian