From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796346644; b=Ig9nUcsjU2ZIDHgPiF5WQisLzlIcOvI8pk2OsjS/+ce2cQ2gfSFZ9ty5qGmTWmvlN64d 9pNAKr/vEFCR/1h4iwtfK50mLzsZfwd49L4PqwGoSHTzWUEOwDjmP2/j0Ql5nN1L4EmdQ WIqCL0xB9VHVk4rm8BDjeTtQLHHGjMpfIbM8CwleLJwpqm2IoKO6W7dS/qqJwTF/mdOOB 45++5FXHBGP8ZdN9kCfWgI32xb77n+LZUi0X9mMyXCJULgP+yxEurb7XPGClh8rh9pHP9 5nrJpKQm2IhBQmuTzOKBsT/CRzhCapUf5RVmiWr0Mc99C4o/t3KSL2tQQNhU5S6dD+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796346644; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=E5BGbTn6TRAHZJsbERF3AgoImZwmB/ePtqWP4FYeFSo=; b=bJqAIAqLVV3k0HEpivNRv7XnxYhq1DbhmrNIfazXXefAZcKxJqerr91rvPSwwTP/qjpN TtZsHOuI+Qmb/JrQJUV4lOLoNpQxByyl24sQk210dt1PkeCd3MuDzFOuTX1RsPbXA+7yu yG+jp7HaA0rliMTLYWL+4uIYEjSmE1zGa2Fefq/zIX2RqlqWy1zxDsWTOdfmE1ssltY2L jPC7/6tsOxvvhV2JqUxm1tZzitVaOnPoC9BGJyQA70a9lX0fZtLh77d/i4lXWkuKnJPFG g5w/tadQ/RLLnzu2AMMNGwVQM13YMRw6Ch2UfGbAdd5tcckXurTNNottvXXbAG77XuQ== 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=HrVHWN9Z; arc=none smtp.remote-ip=74.125.82.198 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) (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 59AA7606A692 for ; Fri, 4 Dec 2026 01:10:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 59AA7606A692 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=HrVHWN9Z Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-30bcb065bfdso343939eec.0 for ; Thu, 03 Dec 2026 17:10:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796346643; x=1796951443; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=E5BGbTn6TRAHZJsbERF3AgoImZwmB/ePtqWP4FYeFSo=; b=HrVHWN9ZhirGeThLqrI701t7GYc/ZGWUndQMjcv+NoQRH/4Nl92pzmetpi+rI9GNvF I22QGV+RcuD/4NOkkFGOYEX01gDm/cCj/MzwQ66BsnHLZqT5h1mWNkqXgify9vUgfFOB +yHmaYY6LKfs5/mEc+/bsk1n7kuG5x0pGeQHZI51OycWTwuuQInNVK7TtwIH/21puYm6 CF52hd6B4WVOOxPz8xnZRJGeaH6M1hWIawh/NMlxsnH47zOUTKtHA63biwzPKCDr55Fy tqjyAcd7ozuFn0X3kefqoqoFRyVb/Fv4UIv/BsOihXlJ6CcfY9596T7iACK+0CQHnoKt AP/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796346643; x=1796951443; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=E5BGbTn6TRAHZJsbERF3AgoImZwmB/ePtqWP4FYeFSo=; b=wEyrIg0XgfvqzvVkNIXL+lZiY13gj7zqSrJZquROzd0lNjyhTzoZatN+wFs5WGDZiC WGQ9Lhtb5ukoLOpjX4r0Oc3JFaX/4lXyKWu1bUvu3e+fY5Gbt9aXHxVHt4FPbeDz8RWT ZaSTCTJbKqwlVHgXkTh7VrMdPCkPf+pBba8zvo5wsX/UcYShSG5p/kCv/eKcrn5QPCcu cux0iK6Aa5f/QmNXFxjP+XiGTmioj7tVMsDfLXveazkTkPkpIlr7BEoPtWqT9oD8U6qk F3+aT05r8PrhbtFyV46VXJMdPZU5AqVdY/EM25+0UEPTySt8xZ0AECZ2XMWjHHOKJNne Hfsw== X-Forwarded-Encrypted: i=1; AL+hErGkAduOhzDJP1UsoPO9HwnV9YJ9SU2hFvw/+xvV63KEAlHo6of9FZHul0kLWZbptqDbMTxESmeCz10ksIE=@vger.kernel.org X-Gm-Message-State: APuU4Z9WnpXGIT+U5FXQC3kqsasTMhYv/+hDzyXH0H0x2w5+ANB9/A7c gNJVMQqYE3nuZZVR60M8iLUhOyLmY3WUPWJs6a+hL5EqzMU7JalHd+duSsbT7ji4bJjowHlJGlo 3x4bsTJGFkA== X-Received: from dyay16.prod.google.com ([2002:a05:693c:62d0:b0:387:429e:8e56]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:ac82:b0:386:b621:d8ec with SMTP id 5a478bee46e88-3875218f90fmr637751eec.19.1796346643147; Thu, 03 Dec 2026 17:10:43 -0800 (PST) Date: Thu, 3 Dec 2026 17:10:22 -0800 In-Reply-To: <20261204011029.1946921-1-irogers@google.com> 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 References: <20261204011029.1946921-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204011029.1946921-6-irogers@google.com> Subject: [PATCH v3 05/11] perf config: Add pmu-events.dir configuration option 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" Add [pmu-events] dir to perf_config() (~/.perfconfig, $(sysconfdir)/perfconfig, or PERF_CONFIG) to configure one or more colon-separated user or system PMU overlay directories (ordered from highest to lowest priority) with leading ~/ home expansion. For example, configuring: [pmu-events] dir = ~/.perf-events/local:/opt/pmu-events allows a developer or fleet administrator to layer host-local or stepping-specific patches in ~/.perf-events/local ahead of a shared multi-CPU directory tree in /opt/pmu-events: ~/.perf-events/local/ |-- cpus/GenuineIntel-6-8F-8/cpu/events/ | `-- inst_retired.any.desc # stepping-specific fixup `-- default_core/metrics/ |-- my_ipc # host-local custom metric `-- my_ipc.desc /opt/pmu-events/ |-- cpus/ | |-- GenuineIntel-6-8F -> ../models/x86/sapphirerapids | `-- 0x00000000410fd490 -> ../models/arm64/arm/neoverse-n2 `-- models/... On a Sapphire Rapids stepping 8 host (GenuineIntel-6-8F-8), perf first matches ~/.perf-events/local/cpus/GenuineIntel-6-8F-8/ (overriding only inst_retired.any.desc while inheriting event encoding terms from lower layers) and ~/.perf-events/local/default_core/, then falls back to /opt/pmu-events/cpus/GenuineIntel-6-8F -> ../models/x86/sapphirerapids, and finally to compiled-in tables and kernel sysfs. When pmu-events.dir is unset, PMU_LAYER_USER_DIR is omitted from the layer stack so runtime overhead is zero extra syscalls. Reset the layer stack in perf_config__exit() and perf_pmus__destroy() so process teardown and perf test config isolation reload cleanly, and document pmu-events.dir in Documentation/perf-config.txt. Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-config.txt | 42 ++++++++++++++++++ tools/perf/util/config.c | 15 +++++++ tools/perf/util/pmu-layer.c | 55 +++++++++++++++++++++++- tools/perf/util/pmus.c | 2 + 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/tools/perf/Documentation/perf-config.txt b/tools/perf/Documentation/perf-config.txt index 6cff6f26af8d..e81c33717688 100644 --- a/tools/perf/Documentation/perf-config.txt +++ b/tools/perf/Documentation/perf-config.txt @@ -94,6 +94,10 @@ Given a $HOME/.perfconfig like this: # Default, disable using /dev/null dir = ~/.debug + [pmu-events] + # Colon-separated sysfs-style PMU event/metric overlay directories + dir = ~/.perf-events + [annotate] # Defaults hide_src_code = false @@ -259,6 +263,44 @@ buildid-cache.*:: buildid-cache.debuginfod=http://192.168.122.174:8002 +pmu-events.*:: + pmu-events.dir:: + Specify a directory, or a colon-separated list of directories + (ordered from highest to lowest priority), containing sysfs-style + PMU event, metric, and metricgroup definitions. Leading '~/' is + expanded to '$HOME/'. ++ +Overlay directories have higher priority than inbuilt JSON tables and kernel +sysfs ('pmu-events.dir > inbuilt CPU/SoC > sysfs > inbuilt common'). Attributes +merge top-down per event or metric so individual sidecar files (such as +'/events/.desc' or '/metrics/.threshold') can override +single fields while preserving lower-layer definitions. ++ +Within each '', perf probes three scopes in order: +'/cpus///', '/sys///', and host-direct +'//' (matching exact PMU names, 'default_core' for core PMUs, and +suffix-stripped uncore prefixes, plus '/metricgroups/.desc'). Under +'/cpus/', perf tries the exact CPUID first and then deterministic base +CPUIDs stripped of stepping or revision fields (for example +'GenuineIntel-6-8F-8' then 'GenuineIntel-6-8F' on x86, '0x00000000411fd493' +then '0x00000000410fd490' on ARM64, '0x00800200' then '0x00800000' on PowerPC, +'IBM,8561,703' then 'IBM,8561' on s390, and '0x489-0x8000000000000007-0x1' then +'0x489-0x8000000000000007' on RISC-V), stopping at the first matching +'cpus/' in that '' and following POSIX symlinks (such as +'cpus/GenuineIntel-6-4E -> ../models/x86/skylake') without requiring a +'mapfile.csv'. ++ +For example, to layer local overrides ahead of a shared directory in +'$HOME/.perfconfig' and define a custom metric: + + [pmu-events] + dir = ~/.perf-events/local:/opt/pmu-events + + $ mkdir -p ~/.perf-events/local/default_core/metrics + $ printf 'instructions / cpu-cycles\n' > ~/.perf-events/local/default_core/metrics/my_ipc + $ printf 'Custom instructions per cycle\n' > ~/.perf-events/local/default_core/metrics/my_ipc.desc + $ perf stat -M my_ipc true + annotate.*:: These are in control of addresses, jump function, source code in lines of assembly code from a specific program. diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index 0e6b77a0a5e7..8452ab806b34 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -33,6 +33,7 @@ #include "header.h" #include "mutex.h" #include "path.h" +#include "pmu-layer.h" #include "srcline.h" #include "unwind.h" #include "util/event.h" /* proc_map_timeout */ @@ -537,6 +538,16 @@ static int perf_stat_config(const char *var, const char *value) return 0; } +static int perf_pmu_events_config(const char *var, const char *value) +{ + if (!strcmp(var, "pmu-events.dir")) { + if (!value) + return config_error_nonbool(var); + } + + return 0; +} + int perf_default_config(const char *var, const char *value, void *dummy __maybe_unused) { @@ -555,6 +566,9 @@ int perf_default_config(const char *var, const char *value, if (strstarts(var, "buildid.")) return perf_buildid_config(var, value); + if (strstarts(var, "pmu-events.")) + return perf_pmu_events_config(var, value); + if (strstarts(var, "stat.")) return perf_stat_config(var, value); @@ -928,6 +942,7 @@ void perf_config__exit(void) perf_config_set__delete(config_set); config_set = NULL; mutex_unlock(&config_set_mutex); + pmu_layers__exit(); } int perf_config_set__write(struct perf_config_set *set, diff --git a/tools/perf/util/pmu-layer.c b/tools/perf/util/pmu-layer.c index d4fb8c64f617..7ab72c8adf9c 100644 --- a/tools/perf/util/pmu-layer.c +++ b/tools/perf/util/pmu-layer.c @@ -21,6 +21,7 @@ #include #include #include +#include "config.h" #include "cpumap.h" #include "debug.h" #include "hashmap.h" @@ -2079,7 +2080,7 @@ static int user_dir_load_metricgroup(const struct pmu_layer *layer, return ret < 0 ? ret : 0; } -static const struct pmu_layer_ops __maybe_unused user_dir_layer_ops = { +static const struct pmu_layer_ops user_dir_layer_ops = { .find_event = user_dir_find_event, .for_each_event = user_dir_for_each_event, .find_metric = user_dir_find_metric, @@ -2318,13 +2319,65 @@ void pmu_layers__exit(void) pmu_layers_initialized = false; } +static int pmu_layers_add_user_dirs(const char *cfg) +{ + const char *p = cfg; + + if (!cfg || !cfg[0]) + return 0; + + while (*p) { + const char *colon = strchr(p, ':'); + size_t seg_len = colon ? (size_t)(colon - p) : strlen(p); + + if (seg_len > 0 && seg_len < PATH_MAX) { + char raw[PATH_MAX]; + char expanded[PATH_MAX]; + const char *use_path = raw; + int ret; + + memcpy(raw, p, seg_len); + raw[seg_len] = '\0'; + + if (raw[0] == '~' && (raw[1] == '\0' || raw[1] == '/')) { + const char *home = getenv("HOME"); + + if (home && home[0]) { + int n = snprintf(expanded, sizeof(expanded), + "%s%s", home, raw + 1); + + if (n < 0 || (size_t)n >= sizeof(expanded)) + return -ENAMETOOLONG; + use_path = expanded; + } + } + + ret = pmu_layers_append(PMU_LAYER_USER_DIR, + &user_dir_layer_ops, use_path); + if (ret) + return ret; + } + + if (!colon) + break; + p = colon + 1; + } + return 0; +} + static int pmu_layers_ensure_init(void) { + const char *user_dirs; int ret; if (pmu_layers_initialized) return 0; + user_dirs = perf_config_get("pmu-events.dir"); + ret = pmu_layers_add_user_dirs(user_dirs); + if (ret) + goto err; + ret = pmu_layers_append(PMU_LAYER_BUILTIN_CPU, &builtin_cpu_layer_ops, NULL); if (ret) goto err; diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 8d3238fd23f1..5c21b28e420d 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -17,6 +17,7 @@ #include "evsel.h" #include "pmus.h" #include "pmu.h" +#include "pmu-layer.h" #include "hwmon_pmu.h" #include "tool_pmu.h" #include "print-events.h" @@ -147,6 +148,7 @@ void perf_pmus__destroy(void) perf_pmu__delete(pmu); } read_pmu_types = 0; + pmu_layers__exit(); } static struct perf_pmu *pmu_find(const char *name) -- 2.56.1.915.g69d5ec7298-goog