Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
	 Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 00/11] perf pmu: Layered sysfs-style PMU event and metric directories
Date: Thu,  3 Dec 2026 17:10:17 -0800	[thread overview]
Message-ID: <20261204011029.1946921-1-irogers@google.com> (raw)

Currently perf resolves PMU event aliases from two hardcoded sources
(/sys/bus/event_source/devices/<pmu>/events/ and compiled-in jevents.py
C tables) while metricgroup.c reads metrics and metricgroup descriptions
directly from compiled-in pmu_metrics_table arrays without associating
them with struct perf_pmu. As a result:

1. Adding, overriding, or testing PMU event encodings, descriptions, or
   metrics requires editing JSON files and recompiling perf.
2. Single-event parsing ('perf stat -e cycles true') eagerly runs
   readdir() across /sys/bus/event_source/devices/<pmu>/events/ rather
   than probing <pmu>/events/<event> on demand via openat().
3. Metrics cannot be overridden or supplied via runtime directory trees
   alongside PMU events.

Building on the block-indexed jevents string compression and per-table
metricgroup descriptions in perf-tools-next, this series unifies PMU
events, metrics, and metricgroup descriptions behind a stateless,
openat()-driven layered VFS stack on struct perf_pmu, configurable via
pmu-events.dir:

  Highest:  1. User directory layers (pmu-events.dir, left-to-right)
            2. Compiled-in CPUID / sys JSON tables (libpmu-events)
            3. Kernel sysfs (/sys/bus/event_source/devices/)
  Lowest:   4. Compiled-in default_core / common JSON tables

This preserves the existing util/pmu.c precedence: CPUID- and identifier-
matched JSON events override sysfs encodings (allowing architectures such
as RISC-V to map legacy event names to CPU-specific encodings in JSON
without bloating the kernel PMU driver), while sysfs events (such as
/sys/bus/event_source/devices/<pmu>/events/cycles) override generic
legacy-hardware and legacy-cache fallback encodings in common JSON
(which remain available in alias->legacy_terms).

Lazy Single-Event Probing Performance
-------------------------------------

Measured on a hybrid Intel Core i7-14700 (31 sysfs PMUs; 100 interleaved
rounds pinned to CPU 0 against perf-tools-next 22f7d78dc87e):

  Workload                         events/    sysfs ev   Total I/O    Wall med
                                   getdents   opens      syscalls     (delta)
  -----------------------------------------------------------------------------
  perf stat -e cpu_core/cycles/     2 -> 0    52 -> 14   524 -> 432   -1.67 ms
  perf stat -e cpu_core/cpu-cycles/ 2 -> 0    56 -> 17   528 -> 437   -1.55 ms
  perf stat -e instructions         4 -> 0   106 -> 61  1190 -> 1056  -1.64 ms
  perf stat -e cycles               2 -> 0    52 -> 55  1099 -> 1046  +0.02 ms
  perf stat true                    4 -> 0   170 -> 629* 1850 -> 1843  -0.36 ms*
  perf list                        19 -> 19  140 -> 117 4746 -> 4809  -3.72 ms

  (* On 'perf stat true', baseline also ran 491 access() probes vs 73
  after caching missing <pmu>/events/ directories via sysfs_aliases_loaded;
  delta shown for 'perf stat true' is median user time, with wall median
  62.72 -> 68.73 ms inside ±27-41 ms fork/event-open jitter and user+sys
  +0.93 ms.)

Single-event 'perf stat -e cpu_core/cycles/' performs zero getdents64()
scans on events/ directories and cuts sysfs event file opens by 73%
(52 -> 14, probing only mem-loads-aux, cycles, and slots rather than
reading all 20 events and sidecars in cpu_core/events/), saving 92 file
I/O syscalls and ~1.6 ms median wall time.

User Directory Layout and CPUID Resolution
------------------------------------------

Each directory <root> in colon-separated pmu-events.dir supports both
portable multi-CPU trees (keyed by literal CPUID or uncore identifier
with POSIX symlinks) and host-direct PMU overrides, with no runtime
mapfile.csv or regex engine:

  <root>/
  |-- cpus/
  |   |-- GenuineIntel-6-4E -> ../models/x86/skylake
  |   |-- GenuineIntel-6-5E -> ../models/x86/skylake
  |   |-- GenuineIntel-6-8F -> ../models/x86/sapphirerapids
  |   |-- GenuineIntel-6-8F-8/          # stepping-specific override
  |   |   `-- cpu/events/inst_retired.any.desc
  |   |-- 0x00000000410fd490 -> ../models/arm64/arm/neoverse-n2-v2
  |   `-- 0x489-0x8000000000000007 -> ../models/riscv/sifive/bullet
  |-- sys/
  |   `-- 0x00000030 -> ../models/arm64/hisilicon/hip09/sys
  |-- models/
  |   |-- x86/skylake/{default_core,uncore_imc,...}/{events,metrics}/...
  |   `-- arm64/arm/neoverse-n2-v2/default_core/{events,metrics}/...
  |-- default_core/metrics/my_ipc{,.desc}
  |-- <pmu>/{events,metrics}/...
  `-- metricgroups/<group>.desc

Within each <root>, perf probes three scopes top-down:

1. <root>/cpus/<cpuid>/:
   Queries each PMU's CPUID (using perf_cpu_map__min(pmu->cpus) so
   hybrid PMUs such as cpu_core and cpu_atom use their own CPU's CPUID,
   caching per-PMU CPUIDs while honoring PERF_CPUID). perf probes the
   exact CPUID first and then deterministic base CPUIDs stripped of
   stepping or revision fields:
   - x86 ('-' delimited):    GenuineIntel-6-8F-8 -> GenuineIntel-6-8F
   - s390 (',' delimited):   IBM,8561,703        -> IBM,8561
   - RISC-V ('-' delimited): 0x489-0x8000000000000007-0x1
                               -> 0x489-0x8000000000000007
   - ARM64 64-bit hex MIDR:  0x00000000411fd493  -> 0x00000000410fd490
                             (Variant[23:20] and Revision[3:0] zeroed)
   - PowerPC 32-bit hex PVR: 0x00800200          -> 0x00800000
                             (Revision[15:0] zeroed)
   Each <root> stops at the first matching cpus/<candidate> directory
   so a stepping override (e.g. cpus/GenuineIntel-6-8F-8/) does not
   accidentally merge coarser model events from the same <root>, while
   unset sidecar attributes and unoverridden events still merge from
   subsequent directories in pmu-events.dir (for example
   ~/.perf-events/local:/opt/pmu-events), compiled-in JSON, and sysfs.
   Because openat() traverses POSIX symlinks in the kernel VFS, multiple
   CPUIDs sharing a microarchitecture point to a single canonical
   models/<arch>/<model>/ directory.
2. <root>/sys/<id>/:
   Matches pmu->id from /sys/bus/event_source/devices/<pmu>/identifier.
3. <root>/ (host-direct and cross-CPU fallback):
   Matches <pmu> (exact name, alias_name, default_core for core PMUs,
   or suffix-stripped uncore prefix such as uncore_imc) plus
   <root>/metricgroups/<group>.desc.

Exporters materialize both layouts:
- 'perf list --export-dir <dir>' writes the running machine's active
  PMUs into host-direct <dir>/<pmu>/{events,metrics}/ for quick local
  edits.
- 'perf list --export-dir <dir> --export-cpuid <cpuid|arch[/model]|all>'
  exports compiled-in tables for any CPUID, model, or architecture (or
  'all') directly from the perf binary into <dir>/models/<arch>/<model>/
  with relative <dir>/cpus/<cpuid> and <dir>/sys/<id> symlinks (for
  example, to inspect or migrate metrics across architectures/models).
- 'jevents.py --output-dir <dir>' exports unbuilt kernel source JSON
  trees into the same <dir>/models/ + <dir>/cpus/ + <dir>/sys/ layout
  (also usable on NO_JEVENTS=1 setups).

Key changes across the series:
- Lazy sysfs probing and struct perf_pmu metric storage (Patches 1-2):
  - Patch 1 generalizes pmu_alias_info_file() to probe single event
    files and sidecars on demand via openat() before falling back to
    readdir(), keeping struct perf_pmu_alias private out of pmu.h.
  - Patch 2 adds metrics and metricgroups hashmaps on struct perf_pmu
    while keeping struct perf_pmu_metric and struct perf_pmu_metricgroup
    private out of pmu.h (so tool, drm, hwmon, and tp PMUs are
    unaffected).
- Stateless struct pmu_layer VFS and pmu-events.dir (Patches 3-6):
  - Patch 3 introduces struct pmu_layer (sharing internal alias/metric
    representations privately between pmu.c and pmu-layer.c via
    pmu-layer.h), adds PMU-filtered pmu_metrics_table__for_each_pmu_metric()
    in jevents.py to avoid decompressing non-matching PMU metrics, and
    wraps compiled-in CPU/sys and common pmu-events tables as the builtin
    layers using per-table pmu_metrics_table__describe_metricgroup().
  - Patch 4 implements the openat() directory layer supporting extended
    event and metric sidecars, %2F escaping for '/' in names, literal
    cpus/<cpuid> and sys/<id> POSIX symlink lookup, per-PMU CPUID
    caching, and direct single-event sysfs open()/read() probing.
  - Patch 5 adds the colon-separated pmu-events.dir perf config option
    (supporting -c pmu-events.dir=..., ~/.perfconfig, and PERF_CONFIG).
  - Patch 6 switches util/pmu.c alias resolution and enumeration over to
    pmu-layer.c, removing ~600 lines of legacy merge plumbing.
- PMU-routed metrics, exporters, and tests (Patches 7-11):
  - Patch 7 routes metricgroup.c, print-events.c, builtin-list.c,
    builtin-script.c, and util/python.c metric and metricgroup lookups
    through struct perf_pmu and perf_pmus helpers using public struct
    pmu_metric.
  - Patch 8 adds 'perf list --export-dir <dir>' (host PMUs) and
    'jevents.py --output-dir <dir>' (unbuilt JSON trees).
  - Patches 9-10 add compiled-in model/sys table iterators in jevents.py
    and 'perf list --export-cpuid <cpuid|arch[/model]|all>' so any
    architecture or CPU model's events and metrics can be exported
    directly from the perf binary.
  - Patch 11 adds C unit tests ('Layered PMU directory overlays' in
    tests/pmu.c) and CLI shell tests (pmu_events_dir.sh) covering
    lossless JSON round-tripping, overlay priority, file deletion
    restoring lower-layer defaults, CPUID stepping symlinks,
    '--export-cpuid' cross-architecture/model export, and heterogeneous
    multi-instance PMU sub-metric resolution.

v3:
- Rebase onto latest perf-tools-next (now containing the perf jevents
  block-indexed string compression and per-table metricgroup series).
- Patch 3: Wire builtin_cpu_layer_ops.load_metricgroup and
  builtin_common_layer_ops.load_metricgroup to the new per-table
  pmu_metrics_table__describe_metricgroup() helper, add PMU-filtered
  pmu_metrics_table__for_each_pmu_metric() to skip decompressing
  non-matching PMUs' metrics, stop early on system PMU lookup matches,
  deduplicate per-table sys metricgroup regex checks with negative
  caching, and make pmu_layer_override_pe() atomic with alias pruning in
  pmu_layer__merge_event_table().
- Patch 4: Avoid opening/closing <pmu>/events/ directories on lazy
  single-event sysfs misses (caching missing events/ directories once),
  drop redundant fstat()/double-read() on sysfs files, fix canonical
  name ordering in sysfs_apply_found_event(), cache per-PMU CPUIDs
  (honoring PERF_CPUID), and stop probing case variants in
  user_dir_find_event_pmu_cb() on first match.
- Patch 8 & Patch 10: Keep per-model metricgroups/<group>.desc scoped to
  models/<arch>/<model>/metricgroups/ without duplicating into the root
  metricgroups/ directory (preventing cross-model description leakage in
  exported multi-arch trees), use pmu_metrics_table__describe_metricgroup()
  in --export-cpuid, and ensure jevents.py --output-dir returns before
  opening output_file after the argparse.FileType removal.
- Add full kernel-doc documentation across util/pmu-layer.h and the new
  APIs in util/pmu.h, util/pmus.h, and pmu-events/pmu-events.h (verified
  with scripts/kernel-doc -none -Wall).
- v2: https://lore.kernel.org/linux-perf-users/20261116070200.2972362-1-irogers@google.com/

v2:
- Rebase onto latest perf-tools-next and v2 of the perf jevents
  block-indexed string compression series (picking up NVIDIA Tegra410
  uncore PMU suffix handling and updated Zen 6 / Tegra410 JSON tables).
- v1: https://lore.kernel.org/linux-perf-users/20261113211105.3118281-1-irogers@google.com/

AI Assistance Disclosure (per Documentation/process/coding-assistants.rst
and Documentation/process/generated-content.rst):
- Tools used: Antigravity coding assistant and Sashiko automated patch
  review.
- Summary of prompts and assistance: Interactive design and
  implementation of the stateless struct pmu_layer stack, openat()
  directory layer and literal CPUID/sys symlink resolution,
  pmu-events.dir config support, struct perf_pmu metric routing and
  encapsulation, 'perf list --export-dir', 'perf list --export-cpuid',
  and 'jevents.py --output-dir' exporters, documentation, unit/shell
  tests, performance profiling, and review fixes.
- Testing performed: Built GCC (MYPY=1 PYLINT=1), CC=clang (with
  context-analysis thread-safety checks), NO_JEVENTS=1, and DEBUG=1 +
  AddressSanitizer/LeakSanitizer builds; ran perf test (including Sysfs
  PMU tests, PMU JSON event tests, Hybrid event merging, perf list,
  pmu-events.dir, and metricgroup suites), verified perf Python module
  metrics(), benchmarked lazy single-event probing, and ran
  scripts/checkpatch.pl.

Ian Rogers (11):
  perf pmu: Generalize pmu_alias_info_file() and lazy single-event sysfs
    loading
  perf pmu: Add metric and metricgroup storage on struct perf_pmu
  perf pmu-layer: Introduce stateless struct pmu_layer abstraction and
    builtin adapter
  perf pmu-layer: Add openat-based directory layer with literal
    CPUID/symlink lookup
  perf config: Add pmu-events.dir configuration option
  perf pmu: Switch alias resolution and enumeration to layered PMU stack
  perf metricgroup: Route metric and metricgroup lookups through struct
    perf_pmu
  perf list, jevents: Add --export-dir and --output-dir sysfs-style
    exporters
  perf jevents: Add compiled-in model and sys table iterators
  perf list: Add --export-cpuid to export compiled-in CPU models
  perf tests: Add round-trip export, CPUID symlink, and pmu-events.dir
    overlay tests

 tools/perf/Documentation/perf-config.txt |   52 +
 tools/perf/Documentation/perf-list.txt   |   44 +-
 tools/perf/builtin-list.c                |  366 +-
 tools/perf/builtin-script.c              |    2 +-
 tools/perf/pmu-events/README             |   19 +
 tools/perf/pmu-events/empty-pmu-events.c |   57 +-
 tools/perf/pmu-events/jevents.py         |  449 ++-
 tools/perf/pmu-events/pmu-events.h       |   65 +
 tools/perf/tests/pmu.c                   |  959 +++++
 tools/perf/tests/shell/pmu_events_dir.sh |  236 ++
 tools/perf/util/Build                    |    1 +
 tools/perf/util/config.c                 |   15 +
 tools/perf/util/metricgroup.c            |  369 +-
 tools/perf/util/pmu-layer.c              | 4194 ++++++++++++++++++++++
 tools/perf/util/pmu-layer.h              |  526 +++
 tools/perf/util/pmu.c                    |  873 ++---
 tools/perf/util/pmu.h                    |   71 +-
 tools/perf/util/pmus.c                   |  239 +-
 tools/perf/util/pmus.h                   |   74 +
 tools/perf/util/print-events.c           |   33 +-
 tools/perf/util/python.c                 |    5 +-
 21 files changed, 7899 insertions(+), 750 deletions(-)
 create mode 100755 tools/perf/tests/shell/pmu_events_dir.sh
 create mode 100644 tools/perf/util/pmu-layer.c
 create mode 100644 tools/perf/util/pmu-layer.h


base-commit: 22f7d78dc87e43ff4f7c03d23f46f49692c41ad0
-- 
2.56.1.915.g69d5ec7298-goog

             reply	other threads:[~2026-12-04  1:10 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  1:10 Ian Rogers [this message]
2026-12-04  1:10 ` [PATCH v3 01/11] perf pmu: Generalize pmu_alias_info_file() and lazy single-event sysfs loading Ian Rogers
2026-12-04  1:32   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 02/11] perf pmu: Add metric and metricgroup storage on struct perf_pmu Ian Rogers
2026-12-04  1:26   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 03/11] perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builtin adapter Ian Rogers
2026-12-04  1:35   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 04/11] perf pmu-layer: Add openat-based directory layer with literal CPUID/symlink lookup Ian Rogers
2026-12-04  1:35   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 05/11] perf config: Add pmu-events.dir configuration option Ian Rogers
2026-12-04  1:27   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 06/11] perf pmu: Switch alias resolution and enumeration to layered PMU stack Ian Rogers
2026-12-04  1:38   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 07/11] perf metricgroup: Route metric and metricgroup lookups through struct perf_pmu Ian Rogers
2026-12-04  1:40   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 08/11] perf list, jevents: Add --export-dir and --output-dir sysfs-style exporters Ian Rogers
2026-12-04  1:36   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 09/11] perf jevents: Add compiled-in model and sys table iterators Ian Rogers
2026-12-04  1:10 ` [PATCH v3 10/11] perf list: Add --export-cpuid to export compiled-in CPU models Ian Rogers
2026-12-04  1:44   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 11/11] perf tests: Add round-trip export, CPUID symlink, and pmu-events.dir overlay tests Ian Rogers
2026-12-04  1:49   ` sashiko-bot
2026-12-04  1:49 ` [PATCH v3 00/11] perf pmu: Layered sysfs-style PMU event and metric directories sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204011029.1946921-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox