perf sysinfo: Add subcommand to probe and summarize PMU capabilities
Ian Rogers
reviewercover perf sysinfo: Add subcommand to probe and summarize PMU capabilities
Determining what performance monitoring hardware, driver capabilities, and event/metric support are available on a system currently requires piecing together sysfs files under /sys/bus/event_source/devices/, perf list output, and trial-and-error perf_event_open() calls. Across architectures and SoCs, support varies widely: an advanced system provides core PMUs with zero-skid precise event sampling (PEBS/IBS/SPE), hardware branch stack recording (LBR/BRBE), user-space counter reading (rdpmc), memory profiling (DATA_SRC), hardware AUX trace PMUs, memory and PCIe/IO interconnect bandwidth PMUs, and multi-level pipeline metric drill-down hierarchies, whereas a minimal system may only offer software timer sampling.
This RFC series introduces 'perf sysinfo' to inspect, actively probe, and summarize the PMU and performance monitoring capabilities of a live system or a recorded perf.data file:
- Patch 1 adds PMU environment reconstruction from perf_env headers (perf_pmus__read_from_env()), public duplicate-collapsing PMU scan helpers, and active live capability probing via perf_event_open() (perf_pmu__probe()). Probing tests counting, sampling, branch stack sampling, precise_ip levels (:p, :pp, :ppp) and empirically measured branch instruction skid across an 8,192-nop runway, supported perf_event_sample_format bits, user/intr general-purpose and SIMD register masks, memory profiling events, privilege exclusion modifiers, execution features (leader sampling, sw_grouping, auto-counter reload, AUX pause/resume/sample), user-space rdpmc availability/enablement and PMC bit-width, and maximum event group sizes (both openable and simultaneously active without multiplexing).
- Patch 2 adds the 'perf sysinfo' subcommand, documentation, and shell test. By default, 'perf sysinfo' outputs a colorized system capability summary, pipeline metric drill-down hierarchy (via <metric>_group chains), overall system rating (Advanced, Standard, Basic, Minimal), and any missing baseline features. Passing '-p'/'--pmus' (or a PMU glob filter, '-v'/'--verbose', '-a'/'--all', or '-j'/'--json') displays detailed per-PMU static and probed attributes.
Example default output on an Intel AlderLake hybrid system:
$ perf sysinfo
Summary:
Core PMUs (2, hybrid): cpu_atom, cpu_core
Core features:
Interrupt-based sampling: supported
Precise event sampling: supported (max_precise: 3 [3 - SAMPLE_IP must have 0 skid, or uses randomization])
Branch stack sampling (LBR/BRBE): supported (depth: 32 branches)
User-space counter reading (rdpmc): supported
Leader sampling: supported
Memory profiling events (DATA_SRC): supported
Hardware AUX trace PMUs: intel_bts, intel_pt
Bus & uncore monitoring:
Memory bandwidth PMUs: uncore_imc, uncore_imc_free_running
PCIe / IO interconnect bandwidth PMUs: uncore_arb
Events & metrics:
Available events: 3161 total (76 sysfs, 3085 json) across 28 PMUs
Available metrics: 438 metrics in 192 groups (advanced)
Metric drill-down levels (6): L1 (TopdownL1: 7 metrics), L2 (TopdownL2: 14 metrics), L3 (TopdownL3: 44 metrics), L4 (TopdownL4: 45 metrics), L5 (TopdownL5: 15 metrics), L6 (TopdownL6: 9 metrics)
System rating: Advanced (core PMU with precise sampling, branch stack (LBR), user rdpmc, bus PMUs, and metric drill-down)Example filtered per-PMU output:
$ perf sysinfo cpu_core
cpu_core (type: 4 [PERF_TYPE_RAW], core):
CPUs: 0-15 (16 CPUs)
Capabilities: branches=32, max_precise=3, pmu_name=alderlake_hybrid
Formats: event (config:0-7), pc (config:19), edge (config:18), offcore_rsp (config1:0-63), ldlat (config1:0-15), inv (config:23), umask (config:8-15), frontend (config1:0-23), cmask (config:24-31)
Events: 1563 total (20 sysfs, 1543 json)
Memory events: ldlat-loads ({cpu_core/mem-loads-aux/,cpu_core/mem-loads,ldlat=30/}:P), ldlat-stores (cpu_core/mem-stores/P)
Probed capabilities (event: cpu_core/cycles/):
Modes: counting, sampling, branch_stack
Precise sampling: :p (1 - SAMPLE_IP must have constant skid), :pp (2 - SAMPLE_IP requested to have 0 skid), :ppp (3 - SAMPLE_IP must have 0 skid, or uses randomization) (max_precise: 3)
Measured branch skid (instructions from branch):
level 0 (no :p): min 105, max 174, avg 156.8 (0/32 exact_ip)
level 1 (:p) : min 1, max 1, avg 1.0 (0/31 exact_ip)
level 2 (:pp) : min 0, max 0, avg 0.0 (31/31 exact_ip)
level 3 (:ppp) : min 0, max 0, avg 0.0 (32/32 exact_ip)
Sample types: ip, tid, time, addr, read, callchain, id, cpu, period, stream_id, raw, branch_stack, regs_user, stack_user, weight, data_src, identifier, transaction, regs_intr, aux, cgroup, data_page_size, code_page_size, weight_struct
User registers: AX, BX, CX, DX, SI, DI, BP, SP, IP, FLAGS, CS, SS, R8, R9, R10, R11, R12, R13, R14, R15
Intr registers: AX, BX, CX, DX, SI, DI, BP, SP, IP, FLAGS, CS, SS, R8, R9, R10, R11, R12, R13, R14, R15, XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15
Memory events (probed): ldlat-loads, ldlat-stores (sample types: addr, weight, data_src, transaction, data_page_size, weight_struct)
Exclusion modifiers: exclude_user, exclude_kernel, exclude_hv, exclude_idle, exclude_host, exclude_guest
Features: leader_sampling, sw_grouping, inherit, sample_freq, write_backward, callchain, data_src, weight, pinned, exclusive, format_lost, user_rdpmc, user_time, user_time_zero
User space counter reading (rdpmc): available, enabled, working (width: 48 bits)
Max event group size: 18 (active: 8)
Max GP event group size: 16 (active: 7)This patch series was generated with the assistance of antigravity and Gemini 4.0.
Ian Rogers (2):
perf pmus: Add PMU environment loading and live capability probing perf sysinfo: Add sysinfo subcommand, documentation, and test
tools/perf/Build | 1 + tools/perf/Documentation/perf-sysinfo.txt | 109 ++ tools/perf/Documentation/perf.txt | 2 +- tools/perf/builtin-help.c | 3 +- tools/perf/builtin-sysinfo.c | 1958 ++++++++++++++++++++ tools/perf/builtin.h | 1 + tools/perf/perf.c | 1 + tools/perf/tests/shell/sysinfo.sh | 71 + tools/perf/util/mem-events.c | 14 +- tools/perf/util/mem-events.h | 3 + tools/perf/util/pmu.c | 1976 ++++++++++++++++++++- tools/perf/util/pmu.h | 135 +- tools/perf/util/pmus.c | 221 ++- tools/perf/util/pmus.h | 4 + 14 files changed, 4486 insertions(+), 13 deletions(-) create mode 100644 tools/perf/Documentation/perf-sysinfo.txt create mode 100644 tools/perf/builtin-sysinfo.c create mode 100755 tools/perf/tests/shell/sysinfo.sh
Signature · 1 line
2.56.0.212.g129ede20d9-goog
Ian Rogers
reviewerRFC PATCH 1/2 perf pmus: Add PMU environment loading and live capability probing
Add helpers to reconstruct PMU lists and capabilities from perf_env headers (for perf.data files), expose duplicate uncore PMU scanning and mem-events formatting helpers, and add perf_pmu__probe() to actively probe live PMU capabilities via perf_event_open().
Probed capabilities stored in struct perf_pmu::probed include counting, sampling, and branch_stack modes, precise_ip levels and measured branch instruction skid, supported perf_event_sample_format flags, user and interrupt sampling registers, memory profiling events and sample types, privilege exclusion modifiers, leader sampling, software event grouping, auto-counter reload, AUX actions (start_paused, pause, resume, aux_sample), user-space counter reading (rdpmc) and PMC width, and rotating event group size limits.
--- tools/perf/util/mem-events.c | 14 +- tools/perf/util/mem-events.h | 3 + tools/perf/util/pmu.c | 1976 +++++++++++++++++++++++++++++++++- tools/perf/util/pmu.h | 135 ++- tools/perf/util/pmus.c | 221 +++- tools/perf/util/pmus.h | 4 + 6 files changed, 2342 insertions(+), 11 deletions(-)
tools/perf/util/mem-events.c +7 -7
@@ -79,12 +79,12 @@ int perf_pmu__mem_events_num_mem_pmus(struct perf_pmu *pmu)return num; } -static const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, -char *buf,size_t buf_size) +const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, +char *buf,size_t buf_size) {struct perf_mem_event *e; -if (i >=PERF_MEM_EVENTS__MAX || !pmu) +if (i >=PERF_MEM_EVENTS__MAX || !pmu || !pmu->mem_events)return NULL ; e = &pmu->mem_events[i];
@@ -193,13 +193,13 @@ static bool perf_pmu__mem_events_supported(const char *mnt, struct perf_pmu *pmureturn !stat (path, &st); } -static int __perf_pmu__mem_events_init (struct perf_pmu *pmu) +int perf_pmu__mem_events_init_pmu (struct perf_pmu *pmu) {const char *mnt =sysfs__mount ();bool found = false;int j; -if (!mnt) +if (!mnt || !pmu || !pmu->mem_events)return -ENOENT ;for (j =0 ; j <PERF_MEM_EVENTS__MAX ; j++) {
@@ -209,7 +209,7 @@ static int __perf_pmu__mem_events_init(struct perf_pmu *pmu)* If the event entry isn't valid, skip initialization * and "e->supported" will keep false. */ -if (!e->tag) +if (!e || !e->tag)continue ; e->supported |=perf_pmu__mem_events_supported (mnt, pmu, e);
@@ -225,7 +225,7 @@ int perf_pmu__mem_events_init(void)struct perf_pmu *pmu =NULL ;while ((pmu =perf_pmus__scan_mem (pmu)) !=NULL ) { -if (__perf_pmu__mem_events_init (pmu)) +if (perf_pmu__mem_events_init_pmu (pmu))return -ENOENT ; }
tools/perf/util/mem-events.h +3 -0
@@ -31,7 +31,10 @@ extern struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX];extern bool perf_mem_record[PERF_MEM_EVENTS__MAX ];int perf_pmu__mem_events_parse (struct perf_pmu *pmu,const char *str); +int perf_pmu__mem_events_init_pmu (struct perf_pmu *pmu);int perf_pmu__mem_events_init (void ); +const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, +char *buf,size_t buf_size);struct perf_mem_event *perf_pmu__mem_events_ptr (struct perf_pmu *pmu,int i);struct perf_pmu *perf_mem_events_find_pmu (void );
tools/perf/util/pmu.c +1974 -2
2,010 lines not on this page · raw message
tools/perf/util/pmu.h +134 -1
148 lines not on this page · raw message
tools/perf/util/pmus.c +220 -1
249 lines not on this page · raw message
tools/perf/util/pmus.h +4 -0
@@ -6,6 +6,7 @@#include <stddef.h> struct perf_event_attr ; +struct perf_env ;struct perf_pmu ;struct print_callbacks ;
@@ -14,16 +15,19 @@ size_t pmu_name_len_no_suffix(const char *str);int pmu_name_cmp (const char *lhs_pmu_name,const char *rhs_pmu_name);void perf_pmus__destroy (void ); +int perf_pmus__read_from_env (struct perf_env *env);struct perf_pmu *perf_pmus__find (const char *name);struct perf_pmu *perf_pmus__find_by_type (unsigned int type);struct perf_pmu *perf_pmus__find_by_attr (const struct perf_event_attr *attr);struct perf_pmu *perf_pmus__scan (struct perf_pmu *pmu); +struct perf_pmu *perf_pmus__scan_skip_duplicates (struct perf_pmu *pmu);struct perf_pmu *perf_pmus__scan_core (struct perf_pmu *pmu);struct perf_pmu *perf_pmus__scan_for_event (struct perf_pmu *pmu,const char *event);struct perf_pmu *perf_pmus__scan_matching_wildcard (struct perf_pmu *pmu,const char *wildcard);struct perf_pmu *perf_pmus__scan_for_uncore_id (struct perf_pmu *pmu,const char *compat); +size_t perf_pmu__num_duplicates (const struct perf_pmu *pmu);const struct perf_pmu *perf_pmus__pmu_for_pmu_filter (const char *str);
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v1 1/2] perf pmus: Add PMU environment loading and live capability probing
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] perf pmus: out-of-bounds container_of() access in env_pmu_find_or_add() - [Medium] perf pmus…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] perf pmus: out-of-bounds container_of() access in env_pmu_find_or_add()
- [Medium] perf pmus: broken CPUID matching in perf_pmus__read_from_env()
- [Medium] perf pmu: broken bulk and callchain probing in probe_sample_types()
- [Medium] perf pmu: fixed-counter ref-cycles in GP probe in perf_pmu__probe()
- [Medium] perf pmu: duplicate legacy XMM and SIMD probing in probe_pmu_regs_one()
- [Medium] perf pmu: unchecked CPU_SET() index in probe_group_limits()
--
commit 73f853dff74bd3f7177753b6ce1c952979dbfcbb
Author: Ian Rogers <[email protected]>
perf pmus: Add PMU environment loading and live capability probing
This commit adds helpers to reconstruct PMU lists and capabilities from perf_env headers, exposes duplicate uncore PMU scanning and mem-events formatting helpers, and adds perf_pmu__probe() to probe live PMU capabilities via perf_event_open().
Quoted 4 lines
tools/perf/util/pmu.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/pmu.c +0
-0@@ -2798,9 +2844,1935 @@ void perf_pmu__delete(struct perf_pmu *pmu)
[ ... ]
Quoted 18 lines
tools/perf/util/pmu.c +18
-0+static void probe_group_limits (const struct perf_event_attr *attrs,size_t nr_attrs, +pid_t pid,int cpu,unsigned long flags, +int *max_open,bool *open_capped, +int *max_active,bool *active_capped) +{ +struct perf_event_attr leader_attr; +int leader_fd; +int fds[MAX_PROBE_GROUP_SIZE ]; +int nr_followers =0 ; +bool has_time_read = true; +bool active_ok = true; +enum rlimit_action set_rlimit =NO_CHANGE ; +cpu_set_t saved_affinity, probe_affinity; +bool affinity_changed = false; + + *max_open =0 ; + *open_capped = false; + *max_active =0 ;
tools/perf/util/pmu.c void perf_pmu__delete(struct perf_pmu *pmu) · line 3135
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+ *active_capped = false; + +if (!attrs || nr_attrs ==0 ) +return ; + +if (pid ==0 && cpu >=0 ) { +CPU_ZERO (&saved_affinity); +if (sched_getaffinity (0 ,sizeof (saved_affinity), &saved_affinity) ==0 ) { +CPU_ZERO (&probe_affinity); +CPU_SET (cpu, &probe_affinity);
[Severity: Medium]
Can CPU_SET(cpu, &probe_affinity) write out of bounds on the stack here when cpu >= CPU_SETSIZE?
In perf_pmu__probe() -> probe_group_limits(), cpu comes from select_probe_cpu(pmu), which returns the first online CPU in pmu->cpus without bounding it to CPU_SETSIZE.
Unlike probe_mmap_caps() and probe_pmu_skid() later in this file, which both check pmu_cpu < CPU_SETSIZE before calling CPU_SET(), probe_group_limits() only checks cpu >= 0. On systems with CPU IDs >= 1024, this can write past probe_affinity on libcs without bounds checking (such as musl) or produce an empty affinity mask on glibc.
Quoted 4 lines
tools/perf/util/pmu.c +4
-0+if (sched_setaffinity (0 ,sizeof (probe_affinity), &probe_affinity) ==0 ) + affinity_changed = true; + } + }
[ ... ]
Quoted 54 lines
tools/perf/util/pmu.c +54
-0+static void probe_sample_types (struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags) +{ +u64 reg_bit =BIT_ULL (perf_arch_reg_ip (EM_HOST )) ?:1ULL ; +u64 bulk_mask =PERF_SAMPLE_IP |PERF_SAMPLE_TID |PERF_SAMPLE_TIME | +PERF_SAMPLE_ADDR |PERF_SAMPLE_READ |PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_ID |PERF_SAMPLE_CPU |PERF_SAMPLE_PERIOD | +PERF_SAMPLE_STREAM_ID |PERF_SAMPLE_RAW |PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER |PERF_SAMPLE_WEIGHT |PERF_SAMPLE_DATA_SRC | +PERF_SAMPLE_IDENTIFIER |PERF_SAMPLE_TRANSACTION | +PERF_SAMPLE_REGS_INTR |PERF_SAMPLE_AUX |PERF_SAMPLE_CGROUP | +PERF_SAMPLE_DATA_PAGE_SIZE |PERF_SAMPLE_CODE_PAGE_SIZE | +PERF_SAMPLE_WEIGHT_STRUCT ; +struct perf_event_attr b_attr = *base_attr; +int b_fd; + + b_attr.disabled =1 ; + b_attr.sample_period = base_attr->sample_period ?:10000 ; + b_attr.sample_type = bulk_mask; + b_attr.sample_regs_user = reg_bit; + b_attr.sample_regs_intr = reg_bit; + b_attr.sample_stack_user =64 ; + b_attr.inherit =0 ; + b_fd =sys_perf_event_open (&b_attr, pid, cpu,-1 , flags); +if (b_fd >=0 ) { +close (b_fd); + pmu->probed.sample_types |= bulk_mask; + } + +for (unsigned int shift =0 ; (1ULL << shift) <PERF_SAMPLE_MAX ; shift++) { +u64 bit =1ULL << shift; +struct perf_event_attr attr = *base_attr; +int fd; + +if (pmu->probed.sample_types & bit) +continue ; + + attr.disabled =1 ; + attr.sample_period = base_attr->sample_period ?:10000 ; + attr.sample_type = bit; + +switch (bit) { +case PERF_SAMPLE_BRANCH_STACK : + attr.branch_sample_type =PERF_SAMPLE_BRANCH_ANY | + (attr.exclude_kernel ?PERF_SAMPLE_BRANCH_USER :0 ); +break ; +case PERF_SAMPLE_REGS_USER : + attr.sample_regs_user = reg_bit; +break ; +case PERF_SAMPLE_REGS_INTR : + attr.sample_regs_intr = reg_bit; +break ; +case PERF_SAMPLE_STACK_USER :
tools/perf/util/pmu.c void perf_pmu__delete(struct perf_pmu *pmu) · line 3322
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+ attr.sample_stack_user =64 ; +break ; +case PERF_SAMPLE_READ : + attr.inherit =0 ; +break ; +default : +break ; + } + + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags);
[Severity: Medium]
There appear to be two issues in probe_sample_types():
First, bulk_mask sets both PERF_SAMPLE_WEIGHT and PERF_SAMPLE_WEIGHT_STRUCT (as well as PERF_SAMPLE_AUX without an AUX group leader). In kernel/events/core.c:perf_copy_attr():
if ((attr->sample_type & PERF_SAMPLE_WEIGHT) && (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) return -EINVAL;
Doesn't this cause the bulk sys_perf_event_open(&b_attr, ...) fast-path to always fail with -EINVAL?
Second, in the per-bit fallback loop, when base_attr->exclude_kernel == 1 (for example, when running unprivileged with perf_event_paranoid >= 1), probing PERF_SAMPLE_CALLCHAIN leaves attr.exclude_callchain_kernel == 0. In kernel/events/core.c:SYSCALL_DEFINE5(perf_event_open):
if (!attr.exclude_kernel ||
((attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
!attr.exclude_callchain_kernel)) {
err = perf_allow_kernel();
if (err)
return err;
}When perf_allow_kernel() denies kernel profiling, won't the kernel reject PERF_SAMPLE_CALLCHAIN with -EACCES and cause pmu->probed.can_callchain to be falsely reported as false unless attr.exclude_callchain_kernel = attr.exclude_kernel is set?
Quoted 4 lines
tools/perf/util/pmu.c +4
-0+if (fd <0 && bit ==PERF_SAMPLE_BRANCH_STACK ) { + attr.branch_sample_type =PERF_SAMPLE_BRANCH_ANY ; + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); + }
[ ... ]
Quoted 7 lines
tools/perf/util/pmu.c +7
-0+static void probe_pmu_regs_one (const struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags, +int max_precise,bool intr, +u64 *out_mask,int *out_abi, +u64 *out_simd_mask,u64 *out_pred_mask) +{
[ ... ]
Quoted 7 lines
tools/perf/util/pmu.c +7
-0+if (EM_HOST ==EM_X86_64 ||EM_HOST ==EM_386 ) { +int host_abi =0 ; +u64 host_mask = intr ?perf_intr_reg_mask (EM_HOST , &host_abi) : +perf_user_reg_mask (EM_HOST , &host_abi); + +if ((host_abi &PERF_SAMPLE_REGS_ABI_SIMD ) && (host_mask & ~gpr_mask)) { +u64 egpr_masks[] = {GENMASK_ULL (39 ,24 ),BIT_ULL (40 ) };
[ ... ]
tools/perf/util/pmu.c void perf_pmu__delete(struct perf_pmu *pmu) · line 3441
Quoted 13 lines
tools/perf/util/pmu.c +13
-0+if (fd >=0 ) { +close (fd); + mask |= egpr_masks[i]; + abi |=PERF_SAMPLE_REGS_ABI_SIMD ; + } + } + } + } + +if (!(abi &PERF_SAMPLE_REGS_ABI_SIMD )) { +u64 ext_mask =GENMASK_ULL (63 ,32 ) & ~gpr_mask; +struct perf_event_attr x_attr = *base_attr; +bool batch_ok = false;
[Severity: Medium]
On x86_64 systems that support the SIMD register sampling ABI (sample_simd_regs_enabled = 1) for XMM/YMM/ZMM registers but do not have APX or CET SSP, won't this probe both legacy PEBS XMM bits (32..47) in *out_mask and the XMM register class in *out_simd_mask?
In tools/perf/util/perf-regs-arch/perf_regs_x86.c:__perf_reg_mask_x86(), PERF_SAMPLE_REGS_ABI_SIMD is only set in host_abi when APX (R16-R31) or CET (SSP) is supported. Because the legacy bit 32..63 probe here checks !(abi & PERF_SAMPLE_REGS_ABI_SIMD) before probing the SIMD vector/predicate register classes (*out_simd_mask and *out_pred_mask) below, abi is still 0 when APX and SSP are absent, causing XMM registers to be recorded in both intr_regs_mask and intr_simd_mask.
Quoted 6 lines
tools/perf/util/pmu.c +6
-0+ + x_attr.disabled =1 ; + x_attr.sample_period = base_attr->sample_period ?:10000 ; + x_attr.sample_type = sample_type; +if (intr && max_precise >0 ) + x_attr.precise_ip =1 ;
[ ... ]
Quoted 2 lines
tools/perf/util/pmu.c +2
-0+void perf_pmu__probe (struct perf_pmu *pmu) +{
[ ... ]
Quoted 16 lines
tools/perf/util/pmu.c +16
-0+/* +* On core PMUs, cycles/instructions may use fixed counters in addition +* to general-purpose (GP) programmable counters. Probe a GP-only event +* group rotating through distinct GP events to determine the GP limit. +*/ +if (pmu->is_core) { +static const char *const gp_events[] = { +"branches" , +"branch-misses" , +"cache-misses" , +"cache-references" , +"bus-cycles" , +"stalled-cycles-frontend" , +"stalled-cycles-backend" , +"ref-cycles" , + };
tools/perf/util/pmu.c void perf_pmu__delete(struct perf_pmu *pmu) · line 4715
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+static const int gp_configs[] = { +PERF_COUNT_HW_BRANCH_INSTRUCTIONS , +PERF_COUNT_HW_BRANCH_MISSES , +PERF_COUNT_HW_CACHE_MISSES , +PERF_COUNT_HW_CACHE_REFERENCES , +PERF_COUNT_HW_BUS_CYCLES , +PERF_COUNT_HW_STALLED_CYCLES_FRONTEND , +PERF_COUNT_HW_STALLED_CYCLES_BACKEND , +PERF_COUNT_HW_REF_CPU_CYCLES , + };
[Severity: Medium]
Should ref-cycles / PERF_COUNT_HW_REF_CPU_CYCLES be excluded from the GP-only probe list in perf_pmu__probe()?
On Intel core PMUs (arch/x86/events/intel/core.c, FIXED_EVENT_CONSTRAINT(0x0300, 2)), both sysfs cpu/ref-cycles/ (event=0x00,umask=0x03) and PERF_COUNT_HW_REF_CPU_CYCLES map to pseudo-encoding 0x0300, which is constrained exclusively to Fixed Counter 2 rather than a general-purpose counter. Including it in gp_events[] and gp_configs[] causes Fixed Counter 2 to be allocated during the GP group probe, inflating max_gp_group_size and max_active_gp_group_size by 1 on Intel CPUs with >= 5 GP counters.
Quoted 2 lines
tools/perf/util/pmu.c +2
-0+struct perf_event_attr gp_attrs[MAX_PROBE_GROUP_SIZE ]; +size_t nr_gp_attrs =0 ;
[ ... ]
Quoted 4 lines
tools/perf/util/pmus.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/pmus.c +0
-0@@ -916,3 +940,198 @@ struct perf_pmu *perf_pmus__fake_pmu(void)
[ ... ]
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+static struct perf_pmu *env_pmu_find_or_add (const char *name,__u32 type, +struct perf_env *env, +const struct pmu_events_table *core_table) +{ +struct perf_pmu *pmu =pmu_find (name); + +if (pmu) { +if (pmu->type ==PERF_PMU_TYPE_FAKE && type !=PERF_PMU_TYPE_FAKE ) + pmu->type = type; +return pmu;
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 987
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+ } + + pmu =zalloc (sizeof (*pmu)); +if (!pmu) +return NULL ; + +if (perf_pmu__init (pmu, type, name) !=0 ) { +perf_pmu__delete (pmu); +return NULL ; + }
[Severity: High]
Can this trigger an out-of-bounds container_of() access and invalid free when HEADER_PMU_MAPPINGS contains hwmon or drm PMUs?
When HEADER_PMU_MAPPINGS is written by write_pmu_mappings() or populated by perf_env__read_pmu_mappings(), perf_pmus__scan() includes hwmon PMUs (type in PERF_PMU_TYPE_HWMON_START..PERF_PMU_TYPE_HWMON_END) and drm PMUs (type in PERF_PMU_TYPE_DRM_START..PERF_PMU_TYPE_DRM_END).
In perf_pmus__read_from_env() -> env_pmu_find_or_add(), pmu is allocated as a plain struct perf_pmu via zalloc(sizeof(*pmu)) with pmu->type set to the raw type from env->pmu_mappings. Because perf_pmu__is_hwmon() and perf_pmu__is_drm() only check whether pmu->type falls in those ranges, subsequent calls to perf_pmu__delete() (for example from perf_pmus__destroy()):
if (perf_pmu__is_hwmon(pmu)) hwmon_pmu__exit(pmu); else if (perf_pmu__is_drm(pmu)) drm_pmu__exit(pmu);
as well as perf_pmu__num_events(), perf_pmu__for_each_event(), and perf_pmu__have_event() will dispatch to hwmon_pmu__* / drm_pmu__*, which use container_of() to cast pmu to struct hwmon_pmu or struct drm_pmu and read or free fields past the end of the struct perf_pmu allocation.
Quoted 6 lines
tools/perf/util/pmus.c +6
-0+ + pmu->sysfs_aliases_loaded = true; + pmu->caps_initialized = true; + pmu->max_precise =-1 ; + pmu->is_core =env_is_pmu_core (name, env); + pmu->is_uncore =strstarts (name,"uncore_" );
[ ... ]
Quoted 19 lines
tools/perf/util/pmus.c +19
-0+int perf_pmus__read_from_env (struct perf_env *env) +{ +const struct pmu_events_table *core_table =NULL ; +struct perf_pmu *pmu; + +if (!env) +return -EINVAL ; + +perf_pmus__destroy (); + read_pmu_types =PERF_TOOL_PMU_TYPE_ALL_MASK ; + +if (env->cpuid) { +char cpuid_str[128 ]; +char vendor[32 ]; +unsigned int family, model, step; +const char *cpuid = env->cpuid; + +/* +* x86 HEADER_CPUID stores "vendor,family,model,step" in decimal
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 1057
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+* while pmu-events tables match "vendor-family-HEXMODEL-HEXSTEP". +*/ +if (sscanf (env->cpuid,"%31[^,],%u,%u,%u" , + vendor, &family, &model, &step) ==4 ) { +snprintf (cpuid_str,sizeof (cpuid_str),"%s-%u-%X-%X" , + vendor, family, model, step); + cpuid = cpuid_str; + } + core_table =find_core_events_table (perf_env__arch (env), cpuid); + }
[Severity: Medium]
Does this CPUID conversion break core_table lookup on s390 and on cross-arch analysis of x86 perf.data files?
On s390, get_cpuid() in tools/perf/arch/s390/util/header.c formats HEADER_CPUID as "manufacturer,type,model,version,authorization" (for example, "IBM,2964,702,3.5,002f"). Because the sscanf() call does not check that the architecture is x86 or that the string ends after the 4th field, s390 CPUIDs match all 4 specifiers and get rewritten to "IBM-2964-2BE-3", which fails to match the s390 regexes in tools/perf/pmu-events/arch/s390/mapfile.csv.
Additionally, when analyzing an x86 perf.data file on a non-x86 host, find_core_events_table() uses the generic strcmp_cpuid_str() in tools/perf/util/header.c:
if (match) {
size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
/* Verify the entire string matched. */
if (match_len == strlen(cpuid))
return 0;
}Since most x86 entries in tools/perf/pmu-events/arch/x86/mapfile.csv are 3-field patterns without stepping (such as GenuineIntel-6-CF), won't a 4-field "%s-%u-%X-%X" string fail the match_len == strlen(cpuid) check unless there is a 3-field "%s-%u-%X" fallback when find_core_events_table() returns NULL?
Quoted 3 lines
tools/perf/util/pmus.c +3
-0+ +/* 1. Populate from HEADER_PMU_MAPPINGS */ +if (env->nr_pmu_mappings >0 && env->pmu_mappings) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerRFC PATCH 2/2 perf sysinfo: Add sysinfo subcommand, documentation, and test
In reply to Ian RogersAdd the 'perf sysinfo' subcommand to display and probe PMUs and system performance monitoring capabilities on the running system or from a perf.data file.
For each PMU, 'perf sysinfo' reports static attributes (type, core/uncore/software/auxtrace/drm/hwmon/tool classification, CPUs, capabilities, sysfs formats, event counts, and memory profiling events) as well as probed runtime capabilities in text or JSON format, followed by a system-wide summary of core features, memory and PCIe/IO bus bandwidth monitoring, total events and metrics, pipeline metric drill-down levels, and an overall system rating.
--- tools/perf/Build | 1 + tools/perf/Documentation/perf-sysinfo.txt | 109 ++ tools/perf/Documentation/perf.txt | 2 +- tools/perf/builtin-help.c | 3 +- tools/perf/builtin-sysinfo.c | 1958 +++++++++++++++++++++ tools/perf/builtin.h | 1 + tools/perf/perf.c | 1 + tools/perf/tests/shell/sysinfo.sh | 71 + 8 files changed, 2144 insertions(+), 2 deletions(-) create mode 100644 tools/perf/Documentation/perf-sysinfo.txt create mode 100644 tools/perf/builtin-sysinfo.c create mode 100755 tools/perf/tests/shell/sysinfo.sh
tools/perf/Build +1 -0
@@ -13,6 +13,7 @@ perf-y += builtin-list.o perf-y += builtin-record.o perf-y += builtin-report.o perf-y += builtin-stat.o +perf-y += builtin-sysinfo.o perf-y += builtin-top.o perf-y += builtin-script.o perf-y += builtin-kvm.o
tools/perf/Documentation/perf-sysinfo.txt new file +109 -0
109 lines not on this page · raw message
tools/perf/Documentation/perf.txt +1 -1
@@ -92,5 +92,5 @@ linkperf:perf-help[1], linkperf:perf-inject[1], linkperf:perf-intel-pt[1], linkperf:perf-iostat[1], linkperf:perf-kallsyms[1], linkperf:perf-kmem[1], linkperf:perf-kvm[1], linkperf:perf-lock[1], linkperf:perf-mem[1], linkperf:perf-probe[1], linkperf:perf-sched[1], -linkperf:perf-script[1], linkperf:perf-test[1], +linkperf:perf-script[1], linkperf:perf-sysinfo[1], linkperf:perf-test[1], linkperf:perf-trace[1], linkperf:perf-version[1]
tools/perf/builtin-help.c +2 -1
@@ -329,6 +329,7 @@ void list_common_cmds_help(void) {"report", "Read perf.data (created by perf record) and display the profile"}, {"script", "Read perf.data (created by perf record) and display trace output"}, {"stat", "Run a command and gather performance counter statistics"}, + {"sysinfo", "Display and probe PMU and system performance monitoring information"}, {"test", "Runs sanity tests."}, {"top", "System profiling tool."}, {"version", "display the version of perf binary"},
@@ -485,7 +486,7 @@ int cmd_help(int argc, const char **argv) };const char *const builtin_help_subcommands[] = {"buildid-cache" ,"buildid-list" ,"diff" ,"evlist" ,"help" ,"list" , -"record" ,"report" ,"bench" ,"stat" ,"timechart" ,"top" ,"annotate" , +"record" ,"report" ,"bench" ,"stat" ,"sysinfo" ,"timechart" ,"top" ,"annotate" ,"script" ,"sched" ,"kallsyms" ,"kmem" ,"lock" ,"kvm" ,"test" ,"inject" ,"mem" ,"data" , #ifdef HAVE_LIBELF_SUPPORT "probe",
tools/perf/builtin-sysinfo.c new file +1958 -0
1,958 lines not on this page · raw message
tools/perf/builtin.h +1 -0
@@ -37,6 +37,7 @@ int cmd_list(int argc, const char **argv);int cmd_record (int argc,const char **argv);int cmd_report (int argc,const char **argv);int cmd_stat (int argc,const char **argv); +int cmd_sysinfo (int argc,const char **argv);int cmd_timechart (int argc,const char **argv);int cmd_top (int argc,const char **argv);int cmd_script (int argc,const char **argv);
tools/perf/perf.c +1 -0
@@ -112,6 +112,7 @@ static const struct cmd_struct commands[] = { { "report", cmd_report, 0 }, { "bench", cmd_bench, 0 }, { "stat", cmd_stat, 0 }, + { "sysinfo", cmd_sysinfo, 0 }, { "timechart", cmd_timechart, 0 }, { "top", cmd_top, 0 }, { "annotate", cmd_annotate, 0 },
tools/perf/tests/shell/sysinfo.sh new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v1 2/2] perf sysinfo: Add sysinfo subcommand, documentation, and test
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf sysinfo: broken perf.data metrics lookup in print_system_summary() - [Medium] perf sysinfo…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf sysinfo: broken perf.data metrics lookup in print_system_summary()
- [Medium] perf sysinfo: inaccurate mem_events detection in print_system_summary() --
commit eeafacdf44b45876a713b51b3eacc1d62e8dae1a
Author: Ian Rogers <[email protected]>
perf sysinfo: Add sysinfo subcommand, documentation, and test
This commit adds the perf sysinfo subcommand to display and probe PMU and system performance monitoring capabilities on a live system or from a perf.data file.
Quoted 6 lines
tools/perf/builtin-sysinfo.c new file +0
-0@@ -0,0 +1,1958 @@
[ ... ]
Quoted 4 lines
tools/perf/builtin-sysinfo.c new file +4
-0+static void print_system_summary (FILE *fp,const struct perf_session *session, +bool probed,bool leading_newline) +{ +const struct pmu_metrics_table *mtable;
[ ... ]
Quoted 40 lines
tools/perf/builtin-sysinfo.c new file +40
-0+while ((pmu =perf_pmus__scan_skip_duplicates (pmu)) !=NULL ) { +size_t pmu_events =perf_pmu__num_events (pmu); +size_t sysfs_ev = pmu->sysfs_aliases; +size_t json_ev = pmu_events > sysfs_ev ? pmu_events - sysfs_ev :0 ; + + total_pmus++; + total_events += pmu_events; + total_sysfs_events += sysfs_ev; + total_json_events += json_ev; + +if (pmu->is_core) { +struct perf_pmu_caps *br_cap; + +add_pmu_display_name (core_pmus, pmu); +perf_pmu__caps_parse (pmu); + br_cap =perf_pmu__get_cap (pmu,"branches" ); +if (br_cap && br_cap->value) { +int depth =atoi (br_cap->value); + +if (depth > max_br_depth) + max_br_depth = depth; + } +if (probed && pmu->probed.checked && pmu->probed.open_supported) { +if (pmu->probed.can_sample) + core_can_sample = true; +if (pmu->probed.max_precise >0 ) { + core_precise = true; +if (pmu->probed.max_precise > max_precise) + max_precise = pmu->probed.max_precise; + } +if (pmu->probed.can_user_rdpmc) + core_rdpmc = true; +if (pmu->probed.can_branch_stack) + core_branch_stack = true; +if (pmu->probed.mem_events_mask) + core_mem_events = true; +if (pmu->probed.can_leader_sampling) + core_leader_sampling = true; + }else { + core_can_sample = true;
tools/perf/builtin-sysinfo.c line 1592
Quoted 10 lines
tools/perf/builtin-sysinfo.c new file +10
-0+if (pmu->max_precise >0 ) { + core_precise = true; +if (pmu->max_precise > max_precise) + max_precise = pmu->max_precise; + } +if (max_br_depth >0 ) + core_branch_stack = true; +if (pmu->mem_events) + core_mem_events = true; + }
[Severity: Medium]
Can this misreport core_mem_events in print_system_summary()?
On AMD x86 and ARM64 systems, pmu->mem_events is attached to non-core PMUs (ibs_op and arm_spe_*, where pmu->is_core is false), while the core PMU has pmu->mem_events == NULL. Checking mem_events only inside the if (pmu->is_core) block causes the summary to always report memory profiling events as missing on those architectures.
Conversely, in the unprobed else branch, checking if (pmu->mem_events) without calling perf_pmu__mem_events_init_pmu(pmu) (unlike print_mem_events_static()) will set core_mem_events = true whenever perf_pmu__arch_init() assigns a static perf_mem_events array to a core PMU, even if none of the events (such as events/mem-loads or events/mem-stores) are actually supported in sysfs.
Quoted 8 lines
tools/perf/builtin-sysinfo.c new file +8
-0+ }else if (pmu->type ==PERF_TYPE_SOFTWARE ) { +if (!probed || !pmu->probed.checked || pmu->probed.can_sample) + has_sw_sampling = true; + } + +if (pmu->auxtrace) +add_pmu_display_name (aux_pmus, pmu); +if (is_mem_bw_pmu_name (pmu->name))
tools/perf/builtin-sysinfo.c line 1610
Quoted 10 lines
tools/perf/builtin-sysinfo.c new file +10
-0+add_pmu_display_name (mem_bw_pmus, pmu); +if (is_io_bw_pmu_name (pmu->name)) +add_pmu_display_name (io_bw_pmus, pmu); + } + +if (session && session->header.env.arch && session->header.env.cpuid) + mtable =find_core_metrics_table (session->header.env.arch, + session->header.env.cpuid); +else + mtable =pmu_metrics_table__find ();
[Severity: Medium]
Will find_core_metrics_table() fail to match the metrics table when reading from a perf.data file here?
session->header.env.arch contains the raw uts.machine string from HEADER_ARCH (such as "x86_64", "aarch64", or "ppc64le"), whereas pmu_events_map[].arch uses the normalized architecture name returned by perf_env__arch(&session->header.env) ("x86", "arm64", "powerpc", etc.).
In addition, on x86, session->header.env.cpuid stores "vendor,family,model,step" in decimal, whereas pmu_events_map and strcmp_cpuid_str() expect the "vendor-family-HEXMODEL-HEXSTEP" format. In perf_pmus__read_from_env() in tools/perf/util/pmus.c, both conversions are performed before calling find_core_events_table():
if (env->cpuid) {
char cpuid_str[128];
char vendor[32];
unsigned int family, model, step;
const char *cpuid = env->cpuid;
if (sscanf(env->cpuid, "%31[^,],%u,%u,%u",
vendor, &family, &model, &step) == 4) {
snprintf(cpuid_str, sizeof(cpuid_str), "%s-%u-%X-%X",
vendor, family, model, step);
cpuid = cpuid_str;
}
core_table = find_core_events_table(perf_env__arch(env), cpuid);
}Also, if session is non-NULL (-i perf.data) but either arch or cpuid is NULL in the header, falling into the else branch calls pmu_metrics_table__find(), which queries the live host CPU's metrics table instead of treating the recorded environment as having no core metrics table.
[ ... ]
tools/perf/builtin-sysinfo.c line 1926
Quoted 8 lines
tools/perf/builtin-sysinfo.c new file +8
-0+while ((pmu = show_all ?perf_pmus__scan (pmu) : +perf_pmus__scan_skip_duplicates (pmu)) !=NULL ) { +if (!pmu_matches_filters (pmu, argc, argv)) +continue ; + +if (do_probe && !input_name && + (show_pmus || pmu->is_core || pmu->type ==PERF_TYPE_SOFTWARE )) +perf_pmu__probe (pmu);
[Severity: Medium]
Related to the core_mem_events check in print_system_summary(), when show_pmus is false, should this condition also probe PMUs that have pmu->mem_events != NULL so that non-core memory profiling PMUs (such as ibs_op and arm_spe_*) are probed for the default summary output?
[ ... ]
Quoted 1 line
+}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerv2 cover perf sysinfo: Add subcommand to probe and summarize PMU capabilities
In reply to Ian RogersDetermining what performance monitoring hardware, driver capabilities, and event/metric support are available on a system currently requires piecing together sysfs files under /sys/bus/event_source/devices/, perf list output, and trial-and-error perf_event_open() calls. Across architectures and SoCs, support varies widely: an advanced system provides core PMUs with zero-skid precise event sampling (PEBS/IBS/SPE), hardware branch stack recording (LBR/BRBE), user-space counter reading (rdpmc), memory profiling (DATA_SRC), hardware AUX trace PMUs, memory and PCIe/IO interconnect bandwidth PMUs, and multi-level pipeline metric drill-down hierarchies, whereas a minimal system may only offer software timer sampling.
This RFC series introduces 'perf sysinfo' to inspect, actively probe, and summarize the PMU and performance monitoring capabilities of a live system or a recorded perf.data file:
- Patch 1 adds a from_env constructor parameter to perf_pmu__init() to distinguish PMUs reconstructed from perf_env headers from live host PMUs.
- Patch 2 adds perf_pmus__read_from_env() and perf_pmu__add_cap() to reconstruct core_pmus and other_pmus lists and their capabilities from perf.data headers.
- Patch 3 exports perf_pmus__scan_skip_duplicates() and adds perf_pmu__num_duplicates() to count numbered uncore PMU instances.
- Patch 4 exports perf_pmu__mem_events_init_pmu() and perf_pmu__mem_events_name() for per-PMU memory profiling events.
- Patch 5 adds struct perf_pmu::probed and perf_pmu__probe() to probe counting/sampling/branch_stack modes, precise_ip levels, sample format flags, privilege exclusion modifiers, execution features (leader sampling, software grouping, auto-counter reload, AUX pause/resume), and event group size limits.
- Patch 6 extends perf_pmu__probe() to probe user/interrupt sampling register masks (general-purpose and SIMD), memory profiling events, user-space rdpmc counter reading and PMC bit-width, and empirically measured branch instruction skid across precision levels.
- Patch 7 adds the 'perf sysinfo' subcommand. By default, 'perf sysinfo' outputs a colorized system capability summary, pipeline metric drill-down hierarchy (via <metric>_group chains), overall system rating (Advanced, Standard, Basic, Minimal), and any missing baseline features. Passing '-p'/'--pmus' (or a PMU glob filter, '-v'/'--verbose', '-a'/'--all', or '-j'/'--json') displays detailed per-PMU static and probed attributes.
- Patch 8 adds the 'perf-sysinfo' man page documentation.
- Patch 9 adds the 'perf sysinfo' shell test.
v2:
- Split the initial PMU environment, capability probing, and subcommand patches into 9 smaller, single-purpose patches.
- Disable the pager in 'perf sysinfo'.
- Fix ARM64 and RISC-V branch-range assembly errors in run_skid_branch_loop() by marking it noinline and reducing the .rept nop counts on ARM64 and RISC-V.
- Count tracepoint, drm, and hwmon events under sysfs rather than json in per-PMU and summary event totals.
- Replace glibc-specific ElfW() macro in get_self_uprobe_target() with standard <elf.h> Elf64_Ehdr/Elf64_Phdr and Elf32_Ehdr/Elf32_Phdr for musl libc compatibility.
Example default output on an Intel AlderLake hybrid system:
$ perf sysinfo
Summary:
Core PMUs (2, hybrid): cpu_atom, cpu_core
Core features:
Interrupt-based sampling: supported
Precise event sampling: supported (max_precise: 3 [3 - SAMPLE_IP must have 0 skid, or uses randomization])
Branch stack sampling (LBR/BRBE): supported (depth: 32 branches)
User-space counter reading (rdpmc): supported
Leader sampling: supported
Memory profiling events (DATA_SRC): supported
Hardware AUX trace PMUs: intel_bts, intel_pt
Bus & uncore monitoring:
Memory bandwidth PMUs: uncore_imc, uncore_imc_free_running
PCIe / IO interconnect bandwidth PMUs: uncore_arb
Events & metrics:
Available events: 3161 total (120 sysfs, 3041 json) across 28 PMUs
Available metrics: 438 metrics in 192 groups (advanced)
Metric drill-down levels (6): L1 (TopdownL1: 7 metrics), L2 (TopdownL2: 14 metrics), L3 (TopdownL3: 44 metrics), L4 (TopdownL4: 45 metrics), L5 (TopdownL5: 15 metrics), L6 (TopdownL6: 9 metrics)
System rating: Advanced (core PMU with precise sampling, branch stack (LBR), user rdpmc, bus PMUs, and metric drill-down)Example filtered per-PMU output:
$ perf sysinfo cpu_core
cpu_core (type: 4 [PERF_TYPE_RAW], core):
CPUs: 0-15 (16 CPUs)
Capabilities: branches=32, max_precise=3, pmu_name=alderlake_hybrid
Formats: event (config:0-7), pc (config:19), edge (config:18), offcore_rsp (config1:0-63), ldlat (config1:0-15), inv (config:23), umask (config:8-15), frontend (config1:0-23), cmask (config:24-31)
Events: 1563 total (20 sysfs, 1543 json)
Memory events: ldlat-loads ({cpu_core/mem-loads-aux/,cpu_core/mem-loads,ldlat=30/}:P), ldlat-stores (cpu_core/mem-stores/P)
Probed capabilities (event: cpu_core/cycles/):
Modes: counting, sampling, branch_stack
Precise sampling: :p (1 - SAMPLE_IP must have constant skid), :pp (2 - SAMPLE_IP requested to have 0 skid), :ppp (3 - SAMPLE_IP must have 0 skid, or uses randomization) (max_precise: 3)
Measured branch skid (instructions from branch):
level 0 (no :p): min 105, max 174, avg 156.8 (0/32 exact_ip)
level 1 (:p) : min 1, max 1, avg 1.0 (0/31 exact_ip)
level 2 (:pp) : min 0, max 0, avg 0.0 (31/31 exact_ip)
level 3 (:ppp) : min 0, max 0, avg 0.0 (32/32 exact_ip)
Sample types: ip, tid, time, addr, read, callchain, id, cpu, period, stream_id, raw, branch_stack, regs_user, stack_user, weight, data_src, identifier, transaction, regs_intr, aux, cgroup, data_page_size, code_page_size, weight_struct
User registers: AX, BX, CX, DX, SI, DI, BP, SP, IP, FLAGS, CS, SS, R8, R9, R10, R11, R12, R13, R14, R15
Intr registers: AX, BX, CX, DX, SI, DI, BP, SP, IP, FLAGS, CS, SS, R8, R9, R10, R11, R12, R13, R14, R15, XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15
Memory events (probed): ldlat-loads, ldlat-stores (sample types: addr, weight, data_src, transaction, data_page_size, weight_struct)
Exclusion modifiers: exclude_user, exclude_kernel, exclude_hv, exclude_idle, exclude_host, exclude_guest
Features: leader_sampling, sw_grouping, inherit, sample_freq, write_backward, callchain, data_src, weight, pinned, exclusive, format_lost, user_rdpmc, user_time, user_time_zero
User space counter reading (rdpmc): available, enabled, working (width: 48 bits)
Max event group size: 18 (active: 8)
Max GP event group size: 16 (active: 7)Example output on a Raspberry Pi 5 (ARM Cortex-A76):
$ sudo perf sysinfo armv8_cortex_a76
armv8_cortex_a76 (type: 9 [dynamic], core):
CPUs: 0-3 (4 CPUs)
Capabilities: slots=0x00000000, threshold_max=0x00000000, bus_slots=0x00000000, bus_width=0x00000000
Formats: threshold_count (config1:2), event (config:0-15), long (config1:0), rdpmc (config1:1), threshold (config1:5-16), threshold_compare (config1:3-4)
Events: 1336 total (40 sysfs, 1296 json)
Probed capabilities (event: armv8_cortex_a76/cycles/):
Modes: counting, sampling
Precise sampling: not supported (0 - SAMPLE_IP can have arbitrary skid)
Measured branch skid (instructions from branch):
level 0 (no :p): min 340, max 352, avg 346.2 (0/31 exact_ip)
Sample types: ip, tid, time, addr, read, callchain, id, cpu, period, stream_id, raw, regs_user, stack_user, weight, data_src, identifier, transaction, regs_intr, phys_addr, aux, cgroup, data_page_size, code_page_size, weight_struct
User registers: x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, lr, sp, pc
Intr registers: x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, lr, sp, pc
Exclusion modifiers: exclude_user, exclude_kernel, exclude_hv, exclude_host, exclude_guest
Features: leader_sampling, sw_grouping, inherit, sample_freq, write_backward, callchain, data_src, weight, pinned, exclusive, format_lost, user_time, user_time_zero, user_time_short
User space counter reading (rdpmc): available, disabled (sysfs: 0) (width: 32 bits)
Max event group size: 14 (active: 7)
Max GP event group size: 13 (active: 6) $ sudo perf sysinfo
Summary:
Core PMUs (1): armv8_cortex_a76
Core features:
Interrupt-based sampling: supported
Precise event sampling: missing (0 - SAMPLE_IP can have arbitrary skid)
Branch stack sampling (LBR/BRBE): missing
User-space counter reading (rdpmc): missing/disabled
Leader sampling: supported
Memory profiling events (DATA_SRC): missing
Hardware AUX trace PMUs: missing
Bus & uncore monitoring:
Memory bandwidth PMUs: missing
PCIe / IO interconnect bandwidth PMUs: missing
Events & metrics:
Available events: 3695 total (2371 sysfs, 1324 json) across 11 PMUs
Available metrics: 21 metrics in 5 groups (minimal)
Metric drill-down levels: none (flat metrics only)
System rating: Basic (core PMU with basic counting/sampling only; missing advanced features)
Missing baseline features: PCIe/IO interconnect bandwidth PMUs, hardware AUX trace PMU, hardware branch stack sampling (LBR/BRBE), memory bus bandwidth PMUs, memory load/store profiling events, multi-level pipeline drill-down metrics (<metric>_group), precise event sampling (PEBS/IBS/SPE), user-space counter reading (rdpmc)This patch series was generated with the assistance of antigravity and Gemini 4.0.
Ian Rogers (9):
perf pmu: Add from_env parameter to perf_pmu__init() perf pmus: Add perf_pmus__read_from_env()
perf pmus: Expose duplicate uncore PMU scanning helpers perf mem-events: Expose PMU init and name formatting helpers perf pmu: Add perf_pmu__probe() for live capability probing perf pmu: Probe registers, memory events, rdpmc, and branch skid perf sysinfo: Add sysinfo subcommand
perf docs: Add perf-sysinfo man page
perf test: Add perf sysinfo shell test
tools/perf/Build | 1 + tools/perf/Documentation/perf-sysinfo.txt | 109 ++ tools/perf/Documentation/perf.txt | 2 +- tools/perf/builtin-help.c | 3 +- tools/perf/builtin-sysinfo.c | 2010 ++++++++++++++++++++ tools/perf/builtin.h | 1 + tools/perf/perf.c | 1 + tools/perf/tests/pmu-events.c | 4 +- tools/perf/tests/shell/sysinfo.sh | 71 + tools/perf/util/drm_pmu.c | 2 +- tools/perf/util/hwmon_pmu.c | 2 +- tools/perf/util/mem-events.c | 14 +- tools/perf/util/mem-events.h | 3 + tools/perf/util/pmu.c | 2047 ++++++++++++++++++++- tools/perf/util/pmu.h | 139 +- tools/perf/util/pmus.c | 228 ++- tools/perf/util/pmus.h | 4 + tools/perf/util/tool_pmu.c | 2 +- 18 files changed, 4611 insertions(+), 32 deletions(-) create mode 100644 tools/perf/Documentation/perf-sysinfo.txt create mode 100644 tools/perf/builtin-sysinfo.c create mode 100755 tools/perf/tests/shell/sysinfo.sh
Signature · 1 line
2.56.0.212.g129ede20d9-goog
Ian Rogers
reviewerRFC PATCH v2 1/9 perf pmu: Add from_env parameter to perf_pmu__init()
Add a boolean from_env field to struct perf_pmu initialized via perf_pmu__init() to distinguish PMUs reconstructed from perf_env headers from live host PMUs. Guard tp_pmu, hwmon_pmu, and drm_pmu callbacks on !pmu->from_env so perf.data-derived PMUs do not access host tracepoints or cast plain struct perf_pmu allocations via container_of().
--- tools/perf/tests/pmu-events.c | 4 ++-- tools/perf/util/drm_pmu.c | 2 +- tools/perf/util/hwmon_pmu.c | 2 +- tools/perf/util/pmu.c | 33 ++++++++++++++++++--------------- tools/perf/util/pmu.h | 4 +++- tools/perf/util/tool_pmu.c | 2 +- 6 files changed, 26 insertions(+), 21 deletions(-)
tools/perf/tests/pmu-events.c +2 -2
@@ -514,7 +514,7 @@ static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)if (!pmu)return -1 ; -if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , pmu_name) !=0 ) { +if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , pmu_name,/*from_env=*/ false) !=0 ) {perf_pmu__delete (pmu);return -1 ; }
@@ -566,7 +566,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)if (!pmu)return -1 ; -if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , test_pmu->pmu_name) !=0 ) { +if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , test_pmu->pmu_name,/*from_env=*/ false) !=0 ) {perf_pmu__delete (pmu);return -1 ; }
tools/perf/util/drm_pmu.c +1 -1
@@ -116,7 +116,7 @@ static struct drm_pmu *add_drm_pmu(struct list_head *pmus, char *line, size_t liif (!drm)return NULL ; -if (perf_pmu__init (&drm->pmu, type, name) !=0 ) { +if (perf_pmu__init (&drm->pmu, type, name,/*from_env=*/ false) !=0 ) {perf_pmu__delete (&drm->pmu);return NULL ; }
tools/perf/util/hwmon_pmu.c +1 -1
@@ -365,7 +365,7 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, const char *hwmon_dir,if (!hwm)return NULL ; -if (perf_pmu__init (&hwm->pmu, type, buf) !=0 ) { +if (perf_pmu__init (&hwm->pmu, type, buf,/*from_env=*/ false) !=0 ) {perf_pmu__delete (&hwm->pmu);return NULL ; }
tools/perf/util/pmu.c +18 -15
@@ -1188,9 +1188,10 @@ static bool aliases__equal(long key1, long key2, void *ctx __maybe_unused)return strcasecmp ((const char *)key1, (const char *)key2) ==0 ; } -int perf_pmu__init (struct perf_pmu *pmu,__u32 type,const char *name) +int perf_pmu__init (struct perf_pmu *pmu,__u32 type,const char *name,bool from_env) { pmu->type = type; + pmu->from_env = from_env;INIT_LIST_HEAD (&pmu->format);INIT_LIST_HEAD (&pmu->caps);
@@ -1240,7 +1241,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charif (!pmu)return NULL ; -if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , name) !=0 ) { +if (perf_pmu__init (pmu,PERF_PMU_TYPE_FAKE , name,/*from_env=*/ false) !=0 ) {perf_pmu__delete (pmu);return NULL ; }
@@ -2118,11 +2119,11 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)if (perf_pmu__is_tool (pmu) &&tool_pmu__skip_event (name))return false;if (perf_pmu__is_tracepoint (pmu)) -return tp_pmu__have_event (pmu, name); +return pmu->from_env ? false :tp_pmu__have_event (pmu, name);if (perf_pmu__is_hwmon (pmu)) -return hwmon_pmu__have_event (pmu, name); +return pmu->from_env ? false :hwmon_pmu__have_event (pmu, name);if (perf_pmu__is_drm (pmu)) -return drm_pmu__have_event (pmu, name); +return pmu->from_env ? false :drm_pmu__have_event (pmu, name);if (perf_pmu__find_alias (pmu, name,/*load=*/ true) !=NULL )return true;if (pmu->cpu_aliases_added || (!pmu->events_table && !pmu->is_core))
@@ -2139,11 +2140,11 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)size_t nr;if (perf_pmu__is_tracepoint (pmu)) -return tp_pmu__num_events (pmu); +return pmu->from_env ?0 :tp_pmu__num_events (pmu);if (perf_pmu__is_hwmon (pmu)) -return hwmon_pmu__num_events (pmu); +return pmu->from_env ?0 :hwmon_pmu__num_events (pmu);if (perf_pmu__is_drm (pmu)) -return drm_pmu__num_events (pmu); +return pmu->from_env ?0 :drm_pmu__num_events (pmu);pmu_aliases_parse (pmu); nr = pmu->sysfs_aliases + pmu->sys_json_aliases;
@@ -2275,11 +2276,11 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,size_t size_rem;if (perf_pmu__is_tracepoint (pmu)) -return tp_pmu__for_each_event (pmu, state, cb); +return pmu->from_env ?0 :tp_pmu__for_each_event (pmu, state, cb);if (perf_pmu__is_hwmon (pmu)) -return hwmon_pmu__for_each_event (pmu, state, cb); +return pmu->from_env ?0 :hwmon_pmu__for_each_event (pmu, state, cb);if (perf_pmu__is_drm (pmu)) -return drm_pmu__for_each_event (pmu, state, cb); +return pmu->from_env ?0 :drm_pmu__for_each_event (pmu, state, cb);pmu_aliases_parse (pmu);pmu_add_cpu_aliases (pmu);
@@ -2784,10 +2785,12 @@ void perf_pmu__delete(struct perf_pmu *pmu)if (!pmu)return ; -if (perf_pmu__is_hwmon (pmu)) -hwmon_pmu__exit (pmu); -else if (perf_pmu__is_drm (pmu)) -drm_pmu__exit (pmu); +if (!pmu->from_env) { +if (perf_pmu__is_hwmon (pmu)) +hwmon_pmu__exit (pmu); +else if (perf_pmu__is_drm (pmu)) +drm_pmu__exit (pmu); + }perf_pmu__del_formats (&pmu->format);perf_pmu__del_aliases (pmu);
tools/perf/util/pmu.h +3 -1
@@ -161,6 +161,8 @@ struct perf_pmu {uint32_t cpu_common_json_aliases;/** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */ bool sysfs_aliases_loaded; +/** @from_env: Was the PMU reconstructed from a perf_env header? */ +bool from_env;/** * @cpu_aliases_added: Have all json events table entries for the PMU * been added?
@@ -345,7 +347,7 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size,int perf_pmu__event_source_devices_fd (void );int perf_pmu__pathname_fd (int dirfd,const char *pmu_name,const char *filename,int flags); -int perf_pmu__init (struct perf_pmu *pmu,__u32 type,const char *name); +int perf_pmu__init (struct perf_pmu *pmu,__u32 type,const char *name,bool from_env);struct perf_pmu *perf_pmu__lookup (struct list_head *pmus,int dirfd,const char *lookup_name,bool eager_load);struct perf_pmu *perf_pmu__create_placeholder_core_pmu (struct list_head *core_pmus);
tools/perf/util/tool_pmu.c +1 -1
@@ -714,7 +714,7 @@ struct perf_pmu *tool_pmu__new(void)if (!tool)return NULL ; -if (perf_pmu__init (tool,PERF_PMU_TYPE_TOOL ,"tool" ) !=0 ) { +if (perf_pmu__init (tool,PERF_PMU_TYPE_TOOL ,"tool" ,/*from_env=*/ false) !=0 ) {perf_pmu__delete (tool);return NULL ; }
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 1/9] perf pmu: Add from_env parameter to perf_pmu__init()
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines
Add a boolean
from_envfield to structperf_pmuinitialized viaperf_pmu__init()to distinguish PMUs reconstructed fromperf_envheaders from live host PMUs. Guardtp_pmu,hwmon_pmu, anddrm_pmucallbacks on !pmu->from_env so perf.data-derived PMUs do not access host tracepoints or cast plain structperf_pmuallocations viacontainer_of().Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerRFC PATCH v2 2/9 perf pmus: Add perf_pmus__read_from_env()
In reply to Ian RogersAdd perf_pmus__read_from_env() and perf_pmu__add_cap() to reconstruct core_pmus and other_pmus lists and their capabilities from HEADER_PMU_MAPPINGS, HEADER_HYBRID_TOPOLOGY, HEADER_CPU_PMU_CAPS, and HEADER_PMU_CAPS in a perf.data header environment.
--- tools/perf/util/pmu.c | 31 +++++++ tools/perf/util/pmu.h | 1 + tools/perf/util/pmus.c | 205 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/pmus.h | 2 + 4 files changed, 239 insertions(+)
tools/perf/util/pmu.c +31 -0
@@ -2565,6 +2565,37 @@ static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)return -ENOMEM ; } +int perf_pmu__add_cap (struct perf_pmu *pmu,const char *name,const char *value) +{ +struct perf_pmu_caps *caps; + +if (!pmu || !name || !value) +return -EINVAL ; +if (perf_pmu__get_cap (pmu, name)) +return 0 ; + + caps =zalloc (sizeof (*caps)); +if (!caps) +return -ENOMEM ; + + caps->name =strdup (name); +if (!caps->name) { +free (caps); +return -ENOMEM ; + } + caps->value =strdup (value); +if (!caps->value) { +zfree (&caps->name); +free (caps); +return -ENOMEM ; + } +list_add_tail (&caps->list, &pmu->caps); + pmu->nr_caps++; +if (!strcmp (name,"max_precise" )) + pmu->max_precise =atoi (value); +return 0 ; +} +static void perf_pmu__del_caps (struct perf_pmu *pmu) {struct perf_pmu_caps *caps, *tmp;
tools/perf/util/pmu.h +1 -0
@@ -331,6 +331,7 @@ bool pmu_uncore_identifier_match(const char *compat, const char *id);int perf_pmu__convert_scale (const char *scale,char **end,double *sval);struct perf_pmu_caps *perf_pmu__get_cap (struct perf_pmu *pmu,const char *name); +int perf_pmu__add_cap (struct perf_pmu *pmu,const char *name,const char *value);int perf_pmu__caps_parse (struct perf_pmu *pmu);
tools/perf/util/pmus.c +205 -0
221 lines not on this page · raw message
tools/perf/util/pmus.h +2 -0
@@ -6,6 +6,7 @@#include <stddef.h> struct perf_event_attr ; +struct perf_env ;struct perf_pmu ;struct print_callbacks ;
@@ -14,6 +15,7 @@ size_t pmu_name_len_no_suffix(const char *str);int pmu_name_cmp (const char *lhs_pmu_name,const char *rhs_pmu_name);void perf_pmus__destroy (void ); +int perf_pmus__read_from_env (struct perf_env *env);struct perf_pmu *perf_pmus__find (const char *name);struct perf_pmu *perf_pmus__find_by_type (unsigned int type);
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 2/9] perf pmus: Add perf_pmus__read_from_env()
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] perf pmus: stale core PMU cache and host probing in perf_pmus__read_from_env() - [Medium] perf…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] perf pmus: stale core PMU cache and host probing in perf_pmus__read_from_env()
- [Medium] perf pmu: out-of-bounds container_of() read on from_env drm and hwmon PMUs
- [Medium] perf pmus: broken x86 and cross-arch CPUID lookup in perf_pmus__read_from_env()
- [Medium] perf pmus: env_pmu_find_or_add() misclassifies non-'uncore_' uncore PMUs --
commit d0c91f9e30f93d2ae73777064d63c0d94f79c0b9
Author: Ian Rogers <[email protected]>
perf pmus: Add perf_pmus__read_from_env()
This commit adds perf_pmus__read_from_env() and perf_pmu__add_cap() to reconstruct core_pmus and other_pmus lists and their capabilities from a perf.data header environment.
Quoted 4 lines
tools/perf/util/pmus.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/pmus.c +0
-0@@ -916,3 +919,205 @@ struct perf_pmu *perf_pmus__fake_pmu(void)
[ ... ]
Quoted 12 lines
tools/perf/util/pmus.c +12
-0+static struct perf_pmu *env_pmu_find_or_add (const char *name,__u32 type, +struct perf_env *env, +const struct pmu_events_table *core_table) +{ +struct perf_pmu *pmu =pmu_find (name); + +if (pmu) { +if (pmu->type ==PERF_PMU_TYPE_FAKE && type !=PERF_PMU_TYPE_FAKE ) + pmu->type = type; +return pmu; + } +
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 968
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+ pmu =zalloc (sizeof (*pmu)); +if (!pmu) +return NULL ; + +if (perf_pmu__init (pmu, type, name,/*from_env=*/ true) !=0 ) { +perf_pmu__delete (pmu); +return NULL ; + } + + pmu->sysfs_aliases_loaded = true;
[Severity: Medium]
Can allocating a plain struct perf_pmu here trigger an out-of-bounds read via container_of() when type belongs to a DRM or HWMON PMU?
When HEADER_PMU_MAPPINGS contains drm_* or hwmon_* PMUs (with type in PERF_PMU_TYPE_DRM_START..PERF_PMU_TYPE_DRM_END or PERF_PMU_TYPE_HWMON_START..PERF_PMU_TYPE_HWMON_END), env_pmu_find_or_add() allocates only sizeof(struct perf_pmu) with pmu->from_env = true and pmu->sysfs_aliases_loaded = true.
Unlike perf_pmu__have_event() or perf_pmu__delete(), perf_pmu__config_terms() and perf_pmu__check_alias() in tools/perf/util/pmu.c do not check !pmu->from_env before dispatching on perf_pmu__is_drm() and perf_pmu__is_hwmon():
tools/perf/util/pmu.c:perf_pmu__config_terms() {
if (perf_pmu__is_hwmon(pmu))
return hwmon_pmu__config_terms(pmu, attr, terms, err);
if (perf_pmu__is_drm(pmu))
return drm_pmu__config_terms(pmu, attr, terms, err);
}tools/perf/util/pmu.c:perf_pmu__check_alias() {
if (perf_pmu__is_hwmon(pmu)) {
ret = hwmon_pmu__check_alias(head_terms, info, err);
goto out;
}
if (perf_pmu__is_drm(pmu)) {
ret = drm_pmu__check_alias(pmu, head_terms, info, err);
goto out;
}
}drm_pmu__check_alias() and drm_pmu__config_terms() cast pmu via container_of(pmu, struct drm_pmu, pmu) and read drm->num_events and drm->events past the end of struct perf_pmu. Similarly, hwmon_pmu__config_terms() casts pmu via container_of(pmu, struct hwmon_pmu, pmu), sees pmu->sysfs_aliases_loaded == true in hwmon_pmu__read_events(), and reads hwm->events out-of-bounds in hwmon_pmu__config_term().
Should perf_pmu__config_terms() and perf_pmu__check_alias() also guard the DRM and HWMON paths with !pmu->from_env?
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 977
Quoted 9 lines
tools/perf/util/pmus.c +9
-0+ pmu->caps_initialized = true; + pmu->max_precise =-1 ; + pmu->is_core =env_is_pmu_core (name, env); + pmu->is_uncore =strstarts (name,"uncore_" ); + +if (!strcmp (name,"software" ) || !strcmp (name,"tool" )) + pmu->events_table =perf_pmu__default_core_events_table (); +else if (core_table && (pmu->is_core || pmu->is_uncore)) + pmu->events_table = core_table;
[Severity: Medium]
Does checking strstarts(name, "uncore_") leave non-"uncore_" uncore PMUs without pmu->is_uncore and pmu->events_table?
Many uncore and package PMUs whose JSON events are compiled into core_table by jevents.py do not have names starting with "uncore_" in sysfs and HEADER_PMU_MAPPINGS, such as amd_l3, amd_df, and amd_umc_<N> on AMD x86, hisi_sccl<N>_ddrc<M> / hisi_sccl<N>_hha<M> / hisi_sccl<N>_l3c<M> on arm64, and pai_crypto / pai_ext on s390.
Because pmu->is_uncore is false for these PMUs, env_pmu_find_or_add() leaves pmu->events_table as NULL, and perf_pmu___name_match() in tools/perf/util/pmu.c takes the !pmu->is_uncore exact-match branch:
tools/perf/util/pmu.c:perf_pmu___name_match() {
...
if (!pmu->is_uncore) {
/*
* PMU isn't core or uncore, some kind of broken CPU mask
* situation. Only match exact name.
*/
return is_same;
}
return pmu->is_core ?
perf_pmu__match_ignoring_suffix(pmu_name, to_match) :
(wildcard_match ?
perf_pmu__match_wildcard_uncore(pmu_name, to_match) :
perf_pmu__match_ignoring_suffix_uncore(pmu_name, to_match));
}As a result, perf_pmu__num_events(), perf_pmu__for_each_event(), and perf_pmu__have_event() miss all JSON events for these uncore PMUs.
Quoted 4 lines
tools/perf/util/pmus.c +4
-0+ +list_add_tail (&pmu->list, pmu->is_core ? &core_pmus : &other_pmus); +return pmu; +}
[ ... ]
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 1017
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+int perf_pmus__read_from_env (struct perf_env *env) +{ +const struct pmu_events_table *core_table =NULL ; +struct perf_pmu *pmu; + +if (!env) +return -EINVAL ; + +perf_pmus__destroy (); + read_pmu_types =PERF_TOOL_PMU_TYPE_ALL_MASK ;
[Severity: Medium]
When perf_pmus__read_from_env() destroys and replaces core_pmus, how are the cached states in perf_pmus__num_core_pmus() and perf_pmus__supports_extended_type() handled?
First, perf_pmus__num_core_pmus() caches the core PMU count in a function-local static variable that is not reset by perf_pmus__destroy():
tools/perf/util/pmus.c:perf_pmus__num_core_pmus() {
static int count;
if (!count) {
struct perf_pmu *pmu = NULL;
while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
count++;
}
return count;
}If perf_pmus__num_core_pmus() is called before perf_pmus__read_from_env() (or after a later perf_pmus__destroy()), it returns the stale count to callers such as evsel__is_hybrid(), perf_pmu__auto_merge_stats(), perf_env__read_core_pmu_caps(), and hybrid_topology__new() that use it to detect hybrid topologies and size heap allocations before iterating perf_pmus__scan_core().
Second, perf_pmus__supports_extended_type() caches perf_pmus__do_support_extended_type via pthread_once, and __perf_pmus__supports_extended_type() probes the live host kernel via is_event_supported():
tools/perf/util/pmus.c:__perf_pmus__supports_extended_type() {
struct perf_pmu *pmu = NULL;
if (perf_pmus__num_core_pmus() <= 1)
return false;
while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
return false;
}
return true;
}When analyzing a hybrid perf.data on a non-hybrid or cross-architecture host (or when a hybrid PMU reconstructed from HEADER_HYBRID_TOPOLOGY has PERF_PMU_TYPE_FAKE), is_event_supported() fails against the host kernel. Consequently, perf_pmus__find_by_attr() and evsel__find_pmu() skip resolving attr->config >> PERF_PMU_TYPE_SHIFT and assign PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE events to the first core PMU.
Quoted 16 lines
tools/perf/util/pmus.c +16
-0+ +if (env->cpuid) { +const char *arch =perf_env__arch (env); +char cpuid_str[128 ]; +char vendor[32 ]; +unsigned int family, model, step; +char extra; + + core_table =find_core_events_table (arch, env->cpuid); +/* +* x86 HEADER_CPUID stores "vendor,family,model,step" in decimal +* while pmu-events tables match "vendor-family-HEXMODEL[-HEXSTEP]". +*/ +if (!core_table && !strcmp (arch,"x86" ) && +sscanf (env->cpuid,"%31[^,],%u,%u,%u%c" , + vendor, &family, &model, &step, &extra) ==4 ) {
tools/perf/util/pmus.c struct perf_pmu *perf_pmus__fake_pmu(void) · line 1043
Quoted 10 lines
tools/perf/util/pmus.c +10
-0+snprintf (cpuid_str,sizeof (cpuid_str),"%s-%u-%X-%X" , + vendor, family, model, step); + core_table =find_core_events_table (arch, cpuid_str); +if (!core_table) { +snprintf (cpuid_str,sizeof (cpuid_str),"%s-%u-%X" , + vendor, family, model); + core_table =find_core_events_table (arch, cpuid_str); + } + } + }
[Severity: Medium]
Does calling find_core_events_table(arch, env->cpuid) with the unformatted x86 env->cpuid emit error messages and misbehave across architectures?
On x86, HEADER_CPUID is recorded as comma-separated "vendor,family,model,step" with no '-' characters. Passing raw env->cpuid into find_core_events_table() calls x86's strcmp_cpuid_str() against every x86 entry in pmu_events_map. When it reaches 3-dash map entries such as GenuineIntel-6-55-[56789ABCDEF], full_mapcpuid is true while full_cpuid is false:
tools/perf/arch/x86/util/header.c:strcmp_cpuid_str() {
...
if (full_mapcpuid && !full_cpuid) {
pr_info("Invalid CPUID %s. Full CPUID is required, "
"vendor-family-model-stepping\n", id);
return 1;
}
}This prints "Invalid CPUID ... Full CPUID is required, vendor-family-model-stepping" to stderr on every x86 perf.data file before the sscanf() conversion runs, and the 3-field "%s-%u-%X" fallback triggers the same message again if the 4-field lookup does not find a match (while x86's strcmp_cpuid_str() already strips stepping when !full_mapcpuid && full_cpuid).
In addition, find_core_events_table() calls the host binary's strcmp_cpuid_str() rather than matching according to the target architecture in arch:
tools/perf/arch/arm64/util/header.c:strcmp_cpuid_str() {
u64 map_id = strtoull(mapcpuid, NULL, 16);
char map_id_variant = FIELD_GET(MIDR_VARIANT_MASK, map_id);
char map_id_revision = FIELD_GET(MIDR_REVISION_MASK, map_id);
u64 id = strtoull(idstr, NULL, 16);
...
}When an arm64 host binary inspects an x86 perf.data file, arm64's strcmp_cpuid_str() parses both non-hex x86 CPUID strings as 0 via strtoull() and falsely matches the first x86 table entry. Conversely, on a host using the default __weak strcmp_cpuid_str(), 4-field "%s-%u-%X-%X" strings do not strip the stepping to match 3-field x86 table entries.
[ ... ]
Quoted 1 line
+}
([ ... ])
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerRFC PATCH v2 3/9 perf pmus: Expose duplicate uncore PMU scanning helpers
In reply to Ian RogersExport perf_pmus__scan_skip_duplicates() and add perf_pmu__num_duplicates() to count numbered uncore PMU instances sharing a common prefix.
--- tools/perf/util/pmus.c | 23 ++++++++++++++++++++++- tools/perf/util/pmus.h | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-)
tools/perf/util/pmus.c +22 -1
@@ -455,7 +455,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const creturn NULL ; } -static struct perf_pmu *perf_pmus__scan_skip_duplicates (struct perf_pmu *pmu) +struct perf_pmu *perf_pmus__scan_skip_duplicates (struct perf_pmu *pmu) {bool use_core_pmus = !pmu || pmu->is_core;int last_pmu_name_len =0 ;
@@ -492,6 +492,27 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)return NULL ; } +size_t perf_pmu__num_duplicates (const struct perf_pmu *pmu) +{ +const struct perf_pmu *pos = pmu; +size_t base_len; +size_t count =1 ; + +if (!pmu || pmu->is_core) +return 1 ; + + base_len =pmu_name_len_no_suffix (pmu->name ?:"" ); +list_for_each_entry_continue (pos, &other_pmus, list) { +size_t pos_len =pmu_name_len_no_suffix (pos->name ?:"" ); + +if (pos_len != base_len || +strncmp (pmu->name ?:"" , pos->name ?:"" , base_len)) +break ; + count++; + } +return count; +} +struct perf_pmu *perf_pmus__scan_for_uncore_id (struct perf_pmu *pmu,const char *compat) {if (!pmu) {
tools/perf/util/pmus.h +2 -0
@@ -23,6 +23,8 @@ struct perf_pmu *perf_pmus__find_by_attr(const struct perf_event_attr *attr);struct perf_pmu *perf_pmus__scan (struct perf_pmu *pmu);struct perf_pmu *perf_pmus__scan_core (struct perf_pmu *pmu); +struct perf_pmu *perf_pmus__scan_skip_duplicates (struct perf_pmu *pmu); +size_t perf_pmu__num_duplicates (const struct perf_pmu *pmu);struct perf_pmu *perf_pmus__scan_for_event (struct perf_pmu *pmu,const char *event);struct perf_pmu *perf_pmus__scan_matching_wildcard (struct perf_pmu *pmu,const char *wildcard);struct perf_pmu *perf_pmus__scan_for_uncore_id (struct perf_pmu *pmu,const char *compat);
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 3/9] perf pmus: Expose duplicate uncore PMU scanning helpers
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Export
perf_pmus__scan_skip_duplicates()and addperf_pmu__num_duplicates()to count numbered uncore PMU instances sharing a common prefix.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerRFC PATCH v2 4/9 perf mem-events: Expose PMU init and name formatting helpers
In reply to Ian RogersExport perf_pmu__mem_events_init_pmu() and perf_pmu__mem_events_name() so callers can initialize and format per-PMU memory profiling events.
--- tools/perf/util/mem-events.c | 14 +++++++------- tools/perf/util/mem-events.h | 3 +++ 2 files changed, 10 insertions(+), 7 deletions(-)
tools/perf/util/mem-events.c +7 -7
@@ -79,12 +79,12 @@ int perf_pmu__mem_events_num_mem_pmus(struct perf_pmu *pmu)return num; } -static const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, -char *buf,size_t buf_size) +const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, +char *buf,size_t buf_size) {struct perf_mem_event *e; -if (i >=PERF_MEM_EVENTS__MAX || !pmu) +if (i >=PERF_MEM_EVENTS__MAX || !pmu || !pmu->mem_events)return NULL ; e = &pmu->mem_events[i];
@@ -193,13 +193,13 @@ static bool perf_pmu__mem_events_supported(const char *mnt, struct perf_pmu *pmureturn !stat (path, &st); } -static int __perf_pmu__mem_events_init (struct perf_pmu *pmu) +int perf_pmu__mem_events_init_pmu (struct perf_pmu *pmu) {const char *mnt =sysfs__mount ();bool found = false;int j; -if (!mnt) +if (!mnt || !pmu || pmu->from_env || !pmu->mem_events)return -ENOENT ;for (j =0 ; j <PERF_MEM_EVENTS__MAX ; j++) {
@@ -209,7 +209,7 @@ static int __perf_pmu__mem_events_init(struct perf_pmu *pmu)* If the event entry isn't valid, skip initialization * and "e->supported" will keep false. */ -if (!e->tag) +if (!e || !e->tag)continue ; e->supported |=perf_pmu__mem_events_supported (mnt, pmu, e);
@@ -225,7 +225,7 @@ int perf_pmu__mem_events_init(void)struct perf_pmu *pmu =NULL ;while ((pmu =perf_pmus__scan_mem (pmu)) !=NULL ) { -if (__perf_pmu__mem_events_init (pmu)) +if (perf_pmu__mem_events_init_pmu (pmu))return -ENOENT ; }
tools/perf/util/mem-events.h +3 -0
@@ -31,7 +31,10 @@ extern struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX];extern bool perf_mem_record[PERF_MEM_EVENTS__MAX ];int perf_pmu__mem_events_parse (struct perf_pmu *pmu,const char *str); +int perf_pmu__mem_events_init_pmu (struct perf_pmu *pmu);int perf_pmu__mem_events_init (void ); +const char *perf_pmu__mem_events_name (struct perf_pmu *pmu,int i, +char *buf,size_t buf_size);struct perf_mem_event *perf_pmu__mem_events_ptr (struct perf_pmu *pmu,int i);struct perf_pmu *perf_mem_events_find_pmu (void );
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 4/9] perf mem-events: Expose PMU init and name formatting helpers
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines
Export
perf_pmu__mem_events_init_pmu()andperf_pmu__mem_events_name()so callers can initialize and format per-PMU memory profiling events.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerRFC PATCH v2 5/9 perf pmu: Add perf_pmu__probe() for live capability probing
In reply to Ian RogersAdd struct perf_pmu::probed and perf_pmu__probe() to actively probe live PMU capabilities via perf_event_open(), including counting/sampling/branch_stack modes, precise_ip levels, supported perf_event_sample_format flags, privilege exclusion modifiers, execution features (leader sampling, software grouping, auto-counter reload, AUX pause/resume/sample), and event group size limits.
--- tools/perf/util/pmu.c | 1225 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/pmu.h | 134 ++++- 2 files changed, 1358 insertions(+), 1 deletion(-)
tools/perf/util/pmu.c +1225 -0
@@ -9,6 +9,7 @@#include <sys/stat.h> #include <unistd.h> #include <errno.h> +#include <limits.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h>
@@ -38,7 +39,21 @@#include "strbuf.h" #include "fncache.h" #include "util/evsel_config.h" +#include "cloexec.h" +#include "dwarf-regs.h" +#include "evlist.h" +#include "mem-events.h" +#include "mmap.h" +#include "perf_regs.h" +#include "rlimit.h" +#include "util/sample.h" +#include "../perf-sys.h" +#include <linux/hw_breakpoint.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sched.h> #include <regex.h> +#include <elf.h> #define UNIT_MAX_LEN 31/* max length for event unit name */
@@ -2832,9 +2847,1219 @@ void perf_pmu__delete(struct perf_pmu *pmu)zfree (&pmu->name);zfree (&pmu->alias_name);zfree (&pmu->id); +zfree (&pmu->probed.probe_event);free (pmu); } +#define MAX_PROBE_GROUP_SIZE 64 +#define MAX_PROBE_CANDIDATES 16 + +static unsigned long dummy_bp_var; + +static bool get_self_uprobe_target (char *exe_path,__u64 *out_offset) +{ +#if __SIZEOF_POINTER__ ==8 +Elf64_Ehdr ehdr; +#else +Elf32_Ehdr ehdr; +#endif +int fd; +bool found = false; + +if (!realpath ("/proc/self/exe" , exe_path)) +return false; + + fd =open (exe_path,O_RDONLY |O_CLOEXEC ); +if (fd <0 ) +return false; + +if (pread (fd, &ehdr,sizeof (ehdr),0 ) !=sizeof (ehdr) || +memcmp (ehdr.e_ident,ELFMAG ,SELFMAG ) !=0 ) { +close (fd); +return false; + } + +for (unsigned int i =0 ; i < ehdr.e_phnum; i++) { +#if __SIZEOF_POINTER__ ==8 +Elf64_Phdr phdr; +#else +Elf32_Phdr phdr; +#endif +off_t pos = (off_t )ehdr.e_phoff + (off_t )i * ehdr.e_phentsize; + +if (ehdr.e_phentsize <sizeof (phdr)) +break ; + +if (pread (fd, &phdr,sizeof (phdr), pos) !=sizeof (phdr)) +break ; +if (phdr.p_type !=PT_LOAD || !(phdr.p_flags &PF_X )) +continue ; +if (ehdr.e_entry >= phdr.p_vaddr && + ehdr.e_entry < phdr.p_vaddr + phdr.p_filesz) { + *out_offset = phdr.p_offset + (ehdr.e_entry - phdr.p_vaddr); + found = true; +break ; + } + } + +close (fd); +return found; +} + +struct probe_candidates { +char *names[MAX_PROBE_CANDIDATES ]; +size_t nr; +}; + +static void probe_candidates__add (struct probe_candidates *cands,const char *str) +{ +if (cands->nr >=MAX_PROBE_CANDIDATES || !str) +return ; +for (size_t i =0 ; i < cands->nr; i++) { +if (!strcmp (cands->names[i], str)) +return ; + } + cands->names[cands->nr] =strdup (str); +if (cands->names[cands->nr]) + cands->nr++; +} + +static void probe_candidates__exit (struct probe_candidates *cands) +{ +for (size_t i =0 ; i < cands->nr; i++) +zfree (&cands->names[i]); + cands->nr =0 ; +} + +static int collect_probe_candidate_cb (void *vstate,struct pmu_event_info *info) +{ +struct probe_candidates *cands = vstate; +char buf[512 ]; + +if (info->deprecated) +return 0 ; +if (info->str &&strstr (info->str,"=?" )) +return 0 ; +if (!info->name) +return 0 ; + +if (info->pmu && info->pmu->type ==PERF_TYPE_TRACEPOINT ) { +probe_candidates__add (cands, info->name); +return 1 ; + }else if (strchr (info->name,'/' )) { +probe_candidates__add (cands, info->name); + }else if (info->pmu && info->pmu->name) { +snprintf (buf,sizeof (buf),"%s/%s/" , info->pmu->name, info->name); +probe_candidates__add (cands, buf); + }else { +probe_candidates__add (cands, info->name); + } + +return cands->nr >=MAX_PROBE_CANDIDATES ?1 :0 ; +} + +static bool parse_candidate_attr (const char *event_str,struct perf_event_attr *attr) +{ +struct evlist *evlist; +struct parse_events_error err; +struct evsel *evsel; +int ret; + + evlist =evlist__new (); +if (!evlist) +return false; + +parse_events_error__init (&err); + ret =__parse_events (evlist, event_str,/*pmu_filter=*/ NULL , +/*cputype_filter=*/ false, &err,/*fake_pmu=*/ false, +/*warn_if_reordered=*/ false,/*fake_tp=*/ false); +parse_events_error__exit (&err); +if (ret ||evlist__empty (evlist)) { +evlist__put (evlist); +return false; + } + + evsel =evlist__first (evlist); + *attr = evsel->core.attr; +evlist__put (evlist); +return true; +} + +static int select_probe_cpu (const struct perf_pmu *pmu) +{ +struct perf_cpu_map *online =perf_cpu_map__new_online_cpus (); +int cpu =0 ; + +if (pmu->cpus && !perf_cpu_map__is_empty (pmu->cpus)) { +int nr =perf_cpu_map__nr (pmu->cpus); + + cpu =perf_cpu_map__cpu (pmu->cpus,0 ).cpu; +if (online) { +for (int i =0 ; i < nr; i++) { +struct perf_cpu c =perf_cpu_map__cpu (pmu->cpus, i); + +if (perf_cpu_map__has (online, c)) { + cpu = c.cpu; +break ; + } + } + } + }else if (online && !perf_cpu_map__is_empty (online)) { + cpu =perf_cpu_map__cpu (online,0 ).cpu; + } + +perf_cpu_map__put (online); +return cpu >=0 ? cpu :0 ; +} + +static bool try_open_attr_variants (struct perf_event_attr *attr, +bool is_uncore,int pmu_cpu, +unsigned long flags, +pid_t *out_pid,int *out_cpu, +int *last_err) +{ +pid_t pids[2 ]; +size_t nr_pids =2 ; + +if (is_uncore) { + pids[0 ] =-1 ; + pids[1 ] =0 ; + }else { + pids[0 ] =0 ; + pids[1 ] =-1 ; + } + +for (size_t p =0 ; p < nr_pids; p++) { +pid_t pid = pids[p]; + +for (int samp =0 ; samp <2 ; samp++) { +for (int priv =0 ; priv <4 ; priv++) { +struct perf_event_attr try_attr = *attr; +int fd; + + try_attr.size =sizeof (try_attr); + try_attr.disabled =1 ; + try_attr.read_format =0 ; +if (samp ==0 ) { + try_attr.sample_period =0 ; + try_attr.freq =0 ; + try_attr.sample_type =0 ; + }else { + try_attr.sample_period =10000 ; + try_attr.freq =0 ; + try_attr.sample_type =PERF_SAMPLE_IP ; + } + +if (priv >=1 ) + try_attr.exclude_kernel =1 ; +if (priv >=2 ) + try_attr.exclude_hv =1 ; +if (priv >=3 ) + try_attr.exclude_guest =1 ; + + fd =sys_perf_event_open (&try_attr, pid, pmu_cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + *attr = try_attr; + *out_pid = pid; + *out_cpu = pmu_cpu; +return true; + } +if (*last_err ==0 || *last_err ==ENOENT || *last_err ==EINVAL ) + *last_err = errno; + } + } + } +return false; +} + +static bool check_group_active (int leader_fd,int last_fd) +{ +u64 before[3 ] = {0 ,0 ,0 }; +u64 after[3 ] = {0 ,0 ,0 }; +u64 f_before[3 ] = {0 ,0 ,0 }; +u64 f_after[3 ] = {0 ,0 ,0 }; +u64 delta_ena, delta_run; +bool check_follower = false; + +if (read (leader_fd, before,sizeof (before)) !=sizeof (before)) +return false; + +if (last_fd >=0 &&read (last_fd, f_before,sizeof (f_before)) ==sizeof (f_before)) + check_follower = true; + +if (ioctl (leader_fd,PERF_EVENT_IOC_ENABLE ,PERF_IOC_FLAG_GROUP ) <0 ) +return false; + +for (int spin =0 ; spin <20000 ; spin++) +barrier (); + +ioctl (leader_fd,PERF_EVENT_IOC_DISABLE ,PERF_IOC_FLAG_GROUP ); + +if (read (leader_fd, after,sizeof (after)) !=sizeof (after)) +return false; + +if (check_follower &&read (last_fd, f_after,sizeof (f_after)) !=sizeof (f_after)) +return false; + + delta_ena = after[1 ] - before[1 ]; + delta_run = after[2 ] - before[2 ]; +if (delta_ena ==0 || delta_run != delta_ena) +return false; + +if (check_follower) { +u64 f_delta_ena = f_after[1 ] - f_before[1 ]; +u64 f_delta_run = f_after[2 ] - f_before[2 ]; + +if (f_delta_ena ==0 || f_delta_run != f_delta_ena) +return false; + } + +return true; +} + +static void probe_group_limits (const struct perf_event_attr *attrs,size_t nr_attrs, +pid_t pid,int cpu,unsigned long flags, +int *max_open,bool *open_capped, +int *max_active,bool *active_capped) +{ +struct perf_event_attr leader_attr; +int leader_fd; +int fds[MAX_PROBE_GROUP_SIZE ]; +int nr_followers =0 ; +bool has_time_read = true; +bool active_ok = true; +enum rlimit_action set_rlimit =NO_CHANGE ; +cpu_set_t saved_affinity, probe_affinity; +bool affinity_changed = false; + + *max_open =0 ; + *open_capped = false; + *max_active =0 ; + *active_capped = false; + +if (!attrs || nr_attrs ==0 ) +return ; + +if (pid ==0 && cpu >=0 && cpu <CPU_SETSIZE ) { +CPU_ZERO (&saved_affinity); +if (sched_getaffinity (0 ,sizeof (saved_affinity), &saved_affinity) ==0 ) { +CPU_ZERO (&probe_affinity); +CPU_SET (cpu, &probe_affinity); +if (sched_setaffinity (0 ,sizeof (probe_affinity), &probe_affinity) ==0 ) + affinity_changed = true; + } + } + + leader_attr = attrs[0 ]; + leader_attr.disabled =1 ; + leader_attr.read_format =PERF_FORMAT_TOTAL_TIME_ENABLED |PERF_FORMAT_TOTAL_TIME_RUNNING ; + + leader_fd =sys_perf_event_open (&leader_attr, pid, cpu,-1 , flags); +if (leader_fd <0 ) { + leader_attr.read_format =0 ; + has_time_read = false; + leader_fd =sys_perf_event_open (&leader_attr, pid, cpu,-1 , flags); + } +if (leader_fd <0 ) + goto out_affinity; + + *max_open =1 ; +if (has_time_read &&check_group_active (leader_fd,-1 )) + *max_active =1 ; +else + active_ok = false; + +for (int count =2 ; count <=MAX_PROBE_GROUP_SIZE ; count++) { +struct perf_event_attr follower_attr = attrs[(count -1 ) % nr_attrs]; +int fd; + + follower_attr.disabled =0 ; + follower_attr.read_format = leader_attr.read_format; + + fd =sys_perf_event_open (&follower_attr, pid, cpu, leader_fd, flags); +if (fd <0 && errno ==EMFILE &&rlimit__increase_nofile (&set_rlimit)) + fd =sys_perf_event_open (&follower_attr, pid, cpu, leader_fd, flags); +if (fd <0 && follower_attr.read_format !=0 ) { + follower_attr.read_format =0 ; + fd =sys_perf_event_open (&follower_attr, pid, cpu, leader_fd, flags); + } +if (fd <0 ) +break ; + + fds[nr_followers++] = fd; + *max_open = count; +if (active_ok && has_time_read) { +if (check_group_active (leader_fd, fd)) + *max_active = count; +else + active_ok = false; + } + } + +if (*max_open >=MAX_PROBE_GROUP_SIZE ) + *open_capped = true; +if (*max_active >=MAX_PROBE_GROUP_SIZE ) + *active_capped = true; + +for (int i =0 ; i < nr_followers; i++) +close (fds[i]); +close (leader_fd); + +out_affinity: +if (affinity_changed) +sched_setaffinity (0 ,sizeof (saved_affinity), &saved_affinity); +} + +enum probe_exclude_flag { +PROBE_EXCLUDE_USER , +PROBE_EXCLUDE_KERNEL , +PROBE_EXCLUDE_HV , +PROBE_EXCLUDE_IDLE , +PROBE_EXCLUDE_HOST , +PROBE_EXCLUDE_GUEST , +}; + +static bool test_exclude_flag (const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags, +enum probe_exclude_flag which) +{ +struct perf_event_attr attr = *base_attr; +int fd; + +switch (which) { +case PROBE_EXCLUDE_USER : + attr.exclude_user =1 ; +break ; +case PROBE_EXCLUDE_KERNEL : + attr.exclude_kernel =1 ; + attr.exclude_user =0 ; +break ; +case PROBE_EXCLUDE_HV : + attr.exclude_hv =1 ; +break ; +case PROBE_EXCLUDE_IDLE : + attr.exclude_idle =1 ; +break ; +case PROBE_EXCLUDE_HOST : + attr.exclude_host =1 ; + attr.exclude_guest =0 ; +break ; +case PROBE_EXCLUDE_GUEST : + attr.exclude_guest =1 ; + attr.exclude_host =0 ; +break ; +default : +return false; + } + + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); +return true; + } +if (which ==PROBE_EXCLUDE_USER && attr.exclude_kernel ==1 ) { + attr.exclude_kernel =0 ; + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); +return true; + } + } +return false; +} + +static void probe_sample_types (struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags) +{ +u64 reg_bit =BIT_ULL (perf_arch_reg_ip (EM_HOST )) ?:1ULL ; +u64 bulk_mask =PERF_SAMPLE_IP |PERF_SAMPLE_TID |PERF_SAMPLE_TIME | +PERF_SAMPLE_ADDR |PERF_SAMPLE_READ |PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_ID |PERF_SAMPLE_CPU |PERF_SAMPLE_PERIOD | +PERF_SAMPLE_STREAM_ID |PERF_SAMPLE_RAW |PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER |PERF_SAMPLE_WEIGHT |PERF_SAMPLE_DATA_SRC | +PERF_SAMPLE_IDENTIFIER |PERF_SAMPLE_TRANSACTION | +PERF_SAMPLE_REGS_INTR |PERF_SAMPLE_CGROUP | +PERF_SAMPLE_DATA_PAGE_SIZE |PERF_SAMPLE_CODE_PAGE_SIZE ; +struct perf_event_attr b_attr = *base_attr; +int b_fd; + + b_attr.disabled =1 ; + b_attr.sample_period = base_attr->sample_period ?:10000 ; + b_attr.sample_type = bulk_mask; + b_attr.sample_regs_user = reg_bit; + b_attr.sample_regs_intr = reg_bit; + b_attr.sample_stack_user =64 ; + b_attr.exclude_callchain_kernel = b_attr.exclude_kernel; + b_attr.inherit =0 ; + b_fd =sys_perf_event_open (&b_attr, pid, cpu,-1 , flags); +if (b_fd >=0 ) { +close (b_fd); + pmu->probed.sample_types |= bulk_mask; + } + +for (unsigned int shift =0 ; (1ULL << shift) <PERF_SAMPLE_MAX ; shift++) { +u64 bit =1ULL << shift; +struct perf_event_attr attr = *base_attr; +int fd; + +if (pmu->probed.sample_types & bit) +continue ; + + attr.disabled =1 ; + attr.sample_period = base_attr->sample_period ?:10000 ; + attr.sample_type = bit; + +switch (bit) { +case PERF_SAMPLE_BRANCH_STACK : + attr.branch_sample_type =PERF_SAMPLE_BRANCH_ANY | + (attr.exclude_kernel ?PERF_SAMPLE_BRANCH_USER :0 ); +break ; +case PERF_SAMPLE_CALLCHAIN : + attr.exclude_callchain_kernel = attr.exclude_kernel; +break ; +case PERF_SAMPLE_REGS_USER : + attr.sample_regs_user = reg_bit; +break ; +case PERF_SAMPLE_REGS_INTR : + attr.sample_regs_intr = reg_bit; +break ; +case PERF_SAMPLE_STACK_USER : + attr.sample_stack_user =64 ; +break ; +case PERF_SAMPLE_READ : + attr.inherit =0 ; +break ; +default : +break ; + } + + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); +if (fd <0 && bit ==PERF_SAMPLE_BRANCH_STACK ) { + attr.branch_sample_type =PERF_SAMPLE_BRANCH_ANY ; + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); + } +if (fd <0 && bit !=PERF_SAMPLE_IP ) { + attr.sample_type =PERF_SAMPLE_IP | bit; + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); + } +if (fd >=0 ) { +close (fd); + pmu->probed.sample_types |= bit; + } + } + + pmu->probed.can_branch_stack = !!(pmu->probed.sample_types &PERF_SAMPLE_BRANCH_STACK ); + pmu->probed.can_callchain = !!(pmu->probed.sample_types &PERF_SAMPLE_CALLCHAIN ); + pmu->probed.can_data_src = !!(pmu->probed.sample_types &PERF_SAMPLE_DATA_SRC ); + pmu->probed.can_weight = !!(pmu->probed.sample_types &PERF_SAMPLE_WEIGHT ); +} + +void perf_pmu__probe (struct perf_pmu *pmu) +{ +struct probe_candidates cands = { .nr =0 }; +struct perf_event_attr base_attr; +struct perf_event_attr group_attrs[MAX_PROBE_GROUP_SIZE ]; +char uprobe_exe_path[PATH_MAX ]; +__u64 uprobe_offset =0 ; +size_t nr_group_attrs =0 ; +unsigned long flags =perf_event_open_cloexec_flag (); +int keepalive_fd =-1 ; +int pmu_cpu; +pid_t pid =0 ; +int cpu =0 ; +int last_err =0 ; +bool found = false; + +if (!pmu || pmu->from_env || pmu->probed.checked) +return ; +if (perf_pmu__kind (pmu) !=PERF_PMU_KIND_PE ) +return ; + + pmu_cpu =select_probe_cpu (pmu); + +if (pmu->type ==PERF_TYPE_TRACEPOINT ) { +static const struct { +const char *sys; +const char *name; + } fast_tps[] = { + {"sched" ,"sched_switch" }, + {"raw_syscalls" ,"sys_enter" }, + {"irq" ,"irq_handler_entry" }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (fast_tps); i++) { +int tp_id =tp_pmu__id (fast_tps[i].sys, fast_tps[i].name); + +if (tp_id >=0 ) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type =PERF_TYPE_TRACEPOINT ; + base_attr.config = tp_id; +if (try_open_attr_variants (&base_attr, false, pmu_cpu, + flags, &pid, &cpu, &last_err)) { +char tp_buf[128 ]; + +snprintf (tp_buf,sizeof (tp_buf),"%s:%s" , + fast_tps[i].sys, fast_tps[i].name); + pmu->probed.probe_event =strdup (tp_buf); + found = true; +break ; + } + } + } +if (!found) { +perf_pmu__for_each_event (pmu,/*skip_duplicate_pmus=*/ false, + &cands, collect_probe_candidate_cb); + } + }else if (pmu->name && !strcmp (pmu->name,"kprobe" )) { +static const char *const kprobe_funcs[] = { +"vfs_read" , +"schedule" , +"do_sys_openat2" , + }; + +for (size_t i =0 ; i <ARRAY_SIZE (kprobe_funcs); i++) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type = pmu->type; + base_attr.size =sizeof (base_attr); + base_attr.kprobe_func = (uintptr_t )kprobe_funcs[i]; + base_attr.probe_offset =0 ; +if (try_open_attr_variants (&base_attr, false, pmu_cpu, + flags, &pid, &cpu, &last_err)) { +char kp_buf[128 ]; + +snprintf (kp_buf,sizeof (kp_buf),"kprobe:%s" , + kprobe_funcs[i]); + pmu->probed.probe_event =strdup (kp_buf); + found = true; +break ; + } + } + }else if (pmu->name && !strcmp (pmu->name,"uprobe" )) { +if (get_self_uprobe_target (uprobe_exe_path, &uprobe_offset)) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type = pmu->type; + base_attr.size =sizeof (base_attr); + base_attr.uprobe_path = (uintptr_t )uprobe_exe_path; + base_attr.probe_offset = uprobe_offset; +if (try_open_attr_variants (&base_attr, false, pmu_cpu, + flags, &pid, &cpu, &last_err)) { +const char *slash =strrchr (uprobe_exe_path,'/' ); +const char *bname = slash ? slash +1 : uprobe_exe_path; + +if (asprintf (&pmu->probed.probe_event,"uprobe:%s+0x%llx" , + bname, (unsigned long long )uprobe_offset) <0 ) + pmu->probed.probe_event =NULL ; + found = true; + } + } + }else if (pmu->is_core) { +static const char *const core_events[] = { +"cycles" , +"instructions" , +"branches" , +"branch-misses" , +"cache-misses" , +"cache-references" , +"bus-cycles" , +"ref-cycles" , + }; +char buf[256 ]; + +for (size_t i =0 ; i <ARRAY_SIZE (core_events); i++) { +snprintf (buf,sizeof (buf),"%s/%s/" , pmu->name, core_events[i]); +probe_candidates__add (&cands, buf); + } +perf_pmu__for_each_event (pmu,/*skip_duplicate_pmus=*/ false, + &cands, collect_probe_candidate_cb); + }else if (pmu->type ==PERF_TYPE_SOFTWARE ) { +static const char *const sw_events[] = { +"software/cpu-clock/" , +"software/task-clock/" , +"software/page-faults/" , +"software/context-switches/" , +"software/cpu-migrations/" , +"software/minor-faults/" , +"software/major-faults/" , +"software/dummy/" , + }; + +for (size_t i =0 ; i <ARRAY_SIZE (sw_events); i++) +probe_candidates__add (&cands, sw_events[i]); + }else if (pmu->type !=PERF_TYPE_BREAKPOINT ) { +if (pmu->selectable) { +char buf[256 ]; + +if (perf_pmu__has_format (pmu,"swfilt" )) { +snprintf (buf,sizeof (buf),"%s/swfilt=1/" , pmu->name); +probe_candidates__add (&cands, buf); + } +snprintf (buf,sizeof (buf),"%s//" , pmu->name); +probe_candidates__add (&cands, buf); + } +perf_pmu__for_each_event (pmu,/*skip_duplicate_pmus=*/ false, + &cands, collect_probe_candidate_cb); + } + +for (size_t i =0 ; !found && i < cands.nr; i++) { +memset (&base_attr,0 ,sizeof (base_attr)); +if (!parse_candidate_attr (cands.names[i], &base_attr)) +continue ; + +if (try_open_attr_variants (&base_attr, pmu->is_uncore, pmu_cpu, + flags, &pid, &cpu, &last_err)) { + pmu->probed.probe_event =strdup (cands.names[i]); + found = true; +break ; + } + } + +if (!found && pmu->is_core) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type =PERF_TYPE_HARDWARE ; + base_attr.config =PERF_COUNT_HW_CPU_CYCLES ; +if (perf_pmus__supports_extended_type ()) + base_attr.config |= (__u64 )pmu->type <<PERF_PMU_TYPE_SHIFT ; +if (try_open_attr_variants (&base_attr, false, pmu_cpu, + flags, &pid, &cpu, &last_err)) { + pmu->probed.probe_event =strdup ("cycles" ); + found = true; + } + } + +if (!found && pmu->type ==PERF_TYPE_BREAKPOINT ) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type =PERF_TYPE_BREAKPOINT ; + base_attr.bp_addr = (unsigned long )&dummy_bp_var; + base_attr.bp_type =HW_BREAKPOINT_W ; + base_attr.bp_len =HW_BREAKPOINT_LEN_4 ; +if (try_open_attr_variants (&base_attr, false, pmu_cpu, + flags, &pid, &cpu, &last_err)) { + pmu->probed.probe_event =strdup ("mem:<addr>:w" ); + found = true; + } + } + +if (!found && cands.nr ==0 ) { +memset (&base_attr,0 ,sizeof (base_attr)); + base_attr.type = pmu->type; + base_attr.size =sizeof (base_attr); +if (pmu->perf_event_attr_init_default) + pmu->perf_event_attr_init_default (pmu, &base_attr); +if (try_open_attr_variants (&base_attr, pmu->is_uncore, pmu_cpu, + flags, &pid, &cpu, &last_err)) { +char buf[256 ]; + +snprintf (buf,sizeof (buf),"%s//" , pmu->name); + pmu->probed.probe_event =strdup (buf); + found = true; + } + } + +if (!found) { +probe_candidates__exit (&cands); + pmu->probed.open_supported = false; + pmu->probed.open_err = last_err ?:ENOENT ; + pmu->probed.checked = true; +return ; + } + + pmu->probed.open_supported = true; + +/* +* For tracepoint events, closing the last fd for a tracepoint triggers +* tracepoint_synchronize_unregister() (an RCU grace period) in the +* kernel. Hold one disabled fd open across the probe sequence so +* tp_event->perf_refcount stays >= 1 and each probe open/close is +* instantaneous. +*/ +if (pmu->type ==PERF_TYPE_TRACEPOINT ) { +struct perf_event_attr ka_attr = base_attr; + + ka_attr.disabled =1 ; + keepalive_fd =sys_perf_event_open (&ka_attr, pid, cpu,-1 , flags); + } + + group_attrs[nr_group_attrs++] = base_attr; + +for (size_t i =0 ; pmu->type !=PERF_TYPE_TRACEPOINT && + i < cands.nr && nr_group_attrs <MAX_PROBE_GROUP_SIZE ; i++) { +struct perf_event_attr cand_attr; +int fd; +bool dup = false; + +memset (&cand_attr,0 ,sizeof (cand_attr)); +if (!parse_candidate_attr (cands.names[i], &cand_attr)) +continue ; + + cand_attr.sample_period = base_attr.sample_period; + cand_attr.freq = base_attr.freq; + cand_attr.sample_type = base_attr.sample_type; + cand_attr.exclude_user = base_attr.exclude_user; + cand_attr.exclude_kernel = base_attr.exclude_kernel; + cand_attr.exclude_hv = base_attr.exclude_hv; + cand_attr.exclude_idle = base_attr.exclude_idle; + cand_attr.exclude_host = base_attr.exclude_host; + cand_attr.exclude_guest = base_attr.exclude_guest; + cand_attr.disabled =1 ; + +for (size_t j =0 ; j < nr_group_attrs; j++) { +if (group_attrs[j].type == cand_attr.type && + group_attrs[j].config == cand_attr.config && + group_attrs[j].config1 == cand_attr.config1 && + group_attrs[j].config2 == cand_attr.config2) { + dup = true; +break ; + } + } +if (dup) +continue ; + + fd =sys_perf_event_open (&cand_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + group_attrs[nr_group_attrs++] = cand_attr; + } + } + +probe_candidates__exit (&cands); + +/* 1. Counting vs Sampling support */ +if (base_attr.sample_period ==0 ) { +struct perf_event_attr samp_attr = base_attr; +int fd; + + pmu->probed.can_count = true; + samp_attr.sample_period =10000 ; + samp_attr.sample_type =PERF_SAMPLE_IP ; + fd =sys_perf_event_open (&samp_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_sample = true; + } + }else { + pmu->probed.can_count = false; + pmu->probed.can_sample = true; + } + +/* 2. All enum perf_event_sample_format types */ +if (pmu->probed.can_sample) +probe_sample_types (pmu, &base_attr, pid, cpu, flags); + +/* +* 3. Precise sampling support (:p, :pp, :ppp). +* PMUs that do not validate attr.precise_ip (such as software, +* breakpoint, or intel_pt) silently accept precise_ip > 0 even when +* sample_period == 0. A PMU that genuinely implements precise_ip +* either rejects precise_ip > 0 on non-sampling events or is a +* sampling-only PMU (such as AMD ibs_op). +* Note: On AMD, core events like cycles:P are forwarded to IBS and +* support higher precision than advertised in sysfs caps/max_precise. +*/ +if (pmu->probed.can_sample && !pmu->auxtrace) { +bool ignores_precise_ip = false; + +if (pmu->probed.can_count) { +struct perf_event_attr c_attr = base_attr; +int fd; + + c_attr.sample_period =0 ; + c_attr.freq =0 ; + c_attr.sample_type =0 ; + c_attr.precise_ip =1 ; + fd =sys_perf_event_open (&c_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + ignores_precise_ip = true; + } + } + +if (!ignores_precise_ip) { +for (int level =1 ; level <=3 ; level++) { +struct perf_event_attr p_attr = base_attr; +int fd; + + p_attr.sample_period =10000 ; + p_attr.sample_type =PERF_SAMPLE_IP ; + p_attr.precise_ip = level; + fd =sys_perf_event_open (&p_attr, pid, cpu,-1 , flags); +if (fd <0 && p_attr.exclude_kernel && p_attr.config2 ==0 ) { + p_attr.config2 =1 ; + fd =sys_perf_event_open (&p_attr, pid, cpu,-1 , flags); + } +if (fd >=0 ) { +close (fd); + pmu->probed.precise_mask |= (1u << level); + pmu->probed.max_precise = level; + } + } + } + + } + +/* 4. Privilege / exclusion modifiers */ + pmu->probed.exclude_user = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_USER ); + pmu->probed.exclude_kernel = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_KERNEL ); + pmu->probed.exclude_hv = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_HV ); + pmu->probed.exclude_idle = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_IDLE ); + pmu->probed.exclude_host = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_HOST ); + pmu->probed.exclude_guest = +test_exclude_flag (&base_attr, pid, cpu, flags,PROBE_EXCLUDE_GUEST ); + +/* 5. Execution features */ +if (pmu->probed.can_sample) { +struct perf_event_attr l_attr = base_attr; +struct perf_event_attr f_attr = nr_group_attrs >1 ? group_attrs[1 ] : base_attr; +int l_fd; + + l_attr.disabled =1 ; + l_attr.sample_period =10000 ; + l_attr.sample_type =PERF_SAMPLE_IP |PERF_SAMPLE_READ ; + l_attr.read_format =PERF_FORMAT_GROUP |PERF_FORMAT_ID ; + + f_attr.disabled =0 ; + f_attr.sample_period =0 ; + f_attr.sample_type =0 ; + f_attr.read_format =0 ; + + l_fd =sys_perf_event_open (&l_attr, pid, cpu,-1 , flags); +if (l_fd >=0 ) { +int f_fd =sys_perf_event_open (&f_attr, pid, cpu, l_fd, flags); + +if (f_fd >=0 ) { +if (ioctl (l_fd,PERF_EVENT_IOC_ENABLE ,PERF_IOC_FLAG_GROUP ) >=0 ) + pmu->probed.can_leader_sampling = true; +ioctl (l_fd,PERF_EVENT_IOC_DISABLE ,PERF_IOC_FLAG_GROUP ); +close (f_fd); + } +close (l_fd); + } + } + + { +struct perf_event_attr l_attr = base_attr; +int l_fd; + + l_attr.disabled =1 ; + l_fd =sys_perf_event_open (&l_attr, pid, cpu,-1 , flags); +if (l_fd >=0 ) { +struct perf_event_attr sw_attr; +int sw_fd; + +memset (&sw_attr,0 ,sizeof (sw_attr)); + sw_attr.type =PERF_TYPE_SOFTWARE ; + sw_attr.size =sizeof (sw_attr); + sw_attr.config =PERF_COUNT_SW_DUMMY ; + sw_attr.disabled =0 ; + sw_attr.exclude_user = base_attr.exclude_user; + sw_attr.exclude_kernel = base_attr.exclude_kernel; + sw_attr.exclude_hv = base_attr.exclude_hv; + sw_attr.exclude_idle = base_attr.exclude_idle; + sw_attr.exclude_host = base_attr.exclude_host; + sw_attr.exclude_guest = base_attr.exclude_guest; + + sw_fd =sys_perf_event_open (&sw_attr, pid, cpu, l_fd, flags); +if (sw_fd <0 ) { + sw_attr.config =PERF_COUNT_SW_CPU_CLOCK ; + sw_fd =sys_perf_event_open (&sw_attr, pid, cpu, l_fd, flags); + } +if (sw_fd >=0 ) { + pmu->probed.can_sw_grouping = true; +close (sw_fd); + } +close (l_fd); + } + } + + { +struct perf_event_attr inh_attr = base_attr; +int fd; + + inh_attr.disabled =1 ; + inh_attr.inherit =1 ; + fd =sys_perf_event_open (&inh_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_inherit = true; + } + } + +if (pmu->probed.can_sample) { +struct perf_event_attr freq_attr = base_attr; +int fd; + + freq_attr.disabled =1 ; + freq_attr.freq =1 ; + freq_attr.sample_freq =4000 ; + freq_attr.sample_type =PERF_SAMPLE_IP ; + fd =sys_perf_event_open (&freq_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_freq = true; + } + } + +if (pmu->probed.can_sample) { +struct perf_event_attr wb_attr = base_attr; +int fd; + + wb_attr.disabled =1 ; + wb_attr.sample_period =10000 ; + wb_attr.sample_type =PERF_SAMPLE_IP ; + wb_attr.write_backward =1 ; + fd =sys_perf_event_open (&wb_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_write_backward = true; + } + } + + { +struct perf_event_attr pin_attr = base_attr; +int fd; + + pin_attr.disabled =1 ; + pin_attr.pinned =1 ; + fd =sys_perf_event_open (&pin_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_pinned = true; + } + } + + { +struct perf_event_attr exc_attr = base_attr; +int fd; + + exc_attr.disabled =1 ; + exc_attr.exclusive =1 ; + fd =sys_perf_event_open (&exc_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_exclusive = true; + } + } + + { +struct perf_event_attr lost_attr = base_attr; +int fd; + + lost_attr.disabled =1 ; + lost_attr.read_format =PERF_FORMAT_TOTAL_TIME_ENABLED | +PERF_FORMAT_TOTAL_TIME_RUNNING | +PERF_FORMAT_LOST ; + fd =sys_perf_event_open (&lost_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + pmu->probed.can_format_lost = true; + } + } + +/* 5c. Auto-counter reload (acr_mask / ratio-to-prev) */ +if (pmu->probed.can_sample &&perf_pmu__has_format (pmu,"acr_mask" )) { +struct perf_event_attr l_attr = base_attr; +struct perf_event_attr f_attr = nr_group_attrs >1 ? group_attrs[1 ] : base_attr; +int l_fd; + + l_attr.disabled =1 ; + l_attr.freq =0 ; + l_attr.sample_period =200000 ; + l_attr.sample_type =PERF_SAMPLE_IP ; + l_attr.config2 =0x2 ; + + f_attr.disabled =0 ; + f_attr.freq =0 ; + f_attr.sample_period =100000 ; + f_attr.sample_type =PERF_SAMPLE_IP ; + f_attr.config2 =0x3 ; + + l_fd =sys_perf_event_open (&l_attr, pid, cpu,-1 , flags); +if (l_fd >=0 ) { +int f_fd =sys_perf_event_open (&f_attr, pid, cpu, l_fd, flags); + +if (f_fd >=0 ) { +if (ioctl (l_fd,PERF_EVENT_IOC_ENABLE ,PERF_IOC_FLAG_GROUP ) >=0 ) + pmu->probed.can_acr = true; +ioctl (l_fd,PERF_EVENT_IOC_DISABLE ,PERF_IOC_FLAG_GROUP ); +close (f_fd); + } +close (l_fd); + } + } + +/* 5d. AUX area event actions (start_paused, pause, resume, aux_sample) */ +if (pmu->auxtrace) { +struct perf_event_attr a_attr = base_attr; +int l_fd; + + a_attr.size =max_t (__u32, base_attr.size,PERF_ATTR_SIZE_VER8 ); + a_attr.disabled =1 ; + a_attr.aux_start_paused =1 ; + l_fd =sys_perf_event_open (&a_attr, pid, cpu,-1 , flags); +if (l_fd >=0 ) { +close (l_fd); + pmu->probed.can_aux_start_paused = true; + } + + a_attr = base_attr; + a_attr.disabled =1 ; + l_fd =sys_perf_event_open (&a_attr, pid, cpu,-1 , flags); +if (l_fd >=0 ) { +struct perf_event_attr f_attr; +int f_fd; + +memset (&f_attr,0 ,sizeof (f_attr)); + f_attr.type =PERF_TYPE_SOFTWARE ; + f_attr.size =max_t (__u32, base_attr.size,PERF_ATTR_SIZE_VER8 ); + f_attr.config =PERF_COUNT_SW_CPU_CLOCK ; + f_attr.sample_period =10000 ; + f_attr.sample_type =PERF_SAMPLE_IP ; + f_attr.exclude_user = base_attr.exclude_user; + f_attr.exclude_kernel = base_attr.exclude_kernel; + f_attr.exclude_hv = base_attr.exclude_hv; + f_attr.exclude_idle = base_attr.exclude_idle; + f_attr.exclude_host = base_attr.exclude_host; + f_attr.exclude_guest = base_attr.exclude_guest; + + f_attr.aux_pause =1 ; + f_attr.aux_resume =0 ; + f_fd =sys_perf_event_open (&f_attr, pid, cpu, l_fd, flags); +if (f_fd >=0 ) { +close (f_fd); + pmu->probed.can_aux_pause = true; + } + + f_attr.aux_pause =0 ; + f_attr.aux_resume =1 ; + f_fd =sys_perf_event_open (&f_attr, pid, cpu, l_fd, flags); +if (f_fd >=0 ) { +close (f_fd); + pmu->probed.can_aux_resume = true; + } + + f_attr.aux_resume =0 ; + f_attr.sample_type =PERF_SAMPLE_IP |PERF_SAMPLE_AUX ; + f_attr.aux_sample_size =64 ; + f_fd =sys_perf_event_open (&f_attr, pid, cpu, l_fd, flags); +if (f_fd >=0 ) { +close (f_fd); + pmu->probed.can_aux_sample = true; + } + +close (l_fd); + } + } + +/* 6. Max event group size & max simultaneously active group size */ +if (pmu->name && (!strcmp (pmu->name,"kprobe" ) || !strcmp (pmu->name,"uprobe" ))) { +if (pmu->probed.can_leader_sampling || pmu->probed.can_sw_grouping) { + pmu->probed.max_group_size =MAX_PROBE_GROUP_SIZE ; + pmu->probed.max_group_capped = true; + pmu->probed.max_active_group_size =MAX_PROBE_GROUP_SIZE ; + pmu->probed.max_active_capped = true; + }else { + pmu->probed.max_group_size =1 ; + pmu->probed.max_active_group_size =1 ; + } + }else { +probe_group_limits (group_attrs, nr_group_attrs, pid, cpu, flags, + &pmu->probed.max_group_size, + &pmu->probed.max_group_capped, + &pmu->probed.max_active_group_size, + &pmu->probed.max_active_capped); + } + +/* +* On core PMUs, cycles/instructions may use fixed counters in addition +* to general-purpose (GP) programmable counters. Probe a GP-only event +* group rotating through distinct GP events to determine the GP limit. +*/ +if (pmu->is_core) { +static const char *const gp_events[] = { +"branches" , +"branch-misses" , +"cache-misses" , +"cache-references" , +"bus-cycles" , +"stalled-cycles-frontend" , +"stalled-cycles-backend" , + }; +static const int gp_configs[] = { +PERF_COUNT_HW_BRANCH_INSTRUCTIONS , +PERF_COUNT_HW_BRANCH_MISSES , +PERF_COUNT_HW_CACHE_MISSES , +PERF_COUNT_HW_CACHE_REFERENCES , +PERF_COUNT_HW_BUS_CYCLES , +PERF_COUNT_HW_STALLED_CYCLES_FRONTEND , +PERF_COUNT_HW_STALLED_CYCLES_BACKEND , + }; +struct perf_event_attr gp_attrs[MAX_PROBE_GROUP_SIZE ]; +size_t nr_gp_attrs =0 ; +pid_t gp_pid = pid; +int gp_cpu = cpu; +int gp_err =0 ; + +for (size_t i =0 ; + i <ARRAY_SIZE (gp_events) && nr_gp_attrs <MAX_PROBE_GROUP_SIZE ; i++) { +char gp_buf[256 ]; +struct perf_event_attr gp_attr; +bool parsed; + +snprintf (gp_buf,sizeof (gp_buf),"%s/%s/" , pmu->name, gp_events[i]); +memset (&gp_attr,0 ,sizeof (gp_attr)); + parsed =parse_candidate_attr (gp_buf, &gp_attr); +if (!parsed) { + gp_attr.type =PERF_TYPE_HARDWARE ; + gp_attr.config = gp_configs[i]; +if (perf_pmus__supports_extended_type ()) + gp_attr.config |= (__u64 )pmu->type <<PERF_PMU_TYPE_SHIFT ; + } + gp_attr.sample_period = base_attr.sample_period; + gp_attr.freq = base_attr.freq; + gp_attr.sample_type = base_attr.sample_type; + gp_attr.exclude_user = base_attr.exclude_user; + gp_attr.exclude_kernel = base_attr.exclude_kernel; + gp_attr.exclude_hv = base_attr.exclude_hv; + gp_attr.exclude_idle = base_attr.exclude_idle; + gp_attr.exclude_host = base_attr.exclude_host; + gp_attr.exclude_guest = base_attr.exclude_guest; + gp_attr.disabled =1 ; + +if (nr_gp_attrs ==0 ) { +if (try_open_attr_variants (&gp_attr, false, pmu_cpu, + flags, &gp_pid, &gp_cpu, &gp_err)) { + gp_attrs[nr_gp_attrs++] = gp_attr; + } + }else { +int fd =sys_perf_event_open (&gp_attr, gp_pid, gp_cpu,-1 , flags); + +if (fd >=0 ) { +close (fd); + gp_attrs[nr_gp_attrs++] = gp_attr; + } + } + } + +if (nr_gp_attrs >0 ) { +probe_group_limits (gp_attrs, nr_gp_attrs, gp_pid, gp_cpu, flags, + &pmu->probed.max_gp_group_size, + &pmu->probed.max_gp_group_capped, + &pmu->probed.max_active_gp_group_size, + &pmu->probed.max_active_gp_capped); + } + } + +if (keepalive_fd >=0 ) +close (keepalive_fd); + pmu->probed.checked = true; +} +const char *perf_pmu__name_from_config (struct perf_pmu *pmu,u64 config) {struct hashmap_entry *entry;
tools/perf/util/pmu.h +133 -1
147 lines not on this page · raw message
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 5/9] perf pmu: Add perf_pmu__probe() for live capability probing
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf pmu: select_probe_cpu() ignores task CPU affinity mask --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf pmu: select_probe_cpu() ignores task CPU affinity mask
--
commit a706bded05059ea05d5c1e851935d2f599c67616
Author: Ian Rogers <[email protected]>
perf pmu: Add perf_pmu__probe() for live capability probing
This commit adds struct perf_pmu::probed and perf_pmu__probe() to actively probe live PMU capabilities via perf_event_open(), covering counting and sampling modes, precise_ip levels, sample formats, privilege exclusion modifiers, execution features, and event group size limits.
Quoted 4 lines
tools/perf/util/pmu.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/pmu.c +0
-0@@ -2832,9 +2847,1219 @@ void perf_pmu__delete(struct perf_pmu *pmu)
[ ... ]
Quoted 26 lines
tools/perf/util/pmu.c +26
-0+static int select_probe_cpu (const struct perf_pmu *pmu) +{ +struct perf_cpu_map *online =perf_cpu_map__new_online_cpus (); +int cpu =0 ; + +if (pmu->cpus && !perf_cpu_map__is_empty (pmu->cpus)) { +int nr =perf_cpu_map__nr (pmu->cpus); + + cpu =perf_cpu_map__cpu (pmu->cpus,0 ).cpu; +if (online) { +for (int i =0 ; i < nr; i++) { +struct perf_cpu c =perf_cpu_map__cpu (pmu->cpus, i); + +if (perf_cpu_map__has (online, c)) { + cpu = c.cpu; +break ; + } + } + } + }else if (online && !perf_cpu_map__is_empty (online)) { + cpu =perf_cpu_map__cpu (online,0 ).cpu; + } + +perf_cpu_map__put (online); +return cpu >=0 ? cpu :0 ; +}
[ ... ]
Quoted 22 lines
tools/perf/util/pmu.c +22
-0+static void probe_group_limits (const struct perf_event_attr *attrs,size_t nr_attrs, +pid_t pid,int cpu,unsigned long flags, +int *max_open,bool *open_capped, +int *max_active,bool *active_capped) +{ +struct perf_event_attr leader_attr; +int leader_fd; +int fds[MAX_PROBE_GROUP_SIZE ]; +int nr_followers =0 ; +bool has_time_read = true; +bool active_ok = true; +enum rlimit_action set_rlimit =NO_CHANGE ; +cpu_set_t saved_affinity, probe_affinity; +bool affinity_changed = false; + + *max_open =0 ; + *open_capped = false; + *max_active =0 ; + *active_capped = false; + +if (!attrs || nr_attrs ==0 ) +return ;
tools/perf/util/pmu.c void perf_pmu__delete(struct perf_pmu *pmu) · line 3152
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+ +if (pid ==0 && cpu >=0 && cpu <CPU_SETSIZE ) { +CPU_ZERO (&saved_affinity); +if (sched_getaffinity (0 ,sizeof (saved_affinity), &saved_affinity) ==0 ) { +CPU_ZERO (&probe_affinity); +CPU_SET (cpu, &probe_affinity); +if (sched_setaffinity (0 ,sizeof (probe_affinity), &probe_affinity) ==0 ) + affinity_changed = true; + } + }
[Severity: Medium]
What happens in probe_group_limits() when perf runs inside a cgroup cpuset (such as a container or a systemd unit with AllowedCPUs=) that excludes the first online CPU of a PMU?
In select_probe_cpu(), the probe CPU is chosen as the first online CPU in pmu->cpus (or online) without checking whether that CPU is in the calling thread's allowed affinity mask via sched_getaffinity().
For non-uncore PMUs, try_open_attr_variants() opens events with pid = 0 and cpu = pmu_cpu, which sys_perf_event_open() allows even when pmu_cpu is outside the task's cpuset. However, when probe_group_limits() then tries to pin the calling thread to cpu with sched_setaffinity(), it fails with -EINVAL.
When check_group_active() subsequently enables the (pid = 0, cpu = pmu_cpu) group via PERF_EVENT_IOC_ENABLE and spins in userspace on a different CPU, the kernel sets the event to PERF_EVENT_STATE_INACTIVE (incrementing TOTAL_TIME_ENABLED), while event_filter_match() prevents scheduling it onto the PMU because event->cpu != smp_processor_id(). As a result, TOTAL_TIME_RUNNING stays 0 (delta_run != delta_ena), check_group_active() returns false even for a single event, and max_active_group_size and max_active_gp_group_size are reported as 0.
Should select_probe_cpu() prefer an online CPU that is also present in the calling thread's sched_getaffinity() mask when available?
[ ... ]
Quoted 3 lines
tools/perf/util/pmu.c +3
-0+ leader_attr = attrs[0 ]; + leader_attr.disabled =1 ; + leader_attr.read_format =PERF_FORMAT_TOTAL_TIME_ENABLED |PERF_FORMAT_TOTAL_TIME_RUNNING ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerRFC PATCH v2 6/9 perf pmu: Probe registers, memory events, rdpmc, and branch skid
In reply to Ian RogersExtend perf_pmu__probe() to probe supported user and interrupt sampling register masks (both general-purpose and SIMD), memory profiling events and sample formats, user-space mmap counter reading (rdpmc) and PMC bit- width, and empirically measured branch instruction skid across precision levels.
--- tools/perf/util/pmu.c | 758 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 758 insertions(+)
tools/perf/util/pmu.c +758 -0
@@ -3356,6 +3356,739 @@ static void probe_sample_types(struct perf_pmu *pmu, pmu->probed.can_weight = !!(pmu->probed.sample_types &PERF_SAMPLE_WEIGHT ); } +static void probe_pmu_regs_one (const struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags, +int max_precise,bool intr, +u64 *out_mask,int *out_abi, +u64 *out_simd_mask,u64 *out_pred_mask) +{ +int dummy_abi =-1 ; +u64 gpr_mask = intr ?perf_intr_reg_mask (EM_HOST , &dummy_abi) : +perf_user_reg_mask (EM_HOST , &dummy_abi); +u64 sample_type = intr ?PERF_SAMPLE_REGS_INTR :PERF_SAMPLE_REGS_USER ; +u64 mask =0 ; +int abi =0 ; +struct perf_event_attr attr = *base_attr; +int fd; + + attr.disabled =1 ; + attr.sample_period = base_attr->sample_period ?:10000 ; + attr.sample_type = sample_type; + +if (gpr_mask) { +if (intr) + attr.sample_regs_intr = gpr_mask; +else + attr.sample_regs_user = gpr_mask; + + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + mask |= gpr_mask; + }else { +for (int b =0 ; b <64 ; b++) { +if (!(gpr_mask &BIT_ULL (b))) +continue ; +if (intr) + attr.sample_regs_intr =BIT_ULL (b); +else + attr.sample_regs_user =BIT_ULL (b); + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + mask |=BIT_ULL (b); + } + } + } + } + +if (perf_pmu__is_software (pmu)) { + *out_mask = mask; + *out_abi = abi; +return ; + } + +/* +* Probe extended GPRs outside the base GPR mask when the host ABI +* supports PERF_SAMPLE_REGS_ABI_SIMD (e.g. APX R16-R31 and CET SSP). +* Note: perf_pmu__probe() actively probes the running host kernel via +* perf_event_open(), so EM_HOST is used intentionally here. +*/ + { +int host_abi =0 ; +u64 host_mask = intr ?perf_intr_reg_mask (EM_HOST , &host_abi) : +perf_user_reg_mask (EM_HOST , &host_abi); +u64 ext_gpr_mask = host_mask & ~gpr_mask; + +if ((host_abi &PERF_SAMPLE_REGS_ABI_SIMD ) && ext_gpr_mask) { +struct perf_event_attr e_attr = *base_attr; +bool ext_batch_ok = false; + + e_attr.size =sizeof (e_attr); + e_attr.disabled =1 ; + e_attr.sample_period = base_attr->sample_period ?:10000 ; + e_attr.sample_type = sample_type; + e_attr.sample_simd_regs_enabled =1 ; +if (intr) + e_attr.sample_regs_intr = ext_gpr_mask; +else + e_attr.sample_regs_user = ext_gpr_mask; + + fd =sys_perf_event_open (&e_attr, pid, cpu,-1 , flags); +if (fd <0 && intr && max_precise >0 ) { + e_attr.precise_ip =1 ; + fd =sys_perf_event_open (&e_attr, pid, cpu,-1 , flags); + } +if (fd >=0 ) { +close (fd); + mask |= ext_gpr_mask; + abi |=PERF_SAMPLE_REGS_ABI_SIMD ; + ext_batch_ok = true; + } + +if (!ext_batch_ok) { +for (int b =0 ; b <64 ; b++) { +if (!(ext_gpr_mask &BIT_ULL (b))) +continue ; + e_attr.precise_ip = base_attr->precise_ip; +if (intr) + e_attr.sample_regs_intr =BIT_ULL (b); +else + e_attr.sample_regs_user =BIT_ULL (b); + fd =sys_perf_event_open (&e_attr, pid, cpu,-1 , flags); +if (fd <0 && intr && max_precise >0 ) { + e_attr.precise_ip =1 ; + fd =sys_perf_event_open (&e_attr, pid, cpu,-1 , + flags); + } +if (fd >=0 ) { +close (fd); + mask |=BIT_ULL (b); + abi |=PERF_SAMPLE_REGS_ABI_SIMD ; + } + } + } + } + } + +for (int is_pred =0 ; is_pred <=1 ; is_pred++) { +bool pred = is_pred !=0 ; +u64 class_mask = intr ? +perf_intr_simd_reg_class_mask (EM_HOST , pred) : +perf_user_simd_reg_class_mask (EM_HOST , pred); +u64 supported_classes =0 ; + +for (int reg_c =0 ; reg_c <64 ; reg_c++) { +struct perf_event_attr s_attr = *base_attr; +u16 qwords =0 ; +u64 bitmap; + +if (!(class_mask &BIT_ULL (reg_c))) +continue ; + bitmap = intr ? +perf_intr_simd_reg_class_bitmap_qwords (EM_HOST , reg_c, + &qwords, pred) : +perf_user_simd_reg_class_bitmap_qwords (EM_HOST , reg_c, + &qwords, pred); +if (!bitmap || !qwords) +continue ; + + s_attr.size =sizeof (s_attr); + s_attr.disabled =1 ; + s_attr.sample_period = base_attr->sample_period ?:10000 ; + s_attr.sample_type = sample_type; + s_attr.sample_simd_regs_enabled =1 ; +if (pred) { + s_attr.sample_simd_pred_reg_qwords = qwords; +if (intr) + s_attr.sample_simd_pred_reg_intr = bitmap; +else + s_attr.sample_simd_pred_reg_user = bitmap; + }else { + s_attr.sample_simd_vec_reg_qwords = qwords; +if (intr) + s_attr.sample_simd_vec_reg_intr = bitmap; +else + s_attr.sample_simd_vec_reg_user = bitmap; + } + + fd =sys_perf_event_open (&s_attr, pid, cpu,-1 , flags); +if (fd <0 && intr && max_precise >0 ) { + s_attr.precise_ip =1 ; + fd =sys_perf_event_open (&s_attr, pid, cpu,-1 , flags); + } +if (fd >=0 ) { +close (fd); + supported_classes |=BIT_ULL (reg_c); + abi |=PERF_SAMPLE_REGS_ABI_SIMD ; + } + } + +if (pred) + *out_pred_mask = supported_classes; +else + *out_simd_mask = supported_classes; + } + +if (!(abi &PERF_SAMPLE_REGS_ABI_SIMD )) { +u64 ext_mask =GENMASK_ULL (63 ,32 ) & ~gpr_mask; +struct perf_event_attr x_attr = *base_attr; +bool batch_ok = false; + + x_attr.disabled =1 ; + x_attr.sample_period = base_attr->sample_period ?:10000 ; + x_attr.sample_type = sample_type; +if (intr && max_precise >0 ) + x_attr.precise_ip =1 ; + +if (ext_mask) { +if (intr) + x_attr.sample_regs_intr = ext_mask; +else + x_attr.sample_regs_user = ext_mask; + + fd =sys_perf_event_open (&x_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + mask |= ext_mask; + batch_ok = true; + } + } + +if (!batch_ok) { +for (int b =0 ; b <64 ; b++) { +const char *rname; + +if (gpr_mask &BIT_ULL (b)) +continue ; + rname =perf_reg_name (b,EM_HOST ,EF_HOST ,0 ); +if (!rname || !strcmp (rname,"unknown" )) +continue ; +if (intr) + x_attr.sample_regs_intr =BIT_ULL (b); +else + x_attr.sample_regs_user =BIT_ULL (b); + fd =sys_perf_event_open (&x_attr, pid, cpu,-1 , flags); +if (fd >=0 ) { +close (fd); + mask |=BIT_ULL (b); + } + } + } + } + + *out_mask = mask; + *out_abi = abi; +} + +static bool try_open_mem_evlist (struct evlist *evlist, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags, +int precise_ip,u64 sample_type) +{ +int fds[16 ]; +size_t nr_fds =0 ; +int leader_fd =-1 ; +struct evsel *evsel; +bool ok = true; + +evlist__for_each_entry (evlist, evsel) { +struct perf_event_attr attr = evsel->core.attr; +int fd; + +if (nr_fds >=ARRAY_SIZE (fds)) { + ok = false; +break ; + } + + attr.disabled = (leader_fd <0 ) ?1 :0 ; + attr.sample_period = attr.sample_period ?:10000 ; + attr.sample_type = sample_type; + attr.exclude_user = base_attr->exclude_user; + attr.exclude_kernel = base_attr->exclude_kernel; + attr.exclude_hv = base_attr->exclude_hv; + attr.exclude_idle = base_attr->exclude_idle; + attr.exclude_host = base_attr->exclude_host; + attr.exclude_guest = base_attr->exclude_guest; +if (evsel->precise_max || precise_ip >0 ) + attr.precise_ip = precise_ip; + + fd =sys_perf_event_open (&attr, pid, cpu, leader_fd, flags); +if (fd <0 ) { + ok = false; +break ; + } +if (leader_fd <0 ) + leader_fd = fd; + fds[nr_fds++] = fd; + } + +for (size_t i = nr_fds; i >0 ; i--) +close (fds[i -1 ]); + +return ok && nr_fds >0 ; +} + +static void probe_mem_events (struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +pid_t pid,int cpu,unsigned long flags) +{ +static const u64 extra_mem_stypes[] = { +PERF_SAMPLE_WEIGHT , +PERF_SAMPLE_WEIGHT_STRUCT , +PERF_SAMPLE_PHYS_ADDR , +PERF_SAMPLE_DATA_PAGE_SIZE , +PERF_SAMPLE_TRANSACTION , + }; + +if (!pmu->mem_events) +return ; + +perf_pmu__mem_events_init_pmu (pmu); + +for (int j =0 ; j <PERF_MEM_EVENTS__MAX ; j++) { +struct perf_mem_event *e =perf_pmu__mem_events_ptr (pmu, j); +const char *s; +char buf[256 ]; +struct evlist *evlist; +struct parse_events_error err; +struct evsel *first; +int max_p, min_p, working_p =-1 ; +u64 base_stype =PERF_SAMPLE_IP |PERF_SAMPLE_ADDR |PERF_SAMPLE_DATA_SRC ; +int ret; + +if (!e || !e->tag || !e->supported) +continue ; + + s =perf_pmu__mem_events_name (pmu, j, buf,sizeof (buf)); +if (!s) +continue ; + + evlist =evlist__new (); +if (!evlist) +continue ; + +parse_events_error__init (&err); + ret =__parse_events (evlist, s,/*pmu_filter=*/ NULL , +/*cputype_filter=*/ false, &err,/*fake_pmu=*/ false, +/*warn_if_reordered=*/ false,/*fake_tp=*/ false); +parse_events_error__exit (&err); +if (ret ||evlist__empty (evlist)) { +evlist__put (evlist); +continue ; + } + + first =evlist__first (evlist); +if (first->precise_max) { + max_p = pmu->probed.max_precise >0 ? pmu->probed.max_precise :3 ; + min_p =1 ; + }else { + max_p = first->core.attr.precise_ip; + min_p = first->core.attr.precise_ip; + } + +for (int p = max_p; p >= min_p; p--) { +if (try_open_mem_evlist (evlist, base_attr, pid, cpu, + flags, p, base_stype)) { + working_p = p; +break ; + } + } + +if (working_p >=0 ) { + pmu->probed.mem_events_mask |= (1u << j); + pmu->probed.mem_sample_types |=PERF_SAMPLE_ADDR |PERF_SAMPLE_DATA_SRC ; + +for (size_t k =0 ; k <ARRAY_SIZE (extra_mem_stypes); k++) { +u64 st = base_stype | extra_mem_stypes[k]; + +if (try_open_mem_evlist (evlist, base_attr, pid, cpu, + flags, working_p, st)) { + pmu->probed.mem_sample_types |= extra_mem_stypes[k]; + } + } + } + +evlist__put (evlist); + } +} + +static void probe_mmap_caps (struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +int pmu_cpu,pid_t pid,int cpu, +unsigned long flags) +{ +struct perf_event_attr attr = *base_attr; +struct perf_event_mmap_page *pc; +long page_sz =sysconf (_SC_PAGESIZE); +cpu_set_t old_mask, mask; +bool affinity_changed = false; +bool has_rdpmc_cap = false; +int events_fd; +int fd =-1 ; + + pmu->probed.rdpmc_sysfs =-1 ; + +if (page_sz <=0 ) + page_sz =4096 ; + + events_fd =perf_pmu__event_source_devices_fd (); +if (events_fd >=0 ) { +int pmu_dirfd =openat (events_fd, pmu->name,O_DIRECTORY |O_RDONLY |O_CLOEXEC ); + +if (pmu_dirfd >=0 ) { +if (faccessat (pmu_dirfd,"rdpmc" ,F_OK ,0 ) ==0 ) { +int rfd; + + has_rdpmc_cap = true; + rfd =openat (pmu_dirfd,"rdpmc" ,O_RDONLY |O_CLOEXEC ); +if (rfd >=0 ) { +char rbuf[16 ]; +ssize_t n =read (rfd, rbuf,sizeof (rbuf) -1 ); + +if (n >0 ) { + rbuf[n] ='\0' ; + pmu->probed.rdpmc_sysfs =atoi (rbuf); + } +close (rfd); + } + } +close (pmu_dirfd); + } +close (events_fd); + } + +if (pmu_cpu >=0 && pmu_cpu <CPU_SETSIZE && +sched_getaffinity (0 ,sizeof (old_mask), &old_mask) ==0 ) { +CPU_ZERO (&mask); +CPU_SET (pmu_cpu, &mask); +if (sched_setaffinity (0 ,sizeof (mask), &mask) ==0 ) + affinity_changed = true; + } + + attr.disabled =0 ; + +/* +* On architectures such as arm64, user-space counter reading must be +* explicitly requested via format/rdpmc (config1:1). +*/ +if (perf_pmu__has_format (pmu,"rdpmc" )) { +struct perf_event_attr r_attr = attr; +int uval; + + has_rdpmc_cap = true; +if (pmu->probed.rdpmc_sysfs <0 && +sysctl__read_int ("kernel/perf_user_access" , &uval) ==0 ) + pmu->probed.rdpmc_sysfs = uval; + + r_attr.config1 |=0x2 ; + fd =sys_perf_event_open (&r_attr,0 ,-1 ,-1 , flags); + } + +if (fd <0 && !pmu->is_uncore) + fd =sys_perf_event_open (&attr,0 ,-1 ,-1 , flags); +if (fd <0 ) + fd =sys_perf_event_open (&attr, pid, cpu,-1 , flags); + +if (fd >=0 ) { + pc =mmap (NULL , page_sz,PROT_READ ,MAP_SHARED , fd,0 ); +if (pc !=MAP_FAILED ) { +if (has_rdpmc_cap || pc->cap_user_rdpmc) { + pmu->probed.rdpmc_available = true; + pmu->probed.pmc_width = pc->pmc_width; + } +if (pc->cap_user_rdpmc && pc->index !=0 ) { +struct perf_mmap map = { .base = pc }; +struct perf_counts_values start = { .val =0 }; +struct perf_counts_values end = { .val =0 }; + + pmu->probed.can_user_rdpmc = true; +if (perf_mmap__read_self (&map, &start) ==0 ) { +for (int spin =0 ; spin <10000 ; spin++) +barrier (); +if (perf_mmap__read_self (&map, &end) ==0 && + end.val >= start.val && end.val >0 ) + pmu->probed.rdpmc_works = true; + } + } + pmu->probed.can_user_time = !!pc->cap_user_time; + pmu->probed.can_user_time_zero = !!pc->cap_user_time_zero; + pmu->probed.can_user_time_short = !!pc->cap_user_time_short; +munmap (pc, page_sz); + } +close (fd); + } + +if (affinity_changed) +sched_setaffinity (0 ,sizeof (old_mask), &old_mask); +} + +/* +* Mark noinline so the compiler does not inline the multi-kilobyte .rept nop +* runway into callers; GCC estimates inline asm size by counting newlines in +* the template string and would otherwise emit short-range conditional +* branches (such as ARM64 tbz/tbnz with a +-32KB range) across the inlined +* block. +*/ +static noinline bool run_skid_branch_loop(u64 iters, uintptr_t *out_s_ip, +uintptr_t *out_b_ip, +uintptr_t *out_e_ip, +int *out_insn_sz,int *out_nop_cnt) +{ +#if defined(__x86_64__) +uintptr_t s_ip, b_ip, e_ip; + + asmvolatile ( +"lea 1f(%%rip), %[s_ip]\n\t" +"lea 2f(%%rip), %[b_ip]\n\t" +"lea 3f(%%rip), %[e_ip]\n\t" +".balign 64\n" +"1:\n\t" +".rept 8192\n\t" +"nop\n\t" +".endr\n\t" +"dec %[cnt]\n\t" +"2:\n\t" +"jnz 1b\n" +"3:\n\t" + : [cnt]"+r" (iters), + [s_ip]"=&r" (s_ip), + [b_ip]"=&r" (b_ip), + [e_ip]"=&r" (e_ip) + : + :"cc" ,"memory" + ); + *out_s_ip = s_ip; + *out_b_ip = b_ip; + *out_e_ip = e_ip; + *out_insn_sz =1 ; + *out_nop_cnt =8192 ; +return true; +#elif defined(__aarch64__) +uintptr_t s_ip, b_ip, e_ip; + + asmvolatile ( +"adr %[s_ip], 1f\n\t" +"adr %[b_ip], 2f\n\t" +"adr %[e_ip], 3f\n\t" +".balign 64\n" +"1:\n\t" +".rept 1024\n\t" +"nop\n\t" +".endr\n\t" +"subs %[cnt], %[cnt], #1\n\t" +"2:\n\t" +"b.ne 1b\n" +"3:\n\t" + : [cnt]"+r" (iters), + [s_ip]"=&r" (s_ip), + [b_ip]"=&r" (b_ip), + [e_ip]"=&r" (e_ip) + : + :"cc" ,"memory" + ); + *out_s_ip = s_ip; + *out_b_ip = b_ip; + *out_e_ip = e_ip; + *out_insn_sz =4 ; + *out_nop_cnt =1024 ; +return true; +#elif defined(__riscv) +uintptr_t s_ip, b_ip, e_ip; + + asmvolatile ( +".option push\n\t" +".option norvc\n\t" +"la %[s_ip], 1f\n\t" +"la %[b_ip], 2f\n\t" +"la %[e_ip], 3f\n\t" +".balign 64\n" +"1:\n\t" +".rept 512\n\t" +"nop\n\t" +".endr\n\t" +"addi %[cnt], %[cnt], -1\n\t" +"2:\n\t" +"bnez %[cnt], 1b\n" +"3:\n\t" +".option pop\n\t" + : [cnt]"+r" (iters), + [s_ip]"=&r" (s_ip), + [b_ip]"=&r" (b_ip), + [e_ip]"=&r" (e_ip) + : + :"memory" + ); + *out_s_ip = s_ip; + *out_b_ip = b_ip; + *out_e_ip = e_ip; + *out_insn_sz =4 ; + *out_nop_cnt =512 ; +return true; +#else + (void )iters; + *out_s_ip =0 ; + *out_b_ip =0 ; + *out_e_ip =0 ; + *out_insn_sz =0 ; + *out_nop_cnt =0 ; +return false; +#endif +} + +static void probe_pmu_skid (struct perf_pmu *pmu, +const struct perf_event_attr *base_attr, +int pmu_cpu,unsigned long flags) +{ +struct perf_event_attr br_attr; +char br_buf[256 ]; +long page_sz =sysconf (_SC_PAGESIZE); +size_t data_sz, mmap_sz; +cpu_set_t old_mask, mask; +bool affinity_changed = false; + +if (!pmu->is_core || !pmu->probed.can_sample) +return ; + +if (page_sz <=0 ) + page_sz =4096 ; + data_sz =4 * (size_t )page_sz; + mmap_sz = (size_t )page_sz + data_sz; + +memset (&br_attr,0 ,sizeof (br_attr)); +snprintf (br_buf,sizeof (br_buf),"%s/branches/" , pmu->name); +if (!parse_candidate_attr (br_buf, &br_attr)) { + br_attr.type =PERF_TYPE_HARDWARE ; + br_attr.size = base_attr->size; + br_attr.config =PERF_COUNT_HW_BRANCH_INSTRUCTIONS ; +if (perf_pmus__supports_extended_type ()) + br_attr.config |= (__u64 )pmu->type <<PERF_PMU_TYPE_SHIFT ; + } + br_attr.exclude_user = base_attr->exclude_user; + br_attr.exclude_kernel =1 ; + br_attr.exclude_hv =1 ; + br_attr.exclude_idle = base_attr->exclude_idle; + br_attr.exclude_host = base_attr->exclude_host; + br_attr.exclude_guest = base_attr->exclude_guest; + +if (pmu_cpu >=0 && pmu_cpu <CPU_SETSIZE && +sched_getaffinity (0 ,sizeof (old_mask), &old_mask) ==0 ) { +CPU_ZERO (&mask); +CPU_SET (pmu_cpu, &mask); +if (sched_setaffinity (0 ,sizeof (mask), &mask) ==0 ) + affinity_changed = true; + } + +for (int lvl =0 ; lvl <=3 ; lvl++) { +struct perf_event_attr attr = br_attr; +struct perf_event_mmap_page *pc; +uintptr_t s_ip =0 , b_ip =0 , e_ip =0 ; +int insn_sz =0 , nop_cnt =0 ; +void *ring; +u8 *data; +u64 head, tail =0 ; +int samples =0 , exact =0 ; +int min_skid =999999 , max_skid =0 ; +long sum_skid =0 ; +int fd; + +if (lvl >0 && !(pmu->probed.precise_mask & (1u << lvl))) +continue ; + + attr.disabled =1 ; + attr.sample_period =251 ; + attr.sample_type =PERF_SAMPLE_IP ; + attr.precise_ip = lvl; + + fd =sys_perf_event_open (&attr,0 ,-1 ,-1 , flags); +if (fd <0 && lvl >0 && attr.exclude_kernel && attr.config2 ==0 ) { + attr.config2 =1 ; + fd =sys_perf_event_open (&attr,0 ,-1 ,-1 , flags); + } +if (fd <0 ) +continue ; + + ring =mmap (NULL , mmap_sz,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); +if (ring ==MAP_FAILED ) { +close (fd); +continue ; + } + +ioctl (fd,PERF_EVENT_IOC_RESET ,0 ); +ioctl (fd,PERF_EVENT_IOC_ENABLE ,0 ); +if (!run_skid_branch_loop (8000 , &s_ip, &b_ip, &e_ip, + &insn_sz, &nop_cnt)) { +ioctl (fd,PERF_EVENT_IOC_DISABLE ,0 ); +munmap (ring, mmap_sz); +close (fd); +break ; + } +ioctl (fd,PERF_EVENT_IOC_DISABLE ,0 ); + + pc = ring; + head =READ_ONCE (pc->data_head); +/* Ensure data_head is read before reading ring buffer contents. */ +rmb (); +if (head > data_sz) + head = data_sz; + data = (u8 *)ring + page_sz; + +while (tail +sizeof (struct perf_event_header ) <= head) { +struct perf_event_header *hdr = + (struct perf_event_header *)(data + tail); + +if (hdr->size ==0 || tail + hdr->size > head) +break ; +if (hdr->type ==PERF_RECORD_SAMPLE && + hdr->size >=sizeof (*hdr) +sizeof (u64)) { +u64 ip = *(u64 *)((u8 *)hdr +sizeof (*hdr)); +uintptr_t uip = (uintptr_t )ip; + +if (uip >= s_ip && uip <= e_ip && insn_sz >0 ) { +int skid; + +if (uip == b_ip) { + skid =0 ; + }else if (uip >= s_ip && uip < b_ip) { +long off = (long )(uip - s_ip) / insn_sz; + + skid = (off < nop_cnt) ? + (int )(off +1 ) : (nop_cnt +1 ); + }else { + skid =1 ; + } + + samples++; +if (hdr->misc &PERF_RECORD_MISC_EXACT_IP ) + exact++; +if (skid < min_skid) + min_skid = skid; +if (skid > max_skid) + max_skid = skid; + sum_skid += skid; + } + } + tail += hdr->size; + } + +if (samples >0 ) { + pmu->probed.skid[lvl].measured = true; + pmu->probed.skid[lvl].samples = samples; + pmu->probed.skid[lvl].exact_samples = exact; + pmu->probed.skid[lvl].min_skid = min_skid; + pmu->probed.skid[lvl].max_skid = max_skid; + pmu->probed.skid[lvl].avg_skid = (double )sum_skid / samples; + } + +munmap (ring, mmap_sz); +close (fd); + } + +if (affinity_changed) +sched_setaffinity (0 ,sizeof (old_mask), &old_mask); +} +void perf_pmu__probe (struct perf_pmu *pmu) {struct probe_candidates cands = { .nr =0 };
@@ -3696,7 +4429,29 @@ void perf_pmu__probe(struct perf_pmu *pmu) } } +probe_pmu_skid (pmu, &base_attr, pmu_cpu, flags); + } + +/* 3b. User and interrupt register sampling masks */ +if (pmu->probed.sample_types &PERF_SAMPLE_REGS_USER ) { +probe_pmu_regs_one (pmu, &base_attr, pid, cpu, flags, + pmu->probed.max_precise,/*intr=*/ false, + &pmu->probed.user_regs_mask, + &pmu->probed.user_regs_abi, + &pmu->probed.user_simd_mask, + &pmu->probed.user_pred_mask); } +if (pmu->probed.sample_types &PERF_SAMPLE_REGS_INTR ) { +probe_pmu_regs_one (pmu, &base_attr, pid, cpu, flags, + pmu->probed.max_precise,/*intr=*/ true, + &pmu->probed.intr_regs_mask, + &pmu->probed.intr_regs_abi, + &pmu->probed.intr_simd_mask, + &pmu->probed.intr_pred_mask); + } + +/* 3c. Memory event types and memory sample formats */ +probe_mem_events (pmu, &base_attr, pid, cpu, flags);/* 4. Privilege / exclusion modifiers */ pmu->probed.exclude_user =
@@ -3861,6 +4616,9 @@ void perf_pmu__probe(struct perf_pmu *pmu) } } +/* 5b. User-space mmap capabilities (rdpmc, user_time, pmc_width) */ +probe_mmap_caps (pmu, &base_attr, pmu_cpu, pid, cpu, flags); +/* 5c. Auto-counter reload (acr_mask / ratio-to-prev) */ if (pmu->probed.can_sample &&perf_pmu__has_format (pmu,"acr_mask" )) {struct perf_event_attr l_attr = base_attr;
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 6/9] perf pmu: Probe registers, memory events, rdpmc, and branch skid
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf pmu: 64-bit operand in rv32 asm constraint in run_skid_branch_loop() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf pmu: 64-bit operand in rv32 asm constraint in run_skid_branch_loop() --
commit 1d20e4dd9d51fee9a24330f6bf13bda0cca94b25
Author: Ian Rogers <[email protected]>
perf pmu: Probe registers, memory events, rdpmc, and branch skid
This commit extends perf_pmu__probe() in tools/perf/util/pmu.c to probe supported user and interrupt sampling register masks, memory profiling events and sample formats, user-space mmap counter reading (rdpmc) and PMC bit-width, and empirically measured branch instruction skid across precision levels.
Quoted 5 lines
tools/perf/util/pmu.c +0
-0@@ -3356,6 +3356,739 @@ static void probe_sample_types(struct perf_pmu *pmu,
[ ... ]
Quoted 5 lines
tools/perf/util/pmu.c +5
-0+static noinline bool run_skid_branch_loop(u64 iters, uintptr_t *out_s_ip, +uintptr_t *out_b_ip, +uintptr_t *out_e_ip, +int *out_insn_sz,int *out_nop_cnt) +{
[ ... ]
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+#elif defined(__riscv) +uintptr_t s_ip, b_ip, e_ip; + + asmvolatile ( +".option push\n\t" +".option norvc\n\t" +"la %[s_ip], 1f\n\t" +"la %[b_ip], 2f\n\t" +"la %[e_ip], 3f\n\t" + ".balign 64\n"
tools/perf/util/pmu.c static void probe_sample_types(struct perf_pmu *pmu, · line 3916
Quoted 10 lines
tools/perf/util/pmu.c +10
-0+"1:\n\t" +".rept 512\n\t" +"nop\n\t" +".endr\n\t" +"addi %[cnt], %[cnt], -1\n\t" +"2:\n\t" +"bnez %[cnt], 1b\n" +"3:\n\t" +".option pop\n\t" + : [cnt] "+r" (iters),
[Severity: Low]
Will this fail to build on 32-bit RISC-V (__riscv_xlen == 32) when compiling with Clang?
In run_skid_branch_loop(), the loop counter parameter iters is declared as a 64-bit u64. Unlike the x86 and ARM blocks which are guarded by 64-bit target macros (__x86_64__ and __aarch64__), #elif defined(__riscv) matches both 32-bit and 64-bit RISC-V targets.
On 32-bit RISC-V, general-purpose registers are 32 bits wide, so binding the 64-bit u64 variable iters to the single-register constraint "+r" causes Clang to reject the inline assembly with a register size mismatch error.
Should iters be changed to unsigned long (or uintptr_t), or should the preprocessor guard check defined(__riscv) && __riscv_xlen == 64?
Quoted 6 lines
tools/perf/util/pmu.c +6
-0+ [s_ip] "=&r" (s_ip), + [b_ip] "=&r" (b_ip), + [e_ip] "=&r" (e_ip) + : + : "memory" + );
[ ... ]
Quoted 1 line
+}
[ ... ]
Quoted 1 line
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerRFC PATCH v2 7/9 perf sysinfo: Add sysinfo subcommand
In reply to Ian RogersAdd the 'perf sysinfo' subcommand to display and probe PMUs and system performance monitoring capabilities on the running system or from a perf.data file.
By default, 'perf sysinfo' reports a system-wide summary of core PMU features (precise sampling, branch stack sampling, user-space counter reading, leader sampling, memory profiling events, and hardware AUX trace PMUs), memory and PCIe/IO interconnect bus bandwidth monitoring, total events and metrics, pipeline metric drill-down levels, and an overall system rating.
When '-p'/'--pmus' (or a PMU glob filter, '-v'/'--verbose', '-a'/'--all', or '-j'/'--json') is specified, it also reports per-PMU static attributes and live probed capabilities in text or JSON format.
--- tools/perf/Build | 1 + tools/perf/builtin-help.c | 3 +- tools/perf/builtin-sysinfo.c | 2010 ++++++++++++++++++++++++++++++++++ tools/perf/builtin.h | 1 + tools/perf/perf.c | 1 + 5 files changed, 2015 insertions(+), 1 deletion(-) create mode 100644 tools/perf/builtin-sysinfo.c
tools/perf/Build +1 -0
@@ -13,6 +13,7 @@ perf-y += builtin-list.o perf-y += builtin-record.o perf-y += builtin-report.o perf-y += builtin-stat.o +perf-y += builtin-sysinfo.o perf-y += builtin-top.o perf-y += builtin-script.o perf-y += builtin-kvm.o
tools/perf/builtin-help.c +2 -1
@@ -329,6 +329,7 @@ void list_common_cmds_help(void) {"report", "Read perf.data (created by perf record) and display the profile"}, {"script", "Read perf.data (created by perf record) and display trace output"}, {"stat", "Run a command and gather performance counter statistics"}, + {"sysinfo", "Display and probe PMU and system performance monitoring information"}, {"test", "Runs sanity tests."}, {"top", "System profiling tool."}, {"version", "display the version of perf binary"},
@@ -485,7 +486,7 @@ int cmd_help(int argc, const char **argv) };const char *const builtin_help_subcommands[] = {"buildid-cache" ,"buildid-list" ,"diff" ,"evlist" ,"help" ,"list" , -"record" ,"report" ,"bench" ,"stat" ,"timechart" ,"top" ,"annotate" , +"record" ,"report" ,"bench" ,"stat" ,"sysinfo" ,"timechart" ,"top" ,"annotate" ,"script" ,"sched" ,"kallsyms" ,"kmem" ,"lock" ,"kvm" ,"test" ,"inject" ,"mem" ,"data" , #ifdef HAVE_LIBELF_SUPPORT "probe",
tools/perf/builtin-sysinfo.c new file +2010 -0
2,010 lines not on this page · raw message
tools/perf/builtin.h +1 -0
@@ -37,6 +37,7 @@ int cmd_list(int argc, const char **argv);int cmd_record (int argc,const char **argv);int cmd_report (int argc,const char **argv);int cmd_stat (int argc,const char **argv); +int cmd_sysinfo (int argc,const char **argv);int cmd_timechart (int argc,const char **argv);int cmd_top (int argc,const char **argv);int cmd_script (int argc,const char **argv);
tools/perf/perf.c +1 -0
@@ -112,6 +112,7 @@ static const struct cmd_struct commands[] = { { "report", cmd_report, 0 }, { "bench", cmd_bench, 0 }, { "stat", cmd_stat, 0 }, + { "sysinfo", cmd_sysinfo, 0 }, { "timechart", cmd_timechart, 0 }, { "top", cmd_top, 0 }, { "annotate", cmd_annotate, 0 },
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 7/9] perf sysinfo: Add sysinfo subcommand
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf sysinfo: non-first numbered PMU instances skipped in cmd_sysinfo() filter - [Medium] perf…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf sysinfo: non-first numbered PMU instances skipped in cmd_sysinfo() filter
- [Medium] perf sysinfo: unchecked open_supported in print_system_summary() --
commit e12f0b6dfa1f94c66afcbf3ca5d231990fbac509
Author: Ian Rogers <[email protected]>
perf sysinfo: Add sysinfo subcommand
This commit adds the perf sysinfo subcommand to display and probe PMUs and system performance monitoring capabilities on the running system or from a perf.data file.
Quoted 6 lines
tools/perf/builtin-sysinfo.c new file +0
-0@@ -0,0 +1,2010 @@
[ ... ]
Quoted 3 lines
tools/perf/builtin-sysinfo.c new file +3
-0+static void print_system_summary (FILE *fp,const struct perf_session *session, +bool probed,bool leading_newline) +{
[ ... ]
Quoted 36 lines
tools/perf/builtin-sysinfo.c new file +36
-0+if (pmu->is_core) { +struct perf_pmu_caps *br_cap; + +add_pmu_display_name (core_pmus, pmu); +perf_pmu__caps_parse (pmu); + br_cap =perf_pmu__get_cap (pmu,"branches" ); +if (br_cap && br_cap->value) { +int depth =atoi (br_cap->value); + +if (depth > max_br_depth) + max_br_depth = depth; + } +if (probed && pmu->probed.checked && pmu->probed.open_supported) { +if (pmu->probed.can_sample) + core_can_sample = true; +if (pmu->probed.max_precise >0 ) { + core_precise = true; +if (pmu->probed.max_precise > max_precise) + max_precise = pmu->probed.max_precise; + } +if (pmu->probed.can_user_rdpmc) + core_rdpmc = true; +if (pmu->probed.can_branch_stack) + core_branch_stack = true; +if (pmu->probed.can_leader_sampling) + core_leader_sampling = true; + }else { + core_can_sample = true; +if (pmu->max_precise >0 ) { + core_precise = true; +if (pmu->max_precise > max_precise) + max_precise = pmu->max_precise; + } +if (max_br_depth >0 ) + core_branch_stack = true; + }
[ ... ]
Quoted 15 lines
tools/perf/builtin-sysinfo.c new file +15
-0+if (probed) { +fprintf (fp," User-space counter reading (rdpmc): " ); +color_fprintf (fp, core_rdpmc ?PERF_COLOR_GREEN :PERF_COLOR_YELLOW , +"%s" , core_rdpmc ?"supported" :"missing/disabled" ); +fputc ('\n' , fp); +if (!core_rdpmc) +strlist__add (missing,"user-space counter reading (rdpmc)" ); + +fprintf (fp," Leader sampling: " ); +color_fprintf (fp, + core_leader_sampling ?PERF_COLOR_GREEN :PERF_COLOR_RED , +"%s" , + core_leader_sampling ?"supported" :"missing" ); +fputc ('\n' , fp); + }
[ ... ]
tools/perf/builtin-sysinfo.c line 1833
Quoted 10 lines
tools/perf/builtin-sysinfo.c new file +10
-0+color_fprintf (fp,PERF_COLOR_BOLD ," System rating:" ); +if (nr_core ==0 ) { +const char *min_desc = +"no hardware core PMU; profiling limited to software timer events" ; + +fprintf (fp," " ); +color_fprintf (fp,PERF_COLOR_RED ,"Minimal" ); +fprintf (fp," (%s)\n" , min_desc); + }else if (core_precise && core_branch_stack && (!probed || core_rdpmc) && + !strlist__empty (mem_bw_pmus) && max_drill_lvl >=2 ) {
[Severity: Medium]
What happens in print_system_summary() when probing is enabled (probed == true), but perf_event_open() fails on the core PMUs (for example, when kernel.perf_event_paranoid > 2 or in an unprivileged container)?
In perf_pmu__probe(), failure to open any candidate event sets pmu->probed.open_supported = false and returns early without probing can_user_rdpmc or can_leader_sampling.
In the PMU scan loop above, print_system_summary() checks pmu->probed.open_supported and falls back to static sysfs properties for core_can_sample, core_precise, core_branch_stack, and core_mem_events, leaving core_rdpmc and core_leader_sampling false.
However, the summary output and rating checks here only test probed rather than whether any core PMU had open_supported == true. Should this track whether core PMU probing actually succeeded so that rdpmc and leader sampling are not reported as missing (and the system rating is not downgraded) when perf_event_open() is unavailable?
[ ... ]
Quoted 2 lines
tools/perf/builtin-sysinfo.c new file +2
-0+int cmd_sysinfo (int argc,const char **argv) +{
[ ... ]
tools/perf/builtin-sysinfo.c line 1973
Quoted 4 lines
tools/perf/builtin-sysinfo.c new file +4
-0+while ((pmu = show_all ?perf_pmus__scan (pmu) : +perf_pmus__scan_skip_duplicates (pmu)) !=NULL ) { +if (!pmu_matches_filters (pmu, argc, argv)) +continue ;
[Severity: Medium]
When a user passes a filter argument matching a non-first numbered uncore PMU instance (such as uncore_imc_1 or "*_1") without specifying -a/--all or -v/--verbose, will cmd_sysinfo() fail to match it?
When show_all is false, perf_pmus__scan_skip_duplicates() visits only the first instance (e.g., uncore_imc_0) and skips all subsequent instances sharing the same base prefix before pmu_matches_filters() can inspect their pmu->name:
tools/perf/builtin-sysinfo.c:pmu_matches_filters() {
...
for (int i = 0; i < argc; i++) {
const char *pat = argv[i];
if (perf_pmu__wildcard_match(pmu, pat))
return true;
if (pmu->name && (strglobmatch(pmu->name, pat) || !strcmp(pmu->name, pat)))
return true;
...
}As a result, running perf sysinfo uncore_imc_0 matches and prints the collapsed PMU group, whereas perf sysinfo uncore_imc_1 produces no output.
Quoted 5 lines
tools/perf/builtin-sysinfo.c new file +5
-0+ +if (do_probe && !input_name && + (show_pmus || pmu->is_core || pmu->mem_events || + pmu->type ==PERF_TYPE_SOFTWARE )) +perf_pmu__probe (pmu);
[ ... ]
Quoted 1 line
+}
[ ... ]
完美结束,确保最后有一个空行。]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerRFC PATCH v2 8/9 perf docs: Add perf-sysinfo man page
In reply to Ian RogersDocument the 'perf sysinfo' subcommand and its options in Documentation/perf-sysinfo.txt and link it from Documentation/perf.txt.
--- tools/perf/Documentation/perf-sysinfo.txt | 109 ++++++++++++++++++++++ tools/perf/Documentation/perf.txt | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tools/perf/Documentation/perf-sysinfo.txt
tools/perf/Documentation/perf-sysinfo.txt new file +109 -0
@@ -0,0 +1,109 @@ +perf-sysinfo(1) +=============== + +NAME +---- +perf-sysinfo - Display and probe PMU and system performance monitoring information + +SYNOPSIS +-------- +[verse] +'perf sysinfo' [<options>] [pmu_glob ...] + +DESCRIPTION +----------- +This command displays a summary of the performance monitoring capabilities of +the current system or a perf.data file, including core PMU features (precise +sampling, branch stack sampling, user-space counter reading, memory profiling, +and hardware AUX trace PMUs), memory and PCIe/IO interconnect bus bandwidth +monitoring support, total events and metrics, pipeline metric drill-down levels +discovered via '<metric>_group' hierarchies, and an overall system rating. + +When '-p'/'--pmus' is specified (or when PMU filter globs, '-v'/'--verbose', +'-a'/'--all', or '-j'/'--json' are given), it also displays detailed +information for each PMU, including its perf_event_attr type, kind (core, +uncore, software, auxtrace, tool, hwmon, drm), CPUs/cpumask, capabilities, +sysfs format bitfields, event counts, and memory profiling events. + +When run on a live system (without '-i'/'--input' and with probing enabled), +it also actively probes the kernel via 'perf_event_open' to determine: +- Supported modes (counting, interrupt-based sampling, branch_stack sampling) +- Supported precise sampling levels, skid semantics, and empirically measured + branch instruction skid (minimum, maximum, average instructions from the + branch and 'PERF_RECORD_MISC_EXACT_IP' ratio) at each supported precision + level: + 0 ('SAMPLE_IP' can have arbitrary skid), ':p' / 1 ('SAMPLE_IP' must have + constant skid), ':pp' / 2 ('SAMPLE_IP' requested to have 0 skid), and + ':ppp' / 3 ('SAMPLE_IP' must have 0 skid, or uses randomization to avoid + sample shadowing effects) +- Supported 'perf_event_sample_format' sample types ('ip', 'tid', 'time', + 'addr', 'read', 'callchain', 'id', 'cpu', 'period', 'stream_id', 'raw', + 'branch_stack', 'regs_user', 'stack_user', 'weight', 'data_src', + 'identifier', 'transaction', 'regs_intr', 'phys_addr', 'aux', 'cgroup', + 'data_page_size', 'code_page_size', 'weight_struct') +- Supported user and interrupt registers for 'PERF_SAMPLE_REGS_USER' and + 'PERF_SAMPLE_REGS_INTR' +- Supported memory profiling events ('ldlat-loads', 'ldlat-stores', 'mem-ldst') + and their supported memory sample types ('data_src', 'addr', 'weight', etc.) +- Supported privilege/exclusion modifiers ('exclude_user', 'exclude_kernel', + 'exclude_hv', 'exclude_idle', 'exclude_host', 'exclude_guest') +- Supported execution features ('leader_sampling', 'sw_grouping', 'inherit', + 'sample_freq', 'write_backward', 'callchain', 'data_src', 'weight', + 'pinned', 'exclusive', 'format_lost', 'auto_counter_reload', + 'aux_start_paused', 'aux_pause', 'aux_resume', 'aux_sample', 'user_rdpmc', + 'user_time', 'user_time_zero', 'user_time_short') and hardware counter + bit-width ('pmc_width') when user-space counter reading is supported +- Maximum event group size accepted by 'perf_event_open' and maximum + simultaneously active counters in a group without multiplexing + +At the end of the text output, a summary section reports overall core PMU +capabilities (including hardware AUX trace PMUs), memory and PCIe/IO +interconnect bus bandwidth monitoring support, total events and metrics, and +pipeline metric drill-down levels discovered via '<metric>_group' hierarchies. + +By default, numbered uncore PMU instances sharing a common prefix are collapsed +into a single entry showing the instance count, unless '-v'/'--verbose' is +specified. + +OPTIONS +------- +-i:: +--input=:: + Input file name to read PMU information from (e.g. perf.data). + +-f:: +--force:: + Don't complain, do it (when reading input perf.data file). + +-o:: +--output=:: + Output file name. + +-p:: +--pmus:: + Display detailed information for each PMU in addition to the system + summary. + +-j:: +--json:: + Encode PMU information as JSON. + +-a:: +--all:: +-v:: +--verbose:: + Display all PMUs including all similar PMUs with alphanumeric suffixes + instead of collapsing numbered uncore PMU instances. + +--probe:: +--no-probe:: + Enable or disable active kernel probing of PMU capabilities on the live + system (default: enabled). + +--debug:: + Enable debugging output. + +SEE ALSO +-------- +linkperf:perf-list[1], linkperf:perf-evlist[1], linkperf:perf-stat[1], +linkperf:perf-record[1]
tools/perf/Documentation/perf.txt +1 -1
@@ -92,5 +92,5 @@ linkperf:perf-help[1], linkperf:perf-inject[1], linkperf:perf-intel-pt[1], linkperf:perf-iostat[1], linkperf:perf-kallsyms[1], linkperf:perf-kmem[1], linkperf:perf-kvm[1], linkperf:perf-lock[1], linkperf:perf-mem[1], linkperf:perf-probe[1], linkperf:perf-sched[1], -linkperf:perf-script[1], linkperf:perf-test[1], +linkperf:perf-script[1], linkperf:perf-sysinfo[1], linkperf:perf-test[1], linkperf:perf-trace[1], linkperf:perf-version[1]
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 8/9] perf docs: Add perf-sysinfo man page
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines
Document the 'perf sysinfo' subcommand and its options in
Documentation/perf-sysinfo.txtand link it fromDocumentation/perf.txt.Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerRFC PATCH v2 9/9 perf test: Add perf sysinfo shell test
In reply to Ian RogersAdd a shell test exercising default 'perf sysinfo' summary output, '--pmus' per-PMU output, '-j' JSON validation, PMU glob filtering, and '-i perf.data' inspection.
--- tools/perf/tests/shell/sysinfo.sh | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 tools/perf/tests/shell/sysinfo.sh
tools/perf/tests/shell/sysinfo.sh new file +71 -0
@@ -0,0 +1,71 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf sysinfo tests + +set -e + +shelldir=$(dirname "$0") +. "${shelldir}"/lib/setup_python.sh + +sysinfo_output=$(mktemp /tmp/__perf_test.sysinfo_output.XXXXX) +perf_data=$(mktemp /tmp/__perf_test.sysinfo.perf.data.XXXXX) + +cleanup() { + rm -f "${sysinfo_output}" "${perf_data}" "${perf_data}.old" + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +test_sysinfo_default() { + echo "Default perf sysinfo output test" + perf sysinfo -o "${sysinfo_output}" + grep -q "^Summary:" "${sysinfo_output}" + grep -q "System rating:" "${sysinfo_output}" + perf sysinfo --pmus -o "${sysinfo_output}" + grep -q "^software (" "${sysinfo_output}" + grep -q "^Summary:" "${sysinfo_output}" + echo "Default perf sysinfo output test [Success]" +} + +test_sysinfo_json() { + echo "Json perf sysinfo output test" + perf sysinfo -j -o "${sysinfo_output}" + $PYTHON -m json.tool "${sysinfo_output}" /dev/null + echo "Json perf sysinfo output test [Success]" +} + +test_sysinfo_filter() { + echo "Filtered perf sysinfo output test" + perf sysinfo -o "${sysinfo_output}" software + grep -q "^software (" "${sysinfo_output}" + grep -q "Sample types:.*ip" "${sysinfo_output}" + grep -q "User registers:" "${sysinfo_output}" + grep -q "Intr registers:" "${sysinfo_output}" + grep -q "Features:.*leader_sampling" "${sysinfo_output}" + grep -q "Features:.*sw_grouping" "${sysinfo_output}" + echo "Filtered perf sysinfo output test [Success]" +} + +test_sysinfo_perf_data() { + echo "perf.data perf sysinfo test" + perf record -e software/cpu-clock/u -o "${perf_data}" -- true 2>/dev/null + perf sysinfo -i "${perf_data}" -o "${sysinfo_output}" + grep -q "^Summary:" "${sysinfo_output}" + perf sysinfo --pmus -i "${perf_data}" -o "${sysinfo_output}" + grep -q "software" "${sysinfo_output}" + perf sysinfo -i "${perf_data}" -j -o "${sysinfo_output}" + $PYTHON -m json.tool "${sysinfo_output}" /dev/null + echo "perf.data perf sysinfo test [Success]" +} + +test_sysinfo_default +test_sysinfo_json +test_sysinfo_filter +test_sysinfo_perf_data +cleanup +exit 0
Signature · 1 line
2.56.0.212.g129ede20d9-goog
[email protected]
AI reviewRe: [RFC PATCH v2 9/9] perf test: Add perf sysinfo shell test
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Add a shell test exercising default 'perf sysinfo' summary output, '--pmus' per-PMU output, '-j' JSON validation, PMU glob filtering, and '-i perf.data' inspection.
Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9