[PATCH 0/4] Bug fixes on topdown metrics group leader selection

Dapeng Mi
2024-07-02T06:39:42Z

when counting/sampling topdown slots and metrics events, the following
issues are found.

a. incorrect sampling leader selection if group only contains topdown
slots event without topdown metrics event, such as

perf record -e '{slots,branches}:S' -c 10000 -vv sleep 1

In this case, the slots event should be sampled as leader but the
branches event is sampled in fact like the verbose output shows.

perf_event_attr:
type 4 (cpu)
size 168
config 0x400 (slots)
sample_type IP|TID|TIME|READ|CPU|IDENTIFIER
read_format ID|GROUP|LOST
disabled 1
sample_id_all 1
exclude_guest 1
------------------------------------------------------------
sys_perf_event_open: pid -1 cpu 0 group_fd -1 flags 0x8 = 5
------------------------------------------------------------
perf_event_attr:
type 0 (PERF_TYPE_HARDWARE)
size 168
config 0x4 (PERF_COUNT_HW_BRANCH_INSTRUCTIONS)
{ sample_period, sample_freq } 10000
sample_type IP|TID|TIME|READ|CPU|IDENTIFIER
read_format ID|GROUP|LOST
sample_id_all 1
exclude_guest 1

b. Fail to run the perf command

perf record -e "{slots,instructions,topdown-retiring}:S" -vv -C0 sleep 1

------------------------------------------------------------
perf_event_attr:
type 4 (cpu)
size 168
config 0x400 (slots)
sample_type IP|TID|TIME|READ|CPU|PERIOD|IDENTIFIER
read_format ID|GROUP|LOST
disabled 1
sample_id_all 1
exclude_guest 1
------------------------------------------------------------
sys_perf_event_open: pid -1 cpu 0 group_fd -1 flags 0x8 = 5
------------------------------------------------------------
perf_event_attr:
type 4 (cpu)
size 168
config 0x8000 (topdown-retiring)
{ sample_period, sample_freq } 4000
sample_type IP|TID|TIME|READ|CPU|PERIOD|IDENTIFIER
read_format ID|GROUP|LOST
freq 1
sample_id_all 1
exclude_guest 1
------------------------------------------------------------
sys_perf_event_open: pid -1 cpu 0 group_fd 5 flags 0x8
sys_perf_event_open failed, error -22

Error:
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (topdown-retiring).

Perf tool tries to regroup the events and move topdown-retiring event
closely after slots event and leads to topdown-retiring event is select
to sample. This is incorrect.

c. unnecessary events regroup for the group which only has slots event
but without topdown metrics events, such as

perf stat -e "{instructions,slots}" -C 0 sleep 1
WARNING: events were regrouped to match PMUs

Performance counter stats for 'CPU(s) 0':

27,581,148 slots
8,390,827 instructions

1.045546595 seconds time elapsed

Obviously, this events regroup is unnecessary.

The patches 1-3 separately fixes the above 3 issues in order and the
patch 4/4 adds a new perf test to verify the leader sampling.

Dapeng Mi (4):
perf topdown: Correct leader selection with sample_read enabled
perf parse-events: Don't move topdown metrics events when sorting
events
perf parse-events: Don't move slots event when no topdwon metrics
event
perf tests: Add leader sampling test in record tests

tools/perf/arch/x86/util/evlist.c | 11 ++++------
tools/perf/arch/x86/util/topdown.c | 16 ++++++++++++--
tools/perf/tests/shell/record.sh | 28 ++++++++++++++++++++++++
tools/perf/util/evlist.h | 7 +++++-
tools/perf/util/parse-events.c | 35 ++++++++++++++++++------------
5 files changed, 73 insertions(+), 24 deletions(-)

base-commit: 73e931504f8e0d42978bfcda37b323dbbd1afc08
--
2.40.1