From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796346634; b=lv2V5IacDzj/eaBnN8xfAldzd7GP/FJVQcQQ0/xXtaowgIxyM3oTxeFdOgQ0gKh9FU3a OHPo4wBC/Y1Tt7o3ua21Fm1S9gcj/04YVp2g+olAR5EiP35WCa2pDn0W7TZXHC/m7lac4 JrfrZwVWZup7WGpIhVlW/05ToaJud4959HzVk0SFjONsjJ7pet6001EO5ep441Bj6SD5Q u7SMR1at3+gvWaPCEipu2yxhKZEY3ang1SCSrDUIQjkgd1Y3+RfgWBA8jnQfPu2VslP1O qoMQVbo/KimwEkNR5CX2JbWxJejo24TYm7TjwXEZzezpdud9pMx4p+RU6cdNazsrHUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796346634; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type: Content-Transfer-Encoding; bh=tCbiPbZGgp/ZTLiHHNyv9xREKE+iKH3EjIBrZoFzDS4=; b=mwN2moOqnT54qxuPFNYbCUM15ucCkSxa0vJwSmgVxrw/5tnmwQkepsh6sVC4WDUsckUK 1Cl9nqAldCPk11xdFb0rroEpcV23/GLOX5X+KPvNMkFK/U9S+nA5Em56l57nZMdHIcsNf 203Onw0OauDjIV7/IxT0dCK3e5hZ+F+JGVU6CoYIoksZKshGbAI6QwZrB6lIGqLPFLGHC 98Cu+X5HvGdHGJTHpinNYQ2C5qyBbLGP6k1DaDV2iNoa6g3krMhzWJr3awBJbU/sKBz+r rcQ0P4agOIR1Za9RcmmkjV9JkozmkUJt10UfHAmtobXrNj+kT8dR/eauUexJVSe1qhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=SaMcX2D9; arc=none smtp.remote-ip=74.125.82.71 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BFE2606A322 for ; Fri, 4 Dec 2026 01:10:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0BFE2606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=SaMcX2D9 Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1b5d5e46459so786193c88.0 for ; Thu, 03 Dec 2026 17:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796346633; x=1796951433; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:mime-version:date:from:to:cc:subject:date:message-id :reply-to:content-type; bh=tCbiPbZGgp/ZTLiHHNyv9xREKE+iKH3EjIBrZoFzDS4=; b=SaMcX2D901jpKsUsJ16CUOv4pCNhAmkxD+HdPbX8Fa5fwpJ3eupUXiA9s3FVItvbCa C13OhezFkGSQexgEHFR4HNCffIjP0T5wzomZV8b7DZgu105cJcGjIUNnpFBx+hsyvBbI QGa6ItM8+4KrLU3hufRCu9Y33AMYkeXBcWA9BePmaHBiWFO8QWy8mAM2oC5MtZf7sSeF RaFDA0XOez9duEm4f7p5aBmbFZcbCfCCADL36FyB2xezjvCQgM50ve71ADunsslvbrvD wOaEYYgI7eN1yVpLZb9K4AugC8TU6Hi232IQBwtzy7IAqi+rXcz9QCVcFSa4EttyYYEO HvJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796346633; x=1796951433; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:mime-version:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=tCbiPbZGgp/ZTLiHHNyv9xREKE+iKH3EjIBrZoFzDS4=; b=qy+vG2u2SJtW7dOz4hJSHEP5FYNYMHfY3sBZvW0RHVHhUXyXqiVgSkEhl25OCtHykq BudYhwKHGysqkQ3usWmHZVs1jX2ItK28uwzAoRHwXWqsEjMCiQqyCderp1kCCHf29xuu 32C4WpbsN/nL+CCpBplPeDiP5iM5hidijYf8A2UVyhQRc5orORSBMWEOtNtFmUG2Caq8 AAO46jgy3rO08IJ5yu1CGqkezolQ/XifQlckuFSMiL0GT4nmXpRNXuqDZb0tqhv/BtJK 38moqFNAi1OmoCaxkjdIp/UIAZutDUDvZqFeal5ROJGeQuPPs+6vzYEH07BXU8km1hku x4fA== X-Forwarded-Encrypted: i=1; AL+hErE22y9sEUCg/dim0k26ZTjoRQQ+arF2Bi00CFxwWGNCzSBbFGdBGpPfyavE7NafBb3OUQhNwQ+bJlxsC/8=@vger.kernel.org X-Gm-Message-State: APuU4Z+pZWE695DLzxOLL0NVCssCEiuRwx5s31lGgK0OBBgOwOq6hRJL ghUyTi1z4DpGfXoFDW/EgGnBp2W+Ozk93FV+fzy7JdlluluyoXvy09Sphi2BOUzjT/AgDPEdIdg YQ38FwTlqZQ== X-Received: from dybnj9.prod.google.com ([2002:a05:7300:d089:b0:386:33c7:a7de]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4343:b0:1b3:b833:6565 with SMTP id a92af1059eb24-1b5d7120fa0mr515215c88.20.1796346632738; Thu, 03 Dec 2026 17:10:32 -0800 (PST) Date: Thu, 3 Dec 2026 17:10:17 -0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204011029.1946921-1-irogers@google.com> Subject: [PATCH v3 00/11] perf pmu: Layered sysfs-style PMU event and metric directories From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Currently perf resolves PMU event aliases from two hardcoded sources (/sys/bus/event_source/devices//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//events/ rather than probing /events/ 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//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 (delt= a) -------------------------------------------------------------------------= ---- 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.3= 6 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 /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 =C2=B127-41 ms fork/event-open jitter and user+s= ys +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 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: / |-- 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} |-- /{events,metrics}/... `-- metricgroups/.desc Within each , perf probes three scopes top-down: 1. /cpus//: 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 stops at the first matching cpus/ directory so a stepping override (e.g. cpus/GenuineIntel-6-8F-8/) does not accidentally merge coarser model events from the same , 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/// directory. 2. /sys//: Matches pmu->id from /sys/bus/event_source/devices//identifier. 3. / (host-direct and cross-CPU fallback): Matches (exact name, alias_name, default_core for core PMUs, or suffix-stripped uncore prefix such as uncore_imc) plus /metricgroups/.desc. Exporters materialize both layouts: - 'perf list --export-dir ' writes the running machine's active PMUs into host-direct //{events,metrics}/ for quick local edits. - 'perf list --export-dir --export-cpuid ' exports compiled-in tables for any CPUID, model, or architecture (or 'all') directly from the perf binary into /models/// with relative /cpus/ and /sys/ symlinks (for example, to inspect or migrate metrics across architectures/models). - 'jevents.py --output-dir ' exports unbuilt kernel source JSON trees into the same /models/ + /cpus/ + /sys/ layout (also usable on NO_JEVENTS=3D1 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/ and sys/ 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=3D..., ~/.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 ' (host PMUs) and 'jevents.py --output-dir ' (unbuilt JSON trees). - Patches 9-10 add compiled-in model/sys table iterators in jevents.py and 'perf list --export-cpuid ' 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 /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/.desc scoped to models///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-iro= gers@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-iro= gers@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=3D1 PYLINT=3D1), CC=3Dclang (with context-analysis thread-safety checks), NO_JEVENTS=3D1, and DEBUG=3D1 + 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 --=20 2.56.1.915.g69d5ec7298-goog