From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212826; b=G2v8IPbH64waNALBoCvXEAQMzuAyFz+LCq5aAoLgezi3FfdjAQihW8EAQvTLud30RY5i rBZVCaGpyO1QUNJXHKYp6twxhPCJeaiFMrTLUH0tnFhzSJ9nVSqENgdUpgl5m74tCpeY3 Tso2AqWj0WO9TgGHOlZfGXo4LTIRaZnGIv9uarCjN91xtJv+XOOWASjk9wRjjYG4ht65F T5sKVd5nNR24IRT/cAlBv/vLnek2ctXKztD0v1H2rxz8zmLJ7PWsgskFDMAafKK7JHXqD 8PculYHAg+stKL3xeJOF1+a8znUxeu71Fqx7v4shPe4gA8eE3vmmwMDWi0vHsyWlJAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212826; 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=fdTVu9ac2YLECwcoGWeJTi+3MACuUXbIR+eJpbYlxEk=; b=ZCR6apwHAZ9XnglkJwgMbE0tJLXu9Y1TOpKIrMJjj3nS9wJTrssP4TQl9X5rZdKK3JGz fQ6iIqVaBaf+kym06ObfjW6pe1aZMRxuCvV44JRENNwSSFN0uhM33nK2Y35/sSeUXKrby wo3eM1BJjZFlhXCdX+WGEuWzYIzvveD3OOKlv82Rl5FEedHAq5S/RxioeNFe4WiEH6NbA P6eCqp4BrbGOfLzzW9NdKccN01WKHE6zUgWsTP+qqhbRRrjxsbokrNL3q/5Q112AGD3r0 E2a3QbsyvWylJGFMEV75qxGfNmv4auR1wQLRnT1wqn5JwS0jclBMMCiWYvfR/7/4TDQ== 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=JFpASQic; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (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 3796160A260B for ; Fri, 20 Nov 2026 22:13:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3796160A260B 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=JFpASQic Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b3ae17c536so1336490c88.0 for ; Fri, 20 Nov 2026 14:13:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212819; x=1795817619; 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=fdTVu9ac2YLECwcoGWeJTi+3MACuUXbIR+eJpbYlxEk=; b=JFpASQicDGpQu9Wjo+7e0MRRG/c6JZD3XKzAGRnD7fUKbN67dE/kwUdHfALquOVo9j f94S5KhYSjIZF3FaGTdxa1a5WQ6fagZZEqGKP4xvUw1qBol/7f+0wSdFimCfVcENqLbU PrztFv8aGjdS4zvVNCdrkl09PSoGQ32VBezfKR/fKSC/T/wTNNpx1Rhy4iJi33WTZA/S gUceMdKfYauuumnLXnaPsUcfpQj4g1GvXFEFM+JcPka251JR0ssZ3wvXEox4OosN8F9V jmRsTXwIf2BRRn1X+z1SSiKhdPZ8tQPou86lJn13om1rkKgk9iEsszt/zMEDQ3u5XP6F Vr/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212819; x=1795817619; 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=fdTVu9ac2YLECwcoGWeJTi+3MACuUXbIR+eJpbYlxEk=; b=SLP0NqNAQmVqsMquz34GX4S4W1fbaAs70kDkPD9sXOpdrXaAZq8mm6Gjz7Cm6TH8ZV gtYqNARsfz0JdaFHaA0w/eNJ/zw1fpSmzlWl9t5rpyEArTdWtaQWCjzVVkfDYoUZi9kT aNQIyu0k1KsNFDHXfYfj5gWfCrQGlBze7H3DbSKthOfQ2CErIqvv++ktjJxO2zmoJs+B Lc3v9AKEg4VKSq1lXseiI05OrIOprj7kWpLR79MvujqbNwzCw3RIe5NE9CN+udsbuqhv FmakuFcVbDlybeKNctnK0BbAdLvKyNPKUZiqmMS9WP2MG+hzdk0fZ4W3nfAdDrGTC3HA C0yg== X-Forwarded-Encrypted: i=1; AL+hErHcGhSqnkTQVg0qoSAxPavtprdy3rq05+FUG98CZDUz2GiA0hQwKYvnL91cygRQQTF0ERwARQajDSQdzUc=@vger.kernel.org X-Gm-Message-State: APuU4Z+aFZbc338y1y6GAT+S9pUh1gFEM/vHkjGSbn9EL8HALXs8yE6D KNAKaK2BgX8CbkZs9kMjcHlYISm9p8CIJhaImHbEqbQOjL4KdZQ9U7P1i6D1UFywcjlA2xJRoUo UaXzYaHQFWg== X-Received: from dlbuy9.prod.google.com ([2002:a05:7022:1e09:b0:18c:3061:3bca]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:f109:b0:1b3:8814:8383 with SMTP id a92af1059eb24-1b55689fca6mr3255964c88.9.1795212818746; Fri, 20 Nov 2026 14:13:38 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:02 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-2-irogers@google.com> Subject: [PATCH v3 01/30] perf pmus: Don't skip core PMUs as duplicates 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_pmus__scan_skip_duplicates() skips a PMU whose name, without a numeric suffix, matches the previous PMU's. This is intended for uncore PMUs, such as uncore_cha_0 and uncore_cha_1, that have the same events. The check was also applied to core PMUs, but core PMUs of heterogeneous systems may differ only by such a suffix while having different events. For example, ARM ACPI systems with two kinds of core name their PMUs armv8_pmuv3_0 and armv8_pmuv3_1, and devicetree systems may name them armv8_cortex_a53 and armv8_cortex_a57 where "a53" and "a57" are hex suffixes. Only the first core PMU was listed by "perf list" and the events of the other core PMUs were missing. Before, on such a system, "perf list" and "perf list --unit=3D" only showed the events of the first core PMU, the scan having skipped the others. After, the events of every core PMU are shown, as on Intel hybrid systems with cpu_core and cpu_atom PMUs. Uncore PMU deduplication is unchanged and pmu_deduped_name_len() already avoided removing the suffix of core PMU names when printing events. Similarly avoid removing the suffix of core PMU names in the raw event encodings that "perf list" shows for each PMU, so that each core PMU's encoding is shown with its own name. Fixes: cd4e1efbbc40 ("perf pmus: Skip duplicate PMUs and don't print list s= uffix by default") Signed-off-by: Ian Rogers --- tools/perf/util/pmus.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 8d3238fd23f1..03d79ec5037f 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -488,15 +488,14 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicat= es(struct perf_pmu *pmu) last_pmu_name_len =3D pmu_name_len_no_suffix(pmu->name ?: ""); =20 if (use_core_pmus) { - list_for_each_entry_continue(pmu, &core_pmus, list) { - int pmu_name_len =3D pmu_name_len_no_suffix(pmu->name ?: ""); - - if (last_pmu_name_len =3D=3D pmu_name_len && - !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len)) - continue; - + /* + * Core PMUs, such as armv8_cortex_a53 and armv8_cortex_a57, + * may differ only by a suffix but have different events, so + * they are never duplicates. + */ + list_for_each_entry_continue(pmu, &core_pmus, list) return pmu; - } + pmu =3D NULL; pmu =3D list_prepare_entry(pmu, &other_pmus, list); } @@ -785,7 +784,9 @@ void perf_pmus__print_raw_pmu_events(const struct print= _callbacks *print_cb, voi .long_string =3D STRBUF_INIT, .num_formats =3D 0, }; - int len =3D pmu_name_len_no_suffix(pmu->name); + /* Core PMUs aren't duplicates so keep their suffix. */ + int len =3D pmu->is_core ? (int)strlen(pmu->name) + : (int)pmu_name_len_no_suffix(pmu->name); const char *desc =3D "(see 'man perf-list' or 'man perf-record' on how t= o encode it)"; =20 if (!pmu->is_core) --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212827; b=IX/sPrJHN3Lc1NKbgwIj7yzXsUAzIbNbtrEAV2zOtJNpU/NHuqj55Zy4trwbxVS/OG5p Kk5DGb5G0revV0U1RJbjzBOAtigYEABU9uWZHKfl9u0hDYXIHbas+R3x2hSkA2Lyoy8MG kxbWPN3mr6tlJV58D5LPkxJxYG8y5Ql3KPc1imelx8PUVNRT7xJ/rufRR/Zxv31KEyh9b +Vw6mH1uKRFAGrbXXM0lMsSs8JuQ90dIB/Vw6+SKLC2kU0xErVFtARjZsdC/zyhrBXeP0 gchuQ5R0iYKCa29b+G1eUGjpdRAVpPashB/heA7CcHRfXfuDZh3jmBT5QHsi0/tObzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212827; 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=/iqd1s6yIkM3QgjlFuj6dLBfLTgftpemZYmyudK9OX0=; b=lRbxKTAg3CgfnJOsXNchlJ8UYdTVqUBGhSa6YG1kaDbei+Zw05JC5zXkoD3yM1+wgu/b MdAzMak4sWOqFkVY5rGcVc5oY+9RYsQ5AoKNOnVe/rRu/x1+snb70wJYjF0JXcSWFLnOB N7BLaprBJhzz9Oar41NgaSfgdoszupDRE8FSh5rtAa5Q3PmG8+pymJkovVmlIfm7f/V6x UA+ejh78eiGeckTywesfHbR/YbdCbTxVJ8JmB7LsZm03XjUWxYqhkGgHEqA2LRAEoq0z3 2/dW2PNS3/+WnrVFBKZESQ5+yZSgkGAoQoTlO7m/knn89pPX1sqrnfT0tVR64QVctZw== 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=h8xCOweh; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 722B3606A6B6 for ; Fri, 20 Nov 2026 22:13:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 722B3606A6B6 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=h8xCOweh Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-370230d93efso4226693eec.1 for ; Fri, 20 Nov 2026 14:13:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212825; x=1795817625; 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=/iqd1s6yIkM3QgjlFuj6dLBfLTgftpemZYmyudK9OX0=; b=h8xCOwehtj+kfV7SY8E7fMyWYaLTjBfcFyMDlOoSRuK0P83xLZTMDfK/frahTNL7Hw Z6PM5dfmNvqVL8qWfBcfhQK8Mc+KLAwYD+HVdMFLeckznFlPaKHAiHhQeUZHegTFW+wB l+1C2jinEvjTt0wISFoUusWNKIlDzngyCv5Jo+mcIPoim2Hsg+vEtEsbIC6J69ueXlGk /rk2IDZNA08nHgpeAwuDgPTRDups4rv7qiSS7VBR60+au2FSJ9kmA5P4zu8LrE5eaHut ZsLI2EhsENoV8L5vB43ghsExLZHG94gEH5/26WsX2DfDEON6874e8hip/+mwgpizu7Sv e7/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212825; x=1795817625; 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=/iqd1s6yIkM3QgjlFuj6dLBfLTgftpemZYmyudK9OX0=; b=pQXYSn14uO59zt27Mxwkx0+KguqmwuviPkk2WGqAPuXRdSImXq0xBotzL3F8Uomav1 nvzvz+wbXLbBCRl95gn9xESaRFTPXDf0zuoBc6OqtwZ5ncRtxWuaSgnpm/nBMcCjFURS RJHTOMDdRkUECzup3Gyb8y3vl/V72vNblcXnKpTG+amsNqYDC8DnyO0xEq2H1kwCqGab gix+fpCPucp6lDiCzwuRBLz+GWjG13pO7kEkiX72PVtSRpF7chA2HNIJKnR1cY/EA2Ru qdQMlOQejKa5LwWHA1J+yQr1P+fgZno2/9lN1NS1yOu6dlFWNsTVkfCrWov5HZHkYke+ 9xoQ== X-Forwarded-Encrypted: i=1; AL+hErGo71hxlH8+o9CXtTfgSZvXLBuezOd1T+/ouD9nNWnQ8cq9xipB9RzN8aEMZrAbjOVe2db8xQgzrHZCLHw=@vger.kernel.org X-Gm-Message-State: APuU4Z/l/0xSK9VrfYUtt21fxiFth+iZw/NwkbH46iuB1xCB0m/cXXhW OL25jCWV9ROIGUbvC2ll96bCA1v8XUsld5pQ2/IKg5cWahvg+Hw2IaSeabgz0qrQB2WN/yBOq+9 VgcM32DVZ5Q== X-Received: from dyev10.prod.google.com ([2002:a05:7300:430a:b0:343:520f:7a80]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:d090:b0:386:8bd2:da2c with SMTP id 5a478bee46e88-3868bd2db9amr1848412eec.2.1795212824252; Fri, 20 Nov 2026 14:13:44 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:03 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-3-irogers@google.com> Subject: [PATCH v3 02/30] perf script: Fix metrics computed from samples 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf script -F metric computes metrics from the sum of sample periods of the events, but the computed values were wrong and the output was garbled: - perf_stat_process_counter() replaces each count with its delta from the previous raw count. As counts were left as deltas, the next sample computed negative deltas for the counts of all CPUs and threads other than the sample's. Restore the counts to the running totals after processing. - Metrics for all events were displayed after every sample. Only display the metrics associated with the sampled event. - The new_line callback started a metric line that the next metric started again. Each metric starts its own line, so make it a no-op. - The metric value was written to stdout rather than the event's output file. For example, with whitespace reduced: $ perf record -e '{cpu_core/instructions/,cpu_core/cycles/}:S' -a sleep 1 $ perf script -F comm,cpu,event,period,metric Before: perf [001] 2206 cpu_core/cycles/: perf [001] metric: 42972.00 tma_info_inst_mix_instructions perf [001] metric: perf [001] metric: 1.20 tma_info_thread_cpi perf [001] metric: perf [001] metric: 0.83 tma_info_thread_ipc perf [001] metric: 51762.00 tma_info_thread_clks perf [001] 804 cpu_core/instructions/: perf [001] metric: 18446744073709549568.00 tma_info_inst_mix_instructions After: perf [001] 2206 cpu_core/cycles/: perf [001] metric: 67668.00 tma_info_thread_clks perf [001] 804 cpu_core/instructions/: perf [001] metric: 46758.00 tma_info_inst_mix_instructions perf [001] metric: 1.45 tma_info_thread_cpi perf [001] metric: 0.69 tma_info_thread_ipc Before, the cpu_core/cycles/ sample is followed by the metrics of both events, such as tma_info_inst_mix_instructions that only uses the instructions event, rather than just tma_info_thread_clks. The CPI and IPC lines repeat the "perf [001] metric:" prefix as script_new_line() started a line that script_print_metric() started again. The following cpu_core/instructions/ sample shows tma_info_inst_mix_instructions as 18446744073709549568, a negative delta, as the counts of the other CPUs were left as deltas by the previous sample. The same deltas made the before tma_info_thread_clks value of 51762 too small. After, each sample is followed only by the metrics of the sampled event, one per line, and the metric values are computed from the running totals of the sample periods of all CPUs. Fixes: 4bd1bef8bba2 ("perf script: Allow computing 'perf stat' style metric= s") Fixes: 3622990efaab ("perf script: Change metric format to use json metrics= ") Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index b6691ebb1b4e..c5a9651a197c 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2340,18 +2340,14 @@ static void script_print_metric(struct perf_stat_co= nfig *config __maybe_unused, if (color) color_fprintf(mctx->fp, color, fmt, val); else - printf(fmt, val); + fprintf(mctx->fp, fmt, val); fprintf(mctx->fp, " %s\n", unit); } =20 static void script_new_line(struct perf_stat_config *config __maybe_unused, - void *ctx) + void *ctx __maybe_unused) { - struct metric_ctx *mctx =3D ctx; - - perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel, - PERF_RECORD_SAMPLE, mctx->fp); - fputs("\tmetric: ", mctx->fp); + /* Each metric is printed on its own line by script_print_metric(). */ } =20 struct script_find_metrics_args { @@ -2524,7 +2520,6 @@ static void perf_sample__fprint_metric(struct thread = *thread, }; struct perf_counts_values *count, *old_count; int cpu_map_idx, thread_map_idx, aggr_idx; - struct evsel *pos; =20 if (!init_metrics) { /* One time initialization of stat_config and metric data. */ @@ -2593,14 +2588,23 @@ static void perf_sample__fprint_metric(struct threa= d *thread, /* Update the aggregated stats. */ perf_stat_process_counter(&stat_config, evsel); =20 - /* Display all metrics. */ - evlist__for_each_entry(evsel->evlist, pos) { - cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) { - perf_stat__print_shadow_stats(&stat_config, pos, - aggr_idx, - &ctx); + /* + * Processing the counter replaced the counts with deltas from the + * previous raw counts. Restore the running totals so that only the + * next sample's period is added to the aggregated stats. + */ + for (unsigned int cpu_idx =3D 0; cpu_idx < perf_cpu_map__nr(evsel->core.c= pus); cpu_idx++) { + for (int thread_idx =3D 0; + thread_idx < perf_thread_map__nr(evsel->core.threads); + thread_idx++) { + *perf_counts(evsel->counts, cpu_idx, thread_idx) =3D + *perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx); } } + + /* Display the metrics associated with the sampled event. */ + cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) + perf_stat__print_shadow_stats(&stat_config, evsel, aggr_idx, &ctx); } =20 static bool show_event(struct perf_sample *sample, --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212832; b=eK6134Kgci4/HatAg+xlVVLuKLs0V/t0w28jephfUvIqsPVv6mPow9/qoOA67wZzi6GM Se2gIq2LbTj4W609NQ9AY9si78y2Z+9c9ggdTkeJ5G68WkKz9Q8Pbt8cLZ/4BcNjRMjkn DhzH2qDCRv3gC9fXh/4++p0lzah13UWsSCEKlxJT/nFxE8ltaS3/Jw4HjZrz43UkzHR0P YPUV5AcehyVq56CzjaOT1hi+qQlrgezWPooT2GXdAQHviQbbik+DuMqh0r5EMUWmOcd8q juSyKsmCN3GZoh0qheTOkyCthAIleOW+l8ICrJNhf5s4de6gQF4Ajkpi0O+HlInYRDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212832; 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=4peQzHs6AvoizxfnquPA5N6VEKo2Q/iCjNJ4PP9zkI8=; b=quWw4JoXGDK6cG2BAKa6rt6/sLavF6nTJH4ZIgCSSSHRst5Gmr7985M27F9P3VEYRzXT yiegzLedS+YjFZwztfcM0w1iNl9y2gSvEcX/VFx+k6Vv3XTf+qyeZzWFMeKaItBQzXa7T +EpY6/qvGY+D/aduNhVKSJzpZDW5XI9ibBQtEuAzfieAZypvJiqPajKQ6Ln18Fc14tCLs uT6pNRViXvPwZinS17s/a4wxdixeZ8FNnuFcG10s1EIc3KQAtyhCv8H96UesCSPoxsKAG bpBNj206HCYEdotBJiRuTsHO2W8eIE57cQuiCTXBVrfQNvak0Afr7g8jJX1XnQtlJ1g== 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=Y7efLO5D; arc=none smtp.remote-ip=209.85.210.71 Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.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 47C51606A6A0 for ; Fri, 20 Nov 2026 22:13:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 47C51606A6A0 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=Y7efLO5D Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-86285932977so1617944a34.0 for ; Fri, 20 Nov 2026 14:13:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212830; x=1795817630; 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=4peQzHs6AvoizxfnquPA5N6VEKo2Q/iCjNJ4PP9zkI8=; b=Y7efLO5DP6MUsIEbfesLt2qqrmpTKMB9CJ0Rc0ah0pyKmtRlsO/qE/NJjvNu1Yo3ax NON6jv/hBWYsyKrUic1LhUfsnr2kYWgxvi9vC10ipMdDl42FeBewblbN60+3GnjSRfRk wjdleE2ytmxAjzaHtpe6GPO0i81TXSlekXM/vHGS5nIxF87RMFrrvSk2gMsE2FlCmQ6z i7UIsgZLAntqt9jSph5B55h5onTLYO49/KWzNhKg2oqbJLFgcUsCLg8cgifWUaNFWlGe mAna3jXKgSNjkMu/ijr44L3YYeecHe+nOt/YmFP0ncnstmbzhPO9/2f6GZLUJoUUBER5 PIVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212830; x=1795817630; 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=4peQzHs6AvoizxfnquPA5N6VEKo2Q/iCjNJ4PP9zkI8=; b=19mu3l3UZS26EaWI4FwPi1yTakMlc40PyjY1Sm6YTo4dO2uXkArp3ALsbRsZcvxykW GYaZ05F0COIA8NOL8+VOnYPHVjspdF05SJdyYV7tH4jrggJX8NoczoaGWw6WxoHtVmcI YiYAxrocA4BthE3LlYejU6qOvWcc8bNt43FtHc412Exalxc+FwXvd+ZIbR77/r+JHhEm bmJVa2lcUK5J65tnHbruzoIqvjS2LuTgU4Kx+PGBd7IxoHgs/6hhTRJXnSmec6aP6wAC DXk7O+JhOZG2KTKnyTgO/kvGX2nEAtvmffEisP9A0nFqA3Kp5slD99m5KHR5p8fZfHT7 NA2Q== X-Forwarded-Encrypted: i=1; AL+hErEkR85RKpdxky/ePH2+HmuxJ8lzTRDLpD3JUETannba0QfBi55gZTu5t05I0E9W6fNj9ZGdW+uU2i1xrPk=@vger.kernel.org X-Gm-Message-State: APuU4Z8KAX/8lTYzTvphNwZG4X8EDI1ixxHqFpASWyCgMtXsHmwHJKjE 5Hfbn1bknFKDQOXxme8+6kAtbV1M3whm304tP+KUTwlJkx8BVJKLRNO11sY+vhdXazcTMZx08aU zJZpbhhgfrQ== X-Received: from jatn14.prod.google.com ([2002:a05:6638:264e:b0:620:1b60:b872]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:6711:b0:571:a186:d69e with SMTP id 5614622812f47-57a09e0eb99mr4908901b6e.47.1795212830035; Fri, 20 Nov 2026 14:13:50 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:04 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-4-irogers@google.com> Subject: [PATCH v3 03/30] perf script: Don't drop metric samples for CPUs missing from the evsel 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf script -F metric looks up the index of the sample's CPU in the evsel's CPU map and, when it isn't present, prints "Missing CPU map entry for CPU " and ignores the sample. When the perf.data file doesn't record the CPUs of an event, as is the case for perf record, __perf_evlist__propagate_maps() sets the evsel's CPUs to the host's online CPUs. If a CPU is offline when perf script is run, or the perf.data file was recorded on a machine with more CPUs, then every sample on such a CPU is dropped from the metrics with a message, for example thousands of: Missing CPU map entry for CPU 1 perf script currently only supports global aggregation of the counts used for metrics, the counts of all CPUs are summed. So place the counts of a sample whose CPU isn't in the evsel's CPU map in the first CPU of the map, as was already done for samples without a CPU. This doesn't change the metric values computed for samples of the CPUs in the map, the summed counts just include the previously dropped samples, and so isn't a regression. Aggregating per CPU, and using the CPUs of the perf.data file's environment rather than the host's, would be future improvements. Fixes: 3622990efaab ("perf script: Change metric format to use json metrics= ") Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index c5a9651a197c..97bad639753f 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2557,15 +2557,14 @@ static void perf_sample__fprint_metric(struct threa= d *thread, /* Update the evsel's count using the sample's data. */ cpu_map_idx =3D perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){sam= ple->cpu}); if (cpu_map_idx < 0) { - /* Missing CPU, check for any CPU. */ - if (perf_cpu_map__cpu(evsel->core.cpus, /*idx=3D*/0).cpu =3D=3D -1 || - sample->cpu =3D=3D (u32)-1) { - /* Place the counts in the which ever CPU is first in the map. */ - cpu_map_idx =3D 0; - } else { - pr_info("Missing CPU map entry for CPU %d\n", sample->cpu); - return; - } + /* + * The evsel's CPUs may not contain the sample's CPU, for + * example the sample may have no CPU or, when the perf.data + * file doesn't record the evsel's CPUs, the evsel's CPUs are + * the host's online CPUs. As only global aggregation is + * supported, place the counts in the first CPU of the map. + */ + cpu_map_idx =3D 0; } thread_map_idx =3D perf_thread_map__idx(evsel->core.threads, sample->tid); if (thread_map_idx < 0) { --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212836; b=FGgBHjZF9vVLAJpYqCnQdSWPtPbHtQKKxdgQfQLZKzgDWdwACETO5xVRAyzewsjip5su fFctqjFTXpnvUVATU9duEFUdiop5R+/IgW9daMMSvF/hL6UROlHICbhQ3XpQ0K2naon3I XqgBHa49Zu4VN67UQXvUuzOVxDgP22nqPzV+ool0vSIHxwOS1xP97KImJT9Uiv+4G6HHx 8MH82ewjfkyjA5V1L7Ytrny/LZLKnvsXEteenB2h0cy0eeeArDiQeSOxGALNJKOuz8rqG 9knCY7DUsM25vQqkXcHS3b1TuY0FoZoJhl6hF2JO0Vo8Q6pNYwrdpwrrTLmh29mJ/Sg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212836; 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=qZdMAm29i2lB188sbmGGTc+2IJf3WyF7WYFVB9nwzmw=; b=k0+eSuiyhV2exKrOS0sCzPM5LIOcS26XG1qkIpMFOUiIauaZOy05Jl57H+SESSY/+x5q +Y8obHY7ATIYbvOK8l/ePWTTN6Y2yOuEkuVS+Ym7Bbk+nrvbuaV3OBZra2J2aZJKtFeRq 8MKrZHI45fs25eEPeVqxVbVlAQcFWxCreCRthS1oWz9mnrxoRfL1rEiHhS6cCgG0GSYFU w+zr8h8P0Sp1Lfop+Bzp6SbMoXJjTki573JDgVRgTPICJvha7vH/W4Irn8sao0W1bCEG/ QNPeqXvUoWl6cOjlg8FdyWTRcGJzM4WYuzlHrrq0N8LAyf4tQ0tjLUjih/vtir91t0g== 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=WEtgcrIk; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 ED1B6606A680 for ; Fri, 20 Nov 2026 22:13:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ED1B6606A680 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=WEtgcrIk Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-38355b03cb8so1310489eec.0 for ; Fri, 20 Nov 2026 14:13:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212834; x=1795817634; 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=qZdMAm29i2lB188sbmGGTc+2IJf3WyF7WYFVB9nwzmw=; b=WEtgcrIkVFQTqXNb3RiVYhcNLtxmqjybnhFcRSCqB19B2dnO72V+8JUXIVQesJR54T RWXU4QtQghYyIi0BLW0RW/VdPKM33HSaBSgYJ2TobNmpKe5p4CV0iaXP0JeQE6OmczvD ncLQZTWTIFtCJosQR6x7xr1ecj5QuDT5RSGmcoFfIIn8uVPuDF3ZGJNub5bjMRi6v3ZR 5tJZAkfrTnOljhdmFoEzYFWtounRgFOu+FCIc6ojbOcMlesoIC6e+YAkcew31zwDrWua oePg4fxbTACeZrBxcn0ZrOCSugGlBZRki7mcLl0KVLDUcNBE8q6ZJfUJaNfdEOR5xpmk TFSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212834; x=1795817634; 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=qZdMAm29i2lB188sbmGGTc+2IJf3WyF7WYFVB9nwzmw=; b=oV6FlMtwjBNsEkoZl5nSVBAJZfBib4XUIbPUV2zdhtpqTRvcK+5yO0Uq62rTCvlcLL 8vnZcX/i2HxxDE+3N4H4l8MXUjFbBVsjFt+ZywdMKEGe5PZfCG1aWu9HkdRCbD9g5jKd F9OXS48C+oWtUywHBEO4qVGG35UwHVSZXd4NUw5Z+Lxh3yOca5qOF2BkaWi67ZYXiOQR L/OXJuPOwJY4RzU6GFe4SWikHAVEO+MuEjb7ZT1NEo6APm3b5EPvbPPaILa4Qf5RqKni k9obq0fT+dFtBuOWseyGumiYMQc32P4i4AgLIxYHcStU0G58igDp7foKnQUQxjy80TFc kfxA== X-Forwarded-Encrypted: i=1; AL+hErEMjjyhSanU5j+ZhNPBijcB19CRYgyDCuPNiDpMNqHBv3FHq31JzhgeTghqxnY/AdxEiye5bm604iMK8no=@vger.kernel.org X-Gm-Message-State: APuU4Z+DOM1KBB+m+7CSKwOFDRyJNG4DjtxxnxtQLfb0+rETQOvJCF3N ktnUUwrUDDgZVzmGcRKRytMCNmp3kOk0K9LocVli/f3HY4HNwzD7V44+OpDaWNFASJSM0zZ2GvB 2EgkXzLd6zw== X-Received: from dyah11-n2.prod.google.com ([2002:a05:693c:83cb:20b0:384:337f:4c9d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:d205:b0:36a:1f8:a56e with SMTP id 5a478bee46e88-3856fe564a1mr3602765eec.23.1795212833694; Fri, 20 Nov 2026 14:13:53 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:05 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-5-irogers@google.com> Subject: [PATCH v3 04/30] perf metricgroup: Don't prefer tool events for displaying a metric 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pick_display_evsel() selects the event of a metric that perf stat shows the metric alongside, preferring an event that isn't a tool event, such as duration_time, and that is used by the fewest metrics. A tool event used by fewer metrics replaced a non-tool event, so whether the metric was displayed with a tool event depended on the order of the metric's events. For example, CPUs_utilized could be shown alongside the duration_time tool event rather than cpu-clock, and cpu-clock not shown. Only compare the number of uses of events that are both, or both not, tool events. Fixes: c8035a496141 ("perf metricgroup: Add care to picking the evsel for d= isplaying a metric") Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 779fe56fe8d0..98f3ba8f9533 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -1388,8 +1388,10 @@ static struct evsel *pick_display_evsel(struct list_= head *metric_list, struct evsel *candidate =3D metric_events[i]; size_t candidate_uses =3D count_uses(metric_list, candidate); =20 - if ((selected_is_tool && !evsel__is_tool(candidate)) || - (candidate_uses < selected_uses)) { + bool candidate_is_tool =3D evsel__is_tool(candidate); + + if ((selected_is_tool && !candidate_is_tool) || + (selected_is_tool =3D=3D candidate_is_tool && candidate_uses < selec= ted_uses)) { selected =3D candidate; selected_uses =3D candidate_uses; selected_is_tool =3D evsel__is_tool(selected); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212841; b=vsisZrUhxP4NtTaXlX22eiGpYU74qiCsEnVpPaL8lbQsl1O8GJxXns2rmGipsbPNxQhf oQZtftwb6vV8x3N6dpuCfpuI5ojENV1mBMs6MowxvwQwiDP+JuHI9OVn1ntO3bVbT0IDI O0S5lqwi+qh30Hxw8DdxdD8p5aACS1jEupkiYP1a+dQJCZQLXQU7B9V3Hllz03LdFl/xd 527IgjFYkIIW1lQL6Hpcw3XChiIaTrLrm1HtKK3OhCakm62EbuFzdp2SzV6L3gBCmR2p5 D9Jw1SGQcqhdWIEJJ5r8gFK7y5Kb/XstbVG82MYBZ5XsCUd0WEprxn08ki7iDagpZsg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212841; 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=VstbANpJzGmmJC7LhvRbkS5fgmk2WrM9lQ7Ho0E2PCY=; b=G6VsmDuKj95c2WtuiC2tHurALl67azKcI9zbmXVMS9mKXXQ3zqHR0w7FSu8KpinDSGgI FXrp4ygQ8AtDpYJy2LLsrKT/V6/VDrFBkL9J3QLJTqzFBWSlpDNB6TgNop12KPVfkzsYr Exc51tmc+52rosUJBzvruNVhfN9m7dwcyXs6fVGyRaanw7Az9H4qD4amw6FLn2NTjWtd0 OM9l5e/PxtXsmcd9F+99W5kD85wAdSDcLDfxUSfy9Srkk4Zbx8vIMI58iYk9skLyBKPax ZnyNLYSJcdL2vDVV3edhQ01OWG4VeCUyelnsCWH/dvlySb/SFraBdp1xopNi6Zo6g1w== 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=E0iXOWMJ; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (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 2E0F7606A6B3 for ; Fri, 20 Nov 2026 22:13:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E0F7606A6B3 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=E0iXOWMJ Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1b53bfc9534so398052c88.0 for ; Fri, 20 Nov 2026 14:13:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212838; x=1795817638; 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=VstbANpJzGmmJC7LhvRbkS5fgmk2WrM9lQ7Ho0E2PCY=; b=E0iXOWMJ1GZygeDzXydDm970/AMkl832gVrnoR943d01zPzeykbgLk2GE6euotbBEq Ak9dQ7uhuYCqJtD07fG3fMi2aWmwRpRfTKA8L1R8zLZ9ncZwXfmBNNVc9S8dDDIjcE/f YBYfwO/X0KQACEl1unyVcl9R7+eMQ6XLWbClhFqcOolLZ0Vh13JTZ8FygCvWZNUqrcCH vWHuk3q6j+feuSD13EMxPUWmLVLVrE+V+ehx3G4PmU0mJQSogJqAaJQwXHZ66B7tAKYO ubr8hmCFVQ/5ZQYdyjAoWr2OiOVmKnuUIsqlrnmimrQQOEfINftKc+/LoCQ4hSF8GccZ 5r3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212838; x=1795817638; 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=VstbANpJzGmmJC7LhvRbkS5fgmk2WrM9lQ7Ho0E2PCY=; b=aVyhH1Tyl0u+sIDCE9LkCfx7Jl9U2gU104g/DQBVtVb/3q05q2f+qjsPGaIKv0bNi2 ADKPphA79eC5+Toebuo2z9WeDtF6N11GW+tOBwTuW+bRB63Jh7JkN2lskFSFTb6BCQoQ cWbrJROYBK7B8N4NplZQa7z94QbEMP1Dr2srD60+cPeC9I1tozzqJ2zPE6MdEr0AAobV e4cv++JV2BO1fZs+wJ77lHpheaITVqFDYVAAd55LIWhnqYhoM7MzbP5400SXunr74iS+ HtQF943jPYnsRSsbMAbhgQJlxZGDpnKH6jEvXl6cVNgYrpJ2T3G8FQoyMzZq3vD5cSbs /8Kw== X-Forwarded-Encrypted: i=1; AL+hErES3Yr1uxMA5H/quidlx6yeqe2hc8rPEQiwOCUihzcT8jNUtrBknBB/x8mV1cNTSgwfJ5IyUzHDJkdkZz0=@vger.kernel.org X-Gm-Message-State: APuU4Z9a++BXWcP6ypwOwAem9L4ELu1LSdEZVgw4ir1ZEEAWvEGMlV4y a35Y35KXypgXBaMQb+17woeSuahUgABk2aijcP+zEkjfjQmXUMr9mcmxX8ZzLQDEVeAjf4SZjNI 6NAFVti3CYg== X-Received: from dleb16-n2.prod.google.com ([2002:a05:701b:4250:20b0:1ac:2245:a27b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:ea8c:b0:154:fa0b:6df1 with SMTP id a92af1059eb24-1b5568e1559mr4071055c88.37.1795212837609; Fri, 20 Nov 2026 14:13:57 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:06 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-6-irogers@google.com> Subject: [PATCH v3 05/30] perf env: Make lazy perf_env initialization thread-safe 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Guard lazy initialization of kernel_is_64_bit, nr_cpus_avail, e_machine, cpuid, nr_pmu_mappings, pmu_mappings, and numa_map in struct perf_env with env->lock, and use DO_ONCE() to initialize the cached host utsname structure. This prevents data races when multiple threads concurrently query a shared struct perf_env. Signed-off-by: Ian Rogers --- tools/perf/util/env.c | 178 +++++++++++++++++++++++++------------- tools/perf/util/session.c | 15 +++- 2 files changed, 131 insertions(+), 62 deletions(-) diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 50993b0dedc5..4ab46ca6b4ef 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -313,18 +313,17 @@ void perf_env__init(struct perf_env *env) mutex_init(&env->lock); } =20 -static void perf_env__init_kernel_mode(struct perf_env *env) +static const char *uts_machine(void) { - const char *arch =3D env->arch; + static struct utsname uts; =20 - if (!arch) { - static struct utsname uts =3D { .machine[0] =3D '\0', }; + DO_ONCE(uname, &uts); + return uts.machine[0] !=3D '\0' ? uts.machine : NULL; +} =20 - if (uts.machine[0] =3D=3D '\0') - uname(&uts); - if (uts.machine[0] !=3D '\0') - arch =3D uts.machine; - } +static void perf_env__init_kernel_mode(struct perf_env *env) +{ + const char *arch =3D env->arch ?: uts_machine(); =20 if (arch) { if (strstr(arch, "64") || strstr(arch, "s390x")) @@ -340,10 +339,15 @@ static void perf_env__init_kernel_mode(struct perf_en= v *env) =20 int perf_env__kernel_is_64_bit(struct perf_env *env) { + int is_64_bit; + + mutex_lock(&env->lock); if (env->kernel_is_64_bit =3D=3D -1) perf_env__init_kernel_mode(env); + is_64_bit =3D env->kernel_is_64_bit; + mutex_unlock(&env->lock); =20 - return env->kernel_is_64_bit; + return is_64_bit; } =20 bool perf_arch_is_big_endian(const char *arch) @@ -474,7 +478,6 @@ int perf_env__read_pmu_mappings(struct perf_env *env) pr_debug("pmu mappings not available\n"); return -ENOENT; } - env->nr_pmu_mappings =3D pmu_num; =20 if (strbuf_init(&sb, 128 * pmu_num) < 0) return -ENOMEM; @@ -488,6 +491,7 @@ int perf_env__read_pmu_mappings(struct perf_env *env) } =20 env->pmu_mappings =3D strbuf_detach(&sb, NULL); + env->nr_pmu_mappings =3D pmu_num; =20 return 0; =20 @@ -632,7 +636,17 @@ int perf_env__read_core_pmu_caps(struct perf_env *env) =20 int perf_env__nr_cpus_avail(struct perf_env *env) { - return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0; + int nr_cpus =3D 0; + + if (!env) + return 0; + + mutex_lock(&env->lock); + if (!perf_env__read_nr_cpus_avail(env)) + nr_cpus =3D env->nr_cpus_avail; + mutex_unlock(&env->lock); + + return nr_cpus; } =20 void cpu_cache_level__free(struct cpu_cache_level *cache) @@ -780,7 +794,7 @@ static const char *e_machine_to_perf_arch(uint16_t e_ma= chine) return "unknown"; } =20 -uint16_t perf_env__e_machine_nocache(struct perf_env *env, uint32_t *e_fla= gs) +static uint16_t __perf_env__e_machine_nocache(struct perf_env *env, uint32= _t *e_flags) { uint16_t e_machine =3D EM_NONE; const char *arch =3D NULL; @@ -794,14 +808,8 @@ uint16_t perf_env__e_machine_nocache(struct perf_env *= env, uint32_t *e_flags) is_64_bit =3D env->kernel_is_64_bit; } =20 - if (!arch) { - static struct utsname uts =3D { .machine[0] =3D '\0', }; - - if (uts.machine[0] =3D=3D '\0') - uname(&uts); - if (uts.machine[0] !=3D '\0') - arch =3D uts.machine; - } + if (!arch) + arch =3D uts_machine(); =20 e_machine =3D perf_arch_to_e_machine(arch, is_64_bit); =20 @@ -811,18 +819,37 @@ uint16_t perf_env__e_machine_nocache(struct perf_env = *env, uint32_t *e_flags) return e_machine; } =20 +uint16_t perf_env__e_machine_nocache(struct perf_env *env, uint32_t *e_fla= gs) +{ + uint16_t e_machine; + + if (!env) + return __perf_env__e_machine_nocache(NULL, e_flags); + + mutex_lock(&env->lock); + e_machine =3D __perf_env__e_machine_nocache(env, e_flags); + mutex_unlock(&env->lock); + + return e_machine; +} + uint16_t perf_env__e_machine(struct perf_env *env, uint32_t *e_flags) { uint16_t e_machine; uint32_t local_e_flags =3D 0; =20 - if (env && env->e_machine !=3D EM_NONE) { + if (!env) + return __perf_env__e_machine_nocache(NULL, e_flags); + + mutex_lock(&env->lock); + if (env->e_machine !=3D EM_NONE) { if (e_flags) *e_flags =3D env->e_flags; - - return env->e_machine; + e_machine =3D env->e_machine; + mutex_unlock(&env->lock); + return e_machine; } - e_machine =3D perf_env__e_machine_nocache(env, &local_e_flags); + e_machine =3D __perf_env__e_machine_nocache(env, &local_e_flags); /* * Only cache the e_machine in perf_env if env->arch is not NULL. * If env->arch is NULL, the e_machine is just a fallback to EM_HOST. @@ -830,10 +857,12 @@ uint16_t perf_env__e_machine(struct perf_env *env, ui= nt32_t *e_flags) * thread-based session e_machine scanning later in * perf_session__e_machine(). */ - if (env && env->arch) { + if (env->arch) { env->e_machine =3D e_machine; env->e_flags =3D local_e_flags; } + mutex_unlock(&env->lock); + if (e_flags) *e_flags =3D local_e_flags; =20 @@ -846,13 +875,11 @@ const char *perf_env__arch(struct perf_env *env) const char *arch; =20 if (!env) { - static struct utsname uts =3D { .machine[0] =3D '\0', }; - uint16_t host_e_machine; + const char *host_arch =3D uts_machine(); + + if (host_arch) { + uint16_t host_e_machine =3D perf_arch_to_e_machine(host_arch, -1); =20 - if (uts.machine[0] =3D=3D '\0') - uname(&uts); - if (uts.machine[0] !=3D '\0') { - host_e_machine =3D perf_arch_to_e_machine(uts.machine, -1); return e_machine_to_perf_arch(host_e_machine); } return e_machine_to_perf_arch(EM_HOST); @@ -884,45 +911,60 @@ const char *perf_env__arch_strerrno(uint16_t e_machin= e, int err) =20 const char *perf_env__cpuid(struct perf_env *env) { - int status; + const char *cpuid; =20 + mutex_lock(&env->lock); if (!env->cpuid) { /* Assume local operation */ - status =3D perf_env__read_cpuid(env); - if (status) + if (perf_env__read_cpuid(env)) { + mutex_unlock(&env->lock); return NULL; + } } + cpuid =3D env->cpuid; + mutex_unlock(&env->lock); =20 - return env->cpuid; + return cpuid; } =20 int perf_env__nr_pmu_mappings(struct perf_env *env) { - int status; + int nr; =20 + mutex_lock(&env->lock); if (!env->nr_pmu_mappings) { /* Assume local operation */ - status =3D perf_env__read_pmu_mappings(env); - if (status) + if (perf_env__read_pmu_mappings(env)) { + mutex_unlock(&env->lock); return 0; + } } + nr =3D env->nr_pmu_mappings; + mutex_unlock(&env->lock); =20 - return env->nr_pmu_mappings; + return nr; } =20 const char *perf_env__pmu_mappings(struct perf_env *env) { - int status; + const char *mappings; =20 + mutex_lock(&env->lock); if (!env->pmu_mappings) { /* Assume local operation */ - status =3D perf_env__read_pmu_mappings(env); - if (status) + if (perf_env__read_pmu_mappings(env)) { + mutex_unlock(&env->lock); return NULL; + } } + mappings =3D env->pmu_mappings; + mutex_unlock(&env->lock); =20 - return env->pmu_mappings; + return mappings; } =20 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu) { + int node; + + mutex_lock(&env->lock); if (!env->nr_numa_map) { struct numa_node *nn; int i, nr =3D 0; @@ -939,8 +981,10 @@ int perf_env__numa_node(struct perf_env *env, struct p= erf_cpu cpu) * it for missing cpus, which return node -1 */ env->numa_map =3D malloc(nr * sizeof(int)); - if (!env->numa_map) + if (!env->numa_map) { + mutex_unlock(&env->lock); return -1; + } =20 for (i =3D 0; i < nr; i++) env->numa_map[i] =3D -1; @@ -957,25 +1001,37 @@ int perf_env__numa_node(struct perf_env *env, struct= perf_cpu cpu) } } =20 - return cpu.cpu >=3D 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.c= pu] : -1; + node =3D cpu.cpu >=3D 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu= .cpu] : -1; + mutex_unlock(&env->lock); + + return node; } =20 bool perf_env__has_pmu_mapping(struct perf_env *env, const char *pmu_name) { - char *pmu_mapping =3D env->pmu_mappings, *colon; + char *pmu_mapping, *colon; + bool ret =3D false; + + mutex_lock(&env->lock); + pmu_mapping =3D env->pmu_mappings; + if (!pmu_mapping) + goto out_unlock; =20 for (int i =3D 0; i < env->nr_pmu_mappings; ++i) { if (strtoul(pmu_mapping, &colon, 0) =3D=3D ULONG_MAX || *colon !=3D ':') - goto out_error; + goto out_unlock; =20 pmu_mapping =3D colon + 1; - if (strcmp(pmu_mapping, pmu_name) =3D=3D 0) - return true; + if (strcmp(pmu_mapping, pmu_name) =3D=3D 0) { + ret =3D true; + break; + } =20 pmu_mapping +=3D strlen(pmu_mapping) + 1; } -out_error: - return false; +out_unlock: + mutex_unlock(&env->lock); + return ret; } =20 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name, @@ -1043,12 +1099,13 @@ void perf_env__find_br_cntr_info(struct perf_env *e= nv, =20 bool perf_env__is_x86_amd_cpu(struct perf_env *env) { - static int is_amd; /* 0: Uninitialized, 1: Yes, -1: No */ + bool is_amd; =20 - if (is_amd =3D=3D 0) - is_amd =3D env->cpuid && strstarts(env->cpuid, "AuthenticAMD") ? 1 : -1; + mutex_lock(&env->lock); + is_amd =3D env->cpuid && strstarts(env->cpuid, "AuthenticAMD"); + mutex_unlock(&env->lock); =20 - return is_amd >=3D 1 ? true : false; + return is_amd; } =20 bool x86__is_amd_cpu(void) @@ -1066,12 +1123,13 @@ bool x86__is_amd_cpu(void) =20 bool perf_env__is_x86_intel_cpu(struct perf_env *env) { - static int is_intel; /* 0: Uninitialized, 1: Yes, -1: No */ + bool is_intel; =20 - if (is_intel =3D=3D 0) - is_intel =3D env->cpuid && strstarts(env->cpuid, "GenuineIntel") ? 1 : -= 1; + mutex_lock(&env->lock); + is_intel =3D env->cpuid && strstarts(env->cpuid, "GenuineIntel"); + mutex_unlock(&env->lock); =20 - return is_intel >=3D 1 ? true : false; + return is_intel; } =20 bool x86__is_intel_cpu(void) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index a58b4fe44b62..51511f0c00e9 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -4235,8 +4235,17 @@ uint16_t perf_session__e_machine(struct perf_session= *session, uint32_t *e_flags * more accurate threads. */ env =3D perf_session__env(session); - if (env && env->e_machine !=3D EM_NONE) - return perf_env__e_machine(env, e_flags); + if (env) { + uint16_t e_machine; + + mutex_lock(&env->lock); + e_machine =3D env->e_machine; + if (e_machine !=3D EM_NONE && e_flags) + *e_flags =3D env->e_flags; + mutex_unlock(&env->lock); + if (e_machine !=3D EM_NONE) + return e_machine; + } =20 /* * Compute from threads, note this is more accurate than @@ -4249,8 +4258,10 @@ uint16_t perf_session__e_machine(struct perf_session= *session, uint32_t *e_flags =20 if (args.e_machine !=3D EM_NONE) { if (env) { + mutex_lock(&env->lock); env->e_machine =3D args.e_machine; env->e_flags =3D args.e_flags; + mutex_unlock(&env->lock); } if (e_flags) *e_flags =3D args.e_flags; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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=1795212845; b=LpzsVESwSKfdF0e/qyPwNiESK8VNxExvlT9i75BiOASbuVpZ17hB1qCGaQW9kFrXMzQl h9HyGNa4TCbzgvihcEsqrjy3Vl1IQ1MCBu0esAMvKkXPXE3azwemBYjcpy+WFr25toMO4 pgwFBrgelTq35LxgH8czi0mePADoeK1PzSVjhDmz8pu6yC3db6eJcGgoZqR39PW8ZQVJi u0rrvumgpZ9upAlwRTRkk3VSWcgxniK5npUegZAom5JHM67gS2yFnnRafp93x3oT2hfP9 I3jEvdIWsSb5KABxCa10CnKVPXTNHWKoSI+zeRW4l1l82uAElXOpNqrj6rHIQxMgRkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212845; 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=6u3lfcuJ5SxRL1qCsNxQHjo7pzGjwuwb1sgBf6pApoI=; b=RZL2evYU2JCopcw84YL7M8zcD8XawCf1LrbWoVMHb54nDVcGzjMRSnykKp3acGMJiEkx gBQQS4aV1TtWzZsW8CNIARyEQAP90o10133AWPYnR40vLcnZJYxtjjbv0QN/X19ApEyrA 0/4ifiSLALw7QFT/ciCHmaZJNIv8dnpduz+sdyd7rzg9mWAkkpWYpQwy94UM9qzJscO2C SHTHVmI3hdubmAIlIotbdDLIH4QzRz3ivQw2pOJMGsOoxoMpBbGwB0+grcaIEWoo7H9RH ZyNW0uAtNEyHKkrbLlYSYGJLmhKzESjcw/d4wF5Z+nehdCBazO/DKbNcDzf+SisHXgw== 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=JlzIAltq; 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 A8EE260A260C for ; Fri, 20 Nov 2026 22:14:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8EE260A260C 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=JlzIAltq Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1b3ae17c536so1336777c88.0 for ; Fri, 20 Nov 2026 14:14:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212843; x=1795817643; 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=6u3lfcuJ5SxRL1qCsNxQHjo7pzGjwuwb1sgBf6pApoI=; b=JlzIAltqRug3q3kbNCBuHHXVO+LI79HBAHSNOHGIAXpJE1zkj2tW6Slh5RZSQgNnRl bnHsKZNZ0c/0t+wlh1VrHijvQwwTI52nSUtb96mXRKgXAutjBckAnHAC1idDX5S9WVIv METXPiZ8+vcbPoQAgfTkBGL/fUn3hj9AMBbppnNxkGOaDc5FRToyR7Dpk4lPaGYqJhhG ENdaIz+xUtT1UYyRsQtnb78YHfIIZGEkRpX4svJmDAmnCfSOWewUWaDVtO3eBa81PxYG Fws4fYCu/6HP71rSVUxPp1APOZzYQU9BN236bcRpeDgGJrykq/KYlXMxBXqy5TNbVyRc y6EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212843; x=1795817643; 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=6u3lfcuJ5SxRL1qCsNxQHjo7pzGjwuwb1sgBf6pApoI=; b=PTYuPp4srAIrkUvTXNt9c54KOQ5xref+nRG70+/d/PcH3n/gQ0loM/9576uBTV3Rgu BbMzw5ldgc0bQ2O99ps8ULkPBCEJaLAUtg0f8sAuW5hsZfx7bWm5KMJ6Edrzj1HZjilU TPtvCn+iiM3uHArpCJG5xxEudNvoDXyUMOBMjp98uuhs94U/uIS2PfQCeE5Z3+ofwDqD XtDhUiC8DHSdY9PL9R6AsfcEHp7+wnBy48QHxO+EJgg1GOJQUIRqgM5Yy1Gs7VNiDH/g vKswFKTFyJHiB5xZSfPXDXJU2Ox+3xvD/IfXM/b6Mu34hKLsJUFdgwPhCK4rvzLOeQWx WkCg== X-Forwarded-Encrypted: i=1; AL+hErHcGhpABL+6kYt7bbPAecsJs+1Oy6A7vbgHrBopXrvSQRLyGXT/TWoqQpVyaxqqB9raDY9gGD0tGvcGiWE=@vger.kernel.org X-Gm-Message-State: APuU4Z+fgSFfKASaL5DZcrod5M2nOyh8OIsGsc1z2zYi14yaNgFV+RyW 5hvcYulbVM1W7QrQ4FvbKCFi5IZR6QotOJwEpWzo19FI4/AQBw4rpZYOtZ2UjRO3JX9+92Nh3Yq ClJ5ggAH2ZQ== X-Received: from dlaj4.prod.google.com ([2002:a05:701b:2804:b0:1a1:3ce6:560b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2815:b0:1b5:2746:8cee with SMTP id a92af1059eb24-1b5568c342dmr3995988c88.18.1795212842141; Fri, 20 Nov 2026 14:14:02 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:07 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-7-irogers@google.com> Subject: [PATCH v3 06/30] perf env: Add host_env() singleton 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add host_env() to provide a statically initialized singleton struct perf_env representing the live host environment, replacing ad-hoc stack and static host_env instances across subcommands, utilities, benchmarks, and tests. Populate and clean up host_env()'s command line in run_builtin() (and route the direct 'trace' alias in main() through handle_internal_command() so it uses run_builtin() too), and always write HEADER_CMDLINE from host_env() so commands such as 'perf inject' record their own invocation rather than duplicating perf_exe() onto an input file's command line. Drop the redundant struct perf_env * parameter from machine__new_host(), machine__new_kallsyms(), machine__new_live(), and __perf_session__new(). Signed-off-by: Ian Rogers --- tools/perf/bench/maps.c | 11 ++----- tools/perf/bench/synthesize.c | 13 ++------ tools/perf/builtin-buildid-list.c | 5 +-- tools/perf/builtin-inject.c | 3 +- tools/perf/builtin-kallsyms.c | 12 ++----- tools/perf/builtin-lock.c | 6 +--- tools/perf/builtin-top.c | 17 +++------- tools/perf/builtin-trace.c | 18 +++-------- tools/perf/perf.c | 6 +++- tools/perf/tests/code-reading.c | 5 +-- tools/perf/tests/dlfilter-test.c | 5 +-- tools/perf/tests/dwarf-unwind.c | 5 +-- tools/perf/tests/maps.c | 11 ++----- tools/perf/tests/mmap-thread-lookup.c | 5 +-- tools/perf/tests/symbols.c | 7 +---- tools/perf/util/debug.c | 6 +--- tools/perf/util/env.c | 45 +++++++++++++++++---------- tools/perf/util/env.h | 10 ++++++ tools/perf/util/header.c | 2 +- tools/perf/util/machine.c | 16 +++++----- tools/perf/util/machine.h | 28 +++++++++++++++-- tools/perf/util/parse-events.c | 9 ++---- tools/perf/util/probe-event.c | 5 +-- tools/perf/util/rwsem.h | 6 ++++ tools/perf/util/session.c | 6 ++-- tools/perf/util/session.h | 22 +++++++++++-- 26 files changed, 133 insertions(+), 151 deletions(-) diff --git a/tools/perf/bench/maps.c b/tools/perf/bench/maps.c index da05d39c5e0b..9eff91ff1ed8 100644 --- a/tools/perf/bench/maps.c +++ b/tools/perf/bench/maps.c @@ -16,7 +16,6 @@ #include "bench.h" #include "../util/debug.h" #include "../util/dso.h" -#include "../util/env.h" #include "../util/machine.h" #include "../util/map.h" #include "../util/maps.h" @@ -45,7 +44,6 @@ static int do_maps_bench(void) { struct stats asc_stats, append_stats; unsigned int total_dsos =3D nr_maps + nr_appends; - struct perf_env host_env; struct machine *machine; struct dso **dsos; struct map **all_maps; @@ -55,12 +53,9 @@ static int do_maps_bench(void) if (total_dsos =3D=3D 0) return 0; =20 - perf_env__init(&host_env); - machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/false, getpid(= )); - if (!machine) { - perf_env__exit(&host_env); + machine =3D machine__new_live(/*kernel_maps=3D*/false, getpid()); + if (!machine) return -ENOMEM; - } =20 dsos =3D calloc(total_dsos, sizeof(*dsos)); all_maps =3D calloc(total_dsos, sizeof(*all_maps)); @@ -68,7 +63,6 @@ static int do_maps_bench(void) free(dsos); free(all_maps); machine__delete(machine); - perf_env__exit(&host_env); return -ENOMEM; } =20 @@ -186,7 +180,6 @@ static int do_maps_bench(void) free(all_maps); free(dsos); machine__delete(machine); - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/bench/synthesize.c b/tools/perf/bench/synthesize.c index 265d49a913d9..6aac1ccc7f4b 100644 --- a/tools/perf/bench/synthesize.c +++ b/tools/perf/bench/synthesize.c @@ -115,16 +115,12 @@ static int run_single_threaded(void) .pid =3D "self", }; struct perf_thread_map *threads; - struct perf_env host_env; int err; =20 perf_set_singlethreaded(); - perf_env__init(&host_env); - session =3D __perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL, - /*trace_event_repipe=3D*/false, &host_env); + session =3D perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL); if (IS_ERR(session)) { pr_err("Session creation failed.\n"); - perf_env__exit(&host_env); return PTR_ERR(session); } threads =3D thread_map__new_by_pid(getpid()); @@ -149,7 +145,6 @@ static int run_single_threaded(void) perf_thread_map__put(threads); =20 perf_session__delete(session); - perf_env__exit(&host_env); return err; } =20 @@ -163,14 +158,11 @@ static int do_run_multi_threaded(struct target *targe= t, int err =3D 0; struct stats time_stats, event_stats; struct perf_session *session; - struct perf_env host_env; =20 - perf_env__init(&host_env); init_stats(&time_stats); init_stats(&event_stats); for (i =3D 0; i < multi_iterations; i++) { - session =3D __perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL, - /*trace_event_repipe=3D*/false, &host_env); + session =3D perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL); if (IS_ERR(session)) { err =3D PTR_ERR(session); goto err_out; @@ -209,7 +201,6 @@ static int do_run_multi_threaded(struct target *target, printf(" Average time per event %.3f usec\n", time_average / event_average); err_out: - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid= -list.c index 5555f5dcefe4..f6159300ee63 100644 --- a/tools/perf/builtin-buildid-list.c +++ b/tools/perf/builtin-buildid-list.c @@ -45,14 +45,11 @@ static int buildid__map_cb(struct map *map, void *arg _= _maybe_unused) =20 static void buildid__show_kernel_maps(void) { - struct perf_env host_env; struct machine *machine; =20 - perf_env__init(&host_env); - machine =3D machine__new_host(&host_env); + machine =3D machine__new_host(); machine__for_each_kernel_map(machine, buildid__map_cb, NULL); machine__delete(machine); - perf_env__exit(&host_env); } =20 static int sysfs__fprintf_build_id(FILE *fp) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 3f3108fd3981..26c4a9216c93 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -2884,8 +2884,7 @@ int cmd_inject(int argc, const char **argv) } } inject.session =3D __perf_session__new(&data, tool, - /*trace_event_repipe=3D*/inject.output.is_pipe, - /*host_env=3D*/NULL); + /*trace_event_repipe=3D*/inject.output.is_pipe); =20 if (IS_ERR(inject.session)) { ret =3D PTR_ERR(inject.session); diff --git a/tools/perf/builtin-kallsyms.c b/tools/perf/builtin-kallsyms.c index 3c4339982b16..e85beb380d9d 100644 --- a/tools/perf/builtin-kallsyms.c +++ b/tools/perf/builtin-kallsyms.c @@ -19,17 +19,10 @@ =20 static int __cmd_kallsyms(int argc, const char **argv) { - int i, err; - struct perf_env host_env; + int i, err =3D 0; struct machine *machine =3D NULL; =20 - - perf_env__init(&host_env); - err =3D perf_env__set_cmdline(&host_env, argc, argv); - if (err) - goto out; - - machine =3D machine__new_kallsyms(&host_env); + machine =3D machine__new_kallsyms(); if (machine =3D=3D NULL) { pr_err("Couldn't read /proc/kallsyms\n"); err =3D -1; @@ -54,7 +47,6 @@ static int __cmd_kallsyms(int argc, const char **argv) } out: machine__delete(machine); - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 778923c92f42..6d557c552f36 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -2015,7 +2015,6 @@ static int __cmd_contention(int argc, const char **ar= gv) .owner =3D show_lock_owner, .cgroups =3D RB_ROOT, }; - struct perf_env host_env; =20 lockhash_table =3D calloc(LOCKHASH_SIZE, sizeof(*lockhash_table)); if (!lockhash_table) @@ -2032,9 +2031,7 @@ static int __cmd_contention(int argc, const char **ar= gv) eops.mmap2 =3D perf_event__process_mmap2; eops.tracing_data =3D perf_event__process_tracing_data; =20 - perf_env__init(&host_env); - session =3D __perf_session__new(use_bpf ? NULL : &data, &eops, - /*trace_event_repipe=3D*/false, &host_env); + session =3D perf_session__new(use_bpf ? NULL : &data, &eops); =20 if (IS_ERR(session)) { pr_err("Initializing perf session failed\n"); @@ -2157,7 +2154,6 @@ static int __cmd_contention(int argc, const char **ar= gv) evlist__put(con.evlist); lock_contention_finish(&con); perf_session__delete(session); - perf_env__exit(&host_env); zfree(&lockhash_table); return err; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index a272e6649077..cf509b1467a7 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1669,7 +1669,6 @@ int cmd_top(int argc, const char **argv) NULL }; int status =3D hists__init(); - struct perf_env host_env; =20 if (status < 0) return status; @@ -1683,7 +1682,6 @@ int cmd_top(int argc, const char **argv) if (top.evlist =3D=3D NULL) return -ENOMEM; =20 - perf_env__init(&host_env); status =3D perf_config(perf_top_config, &top); if (status) goto out_put_evlist; @@ -1691,11 +1689,7 @@ int cmd_top(int argc, const char **argv) * Since the per arch annotation init routine may need the cpuid, read * it here, since we are not getting this from the perf.data header. */ - status =3D perf_env__set_cmdline(&host_env, argc, argv); - if (status) - goto out_put_evlist; - - status =3D perf_env__read_cpuid(&host_env); + status =3D perf_env__read_cpuid(host_env()); if (status) { /* * Some arches do not provide a get_cpuid(), so just use pr_debug, other= wise @@ -1814,7 +1808,7 @@ int cmd_top(int argc, const char **argv) symbol_conf.show_branchflag_count =3D true; =20 if (opts->branch_stack) { - status =3D perf_env__read_core_pmu_caps(&host_env); + status =3D perf_env__read_core_pmu_caps(host_env()); if (status) { pr_err("PMU capability data is not available\n"); goto out_put_evlist; @@ -1834,9 +1828,7 @@ int cmd_top(int argc, const char **argv) =20 setup_browser(false); =20 - top.session =3D __perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL, - /*trace_event_repipe=3D*/false, - &host_env); + top.session =3D perf_session__new(/*data=3D*/NULL, /*tool=3D*/NULL); if (IS_ERR(top.session)) { status =3D PTR_ERR(top.session); top.session =3D NULL; @@ -1923,7 +1915,7 @@ int cmd_top(int argc, const char **argv) goto out_put_evlist; } =20 - if (evlist__add_bpf_sb_event(top.sb_evlist, &host_env)) { + if (evlist__add_bpf_sb_event(top.sb_evlist, host_env())) { pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); status =3D -EINVAL; evlist__put(top.sb_evlist); @@ -1947,7 +1939,6 @@ int cmd_top(int argc, const char **argv) evlist__put(top.evlist); perf_session__delete(top.session); annotation_options__exit(); - perf_env__exit(&host_env); =20 return status; } diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 20505654ec9d..1dde3a400c51 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -142,7 +142,6 @@ struct syscall_fmt { }; =20 struct trace { - struct perf_env host_env; struct perf_tool tool; struct { /** Sorted sycall numbers used by the trace. */ @@ -2121,20 +2120,14 @@ static char *trace__machine__resolve_kernel_addr(vo= id *vmachine, unsigned long l return machine__resolve_kernel_addr(vmachine, addrp, modp); } =20 -static int trace__symbols_init(struct trace *trace, int argc, const char *= *argv, - struct evlist *evlist) +static int trace__symbols_init(struct trace *trace, struct evlist *evlist) { int err =3D symbol__init(NULL); =20 if (err) return err; =20 - perf_env__init(&trace->host_env); - err =3D perf_env__set_cmdline(&trace->host_env, argc, argv); - if (err) - goto out; - - trace->host =3D machine__new_host(&trace->host_env); + trace->host =3D machine__new_host(); if (trace->host =3D=3D NULL) { err =3D -ENOMEM; goto out; @@ -2155,10 +2148,8 @@ static int trace__symbols_init(struct trace *trace, = int argc, const char **argv, /*mmap_data=3D*/false, /*nr_threads_synthesize=3D*/1); out: - if (err) { - perf_env__exit(&trace->host_env); + if (err) symbol__exit(); - } return err; } =20 @@ -2167,7 +2158,6 @@ static void trace__symbols__exit(struct trace *trace) machine__delete(trace->host); trace->host =3D NULL; =20 - perf_env__exit(&trace->host_env); symbol__exit(); } =20 @@ -4838,7 +4828,7 @@ static int trace__run(struct trace *trace, int argc, = const char **argv) goto out_put_evlist; } =20 - err =3D trace__symbols_init(trace, argc, argv, evlist); + err =3D trace__symbols_init(trace, evlist); if (err < 0) { fprintf(trace->output, "Problems initializing symbol libraries!\n"); goto out_put_evlist; diff --git a/tools/perf/perf.c b/tools/perf/perf.c index d60bc929c257..8e6f321db15f 100644 --- a/tools/perf/perf.c +++ b/tools/perf/perf.c @@ -32,6 +32,7 @@ #include "util/build-id.h" #include "util/config.h" #include "util/debug.h" +#include "util/env.h" #ifdef HAVE_LIBTRACEEVENT #include "util/trace-event.h" #endif @@ -380,12 +381,14 @@ static int run_builtin(const struct cmd_struct *p, in= t argc, const char **argv) use_pager =3D 1; commit_pager_choice(); =20 + perf_env__set_cmdline(host_env(), argc, argv); status =3D p->fn(argc, argv); perf_config__exit(); #ifdef HAVE_LIBTRACEEVENT trace_event__exit(); #endif exit_browser(status); + perf_env__exit(host_env()); =20 if (status) return status & 0xff; @@ -543,7 +546,8 @@ int main(int argc, const char **argv) if (strstarts(cmd, "trace")) { setup_path(); argv[0] =3D "trace"; - return cmd_trace(argc, argv); + handle_internal_command(argc, argv); + goto out; } /* Look for flags.. */ argv++; diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 7b80c4ab6916..5eb30a791efa 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -715,12 +715,10 @@ static int do_test_code_reading(bool try_kcore) struct dso *dso; const char *events[] =3D { "cpu-cycles", "cpu-cycles:u", "cpu-clock", "cp= u-clock:u", NULL }; int evidx =3D 0; - struct perf_env host_env; =20 pid =3D getpid(); =20 - perf_env__init(&host_env); - machine =3D machine__new_host(&host_env); + machine =3D machine__new_host(); if (!machine) { pr_debug("machine__new_host failed\n"); goto out_err; @@ -856,7 +854,6 @@ static int do_test_code_reading(bool try_kcore) perf_cpu_map__put(cpus); perf_thread_map__put(threads); machine__delete(machine); - perf_env__exit(&host_env); tested_sections__free(&tested_sections); =20 return err; diff --git a/tools/perf/tests/dlfilter-test.c b/tools/perf/tests/dlfilter-t= est.c index 204663571943..312fc7e8c49a 100644 --- a/tools/perf/tests/dlfilter-test.c +++ b/tools/perf/tests/dlfilter-test.c @@ -322,7 +322,6 @@ static int run_perf_script(struct test_data *td) =20 static int test__dlfilter_test(struct test_data *td) { - struct perf_env host_env; u64 sample_type =3D TEST_SAMPLE_TYPE; pid_t pid =3D 12345; pid_t tid =3D 12346; @@ -356,8 +355,7 @@ static int test__dlfilter_test(struct test_data *td) return test_result("Failed to find program symbols", TEST_FAIL); =20 pr_debug("Creating new host machine structure\n"); - perf_env__init(&host_env); - td->machine =3D machine__new_host(&host_env); + td->machine =3D machine__new_host(); =20 td->fd =3D creat(td->perf_data_file_name, 0644); if (td->fd < 0) @@ -390,7 +388,6 @@ static int test__dlfilter_test(struct test_data *td) =20 err =3D run_perf_script(td) ? TEST_FAIL : TEST_OK; out: - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwin= d.c index 9ed78d00fb87..d324c9c09b0b 100644 --- a/tools/perf/tests/dwarf-unwind.c +++ b/tools/perf/tests/dwarf-unwind.c @@ -181,7 +181,6 @@ NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__= krava_1(struct thread *th noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused, int subtest __maybe_unused) { - struct perf_env host_env; struct machine *machine; struct thread *thread; int err =3D -1; @@ -190,8 +189,7 @@ noinline int test__dwarf_unwind(struct test_suite *test= __maybe_unused, callchain_param.record_mode =3D CALLCHAIN_DWARF; dwarf_callchain_users =3D true; =20 - perf_env__init(&host_env); - machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/true, pid); + machine =3D machine__new_live(/*kernel_maps=3D*/true, pid); if (!machine) { pr_err("Could not get machine\n"); goto out; @@ -216,7 +214,6 @@ noinline int test__dwarf_unwind(struct test_suite *test= __maybe_unused, =20 out: machine__delete(machine); - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index ff6d25f9c043..2fbe61e1a64c 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -5,7 +5,6 @@ #include #include "debug.h" #include "dso.h" -#include "env.h" #include "map.h" #include "maps.h" #include "tests.h" @@ -102,12 +101,10 @@ static int test__maps__merge_in(struct test_suite *t = __maybe_unused, int subtest }; struct map *map_kcore1, *map_kcore2, *map_kcore3; int ret; - struct perf_env host_env; struct machine *machine; struct maps *maps; =20 - perf_env__init(&host_env); - machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/false, getpid(= )); + machine =3D machine__new_live(/*kernel_maps=3D*/false, getpid()); maps =3D maps__new(machine); =20 TEST_ASSERT_VAL("failed to create machine", machine); @@ -169,7 +166,6 @@ static int test__maps__merge_in(struct test_suite *t __= maybe_unused, int subtest map__zput(map_kcore2); map__zput(map_kcore3); machine__delete(machine); - perf_env__exit(&host_env); return TEST_OK; } =20 @@ -200,12 +196,10 @@ static int test__maps__fixup_overlap_and_insert(struc= t test_suite *t __maybe_unu struct map *map_split, *map_eclipse; int ret; unsigned int i; - struct perf_env host_env; struct machine *machine; struct maps *maps; =20 - perf_env__init(&host_env); - machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/false, getpid(= )); + machine =3D machine__new_live(/*kernel_maps=3D*/false, getpid()); maps =3D maps__new(machine); =20 TEST_ASSERT_VAL("failed to create machine", machine); @@ -249,7 +243,6 @@ static int test__maps__fixup_overlap_and_insert(struct = test_suite *t __maybe_unu =20 maps__zput(maps); machine__delete(machine); - perf_env__exit(&host_env); return TEST_OK; } =20 diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-= thread-lookup.c index 0c5619c6e6e9..7559123983c5 100644 --- a/tools/perf/tests/mmap-thread-lookup.c +++ b/tools/perf/tests/mmap-thread-lookup.c @@ -156,7 +156,6 @@ static int synth_process(struct machine *machine) =20 static int mmap_events(synth_cb synth) { - struct perf_env host_env; struct machine *machine; int err, i; =20 @@ -169,8 +168,7 @@ static int mmap_events(synth_cb synth) */ TEST_ASSERT_VAL("failed to create threads", !threads_create()); =20 - perf_env__init(&host_env); - machine =3D machine__new_host(&host_env); + machine =3D machine__new_host(); =20 dump_trace =3D verbose > 1 ? 1 : 0; =20 @@ -212,7 +210,6 @@ static int mmap_events(synth_cb synth) } =20 machine__delete(machine); - perf_env__exit(&host_env); return err; } =20 diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c index d3180db45ee6..dce397f92006 100644 --- a/tools/perf/tests/symbols.c +++ b/tools/perf/tests/symbols.c @@ -18,18 +18,15 @@ #include "tests.h" =20 struct test_info { - struct perf_env host_env; struct machine *machine; struct thread *thread; }; =20 static int init_test_info(struct test_info *ti) { - perf_env__init(&ti->host_env); - ti->machine =3D machine__new_host(&ti->host_env); + ti->machine =3D machine__new_host(); if (!ti->machine) { pr_debug("machine__new_host() failed!\n"); - perf_env__exit(&ti->host_env); return TEST_FAIL; } =20 @@ -37,7 +34,6 @@ static int init_test_info(struct test_info *ti) ti->thread =3D machine__findnew_thread(ti->machine, 100, 100); if (!ti->thread) { pr_debug("machine__findnew_thread() failed!\n"); - perf_env__exit(&ti->host_env); return TEST_FAIL; } =20 @@ -48,7 +44,6 @@ static void exit_test_info(struct test_info *ti) { thread__put(ti->thread); machine__delete(ti->machine); - perf_env__exit(&ti->host_env); } =20 struct dso_map { diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index b7095519419e..dfba9ddeacb7 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -312,10 +312,8 @@ void __dump_stack(FILE *file, void **stackdump, size_t= stackdump_size) pid_t pid =3D getpid(); struct machine *machine; struct thread *thread =3D NULL; - struct perf_env host_env; =20 - perf_env__init(&host_env); - machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/false, pid); + machine =3D machine__new_live(/*kernel_maps=3D*/false, pid); =20 if (machine) thread =3D machine__find_thread(machine, pid, pid); @@ -328,7 +326,6 @@ void __dump_stack(FILE *file, void **stackdump, size_t = stackdump_size) */ backtrace_symbols_fd(stackdump, stackdump_size, fileno(file)); machine__delete(machine); - perf_env__exit(&host_env); return; } #endif @@ -355,7 +352,6 @@ void __dump_stack(FILE *file, void **stackdump, size_t = stackdump_size) } thread__put(thread); machine__delete(machine); - perf_env__exit(&host_env); } =20 /* Obtain a backtrace and print it to stdout. */ diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 4ab46ca6b4ef..1fe6ce84e607 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -13,6 +13,7 @@ #include #include "cgroup.h" #include +#include #include #include #include @@ -246,6 +247,22 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_m= ap, u32 schedstat_version, zfree(&cd_map); } =20 +static struct perf_env __host_env =3D { +#ifdef HAVE_LIBBPF_SUPPORT + .bpf_progs =3D { + .lock =3D __PERF_RWSEM_INITIALIZER, + .infos =3D RB_ROOT, + .btfs =3D RB_ROOT, + }, +#endif + .cgroups =3D { + .lock =3D __PERF_RWSEM_INITIALIZER, + .tree =3D RB_ROOT, + }, + .kernel_is_64_bit =3D -1, + .lock =3D { .lock =3D __PERF_MUTEX_INITIALIZER }, +}; + void perf_env__exit(struct perf_env *env) { int i, j; @@ -313,6 +330,11 @@ void perf_env__init(struct perf_env *env) mutex_init(&env->lock); } =20 +struct perf_env *host_env(void) +{ + return &__host_env; +} + static const char *uts_machine(void) { static struct utsname uts; @@ -409,6 +431,7 @@ int perf_env__set_cmdline(struct perf_env *env, int arg= c, const char *argv[]) int i; =20 /* do not include NULL termination */ + zfree(&env->cmdline_argv); env->cmdline_argv =3D calloc(argc, sizeof(char *)); if (env->cmdline_argv =3D=3D NULL) goto out_enomem; @@ -1110,15 +1133,10 @@ bool perf_env__is_x86_amd_cpu(struct perf_env *env) =20 bool x86__is_amd_cpu(void) { - struct perf_env env =3D { .total_mem =3D 0, }; - bool is_amd; + struct perf_env *env =3D host_env(); =20 - perf_env__init(&env); - perf_env__cpuid(&env); - is_amd =3D perf_env__is_x86_amd_cpu(&env); - perf_env__exit(&env); - - return is_amd; + perf_env__cpuid(env); + return perf_env__is_x86_amd_cpu(env); } =20 bool perf_env__is_x86_intel_cpu(struct perf_env *env) @@ -1134,13 +1152,8 @@ bool perf_env__is_x86_intel_cpu(struct perf_env *env) =20 bool x86__is_intel_cpu(void) { - struct perf_env env =3D { .total_mem =3D 0, }; - bool is_intel; - - perf_env__init(&env); - perf_env__cpuid(&env); - is_intel =3D perf_env__is_x86_intel_cpu(&env); - perf_env__exit(&env); + struct perf_env *env =3D host_env(); =20 - return is_intel; + perf_env__cpuid(env); + return perf_env__is_x86_intel_cpu(env); } diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index 23d35425efe3..134102148a5c 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -221,6 +221,16 @@ const char *perf_env__cpuid(struct perf_env *env); int perf_env__nr_cpus_avail(struct perf_env *env); =20 void perf_env__init(struct perf_env *env); + +/** + * host_env() - Return the singleton struct perf_env for the live host. + * + * Returns a pointer to the statically initialized global struct perf_env + * representing the live host. + * + * Return: Pointer to the live host's struct perf_env. + */ +struct perf_env *host_env(void); #ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 9eec6deee190..12d82775b88a 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -666,7 +666,7 @@ static int write_event_desc(struct feat_fd *ff, static int write_cmdline(struct feat_fd *ff, struct evlist *evlist __maybe_unused) { - struct perf_env *env =3D &ff->ph->env; + struct perf_env *env =3D host_env(); char pbuf[MAXPATHLEN], *buf; int i, ret, n; =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 5b4550e97172..bf23015085f4 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -130,7 +130,7 @@ int machine__init(struct machine *machine, const char *= root_dir, pid_t pid) return err; } =20 -static struct machine *__machine__new_host(struct perf_env *host_env, bool= kernel_maps) +static struct machine *__machine__new_host(bool kernel_maps) { struct machine *machine =3D malloc(sizeof(*machine)); =20 @@ -146,13 +146,13 @@ static struct machine *__machine__new_host(struct per= f_env *host_env, bool kerne free(machine); return NULL; } - machine->env =3D host_env; + machine->env =3D host_env(); return machine; } =20 -struct machine *machine__new_host(struct perf_env *host_env) +struct machine *machine__new_host(void) { - return __machine__new_host(host_env, /*kernel_maps=3D*/true); + return __machine__new_host(/*kernel_maps=3D*/true); } =20 static int mmap_handler(const struct perf_tool *tool __maybe_unused, @@ -172,9 +172,9 @@ static int machine__init_live(struct machine *machine, = pid_t pid) mmap_handler, machine, true); } =20 -struct machine *machine__new_live(struct perf_env *host_env, bool kernel_m= aps, pid_t pid) +struct machine *machine__new_live(bool kernel_maps, pid_t pid) { - struct machine *machine =3D __machine__new_host(host_env, kernel_maps); + struct machine *machine =3D __machine__new_host(kernel_maps); =20 if (!machine) return NULL; @@ -186,9 +186,9 @@ struct machine *machine__new_live(struct perf_env *host= _env, bool kernel_maps, p return machine; } =20 -struct machine *machine__new_kallsyms(struct perf_env *host_env) +struct machine *machine__new_kallsyms(void) { - struct machine *machine =3D machine__new_host(host_env); + struct machine *machine =3D machine__new_host(); /* * FIXME: * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 80191545ee8a..a2290a3ab3fc 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -178,9 +178,31 @@ struct thread *machine__findnew_guest_code(struct mach= ine *machine, pid_t pid); void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size); void machines__set_comm_exec(struct machines *machines, bool comm_exec); =20 -struct machine *machine__new_host(struct perf_env *host_env); -struct machine *machine__new_kallsyms(struct perf_env *host_env); -struct machine *machine__new_live(struct perf_env *host_env, bool kernel_m= aps, pid_t pid); +/** + * machine__new_host() - Allocate and initialize a struct machine for the = live + * host with kernel maps populated. + * + * Return: Pointer to the newly allocated struct machine, or NULL on failu= re. + */ +struct machine *machine__new_host(void); + +/** + * machine__new_kallsyms() - Allocate and initialize a struct machine for = the + * live host and load /proc/kallsyms. + * + * Return: Pointer to the newly allocated struct machine, or NULL on failu= re. + */ +struct machine *machine__new_kallsyms(void); + +/** + * machine__new_live() - Allocate and initialize a struct machine for live + * inspection of @pid on the host. + * @kernel_maps: Whether to create kernel maps on the machine. + * @pid: Process ID whose mmap events should be synthesized. + * + * Return: Pointer to the newly allocated struct machine, or NULL on failu= re. + */ +struct machine *machine__new_live(bool kernel_maps, pid_t pid); int machine__init(struct machine *machine, const char *root_dir, pid_t pid= ); void machine__exit(struct machine *machine); void machine__delete_threads(struct machine *machine); diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index ca13fcc05db0..ad2999cb5371 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -585,13 +585,8 @@ size_t default_breakpoint_len(void) #if defined(__i386__) static int len; =20 - if (len =3D=3D 0) { - struct perf_env env =3D {}; - - perf_env__init(&env); - len =3D perf_env__kernel_is_64_bit(&env) ? sizeof(u64) : sizeof(long); - perf_env__exit(&env); - } + if (len =3D=3D 0) + len =3D perf_env__kernel_is_64_bit(host_env()) ? sizeof(u64) : sizeof(lo= ng); return len; #elif defined(__aarch64__) return 4; diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 1534280c0c1d..d042d8f69f59 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -74,14 +74,12 @@ int e_snprintf(char *str, size_t size, const char *form= at, ...) } =20 static struct machine *host_machine; -static struct perf_env host_env; =20 /* Initialize symbol maps and path of vmlinux/modules */ int init_probe_symbol_maps(bool user_only) { int ret; =20 - perf_env__init(&host_env); symbol_conf.allow_aliases =3D true; ret =3D symbol__init(NULL); if (ret < 0) { @@ -95,7 +93,7 @@ int init_probe_symbol_maps(bool user_only) if (symbol_conf.vmlinux_name) pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); =20 - host_machine =3D machine__new_host(&host_env); + host_machine =3D machine__new_host(); if (!host_machine) { pr_debug("machine__new_host() failed.\n"); symbol__exit(); @@ -112,7 +110,6 @@ void exit_probe_symbol_maps(void) machine__delete(host_machine); host_machine =3D NULL; symbol__exit(); - perf_env__exit(&host_env); } =20 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap) diff --git a/tools/perf/util/rwsem.h b/tools/perf/util/rwsem.h index b102d8143181..d2bf722e409c 100644 --- a/tools/perf/util/rwsem.h +++ b/tools/perf/util/rwsem.h @@ -18,6 +18,12 @@ struct LOCKABLE rw_semaphore { #endif }; =20 +#if RWS_ERRORCHECK +#define __PERF_RWSEM_INITIALIZER { .mtx =3D { .lock =3D __PERF_MUTEX_INITI= ALIZER } } +#else +#define __PERF_RWSEM_INITIALIZER { .lock =3D PTHREAD_RWLOCK_INITIALIZER } +#endif + int init_rwsem(struct rw_semaphore *sem); int exit_rwsem(struct rw_semaphore *sem); =20 diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 51511f0c00e9..c75646a49ca7 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -151,8 +151,7 @@ static int ordered_events__deliver_event(struct ordered= _events *oe, =20 struct perf_session *__perf_session__new(struct perf_data *data, struct perf_tool *tool, - bool trace_event_repipe, - struct perf_env *host_env) + bool trace_event_repipe) { int ret =3D -ENOMEM; struct perf_session *session =3D zalloc(sizeof(*session)); @@ -206,8 +205,7 @@ struct perf_session *__perf_session__new(struct perf_da= ta *data, symbol_conf.kallsyms_name =3D perf_data__kallsyms_name(data); } } else { - assert(host_env !=3D NULL); - session->machines.host.env =3D host_env; + session->machines.host.env =3D host_env(); } if (session->evlist) evlist__set_session(session->evlist, session); diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h index 05ea2c1ee8c9..737c5d3e92f5 100644 --- a/tools/perf/util/session.h +++ b/tools/perf/util/session.h @@ -107,15 +107,31 @@ struct decomp { =20 struct perf_tool; =20 +/** + * __perf_session__new() - Allocate and initialize a new struct perf_sessi= on. + * @data: Input/output perf_data descriptor, or NULL for a live host sessi= on. + * @tool: Event processing callbacks, or NULL. + * @trace_event_repipe: Whether to repipe trace event records to stdout. + * + * When @data is NULL, associates the session's host machine with host_env= (). + * + * Return: Pointer to the new session, or an ERR_PTR() on failure. + */ struct perf_session *__perf_session__new(struct perf_data *data, struct perf_tool *tool, - bool trace_event_repipe, - struct perf_env *host_env); + bool trace_event_repipe); =20 +/** + * perf_session__new() - Allocate and initialize a new struct perf_session. + * @data: Input/output perf_data descriptor, or NULL for a live host sessi= on. + * @tool: Event processing callbacks, or NULL. + * + * Return: Pointer to the new session, or an ERR_PTR() on failure. + */ static inline struct perf_session *perf_session__new(struct perf_data *dat= a, struct perf_tool *tool) { - return __perf_session__new(data, tool, /*trace_event_repipe=3D*/false, /*= host_env=3D*/NULL); + return __perf_session__new(data, tool, /*trace_event_repipe=3D*/false); } =20 void perf_session__delete(struct perf_session *session); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212860; b=gESwtXRwwbJCn4ateyV4UnYwzPW+AK+6IKaXHwLqy0LB0LxIqWI4ESU5wQEpCmT1GoZJ g75k8dl1rdM6/VcCx99QGJ36QjGTJlLtIMAX7ay8ErTc/z/mET3xc6dWoUD9ahGpy2VOs enH9DEJBc8SpM2PVKVo2/W3MPlp/j3XuJ2Up3P4wKHq2Vo61d/R97oXGgmN8ARwqiKfGC zy8c+QGuIaL5UgjlpTRu1/D5XinD4k7oWwCqysnnzpJOv7wlcjRSiLNYxeefUXZm+mvK+ TS5oRbXxg16PIZ+UNztDkt7BWesojjF/YfRyqx9j7/p/ted7a6kW+hQZ2z+tiUyAlZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212860; 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=a4aFWtCTxXXnKU0JSJ5Rs/oq/q1Vz1+RyM7IGPyxnvQ=; b=gMPnfuw+sk2vyXB8bTM+GoFmGlpQDRvIK/30WlyiyJjvVwrzJXvHt/+K2RqCANPKTTPZ lC1yKbJ+xyTNj6aobIcue4AArzhGD4VBYdjgNhh2DXHVF5IFXiuNKNSMM/El11X2MzUlD XuiURT87eoHCxdogQwbeMpnLSVfWVOTlVq0ZPOSggsTbWDhRVjcfsqsaeT4a6P3HdBumT fsS0qV4fb8Dp92CEs6I0CzqUWUas7dZxzqEQe8RYCI5uLgKfQGPqhljNeBqTFaObOyyID 4Xx25LnRJk7wG69f/hY1rNiz/FETvnmcyp4nZdUk9swhHiCIZK+E4pojDVHFTREkYdg== 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=WQSWDXru; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (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 67822606A33F for ; Fri, 20 Nov 2026 22:14:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 67822606A33F 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=WQSWDXru Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2ee04c49451so18638915ad.2 for ; Fri, 20 Nov 2026 14:14:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212850; x=1795817650; 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=a4aFWtCTxXXnKU0JSJ5Rs/oq/q1Vz1+RyM7IGPyxnvQ=; b=WQSWDXruOCVnkGTYXEtQ35CkrioNvYSQtFWT/lflJbB2FlzoELX/LmjNpxo1NTszto qwvmaZkPRWwjLs7jqZw54cPVG7ZD4pytI3a9s+BgQKAvjzX64Ram1SN+OR+yUACfCTOv Od0mqko5smJYOsZjxdq+nbAZ6GwA3hbxA8g5IzYy+j/yNxXFngvXqQdd5P5rU0k8ZMRq tihQgs6FYJDO08u4ArDoN9lcNlpPu5w/aQaX/s/R2N9oIYCD8gglmb3TzkK9oiFrO0Zj UVzv8TBwVZFtbEF3XgIhKzst3LB1fAxXtlvhKiDCzYMPUOAprBphApY5gvEQ+62dXPqR BITg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212850; x=1795817650; 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=a4aFWtCTxXXnKU0JSJ5Rs/oq/q1Vz1+RyM7IGPyxnvQ=; b=KWyZkdxxfjn+E8NGJg6iN0XTMNkFV5EGAsozUFgODVbBCA/a2RqJBRLsb+JBGVuIet MI1pIQ+tQRSe1nO24i6IP/MWJ/YQbF41z3VsPEnafjBmLOzc1eMlwcr0eQ9cHJWSzNbE unUPz7I4UN7LVZJUkHLSYyUxWJ6m6gf2GRaZdUNofm1i9iYIRL9SyCuyqsVpxFA0c48f 9QdUy+vTcJKyRL924yI4EDnCS52jArD6nzzFxAuF3/PhppvCQjyQmDILSmKMgCrB3tkb TuIbJonIESNMIg/WJdqRNYFb3HFeB/Y6cj9Y0NjqUF6rwKRLmLMCLsbnbYTHHcMsouEO QQPA== X-Forwarded-Encrypted: i=1; AL+hErEkJIkV3oZZjuTOoK6PDqf0DtT7Nszs6TnVZEZroLSflqI7YoO5f9kfUWhx5RJa0UegrvXUJkXRhDr+w3c=@vger.kernel.org X-Gm-Message-State: APuU4Z9EoW4IGZUxDk3LRrRg75PCzBuqpzxksvTdFTUeK6/WQHX6eMHL x2iIN7K+wSv5hkrB0oLIe/ht4nMEZmgziJGQrpG0nMecAC5PjUBti8lgVmDbsYiepPvMjY3DvXz rD37ikT5ABg== X-Received: from plbjg14.prod.google.com ([2002:a17:903:26ce:b0:2f0:41cb:f4d0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:37d0:b0:2ed:e22b:77ac with SMTP id d9443c01a7336-2f03f11e58dmr42348455ad.23.1795212849412; Fri, 20 Nov 2026 14:14:09 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:08 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-8-irogers@google.com> Subject: [PATCH v3 07/30] perf pmu: Add from_env parameter to perf_pmu__init() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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(). The hwmon and drm evsel open and read callbacks fail with -EINVAL for such PMUs. Signed-off-by: Ian Rogers --- tools/perf/tests/pmu-events.c | 4 ++-- tools/perf/util/drm_pmu.c | 16 ++++++++++---- tools/perf/util/hwmon_pmu.c | 16 +++++++++++--- tools/perf/util/pmu.c | 41 +++++++++++++++++++---------------- tools/perf/util/pmu.h | 13 ++++++++++- tools/perf/util/tool_pmu.c | 2 +- 6 files changed, 62 insertions(+), 30 deletions(-) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index e6e4f4cf1fa2..acf8e8ee1237 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -514,7 +514,7 @@ static int __test_core_pmu_event_aliases(const char *pm= u_name, int *count) if (!pmu) return -1; =20 - if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, pmu_name) !=3D 0) { + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, pmu_name, /*from_env=3D*/fals= e) !=3D 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; =20 - if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, test_pmu->pmu_name) !=3D 0) { + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, test_pmu->pmu_name, /*from_en= v=3D*/false) !=3D 0) { perf_pmu__delete(pmu); return -1; } diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c index 5e4bdd8e7fa8..7ef4126ee7e9 100644 --- a/tools/perf/util/drm_pmu.c +++ b/tools/perf/util/drm_pmu.c @@ -115,7 +115,7 @@ static struct drm_pmu *add_drm_pmu(struct list_head *pm= us, char *line, size_t li if (!drm) return NULL; =20 - if (perf_pmu__init(&drm->pmu, type, name) !=3D 0) { + if (perf_pmu__init(&drm->pmu, type, name, /*from_env=3D*/false) !=3D 0) { perf_pmu__delete(&drm->pmu); return NULL; } @@ -515,10 +515,12 @@ int evsel__drm_pmu_open(struct evsel *evsel, struct perf_thread_map *threads, int start_cpu_map_idx, int end_cpu_map_idx) { - (void)evsel; (void)threads; (void)start_cpu_map_idx; (void)end_cpu_map_idx; + /* PMUs reconstructed from a perf.data file can't be opened. */ + if (!evsel->pmu || evsel->pmu->from_env) + return -EINVAL; return 0; } =20 @@ -657,11 +659,17 @@ static uint64_t drm_pmu__read_for_pid(struct drm_pmu = *drm, struct evsel *evsel, =20 int evsel__drm_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread) { - struct drm_pmu *drm =3D container_of(evsel->pmu, struct drm_pmu, pmu); + struct drm_pmu *drm; struct perf_counts_values *count, *old_count =3D NULL; - int pid =3D perf_thread_map__pid(evsel->core.threads, thread); + int pid; uint64_t counter; =20 + if (!evsel->pmu || evsel->pmu->from_env) + return -EINVAL; + + drm =3D container_of(evsel->pmu, struct drm_pmu, pmu); + pid =3D perf_thread_map__pid(evsel->core.threads, thread); + if (pid !=3D -1) counter =3D drm_pmu__read_for_pid(drm, evsel, pid); else diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c index 96aeb379e169..1526afa37e87 100644 --- a/tools/perf/util/hwmon_pmu.c +++ b/tools/perf/util/hwmon_pmu.c @@ -365,7 +365,7 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus,= const char *hwmon_dir, if (!hwm) return NULL; =20 - if (perf_pmu__init(&hwm->pmu, type, buf) !=3D 0) { + if (perf_pmu__init(&hwm->pmu, type, buf, /*from_env=3D*/false) !=3D 0) { perf_pmu__delete(&hwm->pmu); return NULL; } @@ -763,12 +763,18 @@ int evsel__hwmon_pmu_open(struct evsel *evsel, struct perf_thread_map *threads, int start_cpu_map_idx, int end_cpu_map_idx) { - struct hwmon_pmu *hwm =3D container_of(evsel->pmu, struct hwmon_pmu, pmu); + struct hwmon_pmu *hwm; union hwmon_pmu_event_key key =3D { .type_and_num =3D evsel->core.attr.config, }; int idx =3D 0, thread =3D 0, nthreads, err =3D 0; - int dir =3D open(hwm->hwmon_dir, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + int dir; + + if (!evsel->pmu || evsel->pmu->from_env) + return -EINVAL; + + hwm =3D container_of(evsel->pmu, struct hwmon_pmu, pmu); + dir =3D open(hwm->hwmon_dir, O_CLOEXEC | O_DIRECTORY | O_RDONLY); =20 if (dir < 0) return -errno; @@ -815,6 +821,10 @@ int evsel__hwmon_pmu_read(struct evsel *evsel, int cpu= _map_idx, int thread) ssize_t len; struct perf_counts_values *count, *old_count =3D NULL; =20 + /* PMUs reconstructed from a perf.data file can't be read. */ + if (!evsel->pmu || evsel->pmu->from_env) + return -EINVAL; + if (evsel->prev_raw_counts) old_count =3D perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 1ae8bcf71c68..665f4b9bcf44 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1197,9 +1197,10 @@ static bool aliases__equal(long key1, long key2, voi= d *ctx __maybe_unused) return strcasecmp((const char *)key1, (const char *)key2) =3D=3D 0; } =20 -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, boo= l from_env) { pmu->type =3D type; + pmu->from_env =3D from_env; INIT_LIST_HEAD(&pmu->format); INIT_LIST_HEAD(&pmu->caps); =20 @@ -1249,7 +1250,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *p= mus, int dirfd, const char if (!pmu) return NULL; =20 - if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, name) !=3D 0) { + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, name, /*from_env=3D*/false) != =3D 0) { perf_pmu__delete(pmu); return NULL; } @@ -1704,9 +1705,9 @@ int perf_pmu__config_terms(const struct perf_pmu *pmu, struct parse_events_term *term; =20 if (perf_pmu__is_hwmon(pmu)) - return hwmon_pmu__config_terms(pmu, attr, terms, err); + return pmu->from_env ? -EINVAL : hwmon_pmu__config_terms(pmu, attr, term= s, err); if (perf_pmu__is_drm(pmu)) - return drm_pmu__config_terms(pmu, attr, terms, err); + return pmu->from_env ? -EINVAL : drm_pmu__config_terms(pmu, attr, terms,= err); =20 list_for_each_entry(term, &terms->terms, list) { if (pmu_config_term(pmu, attr, term, terms, zero, apply_hardcoded, err)) @@ -1866,11 +1867,11 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, str= uct parse_events_terms *head_ info->retirement_latency_max =3D 0.0; =20 if (perf_pmu__is_hwmon(pmu)) { - ret =3D hwmon_pmu__check_alias(head_terms, info, err); + ret =3D pmu->from_env ? 0 : hwmon_pmu__check_alias(head_terms, info, err= ); goto out; } if (perf_pmu__is_drm(pmu)) { - ret =3D drm_pmu__check_alias(pmu, head_terms, info, err); + ret =3D pmu->from_env ? 0 : drm_pmu__check_alias(pmu, head_terms, info, = err); goto out; } =20 @@ -2127,11 +2128,11 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, con= st 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=3D*/ true) !=3D NULL) return true; if (pmu->cpu_aliases_added || (!pmu->events_table && !pmu->is_core)) @@ -2148,11 +2149,11 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) size_t nr; =20 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); =20 pmu_aliases_parse(pmu); nr =3D pmu->sysfs_aliases + pmu->sys_json_aliases; @@ -2284,11 +2285,11 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, = bool skip_duplicate_pmus, size_t size_rem; =20 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); =20 pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); @@ -2793,10 +2794,12 @@ void perf_pmu__delete(struct perf_pmu *pmu) if (!pmu) return; =20 - 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); + } =20 perf_pmu__del_formats(&pmu->format); perf_pmu__del_aliases(pmu); diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 2c8916ad073e..ea55882ce670 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -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,16 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t siz= e, int perf_pmu__event_source_devices_fd(void); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags); =20 -int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name); +/** + * perf_pmu__init() - Initialize a pre-allocated struct perf_pmu. + * @pmu: The PMU structure to initialize. + * @type: The PMU type identifier (PERF_TYPE_* or dynamic sysfs type). + * @name: The PMU name string (copied via strdup). + * @from_env: True if reconstructed from a perf_env header rather than liv= e sysfs. + * + * Return: 0 on success, -ENOMEM if duplicating @name fails. + */ +int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name, boo= l 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 *c= ore_pmus); diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index d2f7591747d9..6054d5d5aece 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -714,7 +714,7 @@ struct perf_pmu *tool_pmu__new(void) if (!tool) return NULL; =20 - if (perf_pmu__init(tool, PERF_PMU_TYPE_TOOL, "tool") !=3D 0) { + if (perf_pmu__init(tool, PERF_PMU_TYPE_TOOL, "tool", /*from_env=3D*/false= ) !=3D 0) { perf_pmu__delete(tool); return NULL; } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212870; b=u9AM+n0EI8wiZYDWrWcxK4c45B4pLVZYAoQRPsTDB1XogHOYOIYXAtIXEmRwaAgWHUvf 1ecCx/hVuuqafrOlxZV6a1WKqPr0pMLGkdohU46WXe74HTDeUM84rX2DQXGZi9+oGa+dL 1rS/WQnMLyC6mhOX4kOlbBWYaWGHfF9D4f279ceaJrVC2IQP5OhqIGDzHF9KVXDzpaubf Mut+eMdXokq5nq7j2oM4X1uIFw1BmUskU/f4tUxi3PvbP0DdGn6lM+T959NgXlq1vjMF1 YpAP6wNGBMslPhickK3pNSHKg8bXsYMU8ftQnn0bRARmf8vJNKMDI8Ju6av9G/q4J4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212870; 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=DmNQK/ZGXBH6FTbGznjl+6RV6y+HJEOJ4H7TW0t9NaA=; b=Z+0iALAVZ2bgewAMPbPYb5mEJumAc6PGUUF137Oye+SYOveH42uGWAVxFBPEZ+hFk6yE /GW/CLV4RZ6Yh2/8+95kLFSr6OdnjbsgUboPiiD/kmrkH2+yDHgXjelygvFMxwrK+/bGW Tt2aRyFeVNEQsNNW687qgIQPV+ykljod8DnQVcei9RLj+04mYy/R/dwGUHSbd85SSGwRD GCV3SwEYygqEU8NvDFmTqgtyA5MFeDowR3fWVU71LLxg+Sma7ZRR0zBsFgVh/hIaaERcU mvengEZplrFCijJHixyG4zHGy7dxKdmB7fEY5+bkJ74KYqYky0/mgL1UC+cRiVpc1DQ== 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=ZxJtqre0; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 8FD63606A6AC for ; Fri, 20 Nov 2026 22:14:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8FD63606A6AC 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=ZxJtqre0 Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f0467a525eso9763005ad.2 for ; Fri, 20 Nov 2026 14:14:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212857; x=1795817657; 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=DmNQK/ZGXBH6FTbGznjl+6RV6y+HJEOJ4H7TW0t9NaA=; b=ZxJtqre0jUIxhFt76bzAOeqLn0aVIte294kLS00LWzY1sjZauXMvHBhKuFUpjN+L6T 5EOQV2VefhGLJPtRP9SbAX4l0KmI+GlLeRkOFcF8eTcFGmP+gFyd+gFzbmK848KltIlY IWd5Nz4SARUCA9YMNofjKygZB/9GRfBYsghZWThMKsiYr8K3BW6AW4UWRREQkdNutKIk UJvMNwVpSyxb6a5ZpqE6RV+NhWCt8eO4Q/vr/KZKf864IUs/pbehp8nVE/9GVc6Mh55d BY+bfiHCxC1r8gZ9XYfP8g8F0WQ0y2w73byI8kaT6cB+BXdRFiqq31jI+2BH1QiBUtDj Gv7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212857; x=1795817657; 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=DmNQK/ZGXBH6FTbGznjl+6RV6y+HJEOJ4H7TW0t9NaA=; b=Ay8UAtSTEmMmAINntd8Vr3tDCoEnKSUxnpnXkKFv9TzvdbSymbgxoS/m2Mcwc/5ny+ w86g79MHXPwglgud6HnuoHOZQF9g46+xqetie2QehVs4wpbSAQEnE6A1vSK9qOmILonk UNhC+pdcB4uJkWsntB+iP82KSx4uF1/r7Qh+UEYclLQAGFuUv7F2wD99rGGh8e6o64tu OBxq6uyQPEuWqe/NHrTZksM8sPFmveDoLEdsT+SoO2/8L/PBKd1+QP8y9g2r1gWzl8rt +kI7fXFnSZ/1nXH93/EBXFga2UNdMTeiYn8biLL+/avtoNPeWhdIyeapcHHvF1/9hE67 Rb3g== X-Forwarded-Encrypted: i=1; AL+hErEGlgoGJjJ6ufiZi7RAuWm25bCnFKDkRAZrUo3REMFNjCbIdXFqGlY8bAEndTwfzf4GoQc4lIAelDIcbUQ=@vger.kernel.org X-Gm-Message-State: APuU4Z8yCmNAvGxnwbnaRXY7rL9nhkz8/aRGdmpy9RSWpUU/ZhUrvEtB hvb0vIJoqzjwSWPsZv/AJ1ISJlMyrtIcNkfnXi26McfO1GjS2BJ6KjRttxaKFS1H51rWq97AdAz sFAZcJrobMQ== X-Received: from plbmo12.prod.google.com ([2002:a17:903:a8c:b0:2ed:1d20:a33a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1ce:b0:2ef:d157:e128 with SMTP id d9443c01a7336-2f03f1e8493mr47150315ad.52.1795212856444; Fri, 20 Nov 2026 14:14:16 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:09 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-9-irogers@google.com> Subject: [PATCH v3 08/30] perf pmus: Introduce struct perf_pmus and perf_env__pmus() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the static global core_pmus, other_pmus, read_pmu_types, num_core_pmus, and extended_type state from pmus.c into a new struct perf_pmus embedded in struct perf_env and accessed via perf_env__pmus(). Rename perf_pmus__destroy() to perf_pmus__exit() to match the perf_env__init()/perf_env__exit() naming convention, and update it along with perf_pmus__find_by_attr(), perf_pmus__scan_for_event(), perf_pmus__scan_matching_wildcard(), perf_pmus__scan_for_uncore_id(), perf_pmus__pmu_for_pmu_filter(), perf_pmus__print_pmu_events(), perf_pmus__print_raw_pmu_events(), perf_pmus__have_event(), perf_pmus__num_core_pmus(), perf_pmus__supports_extended_type(), perf_pmus__add_test_pmu(), and perf_pmus__add_test_hwmon_pmu() to take struct perf_pmus *. To keep this commit reviewable, the high-fanout perf_pmus__find(), perf_pmus__find_by_type(), perf_pmus__find_core_pmu(), perf_pmus__scan(), and perf_pmus__scan_core() helpers are updated to take struct perf_pmus * in the next two commits. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/hybrid.c | 3 +- tools/perf/bench/pmu-scan.c | 7 +- tools/perf/builtin-list.c | 19 +- tools/perf/builtin-record.c | 2 +- tools/perf/builtin-stat.c | 2 +- tools/perf/tests/hwmon_pmu.c | 4 +- tools/perf/tests/mmap-basic.c | 4 +- tools/perf/tests/parse-events.c | 15 +- tools/perf/tests/pmu.c | 3 +- tools/perf/util/cputopo.c | 3 +- tools/perf/util/env.c | 12 +- tools/perf/util/env.h | 10 + tools/perf/util/evlist.c | 13 +- tools/perf/util/evlist.h | 2 + tools/perf/util/evsel.c | 14 +- .../util/kvm-stat-arch/kvm-stat-powerpc.c | 3 +- tools/perf/util/metricgroup.c | 6 +- tools/perf/util/parse-events.c | 44 ++- tools/perf/util/parse-events.h | 3 + .../perf/util/perf-regs-arch/perf_regs_x86.c | 7 +- tools/perf/util/pmu.c | 9 +- tools/perf/util/pmu.h | 3 + tools/perf/util/pmus.c | 343 +++++++++++------- tools/perf/util/pmus.h | 205 ++++++++++- tools/perf/util/print-events.c | 7 +- tools/perf/util/session.c | 11 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/stat-shadow.c | 3 +- 28 files changed, 560 insertions(+), 201 deletions(-) diff --git a/tools/perf/arch/x86/tests/hybrid.c b/tools/perf/arch/x86/tests= /hybrid.c index 0477e17b8e53..011217998a3e 100644 --- a/tools/perf/arch/x86/tests/hybrid.c +++ b/tools/perf/arch/x86/tests/hybrid.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "arch-tests.h" #include "debug.h" +#include "env.h" #include "evlist.h" #include "evsel.h" #include "pmu.h" @@ -303,7 +304,7 @@ static int test_events(const struct evlist_test *events= , int cnt) =20 int test__hybrid(struct test_suite *test __maybe_unused, int subtest __may= be_unused) { - if (perf_pmus__num_core_pmus() =3D=3D 1) + if (perf_pmus__num_core_pmus(perf_env__pmus(host_env())) =3D=3D 1) return TEST_SKIP; =20 return test_events(test__hybrid_events, ARRAY_SIZE(test__hybrid_events)); diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c index 14a464ad8cea..1d7dd4b203e0 100644 --- a/tools/perf/bench/pmu-scan.c +++ b/tools/perf/bench/pmu-scan.c @@ -8,6 +8,7 @@ #include #include "bench.h" #include "util/debug.h" +#include "util/env.h" #include "util/pmu.h" #include "util/pmus.h" #include "util/stat.h" @@ -42,6 +43,7 @@ static struct pmu_scan_result *results; =20 static int save_result(void) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; struct list_head *list; struct pmu_scan_result *r; @@ -69,7 +71,7 @@ static int save_result(void) nr_pmus++; } =20 - perf_pmus__destroy(); + perf_pmus__exit(pmus); return 0; } =20 @@ -128,6 +130,7 @@ static void delete_result(void) =20 static int run_pmu_scan(void) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct stats stats; struct timeval start, end, diff; double time_average, time_stddev; @@ -158,7 +161,7 @@ static int run_pmu_scan(void) update_stats(&stats, runtime_us); =20 ret =3D check_result(core_only); - perf_pmus__destroy(); + perf_pmus__exit(pmus); if (ret < 0) break; } diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c index 47668d66c395..573d13baa7fd 100644 --- a/tools/perf/builtin-list.c +++ b/tools/perf/builtin-list.c @@ -11,6 +11,7 @@ #include "builtin.h" =20 #include "util/print-events.h" +#include "util/env.h" #include "util/pmus.h" #include "util/pmu.h" #include "util/debug.h" @@ -567,6 +568,7 @@ static int list__tui(int argc, const char **argv) =20 int cmd_list(int argc, const char **argv) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); int i, ret =3D 0; struct print_state default_ps =3D { .fp =3D stdout, @@ -660,7 +662,8 @@ int cmd_list(int argc, const char **argv) if (unit_name) ps->pmu_glob =3D strdup(unit_name); else if (cputype) { - const struct perf_pmu *pmu =3D perf_pmus__pmu_for_pmu_filter(cputype); + const struct perf_pmu *pmu =3D + perf_pmus__pmu_for_pmu_filter(pmus, cputype); =20 if (!pmu) { pr_err("ERROR: cputype is not supported!\n"); @@ -692,7 +695,7 @@ int cmd_list(int argc, const char **argv) ret =3D -1; goto out; } - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); zfree(&default_ps.pmu_glob); default_ps.pmu_glob =3D old_pmu_glob; } else if (strcmp(argv[i], "hw") =3D=3D 0 || @@ -704,7 +707,7 @@ int cmd_list(int argc, const char **argv) ret =3D -1; goto out; } - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); zfree(&ps->event_glob); ps->event_glob =3D old_event_glob; } else if (strcmp(argv[i], "sw") =3D=3D 0 || @@ -718,7 +721,7 @@ int cmd_list(int argc, const char **argv) ret =3D -1; goto out; } - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); zfree(&ps->pmu_glob); } ps->pmu_glob =3D old_pmu_glob; @@ -731,12 +734,12 @@ int cmd_list(int argc, const char **argv) ret =3D -1; goto out; } - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); zfree(&ps->event_glob); ps->event_glob =3D old_event_glob; } else if (strcmp(argv[i], "pmu") =3D=3D 0) { ps->exclude_abi =3D true; - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); ps->exclude_abi =3D false; } else if (strcmp(argv[i], "sdt") =3D=3D 0) print_sdt_events(&print_cb, ps); @@ -770,7 +773,7 @@ int cmd_list(int argc, const char **argv) ret =3D -1; goto out; } - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); zfree(&ps->pmu_glob); ps->pmu_glob =3D old_pmu_glob; print_sdt_events(&print_cb, ps); @@ -785,7 +788,7 @@ int cmd_list(int argc, const char **argv) continue; } ps->event_glob =3D s; - perf_pmus__print_pmu_events(&print_cb, ps); + perf_pmus__print_pmu_events(pmus, &print_cb, ps); print_sdt_events(&print_cb, ps); ps->metrics =3D true; ps->metricgroups =3D true; diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index ad81458989b5..7de04919d194 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -936,7 +936,7 @@ static int record__config_tracking_events(struct record= *rec) * delay of waiting or event synthesis. */ if (opts->target.initial_delay || target__has_cpu(&opts->target) || - perf_pmus__num_core_pmus() > 1) { + perf_pmus__num_core_pmus(perf_env__pmus(host_env())) > 1) { /* * User space tasks can migrate between CPUs, so when tracing * selected CPUs, sideband for all CPUs is still needed. diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 37752d6bfef4..8706798290eb 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1209,7 +1209,7 @@ static int parse_cputype(const struct option *opt, return -1; } =20 - pmu =3D perf_pmus__pmu_for_pmu_filter(str); + pmu =3D perf_pmus__pmu_for_pmu_filter(perf_env__pmus(host_env()), str); if (!pmu) { fprintf(stderr, "--cputype %s is not supported!\n", str); return -1; diff --git a/tools/perf/tests/hwmon_pmu.c b/tools/perf/tests/hwmon_pmu.c index e26b3fe3fab1..8f3d09d3f2a1 100644 --- a/tools/perf/tests/hwmon_pmu.c +++ b/tools/perf/tests/hwmon_pmu.c @@ -11,6 +11,7 @@ #include =20 #include "debug.h" +#include "env.h" #include "evlist.h" #include "parse-events.h" #include "pmus.h" @@ -137,7 +138,8 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t = sz) } =20 /* Make the PMU reading the files created above. */ - hwm =3D perf_pmus__add_test_hwmon_pmu(dir, "hwmon1234", test_hwmon_name); + hwm =3D perf_pmus__add_test_hwmon_pmu(perf_env__pmus(host_env()), + dir, "hwmon1234", test_hwmon_name); if (!hwm) pr_err("Test hwmon creation failed\n"); =20 diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index 602a78d48b10..dada4af5fb68 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -17,6 +17,7 @@ =20 #include "cpumap.h" #include "debug.h" +#include "env.h" #include "event.h" #include "evlist.h" #include "evsel.h" @@ -264,6 +265,7 @@ static enum user_read_state set_user_read(struct perf_p= mu *pmu, =20 static int test_stat_user_read(u64 event, enum user_read_state enabled) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; struct perf_thread_map *threads =3D perf_thread_map__new_dummy(); int ret =3D TEST_OK; @@ -279,7 +281,7 @@ static int test_stat_user_read(u64 event, enum user_rea= d_state enabled) enum user_read_state saved_user_read_state =3D set_user_read(pmu, enable= d); struct perf_event_attr attr =3D { .type =3D PERF_TYPE_HARDWARE, - .config =3D perf_pmus__supports_extended_type() + .config =3D perf_pmus__supports_extended_type(pmus) ? event | ((u64)pmu->type << PERF_PMU_TYPE_SHIFT) : event, }; diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-event= s.c index d5f0f4ca7ccd..45c83d7d01d6 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include "parse-events.h" +#include "env.h" #include "evsel.h" #include "evsel_fprintf.h" #include "evlist.h" @@ -68,7 +69,7 @@ static int num_core_entries(struct evlist *evlist) if (!evsel->pmu->is_core) continue; if (core_pmu !=3D evsel->pmu && core_pmu !=3D NULL) - return perf_pmus__num_core_pmus(); + return perf_pmus__num_core_pmus(perf_env__pmus(host_env())); core_pmu =3D evsel->pmu; } return 1; @@ -1030,7 +1031,8 @@ static int test__group3(struct evlist *evlist __maybe= _unused) struct evsel *evsel, *group1_leader =3D NULL, *group2_leader =3D NULL; =20 TEST_ASSERT_EVLIST("wrong number of entries", - evlist__nr_entries(evlist) =3D=3D (3 * perf_pmus__num_core_pmus() + = 2), + evlist__nr_entries(evlist) =3D=3D + (3 * perf_pmus__num_core_pmus(perf_env__pmus(host_env())) + 2), evlist); /* * Currently the software event won't be grouped with the hardware event @@ -1590,6 +1592,7 @@ static int test__checkevent_pinned_modifier(struct ev= list *evlist) =20 static int test__pinned_group(struct evlist *evlist) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct evsel *evsel =3D NULL, *leader; =20 TEST_ASSERT_EVLIST("wrong number of entries", @@ -1605,7 +1608,7 @@ static int test__pinned_group(struct evlist *evlist) TEST_ASSERT_EVSEL("wrong group name", !evsel->group_name, evsel); TEST_ASSERT_EVSEL("wrong leader", evsel__has_leader(evsel, leader), evse= l); /* TODO: The group modifier is not copied to the split group leader. */ - if (perf_pmus__num_core_pmus() =3D=3D 1) + if (perf_pmus__num_core_pmus(pmus) =3D=3D 1) TEST_ASSERT_EVSEL("wrong pinned", evsel->core.attr.pinned, evsel); =20 /* cache-misses - can not be pinned, but will go on with the leader */ @@ -1643,6 +1646,7 @@ static int test__checkevent_exclusive_modifier(struct= evlist *evlist) =20 static int test__exclusive_group(struct evlist *evlist) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct evsel *evsel =3D NULL, *leader; =20 TEST_ASSERT_EVLIST("wrong number of entries", @@ -1658,7 +1662,7 @@ static int test__exclusive_group(struct evlist *evlis= t) TEST_ASSERT_EVSEL("wrong group name", !evsel->group_name, evsel); TEST_ASSERT_EVSEL("wrong leader", evsel__has_leader(evsel, leader), evse= l); /* TODO: The group modifier is not copied to the split group leader. */ - if (perf_pmus__num_core_pmus() =3D=3D 1) + if (perf_pmus__num_core_pmus(pmus) =3D=3D 1) TEST_ASSERT_EVSEL("wrong exclusive", evsel->core.attr.exclusive, evsel); =20 /* cache-misses - can not be pinned, but will go on with the leader */ @@ -1811,7 +1815,8 @@ static int test__ratio_to_prev(struct evlist *evlist) struct evsel *evsel, *leader; =20 TEST_ASSERT_VAL("wrong number of entries", - 2 * perf_pmus__num_core_pmus() =3D=3D evlist__nr_entries(evlist)); + 2 * perf_pmus__num_core_pmus(perf_env__pmus(host_env())) =3D=3D + evlist__nr_entries(evlist)); =20 evlist__for_each_entry(evlist, evsel) { if (evsel !=3D evsel__leader(evsel) || diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index f208932590df..fc731857ec71 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include "evlist.h" +#include "env.h" #include "evsel.h" #include "parse-events.h" #include "pmu.h" @@ -143,7 +144,7 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t = sz) close(file); =20 /* Make the PMU reading the files created above. */ - pmu =3D perf_pmus__add_test_pmu(dirfd, "perf-pmu-test"); + pmu =3D perf_pmus__add_test_pmu(perf_env__pmus(host_env()), dirfd, "perf-= pmu-test"); if (!pmu) pr_err("Test PMU creation failed\n"); =20 diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c index e0091804fe98..04ab4d010149 100644 --- a/tools/perf/util/cputopo.c +++ b/tools/perf/util/cputopo.c @@ -470,9 +470,10 @@ static int load_hybrid_node(struct hybrid_topology_nod= e *node, =20 struct hybrid_topology *hybrid_topology__new(void) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; struct hybrid_topology *tp =3D NULL; - int nr =3D perf_pmus__num_core_pmus(), i =3D 0; + int nr =3D perf_pmus__num_core_pmus(pmus), i =3D 0; =20 if (nr <=3D 1) return NULL; diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 1fe6ce84e607..5f16920f0a89 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -261,6 +261,7 @@ static struct perf_env __host_env =3D { }, .kernel_is_64_bit =3D -1, .lock =3D { .lock =3D __PERF_MUTEX_INITIALIZER }, + .pmus =3D PERF_PMUS_INIT(__host_env.pmus), }; =20 void perf_env__exit(struct perf_env *env) @@ -316,6 +317,8 @@ void perf_env__exit(struct perf_env *env) zfree(&env->pmu_caps); zfree(&env->memory_ranges); free_cpu_domain_info(env->cpu_domain, env->schedstat_version, env->nr_cpu= s_avail); + perf_pmus__exit(&env->pmus); + mutex_destroy(&env->pmus.lock); } =20 void perf_env__init(struct perf_env *env) @@ -328,6 +331,7 @@ void perf_env__init(struct perf_env *env) #endif env->kernel_is_64_bit =3D -1; mutex_init(&env->lock); + perf_pmus__init(&env->pmus); } =20 struct perf_env *host_env(void) @@ -335,6 +339,11 @@ struct perf_env *host_env(void) return &__host_env; } =20 +struct perf_pmus *perf_env__pmus(struct perf_env *env) +{ + return &env->pmus; +} + static const char *uts_machine(void) { static struct utsname uts; @@ -595,12 +604,13 @@ static int __perf_env__read_core_pmu_caps(const struc= t perf_pmu *pmu, =20 int perf_env__read_core_pmu_caps(struct perf_env *env) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct pmu_caps *pmu_caps; struct perf_pmu *pmu =3D NULL; int nr_pmu, i =3D 0, j; int ret; =20 - nr_pmu =3D perf_pmus__num_core_pmus(); + nr_pmu =3D perf_pmus__num_core_pmus(pmus); =20 if (!nr_pmu) return -ENODEV; diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index 134102148a5c..e38e953060c9 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -5,6 +5,7 @@ #include #include #include "cpumap.h" +#include "pmus.h" #include "rwsem.h" #include "mutex.h" =20 @@ -169,6 +170,7 @@ struct perf_env { } clock; /* Protects lazy environment initialization (e.g. os_release, e_machine).= */ struct mutex lock; + struct perf_pmus pmus; }; =20 enum perf_compress_type { @@ -231,6 +233,14 @@ void perf_env__init(struct perf_env *env); * Return: Pointer to the live host's struct perf_env. */ struct perf_env *host_env(void); + +/** + * perf_env__pmus() - Return the struct perf_pmus associated with @env. + * @env: Environment whose PMU collection to retrieve. + * + * Return: Pointer to the struct perf_pmus for @env. + */ +struct perf_pmus *perf_env__pmus(struct perf_env *env); #ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index c2402e4791b6..4e641ccc176f 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -64,6 +64,7 @@ #include "pmus.h" #include "record.h" #include "sample.h" +#include "session.h" #include "stat.h" #include "strbuf.h" #include "string2.h" @@ -2941,11 +2942,14 @@ static bool evlist__disable_uniquify(struct evlist = *evlist) return false; first =3D false; if (counter->pmu) { + struct perf_pmus *pmus =3D + counter->pmu->pmus ?: perf_env__pmus(host_env()); + /* Allow uniquify for uncore PMUs. */ if (!counter->pmu->is_core) return false; /* Keep hybrid event names uniquified for clarity. */ - if (perf_pmus__num_core_pmus() > 1) + if (perf_pmus__num_core_pmus(pmus) > 1) return false; } last_pmu =3D counter->pmu; @@ -3006,3 +3010,10 @@ bool evlist__needs_bpf_sb_event(struct evlist *evlis= t) =20 return false; } + +struct perf_env *evlist__env(struct evlist *evlist) +{ + struct perf_session *session =3D evlist ? evlist__session(evlist) : NULL; + + return session ? perf_session__env(session) : NULL; +} diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index 0f380421e03d..fc32979e08b8 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -23,6 +23,7 @@ #include "rblist.h" =20 struct perf_cpu_map; +struct perf_env; struct perf_stat_config; struct pollfd; struct record_opts; @@ -684,5 +685,6 @@ void evlist__warn_user_requested_cpus(struct evlist *ev= list, const char *cpu_lis void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf= _stat_config *config); bool evlist__has_bpf_output(struct evlist *evlist); bool evlist__needs_bpf_sb_event(struct evlist *evlist); +struct perf_env *evlist__env(struct evlist *evlist); =20 #endif /* __PERF_EVLIST_H */ diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 737a8680aaae..5949636dbf3d 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2290,9 +2290,12 @@ bool __evsel__match(const struct evsel *evsel, u32 t= ype, u64 config) return true; if (type !=3D PERF_TYPE_HARDWARE && type !=3D PERF_TYPE_HW_CACHE) return false; - if ((e_type =3D=3D PERF_TYPE_HARDWARE || e_type =3D=3D PERF_TYPE_HW_CACHE= ) && - perf_pmus__supports_extended_type()) - e_config &=3D PERF_HW_EVENT_MASK; + if (e_type =3D=3D PERF_TYPE_HARDWARE || e_type =3D=3D PERF_TYPE_HW_CACHE)= { + struct perf_env *env =3D evsel__env((struct evsel *)evsel) ?: host_env(); + + if (perf_pmus__supports_extended_type(perf_env__pmus(env))) + e_config &=3D PERF_HW_EVENT_MASK; + } if (e_type =3D=3D type && e_config =3D=3D config) return true; if (type =3D=3D PERF_TYPE_HARDWARE && evsel->pmu && evsel->pmu->is_core && @@ -4782,10 +4785,13 @@ void evsel__zero_per_pkg(struct evsel *evsel) */ bool evsel__is_hybrid(const struct evsel *evsel) { + struct perf_env *env; + if (!evsel->core.is_pmu_core) return false; =20 - return perf_pmus__num_core_pmus() > 1; + env =3D evsel__env((struct evsel *)evsel) ?: host_env(); + return perf_pmus__num_core_pmus(perf_env__pmus(env)) > 1; } =20 struct evsel *evsel__leader(const struct evsel *evsel) diff --git a/tools/perf/util/kvm-stat-arch/kvm-stat-powerpc.c b/tools/perf/= util/kvm-stat-arch/kvm-stat-powerpc.c index 315b24f9a592..d3435174106b 100644 --- a/tools/perf/util/kvm-stat-arch/kvm-stat-powerpc.c +++ b/tools/perf/util/kvm-stat-arch/kvm-stat-powerpc.c @@ -3,6 +3,7 @@ #include "../kvm-stat.h" #include "../parse-events.h" #include "../debug.h" +#include "../env.h" #include "../evsel.h" #include "../evlist.h" #include "../pmus.h" @@ -178,7 +179,7 @@ int __kvm_add_default_arch_event_powerpc(int *argc, con= st char **argv) { int j =3D *argc; =20 - if (!perf_pmus__have_event("trace_imc", "trace_cycles")) + if (!perf_pmus__have_event(perf_env__pmus(host_env()), "trace_imc", "trac= e_cycles")) return -EINVAL; =20 argv[j++] =3D "-e"; diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 98f3ba8f9533..63aa326944d7 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -297,8 +297,10 @@ static int setup_metric_events(const char *pmu, struct= hashmap *ids, const char *metric_id; struct evsel *ev; size_t ids_size, matched_events, i; + struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(metric_evlist) ?: h= ost_env()); bool all_pmus =3D !strcmp(pmu, "all") || !strcmp(pmu, "default_core") || - perf_pmus__num_core_pmus() =3D=3D 1 || !is_pmu_core(pmu); + perf_pmus__num_core_pmus(pmus) =3D=3D 1 || + !is_pmu_core(pmu); =20 *out_metric_events =3D NULL; ids_size =3D hashmap__size(ids); @@ -419,7 +421,7 @@ static int metricgroup__sys_event_iter(const struct pmu= _metric *pm, return 0; =20 /* Only process with the iterator if there is a a PMU that matches the ID= . */ - pmu =3D perf_pmus__scan_for_uncore_id(pmu, pm->compat); + pmu =3D perf_pmus__scan_for_uncore_id(perf_env__pmus(host_env()), pmu, pm= ->compat); return pmu ? d->fn(pm, table, d->data) : 0; } =20 diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index ad2999cb5371..a70bccbf51dc 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -174,7 +174,8 @@ static struct evsel * __add_event(struct list_head *list, int *idx, struct perf_event_attr *attr, bool init_attr, - const char *name, const char *metric_id, struct perf_pmu *pmu, + const char *name, const char *metric_id, + struct perf_pmus *pmus, struct perf_pmu *pmu, struct list_head *config_terms, struct evsel *first_wildcard_match, struct perf_cpu_map *user_cpus, u64 alternate_hw_config) { @@ -224,7 +225,7 @@ __add_event(struct list_head *list, int *idx, * warnings won't be generated. */ if (!pmu) - pmu =3D perf_pmus__find_by_attr(attr); + pmu =3D perf_pmus__find_by_attr(pmus, attr); =20 if (pmu) { is_pmu_core =3D pmu->is_core; @@ -298,19 +299,21 @@ struct evsel *parse_events__add_event(int idx, struct= perf_event_attr *attr, const char *name, const char *metric_id, struct perf_pmu *pmu) { + struct perf_pmus *pmus =3D pmu && pmu->pmus ? pmu->pmus : perf_env__pmus(= host_env()); + return __add_event(/*list=3D*/NULL, &idx, attr, /*init_attr=3D*/false, na= me, - metric_id, pmu, /*config_terms=3D*/NULL, + metric_id, pmus, pmu, /*config_terms=3D*/NULL, /*first_wildcard_match=3D*/NULL, /*cpu_list=3D*/NULL, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX); } =20 -static int add_event(struct list_head *list, int *idx, +static int add_event(struct list_head *list, struct parse_events_state *pa= rse_state, struct perf_event_attr *attr, const char *name, const char *metric_id, struct list_head *config_terms, u64 alternate_hw_config) { - return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, - /*pmu=3D*/NULL, config_terms, + return __add_event(list, &parse_state->idx, attr, /*init_attr*/true, name= , metric_id, + parse_state->pmus, /*pmu=3D*/NULL, config_terms, /*first_wildcard_match=3D*/NULL, /*cpu_list=3D*/NULL, alternate_hw_config) ? 0 : -ENOMEM; } @@ -415,7 +418,7 @@ int parse_events__decode_legacy_cache(const char *name,= int extended_pmu_type, _ cache_result =3D PERF_COUNT_HW_CACHE_RESULT_ACCESS; =20 *config =3D cache_type | (cache_op << 8) | (cache_result << 16); - if (perf_pmus__supports_extended_type()) + if (perf_pmus__host_supports_extended_type()) *config |=3D (__u64)extended_pmu_type << PERF_PMU_TYPE_SHIFT; return 0; } @@ -694,7 +697,7 @@ int parse_events_add_breakpoint(struct parse_events_sta= te *parse_state, =20 name =3D get_config_name(head_config); =20 - return add_event(list, &parse_state->idx, &attr, name, /*mertic_id=3D*/NU= LL, + return add_event(list, parse_state, &attr, name, /*mertic_id=3D*/NULL, &config_terms, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX); } =20 @@ -1035,7 +1038,7 @@ static int config_term_pmu(struct perf_event_attr *at= tr, if (!check_pmu_is_core(attr->type, term, parse_state->error)) return -EINVAL; attr->config =3D term->val.num; - if (perf_pmus__supports_extended_type()) + if (perf_pmus__supports_extended_type(parse_state->pmus)) attr->config |=3D (__u64)attr->type << PERF_PMU_TYPE_SHIFT; attr->type =3D PERF_TYPE_HARDWARE; return 0; @@ -1060,7 +1063,7 @@ static int config_term_pmu(struct perf_event_attr *at= tr, if (!check_pmu_is_core(attr->type, term, parse_state->error)) return -EINVAL; attr->config =3D term->val.num; - if (perf_pmus__supports_extended_type()) + if (perf_pmus__supports_extended_type(parse_state->pmus)) attr->config |=3D (__u64)attr->type << PERF_PMU_TYPE_SHIFT; attr->type =3D PERF_TYPE_HW_CACHE; return 0; @@ -1435,7 +1438,7 @@ static int __parse_events_add_numeric(struct parse_ev= ents_state *parse_state, attr.type =3D type; attr.config =3D config; if (extended_type && (type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D PERF_= TYPE_HW_CACHE)) { - assert(perf_pmus__supports_extended_type()); + assert(perf_pmus__supports_extended_type(parse_state->pmus)); attr.config |=3D (u64)extended_type << PERF_PMU_TYPE_SHIFT; } =20 @@ -1451,8 +1454,9 @@ static int __parse_events_add_numeric(struct parse_ev= ents_state *parse_state, metric_id =3D get_config_metric_id(head_config); cpus =3D get_config_cpu(head_config, parse_state->fake_pmu); ret =3D __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, na= me, - metric_id, pmu, &config_terms, first_wildcard_match, - cpus, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX) ? 0 : -ENOMEM; + metric_id, parse_state->pmus, pmu, &config_terms, + first_wildcard_match, cpus, + /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX) ? 0 : -ENOMEM; perf_cpu_map__put(cpus); free_config_terms(&config_terms); return ret; @@ -1468,7 +1472,7 @@ int parse_events_add_numeric(struct parse_events_stat= e *parse_state, bool found_supported =3D false; =20 /* Wildcards on numeric values are only supported by core PMUs. */ - if (wildcard && perf_pmus__supports_extended_type()) { + if (wildcard && perf_pmus__supports_extended_type(parse_state->pmus)) { struct evsel *first_wildcard_match =3D NULL; while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { int ret; @@ -1547,7 +1551,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, if (!const_parsed_terms || list_empty(&const_parsed_terms->terms)) { evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/true, /*name=3D*/NULL, - /*metric_id=3D*/NULL, pmu, + /*metric_id=3D*/NULL, parse_state->pmus, pmu, /*config_terms=3D*/NULL, first_wildcard_match, /*cpu_list=3D*/NULL, alternate_hw_config); return evsel ? 0 : -ENOMEM; @@ -1614,7 +1618,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, term_cpu =3D get_config_cpu(&parsed_terms, parse_state->fake_pmu); evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/tru= e, get_config_name(&parsed_terms), - get_config_metric_id(&parsed_terms), pmu, + get_config_metric_id(&parsed_terms), parse_state->pmus, pmu, &config_terms, first_wildcard_match, term_cpu, alternate_hw_config); perf_cpu_map__put(term_cpu); if (!evsel) { @@ -1684,7 +1688,8 @@ int parse_events_multi_pmu_add(struct parse_events_st= ate *parse_state, =20 INIT_LIST_HEAD(list); =20 - while ((pmu =3D perf_pmus__scan_for_event(pmu, event_name)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_for_event(parse_state->pmus, + pmu, event_name)) !=3D NULL) { =20 if (parse_events__filter_pmu(parse_state, pmu)) continue; @@ -1762,7 +1767,8 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pars= e_events_state *parse_state =20 pmu =3D NULL; /* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */ - while ((pmu =3D perf_pmus__scan_matching_wildcard(pmu, event_or_pmu)) != =3D NULL) { + while ((pmu =3D perf_pmus__scan_matching_wildcard(parse_state->pmus, + pmu, event_or_pmu)) !=3D NULL) { =20 if (parse_events__filter_pmu(parse_state, pmu)) continue; @@ -1987,6 +1993,7 @@ int parse_events_terms(struct parse_events_terms *ter= ms, const char *str) { struct parse_events_state parse_state =3D { .terms =3D NULL, + .pmus =3D perf_env__pmus(host_env()), .stoken =3D PE_START_TERMS, }; int ret; @@ -2319,6 +2326,7 @@ int __parse_events(struct evlist *evlist, const char = *str, .list =3D LIST_HEAD_INIT(parse_state.list), .idx =3D evlist__nr_entries(evlist), .error =3D err, + .pmus =3D perf_env__pmus(evlist__env(evlist) ?: host_env()), .stoken =3D PE_START_EVENTS, .fake_pmu =3D fake_pmu, .fake_tp =3D fake_tp, diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index e8f8cdfd1052..8df8e3cb69fe 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -18,6 +18,7 @@ struct parse_events_error; =20 struct option; struct perf_pmu; +struct perf_pmus; struct strbuf; =20 const char *event_type(size_t type); @@ -156,6 +157,8 @@ struct parse_events_state { struct parse_events_error *error; /* Holds returned terms for term parsing. */ struct parse_events_terms *terms; + /* The PMUs to search during parsing. */ + struct perf_pmus *pmus; /* Start token. */ int stoken; /* Use the fake PMU marker for testing. */ diff --git a/tools/perf/util/perf-regs-arch/perf_regs_x86.c b/tools/perf/ut= il/perf-regs-arch/perf_regs_x86.c index ea1d402b0893..2d80f119629a 100644 --- a/tools/perf/util/perf-regs-arch/perf_regs_x86.c +++ b/tools/perf/util/perf-regs-arch/perf_regs_x86.c @@ -9,6 +9,7 @@ #include =20 #include "../debug.h" +#include "../env.h" #include "../event.h" #include "../pmu.h" #include "../pmus.h" @@ -248,6 +249,7 @@ static uint64_t __arch__reg_mask(u64 sample_type, u64 m= ask, bool has_simd_regs) .exclude_kernel =3D 1, .sample_simd_regs_enabled =3D has_simd_regs, }; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); int fd; /* * In an unnamed union, init it here to build on older gcc versions @@ -258,7 +260,7 @@ static uint64_t __arch__reg_mask(u64 sample_type, u64 m= ask, bool has_simd_regs) else attr.sample_regs_user =3D mask; =20 - if (perf_pmus__num_core_pmus() > 1) { + if (perf_pmus__num_core_pmus(pmus) > 1) { struct perf_pmu *pmu =3D NULL; __u64 type =3D PERF_TYPE_RAW; =20 @@ -512,6 +514,7 @@ __support_simd_reg_class(uint64_t sample_type, uint16_t= qwords, .exclude_kernel =3D 1, .sample_simd_regs_enabled =3D 1, }; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); int fd; =20 attr.sample_period =3D 1; @@ -530,7 +533,7 @@ __support_simd_reg_class(uint64_t sample_type, uint16_t= qwords, attr.sample_simd_pred_reg_user =3D PERF_X86_SIMD_PRED_MASK; } =20 - if (perf_pmus__num_core_pmus() > 1) { + if (perf_pmus__num_core_pmus(pmus) > 1) { __u64 type =3D perf_pmus__find_core_pmu()->type; =20 attr.config |=3D type << PERF_PMU_TYPE_SHIFT; diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 665f4b9bcf44..388070e670b6 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1561,7 +1561,8 @@ static int pmu_config_term(const struct perf_pmu *pmu, assert(term->val.num < PERF_COUNT_HW_MAX); assert(pmu->is_core); attr->config =3D term->val.num; - if (perf_pmus__supports_extended_type()) + if (pmu->pmus ? perf_pmus__supports_extended_type(pmu->pmus) + : perf_pmus__host_supports_extended_type()) attr->config |=3D (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; attr->type =3D PERF_TYPE_HARDWARE; break; @@ -1579,7 +1580,8 @@ static int pmu_config_term(const struct perf_pmu *pmu, assert((term->val.num & ~0xFFFFFF) =3D=3D 0); assert(pmu->is_core); attr->config =3D term->val.num; - if (perf_pmus__supports_extended_type()) + if (pmu->pmus ? perf_pmus__supports_extended_type(pmu->pmus) + : perf_pmus__host_supports_extended_type()) attr->config |=3D (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; attr->type =3D PERF_TYPE_HW_CACHE; break; @@ -2118,7 +2120,8 @@ bool perf_pmu__supports_legacy_cache(const struct per= f_pmu *pmu) =20 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) { - return !pmu->is_core || perf_pmus__num_core_pmus() =3D=3D 1; + return !pmu->is_core || + perf_pmus__num_core_pmus(pmu->pmus ?: perf_env__pmus(host_env())) = =3D=3D 1; } =20 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index ea55882ce670..7605c3859337 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -17,6 +17,7 @@ struct evsel_config_term; struct hashmap; struct perf_cpu_map; struct perf_env; +struct perf_pmus; struct print_callbacks; =20 enum { @@ -163,6 +164,8 @@ struct perf_pmu { bool sysfs_aliases_loaded; /** @from_env: Was the PMU reconstructed from a perf_env header? */ bool from_env; + /** @pmus: The PMU collection this PMU belongs to. */ + struct perf_pmus *pmus; /** * @cpu_aliases_added: Have all json events table entries for the PMU * been added? diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 03d79ec5037f..cf2255e2a8b7 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -14,6 +14,7 @@ #include "cpumap.h" #include "debug.h" #include "drm_pmu.h" +#include "env.h" #include "evsel.h" #include "pmus.h" #include "pmu.h" @@ -38,8 +39,6 @@ * are captured under other_pmus. PMUs belonging to other_pmus * must have pmu->is_core=3D0 but pmu->is_uncore could be 0 or= 1. */ -static LIST_HEAD(core_pmus); -static LIST_HEAD(other_pmus); enum perf_tool_pmu_type { PERF_TOOL_PMU_TYPE_PE_CORE, PERF_TOOL_PMU_TYPE_PE_OTHER, @@ -59,9 +58,8 @@ enum perf_tool_pmu_type { PERF_TOOL_PMU_TYPE_HWMON_MASK | \ PERF_TOOL_PMU_TYPE_DRM_MASK) }; -static unsigned int read_pmu_types; =20 -static void pmu_read_sysfs(unsigned int to_read_pmus); +static void pmu_read_sysfs(struct perf_pmus *pmus, unsigned int to_read_pm= us); =20 size_t pmu_name_len_no_suffix(const char *str) { @@ -132,33 +130,50 @@ int pmu_name_cmp(const char *lhs_pmu_name, const char= *rhs_pmu_name) return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0); } =20 -void perf_pmus__destroy(void) +void perf_pmus__init(struct perf_pmus *pmus) +{ + INIT_LIST_HEAD(&pmus->core_pmus); + INIT_LIST_HEAD(&pmus->other_pmus); + pmus->read_pmu_types =3D 0; + pmus->num_core_pmus =3D 0; + pmus->extended_type =3D PERF_PMUS_EXTENDED_TYPE_UNKNOWN; + mutex_init(&pmus->lock); +} + +void perf_pmus__exit(struct perf_pmus *pmus) { struct perf_pmu *pmu, *tmp; =20 - list_for_each_entry_safe(pmu, tmp, &core_pmus, list) { + if (!pmus->core_pmus.next || !pmus->other_pmus.next) { + perf_pmus__init(pmus); + return; + } + + list_for_each_entry_safe(pmu, tmp, &pmus->core_pmus, list) { list_del(&pmu->list); =20 perf_pmu__delete(pmu); } - list_for_each_entry_safe(pmu, tmp, &other_pmus, list) { + list_for_each_entry_safe(pmu, tmp, &pmus->other_pmus, list) { list_del(&pmu->list); =20 perf_pmu__delete(pmu); } - read_pmu_types =3D 0; + pmus->read_pmu_types =3D 0; + pmus->num_core_pmus =3D 0; + pmus->extended_type =3D PERF_PMUS_EXTENDED_TYPE_UNKNOWN; } =20 -static struct perf_pmu *pmu_find(const char *name) +static struct perf_pmu *pmu_find(struct perf_pmus *pmus, const char *name) { struct perf_pmu *pmu; =20 - list_for_each_entry(pmu, &core_pmus, list) { + list_for_each_entry(pmu, &pmus->core_pmus, list) { if (!strcmp(pmu->name, name) || (pmu->alias_name && !strcmp(pmu->alias_name, name))) return pmu; } - list_for_each_entry(pmu, &other_pmus, list) { + list_for_each_entry(pmu, &pmus->other_pmus, list) { if (!strcmp(pmu->name, name) || (pmu->alias_name && !strcmp(pmu->alias_name, name))) return pmu; @@ -167,7 +182,7 @@ static struct perf_pmu *pmu_find(const char *name) return NULL; } =20 -struct perf_pmu *perf_pmus__find(const char *name) +static struct perf_pmu *__perf_pmus__find(struct perf_pmus *pmus, const ch= ar *name) { struct perf_pmu *pmu; int dirfd; @@ -181,24 +196,26 @@ struct perf_pmu *perf_pmus__find(const char *name) * so we keep us from multiple reading/parsing * the pmu format definitions. */ - pmu =3D pmu_find(name); + pmu =3D pmu_find(pmus, name); if (pmu) return pmu; =20 - if (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) + if (pmus->read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) return NULL; =20 core_pmu =3D is_pmu_core(name); - if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) + if (core_pmu && (pmus->read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) return NULL; =20 dirfd =3D perf_pmu__event_source_devices_fd(); - pmu =3D perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name, + pmu =3D perf_pmu__lookup(core_pmu ? &pmus->core_pmus : &pmus->other_pmus,= dirfd, name, /*eager_load=3D*/false); close(dirfd); =20 - if (pmu) + if (pmu) { + pmu->pmus =3D pmus; return pmu; + } =20 /* Looking up an individual perf event PMU failed, check if a tool PMU sh= ould be read. */ if (!strncmp(name, "hwmon_", 6)) @@ -209,8 +226,8 @@ struct perf_pmu *perf_pmus__find(const char *name) to_read_pmus |=3D PERF_TOOL_PMU_TYPE_TOOL_MASK; =20 if (to_read_pmus) { - pmu_read_sysfs(to_read_pmus); - pmu =3D pmu_find(name); + pmu_read_sysfs(pmus, to_read_pmus); + pmu =3D pmu_find(pmus, name); if (pmu) return pmu; } @@ -218,11 +235,16 @@ struct perf_pmu *perf_pmus__find(const char *name) to_read_pmus =3D PERF_TOOL_PMU_TYPE_PE_CORE_MASK; if (!core_pmu) to_read_pmus |=3D PERF_TOOL_PMU_TYPE_PE_OTHER_MASK; - pmu_read_sysfs(to_read_pmus); - return pmu_find(name); + pmu_read_sysfs(pmus, to_read_pmus); + return pmu_find(pmus, name); +} + +struct perf_pmu *perf_pmus__find(const char *name) +{ + return __perf_pmus__find(perf_env__pmus(host_env()), name); } =20 -static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) +static struct perf_pmu *perf_pmu__find2(struct perf_pmus *pmus, int dirfd,= const char *name) { struct perf_pmu *pmu; bool core_pmu; @@ -232,19 +254,22 @@ static struct perf_pmu *perf_pmu__find2(int dirfd, co= nst char *name) * so we keep us from multiple reading/parsing * the pmu format definitions. */ - pmu =3D pmu_find(name); + pmu =3D pmu_find(pmus, name); if (pmu) return pmu; =20 - if (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) + if (pmus->read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) return NULL; =20 core_pmu =3D is_pmu_core(name); - if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) + if (core_pmu && (pmus->read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) return NULL; =20 - return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name, - /*eager_load=3D*/false); + pmu =3D perf_pmu__lookup(core_pmu ? &pmus->core_pmus : &pmus->other_pmus,= dirfd, name, + /*eager_load=3D*/false); + if (pmu) + pmu->pmus =3D pmus; + return pmu; } =20 static int pmus_cmp(void *priv __maybe_unused, @@ -257,11 +282,11 @@ static int pmus_cmp(void *priv __maybe_unused, } =20 /* Add all pmus in sysfs to pmu list: */ -static void pmu_read_sysfs(unsigned int to_read_types) +static void pmu_read_sysfs(struct perf_pmus *pmus, unsigned int to_read_ty= pes) { - struct perf_pmu *tool_pmu; + struct perf_pmu *pmu, *tool_pmu; =20 - if ((read_pmu_types & to_read_types) =3D=3D to_read_types) { + if ((pmus->read_pmu_types & to_read_types) =3D=3D to_read_types) { /* All requested PMU types have been read. */ return; } @@ -282,60 +307,68 @@ static void pmu_read_sysfs(unsigned int to_read_types) continue; if (core_only && !is_pmu_core(dent->d_name)) continue; - /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */ - perf_pmu__find2(fd, dent->d_name); + /* add to core_pmus or other_pmus: */ + perf_pmu__find2(pmus, fd, dent->d_name); } =20 close(fd); } skip_pe_pmus: - if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&core= _pmus)) { - if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) + if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&pmus= ->core_pmus)) { + pmu =3D perf_pmu__create_placeholder_core_pmu(&pmus->core_pmus); + if (!pmu) pr_err("Failure to set up any core PMUs\n"); + else + pmu->pmus =3D pmus; } - list_sort(NULL, &core_pmus, pmus_cmp); + list_sort(NULL, &pmus->core_pmus, pmus_cmp); =20 if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) !=3D 0 && - (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) =3D=3D 0) { + (pmus->read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) =3D=3D 0) { tool_pmu =3D tool_pmu__new(); - if (tool_pmu) - list_add_tail(&tool_pmu->list, &other_pmus); + if (tool_pmu) { + tool_pmu->pmus =3D pmus; + list_add_tail(&tool_pmu->list, &pmus->other_pmus); + } } if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) !=3D 0 && - (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) =3D=3D 0) - perf_pmus__read_hwmon_pmus(&other_pmus); + (pmus->read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) =3D=3D 0) + perf_pmus__read_hwmon_pmus(&pmus->other_pmus); =20 if ((to_read_types & PERF_TOOL_PMU_TYPE_DRM_MASK) !=3D 0 && - (read_pmu_types & PERF_TOOL_PMU_TYPE_DRM_MASK) =3D=3D 0) - perf_pmus__read_drm_pmus(&other_pmus); + (pmus->read_pmu_types & PERF_TOOL_PMU_TYPE_DRM_MASK) =3D=3D 0) + perf_pmus__read_drm_pmus(&pmus->other_pmus); + + list_for_each_entry(pmu, &pmus->other_pmus, list) + pmu->pmus =3D pmus; =20 - list_sort(NULL, &other_pmus, pmus_cmp); + list_sort(NULL, &pmus->other_pmus, pmus_cmp); =20 - read_pmu_types |=3D to_read_types; + pmus->read_pmu_types |=3D to_read_types; } =20 -static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type) +static struct perf_pmu *__perf_pmus__find_by_type(struct perf_pmus *pmus, = unsigned int type) { struct perf_pmu *pmu; =20 - list_for_each_entry(pmu, &core_pmus, list) { + list_for_each_entry(pmu, &pmus->core_pmus, list) { if (pmu->type =3D=3D type) return pmu; } =20 - list_for_each_entry(pmu, &other_pmus, list) { + list_for_each_entry(pmu, &pmus->other_pmus, list) { if (pmu->type =3D=3D type) return pmu; } return NULL; } =20 -struct perf_pmu *perf_pmus__find_by_type(unsigned int type) +static struct perf_pmu *pmu_find_by_type(struct perf_pmus *pmus, unsigned = int type) { unsigned int to_read_pmus; - struct perf_pmu *pmu =3D __perf_pmus__find_by_type(type); + struct perf_pmu *pmu =3D __perf_pmus__find_by_type(pmus, type); =20 - if (pmu || (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK)) + if (pmu || (pmus->read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK)) return pmu; =20 if (type >=3D PERF_PMU_TYPE_PE_START && type <=3D PERF_PMU_TYPE_PE_END) { @@ -348,48 +381,64 @@ struct perf_pmu *perf_pmus__find_by_type(unsigned int= type) } else { to_read_pmus =3D PERF_TOOL_PMU_TYPE_TOOL_MASK; } - pmu_read_sysfs(to_read_pmus); - pmu =3D __perf_pmus__find_by_type(type); + pmu_read_sysfs(pmus, to_read_pmus); + pmu =3D __perf_pmus__find_by_type(pmus, type); return pmu; } =20 +struct perf_pmu *perf_pmus__find_by_type(unsigned int type) +{ + return pmu_find_by_type(perf_env__pmus(host_env()), type); +} + /* * pmu iterator: If pmu is NULL, we start at the begin, otherwise return t= he * next pmu. Returns NULL on end. */ -struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) +static struct perf_pmu *pmu_scan(struct perf_pmus *pmus, struct perf_pmu *= pmu) { bool use_core_pmus =3D !pmu || pmu->is_core; =20 if (!pmu) { - pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK); - pmu =3D list_prepare_entry(pmu, &core_pmus, list); + pmu_read_sysfs(pmus, PERF_TOOL_PMU_TYPE_ALL_MASK); + pmu =3D list_prepare_entry(pmu, &pmus->core_pmus, list); } if (use_core_pmus) { - list_for_each_entry_continue(pmu, &core_pmus, list) + list_for_each_entry_continue(pmu, &pmus->core_pmus, list) return pmu; =20 pmu =3D NULL; - pmu =3D list_prepare_entry(pmu, &other_pmus, list); + pmu =3D list_prepare_entry(pmu, &pmus->other_pmus, list); } - list_for_each_entry_continue(pmu, &other_pmus, list) + list_for_each_entry_continue(pmu, &pmus->other_pmus, list) return pmu; return NULL; } =20 -struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) +struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) +{ + return pmu_scan(perf_env__pmus(host_env()), pmu); +} + +static struct perf_pmu *pmu_scan_core(struct perf_pmus *pmus, struct perf_= pmu *pmu) { if (!pmu) { - pmu_read_sysfs(PERF_TOOL_PMU_TYPE_PE_CORE_MASK); - return list_first_entry_or_null(&core_pmus, typeof(*pmu), list); + pmu_read_sysfs(pmus, PERF_TOOL_PMU_TYPE_PE_CORE_MASK); + return list_first_entry_or_null(&pmus->core_pmus, typeof(*pmu), list); } - list_for_each_entry_continue(pmu, &core_pmus, list) + list_for_each_entry_continue(pmu, &pmus->core_pmus, list) return pmu; =20 return NULL; } =20 -struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const cha= r *event) +struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) +{ + return pmu_scan_core(perf_env__pmus(host_env()), pmu); +} + +struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *event) { bool use_core_pmus =3D !pmu || pmu->is_core; =20 @@ -413,22 +462,23 @@ struct perf_pmu *perf_pmus__scan_for_event(struct per= f_pmu *pmu, const char *eve if (strlen(event) > 4 && strncmp("drm-", event, 4) =3D=3D 0) to_read_pmus |=3D PERF_TOOL_PMU_TYPE_DRM_MASK; =20 - pmu_read_sysfs(to_read_pmus); - pmu =3D list_prepare_entry(pmu, &core_pmus, list); + pmu_read_sysfs(pmus, to_read_pmus); + pmu =3D list_prepare_entry(pmu, &pmus->core_pmus, list); } if (use_core_pmus) { - list_for_each_entry_continue(pmu, &core_pmus, list) + list_for_each_entry_continue(pmu, &pmus->core_pmus, list) return pmu; =20 pmu =3D NULL; - pmu =3D list_prepare_entry(pmu, &other_pmus, list); + pmu =3D list_prepare_entry(pmu, &pmus->other_pmus, list); } - list_for_each_entry_continue(pmu, &other_pmus, list) + list_for_each_entry_continue(pmu, &pmus->other_pmus, list) return pmu; return NULL; } =20 -struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, c= onst char *wildcard) +struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *wildcard) { bool use_core_pmus =3D !pmu || pmu->is_core; =20 @@ -457,33 +507,33 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(st= ruct perf_pmu *pmu, const c to_read_pmus |=3D PERF_TOOL_PMU_TYPE_HWMON_MASK; } =20 - pmu_read_sysfs(to_read_pmus); - pmu =3D list_prepare_entry(pmu, &core_pmus, list); + pmu_read_sysfs(pmus, to_read_pmus); + pmu =3D list_prepare_entry(pmu, &pmus->core_pmus, list); } if (use_core_pmus) { - list_for_each_entry_continue(pmu, &core_pmus, list) { + list_for_each_entry_continue(pmu, &pmus->core_pmus, list) { if (perf_pmu__wildcard_match(pmu, wildcard)) return pmu; } pmu =3D NULL; - pmu =3D list_prepare_entry(pmu, &other_pmus, list); + pmu =3D list_prepare_entry(pmu, &pmus->other_pmus, list); } - list_for_each_entry_continue(pmu, &other_pmus, list) { + list_for_each_entry_continue(pmu, &pmus->other_pmus, list) { if (perf_pmu__wildcard_match(pmu, wildcard)) return pmu; } return NULL; } =20 -static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *p= mu) +static struct perf_pmu *pmu_scan_skip_duplicates(struct perf_pmus *pmus, s= truct perf_pmu *pmu) { bool use_core_pmus =3D !pmu || pmu->is_core; int last_pmu_name_len =3D 0; const char *last_pmu_name =3D (pmu && pmu->name) ? pmu->name : ""; =20 if (!pmu) { - pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK); - pmu =3D list_prepare_entry(pmu, &core_pmus, list); + pmu_read_sysfs(pmus, PERF_TOOL_PMU_TYPE_ALL_MASK); + pmu =3D list_prepare_entry(pmu, &pmus->core_pmus, list); } else last_pmu_name_len =3D pmu_name_len_no_suffix(pmu->name ?: ""); =20 @@ -493,13 +543,13 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicat= es(struct perf_pmu *pmu) * may differ only by a suffix but have different events, so * they are never duplicates. */ - list_for_each_entry_continue(pmu, &core_pmus, list) + list_for_each_entry_continue(pmu, &pmus->core_pmus, list) return pmu; =20 pmu =3D NULL; - pmu =3D list_prepare_entry(pmu, &other_pmus, list); + pmu =3D list_prepare_entry(pmu, &pmus->other_pmus, list); } - list_for_each_entry_continue(pmu, &other_pmus, list) { + list_for_each_entry_continue(pmu, &pmus->other_pmus, list) { int pmu_name_len =3D pmu_name_len_no_suffix(pmu->name ?: ""); =20 if (last_pmu_name_len =3D=3D pmu_name_len && @@ -511,27 +561,28 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicat= es(struct perf_pmu *pmu) return NULL; } =20 -struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const= char *compat) +struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *compat) { if (!pmu) { /* Only uncore PMUs can have identifiers. */ unsigned int to_read_pmus =3D PERF_TOOL_PMU_TYPE_PE_OTHER_MASK; =20 - pmu_read_sysfs(to_read_pmus); - pmu =3D list_prepare_entry(pmu, &other_pmus, list); + pmu_read_sysfs(pmus, to_read_pmus); + pmu =3D list_prepare_entry(pmu, &pmus->other_pmus, list); } - list_for_each_entry_continue(pmu, &other_pmus, list) { + list_for_each_entry_continue(pmu, &pmus->other_pmus, list) { if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id)) return pmu; } return NULL; } =20 -const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str) +const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(struct perf_pmus *pmu= s, const char *str) { struct perf_pmu *pmu =3D NULL; =20 - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D pmu_scan(pmus, pmu)) !=3D NULL) { if (!strcmp(pmu->name, str)) return pmu; /* Ignore "uncore_" prefix. */ @@ -652,7 +703,8 @@ static int perf_pmus__print_pmu_events__callback(void *= vstate, return 0; } =20 -void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, v= oid *print_state) +void perf_pmus__print_pmu_events(struct perf_pmus *pmus, + const struct print_callbacks *print_cb, void *print_state) { struct perf_pmu *pmu; int printed =3D 0; @@ -660,16 +712,16 @@ void perf_pmus__print_pmu_events(const struct print_c= allbacks *print_cb, void *p struct sevent *aliases; struct events_callback_state state; bool skip_duplicate_pmus =3D print_cb->skip_duplicate_pmus(print_state); - struct perf_pmu *(*scan_fn)(struct perf_pmu *); + struct perf_pmu *(*scan_fn)(struct perf_pmus *p, struct perf_pmu *cur); =20 if (skip_duplicate_pmus) - scan_fn =3D perf_pmus__scan_skip_duplicates; + scan_fn =3D pmu_scan_skip_duplicates; else - scan_fn =3D perf_pmus__scan; + scan_fn =3D pmu_scan; =20 pmu =3D NULL; len =3D 0; - while ((pmu =3D scan_fn(pmu)) !=3D NULL) + while ((pmu =3D scan_fn(pmus, pmu)) !=3D NULL) len +=3D perf_pmu__num_events(pmu); =20 aliases =3D calloc(len, sizeof(struct sevent)); @@ -683,7 +735,7 @@ void perf_pmus__print_pmu_events(const struct print_cal= lbacks *print_cb, void *p .aliases_len =3D len, .index =3D 0, }; - while ((pmu =3D scan_fn(pmu)) !=3D NULL) { + while ((pmu =3D scan_fn(pmus, pmu)) !=3D NULL) { perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state, perf_pmus__print_pmu_events__callback); } @@ -767,18 +819,19 @@ static int build_format_string(void *state, const cha= r *name, int config, return ret1 < 0 ? ret1 : (ret2 < 0 ? ret2 : 0); } =20 -void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_c= b, void *print_state) +void perf_pmus__print_raw_pmu_events(struct perf_pmus *pmus, + const struct print_callbacks *print_cb, void *print_state) { bool skip_duplicate_pmus =3D print_cb->skip_duplicate_pmus(print_state); - struct perf_pmu *(*scan_fn)(struct perf_pmu *); + struct perf_pmu *(*scan_fn)(struct perf_pmus *p, struct perf_pmu *cur); struct perf_pmu *pmu =3D NULL; =20 if (skip_duplicate_pmus) - scan_fn =3D perf_pmus__scan_skip_duplicates; + scan_fn =3D pmu_scan_skip_duplicates; else - scan_fn =3D perf_pmus__scan; + scan_fn =3D pmu_scan; =20 - while ((pmu =3D scan_fn(pmu)) !=3D NULL) { + while ((pmu =3D scan_fn(pmus, pmu)) !=3D NULL) { struct build_format_string_args format_args =3D { .short_string =3D STRBUF_INIT, .long_string =3D STRBUF_INIT, @@ -820,67 +873,79 @@ void perf_pmus__print_raw_pmu_events(const struct pri= nt_callbacks *print_cb, voi } } =20 -bool perf_pmus__have_event(const char *pname, const char *name) +bool perf_pmus__have_event(struct perf_pmus *pmus, const char *pname, cons= t char *name) { - struct perf_pmu *pmu =3D perf_pmus__find(pname); + struct perf_pmu *pmu =3D __perf_pmus__find(pmus, pname); =20 return pmu && perf_pmu__have_event(pmu, name); } =20 -int perf_pmus__num_core_pmus(void) +int perf_pmus__num_core_pmus(struct perf_pmus *pmus) { - static int count; - - if (!count) { + if (!pmus->num_core_pmus) { struct perf_pmu *pmu =3D NULL; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) - count++; + while ((pmu =3D pmu_scan_core(pmus, pmu)) !=3D NULL) + pmus->num_core_pmus++; } - return count; + return pmus->num_core_pmus; } =20 -static bool __perf_pmus__supports_extended_type(void) +static bool __perf_pmus__supports_extended_type(struct perf_pmus *pmus) { struct perf_pmu *pmu =3D NULL; =20 - if (perf_pmus__num_core_pmus() <=3D 1) + if (perf_pmus__num_core_pmus(pmus) <=3D 1) return false; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { - if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | (= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) + while ((pmu =3D pmu_scan_core(pmus, pmu)) !=3D NULL) { + if (!pmu->from_env && + !is_event_supported(PERF_TYPE_HARDWARE, + PERF_COUNT_HW_CPU_CYCLES | + ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) return false; } =20 return true; } =20 -static bool perf_pmus__do_support_extended_type; - -static void perf_pmus__init_supports_extended_type(void) +bool perf_pmus__supports_extended_type(struct perf_pmus *pmus) { - perf_pmus__do_support_extended_type =3D __perf_pmus__supports_extended_ty= pe(); + enum perf_pmus_extended_type extended_type =3D + __atomic_load_n(&pmus->extended_type, __ATOMIC_ACQUIRE); + + if (extended_type =3D=3D PERF_PMUS_EXTENDED_TYPE_UNKNOWN) { + mutex_lock(&pmus->lock); + extended_type =3D pmus->extended_type; + if (extended_type =3D=3D PERF_PMUS_EXTENDED_TYPE_UNKNOWN) { + extended_type =3D __perf_pmus__supports_extended_type(pmus) + ? PERF_PMUS_EXTENDED_TYPE_SUPPORTED + : PERF_PMUS_EXTENDED_TYPE_NOT_SUPPORTED; + __atomic_store_n(&pmus->extended_type, extended_type, + __ATOMIC_RELEASE); + } + mutex_unlock(&pmus->lock); + } + + return extended_type =3D=3D PERF_PMUS_EXTENDED_TYPE_SUPPORTED; } =20 -bool perf_pmus__supports_extended_type(void) +bool perf_pmus__host_supports_extended_type(void) { - static pthread_once_t extended_type_once =3D PTHREAD_ONCE_INIT; - - pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type); - - return perf_pmus__do_support_extended_type; + return perf_pmus__supports_extended_type(perf_env__pmus(host_env())); } =20 -struct perf_pmu *perf_pmus__find_by_attr(const struct perf_event_attr *att= r) +struct perf_pmu *perf_pmus__find_by_attr(struct perf_pmus *pmus, + const struct perf_event_attr *attr) { - struct perf_pmu *pmu =3D perf_pmus__find_by_type(attr->type); + struct perf_pmu *pmu =3D pmu_find_by_type(pmus, attr->type); u32 type =3D attr->type; bool legacy_core_type =3D type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D P= ERF_TYPE_HW_CACHE; =20 - if (!pmu && legacy_core_type && perf_pmus__supports_extended_type()) { + if (!pmu && legacy_core_type && perf_pmus__supports_extended_type(pmus)) { type =3D attr->config >> PERF_PMU_TYPE_SHIFT; =20 - pmu =3D perf_pmus__find_by_type(type); + pmu =3D pmu_find_by_type(pmus, type); } if (!pmu && (legacy_core_type || type =3D=3D PERF_TYPE_RAW)) { /* @@ -891,7 +956,7 @@ struct perf_pmu *perf_pmus__find_by_attr(const struct p= erf_event_attr *attr) * PERF_TYPE_RAW, assume the RAW events are going to be handled * by the first core PMU. */ - pmu =3D perf_pmus__find_core_pmu(); + pmu =3D pmu_scan_core(pmus, NULL); } return pmu; } @@ -899,11 +964,13 @@ struct perf_pmu *perf_pmus__find_by_attr(const struct= perf_event_attr *attr) struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) { struct perf_pmu *pmu =3D evsel->pmu; + struct perf_env *env; =20 if (pmu) return pmu; =20 - pmu =3D perf_pmus__find_by_attr(&evsel->core.attr); + env =3D evsel__env((struct evsel *)evsel) ?: host_env(); + pmu =3D perf_pmus__find_by_attr(perf_env__pmus(env), &evsel->core.attr); ((struct evsel *)evsel)->pmu =3D pmu; return pmu; } @@ -913,21 +980,37 @@ struct perf_pmu *perf_pmus__find_core_pmu(void) return perf_pmus__scan_core(NULL); } =20 -struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char = *name) +struct perf_pmu *perf_pmus__add_test_pmu(struct perf_pmus *pmus, + int test_sysfs_dirfd, const char *name) { + struct perf_pmu *pmu; + + if (!pmus->core_pmus.next || !pmus->other_pmus.next) + perf_pmus__init(pmus); /* * Some PMU functions read from the sysfs mount point, so care is * needed, hence passing the eager_load flag to load things like the * format files. */ - return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load= =3D*/true); + pmu =3D perf_pmu__lookup(&pmus->other_pmus, test_sysfs_dirfd, name, /*eag= er_load=3D*/true); + if (pmu) + pmu->pmus =3D pmus; + return pmu; } =20 -struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir, +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(struct perf_pmus *pmus, + const char *hwmon_dir, const char *sysfs_name, const char *name) { - return hwmon_pmu__new(&other_pmus, hwmon_dir, sysfs_name, name); + struct perf_pmu *pmu; + + if (!pmus->core_pmus.next || !pmus->other_pmus.next) + perf_pmus__init(pmus); + pmu =3D hwmon_pmu__new(&pmus->other_pmus, hwmon_dir, sysfs_name, name); + if (pmu) + pmu->pmus =3D pmus; + return pmu; } =20 struct perf_pmu *perf_pmus__fake_pmu(void) diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index 0d55edb3f2fc..45f4bd8b6aa6 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -2,41 +2,220 @@ #ifndef __PMUS_H #define __PMUS_H =20 +#include #include #include +#include "mutex.h" =20 struct perf_event_attr; struct perf_pmu; struct print_callbacks; =20 +/** + * enum perf_pmus_extended_type - Tri-state cache for extended PMU type su= pport. + * @PERF_PMUS_EXTENDED_TYPE_UNKNOWN: Support has not been checked yet. + * @PERF_PMUS_EXTENDED_TYPE_SUPPORTED: Multiple core PMUs / extended type = supported. + * @PERF_PMUS_EXTENDED_TYPE_NOT_SUPPORTED: Extended type not supported. + */ +enum perf_pmus_extended_type { + PERF_PMUS_EXTENDED_TYPE_UNKNOWN =3D 0, + PERF_PMUS_EXTENDED_TYPE_SUPPORTED, + PERF_PMUS_EXTENDED_TYPE_NOT_SUPPORTED, +}; + +/** + * struct perf_pmus - Collection of core and non-core PMUs for an environm= ent. + */ +struct perf_pmus { + /** + * @core_pmus: In-order list of core PMUs (e.g. "cpu", "cpu_core", + * "cpu_atom"). + */ + struct list_head core_pmus; + /** + * @other_pmus: In-order list of non-core PMUs (uncore, software, + * tracepoint, tool, hwmon, drm, etc.). + */ + struct list_head other_pmus; + /** + * @read_pmu_types: Bitmask of enum perf_tool_pmu_type values whose PMU + * lists have been populated. + */ + unsigned int read_pmu_types; + /** @num_core_pmus: Cached count of entries in @core_pmus. */ + int num_core_pmus; + /** + * @extended_type: Cached result of checking whether extended PMU type + * encoding in perf_event_attr::config is supported. + */ + enum perf_pmus_extended_type extended_type; + /** @lock: Protects lazy initialization of @extended_type. */ + struct mutex lock; +}; + +#define PERF_PMUS_INIT(name) { \ + .core_pmus =3D LIST_HEAD_INIT((name).core_pmus), \ + .other_pmus =3D LIST_HEAD_INIT((name).other_pmus), \ + .lock =3D { .lock =3D __PERF_MUTEX_INITIALIZER }, \ +} + size_t pmu_name_len_no_suffix(const char *str); /* Exposed for testing only. */ int pmu_name_cmp(const char *lhs_pmu_name, const char *rhs_pmu_name); =20 -void perf_pmus__destroy(void); +/** + * perf_pmus__init() - Initialize an empty struct perf_pmus. + * @pmus: The PMU collection to initialize. + */ +void perf_pmus__init(struct perf_pmus *pmus); + +/** + * perf_pmus__exit() - Free all PMUs in @pmus and reset its state. + * @pmus: The PMU collection to clean up. + */ +void perf_pmus__exit(struct perf_pmus *pmus); =20 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 *att= r); + +/** + * perf_pmus__find_by_attr() - Find the PMU corresponding to @attr. + * @pmus: The PMU collection to search. + * @attr: The perf_event_attr whose type/config identifies the PMU. + * + * Return: Pointer to the matching struct perf_pmu, or NULL if not found. + */ +struct perf_pmu *perf_pmus__find_by_attr(struct perf_pmus *pmus, + const struct perf_event_attr *attr); =20 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_for_event(struct perf_pmu *pmu, const cha= r *event); -struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, c= onst char *wildcard); -struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const= char *compat); =20 -const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str); +/** + * perf_pmus__scan_for_event() - Iterate over PMUs in @pmus that have @eve= nt. + * @pmus: The PMU collection to scan. + * @pmu: Previous matching PMU returned, or NULL to start. + * @event: Event name to search for. + * + * Return: Next struct perf_pmu supporting @event, or NULL when done. + */ +struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *event); + +/** + * perf_pmus__scan_matching_wildcard() - Iterate over PMUs matching @wildc= ard. + * @pmus: The PMU collection to scan. + * @pmu: Previous matching PMU returned, or NULL to start. + * @wildcard: Wildcard/prefix pattern to match against PMU names. + * + * Return: Next matching struct perf_pmu, or NULL when done. + */ +struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *wildcard); + +/** + * perf_pmus__scan_for_uncore_id() - Iterate over uncore PMUs matching @co= mpat. + * @pmus: The PMU collection to scan. + * @pmu: Previous matching PMU returned, or NULL to start. + * @compat: Uncore identifier string to match. + * + * Return: Next matching uncore struct perf_pmu, or NULL when done. + */ +struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmus *pmus, + struct perf_pmu *pmu, const char *compat); + +/** + * perf_pmus__pmu_for_pmu_filter() - Find PMU matching a pmu_filter string. + * @pmus: The PMU collection to search. + * @str: Filter string to match against PMU names. + * + * Return: Matching struct perf_pmu, or NULL if none matches. + */ +const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(struct perf_pmus *pmu= s, const char *str); =20 -void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, v= oid *print_state); -void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_c= b, void *print_state); -bool perf_pmus__have_event(const char *pname, const char *name); -int perf_pmus__num_core_pmus(void); -bool perf_pmus__supports_extended_type(void); +/** + * perf_pmus__print_pmu_events() - Print formatted PMU events via @print_c= b. + * @pmus: The PMU collection whose events to print. + * @print_cb: Callbacks for printing event entries. + * @print_state: Caller state passed to @print_cb. + */ +void perf_pmus__print_pmu_events(struct perf_pmus *pmus, + const struct print_callbacks *print_cb, void *print_state); =20 -struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char = *name); -struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir, +/** + * perf_pmus__print_raw_pmu_events() - Print raw PMU event descriptors. + * @pmus: The PMU collection whose raw events to print. + * @print_cb: Callbacks for printing event entries. + * @print_state: Caller state passed to @print_cb. + */ +void perf_pmus__print_raw_pmu_events(struct perf_pmus *pmus, + const struct print_callbacks *print_cb, void *print_state); + +/** + * perf_pmus__have_event() - Check whether @pname in @pmus has event @name. + * @pmus: The PMU collection to search. + * @pname: PMU name. + * @name: Event name. + * + * Return: true if the PMU exists and has @name, false otherwise. + */ +bool perf_pmus__have_event(struct perf_pmus *pmus, const char *pname, cons= t char *name); + +/** + * perf_pmus__num_core_pmus() - Return the number of core PMUs in @pmus. + * @pmus: The PMU collection to query. + * + * Return: Number of core PMUs. + */ +int perf_pmus__num_core_pmus(struct perf_pmus *pmus); + +/** + * perf_pmus__supports_extended_type() - Check whether extended PMU type + * encoding is supported in @pmus. + * @pmus: The PMU collection to query. + * + * Return: true if extended type encoding is supported, false otherwise. + */ +bool perf_pmus__supports_extended_type(struct perf_pmus *pmus); + +/** + * perf_pmus__host_supports_extended_type() - Check whether extended PMU t= ype + * encoding is supported on the= host. + * + * Return: true if extended type encoding is supported on the live host. + */ +bool perf_pmus__host_supports_extended_type(void); + +/** + * perf_pmus__add_test_pmu() - Add a sysfs-backed test PMU to @pmus. + * @pmus: The PMU collection to add the test PMU to. + * @test_sysfs_dirfd: Directory fd of the synthetic sysfs root. + * @name: Name of the test PMU. + * + * Return: Pointer to the created struct perf_pmu, or NULL on failure. + */ +struct perf_pmu *perf_pmus__add_test_pmu(struct perf_pmus *pmus, + int test_sysfs_dirfd, const char *name); + +/** + * perf_pmus__add_test_hwmon_pmu() - Add a synthetic hwmon test PMU to @pm= us. + * @pmus: The PMU collection to add the test hwmon PMU to. + * @hwmon_dir: Path to the synthetic hwmon directory. + * @sysfs_name: Directory entry name (e.g. "hwmon0"). + * @name: Logical name of the hwmon device. + * + * Return: Pointer to the created struct perf_pmu, or NULL on failure. + */ +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(struct perf_pmus *pmus, + const char *hwmon_dir, const char *sysfs_name, const char *name); + +/** + * perf_pmus__fake_pmu() - Return the singleton fake PMU used for metric t= ests. + * + * Return: Pointer to the fake struct perf_pmu. + */ struct perf_pmu *perf_pmus__fake_pmu(void); struct perf_pmu *perf_pmus__find_core_pmu(void); =20 diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 77428d9e73c4..fe48efe9aefb 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -17,6 +17,7 @@ =20 #include "build-id.h" #include "debug.h" +#include "env.h" #include "evsel.h" #include "metricgroup.h" #include "parse-events.h" @@ -320,7 +321,9 @@ void metricgroup__print(const struct print_callbacks *p= rint_cb, void *print_stat */ void print_events(const struct print_callbacks *print_cb, void *print_stat= e) { - perf_pmus__print_pmu_events(print_cb, print_state); + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); + + perf_pmus__print_pmu_events(pmus, print_cb, print_state); =20 print_cb->print_event(print_state, /*topic=3D*/NULL, @@ -335,7 +338,7 @@ void print_events(const struct print_callbacks *print_c= b, void *print_state) /*long_desc=3D*/NULL, /*encoding_desc=3D*/NULL); =20 - perf_pmus__print_raw_pmu_events(print_cb, print_state); + perf_pmus__print_raw_pmu_events(pmus, print_cb, print_state); =20 print_cb->print_event(print_state, /*topic=3D*/NULL, diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index c75646a49ca7..cbdecfe30edd 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -26,6 +26,7 @@ #include "env.h" #include "evlist.h" #include "evsel.h" +#include "pmu.h" #include "memswap.h" #include "map.h" #include "symbol.h" @@ -264,6 +265,16 @@ void perf_session__delete(struct perf_session *session) debuginfo_cache__delete(); perf_session__destroy_kernel_maps(session); perf_decomp__release_events(session->decomp_data.decomp); + if (session->data && perf_data__is_read(session->data) && session->evlist= ) { + struct evsel *evsel; + + evlist__for_each_entry(session->evlist, evsel) { + if (evsel->pmu && evsel->pmu->pmus =3D=3D &session->header.env.pmus) + evsel->pmu =3D NULL; + } + if (evlist__session(session->evlist) =3D=3D session) + evlist__set_session(session->evlist, NULL); + } perf_env__exit(&session->header.env); machines__exit(&session->machines); if (session->data) { diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index 493ed6c88685..8d2b9ab70580 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -770,7 +770,9 @@ static bool evlist__has_hybrid_pmus(struct evlist *evli= st) struct evsel *evsel; struct perf_pmu *last_core_pmu =3D NULL; =20 - if (perf_pmus__num_core_pmus() =3D=3D 1) + struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(evlist) ?: host_env= ()); + + if (perf_pmus__num_core_pmus(pmus) =3D=3D 1) return false; =20 evlist__for_each_entry(evlist, evsel) { diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c index 525a3fe4a46e..f5ae399bf336 100644 --- a/tools/perf/util/stat-shadow.c +++ b/tools/perf/util/stat-shadow.c @@ -236,7 +236,8 @@ static void perf_stat__print_metricgroup_header(struct = perf_stat_config *config, const char *name, struct perf_stat_output_ctx *out) { - bool need_full_name =3D perf_pmus__num_core_pmus() > 1; + bool need_full_name =3D + perf_pmus__num_core_pmus(perf_env__pmus(evsel__env(evsel) ?: host_env())= ) > 1; static const char *last_name; static const struct perf_pmu *last_pmu; char full_name[64]; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212871; b=TS8AIF/yao5jxL36QpG8qc1nhcGB5H0WO9vROeKKPa8GABasoKxtGIp8EHzqBAlzxRnr aNjMmMt9GM+DE5ytsrV2LE8TQknJJFDqVYpg5GxjoNufzQ58eRzFlJw3ELXRFxBmrM46o 3QD0vAGn9JjkuRqc8qDXU7gjB393SuZz5a1ELy4FEM8hLjL3a43c8r0jIy8rozqcUWvjj bcXvurwa8LwIG2GEYV4WkY3ZgXMgk+lOVjHEzYGp7J7BYeTtwigJVxAhS9B0ixPJb92qQ 5y8tsPs5As8exl97v4w/uvj8sAmm1wQ9TBgy/mh6U9Bc2wkycn5l2JI2WWCTzSPfPCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212871; 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:Content-Transfer-Encoding; bh=Am3UUzEMGQAIFgxAbUCYBh4xJDeK+bewR1I1VBgjrv8=; b=rSsdpYld3DpTqC5l9PB7ojh09ftnIOS9knlRhjfRS8avUokKIlkfZJA+l70PK1APcWLR WokJQq/M3dIGE2sYY8x9TjeQmsoxjmxxAUbXAbpF8ETUqqJlqGvUZQl9mFw238R4IXfSC E3ES6tPNHxyoCvsIN3kKOKZ81z1EPpVbHEqL/mMhulVVqptBDnDn9fqUq1+Hya/5u/dtU lsnHgdL6xFp5snxBGsqcXmPuQQDchmqsvi4tQeu4KYqAawGbWN9O0MDlK8ZCvRt/DOrWl RkTDOWwodSJW9Clj24LiRY25eWkPeAWVfPaj+R3a6YKvtwYp8we0ZZ04XaE2opeYjVg== 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=Ao+zncAf; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 F1447606A681 for ; Fri, 20 Nov 2026 22:14:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F1447606A681 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=Ao+zncAf Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3846d5f54c1so1752562eec.1 for ; Fri, 20 Nov 2026 14:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212864; x=1795817664; darn=vger.kernel.org; h=content-transfer-encoding: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=Am3UUzEMGQAIFgxAbUCYBh4xJDeK+bewR1I1VBgjrv8=; b=Ao+zncAf/e/kE8BrMOUjWu3tLPkO8FJ9Ne1m7qYI0qPSZ0EKAupT8q3/umk58oyjV7 uQanENRnqCIYNJRoBn5qCIywMh1Zxc432bP25Atb+Jvy5JwWj/DirW1SY+3U/SqtwNUK 8/2B9O+w46XgsfQAKsZAcba0TOEsbUgoAwbhe+Ga3iu5LD9yiHIIrj7KK1mUPLznaMQx ucX/uyEKOkaH7GP60nHPFnsfboOzmZuRA/CiaJazVBdclppgZmw9lWrhUe42pP3Bc7Sc bN0mocHDn0lqTXIptgW2ol5wD2u/neKXwN+/h/RbkHLceIsXVHweiAMk5jajk+D+xBtW XMAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212864; x=1795817664; h=content-transfer-encoding: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=Am3UUzEMGQAIFgxAbUCYBh4xJDeK+bewR1I1VBgjrv8=; b=GbcwJGYHg5n1LLNHJ7NUgs4YC3AAYV6IpIF2Xvy2t5wyhAxI534gBcL/3s8brVlPTH TVFZ6zlNstFyCb+m2cpGQo8WRIQPDcZK91TwZ7z7rrrLfJcwEM5Iodi2xaihluf0c86e u15BFKJ4BF0GpQWsdf3PKqyI+/26pqyy6t87sSpnfqubl90ouXp+buNnSDAHq8n3OQO3 V5sVP5YItNCaCdYNsXkLiU2lTsWDRK/RDQ2MqMB9PQ3Y7jPPeDc+i0Cm6HkxdoEpQc8I BUukhqiWuullhTVSVcSI7QvDWKb9c4OoC6G8bCOM9RniM6ohdzVV0bS1Vtbdbbv8fRjy IJ7A== X-Forwarded-Encrypted: i=1; AL+hErGqeXTUBBvUAzoMryeGrXd5oF26Lbw8ZUz8o2RBvUk8SBuLplwuGmefyecKwCZgORue2Zuz83VkEmMfV7I=@vger.kernel.org X-Gm-Message-State: APuU4Z8vQmN7SbJHqKWSyD6BPH+KH0zptEKvlc1JqGovTINmyM56mr8F spOpDys5SPkjq37wbIg3w4lUk18WVPaDt7a2LRHYynRa+YxicguKwXZkQ3NogT0WV6JjUEevgOb sGOnN5dedDQ== X-Received: from dleg6-n1.prod.google.com ([2002:a05:701b:4306:10b0:19d:e7f1:171c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701a:c9c1:10b0:15e:6541:3099 with SMTP id a92af1059eb24-1b55687eb4dmr3698042c88.7.1795212863354; Fri, 20 Nov 2026 14:14:23 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:10 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-10-irogers@google.com> Subject: [PATCH v3 09/30] perf pmus: Pass struct perf_pmus to find helpers 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update perf_pmus__find(), perf_pmus__find_by_type(), and perf_pmus__find_core_pmu() to take a struct perf_pmus * argument and update callers to pass the appropriate environment's PMUs. Signed-off-by: Ian Rogers --- tools/perf/arch/arm/util/auxtrace.c | 9 ++-- tools/perf/arch/arm/util/cs-etm.c | 3 +- tools/perf/arch/arm64/util/pmu.c | 3 +- tools/perf/arch/x86/tests/amd-ibs-period.c | 5 +- .../arch/x86/tests/amd-ibs-via-core-pmu.c | 3 +- tools/perf/arch/x86/tests/hybrid.c | 3 +- tools/perf/arch/x86/tests/topdown.c | 3 +- tools/perf/arch/x86/util/auxtrace.c | 9 ++-- tools/perf/arch/x86/util/evsel.c | 6 ++- tools/perf/arch/x86/util/intel-bts.c | 4 +- tools/perf/arch/x86/util/intel-pt.c | 4 +- tools/perf/arch/x86/util/topdown.c | 3 +- tools/perf/bench/pmu-scan.c | 3 +- tools/perf/builtin-stat.c | 8 +-- tools/perf/tests/parse-events.c | 14 ++--- tools/perf/trace/beauty/perf_event_open.c | 4 +- tools/perf/util/aslr.c | 7 ++- tools/perf/util/env.c | 2 +- tools/perf/util/evsel.c | 6 ++- tools/perf/util/evsel_fprintf.c | 6 ++- tools/perf/util/evsel_fprintf.h | 4 +- tools/perf/util/header.c | 8 +-- tools/perf/util/metricgroup.c | 51 ++++++++++++------- tools/perf/util/metricgroup.h | 4 +- tools/perf/util/parse-events.c | 18 ++++--- .../perf/util/perf-regs-arch/perf_regs_x86.c | 2 +- tools/perf/util/perf_event_attr_fprintf.c | 7 +-- tools/perf/util/pfm.c | 6 ++- tools/perf/util/pmus.c | 28 ++++------ tools/perf/util/pmus.h | 27 ++++++++-- tools/perf/util/s390-sample-raw.c | 16 ++++-- 31 files changed, 176 insertions(+), 100 deletions(-) diff --git a/tools/perf/arch/arm/util/auxtrace.c b/tools/perf/arch/arm/util= /auxtrace.c index 27bb14c8b880..f3a4f292a2c7 100644 --- a/tools/perf/arch/arm/util/auxtrace.c +++ b/tools/perf/arch/arm/util/auxtrace.c @@ -13,6 +13,7 @@ =20 #include "../../../util/auxtrace.h" #include "../../../util/debug.h" +#include "../../../util/env.h" #include "../../../util/evlist.h" #include "../../../util/pmu.h" #include "../../../util/pmus.h" @@ -22,6 +23,7 @@ =20 static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu **arm_spe_pmus =3D NULL; int ret, i, nr_cpus =3D sysconf(_SC_NPROCESSORS_CONF); /* arm_spe_xxxxxxxxx\0 */ @@ -42,7 +44,7 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_sp= es, int *err) return NULL; } =20 - arm_spe_pmus[*nr_spes] =3D perf_pmus__find(arm_spe_pmu_name); + arm_spe_pmus[*nr_spes] =3D perf_pmus__find(pmus, arm_spe_pmu_name); if (arm_spe_pmus[*nr_spes]) { pr_debug2("%s %d: arm_spe_pmu %d type %d name %s\n", __func__, __LINE__, *nr_spes, @@ -57,6 +59,7 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_sp= es, int *err) =20 static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu **hisi_ptt_pmus =3D NULL; struct dirent *dent; char path[PATH_MAX]; @@ -89,7 +92,7 @@ static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_p= tts, int *err) rewinddir(dir); while ((dent =3D readdir(dir))) { if (strstr(dent->d_name, HISI_PTT_PMU_NAME) && idx < *nr_ptts) { - hisi_ptt_pmus[idx] =3D perf_pmus__find(dent->d_name); + hisi_ptt_pmus[idx] =3D perf_pmus__find(pmus, dent->d_name); if (hisi_ptt_pmus[idx]) idx++; } @@ -133,7 +136,7 @@ struct auxtrace_record if (!evlist) return NULL; =20 - cs_etm_pmu =3D perf_pmus__find(CORESIGHT_ETM_PMU_NAME); + cs_etm_pmu =3D perf_pmus__find(perf_env__pmus(host_env()), CORESIGHT_ETM_= PMU_NAME); arm_spe_pmus =3D find_all_arm_spe_pmus(&nr_spes, err); hisi_ptt_pmus =3D find_all_hisi_ptt_pmus(&nr_ptts, err); =20 diff --git a/tools/perf/arch/arm/util/cs-etm.c b/tools/perf/arch/arm/util/c= s-etm.c index d2861d66a661..e15bd87f65fb 100644 --- a/tools/perf/arch/arm/util/cs-etm.c +++ b/tools/perf/arch/arm/util/cs-etm.c @@ -25,6 +25,7 @@ #include "../../../util/auxtrace.h" #include "../../../util/cpumap.h" #include "../../../util/debug.h" +#include "../../../util/env.h" #include "../../../util/event.h" #include "../../../util/evlist.h" #include "../../../util/evsel.h" @@ -883,7 +884,7 @@ struct auxtrace_record *cs_etm_record_init(int *err) struct perf_pmu *cs_etm_pmu; struct cs_etm_recording *ptr; =20 - cs_etm_pmu =3D perf_pmus__find(CORESIGHT_ETM_PMU_NAME); + cs_etm_pmu =3D perf_pmus__find(perf_env__pmus(host_env()), CORESIGHT_ETM_= PMU_NAME); =20 if (!cs_etm_pmu) { *err =3D -EINVAL; diff --git a/tools/perf/arch/arm64/util/pmu.c b/tools/perf/arch/arm64/util/= pmu.c index 895fb0d0610c..798b423fe149 100644 --- a/tools/perf/arch/arm64/util/pmu.c +++ b/tools/perf/arch/arm64/util/pmu.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 +#include "../../../util/env.h" #include "../../../util/pmu.h" #include "../../../util/pmus.h" #include "../../../util/tool_pmu.h" @@ -9,7 +10,7 @@ u64 tool_pmu__cpu_slots_per_cycle(void) { char path[PATH_MAX]; unsigned long long slots =3D 0; - struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(host_env= ())); =20 if (pmu) { perf_pmu__pathname_scnprintf(path, sizeof(path), diff --git a/tools/perf/arch/x86/tests/amd-ibs-period.c b/tools/perf/arch/x= 86/tests/amd-ibs-period.c index 32713f8fcd5c..4a32eaea61c7 100644 --- a/tools/perf/arch/x86/tests/amd-ibs-period.c +++ b/tools/perf/arch/x86/tests/amd-ibs-period.c @@ -930,6 +930,7 @@ static bool kernel_v6_15_or_newer(void) int test__amd_ibs_period(struct test_suite *test __maybe_unused, int subtest __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); char perf[PATH_MAX] =3D {'\0'}; int ret =3D TEST_OK; =20 @@ -940,8 +941,8 @@ int test__amd_ibs_period(struct test_suite *test __mayb= e_unused, * of the test to fail if kernel changes it after reading it here. */ perf_event_max_sample_rate =3D get_perf_event_max_sample_rate(); - fetch_pmu =3D perf_pmus__find("ibs_fetch"); - op_pmu =3D perf_pmus__find("ibs_op"); + fetch_pmu =3D perf_pmus__find(pmus, "ibs_fetch"); + op_pmu =3D perf_pmus__find(pmus, "ibs_op"); =20 if (!x86__is_amd_cpu() || !fetch_pmu || !op_pmu) return TEST_SKIP; diff --git a/tools/perf/arch/x86/tests/amd-ibs-via-core-pmu.c b/tools/perf/= arch/x86/tests/amd-ibs-via-core-pmu.c index 78b1902f6f59..a509a861b4f5 100644 --- a/tools/perf/arch/x86/tests/amd-ibs-via-core-pmu.c +++ b/tools/perf/arch/x86/tests/amd-ibs-via-core-pmu.c @@ -2,6 +2,7 @@ #include "arch-tests.h" #include "linux/perf_event.h" #include "tests/tests.h" +#include "env.h" #include "pmu.h" #include "pmus.h" #include "../perf-sys.h" @@ -44,7 +45,7 @@ int test__amd_ibs_via_core_pmu(struct test_suite *test __= maybe_unused, int ret =3D TEST_OK; int fd, i; =20 - ibs_pmu =3D perf_pmus__find("ibs_op"); + ibs_pmu =3D perf_pmus__find(perf_env__pmus(host_env()), "ibs_op"); if (!ibs_pmu) return TEST_SKIP; =20 diff --git a/tools/perf/arch/x86/tests/hybrid.c b/tools/perf/arch/x86/tests= /hybrid.c index 011217998a3e..0146cc2e0c5b 100644 --- a/tools/perf/arch/x86/tests/hybrid.c +++ b/tools/perf/arch/x86/tests/hybrid.c @@ -112,10 +112,11 @@ static int test__hybrid_group_modifier1(struct evlist= *evlist) =20 static int test__hybrid_raw1(struct evlist *evlist) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_evsel *evsel; =20 perf_evlist__for_each_evsel(evlist__core(evlist), evsel) { - struct perf_pmu *pmu =3D perf_pmus__find_by_type(evsel->attr.type); + struct perf_pmu *pmu =3D perf_pmus__find_by_type(pmus, evsel->attr.type); =20 TEST_ASSERT_VAL("missing pmu", pmu); TEST_ASSERT_VAL("unexpected pmu", !strncmp(pmu->name, "cpu_", 4)); diff --git a/tools/perf/arch/x86/tests/topdown.c b/tools/perf/arch/x86/test= s/topdown.c index 520075329c37..827169b60813 100644 --- a/tools/perf/arch/x86/tests/topdown.c +++ b/tools/perf/arch/x86/tests/topdown.c @@ -5,6 +5,7 @@ #include "arch-tests.h" #include "../util/topdown.h" #include "debug.h" +#include "env.h" #include "evlist.h" #include "parse-events.h" #include "pmu.h" @@ -245,7 +246,7 @@ static int test__x86_topdown_metrics_clear(struct test_= suite *test __maybe_unuse if (!topdown_sys_has_perf_metrics()) return TEST_OK; =20 - pmu =3D perf_pmus__find_by_type(PERF_TYPE_RAW); + pmu =3D perf_pmus__find_by_type(perf_env__pmus(host_env()), PERF_TYPE_RAW= ); if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear")) return TEST_OK; =20 diff --git a/tools/perf/arch/x86/util/auxtrace.c b/tools/perf/arch/x86/util= /auxtrace.c index 84fce0b51ccf..540caf06d15c 100644 --- a/tools/perf/arch/x86/util/auxtrace.c +++ b/tools/perf/arch/x86/util/auxtrace.c @@ -9,6 +9,7 @@ =20 #include "../../../util/header.h" #include "../../../util/debug.h" +#include "../../../util/env.h" #include "../../../util/pmu.h" #include "../../../util/pmus.h" #include "../../../util/auxtrace.h" @@ -20,15 +21,13 @@ static struct auxtrace_record *auxtrace_record__init_intel(struct evlist *evlist, int *err) { - struct perf_pmu *intel_pt_pmu; - struct perf_pmu *intel_bts_pmu; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); + struct perf_pmu *intel_pt_pmu =3D perf_pmus__find(pmus, INTEL_PT_PMU_NAME= ); + struct perf_pmu *intel_bts_pmu =3D perf_pmus__find(pmus, INTEL_BTS_PMU_NA= ME); struct evsel *evsel; bool found_pt =3D false; bool found_bts =3D false; =20 - intel_pt_pmu =3D perf_pmus__find(INTEL_PT_PMU_NAME); - intel_bts_pmu =3D perf_pmus__find(INTEL_BTS_PMU_NAME); - evlist__for_each_entry(evlist, evsel) { if (intel_pt_pmu && evsel->core.attr.type =3D=3D intel_pt_pmu->type) found_pt =3D true; diff --git a/tools/perf/arch/x86/util/evsel.c b/tools/perf/arch/x86/util/ev= sel.c index 23a8e662a912..a8e239d29b72 100644 --- a/tools/perf/arch/x86/util/evsel.c +++ b/tools/perf/arch/x86/util/evsel.c @@ -132,6 +132,7 @@ static void ibs_l3miss_warn(void) =20 void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *= attr) { + struct perf_pmus *pmus; struct perf_pmu *evsel_pmu, *ibs_fetch_pmu, *ibs_op_pmu; static int warned_once; =20 @@ -142,8 +143,9 @@ void arch__post_evsel_config(struct evsel *evsel, struc= t perf_event_attr *attr) if (!evsel_pmu) return; =20 - ibs_fetch_pmu =3D perf_pmus__find("ibs_fetch"); - ibs_op_pmu =3D perf_pmus__find("ibs_op"); + pmus =3D perf_env__pmus(host_env()); + ibs_fetch_pmu =3D perf_pmus__find(pmus, "ibs_fetch"); + ibs_op_pmu =3D perf_pmus__find(pmus, "ibs_op"); =20 if (ibs_fetch_pmu && ibs_fetch_pmu->type =3D=3D evsel_pmu->type) { if (attr->config & IBS_FETCH_L3MISSONLY) { diff --git a/tools/perf/arch/x86/util/intel-bts.c b/tools/perf/arch/x86/uti= l/intel-bts.c index d44d568a6d21..2a7b20f5a1ca 100644 --- a/tools/perf/arch/x86/util/intel-bts.c +++ b/tools/perf/arch/x86/util/intel-bts.c @@ -19,6 +19,7 @@ #include "../../../util/auxtrace.h" #include "../../../util/cpumap.h" #include "../../../util/debug.h" +#include "../../../util/env.h" #include "../../../util/event.h" #include "../../../util/evlist.h" #include "../../../util/evsel.h" @@ -420,7 +421,8 @@ static int intel_bts_find_snapshot(struct auxtrace_reco= rd *itr, int idx, =20 struct auxtrace_record *intel_bts_recording_init(int *err) { - struct perf_pmu *intel_bts_pmu =3D perf_pmus__find(INTEL_BTS_PMU_NAME); + struct perf_pmu *intel_bts_pmu =3D perf_pmus__find(perf_env__pmus(host_en= v()), + INTEL_BTS_PMU_NAME); struct intel_bts_recording *btsr; =20 if (!intel_bts_pmu) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index a533114c0048..7756971ade50 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -23,6 +23,7 @@ #include "../../../util/config.h" #include "../../../util/cpumap.h" #include "../../../util/debug.h" +#include "../../../util/env.h" #include "../../../util/event.h" #include "../../../util/evlist.h" #include "../../../util/evsel.h" @@ -1195,7 +1196,8 @@ static int intel_pt_perf_config(const char *var, cons= t char *value, void *data) =20 struct auxtrace_record *intel_pt_recording_init(int *err) { - struct perf_pmu *intel_pt_pmu =3D perf_pmus__find(INTEL_PT_PMU_NAME); + struct perf_pmu *intel_pt_pmu =3D perf_pmus__find(perf_env__pmus(host_env= ()), + INTEL_PT_PMU_NAME); struct intel_pt_recording *ptr; =20 if (!intel_pt_pmu) diff --git a/tools/perf/arch/x86/util/topdown.c b/tools/perf/arch/x86/util/= topdown.c index 64bc64e335a2..797ace8999e4 100644 --- a/tools/perf/arch/x86/util/topdown.c +++ b/tools/perf/arch/x86/util/topdown.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include "util/env.h" #include "util/evlist.h" #include "util/pmu.h" #include "util/pmus.h" @@ -26,7 +27,7 @@ bool topdown_sys_has_perf_metrics(void) * The slots event is only available when the core PMU * supports the perf metrics feature. */ - pmu =3D perf_pmus__find_by_type(PERF_TYPE_RAW); + pmu =3D perf_pmus__find_by_type(perf_env__pmus(host_env()), PERF_TYPE_RAW= ); if (pmu && perf_pmu__have_event(pmu, "slots")) has_perf_metrics =3D true; =20 diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c index 1d7dd4b203e0..754486d1d87b 100644 --- a/tools/perf/bench/pmu-scan.c +++ b/tools/perf/bench/pmu-scan.c @@ -77,6 +77,7 @@ static int save_result(void) =20 static int check_result(bool core_only) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct pmu_scan_result *r; struct perf_pmu *pmu; struct list_head *list; @@ -87,7 +88,7 @@ static int check_result(bool core_only) if (core_only && !r->is_core) continue; =20 - pmu =3D perf_pmus__find(r->name); + pmu =3D perf_pmus__find(pmus, r->name); if (pmu =3D=3D NULL) { pr_err("Cannot find PMU %s\n", r->name); return -1; diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 8706798290eb..f81192cec481 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1982,6 +1982,7 @@ static int default_evlist_evsel_cmp(void *priv __mayb= e_unused, static int add_default_events(void) { const char *pmu =3D parse_events_option_args.pmu_filter ?: "all"; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct parse_events_error err; struct evlist *evlist =3D evlist__new(); struct evsel *evsel; @@ -2002,7 +2003,7 @@ static int add_default_events(void) * will use this approach. To determine transaction support * on an architecture test for such a metric name. */ - if (!metricgroup__has_metric_or_groups(pmu, "transaction")) { + if (!metricgroup__has_metric_or_groups(pmus, pmu, "transaction")) { pr_err("Missing transaction metrics\n"); ret =3D -1; goto out; @@ -2037,7 +2038,7 @@ static int add_default_events(void) smi_reset =3D true; } =20 - if (!metricgroup__has_metric_or_groups(pmu, "smi")) { + if (!metricgroup__has_metric_or_groups(pmus, pmu, "smi")) { pr_err("Missing smi metrics\n"); ret =3D -1; goto out; @@ -2113,7 +2114,8 @@ static int add_default_events(void) for (size_t i =3D 0; i < ARRAY_SIZE(default_metricgroup_names); i++) { struct evlist *metric_evlist; =20 - if (!metricgroup__has_metric_or_groups(pmu, default_metricgroup_names[i= ])) + if (!metricgroup__has_metric_or_groups(pmus, pmu, + default_metricgroup_names[i])) continue; =20 if ((int)i > detailed_run) diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-event= s.c index 45c83d7d01d6..4b7551391b40 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c @@ -649,7 +649,7 @@ static int test__checkevent_pmu(struct evlist *evlist) { =20 struct evsel *evsel =3D evlist__first(evlist); - struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(hos= t_env())); =20 TEST_ASSERT_EVSEL("wrong number of entries", 1 =3D=3D evlist__nr_entries(= evlist), evsel); TEST_ASSERT_EVSEL("wrong type", core_pmu->type =3D=3D evsel->core.attr.ty= pe, evsel); @@ -713,7 +713,7 @@ static int test__checkevent_list(struct evlist *evlist) static int test__checkevent_pmu_name(struct evlist *evlist) { struct evsel *evsel =3D evlist__first(evlist); - struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(hos= t_env())); char buf[256]; =20 /* default_core/config=3D1,name=3Dkrava/u */ @@ -737,7 +737,7 @@ static int test__checkevent_pmu_name(struct evlist *evl= ist) static int test__checkevent_pmu_partial_time_callgraph(struct evlist *evli= st) { struct evsel *evsel =3D evlist__first(evlist); - struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(hos= t_env())); =20 /* default_core/config=3D1,call-graph=3Dfp,time,period=3D100000/ */ TEST_ASSERT_EVLIST("wrong number of entries", @@ -770,7 +770,7 @@ static int test__checkevent_pmu_partial_time_callgraph(= struct evlist *evlist) static int test__checkevent_pmu_events(struct evlist *evlist) { struct evsel *evsel; - struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(hos= t_env())); =20 TEST_ASSERT_EVLIST("wrong number of entries", 1 <=3D evlist__nr_entries(e= vlist), evlist); =20 @@ -1776,7 +1776,7 @@ static int test__checkevent_config_cache(struct evlis= t *evlist) =20 static bool test__pmu_default_core_event_valid(void) { - struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(host_env= ())); =20 if (!pmu) return false; @@ -1786,7 +1786,7 @@ static bool test__pmu_default_core_event_valid(void) =20 static bool test__intel_pt_valid(void) { - return !!perf_pmus__find("intel_pt"); + return !!perf_pmus__find(perf_env__pmus(host_env()), "intel_pt"); } =20 static int test__intel_pt(struct evlist *evlist) @@ -2632,7 +2632,7 @@ static int combine_test_results(int existing, int lat= est) static int test_events(const struct evlist_test *events, int cnt) { int ret =3D TEST_OK; - struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *core_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(hos= t_env())); =20 for (int i =3D 0; i < cnt; i++) { struct evlist_test e =3D events[i]; diff --git a/tools/perf/trace/beauty/perf_event_open.c b/tools/perf/trace/b= eauty/perf_event_open.c index bfdf322c6ede..edf156995be6 100644 --- a/tools/perf/trace/beauty/perf_event_open.c +++ b/tools/perf/trace/beauty/perf_event_open.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: LGPL-2.1 #include #include "trace/beauty/beauty.h" +#include "util/env.h" #include "util/evsel_fprintf.h" #include =20 @@ -75,7 +76,8 @@ static size_t perf_event_attr___scnprintf(struct perf_eve= nt_attr *attr, char *bf .bf =3D bf, }; =20 - perf_event_attr__fprintf(stdout, attr, attr__fprintf, &args); + perf_event_attr__fprintf(stdout, attr, attr__fprintf, &args, + perf_env__pmus(host_env())); return args.printed + scnprintf(bf + args.printed, size - args.printed, "= }"); } =20 diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c index e365f807b021..60928c490eb1 100644 --- a/tools/perf/util/aslr.c +++ b/tools/perf/util/aslr.c @@ -3,6 +3,7 @@ =20 #include "addr_location.h" #include "debug.h" +#include "env.h" #include "event.h" #include "evsel.h" #include "evlist.h" @@ -1255,9 +1256,10 @@ void aslr_tool__strip_attr_event(union perf_event *e= vent, struct evlist *evlist) attr_size >=3D (offsetof(struct perf_event_attr, bp_addr) + sizeof(u= 64))) { event->attr.attr.bp_addr =3D 0; } else if (type >=3D PERF_TYPE_MAX) { + struct perf_env *env =3D evlist__env(evlist) ?: host_env(); struct perf_pmu *pmu; =20 - pmu =3D perf_pmus__find_by_type(type); + pmu =3D perf_pmus__find_by_type(perf_env__pmus(env), type); if (pmu && (!strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"))) { if (attr_size >=3D (offsetof(struct perf_event_attr, config1) + sizeof= (u64))) @@ -1407,6 +1409,7 @@ int aslr_tool__cache_orig_attrs(struct perf_tool *too= l, struct evsel *evsel) =20 void aslr_tool__strip_evlist(const struct perf_tool *tool __maybe_unused, = struct evlist *evlist) { + struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(evlist) ?: host_env= ()); struct evsel *evsel; =20 evlist__for_each_entry(evlist, evsel) { @@ -1419,7 +1422,7 @@ void aslr_tool__strip_evlist(const struct perf_tool *= tool __maybe_unused, struct if (evsel->core.attr.type =3D=3D PERF_TYPE_BREAKPOINT) { evsel->core.attr.bp_addr =3D 0; } else if (evsel->core.attr.type >=3D PERF_TYPE_MAX) { - struct perf_pmu *pmu =3D perf_pmus__find_by_type(evsel->core.attr.type); + struct perf_pmu *pmu =3D perf_pmus__find_by_type(pmus, evsel->core.attr= .type); =20 if (pmu && (!strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"))) { diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 5f16920f0a89..4f4ec49b6a6b 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -616,7 +616,7 @@ int perf_env__read_core_pmu_caps(struct perf_env *env) return -ENODEV; =20 if (nr_pmu =3D=3D 1) { - pmu =3D perf_pmus__find_core_pmu(); + pmu =3D perf_pmus__find_core_pmu(pmus); if (!pmu) return -ENODEV; ret =3D perf_pmu__caps_parse(pmu); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 5949636dbf3d..3122721a3f15 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2472,7 +2472,8 @@ static void display_attr(struct perf_event_attr *attr) if (verbose >=3D 2 || debug_peo_args) { fprintf(stderr, "%.60s\n", graph_dotted_line); fprintf(stderr, "perf_event_attr:\n"); - perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL); + perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL, + perf_env__pmus(host_env())); fprintf(stderr, "%.60s\n", graph_dotted_line); } } @@ -4320,7 +4321,8 @@ bool evsel__fallback(struct evsel *evsel, struct targ= et *target, int err, * available even if no PMU support. PPC returned ENXIO rather * than ENODEV or ENOENT until 2.6.37. */ - evsel->pmu =3D perf_pmus__find_by_type(PERF_TYPE_SOFTWARE); + evsel->pmu =3D perf_pmus__find_by_type(perf_env__pmus(host_env()), + PERF_TYPE_SOFTWARE); assert(evsel->pmu); /* software is a "well-known" and can't fail PMU typ= e. */ =20 /* Configure the event. */ diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprint= f.c index 0f7a25500a44..ed7d13461575 100644 --- a/tools/perf/util/evsel_fprintf.c +++ b/tools/perf/util/evsel_fprintf.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/env.h" #include "util/evlist.h" #include "evsel.h" #include "util/evsel_fprintf.h" @@ -66,8 +67,11 @@ int evsel__fprintf(struct evsel *evsel, struct perf_attr= _details *details, FILE printed +=3D fprintf(fp, "%s", evsel__name(evsel)); =20 if (details->verbose) { + struct perf_env *env =3D evsel__env(evsel) ?: host_env(); + printed +=3D perf_event_attr__fprintf(fp, &evsel->core.attr, - __print_attr__fprintf, &first); + __print_attr__fprintf, &first, + perf_env__pmus(env)); } else if (details->freq) { const char *term =3D "sample_freq"; =20 diff --git a/tools/perf/util/evsel_fprintf.h b/tools/perf/util/evsel_fprint= f.h index c8a9fac2f2dd..792437ceb578 100644 --- a/tools/perf/util/evsel_fprintf.h +++ b/tools/perf/util/evsel_fprintf.h @@ -6,6 +6,7 @@ #include =20 struct evsel; +struct perf_pmus; =20 struct perf_attr_details { bool freq; @@ -46,5 +47,6 @@ int sample__fprintf_sym(struct perf_sample *sample, struc= t addr_location *al, typedef int (*attr__fprintf_f)(FILE *, const char *, const char *, void *); =20 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, - attr__fprintf_f attr__fprintf, void *priv); + attr__fprintf_f attr__fprintf, void *priv, + struct perf_pmus *pmus); #endif // __PERF_EVSEL_H diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 12d82775b88a..16e49baa9d5e 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -1701,7 +1701,7 @@ static int __write_pmu_caps(struct feat_fd *ff, struc= t perf_pmu *pmu, static int write_cpu_pmu_caps(struct feat_fd *ff, struct evlist *evlist __maybe_unused) { - struct perf_pmu *cpu_pmu =3D perf_pmus__find_core_pmu(); + struct perf_pmu *cpu_pmu =3D perf_pmus__find_core_pmu(perf_env__pmus(host= _env())); int ret; =20 if (!cpu_pmu) @@ -2489,6 +2489,7 @@ static int __desc_attr__fprintf(FILE *fp, const char = *name, const char *val, =20 static void print_event_desc(struct feat_fd *ff, FILE *fp) { + struct perf_pmus *pmus =3D perf_env__pmus(&ff->ph->env); struct evsel *evsel, *events; u32 j; u64 *id; @@ -2516,7 +2517,7 @@ static void print_event_desc(struct feat_fd *ff, FILE= *fp) fprintf(fp, " }"); } =20 - perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NU= LL); + perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NU= LL, pmus); =20 fputc('\n', fp); } @@ -5732,7 +5733,8 @@ size_t perf_event__fprintf_event_update(union perf_ev= ent *event, FILE *fp) =20 size_t perf_event__fprintf_attr(union perf_event *event, FILE *fp) { - return perf_event_attr__fprintf(fp, &event->attr.attr, __desc_attr__fprin= tf, NULL); + return perf_event_attr__fprintf(fp, &event->attr.attr, __desc_attr__fprin= tf, NULL, + perf_env__pmus(host_env())); } =20 int perf_event__process_attr(const struct perf_tool *tool __maybe_unused, diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 63aa326944d7..593032197edd 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -388,14 +388,14 @@ static bool match_metric_or_groups(const char *metric= _or_groups, const char *sou return m && match_metric_or_groups(m + 1, sought); } =20 -static bool match_pm_metric_or_groups(const struct pmu_metric *pm, const c= har *pmu, - const char *metric_or_groups) +static bool match_pm_metric_or_groups(struct perf_pmus *pmus, const struct= pmu_metric *pm, + const char *pmu, const char *metric_or_groups) { const char *pm_pmu =3D pm->pmu ?: "cpu"; struct perf_pmu *perf_pmu =3D NULL; =20 if (pm->pmu) - perf_pmu =3D perf_pmus__find(pm->pmu); + perf_pmu =3D perf_pmus__find(pmus, pm->pmu); =20 if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu) && (perf_pmu && !perf_pmu__name_wildcard_match(perf_pmu, pmu))) @@ -697,6 +697,7 @@ struct metricgroup_add_iter_data { }; =20 static int add_metric(struct list_head *metric_list, + struct perf_pmus *pmus, const struct pmu_metric *pm, const char *modifier, bool metric_no_group, @@ -722,6 +723,7 @@ static int metricgroup__find_metric_callback(const stru= ct pmu_metric *pm, * resolve_metric - Locate metrics within the root metric and recursively = add * references to them. * @metric_list: The list the metric is added to. + * @pmus: The PMUs collection for resolving referenced metrics. * @pmu: The PMU name to resolve metrics on, or "all" for all PMUs. * @modifier: if non-null event modifiers like "u". * @metric_no_group: Should events written to events be grouped "{}" or @@ -739,6 +741,7 @@ static int metricgroup__find_metric_callback(const stru= ct pmu_metric *pm, * architecture perf is running upon. */ static int resolve_metric(struct list_head *metric_list, + struct perf_pmus *pmus, struct perf_pmu *pmu, const char *modifier, bool metric_no_group, @@ -793,7 +796,7 @@ static int resolve_metric(struct list_head *metric_list, * context. */ for (i =3D 0; i < pending_cnt; i++) { - ret =3D add_metric(metric_list, &pending[i].pm, modifier, metric_no_grou= p, + ret =3D add_metric(metric_list, pmus, &pending[i].pm, modifier, metric_n= o_group, metric_no_threshold, user_requested_cpu_list, system_wide, fake_pmu, root_metric, visited, table); if (ret) @@ -807,6 +810,7 @@ static int resolve_metric(struct list_head *metric_list, /** * __add_metric - Add a metric to metric_list. * @metric_list: The list the metric is added to. + * @pmus: The PMUs collection for resolving referenced metrics. * @pm: The pmu_metric containing the metric to be added. * @modifier: if non-null event modifiers like "u". * @metric_no_group: Should events written to events be grouped "{}" or @@ -826,6 +830,7 @@ static int resolve_metric(struct list_head *metric_list, * architecture perf is running upon. */ static int __add_metric(struct list_head *metric_list, + struct perf_pmus *pmus, const struct pmu_metric *pm, const char *modifier, bool metric_no_group, @@ -927,11 +932,11 @@ static int __add_metric(struct list_head *metric_list, struct perf_pmu *pmu; =20 if (pm->pmu && pm->pmu[0] !=3D '\0') - pmu =3D perf_pmus__find(pm->pmu); + pmu =3D perf_pmus__find(pmus, pm->pmu); else pmu =3D perf_pmus__scan_core(/*pmu=3D*/ NULL); =20 - ret =3D resolve_metric(metric_list, pmu, modifier, metric_no_group, + ret =3D resolve_metric(metric_list, pmus, pmu, modifier, metric_no_group, metric_no_threshold, user_requested_cpu_list, system_wide, fake_pmu, root_metric, &visited_node, table); @@ -947,6 +952,7 @@ static int __add_metric(struct list_head *metric_list, } =20 static int add_metric(struct list_head *metric_list, + struct perf_pmus *pmus, const struct pmu_metric *pm, const char *modifier, bool metric_no_group, @@ -963,7 +969,7 @@ static int add_metric(struct list_head *metric_list, pr_debug("metric expr %s for %s\n", pm->metric_expr, pm->metric_name); =20 if (!strstr(pm->metric_expr, "?")) { - ret =3D __add_metric(metric_list, pm, modifier, metric_no_group, + ret =3D __add_metric(metric_list, pmus, pm, modifier, metric_no_group, metric_no_threshold, 0, user_requested_cpu_list, system_wide, fake_pmu, root_metric, visited, table); } else { @@ -977,7 +983,7 @@ static int add_metric(struct list_head *metric_list, */ =20 for (j =3D 0; j < count && !ret; j++) - ret =3D __add_metric(metric_list, pm, modifier, metric_no_group, + ret =3D __add_metric(metric_list, pmus, pm, modifier, metric_no_group, metric_no_threshold, j, user_requested_cpu_list, system_wide, fake_pmu, root_metric, visited, table); } @@ -1033,6 +1039,7 @@ static int default_metricgroup_cmp(void *priv __maybe= _unused, =20 struct metricgroup__add_metric_data { struct list_head *list; + struct perf_pmus *pmus; const char *pmu; const char *metric_name; const char *modifier; @@ -1051,14 +1058,15 @@ static int metricgroup__add_metric_callback(const s= truct pmu_metric *pm, struct metricgroup__add_metric_data *data =3D vdata; int ret =3D 0; =20 - if (pm->metric_expr && match_pm_metric_or_groups(pm, data->pmu, data->met= ric_name)) { + if (pm->metric_expr && match_pm_metric_or_groups(data->pmus, pm, data->pm= u, + data->metric_name)) { bool metric_no_group =3D data->metric_no_group || match_metric_or_groups(pm->metricgroup_no_group, data->metric_name); =20 data->has_match =3D true; - ret =3D add_metric(data->list, pm, data->modifier, metric_no_group, + ret =3D add_metric(data->list, data->pmus, pm, data->modifier, metric_no= _group, data->metric_no_threshold, data->user_requested_cpu_list, - data->system_wide, data->fake_pmu, /*root_metric=3D*/NULL, + data->system_wide, data->fake_pmu, /*root_metric=3D*/NULL, /*visited_metrics=3D*/NULL, table); } return ret; @@ -1066,6 +1074,7 @@ static int metricgroup__add_metric_callback(const str= uct pmu_metric *pm, =20 /** * metricgroup__add_metric - Find and add a metric, or a metric group. + * @pmus: The PMUs collection for resolving PMU names and referenced metri= cs. * @pmu: The PMU name to search for metrics on, or "all" for all PMUs. * @metric_name: The name of the metric or metric group. For example, "IPC" * could be the name of a metric and "TopDownL1" the name of= a @@ -1081,7 +1090,8 @@ static int metricgroup__add_metric_callback(const str= uct pmu_metric *pm, * @table: The table that is searched for metrics, most commonly the table= for the * architecture perf is running upon. */ -static int metricgroup__add_metric(const char *pmu, const char *metric_nam= e, const char *modifier, +static int metricgroup__add_metric(struct perf_pmus *pmus, const char *pmu, + const char *metric_name, const char *modifier, bool metric_no_group, bool metric_no_threshold, const char *user_requested_cpu_list, bool system_wide, bool fake_pmu, @@ -1092,6 +1102,7 @@ static int metricgroup__add_metric(const char *pmu, c= onst char *metric_name, con int ret; struct metricgroup__add_metric_data data =3D { .list =3D &list, + .pmus =3D pmus, .pmu =3D pmu, .metric_name =3D metric_name, .modifier =3D modifier, @@ -1122,6 +1133,7 @@ static int metricgroup__add_metric(const char *pmu, c= onst char *metric_name, con /** * metricgroup__add_metric_list - Find and add metrics, or metric groups, * specified in a list. + * @pmus: The PMUs collection for resolving PMU names and referenced metri= cs. * @pmu: A pmu to restrict the metrics to, or "all" for all PMUS. * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopD= ownL1" * would match the IPC and CPI metrics, and TopDownL1 would match a= ll @@ -1136,7 +1148,8 @@ static int metricgroup__add_metric(const char *pmu, c= onst char *metric_name, con * @table: The table that is searched for metrics, most commonly the table= for the * architecture perf is running upon. */ -static int metricgroup__add_metric_list(const char *pmu, const char *list, +static int metricgroup__add_metric_list(struct perf_pmus *pmus, const char= *pmu, + const char *list, bool metric_no_group, bool metric_no_threshold, const char *user_requested_cpu_list, @@ -1157,7 +1170,7 @@ static int metricgroup__add_metric_list(const char *p= mu, const char *list, if (modifier) *modifier++ =3D '\0'; =20 - ret =3D metricgroup__add_metric(pmu, metric_name, modifier, + ret =3D metricgroup__add_metric(pmus, pmu, metric_name, modifier, metric_no_group, metric_no_threshold, user_requested_cpu_list, system_wide, fake_pmu, @@ -1412,6 +1425,7 @@ static int parse_groups(struct evlist *perf_evlist, bool fake_pmu, const struct pmu_metrics_table *table) { + struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(perf_evlist) ?: hos= t_env()); struct evlist *combined_evlist =3D NULL; LIST_HEAD(metric_list); struct metric *m; @@ -1419,7 +1433,7 @@ static int parse_groups(struct evlist *perf_evlist, bool is_default =3D !strcmp(str, "Default"); int ret; =20 - ret =3D metricgroup__add_metric_list(pmu, str, metric_no_group, metric_no= _threshold, + ret =3D metricgroup__add_metric_list(pmus, pmu, str, metric_no_group, met= ric_no_threshold, user_requested_cpu_list, system_wide, fake_pmu, &metric_list, table); @@ -1618,6 +1632,7 @@ int metricgroup__parse_groups_test(struct evlist *evl= ist, } =20 struct metricgroup__has_metric_data { + struct perf_pmus *pmus; const char *pmu; const char *metric_or_groups; }; @@ -1628,13 +1643,15 @@ static int metricgroup__has_metric_or_groups_callba= ck(const struct pmu_metric *p { struct metricgroup__has_metric_data *data =3D vdata; =20 - return match_pm_metric_or_groups(pm, data->pmu, data->metric_or_groups) ?= 1 : 0; + return match_pm_metric_or_groups(data->pmus, pm, data->pmu, data->metric_= or_groups) ? 1 : 0; } =20 -bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric= _or_groups) +bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char = *pmu, + const char *metric_or_groups) { const struct pmu_metrics_table *table =3D pmu_metrics_table__find(); struct metricgroup__has_metric_data data =3D { + .pmus =3D pmus, .pmu =3D pmu, .metric_or_groups =3D metric_or_groups, }; diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index 6a66f14dd01b..bc5d3d21f405 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -10,6 +10,7 @@ struct evlist; struct evsel; struct option; +struct perf_pmus; struct print_callbacks; struct rblist; struct cgroup; @@ -86,7 +87,8 @@ int metricgroup__parse_groups_test(struct evlist *evlist, =20 int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pm= u_metric_iter_fn fn, void *data); -bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric= _or_groups); +bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char = *pmu, + const char *metric_or_groups); unsigned int metricgroups__topdown_max_level(void); int arch_get_runtimeparam(const struct pmu_metric *pm); void metricgroup__rblist_init(struct rblist *metric_events); diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index a70bccbf51dc..f38c83b24e6a 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -85,7 +85,7 @@ static char *get_config_name(const struct parse_events_te= rms *head_terms) } =20 static struct perf_cpu_map *get_config_cpu(const struct parse_events_terms= *head_terms, - bool fake_pmu) + struct parse_events_state *parse_state) { struct parse_events_term *term; struct perf_cpu_map *cpus =3D NULL; @@ -102,7 +102,8 @@ static struct perf_cpu_map *get_config_cpu(const struct= parse_events_terms *head if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_NUM) { term_cpus =3D perf_cpu_map__new_int(term->val.num); } else { - struct perf_pmu *pmu =3D perf_pmus__find(term->val.str); + struct perf_pmu *pmu =3D perf_pmus__find(parse_state->pmus, + term->val.str); =20 if (pmu) { term_cpus =3D pmu->is_core && perf_cpu_map__is_empty(pmu->cpus) @@ -110,7 +111,7 @@ static struct perf_cpu_map *get_config_cpu(const struct= parse_events_terms *head : perf_cpu_map__get(pmu->cpus); } else { term_cpus =3D perf_cpu_map__new(term->val.str); - if (!term_cpus && fake_pmu) { + if (!term_cpus && parse_state->fake_pmu) { /* * Assume the PMU string makes sense on a different * machine and fake a value with all online CPUs. @@ -952,7 +953,7 @@ do { \ break; } assert(term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR); - if (perf_pmus__find(term->val.str) !=3D NULL) + if (perf_pmus__find(parse_state->pmus, term->val.str) !=3D NULL) break; =20 map =3D perf_cpu_map__new(term->val.str); @@ -1452,7 +1453,7 @@ static int __parse_events_add_numeric(struct parse_ev= ents_state *parse_state, =20 name =3D get_config_name(head_config); metric_id =3D get_config_metric_id(head_config); - cpus =3D get_config_cpu(head_config, parse_state->fake_pmu); + cpus =3D get_config_cpu(head_config, parse_state); ret =3D __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, na= me, metric_id, parse_state->pmus, pmu, &config_terms, first_wildcard_match, cpus, @@ -1494,7 +1495,8 @@ int parse_events_add_numeric(struct parse_events_stat= e *parse_state, if (found_supported) return 0; } - return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_t= ype(type), + return __parse_events_add_numeric(parse_state, list, + perf_pmus__find_by_type(parse_state->pmus, type), type, /*extended_type=3D*/0, config, head_config, /*first_wildcard_match=3D*/NULL); } @@ -1615,7 +1617,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, return -EINVAL; } =20 - term_cpu =3D get_config_cpu(&parsed_terms, parse_state->fake_pmu); + term_cpu =3D get_config_cpu(&parsed_terms, parse_state); evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/tru= e, get_config_name(&parsed_terms), get_config_metric_id(&parsed_terms), parse_state->pmus, pmu, @@ -1753,7 +1755,7 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pars= e_events_state *parse_state INIT_LIST_HEAD(*listp); =20 /* Attempt to add to list assuming event_or_pmu is a PMU name. */ - pmu =3D perf_pmus__find(event_or_pmu); + pmu =3D perf_pmus__find(parse_state->pmus, event_or_pmu); if (pmu && !parse_events_add_pmu(parse_state, *listp, pmu, const_parsed_t= erms, first_wildcard_match)) return 0; diff --git a/tools/perf/util/perf-regs-arch/perf_regs_x86.c b/tools/perf/ut= il/perf-regs-arch/perf_regs_x86.c index 2d80f119629a..920f17b89659 100644 --- a/tools/perf/util/perf-regs-arch/perf_regs_x86.c +++ b/tools/perf/util/perf-regs-arch/perf_regs_x86.c @@ -534,7 +534,7 @@ __support_simd_reg_class(uint64_t sample_type, uint16_t= qwords, } =20 if (perf_pmus__num_core_pmus(pmus) > 1) { - __u64 type =3D perf_pmus__find_core_pmu()->type; + __u64 type =3D perf_pmus__find_core_pmu(pmus)->type; =20 attr.config |=3D type << PERF_PMU_TYPE_SHIFT; } diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/pe= rf_event_attr_fprintf.c index 2e8be9a357dc..7fd87c0e19f6 100644 --- a/tools/perf/util/perf_event_attr_fprintf.c +++ b/tools/perf/util/perf_event_attr_fprintf.c @@ -298,9 +298,10 @@ do { \ #define PRINT_ATTRf_bf(_f, _p) PRINT_ATTRn_bf(#_f, _f, _p, false) =20 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, - attr__fprintf_f attr__fprintf, void *priv) + attr__fprintf_f attr__fprintf, void *priv, + struct perf_pmus *pmus) { - struct perf_pmu *pmu =3D perf_pmus__find_by_type(attr->type); + struct perf_pmu *pmu =3D perf_pmus__find_by_type(pmus, attr->type); /* * size =3D=3D 0 means ABI0 =E2=80=94 the producer didn't set attr.size. * perf_event__fprintf_attr() may pass the raw mmap'd event @@ -328,7 +329,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_even= t_attr *attr, u32 extended_type =3D attr->config >> PERF_PMU_TYPE_SHIFT; =20 if (extended_type) - pmu =3D perf_pmus__find_by_type(extended_type); + pmu =3D perf_pmus__find_by_type(pmus, extended_type); } =20 PRINT_ATTRn("type", type, p_type_id, true); diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c index ad9bc481da70..c761e701a41d 100644 --- a/tools/perf/util/pfm.c +++ b/tools/perf/util/pfm.c @@ -6,6 +6,7 @@ */ #include "util/cpumap.h" #include "util/debug.h" +#include "util/env.h" #include "util/event.h" #include "util/evlist.h" #include "util/evsel.h" @@ -35,6 +36,7 @@ int parse_libpfm_events_option(const struct option *opt, = const char *str, int unset __maybe_unused) { struct evlist *evlist =3D *(struct evlist **)opt->value; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_event_attr attr; struct perf_pmu *pmu; struct evsel *evsel, *grp_leader =3D NULL; @@ -84,7 +86,7 @@ int parse_libpfm_events_option(const struct option *opt, = const char *str, goto error; } =20 - pmu =3D perf_pmus__find_by_type((unsigned int)attr.type); + pmu =3D perf_pmus__find_by_type(pmus, (unsigned int)attr.type); evsel =3D parse_events__add_event(evlist__nr_entries(evlist), &attr, q, /*metric_id=3D*/NULL, pmu); @@ -135,7 +137,7 @@ static bool is_libpfm_event_supported(const char *name,= struct perf_cpu_map *cpu if (ret !=3D PFM_SUCCESS) return false; =20 - pmu =3D perf_pmus__find_by_type((unsigned int)attr.type); + pmu =3D perf_pmus__find_by_type(perf_env__pmus(host_env()), (unsigned int= )attr.type); evsel =3D parse_events__add_event(0, &attr, name, /*metric_id=3D*/NULL, p= mu); if (evsel =3D=3D NULL) return false; diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index cf2255e2a8b7..0cfdfc4a8a99 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -182,7 +182,7 @@ static struct perf_pmu *pmu_find(struct perf_pmus *pmus= , const char *name) return NULL; } =20 -static struct perf_pmu *__perf_pmus__find(struct perf_pmus *pmus, const ch= ar *name) +struct perf_pmu *perf_pmus__find(struct perf_pmus *pmus, const char *name) { struct perf_pmu *pmu; int dirfd; @@ -190,7 +190,7 @@ static struct perf_pmu *__perf_pmus__find(struct perf_p= mus *pmus, const char *na unsigned int to_read_pmus =3D 0; =20 if (!strcmp(name, "default_core")) - return perf_pmus__find_core_pmu(); + return perf_pmus__find_core_pmu(pmus); /* * Once PMU is loaded it stays in the list, * so we keep us from multiple reading/parsing @@ -239,11 +239,6 @@ static struct perf_pmu *__perf_pmus__find(struct perf_= pmus *pmus, const char *na return pmu_find(pmus, name); } =20 -struct perf_pmu *perf_pmus__find(const char *name) -{ - return __perf_pmus__find(perf_env__pmus(host_env()), name); -} - static struct perf_pmu *perf_pmu__find2(struct perf_pmus *pmus, int dirfd,= const char *name) { struct perf_pmu *pmu; @@ -363,7 +358,7 @@ static struct perf_pmu *__perf_pmus__find_by_type(struc= t perf_pmus *pmus, unsign return NULL; } =20 -static struct perf_pmu *pmu_find_by_type(struct perf_pmus *pmus, unsigned = int type) +struct perf_pmu *perf_pmus__find_by_type(struct perf_pmus *pmus, unsigned = int type) { unsigned int to_read_pmus; struct perf_pmu *pmu =3D __perf_pmus__find_by_type(pmus, type); @@ -386,11 +381,6 @@ static struct perf_pmu *pmu_find_by_type(struct perf_p= mus *pmus, unsigned int ty return pmu; } =20 -struct perf_pmu *perf_pmus__find_by_type(unsigned int type) -{ - return pmu_find_by_type(perf_env__pmus(host_env()), type); -} - /* * pmu iterator: If pmu is NULL, we start at the begin, otherwise return t= he * next pmu. Returns NULL on end. @@ -875,7 +865,7 @@ void perf_pmus__print_raw_pmu_events(struct perf_pmus *= pmus, =20 bool perf_pmus__have_event(struct perf_pmus *pmus, const char *pname, cons= t char *name) { - struct perf_pmu *pmu =3D __perf_pmus__find(pmus, pname); + struct perf_pmu *pmu =3D perf_pmus__find(pmus, pname); =20 return pmu && perf_pmu__have_event(pmu, name); } @@ -938,14 +928,14 @@ bool perf_pmus__host_supports_extended_type(void) struct perf_pmu *perf_pmus__find_by_attr(struct perf_pmus *pmus, const struct perf_event_attr *attr) { - struct perf_pmu *pmu =3D pmu_find_by_type(pmus, attr->type); + struct perf_pmu *pmu =3D perf_pmus__find_by_type(pmus, attr->type); u32 type =3D attr->type; bool legacy_core_type =3D type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D P= ERF_TYPE_HW_CACHE; =20 if (!pmu && legacy_core_type && perf_pmus__supports_extended_type(pmus)) { type =3D attr->config >> PERF_PMU_TYPE_SHIFT; =20 - pmu =3D pmu_find_by_type(pmus, type); + pmu =3D perf_pmus__find_by_type(pmus, type); } if (!pmu && (legacy_core_type || type =3D=3D PERF_TYPE_RAW)) { /* @@ -956,7 +946,7 @@ struct perf_pmu *perf_pmus__find_by_attr(struct perf_pm= us *pmus, * PERF_TYPE_RAW, assume the RAW events are going to be handled * by the first core PMU. */ - pmu =3D pmu_scan_core(pmus, NULL); + pmu =3D perf_pmus__find_core_pmu(pmus); } return pmu; } @@ -975,9 +965,9 @@ struct perf_pmu *evsel__find_pmu(const struct evsel *ev= sel) return pmu; } =20 -struct perf_pmu *perf_pmus__find_core_pmu(void) +struct perf_pmu *perf_pmus__find_core_pmu(struct perf_pmus *pmus) { - return perf_pmus__scan_core(NULL); + return pmu_scan_core(pmus, NULL); } =20 struct perf_pmu *perf_pmus__add_test_pmu(struct perf_pmus *pmus, diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index 45f4bd8b6aa6..cd0c14f8e04b 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -75,8 +75,23 @@ void perf_pmus__init(struct perf_pmus *pmus); */ void perf_pmus__exit(struct perf_pmus *pmus); =20 -struct perf_pmu *perf_pmus__find(const char *name); -struct perf_pmu *perf_pmus__find_by_type(unsigned int type); +/** + * perf_pmus__find() - Find a PMU by name in @pmus. + * @pmus: The PMU collection to search. + * @name: Name of the PMU to look up. + * + * Return: Pointer to the matching struct perf_pmu, or NULL if not found. + */ +struct perf_pmu *perf_pmus__find(struct perf_pmus *pmus, const char *name); + +/** + * perf_pmus__find_by_type() - Find a PMU by its perf_event_attr type ID. + * @pmus: The PMU collection to search. + * @type: The PMU type identifier (e.g. PERF_TYPE_RAW or dynamic sysfs typ= e). + * + * Return: Pointer to the matching struct perf_pmu, or NULL if not found. + */ +struct perf_pmu *perf_pmus__find_by_type(struct perf_pmus *pmus, unsigned = int type); =20 /** * perf_pmus__find_by_attr() - Find the PMU corresponding to @attr. @@ -217,6 +232,12 @@ struct perf_pmu *perf_pmus__add_test_hwmon_pmu(struct = perf_pmus *pmus, * Return: Pointer to the fake struct perf_pmu. */ struct perf_pmu *perf_pmus__fake_pmu(void); -struct perf_pmu *perf_pmus__find_core_pmu(void); +/** + * perf_pmus__find_core_pmu() - Return the first core PMU in @pmus. + * @pmus: The PMU collection to search. + * + * Return: Pointer to the first core struct perf_pmu, or NULL if none exis= ts. + */ +struct perf_pmu *perf_pmus__find_core_pmu(struct perf_pmus *pmus); =20 #endif /* __PMUS_H */ diff --git a/tools/perf/util/s390-sample-raw.c b/tools/perf/util/s390-sampl= e-raw.c index aaf8838b2cb2..650dfcba794a 100644 --- a/tools/perf/util/s390-sample-raw.c +++ b/tools/perf/util/s390-sample-raw.c @@ -24,6 +24,7 @@ =20 #include "color.h" #include "debug.h" +#include "env.h" #include "evlist.h" #include "hashmap.h" #include "pmu.h" @@ -352,8 +353,11 @@ void evlist__s390_sample_raw(struct evlist *evlist, un= ion perf_event *event, =20 /* Display raw data on screen */ if (sample->evsel->core.attr.config =3D=3D PERF_EVENT_CPUM_CF_DIAG) { - if (!sample->evsel->pmu) - sample->evsel->pmu =3D perf_pmus__find("cpum_cf"); + if (!sample->evsel->pmu) { + struct perf_env *env =3D evsel__env(sample->evsel) ?: host_env(); + + sample->evsel->pmu =3D perf_pmus__find(perf_env__pmus(env), "cpum_cf"); + } if (!s390_cpumcfdg_testctr(sample)) pr_err("Invalid counter set data encountered\n"); else @@ -375,8 +379,12 @@ void evlist__s390_sample_raw(struct evlist *evlist, un= ion perf_event *event, if (!s390_pai_all_test(sample)) { pr_err("Invalid %s raw data encountered\n", pai_name); } else { - if (!sample->evsel->pmu) - sample->evsel->pmu =3D perf_pmus__find_by_type(sample->evsel->core.attr= .type); + if (!sample->evsel->pmu) { + struct perf_env *env =3D evsel__env(sample->evsel) ?: host_env(); + + sample->evsel->pmu =3D perf_pmus__find_by_type(perf_env__pmus(env), + sample->evsel->core.attr.type); + } s390_pai_all_dump(sample); } } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212877; b=bwrfa4UBWrjc2AQutP6XcoPl7gKhUNzZMAfAYozk3fZsv9mIQ7atJuV3E6Xs97xqfCv8 GScXUU/d5rHPmwLXdR+M8x0D+QVLXWiOIFPq1NhLUy9NuTe67NOxC02l74dPrW2mqdVsa TDGYbgLDsTDGhf+YV8L31W9CouAs7/wZ5+r2/oL+NTkdtuJkWZdbH/EXhzJrkygS2UlxR ERNjq9qr26S8om1k2DFuU+pWjDLfET1tlSZ1okvh6odW7yHX8kxeQe8PeaTG+acCmi6oY /YPqzKHjpx70o3NVSuhv8xwNeTDyr5RgFNX8pWms+coq2PBCgp5YW/EH+jay9bntGRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212877; 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=B9WPOTPjhDL+M80CZ64TPj/bjH8xsPtM1MlBXOc0jCw=; b=2a9EhSJRHdM5d1InOMhh3OcjuHyqHh8uR4rNDvgRmLL8GyUem9ZcVh+uut+bY3RwSfNe v37ID+9nXMyCRcmOJE163yq0gLDiMs8tEzidd0TWyAQO22y853AuLdkOYRyoybgbMQ92Q pHCIIPa78HwRVsisNFU6djsOf0RVXMRr5NrkbjUzzptylz/umvHRTcc4Y/8693rN19H3q UIEsEHa13AI0JbVX0riwwrEEtBG9/tmbjakVNizC0vtc3SXsRJHrPb4KBbuNFdfK6GC0u RVwLy/cUgflGFJsu9PRlQF1+tnfIyNtCrrwpoJrWBWEvc06xPRYM8cyFswcC0wZx1Ag== 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=i9SESWcr; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (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 25011606A69C for ; Fri, 20 Nov 2026 22:14:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25011606A69C 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=i9SESWcr Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b3a348a6dbso2598650c88.1 for ; Fri, 20 Nov 2026 14:14:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212866; x=1795817666; 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=B9WPOTPjhDL+M80CZ64TPj/bjH8xsPtM1MlBXOc0jCw=; b=i9SESWcr72eV5x28DK+ndFDUrzt4wPwK+zWLPvVAunWaB1hJ5EMGET/r7tnhzmN6d0 k6ZwwZ2vZFYtWk7Y7GH7lXZCxhF/cPWo9molb+3F1rxt7tHjXNmRbqPnnyoHgyX9w5Ik ov9K434WnDBzteuZItvn7YMyyqnIr+vaUCJjER6yB8vjsJWvEHVPgioT4c3X+s5GDl8v IdZ31P8sPvs2SpjbleVF9x1velCJ+1JDcx927jTHJbJNXbVv+foeivtldl9UtXmje0/R ypI64GZe6t5ZRiZPE4RlfxnIKluTIxSvUG8XJaMfn063SKRRapft9imYVWOZes3Mq0or oVvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212866; x=1795817666; 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=B9WPOTPjhDL+M80CZ64TPj/bjH8xsPtM1MlBXOc0jCw=; b=lQoIknHUeXSThF+uciUlDu8kpuBdMWFOdJe9LrCs08xgaKeiNnL0x5KzTz+H1uLBvZ qRNAw6jmQyCFWat2pssmdsVzkuvkdFrOec2ug+UANjtiSOZ2I6hKwsK4wQRSuFjwYSH6 JHrrqsDJFALiutadxfz+CNgBkVHrwCkxP7zCU1JhVeAxnbS9WBfbYPGAish+Lv11j+42 vaLWoP8ZK44kV0j3M4xSwpg+3pYvBcODZXHazRAbSQDtz9HaXGgh426imF4tRBzuiXyP b5yZ9mQJwDP1cY+ijVHLhz4ptZYrHCeHAkll5GJstwzde6uOtUXCDL4G0LxRfLwtTZY6 p9yg== X-Forwarded-Encrypted: i=1; AL+hErFwxQptuM5Cp1ErxO58yKvSH9aE2h10UekxP/n5zRUr5iFajOrdEHp39Wy1BANAo6a7zWjYm9/V7ThvkIk=@vger.kernel.org X-Gm-Message-State: APuU4Z97lJbLdXbNGAd6CZKryoFJdLZs4kvwel8lAqMxS+3Kycenn+4i OS7DI1AS80FmgFEgw6VO3NjxxF3l8DeOlh49uQR9yaTmX6Mwmx9snx7AAF/DYyLXa2A2jLysJXM JP39d6lTqCQ== X-Received: from dly1-n1.prod.google.com ([2002:a05:701b:2041:10b0:1b5:43fc:8107]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4502:10b0:1b2:539b:496c with SMTP id a92af1059eb24-1b5569050c8mr4719713c88.35.1795212865812; Fri, 20 Nov 2026 14:14:25 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:11 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-11-irogers@google.com> Subject: [PATCH v3 10/30] perf pmus: Pass struct perf_pmus to scan helpers 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update perf_pmus__scan() and perf_pmus__scan_core() to take a struct perf_pmus * argument and update callers to pass the appropriate environment's PMUs. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/topdown.c | 3 +- tools/perf/bench/pmu-scan.c | 6 ++-- tools/perf/tests/event_groups.c | 4 ++- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/parse-events.c | 9 +++-- tools/perf/tests/pmu-events.c | 4 ++- tools/perf/tests/uncore-event-sorting.c | 6 ++-- tools/perf/util/cputopo.c | 2 +- tools/perf/util/env.c | 7 ++-- tools/perf/util/evlist.c | 3 +- tools/perf/util/header.c | 10 +++--- tools/perf/util/mem-events.c | 5 ++- tools/perf/util/metricgroup.c | 2 +- tools/perf/util/parse-events.c | 28 ++++++++-------- .../perf/util/perf-regs-arch/perf_regs_x86.c | 2 +- tools/perf/util/perf_api_probe.c | 3 +- tools/perf/util/pmu.c | 4 ++- tools/perf/util/pmus.c | 33 +++++++------------ tools/perf/util/pmus.h | 19 +++++++++-- tools/perf/util/python.c | 4 +-- 20 files changed, 92 insertions(+), 64 deletions(-) diff --git a/tools/perf/arch/x86/tests/topdown.c b/tools/perf/arch/x86/test= s/topdown.c index 827169b60813..fec51274e76e 100644 --- a/tools/perf/arch/x86/tests/topdown.c +++ b/tools/perf/arch/x86/tests/topdown.c @@ -65,13 +65,14 @@ static int event_cb(void *state, struct pmu_event_info = *info) =20 static int test__x86_topdown(struct test_suite *test __maybe_unused, int s= ubtest __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); int ret =3D TEST_OK; struct perf_pmu *pmu =3D NULL; =20 if (!topdown_sys_has_perf_metrics()) return TEST_OK; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=3D*/false, &ret,= event_cb)) break; } diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c index 754486d1d87b..fe58fbfcf96d 100644 --- a/tools/perf/bench/pmu-scan.c +++ b/tools/perf/bench/pmu-scan.c @@ -48,7 +48,7 @@ static int save_result(void) struct list_head *list; struct pmu_scan_result *r; =20 - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { r =3D realloc(results, (nr_pmus + 1) * sizeof(*r)); if (r =3D=3D NULL) return -ENOMEM; @@ -153,9 +153,9 @@ static int run_pmu_scan(void) for (unsigned int i =3D 0; i < iterations; i++) { gettimeofday(&start, NULL); if (core_only) - perf_pmus__scan_core(NULL); + perf_pmus__scan_core(pmus, NULL); else - perf_pmus__scan(NULL); + perf_pmus__scan(pmus, NULL); gettimeofday(&end, NULL); timersub(&end, &start, &diff); runtime_us =3D diff.tv_sec * USEC_PER_SEC + diff.tv_usec; diff --git a/tools/perf/tests/event_groups.c b/tools/perf/tests/event_group= s.c index c119ff114948..f52d2fb08820 100644 --- a/tools/perf/tests/event_groups.c +++ b/tools/perf/tests/event_groups.c @@ -5,6 +5,7 @@ #include "linux/perf_event.h" #include "tests.h" #include "debug.h" +#include "env.h" #include "pmu.h" #include "pmus.h" #include "header.h" @@ -51,10 +52,11 @@ static int event_open(int type, unsigned long config, i= nt group_fd) =20 static int setup_uncore_event(void) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; int i, fd; =20 - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { for (i =3D 0; i < NR_UNCORE_PMUS; i++) { if (!strcmp(uncore_pmus[i].name, pmu->name)) { pr_debug("Using %s for uncore pmu event\n", pmu->name); diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index dada4af5fb68..a542aadb46a0 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -277,7 +277,7 @@ static int test_stat_user_read(u64 event, enum user_rea= d_state enabled) } perf_thread_map__set_pid(threads, 0, 0); =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { enum user_read_state saved_user_read_state =3D set_user_read(pmu, enable= d); struct perf_event_attr attr =3D { .type =3D PERF_TYPE_HARDWARE, diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-event= s.c index 4b7551391b40..bcd831abdd90 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c @@ -142,6 +142,7 @@ static int test__checkevent_tracepoint_multi(struct evl= ist *evlist) =20 static int test__checkevent_raw(struct evlist *evlist) { + struct perf_pmus *pmus __maybe_unused =3D perf_env__pmus(host_env()); struct evsel *evsel; bool raw_type_match =3D false; =20 @@ -164,7 +165,7 @@ static int test__checkevent_raw(struct evlist *evlist) */ type_matched =3D raw_type_match =3D true; #else - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { if (pmu->type =3D=3D evsel->core.attr.type) { TEST_ASSERT_EVSEL("PMU type expected once", !type_matched, evsel); type_matched =3D true; @@ -1800,9 +1801,10 @@ static int test__intel_pt(struct evlist *evlist) =20 static bool test__acr_valid(void) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (perf_pmu__has_format(pmu, "acr_mask")) return true; } @@ -2711,10 +2713,11 @@ static int test__terms2(struct test_suite *test __m= aybe_unused, int subtest __ma =20 static int test__pmu_events(struct test_suite *test __maybe_unused, int su= btest __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; int ret =3D TEST_OK; =20 - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { struct stat st; char path[PATH_MAX]; char pmu_event[PATH_MAX]; diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index acf8e8ee1237..cb0c2b05c1e8 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -9,6 +9,7 @@ #include #include #include "debug.h" +#include "env.h" #include "../pmu-events/pmu-events.h" #include #include "util/evlist.h" @@ -726,10 +727,11 @@ static struct perf_pmu_test_pmu test_pmus[] =3D { static int test__aliases(struct test_suite *test __maybe_unused, int subtest __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; unsigned long i; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { int count =3D 0; =20 if (list_empty(&pmu->format)) { diff --git a/tools/perf/tests/uncore-event-sorting.c b/tools/perf/tests/unc= ore-event-sorting.c index 7756777c54c2..99af4d88bb81 100644 --- a/tools/perf/tests/uncore-event-sorting.c +++ b/tools/perf/tests/uncore-event-sorting.c @@ -2,6 +2,7 @@ #include =20 #include "debug.h" +#include "env.h" #include "evlist.h" #include "parse-events.h" #include "pmu.h" @@ -84,6 +85,7 @@ static int test__uncore_event_sorting(struct test_suite *= test __maybe_unused, struct evlist *evlist =3D NULL; struct parse_events_error err; struct evsel *evsel; + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; char *pmu_prefix =3D NULL; struct match_state m =3D { NULL, NULL }; @@ -92,7 +94,7 @@ static int test__uncore_event_sorting(struct test_suite *= test __maybe_unused, =20 parse_events_error__init(&err); =20 - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { size_t len; struct perf_pmu *sibling; =20 @@ -104,7 +106,7 @@ static int test__uncore_event_sorting(struct test_suite= *test __maybe_unused, continue; =20 sibling =3D pmu; - while ((sibling =3D perf_pmus__scan(sibling)) !=3D NULL) { + while ((sibling =3D perf_pmus__scan(pmus, sibling)) !=3D NULL) { if (sibling->is_core) continue; if (pmu_name_len_no_suffix(sibling->name) =3D=3D len && diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c index 04ab4d010149..2e8e8754bb3f 100644 --- a/tools/perf/util/cputopo.c +++ b/tools/perf/util/cputopo.c @@ -483,7 +483,7 @@ struct hybrid_topology *hybrid_topology__new(void) return NULL; =20 tp->nr =3D nr; - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (load_hybrid_node(&tp->nodes[i], pmu)) { hybrid_topology__delete(tp); return NULL; diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 4f4ec49b6a6b..fec6a5a8ecb6 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -499,11 +499,12 @@ int perf_env__read_cpu_topology_map(struct perf_env *= env) =20 int perf_env__read_pmu_mappings(struct perf_env *env) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; u32 pmu_num =3D 0; struct strbuf sb; =20 - while ((pmu =3D perf_pmus__scan(pmu))) + while ((pmu =3D perf_pmus__scan(pmus, pmu))) pmu_num++; =20 if (!pmu_num) { @@ -514,7 +515,7 @@ int perf_env__read_pmu_mappings(struct perf_env *env) if (strbuf_init(&sb, 128 * pmu_num) < 0) return -ENOMEM; =20 - while ((pmu =3D perf_pmus__scan(pmu))) { + while ((pmu =3D perf_pmus__scan(pmus, pmu))) { if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0) goto error; /* include a NULL character at the end */ @@ -633,7 +634,7 @@ int perf_env__read_core_pmu_caps(struct perf_env *env) if (!pmu_caps) return -ENOMEM; =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (perf_pmu__caps_parse(pmu) <=3D 0) continue; ret =3D __perf_env__read_core_pmu_caps(pmu, &pmu_caps[i].nr_caps, diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 4e641ccc176f..faf5ce62811b 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -128,10 +128,11 @@ struct evlist *evlist__new_default(const struct targe= t *target, bool sample_call if (err) goto out_err; } else { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct evsel *leader =3D NULL; =20 /* Fallback for cross-platform file analysis missing the topology header= */ - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { snprintf(buf, sizeof(buf), "%s/cycles/%s", pmu->name, can_profile_kernel ? "P" : "Pu"); err =3D parse_event(evlist, buf); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 16e49baa9d5e..4d61451020f3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -852,6 +852,7 @@ static int write_numa_topology(struct feat_fd *ff, static int write_pmu_mappings(struct feat_fd *ff, struct evlist *evlist __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; u32 pmu_num =3D 0; int ret; @@ -860,14 +861,14 @@ static int write_pmu_mappings(struct feat_fd *ff, * Do a first pass to count number of pmu to avoid lseek so this * works in pipe mode as well. */ - while ((pmu =3D perf_pmus__scan(pmu))) + while ((pmu =3D perf_pmus__scan(pmus, pmu))) pmu_num++; =20 ret =3D do_write(ff, &pmu_num, sizeof(pmu_num)); if (ret < 0) return ret; =20 - while ((pmu =3D perf_pmus__scan(pmu))) { + while ((pmu =3D perf_pmus__scan(pmus, pmu))) { ret =3D do_write(ff, &pmu->type, sizeof(pmu->type)); if (ret < 0) return ret; @@ -1717,11 +1718,12 @@ static int write_cpu_pmu_caps(struct feat_fd *ff, static int write_pmu_caps(struct feat_fd *ff, struct evlist *evlist __maybe_unused) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct perf_pmu *pmu =3D NULL; int nr_pmu =3D 0; int ret; =20 - while ((pmu =3D perf_pmus__scan(pmu))) { + while ((pmu =3D perf_pmus__scan(pmus, pmu))) { if (!strcmp(pmu->name, "cpu")) { /* * The "cpu" PMU is special and covered by @@ -1747,7 +1749,7 @@ static int write_pmu_caps(struct feat_fd *ff, * of perf_pmus__scan. */ pmu =3D NULL; - while ((pmu =3D perf_pmus__scan(pmu))) { + while ((pmu =3D perf_pmus__scan(pmus, pmu))) { if (!strcmp(pmu->name, "cpu")) { /* Skip as above. */ continue; diff --git a/tools/perf/util/mem-events.c b/tools/perf/util/mem-events.c index 0cdcb47647bd..62543df0dca1 100644 --- a/tools/perf/util/mem-events.c +++ b/tools/perf/util/mem-events.c @@ -13,6 +13,7 @@ #include "mem-events.h" #include "mem-info.h" #include "debug.h" +#include "env.h" #include "evsel.h" #include "symbol.h" #include "pmu.h" @@ -42,7 +43,9 @@ struct perf_mem_event *perf_pmu__mem_events_ptr(struct pe= rf_pmu *pmu, int i) =20 static struct perf_pmu *perf_pmus__scan_mem(struct perf_pmu *pmu) { - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { + struct perf_pmus *pmus =3D perf_env__pmus(host_env()); + + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { if (pmu->mem_events) return pmu; } diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 593032197edd..80c2086555a8 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -934,7 +934,7 @@ static int __add_metric(struct list_head *metric_list, if (pm->pmu && pm->pmu[0] !=3D '\0') pmu =3D perf_pmus__find(pmus, pm->pmu); else - pmu =3D perf_pmus__scan_core(/*pmu=3D*/ NULL); + pmu =3D perf_pmus__scan_core(pmus, /*pmu=3D*/NULL); =20 ret =3D resolve_metric(metric_list, pmus, pmu, modifier, metric_no_group, metric_no_threshold, user_requested_cpu_list, diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index f38c83b24e6a..0044b6c27ea6 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1008,16 +1008,16 @@ do { \ } =20 static bool check_pmu_is_core(__u32 type, const struct parse_events_term *= term, - struct parse_events_error *err) + struct parse_events_state *parse_state) { struct perf_pmu *pmu =3D NULL; =20 /* Avoid loading all PMUs with perf_pmus__find_by_type, just scan the cor= e ones. */ - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(parse_state->pmus, pmu)) !=3D NULL) { if (pmu->type =3D=3D type) return true; } - parse_events_error__handle(err, term->err_val, + parse_events_error__handle(parse_state->error, term->err_val, strdup("needs a core PMU"), NULL); return false; @@ -1036,7 +1036,7 @@ static int config_term_pmu(struct perf_event_attr *at= tr, NULL); return -EINVAL; } - if (!check_pmu_is_core(attr->type, term, parse_state->error)) + if (!check_pmu_is_core(attr->type, term, parse_state)) return -EINVAL; attr->config =3D term->val.num; if (perf_pmus__supports_extended_type(parse_state->pmus)) @@ -1061,7 +1061,7 @@ static int config_term_pmu(struct perf_event_attr *at= tr, NULL); return -EINVAL; } - if (!check_pmu_is_core(attr->type, term, parse_state->error)) + if (!check_pmu_is_core(attr->type, term, parse_state)) return -EINVAL; attr->config =3D term->val.num; if (perf_pmus__supports_extended_type(parse_state->pmus)) @@ -1475,7 +1475,7 @@ int parse_events_add_numeric(struct parse_events_stat= e *parse_state, /* Wildcards on numeric values are only supported by core PMUs. */ if (wildcard && perf_pmus__supports_extended_type(parse_state->pmus)) { struct evsel *first_wildcard_match =3D NULL; - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(parse_state->pmus, pmu)) !=3D NULL)= { int ret; =20 found_supported =3D true; @@ -2009,7 +2009,8 @@ int parse_events_terms(struct parse_events_terms *ter= ms, const char *str) } =20 static int evsel__compute_group_pmu_name(struct evsel *evsel, - const struct list_head *head) + const struct list_head *head, + struct perf_pmus *pmus) { struct evsel *leader =3D evsel__leader(evsel); struct evsel *pos; @@ -2022,7 +2023,7 @@ static int evsel__compute_group_pmu_name(struct evsel= *evsel, * is a core PMU, but in heterogeneous systems this is * unknown. For now pick the first core PMU. */ - pmu =3D perf_pmus__scan_core(NULL); + pmu =3D perf_pmus__scan_core(pmus, NULL); } if (!pmu) { pr_debug("No PMU found for '%s'\n", evsel__name(evsel)); @@ -2042,7 +2043,7 @@ static int evsel__compute_group_pmu_name(struct evsel= *evsel, =20 if (!leader_pmu) { /* As with determining pmu above. */ - leader_pmu =3D perf_pmus__scan_core(NULL); + leader_pmu =3D perf_pmus__scan_core(pmus, NULL); } /* * Starting with the leader, find the first event with a named @@ -2061,7 +2062,7 @@ static int evsel__compute_group_pmu_name(struct evsel= *evsel, pos_pmu =3D evsel__find_pmu(pos); if (!pos_pmu) { /* As with determining pmu above. */ - pos_pmu =3D perf_pmus__scan_core(NULL); + pos_pmu =3D perf_pmus__scan_core(pmus, NULL); } if (pos_pmu && !perf_pmu__is_software(pos_pmu)) { group_pmu_name =3D pos_pmu->name; @@ -2151,7 +2152,8 @@ int __weak arch_evlist__add_required_events(struct li= st_head *list __always_unus return 0; } =20 -static int parse_events__sort_events_and_fix_groups(struct list_head *list) +static int parse_events__sort_events_and_fix_groups(struct list_head *list, + struct perf_pmus *pmus) { int idx =3D 0, force_grouped_idx =3D -1; struct evsel *pos, *cur_leader =3D NULL; @@ -2174,7 +2176,7 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) list_for_each_entry(pos, list, core.node) { const struct evsel *pos_leader =3D evsel__leader(pos); =20 - ret =3D evsel__compute_group_pmu_name(pos, list); + ret =3D evsel__compute_group_pmu_name(pos, list, pmus); if (ret) return ret; =20 @@ -2345,7 +2347,7 @@ int __parse_events(struct evlist *evlist, const char = *str, return -1; } =20 - ret2 =3D parse_events__sort_events_and_fix_groups(&parse_state.list); + ret2 =3D parse_events__sort_events_and_fix_groups(&parse_state.list, pars= e_state.pmus); if (ret2 < 0 && !ret) ret =3D ret2; =20 diff --git a/tools/perf/util/perf-regs-arch/perf_regs_x86.c b/tools/perf/ut= il/perf-regs-arch/perf_regs_x86.c index 920f17b89659..8cb9dfa6c7d3 100644 --- a/tools/perf/util/perf-regs-arch/perf_regs_x86.c +++ b/tools/perf/util/perf-regs-arch/perf_regs_x86.c @@ -268,7 +268,7 @@ static uint64_t __arch__reg_mask(u64 sample_type, u64 m= ask, bool has_simd_regs) * The same register set is supported among different hybrid PMUs. * Only check the first available one. */ - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { type =3D pmu->type; break; } diff --git a/tools/perf/util/perf_api_probe.c b/tools/perf/util/perf_api_pr= obe.c index f61c4ec52827..7a8b99f31985 100644 --- a/tools/perf/util/perf_api_probe.c +++ b/tools/perf/util/perf_api_probe.c @@ -6,6 +6,7 @@ #include =20 #include "cloexec.h" +#include "env.h" #include "evlist.h" #include "evsel.h" #include "parse-events.h" @@ -78,7 +79,7 @@ static bool perf_probe_api(setup_probe_fn_t fn) if (!ret) return true; =20 - pmu =3D perf_pmus__scan_core(/*pmu=3D*/NULL); + pmu =3D perf_pmus__scan_core(perf_env__pmus(host_env()), /*pmu=3D*/NULL); if (pmu) { const char *try[] =3D {"cycles", "instructions", NULL}; char buf[256]; diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 388070e670b6..fc4fa34fcb0d 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -2080,6 +2080,7 @@ bool is_pmu_core(const char *name) */ bool is_pmu_core_len(struct perf_env *env, const char *name, size_t len) { + struct perf_pmus *pmus; struct perf_pmu *pmu =3D NULL; =20 if (env && env->hybrid_nodes) { @@ -2106,7 +2107,8 @@ bool is_pmu_core_len(struct perf_env *env, const char= *name, size_t len) return false; } =20 - while ((pmu =3D perf_pmus__scan_core(pmu)) !=3D NULL) { + pmus =3D perf_env__pmus(env ?: host_env()); + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (strlen(pmu->name) =3D=3D len && !strncmp(name, pmu->name, len)) return true; } diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 0cfdfc4a8a99..1c3cb53c3d4f 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -385,7 +385,7 @@ struct perf_pmu *perf_pmus__find_by_type(struct perf_pm= us *pmus, unsigned int ty * pmu iterator: If pmu is NULL, we start at the begin, otherwise return t= he * next pmu. Returns NULL on end. */ -static struct perf_pmu *pmu_scan(struct perf_pmus *pmus, struct perf_pmu *= pmu) +struct perf_pmu *perf_pmus__scan(struct perf_pmus *pmus, struct perf_pmu *= pmu) { bool use_core_pmus =3D !pmu || pmu->is_core; =20 @@ -405,12 +405,7 @@ static struct perf_pmu *pmu_scan(struct perf_pmus *pmu= s, struct perf_pmu *pmu) return NULL; } =20 -struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) -{ - return pmu_scan(perf_env__pmus(host_env()), pmu); -} - -static struct perf_pmu *pmu_scan_core(struct perf_pmus *pmus, struct perf_= pmu *pmu) +struct perf_pmu *perf_pmus__scan_core(struct perf_pmus *pmus, struct perf_= pmu *pmu) { if (!pmu) { pmu_read_sysfs(pmus, PERF_TOOL_PMU_TYPE_PE_CORE_MASK); @@ -422,11 +417,6 @@ static struct perf_pmu *pmu_scan_core(struct perf_pmus= *pmus, struct perf_pmu *p return NULL; } =20 -struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) -{ - return pmu_scan_core(perf_env__pmus(host_env()), pmu); -} - struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmus *pmus, struct perf_pmu *pmu, const char *event) { @@ -515,7 +505,8 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(stru= ct perf_pmus *pmus, return NULL; } =20 -static struct perf_pmu *pmu_scan_skip_duplicates(struct perf_pmus *pmus, s= truct perf_pmu *pmu) +static struct perf_pmu * +perf_pmus__scan_skip_duplicates(struct perf_pmus *pmus, struct perf_pmu *p= mu) { bool use_core_pmus =3D !pmu || pmu->is_core; int last_pmu_name_len =3D 0; @@ -572,7 +563,7 @@ const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(st= ruct perf_pmus *pmus, con { struct perf_pmu *pmu =3D NULL; =20 - while ((pmu =3D pmu_scan(pmus, pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan(pmus, pmu)) !=3D NULL) { if (!strcmp(pmu->name, str)) return pmu; /* Ignore "uncore_" prefix. */ @@ -705,9 +696,9 @@ void perf_pmus__print_pmu_events(struct perf_pmus *pmus, struct perf_pmu *(*scan_fn)(struct perf_pmus *p, struct perf_pmu *cur); =20 if (skip_duplicate_pmus) - scan_fn =3D pmu_scan_skip_duplicates; + scan_fn =3D perf_pmus__scan_skip_duplicates; else - scan_fn =3D pmu_scan; + scan_fn =3D perf_pmus__scan; =20 pmu =3D NULL; len =3D 0; @@ -817,9 +808,9 @@ void perf_pmus__print_raw_pmu_events(struct perf_pmus *= pmus, struct perf_pmu *pmu =3D NULL; =20 if (skip_duplicate_pmus) - scan_fn =3D pmu_scan_skip_duplicates; + scan_fn =3D perf_pmus__scan_skip_duplicates; else - scan_fn =3D pmu_scan; + scan_fn =3D perf_pmus__scan; =20 while ((pmu =3D scan_fn(pmus, pmu)) !=3D NULL) { struct build_format_string_args format_args =3D { @@ -875,7 +866,7 @@ int perf_pmus__num_core_pmus(struct perf_pmus *pmus) if (!pmus->num_core_pmus) { struct perf_pmu *pmu =3D NULL; =20 - while ((pmu =3D pmu_scan_core(pmus, pmu)) !=3D NULL) + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) pmus->num_core_pmus++; } return pmus->num_core_pmus; @@ -888,7 +879,7 @@ static bool __perf_pmus__supports_extended_type(struct = perf_pmus *pmus) if (perf_pmus__num_core_pmus(pmus) <=3D 1) return false; =20 - while ((pmu =3D pmu_scan_core(pmus, pmu)) !=3D NULL) { + while ((pmu =3D perf_pmus__scan_core(pmus, pmu)) !=3D NULL) { if (!pmu->from_env && !is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | @@ -967,7 +958,7 @@ struct perf_pmu *evsel__find_pmu(const struct evsel *ev= sel) =20 struct perf_pmu *perf_pmus__find_core_pmu(struct perf_pmus *pmus) { - return pmu_scan_core(pmus, NULL); + return perf_pmus__scan_core(pmus, NULL); } =20 struct perf_pmu *perf_pmus__add_test_pmu(struct perf_pmus *pmus, diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index cd0c14f8e04b..6e7c9f857aaf 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -103,8 +103,23 @@ struct perf_pmu *perf_pmus__find_by_type(struct perf_p= mus *pmus, unsigned int ty struct perf_pmu *perf_pmus__find_by_attr(struct perf_pmus *pmus, const struct perf_event_attr *attr); =20 -struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu); -struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu); +/** + * perf_pmus__scan() - Iterate over all PMUs (core then other) in @pmus. + * @pmus: The PMU collection to scan. + * @pmu: Previous PMU returned by perf_pmus__scan(), or NULL to start. + * + * Return: Next struct perf_pmu in @pmus, or NULL when iteration completes. + */ +struct perf_pmu *perf_pmus__scan(struct perf_pmus *pmus, struct perf_pmu *= pmu); + +/** + * perf_pmus__scan_core() - Iterate over core PMUs in @pmus. + * @pmus: The PMU collection to scan. + * @pmu: Previous core PMU returned by perf_pmus__scan_core(), or NULL to = start. + * + * Return: Next core struct perf_pmu in @pmus, or NULL when iteration comp= letes. + */ +struct perf_pmu *perf_pmus__scan_core(struct perf_pmus *pmus, struct perf_= pmu *pmu); =20 /** * perf_pmus__scan_for_event() - Iterate over PMUs in @pmus that have @eve= nt. diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 18dfe21104f4..f1de03ee3e24 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -2160,7 +2160,7 @@ static PyObject *pyrf_pmu_iterator__new(PyTypeObject = *type, PyObject *args __may struct pyrf_pmu_iterator *itr =3D (void *)type->tp_alloc(type, 0); =20 if (itr !=3D NULL) - itr->pmu =3D perf_pmus__scan(/*pmu=3D*/NULL); + itr->pmu =3D perf_pmus__scan(perf_env__pmus(host_env()), /*pmu=3D*/NULL); =20 return (PyObject *) itr; } @@ -2185,7 +2185,7 @@ static PyObject *pyrf_pmu_iterator__iternext(PyObject= *self) if (ppmu) { ppmu->pmu =3D itr->pmu; // Advance iterator. - itr->pmu =3D perf_pmus__scan(itr->pmu); + itr->pmu =3D perf_pmus__scan(perf_env__pmus(host_env()), itr->pmu); } return (PyObject *)ppmu; } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212875; b=fm7YnzSOznc798U2QFGvamnHrYkvxCxAHy3a9zuyvakM2pur/DOzAKoVnkIL9hBlgVfE OQcIfwMcwo+prGHk0/F9i1WGi5m0W5zKTHtxxrsEed4uh7L8F2jZWOci0QRzOvOi/Vqph 8n2adziSPlCyknoH/bvALxmGpnaWxW6bg3I4ZULOLBOd0kYK1soU5VVwWF6qVvVpOYFgm zHgTz1r2pr7LkUkpRgvN48Eq9TtrgUV9ssUnxZMznEnMG1a0POESYCaziPyqzeHHx+Q/w P5FuG6/W3xmqr0h860f2XZlWqtMwticEAzCZBRA//VBeRhT6EZTn6OewAmjazouSrKg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212875; 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=id0a/mzGN50ic2k00/n8hpihWo/9QDdq+HMV733fdTc=; b=BjoCIyIosdbpbavxCpGZKEZeZImthXlpbIkeTBfc4vobFGDkO9G+JFoOUF1fwbK1TBvY JqPjX/dLyMnD1AG1QSYf8ikRzEzMX8sEqCMCOBp/LueDtaWEJCkiTEjpGxz/mXErGQrCU 8HI022c7c/NZpTHkasJ8d/o+zHuN6SoVWZx2f4na7w9wwcRj5x6LcEFVxGBn79OoyWpIn xanh3x+N3tNKXQJ4k2SmyfAyB8KDXo8iVQYrmkn0UGH8PJPbzJrok5G8bnaVDqv3UQ4w9 DSyj82xVy0jottrztz9ubkFIE4DaHOk5sd96VASk9XqbdVKEdAC/scE7sR5V+ZEQ0kQ== 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=NCE5mPUm; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 4BDA960A2607 for ; Fri, 20 Nov 2026 22:14:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4BDA960A2607 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=NCE5mPUm Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-383165e9b0fso1526356eec.0 for ; Fri, 20 Nov 2026 14:14:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212872; x=1795817672; 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=id0a/mzGN50ic2k00/n8hpihWo/9QDdq+HMV733fdTc=; b=NCE5mPUmYAlTk2Y3M+ju45dppk9LrZGfIbDJG3J0yTc+ZoXV15FqdhLChIOTGI3Cqa 86v1Wbaj8EVTsjkFkzcQ1/ax3cLaCUJUcWyP6vbhPjouTEnArXGXYB4oTH34sIx5HkNq 77vC9+ephkh3cdZkdtNYYP/R6a2O1+aQbA445p9dVRVYhInTvZbcW8Zps53BE8YIlmOD MNHxwzFVcPTjzuSqSQSMJKM4mZAM/CDMBd1dF6Y30Mp6f6459ybabB2cG2LQaoNYeB+w buLaZHMMarE6hSCDbVwxUN09CL7GWEnRbFvxUHPWo/10Vop4ZopJIkhQQwSxkr4L6keH 919Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212872; x=1795817672; 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=id0a/mzGN50ic2k00/n8hpihWo/9QDdq+HMV733fdTc=; b=Z2iYVWbsJ30Pofa4wYhaR25l9Kvrb1SMmSR9yTdyqbCi1UgWC6ILkoA8vAdhswhugW 2FfA+KCSAdpGy/JufQyX+UX8UprxoImtX72JfcrCS2OeO/mCAbZJXS+/XSmltDXs+/ZJ HxeV0o+jwKCv/wIsKwVecbOldKyzrzHnmFPXx6eFxyfXLRP52Jg+mMM6pwmOUv1hMSDT OHn64TAw2wptHgDa735BYL/2b4DJdLffS/KjL12O4YphuJi3LMjdMwWigpy4dk+TjN5P KLSXHy/WYvJFsKJMfe3CYmJEmNyXFY0IDHe7kChbiAHmsxOpY5lnYT26KpvEQkMafNQP E9/g== X-Forwarded-Encrypted: i=1; AL+hErEgycLRZZKkHGHoAKkm0wU07YVPDj0JGkGKD8TzjlZts+N8ICben7wevG3YdIcxiotdj7ape8Rtb4T1iWM=@vger.kernel.org X-Gm-Message-State: APuU4Z8GlguBozkUZuKyzARDQNf92kvUs7BP/vtLVUOraQLmKy/s25sT MAHpanjj9W+FOZdoc8AY35z3pX9N6YjwSPARgoTJ0LIVZH1JWVMebfH5kutaorFZyFH0rzlcIFG sblXB8hBxZw== X-Received: from dyee6.prod.google.com ([2002:a05:693c:6286:b0:386:6850:930a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:50fa:b0:384:6487:b155 with SMTP id 5a478bee46e88-38570c359d3mr4897009eec.29.1795212872032; Fri, 20 Nov 2026 14:14:32 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:12 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-12-irogers@google.com> Subject: [PATCH v3 11/30] perf pmu-events: Add e_machine to struct pmu_events_map 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The CPUID strings in the pmu-events mapfile.csv files have an architecture specific format, such as "GenuineIntel-6-55-[56789ABCDEF]" on x86 or a MIDR value like "0x00000000410fd0c0" on arm64. When perf is built with JEVENTS_ARCH=3Dall the tables of every architecture are present and map_for_cpu() compared the host's CPUID against all of them, even though only the host architecture's tables can match. Record the ELF machine of each architecture's CPUIDs in struct pmu_events_map, EM_NONE for the test and common tables, and have map_for_cpu() only consider tables for the host's ELF machine. The 32 and 64-bit variants of x86 and PowerPC share tables. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 24 +++++++++++++++- tools/perf/pmu-events/jevents.py | 35 +++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 7df1552403e7..7428ffd4d70c 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -4,6 +4,8 @@ #include #include "util/header.h" #include "util/pmu.h" +#include +#include #include #include =20 @@ -5419,6 +5421,8 @@ struct pmu_metrics_table { struct pmu_events_map { const char *arch; const char *cpuid; + /* The ELF machine of the architecture's CPUIDs, EM_NONE for test and com= mon. */ + uint16_t e_machine; struct pmu_events_table event_table; struct pmu_metrics_table metric_table; }; @@ -5431,6 +5435,7 @@ static const struct pmu_events_map pmu_events_map[] = =3D { { .arch =3D "common", .cpuid =3D "common", + .e_machine =3D EM_NONE, .event_table =3D { .pmus =3D pmu_events__common, .num_pmus =3D ARRAY_SIZE(pmu_events__common), @@ -5444,6 +5449,7 @@ static const struct pmu_events_map pmu_events_map[] = =3D { { .arch =3D "testarch", .cpuid =3D "testcpu", + .e_machine =3D EM_NONE, .event_table =3D { .pmus =3D pmu_events__test_soc_cpu, .num_pmus =3D ARRAY_SIZE(pmu_events__test_soc_cpu), @@ -5457,6 +5463,7 @@ static const struct pmu_events_map pmu_events_map[] = =3D { { .arch =3D 0, .cpuid =3D 0, + .e_machine =3D EM_NONE, .event_table =3D { 0, 0 }, .metric_table =3D { 0 }, } @@ -5769,6 +5776,20 @@ int pmu_metrics_table__find_metric(const struct pmu_= metrics_table *table, return PMU_METRICS__NOT_FOUND; } =20 +/* + * Is the map for the architecture of @e_machine? The 32 and 64-bit varian= ts of + * an architecture share tables. + */ +static bool pmu_events_map__e_machine_matches(const struct pmu_events_map = *map, + uint16_t e_machine) +{ + if (e_machine =3D=3D EM_386) + e_machine =3D EM_X86_64; + else if (e_machine =3D=3D EM_PPC) + e_machine =3D EM_PPC64; + return map->e_machine =3D=3D e_machine; +} + static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) { static struct { @@ -5809,7 +5830,8 @@ static const struct pmu_events_map *map_for_cpu(struc= t perf_cpu cpu) break; } =20 - if (!strcmp_cpuid_str(map->cpuid, cpuid)) + if (pmu_events_map__e_machine_matches(map, EM_HOST) && + !strcmp_cpuid_str(map->cpuid, cpuid)) break; } free(last_map_search.cpuid); diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 080e04b51b8c..db1b4749aec3 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -26,6 +26,15 @@ _sys_event_table_to_metric_table_mapping: Dict[str, str]= =3D {} # JsonEvent. Architecture standard events are in json files in the top # f'{_args.starting_dir}/{_args.arch}' directory. _arch_std_events: Dict[str, "JsonEvent"] =3D {} +# The ELF machine of the CPUIDs in each architecture's mapfile.csv. +_arch_e_machine: Dict[str, str] =3D { + 'arm64': 'EM_AARCH64', + 'nds32': 'EM_NDS32', + 'powerpc': 'EM_PPC64', + 'riscv': 'EM_RISCV', + 's390': 'EM_S390', + 'x86': 'EM_X86_64', +} # Events to write out when the table is closed _pending_events: list["JsonEvent"] =3D [] # Name of events table to be written out @@ -730,6 +739,8 @@ struct pmu_metrics_table { struct pmu_events_map { \tconst char *arch; \tconst char *cpuid; +\t/* The ELF machine of the architecture's CPUIDs, EM_NONE for test and co= mmon. */ +\tuint16_t e_machine; \tstruct pmu_events_table event_table; \tstruct pmu_metrics_table metric_table; }; @@ -745,6 +756,7 @@ static const struct pmu_events_map pmu_events_map[] =3D= { _args.output_file.write("""{ \t.arch =3D "testarch", \t.cpuid =3D "testcpu", +\t.e_machine =3D EM_NONE, \t.event_table =3D { \t\t.pmus =3D pmu_events__test_soc_cpu, \t\t.num_pmus =3D ARRAY_SIZE(pmu_events__test_soc_cpu), @@ -760,6 +772,7 @@ static const struct pmu_events_map pmu_events_map[] =3D= { _args.output_file.write("""{ \t.arch =3D "common", \t.cpuid =3D "common", +\t.e_machine =3D EM_NONE, \t.event_table =3D { \t\t.pmus =3D pmu_events__common, \t\t.num_pmus =3D ARRAY_SIZE(pmu_events__common), @@ -772,6 +785,7 @@ static const struct pmu_events_map pmu_events_map[] =3D= { }, """) else: + e_machine =3D _arch_e_machine[arch] with open(f'{_args.starting_dir}/{arch}/mapfile.csv', encoding=3D'ut= f-8') as csvfile: table =3D csv.reader(csvfile) first =3D True @@ -798,6 +812,7 @@ static const struct pmu_events_map pmu_events_map[] =3D= { _args.output_file.write(f"""{{ \t.arch =3D "{arch}", \t.cpuid =3D "{cpuid}", +\t.e_machine =3D {e_machine}, \t.event_table =3D {{ \t\t.pmus =3D {event_tblname}, \t\t.num_pmus =3D {event_size} @@ -814,6 +829,7 @@ static const struct pmu_events_map pmu_events_map[] =3D= { _args.output_file.write("""{ \t.arch =3D 0, \t.cpuid =3D 0, +\t.e_machine =3D EM_NONE, \t.event_table =3D { 0, 0 }, \t.metric_table =3D { 0 }, } @@ -1178,6 +1194,20 @@ int pmu_metrics_table__find_metric(const struct pmu_= metrics_table *table, \treturn PMU_METRICS__NOT_FOUND; } =20 +/* + * Is the map for the architecture of @e_machine? The 32 and 64-bit varian= ts of + * an architecture share tables. + */ +static bool pmu_events_map__e_machine_matches(const struct pmu_events_map = *map, +\t\t\t\t\t uint16_t e_machine) +{ +\tif (e_machine =3D=3D EM_386) +\t\te_machine =3D EM_X86_64; +\telse if (e_machine =3D=3D EM_PPC) +\t\te_machine =3D EM_PPC64; +\treturn map->e_machine =3D=3D e_machine; +} + static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) { \tstatic struct { @@ -1218,7 +1248,8 @@ static const struct pmu_events_map *map_for_cpu(struc= t perf_cpu cpu) \t\t\t\tbreak; \t\t\t} =20 -\t\t\tif (!strcmp_cpuid_str(map->cpuid, cpuid)) +\t\t\tif (pmu_events_map__e_machine_matches(map, EM_HOST) && +\t\t\t !strcmp_cpuid_str(map->cpuid, cpuid)) \t\t\t\tbreak; \t\t} \t\tfree(last_map_search.cpuid); @@ -1503,6 +1534,8 @@ such as "arm/cortex-a34".''', #include #include "util/header.h" #include "util/pmu.h" +#include +#include #include #include =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212884; b=FtZyH9SulqPYYlZ1ZRJUuuXJYqQ/kZOZQ2FV+NLgEhtoEkzY2iSagYFzXaKBJg3Sp5WH qhiEL59twMzfrX/vI9NEYEbJTMEnAXvJ7Z/klxQ7wSyZMcTJKTg9yXeCWEZGmNa03qept jX++CqgzwCrKavMIq64CCor/5M/dSVXuzkCN8cB5AHb0YkHSqp1HgEAP/Dm8Fm8kHXn1o IxHJKiTT4JdztQyZmyXoYSabHCsCyczvBHQNFwP0Mn1A1ZbZP8ZvsVuGV09cICSE5vFub i421yhxZmiYWizJ89bcjS9MtPC3cSyfSCEgn1IA1/XFno2y4upDbivccvnPS2aTnVXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212884; 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=6loZ6ysSmx3Fk3ckOW1hwxkVlfFlF7Hzh9OnNhaH8CI=; b=aYmTYUHDZexD9OSJNlh7lk5C+EC0LTgWbYgeNseuxt6fit/csGfwezG1Qo5DNurn74qp KiBJRTs5dfE8r2uL9k1qIK3zVNE8FgY9TrMLefcfjqVkEt0UTWFmGSOYFKbQfFxKY2VPH GyX+YCT4/tv7I1kYBE1uQWbQ5M4k1wonyvbq7GIxhgglwR/awXYeLv/SShNLynqF4YZ3r wtadlgVVPy3x22Td1UFBHIa4/X5W7GqLzM+fw+vQncF3/1sLH2AkAKTIHjupb5hywH26s t11kim8fV1dfvNJxiIRFu2ORYlssV8OKluN09jztJHr9LqJfQLLgNk9kmfhRVrnKksQ== 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=KKSMBLvh; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 C9EBE606A69B for ; Fri, 20 Nov 2026 22:14:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C9EBE606A69B 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=KKSMBLvh Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-383165e9b0fso1526564eec.0 for ; Fri, 20 Nov 2026 14:14:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212878; x=1795817678; 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=6loZ6ysSmx3Fk3ckOW1hwxkVlfFlF7Hzh9OnNhaH8CI=; b=KKSMBLvhGsgqPka4qA4G8ZNnd2xcJyvuKfJBQGGbdXky8ikZBoPZoxxddPaPnuXPIJ AYsn0IQVZ0OorqOZ3toIu/aGDRK/z4a5gJ3q4UVnbYq45LAoCBOM1AGI82HTdGsOkuHV Q2uyf9CyhhDtXqqkxjAIrHCoQjy2yqeKssCT/A1ZXv3yXmicLd/lYr70Y2OVagO1FLPG fCQ02kA48c+CdVta6keva22QA6TlUammkQPVTgzhsjPVuE6qJr4FLg+Ym8Hxo5WBOk8a JLF9Hcq8NoU2CvlMFs7m858TlThW/LDohbHqnmCdWJ6GIAVEAUsvtm0jn3SYpRJtBmQN zMXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212878; x=1795817678; 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=6loZ6ysSmx3Fk3ckOW1hwxkVlfFlF7Hzh9OnNhaH8CI=; b=UsMvltFEc3EROLNgV43RjE+rcgrH/9gosiYSLkuXGuTKL8okv8StzHdSHRJUXbYQfm 3dn+dv/ezHcV+3jh8bzIx+ahuhMawZVUHoDhoJ9LWhb3TKko5RZj1Dz60tThccTdd0qU OUiXyo1rutPG3Rf+gVZwLLQxXpEHrMFhhpEiNQ8pfPkBlG1qWRSicb+GsaVjloMpS69U aHNJMAExeLI5Kgy6nolYMThTfoJxC+LJ+01dVkFVpcfzyP3t7TtEwV1TPVjVJOsMvaug /qImuLZq1oKrGX1Okc1kmeblMTSqpXG/1X0JlmHzm1tQmY2e0tXx7M1fjC/r+CpvXGSV PowQ== X-Forwarded-Encrypted: i=1; AL+hErH8sUWbs/0InqblZ/jXBdgva4YQ71Pr+reFa5nTxMi04XQCAE/V+++tJZFb+6XhciZvBhFwhYoVjY1mKDM=@vger.kernel.org X-Gm-Message-State: APuU4Z92P+9z9W2WPYvPrwo5zhaeqpheHz+dHTVio8s4Idx8DDhHqPP+ Ev+0vXgMnkmzBATkmpwm+cmXg/FCpZM25bITeV7Ztf0CAbCxZuewy1YCiWvYRvMpWiQHTneXSjR 5y//3gdWOWQ== X-Received: from dybqy9.prod.google.com ([2002:a05:7300:d709:b0:381:e4bb:7fb7]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:50c4:b0:386:121c:c275 with SMTP id 5a478bee46e88-386121cc3c8mr2817152eec.10.1795212877531; Fri, 20 Nov 2026 14:14:37 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:13 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-13-irogers@google.com> Subject: [PATCH v3 12/30] perf pmu-events: Pass e_machine to find_core_*_table() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" find_core_events_table() and find_core_metrics_table() found tables by an architecture name, such as from perf_env__arch(), that has to match the pmu-events architecture directory name, for example "riscv" rather than the "riscv64" from uname. Instead find tables by the ELF machine of the CPUID's architecture, using the e_machine recorded in struct pmu_events_map. The test and common tables are found with EM_NONE. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 10 ++++++---- tools/perf/pmu-events/jevents.py | 10 ++++++---- tools/perf/pmu-events/pmu-events.h | 9 +++++++-- tools/perf/tests/expand-cgroup.c | 3 ++- tools/perf/tests/parse-metric.c | 3 ++- tools/perf/tests/pmu-events.c | 7 ++++--- tools/perf/util/tool_pmu.c | 3 ++- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 7428ffd4d70c..eba5446520c4 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -5927,23 +5927,25 @@ const struct pmu_metrics_table *pmu_metrics_table__= default(void) return NULL; } =20 -const struct pmu_events_table *find_core_events_table(const char *arch, co= nst char *cpuid) +const struct pmu_events_table *find_core_events_table(uint16_t e_machine, = const char *cpuid) { for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpui= d)) + if (pmu_events_map__e_machine_matches(tables, e_machine) && + !strcmp_cpuid_str(tables->cpuid, cpuid)) return &tables->event_table; } return NULL; } =20 -const struct pmu_metrics_table *find_core_metrics_table(const char *arch, = const char *cpuid) +const struct pmu_metrics_table *find_core_metrics_table(uint16_t e_machine= , const char *cpuid) { for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpui= d)) + if (pmu_events_map__e_machine_matches(tables, e_machine) && + !strcmp_cpuid_str(tables->cpuid, cpuid)) return &tables->metric_table; } return NULL; diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index db1b4749aec3..fc3de5fbda27 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -1345,23 +1345,25 @@ const struct pmu_metrics_table *pmu_metrics_table__= default(void) \treturn NULL; } =20 -const struct pmu_events_table *find_core_events_table(const char *arch, co= nst char *cpuid) +const struct pmu_events_table *find_core_events_table(uint16_t e_machine, = const char *cpuid) { \tfor (const struct pmu_events_map *tables =3D &pmu_events_map[0]; \t tables->arch; \t tables++) { -\t\tif (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cp= uid)) +\t\tif (pmu_events_map__e_machine_matches(tables, e_machine) && +\t\t !strcmp_cpuid_str(tables->cpuid, cpuid)) \t\t\treturn &tables->event_table; \t} \treturn NULL; } =20 -const struct pmu_metrics_table *find_core_metrics_table(const char *arch, = const char *cpuid) +const struct pmu_metrics_table *find_core_metrics_table(uint16_t e_machine= , const char *cpuid) { \tfor (const struct pmu_events_map *tables =3D &pmu_events_map[0]; \t tables->arch; \t tables++) { -\t\tif (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cp= uid)) +\t\tif (pmu_events_map__e_machine_matches(tables, e_machine) && +\t\t !strcmp_cpuid_str(tables->cpuid, cpuid)) \t\t\treturn &tables->metric_table; \t} \treturn NULL; diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index cb55c9fbca43..c3f645c97ccd 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -4,6 +4,7 @@ =20 #include #include +#include =20 struct perf_pmu; =20 @@ -134,8 +135,12 @@ const struct pmu_events_table *perf_pmu__find_events_t= able(struct perf_pmu *pmu) const struct pmu_events_table *perf_pmu__default_core_events_table(void); const struct pmu_metrics_table *pmu_metrics_table__find(void); const struct pmu_metrics_table *pmu_metrics_table__default(void); -const struct pmu_events_table *find_core_events_table(const char *arch, co= nst char *cpuid); -const struct pmu_metrics_table *find_core_metrics_table(const char *arch, = const char *cpuid); +/* + * Find the core events or metrics table for a CPUID of the architecture w= ith + * ELF machine @e_machine. The test and common tables use EM_NONE. + */ +const struct pmu_events_table *find_core_events_table(uint16_t e_machine, = const char *cpuid); +const struct pmu_metrics_table *find_core_metrics_table(uint16_t e_machine= , const char *cpuid); int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data); int pmu_for_each_core_metric(pmu_metric_iter_fn fn, void *data); =20 diff --git a/tools/perf/tests/expand-cgroup.c b/tools/perf/tests/expand-cgr= oup.c index 04d62611766a..f5e092e8bb48 100644 --- a/tools/perf/tests/expand-cgroup.c +++ b/tools/perf/tests/expand-cgroup.c @@ -10,6 +10,7 @@ #include "pfm.h" #include "target.h" #include +#include #include #include #include @@ -178,7 +179,7 @@ static int expand_metric_events(void) evlist =3D evlist__new(); TEST_ASSERT_VAL("failed to get evlist", evlist); =20 - pme_test =3D find_core_metrics_table("testarch", "testcpu"); + pme_test =3D find_core_metrics_table(EM_NONE, "testcpu"); ret =3D metricgroup__parse_groups_test(evlist, pme_test, metric_str, /*cputype_filter=3D*/false); if (ret < 0) { diff --git a/tools/perf/tests/parse-metric.c b/tools/perf/tests/parse-metri= c.c index 872d9a7aa72f..6f91e64a4123 100644 --- a/tools/perf/tests/parse-metric.c +++ b/tools/perf/tests/parse-metric.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -91,7 +92,7 @@ static int __compute_metric(const char *name, struct valu= e *vals, perf_evlist__set_maps(evlist__core(evlist), cpus, NULL); =20 /* Parse the metric into metric_events list. */ - pme_test =3D find_core_metrics_table("testarch", "testcpu"); + pme_test =3D find_core_metrics_table(EM_NONE, "testcpu"); err =3D metricgroup__parse_groups_test(evlist, pme_test, name, /*cputype_filter=3D*/false); if (err) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index cb0c2b05c1e8..8e82f7af2aa6 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -4,6 +4,7 @@ #include "pmu.h" #include "pmus.h" #include "tests.h" +#include #include #include #include @@ -449,7 +450,7 @@ static int test__pmu_event_table(struct test_suite *tes= t __maybe_unused, { const struct pmu_events_table *sys_event_table =3D find_sys_events_table("pmu_events__test_soc_sys"); - const struct pmu_events_table *table =3D find_core_events_table("testarch= ", "testcpu"); + const struct pmu_events_table *table =3D find_core_events_table(EM_NONE, = "testcpu"); int map_events =3D 0, expected_events, err; =20 /* ignore 3x sentinels */ @@ -504,7 +505,7 @@ static int __test_core_pmu_event_aliases(const char *pm= u_name, int *count) struct perf_pmu_test_event const **test_event_table; struct perf_pmu *pmu; int res =3D 0; - const struct pmu_events_table *table =3D find_core_events_table("testarch= ", "testcpu"); + const struct pmu_events_table *table =3D find_core_events_table(EM_NONE, = "testcpu"); =20 if (!table) return -1; @@ -559,7 +560,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) const struct pmu_events_table *events_table; int res =3D 0; =20 - events_table =3D find_core_events_table("testarch", "testcpu"); + events_table =3D find_core_events_table(EM_NONE, "testcpu"); if (!events_table) return -1; =20 diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index 6054d5d5aece..d09361999b82 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include =20 @@ -718,6 +719,6 @@ struct perf_pmu *tool_pmu__new(void) perf_pmu__delete(tool); return NULL; } - tool->events_table =3D find_core_events_table("common", "common"); + tool->events_table =3D find_core_events_table(EM_NONE, "common"); return tool; } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212890; b=cSxWjUGffkwqcB3MQotJb0I4fburCNEg3vCLo8fnLrLQ2acsv07M0iVXcMuPE8srWTDf hq0ld4gUOTND0agFjJ7/h2xuLwEL9tYB7vpqUTrfGRVZcUQues3EO21wvCmi+Oj6kobuj HWnutrYongeqy24WCeU2BbJvHEzF0Frgnobu5FRZX3c/PWZXZWlNL+4jRZDKZKpCOCPTZ XQ0Y5PbXHWuITT+jA97OKzVmTqCcyVRl+D7cNAcBvVJyUl6Raqg1wJCj0jJC74sloSBjo TvuV6A71Xsyytusrju3MdYZg3xT7cXsJqBfnrrVtpk8CMcEiRKpTMcDMumOsBUZKZbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212890; 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=o/9qb+I7+xixqC5bd30s5LhP1apRWnODn/jK28mCv9Y=; b=q1AFLX7xfqpFoavCIEsbOnOZxhNOywOqB/I6eQMX7niRz6kxHEclXOXneE6eTD0uHG0t 5Srd5oFGVaB3CUWVCF66sZFSANWmS45W16/foC3LV8pKDVuH+ehfpDUiwGr4aDTcDUa6k +8kEnwJCwpdgQ1c1oOm0fkMIUp7x15CNUXb9v4K9VdD+U9+HQwO6i80HJDW+LUaWFLvyp vkq6T7snt+kBo83KPtq43nvJxUT3FaVHzdzAJUdklPeC667n2SgfA3//EtQFdLQVJttNW 7ZMAcsYliB7vMuFQ+EjFlx+tormXKJzzZBWxzz+Unb86CJd+l6c9zL7dESBV27t9Aig== 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="Q/ZqzaVf"; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (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 B08F0606A6B3 for ; Fri, 20 Nov 2026 22:14:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B08F0606A6B3 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=Q/ZqzaVf Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-154e9d2d94fso3150317c88.0 for ; Fri, 20 Nov 2026 14:14:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212884; x=1795817684; 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=o/9qb+I7+xixqC5bd30s5LhP1apRWnODn/jK28mCv9Y=; b=Q/ZqzaVf+f4rkzUd59kandjIrtABA9qztn+tTIwkWKhHE+tEOo0YkCTR9KZXplXeel F+Xmx+eBijoj7nhT6j6tOFTsY4JzVtMv8dJJt5g2qb7eadtJWSo0NiWZgCRaCdlQQBBO 1ugxQBsPUDmYFadKmaOUUjkkn2/XoDFvAP/HUqKRsEiubL+r33ruLxSiw6770N2L1x5O 7QqGc36f7+vcQJrH5OBWNt7Y9xV+bepDLpb7tJJp1kgCE/AeoyPythn5OGQDyHE/Mu6Z f6htLZbsdUHexzpYNelhuGAkfM3/mYKanZ1TT+B5M2YdBtt0R+ekG4mEb/BjHkKGLcWA YK7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212884; x=1795817684; 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=o/9qb+I7+xixqC5bd30s5LhP1apRWnODn/jK28mCv9Y=; b=X4Q1PtTJjxL2ci54cnu7NhkIyoZD7/2PxOqKshCUz/bLuANgKDRXGH76HbPpzQXwgU Fa5Byb4/dFRitgv86KmCRmUOLWwUDBhfEzORGEU1bZNztXzSf3Ufp2sF8IiMZdDPXDhF 9A2Etj/9r/Un0vVLBJm9sbJhoHHX6in8UqJMRNq/1xO3khG4WmqhAWTiDPH8H/AhvbB2 PQBqdOF5YEPP/4JzandbvwKTxAkJbRdmWpJN8xOa1iSoZgUrIxXJ2jKNKU6WF+T1X5aa 1ISxIZbB3/0RoddUhxFj4LfXF0J4hB+xTTEnrcGuYLiY5FLteWMUvoZxhfqwUQxZZ/cs nKdg== X-Forwarded-Encrypted: i=1; AL+hErGLWWdzRoZu2MvcC+nL9nr7owlI4L5MAdSuIomC5dJ64ZKRPYBRrVpr83RQLRXts7nVyx19PUtziOIWiZY=@vger.kernel.org X-Gm-Message-State: APuU4Z/tddkW34CHPm9sfWX3jESnvI1fKF4Boes1GnBFc0JmdeuBbm0I kNdTD998mbPj1O9X6Zu0mrLur00VdR5r0i7HwCWKIuBZAoxBCyNdcYSDW6LXBiuHY+UsgEPKMn/ oFrHL51tIXw== X-Received: from dlbtu12.prod.google.com ([2002:a05:7022:3c0c:b0:1b5:31ed:e280]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:ec17:b0:166:862b:9bcc with SMTP id a92af1059eb24-1b55689ad7amr5592165c88.18.1795212883256; Fri, 20 Nov 2026 14:14:43 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:14 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-14-irogers@google.com> Subject: [PATCH v3 13/30] perf header: Pass e_machine to strcmp_cpuid_str() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The format of a CPUID depends on its architecture, but strcmp_cpuid_str() is only given the two CPUID strings and its architecture specific implementations assume the CPUIDs are for the architecture perf was built for. Pass the ELF machine of the CPUIDs' architecture to strcmp_cpuid_str() so that CPUIDs can be compared according to their architecture rather than the host's. The pmu-events tables pass their e_machine, comparisons with the host's CPUID pass EM_HOST and the arm64 CPUID matching test passes EM_AARCH64. The architecture specific implementations don't yet use the argument. Signed-off-by: Ian Rogers --- tools/perf/arch/arm64/tests/cpuid-match.c | 17 +++++++++-------- tools/perf/arch/arm64/util/header.c | 2 +- tools/perf/arch/x86/util/header.c | 2 +- tools/perf/arch/x86/util/pmu.c | 10 ++++++---- tools/perf/pmu-events/empty-pmu-events.c | 6 +++--- tools/perf/pmu-events/jevents.py | 6 +++--- tools/perf/util/expr.c | 3 ++- tools/perf/util/header.c | 3 ++- tools/perf/util/header.h | 6 +++++- 9 files changed, 32 insertions(+), 23 deletions(-) diff --git a/tools/perf/arch/arm64/tests/cpuid-match.c b/tools/perf/arch/ar= m64/tests/cpuid-match.c index e8e3947cca18..b073ac33ea9b 100644 --- a/tools/perf/arch/arm64/tests/cpuid-match.c +++ b/tools/perf/arch/arm64/tests/cpuid-match.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include =20 #include "arch-tests.h" #include "tests/tests.h" @@ -9,28 +10,28 @@ int test__cpuid_match(struct test_suite *test __maybe_un= used, int subtest __maybe_unused) { /* midr with no leading zeros matches */ - if (strcmp_cpuid_str("0x410fd0c0", "0x00000000410fd0c0")) + if (strcmp_cpuid_str(EM_AARCH64, "0x410fd0c0", "0x00000000410fd0c0")) return -1; /* Upper case matches */ - if (strcmp_cpuid_str("0x410fd0c0", "0x00000000410FD0C0")) + if (strcmp_cpuid_str(EM_AARCH64, "0x410fd0c0", "0x00000000410FD0C0")) return -1; /* r0p0 =3D r0p0 matches */ - if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000410fd480")) + if (strcmp_cpuid_str(EM_AARCH64, "0x00000000410fd480", "0x00000000410fd48= 0")) return -1; /* r0p1 > r0p0 matches */ - if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000410fd481")) + if (strcmp_cpuid_str(EM_AARCH64, "0x00000000410fd480", "0x00000000410fd48= 1")) return -1; /* r1p0 > r0p0 matches*/ - if (strcmp_cpuid_str("0x00000000410fd480", "0x00000000411fd480")) + if (strcmp_cpuid_str(EM_AARCH64, "0x00000000410fd480", "0x00000000411fd48= 0")) return -1; /* r0p0 < r0p1 doesn't match */ - if (!strcmp_cpuid_str("0x00000000410fd481", "0x00000000410fd480")) + if (!strcmp_cpuid_str(EM_AARCH64, "0x00000000410fd481", "0x00000000410fd4= 80")) return -1; /* r0p0 < r1p0 doesn't match */ - if (!strcmp_cpuid_str("0x00000000411fd480", "0x00000000410fd480")) + if (!strcmp_cpuid_str(EM_AARCH64, "0x00000000411fd480", "0x00000000410fd4= 80")) return -1; /* Different CPU doesn't match */ - if (!strcmp_cpuid_str("0x00000000410fd4c0", "0x00000000430f0af0")) + if (!strcmp_cpuid_str(EM_AARCH64, "0x00000000410fd4c0", "0x00000000430f0a= f0")) return -1; =20 return 0; diff --git a/tools/perf/arch/arm64/util/header.c b/tools/perf/arch/arm64/ut= il/header.c index 95e71c4f6c78..ee28add836b5 100644 --- a/tools/perf/arch/arm64/util/header.c +++ b/tools/perf/arch/arm64/util/header.c @@ -87,7 +87,7 @@ char *get_cpuid_str(struct perf_cpu cpu) * * Return 1 if the CPU type is different or the version of idstr is lower. */ -int strcmp_cpuid_str(const char *mapcpuid, const char *idstr) +int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpu= id, const char *idstr) { u64 map_id =3D strtoull(mapcpuid, NULL, 16); char map_id_variant =3D FIELD_GET(MIDR_VARIANT_MASK, map_id); diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/h= eader.c index 412977f8aa83..b65856f3b846 100644 --- a/tools/perf/arch/x86/util/header.c +++ b/tools/perf/arch/x86/util/header.c @@ -91,7 +91,7 @@ static bool is_full_cpuid(const char *id) return false; } =20 -int strcmp_cpuid_str(const char *mapcpuid, const char *id) +int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpu= id, const char *id) { regex_t re; regmatch_t pmatch[1]; diff --git a/tools/perf/arch/x86/util/pmu.c b/tools/perf/arch/x86/util/pmu.c index fd4491fda117..37269154e5e7 100644 --- a/tools/perf/arch/x86/util/pmu.c +++ b/tools/perf/arch/x86/util/pmu.c @@ -22,6 +22,7 @@ #include "util/debug.h" #include "util/env.h" #include "util/header.h" +#include "dwarf-regs.h" =20 #define GENUINE_INTEL_SPR "GenuineIntel-6-8F" #define GENUINE_INTEL_EMR "GenuineIntel-6-CF" @@ -41,7 +42,8 @@ static void init_snc_support(void) char *cpuid =3D get_cpuid_str((struct perf_cpu){0}); =20 for (size_t i =3D 0; i < ARRAY_SIZE(supported_cpuids); i++) { - cached_snc_supported =3D cpuid && strcmp_cpuid_str(supported_cpuids[i], = cpuid) =3D=3D 0; + cached_snc_supported =3D cpuid && + strcmp_cpuid_str(EM_HOST, supported_cpuids[i], cpuid) =3D=3D 0; if (cached_snc_supported) break; } @@ -173,11 +175,11 @@ static void init_snc_map(void) case 2: cpuid =3D get_cpuid_str((struct perf_cpu){ 0 }); if (cpuid) { - if (strcmp_cpuid_str(GENUINE_INTEL_SPR, cpuid) =3D=3D 0 || - strcmp_cpuid_str(GENUINE_INTEL_EMR, cpuid) =3D=3D 0) { + if (strcmp_cpuid_str(EM_HOST, GENUINE_INTEL_SPR, cpuid) =3D=3D 0 || + strcmp_cpuid_str(EM_HOST, GENUINE_INTEL_EMR, cpuid) =3D=3D 0) { cached_imc_snc_map =3D spr_emr_snc2_map; cached_imc_snc_map_len =3D ARRAY_SIZE(spr_emr_snc2_map); - } else if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) =3D=3D 0) { + } else if (strcmp_cpuid_str(EM_HOST, GENUINE_INTEL_GNR, cpuid) =3D=3D 0= ) { cached_imc_snc_map =3D gnr_snc2_map; cached_imc_snc_map_len =3D ARRAY_SIZE(gnr_snc2_map); } diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index eba5446520c4..872c6ca6e817 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -5831,7 +5831,7 @@ static const struct pmu_events_map *map_for_cpu(struc= t perf_cpu cpu) } =20 if (pmu_events_map__e_machine_matches(map, EM_HOST) && - !strcmp_cpuid_str(map->cpuid, cpuid)) + !strcmp_cpuid_str(map->e_machine, map->cpuid, cpuid)) break; } free(last_map_search.cpuid); @@ -5933,7 +5933,7 @@ const struct pmu_events_table *find_core_events_table= (uint16_t e_machine, const tables->arch; tables++) { if (pmu_events_map__e_machine_matches(tables, e_machine) && - !strcmp_cpuid_str(tables->cpuid, cpuid)) + !strcmp_cpuid_str(tables->e_machine, tables->cpuid, cpuid)) return &tables->event_table; } return NULL; @@ -5945,7 +5945,7 @@ const struct pmu_metrics_table *find_core_metrics_tab= le(uint16_t e_machine, cons tables->arch; tables++) { if (pmu_events_map__e_machine_matches(tables, e_machine) && - !strcmp_cpuid_str(tables->cpuid, cpuid)) + !strcmp_cpuid_str(tables->e_machine, tables->cpuid, cpuid)) return &tables->metric_table; } return NULL; diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index fc3de5fbda27..802ae2ad4405 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -1249,7 +1249,7 @@ static const struct pmu_events_map *map_for_cpu(struc= t perf_cpu cpu) \t\t\t} =20 \t\t\tif (pmu_events_map__e_machine_matches(map, EM_HOST) && -\t\t\t !strcmp_cpuid_str(map->cpuid, cpuid)) +\t\t\t !strcmp_cpuid_str(map->e_machine, map->cpuid, cpuid)) \t\t\t\tbreak; \t\t} \t\tfree(last_map_search.cpuid); @@ -1351,7 +1351,7 @@ const struct pmu_events_table *find_core_events_table= (uint16_t e_machine, const \t tables->arch; \t tables++) { \t\tif (pmu_events_map__e_machine_matches(tables, e_machine) && -\t\t !strcmp_cpuid_str(tables->cpuid, cpuid)) +\t\t !strcmp_cpuid_str(tables->e_machine, tables->cpuid, cpuid)) \t\t\treturn &tables->event_table; \t} \treturn NULL; @@ -1363,7 +1363,7 @@ const struct pmu_metrics_table *find_core_metrics_tab= le(uint16_t e_machine, cons \t tables->arch; \t tables++) { \t\tif (pmu_events_map__e_machine_matches(tables, e_machine) && -\t\t !strcmp_cpuid_str(tables->cpuid, cpuid)) +\t\t !strcmp_cpuid_str(tables->e_machine, tables->cpuid, cpuid)) \t\t\treturn &tables->metric_table; \t} \treturn NULL; diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index f1164c4666b1..09ced31ef59e 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -14,6 +14,7 @@ #include #include "util/hashmap.h" #include "util/header.h" +#include "dwarf-regs.h" #include "util/pmu.h" #include #include @@ -485,7 +486,7 @@ double expr__strcmp_cpuid_str(const struct expr_parse_c= tx *ctx __maybe_unused, if (!cpuid) return NAN; =20 - ret =3D !strcmp_cpuid_str(test_id, cpuid); + ret =3D !strcmp_cpuid_str(EM_HOST, test_id, cpuid); =20 free(cpuid); return ret; diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 4d61451020f3..3b5a0382fe15 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -961,7 +961,8 @@ char *get_cpuid_allow_env_override(struct perf_cpu cpu) * cpuid string generated on this platform. * Otherwise return non-zero. */ -int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) +int __weak strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char = *mapcpuid, + const char *cpuid) { regex_t re; regmatch_t pmatch[1]; diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index 765037762758..d30fb30a0c92 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -219,7 +219,11 @@ char *get_cpuid_str(struct perf_cpu cpu); =20 char *get_cpuid_allow_env_override(struct perf_cpu cpu); =20 -int strcmp_cpuid_str(const char *s1, const char *s2); +/* + * Compare a CPUID from a pmu-events mapfile.csv, @mapcpuid, with @cpuid f= or + * the architecture with ELF machine @e_machine. Returns 0 on a match. + */ +int strcmp_cpuid_str(uint16_t e_machine, const char *mapcpuid, const char = *cpuid); =20 struct cpu_domain_map **build_cpu_domain_map(u32 *schedstat_version, u32 *= max_sched_domains, u32 nr); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212892; b=SMY01ZRys2F215+A1ISGW/oosFN7e6xICRJarFz6Ng+0SSBxi2YUjdHgs+5NKsBvxCT6 rplN1YByxcWY7Akug7hph9qGGnNmeJRdpcPGSWbIF6BGkrVHqd0PNx7j+pXbwdnMx/N3h a8WwxEqyh+OLNFH7HGFCd/DDC8alap+LXliBtTsj6l2U6GAUDIQZGtGW9CqGrINxOtJub E65lx95RFKlR6u2AVA3uoBmSmzVdkQkbwx9hj3EN2HlGAp3SBX5wIuIoN5XSJXN08UtPN EuDfP1HPRCq0fyXwGjc268/g+n27Z7YEJIwNh/XACn5/qHDEKIpFm5r/DMa9kkVPfFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212892; 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=aY1b3UP7YZRvgGaRmfiYW/yKSptqiO+5/M/0pL/1hDM=; b=rbMASGJ5QKs1abQ/9RAZtQ3upX00sUZyQZ4f7tjOaLFlvA9WZN7auRITEmfd3do95Pcl fuWhwecGKJbcDwxEiRhktYi+JwzrbKxGStthLv0xKa190Q/Izs9ffw9ZsQtQIlth9Op5z rj7yviq4vgnSkZOkvnBGGIYir4x2or/2qvctDhVPTZMA0qyRizS6wg8PRCPnZktswVgg8 VbscTvwUUcXTdI8xOMos8JnWWjBfKWe5dRR7UYPLEWI3n2xDQFrW3iK/LwOb+ssNo3mMK 0qbx8sHzyBcWg/vBN6Y/lw0TtL9apU3U9teE4Gb0NDWnoEuPUbYisx9pw9s82O0AKYA== 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=XZIng9WJ; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (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 9E52C606A33C for ; Fri, 20 Nov 2026 22:14:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E52C606A33C 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=XZIng9WJ Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1383723dfddso1464249c88.0 for ; Fri, 20 Nov 2026 14:14:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212889; x=1795817689; 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=aY1b3UP7YZRvgGaRmfiYW/yKSptqiO+5/M/0pL/1hDM=; b=XZIng9WJHK//r8dIgxaTcpd2fqp4O1Xqe7TYHMCnc2EJ1pyO5IE0RT+kc1iqV6Vzid OdmY2gsVt47kQsyfNt3rkevQYIlkaBRtY+y4VTW0c3xh/ecY3nAYK3NZJv6VoHzEPaOq vmHNNLSWx/2SeA4u3EgQ3z9yKe+S39lHB4/iIYndh8vcRxdbUxjSaZQPy7ifYtCVlC+q 6w1G04cEuc89TrMSTZkprqyX27u2Kzb1cKAn8hCkicPqagUg0zv1Lb31eaB3Uo9qJpWP Zrks4Lw1GG8EnvAmYBrauu3Hi+Ols58/BDkjMPBfP6as1z+ZH3FHB3oi1aULjB5Oj4fQ 07ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212889; x=1795817689; 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=aY1b3UP7YZRvgGaRmfiYW/yKSptqiO+5/M/0pL/1hDM=; b=cATbcj5zAC4uU/8ZxEan0f46W7hA61YeDKZwSgR2A+2NIZgoJg08PpXPrPn8mMuCbp gV9IEBasRImgT+j1Rgd8yjpRbxrK2dULg1Hr5V+PrPw0IKPWKKwEp78nfFOY9C7lYLg7 Tx9paCTkDV7q16ISpKaNwTPvn659/P1cNnv51xgpW4G34dWP7X8NSISisllM/Yan3jw/ 46qZCToJekTKtsnRAAH4uboScp3K+06mlrSo15sQVqzKz1ZiL2LrqhCQbupCH4R8JL9c 6xXeCraGmQOby2laEwT1c3+qnC7mfqttY2QEHGRiJMhnrWobCBvvaU5ME7rnBMIVttEP 1Tag== X-Forwarded-Encrypted: i=1; AL+hErFWoIjNvtTztJxSimIN7ypMZtVK7pr4X8KjRiPCKbm8yQ6GI0PDhnvgfUbhli7J+Fq+CIotcIzAm4duDWQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+AQ5+oqVgKgjKf8XYYiRkHa2WGyLPdpDkAtcDX4Z0EIBLxryTh j2v4/e+POYAN8G2tlyuVBWuN/X9+OCHXw3sm6RjkofivHwVDIznAJHlxP84sK0IwyN1At5ZnBYy Br5EmMN1PwA== X-Received: from dlea19-n1.prod.google.com ([2002:a05:701b:4213:10b0:19a:1425:bb9c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:2522:b0:1aa:47e7:97e3 with SMTP id a92af1059eb24-1b5568bc56emr5177726c88.15.1795212888244; Fri, 20 Nov 2026 14:14:48 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:15 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-15-irogers@google.com> Subject: [PATCH v3 14/30] perf header: Support CPUID matching across all architectures 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" strcmp_cpuid_str() had architecture specific overrides of a __weak regular expression matching implementation in arch/x86/util/header.c and arch/arm64/util/header.c, so a CPUID could only be compared using the rules of the architecture perf was built for. In addition, the HEADER_CPUID strings written by get_cpuid() on x86 ("vendor,family, model,step" in decimal) and PowerPC ("pvr_ver,pvr_rev" in decimal) differ from the get_cpuid_str() formats ("vendor-family-model-stepping" in hex and "0x%.8lx") matched against the pmu-events mapfile.csv tables. As a result, finding the core events or metrics table for the CPUID of a perf_env, such as from a perf.data file recorded on a different architecture, failed. Move the architecture specific comparisons into util/header.c and select them using the ELF machine of the CPUIDs: EM_NONE for the test and common tables compares exactly, arm64 compares MIDR part, variant and revision fields, x86 matches with an optional stepping and other architectures match a regular expression. Convert x86 and PowerPC HEADER_CPUID strings before matching. Signed-off-by: Ian Rogers --- tools/perf/arch/arm64/util/header.c | 48 --------- tools/perf/arch/x86/util/header.c | 64 ----------- tools/perf/util/header.c | 161 +++++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 117 deletions(-) diff --git a/tools/perf/arch/arm64/util/header.c b/tools/perf/arch/arm64/ut= il/header.c index ee28add836b5..181836613087 100644 --- a/tools/perf/arch/arm64/util/header.c +++ b/tools/perf/arch/arm64/util/header.c @@ -1,5 +1,3 @@ -#include -#include #include #include #include @@ -10,8 +8,6 @@ =20 #define MIDR "/regs/identification/midr_el1" #define MIDR_SIZE 19 -#define MIDR_REVISION_MASK GENMASK(3, 0) -#define MIDR_VARIANT_MASK GENMASK(23, 20) =20 static int _get_cpuid(char *buf, size_t sz, struct perf_cpu cpu) { @@ -79,47 +75,3 @@ char *get_cpuid_str(struct perf_cpu cpu) =20 return buf; } - -/* - * Return 0 if idstr is a higher or equal to version of the same part as - * mapcpuid. Therefore, if mapcpuid has 0 for revision and variant then any - * version of idstr will match as long as it's the same CPU type. - * - * Return 1 if the CPU type is different or the version of idstr is lower. - */ -int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpu= id, const char *idstr) -{ - u64 map_id =3D strtoull(mapcpuid, NULL, 16); - char map_id_variant =3D FIELD_GET(MIDR_VARIANT_MASK, map_id); - char map_id_revision =3D FIELD_GET(MIDR_REVISION_MASK, map_id); - u64 id =3D strtoull(idstr, NULL, 16); - char id_variant =3D FIELD_GET(MIDR_VARIANT_MASK, id); - char id_revision =3D FIELD_GET(MIDR_REVISION_MASK, id); - u64 id_fields =3D ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK); - - /* Compare without version first */ - if ((map_id & id_fields) !=3D (id & id_fields)) - return 1; - - /* - * ID matches, now compare version. - * - * Arm revisions (like r0p0) are compared here like two digit semver - * values eg. 1.3 < 2.0 < 2.1 < 2.2. - * - * r =3D high value =3D 'Variant' field in MIDR - * p =3D low value =3D 'Revision' field in MIDR - * - */ - if (id_variant > map_id_variant) - return 0; - - if (id_variant =3D=3D map_id_variant && id_revision >=3D map_id_revision) - return 0; - - /* - * variant is less than mapfile variant or variants are the same but - * the revision doesn't match. Return no match. - */ - return 1; -} diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/h= eader.c index b65856f3b846..5203e5cd151c 100644 --- a/tools/perf/arch/x86/util/header.c +++ b/tools/perf/arch/x86/util/header.c @@ -5,7 +5,6 @@ #include #include #include -#include =20 #include "../../../util/debug.h" #include "../../../util/header.h" @@ -73,66 +72,3 @@ char *get_cpuid_str(struct perf_cpu cpu __maybe_unused) } return buf; } - -/* Full CPUID format for x86 is vendor-family-model-stepping */ -static bool is_full_cpuid(const char *id) -{ - const char *tmp =3D id; - int count =3D 0; - - while ((tmp =3D strchr(tmp, '-')) !=3D NULL) { - count++; - tmp++; - } - - if (count =3D=3D 3) - return true; - - return false; -} - -int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpu= id, const char *id) -{ - regex_t re; - regmatch_t pmatch[1]; - int match; - bool full_mapcpuid =3D is_full_cpuid(mapcpuid); - bool full_cpuid =3D is_full_cpuid(id); - - /* - * Full CPUID format is required to identify a platform. - * Error out if the cpuid string is incomplete. - */ - if (full_mapcpuid && !full_cpuid) { - pr_info("Invalid CPUID %s. Full CPUID is required, " - "vendor-family-model-stepping\n", id); - return 1; - } - - if (regcomp(&re, mapcpuid, REG_EXTENDED) !=3D 0) { - /* Warn unable to generate match particular string. */ - pr_info("Invalid regular expression %s\n", mapcpuid); - return 1; - } - - match =3D !regexec(&re, id, 1, pmatch, 0); - regfree(&re); - if (match) { - size_t match_len =3D (pmatch[0].rm_eo - pmatch[0].rm_so); - size_t cpuid_len; - - /* If the full CPUID format isn't required, - * ignoring the stepping. - */ - if (!full_mapcpuid && full_cpuid) - cpuid_len =3D strrchr(id, '-') - id; - else - cpuid_len =3D strlen(id); - - /* Verify the entire string matched. */ - if (match_len =3D=3D cpuid_len) - return 0; - } - - return 1; -} diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 3b5a0382fe15..c1f77babcb70 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -11,7 +11,10 @@ =20 #include #include +#include +#include #include +#include #include #include #include @@ -957,12 +960,70 @@ char *get_cpuid_allow_env_override(struct perf_cpu cp= u) return cpuid; } =20 -/* Return zero when the cpuid from the mapfile.csv matches the - * cpuid string generated on this platform. - * Otherwise return non-zero. +#define MIDR_REVISION_MASK GENMASK(3, 0) +#define MIDR_VARIANT_MASK GENMASK(23, 20) + +/* Is @id a full x86 vendor-family-model-stepping CPUID, with 3 hyphens? */ +static bool is_full_x86_cpuid(const char *id) +{ + const char *tmp =3D id; + int count =3D 0; + + while ((tmp =3D strchr(tmp, '-')) !=3D NULL) { + count++; + tmp++; + } + + return count =3D=3D 3; +} + +/* + * Compare arm64 MIDR values. Return 0 if @idstr is the same CPU type as + * @mapcpuid and its version, the Variant[23:20] and Revision[3:0] fields,= is + * greater than or equal to that of @mapcpuid. Therefore, if @mapcpuid has= 0 for + * the revision and variant then any version of @idstr will match as long = as + * it's the same CPU type. + * + * Return 1 if the CPU type is different or the version of @idstr is lower. */ -int __weak strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char = *mapcpuid, - const char *cpuid) +static int strcmp_arm64_cpuid_str(const char *mapcpuid, const char *idstr) +{ + u64 map_id =3D strtoull(mapcpuid, NULL, 16); + char map_id_variant =3D FIELD_GET(MIDR_VARIANT_MASK, map_id); + char map_id_revision =3D FIELD_GET(MIDR_REVISION_MASK, map_id); + u64 id =3D strtoull(idstr, NULL, 16); + char id_variant =3D FIELD_GET(MIDR_VARIANT_MASK, id); + char id_revision =3D FIELD_GET(MIDR_REVISION_MASK, id); + u64 id_fields =3D ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK); + + /* Compare without version first. */ + if ((map_id & id_fields) !=3D (id & id_fields)) + return 1; + + /* + * ID matches, now compare version. + * + * Arm revisions (like r0p0) are compared here like two digit semver + * values eg. 1.3 < 2.0 < 2.1 < 2.2. + * + * r =3D high value =3D 'Variant' field in MIDR + * p =3D low value =3D 'Revision' field in MIDR + */ + if (id_variant > map_id_variant) + return 0; + + if (id_variant =3D=3D map_id_variant && id_revision >=3D map_id_revision) + return 0; + + /* + * Variant is less than mapfile variant or variants are the same but + * the revision doesn't match. Return no match. + */ + return 1; +} + +/* Return 0 if all of @cpuid matches the extended regular expression @mapc= puid. */ +static int strcmp_regex_cpuid_str(const char *mapcpuid, const char *cpuid) { regex_t re; regmatch_t pmatch[1]; @@ -986,6 +1047,96 @@ int __weak strcmp_cpuid_str(uint16_t e_machine __mayb= e_unused, const char *mapcp return 1; } =20 +/* + * Compare x86 CPUIDs of the form vendor-family-model[-stepping]. A full + * @mapcpuid is stepping specific and requires a full @cpuid, otherwise the + * stepping of a full @cpuid is ignored. + */ +static int strcmp_x86_cpuid_str(const char *mapcpuid, const char *cpuid) +{ + unsigned int family, model, step; + char norm_buf[128], buf[128]; + char vendor[32], extra; + bool full_mapcpuid, full_cpuid; + + /* x86 get_cpuid() writes "vendor,family,model,step" in decimal. */ + if (sscanf(cpuid, "%31[^,],%u,%u,%u%c", vendor, &family, &model, &step, &= extra) =3D=3D 4) { + snprintf(norm_buf, sizeof(norm_buf), "%s-%u-%X-%X", vendor, family, mode= l, step); + cpuid =3D norm_buf; + } + + full_mapcpuid =3D is_full_x86_cpuid(mapcpuid); + full_cpuid =3D is_full_x86_cpuid(cpuid); + + /* + * Full CPUID format is required to identify a stepping specific + * platform. Error out if the cpuid string is incomplete. + */ + if (full_mapcpuid && !full_cpuid) { + pr_info("Invalid CPUID %s. Full CPUID is required, vendor-family-model-s= tepping\n", + cpuid); + return 1; + } + + /* If the full CPUID format isn't required, ignore the stepping. */ + if (!full_mapcpuid && full_cpuid) { + int len =3D strrchr(cpuid, '-') - cpuid; + + snprintf(buf, sizeof(buf), "%.*s", len, cpuid); + cpuid =3D buf; + } + return strcmp_regex_cpuid_str(mapcpuid, cpuid); +} + +/* Compare PowerPC PVR CPUIDs, matching @cpuid against the regex @mapcpuid= . */ +static int strcmp_powerpc_cpuid_str(const char *mapcpuid, const char *cpui= d) +{ + unsigned long pvr_ver, pvr_rev; + char norm_buf[32], extra; + + /* PowerPC get_cpuid() writes "pvr_ver,pvr_rev" in decimal. */ + if (sscanf(cpuid, "%lu,%lu%c", &pvr_ver, &pvr_rev, &extra) =3D=3D 2) { + unsigned long pvr =3D ((pvr_ver & 0xffff) << 16) | (pvr_rev & 0xffff); + + snprintf(norm_buf, sizeof(norm_buf), "0x%.8lx", pvr); + cpuid =3D norm_buf; + } + return strcmp_regex_cpuid_str(mapcpuid, cpuid); +} + +/** + * strcmp_cpuid_str - Compare a pmu-events mapfile.csv CPUID with a CPUID. + * @e_machine: The ELF machine of the CPUIDs' architecture. + * @mapcpuid: The CPUID from the mapfile.csv, a regular expression for most + * architectures. + * @cpuid: The CPUID from get_cpuid_str() or HEADER_CPUID. + * + * The test and common tables use EM_NONE and are compared exactly, arm64 + * compares MIDR values and other architectures match @cpuid against the + * regular expression @mapcpuid. HEADER_CPUID values written by get_cpuid(= ) on + * x86 and PowerPC differ from the get_cpuid_str() format of the mapfile.c= sv + * and are converted before matching. + * + * Return: 0 if the CPUIDs match, otherwise 1. + */ +int strcmp_cpuid_str(uint16_t e_machine, const char *mapcpuid, const char = *cpuid) +{ + switch (e_machine) { + case EM_NONE: + return strcmp(mapcpuid, cpuid) ? 1 : 0; + case EM_AARCH64: + return strcmp_arm64_cpuid_str(mapcpuid, cpuid); + case EM_386: + case EM_X86_64: + return strcmp_x86_cpuid_str(mapcpuid, cpuid); + case EM_PPC: + case EM_PPC64: + return strcmp_powerpc_cpuid_str(mapcpuid, cpuid); + default: + return strcmp_regex_cpuid_str(mapcpuid, cpuid); + } +} + /* * default get_cpuid(): nothing gets recorded * actual implementation must be in arch/$(SRCARCH)/util/header.c --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212894; b=ACVUhCsWSSehhe2MpHP/DwbhLckbi/USjr6CObboRoMEVb3Cpm5A2JzgXqdFqTX9AZvN 6ZqCfvQBnaMy9TMM/q9I7dISazjJkDgdiW5cScmf85jpsCuBXvkD0XRaRZ9Y2Ae+s8uqf Idf1HXNt8KfEeO2L5I8BK5HsdaodatIwCOOrpDlBkB7ixoPCV7NWd6rMIEzBxQ0EiUmPm 24J3/1GtxYBxpHtJin1gS3v+UE0MH0K9knB5KUFVPqhskN814VwJ9yt8OYt8rQegkl070 puDNv2UBO/RU0e2Su6DpFO9ovjOgzNKI+7jvhhKj93aG33KTss8i12L0Cx7M2uv4tlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212894; 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=6oqDW4vAUxH3I7e8ByJaKQ6OdqEOOK+wWOWaZxa4T08=; b=0tbE+6RuzZJ9TA/AXxq4fUyBXlVvT9I+o6hxffiR4guTs1ltLnmsQlnuZzIL05eNzRY5 T0VjW2h/MIpbGUIfi9o0zxPZwo9LhJyeKUoBL7doXAW/4XKuJUwBybsg1AYN+HniiwFGR 87labN2bHt8Uo3yJnm9yBDHHv6zw31BWcGr8ECX1W69+EtLzFNnZUT4u+ckda9+ojKVdi LgenIhX50vgAbGIkB9c8IuZ7uYvd+eOPgPVp84/apFM7OdpQQxOQY4tx66B2RVvIOHGWT YlGUKp4XDq43VhJX5SORQEOpZ8M1DHSCDL0jAmrMsB4GYvP1OfoqVhoHUoADOcW8ndw== 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=HnrCX2f7; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 9E44C606A33A for ; Fri, 20 Nov 2026 22:14:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E44C606A33A 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=HnrCX2f7 Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-3854627c1c8so2016915eec.1 for ; Fri, 20 Nov 2026 14:14:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212892; x=1795817692; 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=6oqDW4vAUxH3I7e8ByJaKQ6OdqEOOK+wWOWaZxa4T08=; b=HnrCX2f7Hkk8C5Z5JUfLlyzqFWh9Cb/zOoN6pnxrwHyWK+QsntDf3mVpKrBI0+Va6W A31rSoARlEIaFu4FUoXZLNIDwd3mdSGjqnxSXN6SeFnreItqxDRjXP9rpOJXsQq9LL4O mDgEJ4COWsXUYA/8iQj8/GXFcGYYRpr+97qN87hyPPWByMKRr+K0ZX8z4C3GEY1DEqVr I4Icvg9HhDtqO3qWeAW6zpO09+PbQ87w4nd5u5DZlQ2S6xWggCCikUB9HuXjUdyPadnM HD6LsTXOHJLgsqppLVnTudK/LYdFGEid1u9LxFlFFYXZQQWxJ4xBT9IbeRXvEfH5KwVX v5tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212892; x=1795817692; 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=6oqDW4vAUxH3I7e8ByJaKQ6OdqEOOK+wWOWaZxa4T08=; b=gIQ1NhR//OVD8T2V85YwUYMwTiAjNYT3snQeHOVcLL4/0w9IRsTeU4I0xD6i7pMAnB 5M52MvK1SNYYd61GphiWch+n6TT78ChP835KNqThAL8bNu6IZhg5vVicocXpfztUGKV5 rtJfm+9MRnUNndyPsREWETH0yg7XG47vzN1vti9Vfn+ABZBntIML5VDcCpow3D8wVEWz au+ugpQWsfDveC40UQwZFIwyIWg51OIB64m4UgJ5BQofsohPlo2TqPdk544PmA2jGz3J fC7JU46YGVA2SdgosI8SpHFAXfWy+34822QpYoCt6+oAEm08ukWaokWju7Ri+g+PAmyO UFkQ== X-Forwarded-Encrypted: i=1; AL+hErHia17Mx0eajyzihKncFySgHllkRk2R+Jxh4A3bMy1KErfFxLxlM/9sQvwI+jZfnM/uRVCFlT302SHfcXQ=@vger.kernel.org X-Gm-Message-State: APuU4Z9xKHrat42H18SSGWlWtSyKMkYoPYS8/QQcRAQv2IuqoytT4hQn 7vXAO21AHN6lEP8fCmBE4ULuDtmQUOAsofUhYNj77KxZ6PUVm0f/LiqCDhnu4YZQSkqdv8VOPy0 q/oaGEJqr6Q== X-Received: from dycc17.prod.google.com ([2002:a05:693c:60d1:b0:385:1a45:47b5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:d0a8:b0:386:9957:752f with SMTP id 5a478bee46e88-386995777f4mr756348eec.25.1795212891371; Fri, 20 Nov 2026 14:14:51 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:16 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-16-irogers@google.com> Subject: [PATCH v3 15/30] perf test: Make the CPUID matching test architecture independent 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The arm64 CPUID matching test called strcmp_cpuid_str(), whose arm64 MIDR comparison was only built for arm64. Now that strcmp_cpuid_str() is given the ELF machine of the CPUIDs and all of its comparisons are in util/header.c, the test can run on every architecture. Move the test from arch/arm64/tests to tests, as a "CPUID matching" suite with an "arm64 MIDR" test case. Signed-off-by: Ian Rogers --- tools/perf/arch/arm64/include/arch-tests.h | 1 - tools/perf/arch/arm64/tests/Build | 1 - tools/perf/arch/arm64/tests/arch-tests.c | 4 ---- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/{arch/arm64 =3D> }/tests/cpuid-match.c | 17 +++++++++++++---- tools/perf/tests/tests.h | 1 + 7 files changed, 16 insertions(+), 10 deletions(-) rename tools/perf/{arch/arm64 =3D> }/tests/cpuid-match.c (75%) diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/a= rm64/include/arch-tests.h index 474d7cf5afbd..2b3834a5ddbb 100644 --- a/tools/perf/arch/arm64/include/arch-tests.h +++ b/tools/perf/arch/arm64/include/arch-tests.h @@ -4,7 +4,6 @@ =20 struct test_suite; =20 -int test__cpuid_match(struct test_suite *test, int subtest); extern struct test_suite *arch_tests[]; =20 #endif diff --git a/tools/perf/arch/arm64/tests/Build b/tools/perf/arch/arm64/test= s/Build index d44c9de92d42..bd15c6bbc1b2 100644 --- a/tools/perf/arch/arm64/tests/Build +++ b/tools/perf/arch/arm64/tests/Build @@ -2,4 +2,3 @@ perf-test-y +=3D regs_load.o perf-test-$(CONFIG_DWARF_UNWIND) +=3D dwarf-unwind.o =20 perf-test-y +=3D arch-tests.o -perf-test-y +=3D cpuid-match.o diff --git a/tools/perf/arch/arm64/tests/arch-tests.c b/tools/perf/arch/arm= 64/tests/arch-tests.c index 74932e72c727..ad16b4f8f63e 100644 --- a/tools/perf/arch/arm64/tests/arch-tests.c +++ b/tools/perf/arch/arm64/tests/arch-tests.c @@ -3,13 +3,9 @@ #include "tests/tests.h" #include "arch-tests.h" =20 - -DEFINE_SUITE("arm64 CPUID matching", cpuid_match); - struct test_suite *arch_tests[] =3D { #ifdef HAVE_DWARF_UNWIND_SUPPORT &suite__dwarf_unwind, #endif - &suite__cpuid_match, NULL, }; diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 5eebb2ec5dde..404da9804f48 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -45,6 +45,7 @@ perf-test-y +=3D stat.o perf-test-y +=3D event_update.o perf-test-y +=3D event-times.o perf-test-y +=3D expr.o +perf-test-y +=3D cpuid-match.o perf-test-y +=3D backward-ring-buffer.o perf-test-y +=3D sdt.o perf-test-y +=3D is_printable_array.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 01c78ef20b97..94b54a786992 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -192,6 +192,7 @@ static struct test_suite *generic_tests[] =3D { &suite__util, &suite__subcmd_help, &suite__kallsyms_split, + &suite__cpuid_match, NULL, }; =20 diff --git a/tools/perf/arch/arm64/tests/cpuid-match.c b/tools/perf/tests/c= puid-match.c similarity index 75% rename from tools/perf/arch/arm64/tests/cpuid-match.c rename to tools/perf/tests/cpuid-match.c index b073ac33ea9b..e3d4a349f393 100644 --- a/tools/perf/arch/arm64/tests/cpuid-match.c +++ b/tools/perf/tests/cpuid-match.c @@ -2,12 +2,11 @@ #include #include =20 -#include "arch-tests.h" -#include "tests/tests.h" +#include "tests.h" #include "util/header.h" =20 -int test__cpuid_match(struct test_suite *test __maybe_unused, - int subtest __maybe_unused) +static int test__cpuid_match_arm64(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) { /* midr with no leading zeros matches */ if (strcmp_cpuid_str(EM_AARCH64, "0x410fd0c0", "0x00000000410fd0c0")) @@ -36,3 +35,13 @@ int test__cpuid_match(struct test_suite *test __maybe_un= used, =20 return 0; } + +static struct test_case tests__cpuid_match[] =3D { + TEST_CASE("arm64 MIDR", cpuid_match_arm64), + { .name =3D NULL, } +}; + +struct test_suite suite__cpuid_match =3D { + .desc =3D "CPUID matching", + .test_cases =3D tests__cpuid_match, +}; diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index af822885890d..2306ffb20b57 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -203,6 +203,7 @@ DECLARE_SUITE(util); DECLARE_SUITE(uncore_event_sorting); DECLARE_SUITE(subcmd_help); DECLARE_SUITE(kallsyms_split); +DECLARE_SUITE(cpuid_match); =20 /* * PowerPC and S390 do not support creation of instruction breakpoints usi= ng the --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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 B794D60A2601 for ; Fri, 20 Nov 2026 22:14:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B794D60A2601 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=CXIw89G5 Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso1769802eec.1 for ; Fri, 20 Nov 2026 14:14:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212895; x=1795817695; 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=onQwmUARv02AR/J2lTtspXZDSvOwK37l22aUUc3GeZQ=; b=CXIw89G50moESDbUUboShdOMONskBGnIFJ9w0uoM30vUwj6G0Lha6A7xO2tp31Be95 68WzdDFp9U5xUuon9ptKOUunbnOhNsRjKN5sQ3NfUKvK24wSf4XsTCgfhyKQD9LP2LqL I+gvTyMdj276662yj3rZkMnEFu0oYXqOjfflG+7BlP7sleFxq34DRkft+srCVO4lvGjK k5aA9E6YHyxteXigSlDr1SD0Vl+4gv132FnzBCelyhRUBB7jNJgkp7oQ75tX8+TXGrrW +yP4wkAUmDhCGCq/Yyj0yKt+9ZZ+HhWs+bsxYuZk+yquo3+XjounAwRYTRURH6jJUINi rjtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212895; x=1795817695; 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=onQwmUARv02AR/J2lTtspXZDSvOwK37l22aUUc3GeZQ=; b=z1AEihVYUxRfSiSBEnYc5zxBC+VplHRCfPdpKEMg40NR980O/gaOC4V0sRLFrO6kXd lR+6PUeo66B6+378jybpgfJrl9j6RTzfjWWlXp04iw1Sq9P7YR9OctHw0cXUUI+W/tkr E/aPPkh3l8EVGZ5utoPyGmJFnxEMCsZOJOms0vplorVCSMZLdyovYawUZuWbbizWEKZe 83OonebAXlHQq9CGe1tkljnA9c09lfIByN0ALyWquYHXa3Ki9TUhg57HB4nVzUnALC/R YDNPrU9pn1TlF5tCc4ASwo3nNPI88WD4Z7av+z+L/EedAZiahb8bDvuEKo68bQJKRMHF BHew== X-Forwarded-Encrypted: i=1; AL+hErER9nH+VEX3HdcyqoRr4V2mGq3rpHNiakor9SOXpydYMDUSVwMZ2GjV9DbPsrottq/EtkVb1pIzZZTXuzc=@vger.kernel.org X-Gm-Message-State: APuU4Z+JQu6uIA9/PbKSgbcspFKpcwNfsjER4RfB029LKX2KAqI22Gwa 01mZ/+bD22Z2KIKQfVr8cN/V+e7BBglLMzng1b4vtystafUoN5p7ZsQZM1jwtoENY5rwemX2cAv tqWiruwIZqg== X-Received: from dycw22.prod.google.com ([2002:a05:7300:2156:b0:342:c2e6:20f7]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:e9ce:10b0:385:2564:c509 with SMTP id 5a478bee46e88-385700541a6mr4874375eec.24.1795212894507; Fri, 20 Nov 2026 14:14:54 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:17 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-17-irogers@google.com> Subject: [PATCH v3 16/30] perf test: Add CPUID matching tests for other architectures 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test strcmp_cpuid_str() for the ELF machines other than arm64: exact matching of the test and common tables, x86 stepping specific and stepping optional matching, PowerPC PVR matching, conversion of x86 and PowerPC HEADER_CPUID strings, and regular expression matching for RISC-V and s390 CPUIDs, where RISC-V's hyphens don't imply an x86 stepping. Signed-off-by: Ian Rogers --- tools/perf/tests/cpuid-match.c | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tools/perf/tests/cpuid-match.c b/tools/perf/tests/cpuid-match.c index e3d4a349f393..0231be861ebe 100644 --- a/tools/perf/tests/cpuid-match.c +++ b/tools/perf/tests/cpuid-match.c @@ -36,8 +36,89 @@ static int test__cpuid_match_arm64(struct test_suite *te= st __maybe_unused, return 0; } =20 +static int test__cpuid_match_exact(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + /* The test and common tables are compared exactly. */ + TEST_ASSERT_VAL("same", !strcmp_cpuid_str(EM_NONE, "testcpu", "testcpu")); + TEST_ASSERT_VAL("different", strcmp_cpuid_str(EM_NONE, "common", "testcpu= ")); + TEST_ASSERT_VAL("no regex", strcmp_cpuid_str(EM_NONE, "test.*", "testcpu"= )); + return 0; +} + +static int test__cpuid_match_x86(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + const char *zen3 =3D "AuthenticAMD-25-([245][[:xdigit:]]|[[:xdigit:]])"; + + /* Stepping specific entries. */ + TEST_ASSERT_VAL("stepping match", + !strcmp_cpuid_str(EM_X86_64, "GenuineIntel-6-55-[01234]", + "GenuineIntel-6-55-4")); + TEST_ASSERT_VAL("stepping mismatch", + strcmp_cpuid_str(EM_X86_64, "GenuineIntel-6-55-[56789ABCDEF]", + "GenuineIntel-6-55-4")); + /* The stepping is ignored when the entry isn't stepping specific. */ + TEST_ASSERT_VAL("ignore stepping", + !strcmp_cpuid_str(EM_X86_64, "GenuineIntel-6-(97|9A|B7|BA|BF)", + "GenuineIntel-6-B7-1")); + TEST_ASSERT_VAL("model mismatch", + strcmp_cpuid_str(EM_X86_64, "GenuineIntel-6-(97|9A|B7|BA|BF)", + "GenuineIntel-6-55-4")); + /* HEADER_CPUID is "vendor,family,model,step" in decimal, model 85 is 0x5= 5. */ + TEST_ASSERT_VAL("header stepping match", + !strcmp_cpuid_str(EM_X86_64, "GenuineIntel-6-55-[01234]", + "GenuineIntel,6,85,4")); + TEST_ASSERT_VAL("header ignore stepping", + !strcmp_cpuid_str(EM_X86_64, zen3, "AuthenticAMD,25,33,2")); + /* 32-bit x86 uses the same comparison. */ + TEST_ASSERT_VAL("i386", !strcmp_cpuid_str(EM_386, "GenuineIntel-6-55-[012= 34]", + "GenuineIntel-6-55-4")); + return 0; +} + +static int test__cpuid_match_powerpc(struct test_suite *test __maybe_unuse= d, + int subtest __maybe_unused) +{ + TEST_ASSERT_VAL("pvr match", + !strcmp_cpuid_str(EM_PPC64, "0x004e[[:xdigit:]]{4}", "0x004e0200")); + TEST_ASSERT_VAL("pvr mismatch", + strcmp_cpuid_str(EM_PPC64, "0x0080[[:xdigit:]]{4}", "0x004e0200")); + /* HEADER_CPUID is "pvr_ver,pvr_rev" in decimal. */ + TEST_ASSERT_VAL("header pvr match", + !strcmp_cpuid_str(EM_PPC64, "0x004e[[:xdigit:]]{4}", "78,512")); + TEST_ASSERT_VAL("header pvr mismatch", + strcmp_cpuid_str(EM_PPC64, "0x0080[[:xdigit:]]{4}", "78,512")); + return 0; +} + +static int test__cpuid_match_regex(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + /* Three hyphens in a RISC-V CPUID regex don't imply an x86 stepping. */ + TEST_ASSERT_VAL("riscv match", + !strcmp_cpuid_str(EM_RISCV, + "0x489-0x8000000000000[1-6]08-0x[9b][[:xdigit:]]+", + "0x489-0x8000000000000108-0x91")); + TEST_ASSERT_VAL("riscv mismatch", + strcmp_cpuid_str(EM_RISCV, + "0x489-0x8000000000000[1-6]08-0x[9b][[:xdigit:]]+", + "0x489-0x8000000000000708-0x91")); + TEST_ASSERT_VAL("s390 match", + !strcmp_cpuid_str(EM_S390, "^IBM.209[78].*[13]\\.[1-5].[[:xdigit:]]+$", + "IBM,2097,705,T01,1.3,002f")); + /* The whole CPUID must match. */ + TEST_ASSERT_VAL("partial match", + strcmp_cpuid_str(EM_S390, "IBM,2097", "IBM,2097,705,T01,1.3,002f")); + return 0; +} + static struct test_case tests__cpuid_match[] =3D { TEST_CASE("arm64 MIDR", cpuid_match_arm64), + TEST_CASE("Exact test and common tables", cpuid_match_exact), + TEST_CASE("x86", cpuid_match_x86), + TEST_CASE("PowerPC", cpuid_match_powerpc), + TEST_CASE("Regular expression", cpuid_match_regex), { .name =3D NULL, } }; =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (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 5C78660A2605 for ; Fri, 20 Nov 2026 22:15:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5C78660A2605 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=B6Gh8MFl Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-154e9d2d94fso3150936c88.0 for ; Fri, 20 Nov 2026 14:15:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212899; x=1795817699; 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=E8k+o+ctljOL+bzUAmsIZZdgRgScMxCPCtfkJ2IOmSg=; b=B6Gh8MFloYS3OkJ9Xel03+2QP6ydBx6auuTANtYs7U9AL0Gqq0AYHy48Qpx0mJWS0h wgrThcW0PC+KpqjcFGK8jU6oupk4wH+AuGMV3GPxSUs9aoQvZwwpkTdLpsfC94Hv5XHL dHhCpYnWm6IPY/7/XpS0q7DSGxSGUlbJFbPWolzfEAheuFs037qmlL5eSb/IbvHJXaT+ be7JK2cvMkrUe+yQL19d7zUzZ3crDwSN+mVUksh/hQjcO/rqYrVY9alHY0NgBktwN4Av LiQoYlSYFK1h7FwesvGCl9nHhyB/6OUHTVplIwqxLDCWp7QR2lQAMGPZBM1XdhqV4GcA MF9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212899; x=1795817699; 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=E8k+o+ctljOL+bzUAmsIZZdgRgScMxCPCtfkJ2IOmSg=; b=r9km+1NfJRIBKJLIdEXIwohlJT40xD9NpbfQyAWMDX/VNdviO/4dL+qJJs06j5bzwg 8YLctEAYGowFXnrrJuQOku3BUAa9JWyI7Up5tSdzAkGKjbl+D6TJerZR3lvzFiwz+7jc fw3FEyoPJ7ZfkRsSkW5OQMpfA412itH6NZsseA6QRaah5WhNWA9WT2lPsgHa3JEs5lg9 ClL+scVTc8PWCBTL+c+hImbLot+OvgCAM6lA9jWKB5oElxQ9cSalyiwnXuV+dWuVK+jw Y/zxdhpMgcb2kkwtiXoA1HPLPUfSBSzexjsV49M/vSctWeJU72cPBeFMNjYcpRB4hqjP Uszg== X-Forwarded-Encrypted: i=1; AL+hErG/hWl0BR+uxaQVqO9mUzXP1wlVdFgNoy9Ivl6MjUATZjJRIZ3Q+1uJZbHPiAsQy/b5bbzrDqmAVyGbxKU=@vger.kernel.org X-Gm-Message-State: APuU4Z+lyrDrLNZ0yDP4Jcuf+YX2LDnbe/KQJIt7PCIr4pjMIdntON6X p1aH7RGUrzcfjapGoOO9ARWGPyRO6EzZPRYJL2JQYZuSEMp3MsqqWCuCAAvOAygSKPaYO74CELM blCaY3Z06Sw== X-Received: from dlam22-n1.prod.google.com ([2002:a05:701b:2096:10b0:1b5:345d:ed30]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4555:20b0:190:8f72:445e with SMTP id a92af1059eb24-1b556877915mr4836441c88.11.1795212899053; Fri, 20 Nov 2026 14:14:59 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:18 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-18-irogers@google.com> Subject: [PATCH v3 17/30] perf dwarf-regs: Add e_machine__same_arch() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 32-bit and 64-bit variants of some architectures have different ELF machines, such as EM_386 and EM_X86_64, while others, such as RISC-V, MIPS and PA-RISC, use one ELF machine for both. Code wanting to know if two ELF machines are the same architecture either normalized the ELF machine itself, as in pmu_events_map__e_machine_matches(), or compared the strings from perf_env__arch(). perf_env__arch() maps EM_386 and EM_X86_64 to "x86", but distinguishes "riscv" and "riscv64", "mips" and "mips64", and "parisc" and "parisc64" by whether the kernel is 64-bit, and computing it requires string lookups. Add e_machine__64bit(), giving the ELF machine of the 64-bit variant of an architecture, and e_machine__same_arch() next to EM_HOST. EM_ARM and EM_AARCH64 remain distinct architectures. Use them when matching pmu-events tables and in is_pmu_core_len(), which compares a perf_env's architecture with the host's. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 6 +--- tools/perf/pmu-events/jevents.py | 6 +--- tools/perf/util/include/dwarf-regs.h | 37 ++++++++++++++++++++++++ tools/perf/util/pmu.c | 5 +++- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 872c6ca6e817..3870356485ef 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -5783,11 +5783,7 @@ int pmu_metrics_table__find_metric(const struct pmu_= metrics_table *table, static bool pmu_events_map__e_machine_matches(const struct pmu_events_map = *map, uint16_t e_machine) { - if (e_machine =3D=3D EM_386) - e_machine =3D EM_X86_64; - else if (e_machine =3D=3D EM_PPC) - e_machine =3D EM_PPC64; - return map->e_machine =3D=3D e_machine; + return e_machine__same_arch(map->e_machine, e_machine); } =20 static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 802ae2ad4405..6adb385b38e5 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -1201,11 +1201,7 @@ int pmu_metrics_table__find_metric(const struct pmu_= metrics_table *table, static bool pmu_events_map__e_machine_matches(const struct pmu_events_map = *map, \t\t\t\t\t uint16_t e_machine) { -\tif (e_machine =3D=3D EM_386) -\t\te_machine =3D EM_X86_64; -\telse if (e_machine =3D=3D EM_PPC) -\t\te_machine =3D EM_PPC64; -\treturn map->e_machine =3D=3D e_machine; +\treturn e_machine__same_arch(map->e_machine, e_machine); } =20 static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) diff --git a/tools/perf/util/include/dwarf-regs.h b/tools/perf/util/include= /dwarf-regs.h index 1da915c2683a..07f285a94e03 100644 --- a/tools/perf/util/include/dwarf-regs.h +++ b/tools/perf/util/include/dwarf-regs.h @@ -85,6 +85,43 @@ #define EF_HOST 0 #endif =20 +/** + * e_machine__64bit() - The ELF machine of the 64-bit variant of an archit= ecture. + * @e_machine: An ELF machine, such as EM_386. + * + * The 32-bit and 64-bit variants of some architectures have different ELF + * machines, for example EM_386 and EM_X86_64, whereas others, like RISC-V, + * MIPS and PA-RISC, use the same ELF machine for both. EM_ARM and EM_AARC= H64 + * are considered distinct architectures. + * + * Return: The 64-bit ELF machine, or @e_machine if there isn't one. + */ +static inline uint16_t e_machine__64bit(uint16_t e_machine) +{ + switch (e_machine) { + case EM_386: + return EM_X86_64; + case EM_PPC: + return EM_PPC64; + case EM_SPARC: + case EM_SPARC32PLUS: + return EM_SPARCV9; + default: + return e_machine; + } +} + +/** + * e_machine__same_arch() - Are two ELF machines the same architecture, + * ignoring differences in word size? + * @a: An ELF machine. + * @b: Another ELF machine. + */ +static inline bool e_machine__same_arch(uint16_t a, uint16_t b) +{ + return e_machine__64bit(a) =3D=3D e_machine__64bit(b); +} + #define DWARF_REG_PC 0xd3af9c /* random number */ #define DWARF_REG_FB 0xd3affb /* random number */ =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index fc4fa34fcb0d..d319ef8ffbb8 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -34,6 +34,8 @@ #include "print-events.h" #include "hashmap.h" #include "header.h" +#include "dwarf-regs.h" +#include "env.h" #include "string2.h" #include "strbuf.h" #include "fncache.h" @@ -2101,7 +2103,8 @@ bool is_pmu_core_len(struct perf_env *env, const char= *name, size_t len) * isn't portable, for example, big.LITTLE ARM and Apple core PMUs are * named after their CPU rather than "cpu_core" and "cpu_atom". */ - if (env && strcmp(perf_env__arch(env), perf_env__arch(/*env=3D*/NULL))) { + if (env && !e_machine__same_arch(perf_env__e_machine(env, /*e_flags=3D*/N= ULL), + perf_env__e_machine(host_env(), /*e_flags=3D*/NULL))) { pr_debug("Can't identify core PMUs of a %s perf.data file recorded witho= ut hybrid topology\n", perf_env__arch(env)); return false; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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=1795212909; b=k9UGZ1TxDIYzGMp51ugFqfV/WjUML+soR+5ZUcjKdLowwE6NKLrjMp3UD3COSojIOP5R railyjChIygvNgTQ/02vdv1ivZQbukVCWLiXtjRym6l8l+4WfXMy600WB3DOGJUL9nura nK5NJcdHqnYGjDY/wgV6qi8QxKIowHhREfPbxpNRqDWOuAPo1eAzs6IsGv0b+FKaHUYZT JcLjvFThYRFZJje0LJ9fnSOZrOGerb/pnqgp1wtZsBMaiGCNDQlQGOuqgNn8A7KJ/xlGj ntLyss2JoGKoyrF3zy+9mxAHsBN49647rmv68kKy9FlpmlCkhW3w69oI/WR4Ey/7uNQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212909; 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=u3apUpuScIs0ycHDErWWJzNI6wCQ/fKpNWRrmrwxM94=; b=pM92gCs3SlRHUvY0aJK5ffx2OZDal1OsUhAFeeZHA0xMDiSTssd5m3n8DnLz5Ma8gf5Z AC3RVz7uOr40Dp6OeAg5o/6h1XfJzCAjGtOrsrT7eosgdj7tL7aqsPMwX/juDnX2r3EWH iByvBw+iO63d6FPO4kyB9swwER3ExkpXYO00rIeJD5YCzGZRXR77abgnnvVWe7QMNDGT+ rJACbaZqjupuHeJjRTRXxKaRVUnvEMbUNRjEPWc7+PVYTL9m5HfIvhQLRQU+K2pvOWVOE poKIk9brXlv/tnmlWt+2bBqXwiuhngYnuFCuGOOOb5L2CC8swFe0IEN/zcKFCNf+wUQ== 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=G2yRsBY0; 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 843FD606A69B for ; Fri, 20 Nov 2026 22:15:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 843FD606A69B 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=G2yRsBY0 Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-382af5cfc58so3819423eec.0 for ; Fri, 20 Nov 2026 14:15:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212905; x=1795817705; 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=u3apUpuScIs0ycHDErWWJzNI6wCQ/fKpNWRrmrwxM94=; b=G2yRsBY0b7thejxXtqDRyLTkzOL0s4D9hd5TtFAkvfy5LRFsFjOvcsi8+9CPE1X99T 1CVZLBMNmMhjHSJrR/wG1IgwyRSP/5MQ6YoE/idP4eRO7Rd7eBmQQgP1E7n1nEH4hk4O ngQPHrAexsQe3gvx+7ImVnZPC77g74DeIlPokp4mzgvKEHHBhvMO3y+L4dIJH1YcKWHb Xh3c8aftS7hDfPyn1Ykh8BzZMbhEJHqMUMIv/w8XvAo7wHdQIpNW5aekf6984ddKwkL4 nUW5msF37PWJgqXZAj67I+k5S+hBhHhX1/Jtj94amiHVEyLeZB40OrrK2WKXLtE1fJn0 M7oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212905; x=1795817705; 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=u3apUpuScIs0ycHDErWWJzNI6wCQ/fKpNWRrmrwxM94=; b=W8foQ9Z7GYhFjQAOybxXmxX4vwFfkyy2mAEj36N7xoPPln5oc/a1+bOXhvWGDydtgW Gu4bxHwJMK7TQ3t2qyUzKIIhm6T03OjiDjZMoxczMkhuSiNhQXj6tXhuOZFJImeBDelq llRfrAHO4u6QhtoAmdSsHsoe52IXIhHaXUNlSHB1hZmkhzFiX9ai/Gkklb+Ady8F1dwC hTkuccM+vPNgrNmW9/t1g891RjIlR3VEc/DP96ZHEyPF9PiayOQJ16wUEQYYnfYPo4L5 cJIJaDQbeWN5okWkoWIarubT3ood1DQYLVMR9k8BpK7NV9pNhaB8nvGpABzmWGlv8CwA 1oIg== X-Forwarded-Encrypted: i=1; AL+hErHNHrHOBoO+Lsm4yyhFrxtLd61jc7kIu1BqXFdXXflLbu73TOUjfFK0Ope884J4GKYS36x0Z0eR6yTi5FE=@vger.kernel.org X-Gm-Message-State: APuU4Z87Xcz0WipaJ28SCQ7FZnjf26zffnVpkHvvKNW9er1kuIC7MS47 5KY+cgD8KdNxaZxGIjCzD5H92k7fL4I267ZxoIL0kj4ormqVkVJbGVP1WKU52AT1UUCJN2jxSKl uK44vt0q5DA== X-Received: from dycue7.prod.google.com ([2002:a05:7300:87:b0:386:46f:406]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:f283:b0:386:9854:9123 with SMTP id 5a478bee46e88-3869854950emr1174215eec.14.1795212905000; Fri, 20 Nov 2026 14:15:05 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:19 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-19-irogers@google.com> Subject: [PATCH v3 18/30] perf metricgroup: Pass struct perf_pmus to metricgroup__for_each_metric() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" metricgroup__sys_event_iter() matched system metrics against uncore PMUs using the live host's PMUs, so when processing a perf.data file from another machine system metrics were matched against the wrong PMU topology. Add a struct perf_pmus * argument to metricgroup__for_each_metric() and use it to match the metric's compat identifier. Callers pass the PMUs of the environment they are working on. Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 4 +++- tools/perf/util/metricgroup.c | 12 +++++++----- tools/perf/util/metricgroup.h | 14 ++++++++++++-- tools/perf/util/print-events.c | 3 ++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 97bad639753f..f854eadb46eb 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2523,6 +2523,7 @@ static void perf_sample__fprint_metric(struct thread = *thread, =20 if (!init_metrics) { /* One time initialization of stat_config and metric data. */ + struct perf_env *env =3D evlist__env(evsel->evlist) ?: host_env(); struct script_find_metrics_args args =3D { .evlist =3D evsel->evlist, .system_wide =3D perf_thread_map__pid(evsel->core.threads, /*idx=3D*/0)= =3D=3D -1, @@ -2541,7 +2542,8 @@ static void perf_sample__fprint_metric(struct thread = *thread, /*needs_sort=3D*/false); } =20 - metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metr= ics, &args); + metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_table__fin= d(), + script_find_metrics, &args); init_metrics =3D true; } =20 diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 80c2086555a8..fba02619ffb5 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -406,6 +406,7 @@ static bool match_pm_metric_or_groups(struct perf_pmus = *pmus, const struct pmu_m } =20 struct metricgroup_iter_data { + struct perf_pmus *pmus; pmu_metric_iter_fn fn; void *data; }; @@ -421,14 +422,15 @@ static int metricgroup__sys_event_iter(const struct p= mu_metric *pm, return 0; =20 /* Only process with the iterator if there is a a PMU that matches the ID= . */ - pmu =3D perf_pmus__scan_for_uncore_id(perf_env__pmus(host_env()), pmu, pm= ->compat); + pmu =3D perf_pmus__scan_for_uncore_id(d->pmus, pmu, pm->compat); return pmu ? d->fn(pm, table, d->data) : 0; } =20 -int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pm= u_metric_iter_fn fn, - void *data) +int metricgroup__for_each_metric(struct perf_pmus *pmus, const struct pmu_= metrics_table *table, + pmu_metric_iter_fn fn, void *data) { struct metricgroup_iter_data sys_data =3D { + .pmus =3D pmus, .fn =3D fn, .data =3D data, }; @@ -1118,7 +1120,7 @@ static int metricgroup__add_metric(struct perf_pmus *= pmus, const char *pmu, * Iterate over all metrics seeing if metric matches either the * name or group. When it does add the metric to the list. */ - ret =3D metricgroup__for_each_metric(table, metricgroup__add_metric_callb= ack, &data); + ret =3D metricgroup__for_each_metric(pmus, table, metricgroup__add_metric= _callback, &data); if (!ret && !data.has_match) ret =3D -ENOENT; =20 @@ -1656,7 +1658,7 @@ bool metricgroup__has_metric_or_groups(struct perf_pm= us *pmus, const char *pmu, .metric_or_groups =3D metric_or_groups, }; =20 - return metricgroup__for_each_metric(table, + return metricgroup__for_each_metric(pmus, table, metricgroup__has_metric_or_groups_callback, &data) ? true : false; diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index bc5d3d21f405..2cc28b3eb983 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -85,8 +85,18 @@ int metricgroup__parse_groups_test(struct evlist *evlist, const char *str, bool cputype_filter); =20 -int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pm= u_metric_iter_fn fn, - void *data); +/** + * metricgroup__for_each_metric() - Iterate over metrics in @table and sys= tem + * metrics matching @pmus. + * @pmus: The PMUs used to match system metric compat identifiers. + * @table: Optional CPU metrics table to iterate over. + * @fn: Callback invoked for each metric. + * @data: User data passed to @fn. + * + * Return: 0 on success or the non-zero return value from @fn. + */ +int metricgroup__for_each_metric(struct perf_pmus *pmus, const struct pmu_= metrics_table *table, + pmu_metric_iter_fn fn, void *data); bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char = *pmu, const char *metric_or_groups); unsigned int metricgroups__topdown_max_level(void); diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index fe48efe9aefb..6145367d2184 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -297,7 +297,8 @@ void metricgroup__print(const struct print_callbacks *p= rint_cb, void *print_stat groups.node_cmp =3D mep_cmp; groups.node_delete =3D mep_delete; =20 - metricgroup__for_each_metric(table, metricgroup__add_to_mep_groups_callba= ck, &groups); + metricgroup__for_each_metric(perf_env__pmus(host_env()), table, + metricgroup__add_to_mep_groups_callback, &groups); =20 for (node =3D rb_first_cached(&groups.entries); node; node =3D next) { struct mep *me =3D container_of(node, struct mep, nd); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212913; b=ko1lCIhNWkO/a/BXBdhyUAMvNfTdSXlp9vnJjC1QvYebqWg2MVrQFX0JCbELDmdabeNV 7kw+OS5PUZJWT+V1PLIaramjzDwoiCK3pXWhCOMgM4OZzH4lpPCwODldIRv5HqHoNJsxt /ATsHQloqpqieHVp/iAxtlWGSJ73riEZub75wYhg0xh88PgQasrk0ccClG8R/2wOc+pmu 7mhOtDlVN9Z65mL3QHjT0YAbfRqApjjYuwnocih0ZZFNbVFgXw/S6X3sdG6P1hIBfSk3d QYqgzm1E+02AKLKdApOGjICExcaX9XdY6uCjMF11Omfh2gbrrYETsQeSx1rWsYvASFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212913; 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=oODa4KiRYyG20hnc0cck5wmWZBwnaiGT90/lewPb5Ng=; b=ZpY2pMXNcULGWlW0THtM3yCySpFC/IQKFw6Q2b8VNMcSQ1TnACtEkQ6+XnFMUTcXMI8p +KY9oXBcfWSCNkoxbL4UBV0DDIP75e0aJ0yXu2l05yQk3pL2eVj+HP2a1t91nY752fSlP cO94Cnydf2o/uaIW1GbOMXJcK+wMuZE2VkTfRZFwwXD/3CJ6GW1QLxodKF38moQLA7vrM xlN7eu+KpmgUTcaX5dKCwA+RJhcfC/t22BlMrJ0Qcc03iiMrsCXyuVfU6EwVH5DekKh+m 03vSQZCwvokKWWwqh9tNNsrxpLT12vqR+wC14Yip0rccneIgh75ItwM3MAPEeesm8oA== 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=UaKI8E+X; arc=none smtp.remote-ip=209.85.215.200 Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (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 12D49606A6B2 for ; Fri, 20 Nov 2026 22:15:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12D49606A6B2 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=UaKI8E+X Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-cde00369ef5so950013a12.3 for ; Fri, 20 Nov 2026 14:15:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212910; x=1795817710; 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=oODa4KiRYyG20hnc0cck5wmWZBwnaiGT90/lewPb5Ng=; b=UaKI8E+XXv6bQK+52Z15+q0SsEEd6ymPwBswV2FutIkRCI8R7Oa5T6TkulGZWQ07Dl aOov3REtYT6x9yY7pDJU506LBUd1Ez/PbZtGHpXTE4+ROkSb1MvElKe/J/lhYVLnseZ5 mBgNbAG+fHqcIgQsMAm1qBknQ/zd94F9JMgK4wV5j3Vet/pOvTPoDF3mM88vDh6KEs+P 32GTabCz83fG9kGGPsa/rOmpb2FX3kyRlKKEdlNfr0B9mNm3cOERx4BRByIGqVKaa/aW 9op3AwQVt7iRWGUXJRrSe4im7Dnqcc38tl3eW8Xe59IeKu3E6ya4GkKwFPQclaiPs5pY 3Rhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212910; x=1795817710; 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=oODa4KiRYyG20hnc0cck5wmWZBwnaiGT90/lewPb5Ng=; b=CbZXmWxxUfU7/6PqdsJZT2YltVsUzKKcuEQX0Qcy5VWlDJUb8H06utoBarfvCUcU1B D1RDEI6X8iLKi4MFb/t6lHw5nHBqTBoyJae3EtXavhKwhppqm+SLfCPNzHgU5wZbUDrL cbtxxDjlC4jYd1K+LaJhhnxi9Rkk8Kc5BZTO//+XSD+f9jscapuuRe6qH4Y17fDRKmsw RWYOArwKp0dU2jZAocowQXIOUwH/IdlJolNY6BC/4Jva0m859gEAX0cRfkgzU0ikbAJ7 qyiXB04rUKG8bRf9RstZEp0Zg+YgsRosTiEltcais7Hgkobj2m6oOqrVEPuKSi7VfAC/ 3Yxg== X-Forwarded-Encrypted: i=1; AL+hErFrx+kDItV4XK9BLJlTTzb0R88x7rTrjS0KOv4BtWir+8gnxgeWDxs/0SCPHmGeNCxh0zDMz2ZZs59z2L8=@vger.kernel.org X-Gm-Message-State: APuU4Z/iM0uFuYD1W77saVSXm1XsBbcWA90NPGb0Si02honc287duSgz MiIoLk+jfgaWfWVC3I5fv1SCq/dhv0ezmindmgD1C3AwywRQlQTKTgLv1qbkqh7JSYgzU99Tk20 IOb+IEZM//g== X-Received: from pgme4-n2.prod.google.com ([2002:a05:6a02:6c84:20b0:cda:d173:c131]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6f07:b0:3e2:168b:d317 with SMTP id adf61e73a8af0-4014e2545fcmr4885746637.44.1795212909740; Fri, 20 Nov 2026 14:15:09 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:20 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-20-irogers@google.com> Subject: [PATCH v3 19/30] perf metricgroup: Move matching of metrics to recorded events from perf script 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf script finds metrics whose events are all present in the perf.data file's evlist so that they can be displayed alongside samples. Move this code into metricgroup.c as metricgroup__add_matching_metrics() so that it can be shared with other commands, such as perf stat report. No functional change. Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 156 +-------------------------------- tools/perf/util/metricgroup.c | 157 ++++++++++++++++++++++++++++++++++ tools/perf/util/metricgroup.h | 12 +++ 3 files changed, 172 insertions(+), 153 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index f854eadb46eb..afed2353ef80 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2350,151 +2350,6 @@ static void script_new_line(struct perf_stat_config= *config __maybe_unused, /* Each metric is printed on its own line by script_print_metric(). */ } =20 -struct script_find_metrics_args { - struct evlist *evlist; - bool system_wide; -}; - -static struct evsel *map_metric_evsel_to_script_evsel(struct evlist *scrip= t_evlist, - struct evsel *metric_evsel) -{ - struct evsel *script_evsel; - - evlist__for_each_entry(script_evlist, script_evsel) { - /* Skip if perf_event_attr differ. */ - if (metric_evsel->core.attr.type !=3D script_evsel->core.attr.type) - continue; - if (metric_evsel->core.attr.config !=3D script_evsel->core.attr.config) - continue; - /* Skip if the script event has a metric_id that doesn't match. */ - if (script_evsel->metric_id && - strcmp(evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel= ))) { - pr_debug("Skipping matching evsel due to differing metric ids '%s' vs '= %s'\n", - evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel)); - continue; - } - return script_evsel; - } - return NULL; -} - -static int script_find_metrics(const struct pmu_metric *pm, - const struct pmu_metrics_table *table __maybe_unused, - void *data) -{ - struct script_find_metrics_args *args =3D data; - struct evlist *script_evlist =3D args->evlist; - struct evlist *metric_evlist =3D evlist__new(); - struct evsel *metric_evsel; - int ret =3D metricgroup__parse_groups(metric_evlist, - /*pmu=3D*/"all", - /*cputype_filter=3D*/false, - pm->metric_name, - /*metric_no_group=3D*/false, - /*metric_no_merge=3D*/false, - /*metric_no_threshold=3D*/true, - /*user_requested_cpu_list=3D*/NULL, - args->system_wide, - /*hardware_aware_grouping=3D*/false); - - if (ret) { - /* Metric parsing failed but continue the search. */ - goto out; - } - - /* - * Check the script_evlist has an entry for each metric_evlist entry. If - * the script evsel was already set up avoid changing data that may - * break it. - */ - evlist__for_each_entry(metric_evlist, metric_evsel) { - struct evsel *script_evsel =3D - map_metric_evsel_to_script_evsel(script_evlist, metric_evsel); - struct evsel *new_metric_leader; - - if (!script_evsel) { - pr_debug("Skipping metric '%s' as evsel '%s' / '%s' is missing\n", - pm->metric_name, evsel__name(metric_evsel), - evsel__metric_id(metric_evsel)); - goto out; - } - - if (script_evsel->metric_leader =3D=3D NULL) - continue; - - if (metric_evsel->metric_leader =3D=3D metric_evsel) { - new_metric_leader =3D script_evsel; - } else { - new_metric_leader =3D - map_metric_evsel_to_script_evsel(script_evlist, - metric_evsel->metric_leader); - } - /* Mismatching evsel leaders. */ - if (script_evsel->metric_leader !=3D new_metric_leader) { - pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders = '%s' vs '%s'\n", - pm->metric_name, evsel__metric_id(metric_evsel), - evsel__metric_id(script_evsel)); - goto out; - } - } - /* - * Metric events match those in the script evlist, copy metric evsel - * data into the script evlist. - */ - evlist__for_each_entry(metric_evlist, metric_evsel) { - struct evsel *script_evsel =3D - map_metric_evsel_to_script_evsel(script_evlist, metric_evsel); - struct metric_event *metric_me =3D - metricgroup__lookup(evlist__metric_events(metric_evlist), - metric_evsel, - /*create=3D*/false); - - if (script_evsel->metric_id =3D=3D NULL) { - script_evsel->metric_id =3D metric_evsel->metric_id; - metric_evsel->metric_id =3D NULL; - } - - if (script_evsel->metric_leader =3D=3D NULL) { - if (metric_evsel->metric_leader =3D=3D metric_evsel) { - script_evsel->metric_leader =3D script_evsel; - } else { - script_evsel->metric_leader =3D - map_metric_evsel_to_script_evsel(script_evlist, - metric_evsel->metric_leader); - } - } - - if (metric_me) { - struct metric_expr *expr; - struct metric_event *script_me =3D - metricgroup__lookup(evlist__metric_events(script_evlist), - script_evsel, - /*create=3D*/true); - - if (!script_me) { - /* - * As the metric_expr is created, the only - * failure is a lack of memory. - */ - goto out; - } - list_splice_init(&metric_me->head, &script_me->head); - list_for_each_entry(expr, &script_me->head, nd) { - for (int i =3D 0; expr->metric_events[i]; i++) { - expr->metric_events[i] =3D - map_metric_evsel_to_script_evsel(script_evlist, - expr->metric_events[i]); - } - } - } - } - pr_debug("Found metric '%s' whose evsels match those of in the perf data\= n", - pm->metric_name); -out: - evlist__put(metric_evlist); - return 0; -} - static struct aggr_cpu_id script_aggr_cpu_id_get(struct perf_stat_config *= config __maybe_unused, struct perf_cpu cpu) { @@ -2523,12 +2378,6 @@ static void perf_sample__fprint_metric(struct thread= *thread, =20 if (!init_metrics) { /* One time initialization of stat_config and metric data. */ - struct perf_env *env =3D evlist__env(evsel->evlist) ?: host_env(); - struct script_find_metrics_args args =3D { - .evlist =3D evsel->evlist, - .system_wide =3D perf_thread_map__pid(evsel->core.threads, /*idx=3D*/0)= =3D=3D -1, - - }; if (!stat_config.output) stat_config.output =3D stdout; =20 @@ -2542,8 +2391,9 @@ static void perf_sample__fprint_metric(struct thread = *thread, /*needs_sort=3D*/false); } =20 - metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_table__fin= d(), - script_find_metrics, &args); + metricgroup__add_matching_metrics(evsel->evlist, + perf_thread_map__pid(evsel->core.threads, + /*idx=3D*/0) =3D=3D -1); init_metrics =3D true; } =20 diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index fba02619ffb5..9922445a6d15 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -453,6 +453,163 @@ int metricgroup__for_each_metric(struct perf_pmus *pm= us, const struct pmu_metric return pmu_for_each_sys_metric(metricgroup__sys_event_iter, &sys_data); } =20 +struct find_matching_metrics_args { + struct evlist *evlist; + bool system_wide; +}; + +static struct evsel *map_metric_evsel_to_evlist(struct evlist *data_evlist, + struct evsel *metric_evsel) +{ + struct evsel *data_evsel; + + evlist__for_each_entry(data_evlist, data_evsel) { + /* Skip if perf_event_attr differ. */ + if (metric_evsel->core.attr.type !=3D data_evsel->core.attr.type) + continue; + if (metric_evsel->core.attr.config !=3D data_evsel->core.attr.config) + continue; + /* Skip if the data event has a metric_id that doesn't match. */ + if (data_evsel->metric_id && + strcmp(evsel__metric_id(metric_evsel), evsel__metric_id(data_evsel))= ) { + pr_debug("Skipping matching evsel due to differing metric ids '%s' vs '= %s'\n", + evsel__metric_id(metric_evsel), evsel__metric_id(data_evsel)); + continue; + } + return data_evsel; + } + return NULL; +} + +static int find_matching_metrics_cb(const struct pmu_metric *pm, + const struct pmu_metrics_table *table __maybe_unused, + void *data) +{ + struct find_matching_metrics_args *args =3D data; + struct evlist *data_evlist =3D args->evlist; + struct evlist *metric_evlist =3D evlist__new(); + struct evsel *metric_evsel; + int ret =3D metricgroup__parse_groups(metric_evlist, + /*pmu=3D*/"all", + /*cputype_filter=3D*/false, + pm->metric_name, + /*metric_no_group=3D*/false, + /*metric_no_merge=3D*/false, + /*metric_no_threshold=3D*/true, + /*user_requested_cpu_list=3D*/NULL, + args->system_wide, + /*hardware_aware_grouping=3D*/false); + + if (ret) { + /* Metric parsing failed but continue the search. */ + goto out; + } + + /* + * Check the data_evlist has an entry for each metric_evlist entry. If + * the data evsel was already set up avoid changing data that may + * break it. + */ + evlist__for_each_entry(metric_evlist, metric_evsel) { + struct evsel *data_evsel =3D + map_metric_evsel_to_evlist(data_evlist, metric_evsel); + struct evsel *new_metric_leader; + + if (!data_evsel) { + pr_debug("Skipping metric '%s' as evsel '%s' / '%s' is missing\n", + pm->metric_name, evsel__name(metric_evsel), + evsel__metric_id(metric_evsel)); + goto out; + } + + if (data_evsel->metric_leader =3D=3D NULL) + continue; + + if (metric_evsel->metric_leader =3D=3D metric_evsel) { + new_metric_leader =3D data_evsel; + } else { + new_metric_leader =3D + map_metric_evsel_to_evlist(data_evlist, + metric_evsel->metric_leader); + } + /* Mismatching evsel leaders. */ + if (data_evsel->metric_leader !=3D new_metric_leader) { + pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders = '%s' vs '%s'\n", + pm->metric_name, evsel__metric_id(metric_evsel), + evsel__metric_id(data_evsel)); + goto out; + } + } + /* + * Metric events match those in the data evlist, copy metric evsel + * data into the data evlist. + */ + evlist__for_each_entry(metric_evlist, metric_evsel) { + struct evsel *data_evsel =3D + map_metric_evsel_to_evlist(data_evlist, metric_evsel); + struct metric_event *metric_me =3D + metricgroup__lookup(evlist__metric_events(metric_evlist), + metric_evsel, + /*create=3D*/false); + + if (data_evsel->metric_id =3D=3D NULL) { + data_evsel->metric_id =3D metric_evsel->metric_id; + metric_evsel->metric_id =3D NULL; + } + + if (data_evsel->metric_leader =3D=3D NULL) { + if (metric_evsel->metric_leader =3D=3D metric_evsel) { + data_evsel->metric_leader =3D data_evsel; + } else { + data_evsel->metric_leader =3D + map_metric_evsel_to_evlist(data_evlist, + metric_evsel->metric_leader); + } + } + + if (metric_me) { + struct metric_expr *expr; + struct metric_event *data_me =3D + metricgroup__lookup(evlist__metric_events(data_evlist), + data_evsel, + /*create=3D*/true); + + if (!data_me) { + /* + * As the metric_expr is created, the only + * failure is a lack of memory. + */ + goto out; + } + list_splice_init(&metric_me->head, &data_me->head); + list_for_each_entry(expr, &data_me->head, nd) { + for (int i =3D 0; expr->metric_events[i]; i++) { + expr->metric_events[i] =3D + map_metric_evsel_to_evlist(data_evlist, + expr->metric_events[i]); + } + } + } + } + pr_debug("Found metric '%s' whose evsels match those of in the perf data\= n", + pm->metric_name); +out: + evlist__put(metric_evlist); + return 0; +} + +int metricgroup__add_matching_metrics(struct evlist *evlist, bool system_w= ide) +{ + struct perf_env *env =3D evlist__env(evlist) ?: host_env(); + struct find_matching_metrics_args args =3D { + .evlist =3D evlist, + .system_wide =3D system_wide, + }; + + return metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_tabl= e__find(), + find_matching_metrics_cb, &args); +} + static const char *code_characters =3D ",-=3D@"; =20 static int encode_metric_id(struct strbuf *sb, const char *x) diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index 2cc28b3eb983..2069211913dc 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -97,6 +97,18 @@ int metricgroup__parse_groups_test(struct evlist *evlist, */ int metricgroup__for_each_metric(struct perf_pmus *pmus, const struct pmu_= metrics_table *table, pmu_metric_iter_fn fn, void *data); +/** + * metricgroup__add_matching_metrics() - Add metrics whose events are in @= evlist. + * @evlist: The evlist, typically read from a perf.data file, to add metri= cs to. + * @system_wide: Are the events in @evlist system wide? + * + * For every metric whose events all have a corresponding evsel in @evlist, + * add the metric to the metric events of @evlist so that it is computed a= nd + * displayed along with the counts of the evsels. + * + * Return: 0 on success or a negative error code. + */ +int metricgroup__add_matching_metrics(struct evlist *evlist, bool system_w= ide); bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char = *pmu, const char *metric_or_groups); unsigned int metricgroups__topdown_max_level(void); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212926; b=Vvt1jY0pgRkwqDyZoa6BKN7EAWO0KP5pzWlowUMfu7qY6MAuvDCSNR7OaJuzhW+EOu5k xEzrhUEXSZjsQCFblyKnF+iUapioHmfDU2EzaWRZmKa6hnEyo69Afr6xnV6fkNZrCWJVL sam7U1pHUtxzElVa3NZz40dlt2jFJYXOpHpPF2G+N+x72tVCsYkSKDi681NSdAAcO3A0Q GfLy0+WHxctxnm41GLzbqICVZVpssyvW0totBbwDrv83hqdOJGYOhXU4CwCEaykDqSMLK M5/N7DZg8sj4cEThr7MD2CmvAlqNJyt5oQc/W7TJww+7VO0OgdXbdKPPtVTWepjWSVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212926; 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=dMnEbDK8wYGDtn9mWnEYq0107+ljqwGa7pSjkv3/TiM=; b=fukjMjmv5UNWJPtXk3X8UZj4PPRB6NcKTnELHsNMs4bvGPdseA3LpEOIuZI8LorxgzYn vqHoYoMrtQBJPQckmPNawpbD8qmMHfUhgrhNO+aNs5iATuR06BFmVY/wie9ItnlRoRRpF NR+ca9cUegJJS2FcyvMh2FdlzeIxcwcw2wIPNohMdXNhmnezN/IOAFd7aVoK5MlcEHLC4 spTwaB40D0HMk6+/xUTf4D3KJMlChNkteD5DCc8LpenpZlBDf7xtNsNqxIt4g48c8EAQa R0HE1K5CCFE2eGOP1B2nVqcCZRD1+i5qD/nFVpjaC213pY1V19ymNef2XcjK3J4bXpg== 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=Yg981a8O; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (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 33BCD606A6BC for ; Fri, 20 Nov 2026 22:15:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 33BCD606A6BC 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=Yg981a8O Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2f05c68629cso3459455ad.1 for ; Fri, 20 Nov 2026 14:15:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212917; x=1795817717; 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=dMnEbDK8wYGDtn9mWnEYq0107+ljqwGa7pSjkv3/TiM=; b=Yg981a8OOKrMjZWrsbdLuIjydDY4TdFSNTo1mnkhm0FEyh63ZVWkUGTXVdcHcG1XyH nRz8tk8JvYb8jXz1L/bs8vKcMlZEsiCd3t7AY0PqsgWcb77L+QL6fs1tCg4erJvmixeP LIoRbkLEY63mkbMY5RgyAErC1sO62PO7nbXNwXhqf4RuXLI17mbDbVregA9DC6Db2X1v u8ZVFvUgw4gjPEk9gsIszgw7c7C/I8Xq/s3oQjLLiJEe1Ga7YzYkorv3YcmdWpl/5Gpv UORPFOzz9I1VUc9fVW1+W0uson5dgpW2y0nMUBtkU2ddH6qw5cA2n0FHJZH+o3zTTjgU G2gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212917; x=1795817717; 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=dMnEbDK8wYGDtn9mWnEYq0107+ljqwGa7pSjkv3/TiM=; b=FkbuNu3r4pvVZ21P3c2APa8bxnFUTfLtZyZbj1t3vM8wEG9eQChZafR+LDWNLyLZtz TAU/iHmL7dJ4E9H2UV4NWJkCGj+xv3/sFhkzIaMVqr85KHrpWhE3N2PYx3j0J2p7ukW1 Ps+b/MGcqp8fnb5+xnJ3L/Yqmh6nH6GMTijH3Nmai7e3rYXnE/+wbPgkkJ61axePHe8G mDCtwjUQ32H9lRgjy4jOXjirYke64mhaJ6w6KBqbBkvLI8bPO/cSYJBrc/eL+j37gFpj rT9KmvFKZvCL3ih8DnEdbRtdTQgKBi5lx8s2PyPqDj734CQ5U/ri5YWiUgv2Oh7w7l/F LShw== X-Forwarded-Encrypted: i=1; AL+hErFTEmyUER8CWUUzqTNirdsUlutIy71XoiS4n7UVOjwtaSKZ7N1HuBi1tntlHivtngNSagoP2v3xWhbaGlg=@vger.kernel.org X-Gm-Message-State: APuU4Z9K5Vnt1DAIZ5ECNSNd2Yx0PG73lFs+4H+iymvBTneTEIWIclmq 7V2kdGdtdTXOTUvpdOKdajgrAGqMPLu/8BkBt88LIU2BdE+ZALkEg8xqA6pMIqoEIV/WlgjdZqs KYixXTtyCnw== X-Received: from pjte4.prod.google.com ([2002:a17:90a:c204:b0:3b4:a73:5d7c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1801:b0:3b3:ffa8:6bc5 with SMTP id 98e67ed59e1d1-3b41c9823cemr4035343a91.60.1795212917097; Fri, 20 Nov 2026 14:15:17 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:21 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-21-irogers@google.com> Subject: [PATCH v3 20/30] perf parse-events: Decode metric-id terms when adding events 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Metric events are parsed from an event string where the identifier from the metric expression is passed as a metric-id term. As characters such as '@' and ',' can't appear in a term value the metricgroup code encodes them and, after parsing, walked the parsed evlist replacing each evsel's metric_id with its decoded version. Move the encoding and decoding to parse-events.c, adding parse_events__encode_metric_id() for the metricgroup code, and store the decoded metric-id on the evsel when it is created in __add_event(). This avoids rewriting the metric_id of evsels after parsing, leaving metricgroup with only the replacement of the evsel names of metric events by friendlier versions of the metric-id. While moving the decoding, fix the out of bounds check on the encoded character index and return -EINVAL for a bad encoding. Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 110 ++++++++------------------------- tools/perf/util/parse-events.c | 82 +++++++++++++++++++++++- tools/perf/util/parse-events.h | 1 + 3 files changed, 108 insertions(+), 85 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 9922445a6d15..08679392bf6d 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -610,57 +610,13 @@ int metricgroup__add_matching_metrics(struct evlist *= evlist, bool system_wide) find_matching_metrics_cb, &args); } =20 -static const char *code_characters =3D ",-=3D@"; - -static int encode_metric_id(struct strbuf *sb, const char *x) -{ - int ret =3D 0; - - for (; *x; x++) { - const char *c =3D strchr(code_characters, *x); - if (c) { - ret =3D strbuf_addch(sb, '!'); - if (ret) - break; - - ret =3D strbuf_addch(sb, '0' + (c - code_characters)); - if (ret) - break; - } else { - ret =3D strbuf_addch(sb, *x); - if (ret) - break; - } - } - return ret; -} - -static int decode_metric_id(struct strbuf *sb, const char *x) -{ - const char *orig =3D x; - size_t i; - char c; - int ret; - - for (; *x; x++) { - c =3D *x; - if (*x =3D=3D '!') { - x++; - i =3D *x - '0'; - if (i > strlen(code_characters)) { - pr_err("Bad metric-id encoding in: '%s'", orig); - return -1; - } - c =3D code_characters[i]; - } - ret =3D strbuf_addch(sb, c); - if (ret) - return ret; - } - return 0; -} - -static int decode_all_metric_ids(struct evlist *perf_evlist, const char *m= odifier) +/* + * The metric events are parsed with a metric-id holding the identifier us= ed in + * the metric expression. If the evsel name is just the parsed event, which + * contains the encoded metric-id, use the metric-id to give a more friend= ly + * display version. + */ +static int set_metric_event_names(struct evlist *perf_evlist, const char *= modifier) { struct evsel *ev; struct strbuf sb =3D STRBUF_INIT; @@ -668,51 +624,39 @@ static int decode_all_metric_ids(struct evlist *perf_= evlist, const char *modifie int ret =3D 0; =20 evlist__for_each_entry(perf_evlist, ev) { - if (!ev->metric_id) + bool has_slash =3D false; + + if (!ev->metric_id || !strstr(ev->name, "metric-id=3D")) continue; =20 ret =3D strbuf_setlen(&sb, 0); if (ret) break; =20 - ret =3D decode_metric_id(&sb, ev->metric_id); + ret =3D strbuf_addstr(&sb, ev->metric_id); if (ret) break; =20 - free((char *)ev->metric_id); - ev->metric_id =3D strdup(sb.buf); - if (!ev->metric_id) { - ret =3D -ENOMEM; - break; + zfree(&ev->name); + for (cur =3D strchr(sb.buf, '@') ; cur; cur =3D strchr(++cur, '@')) { + *cur =3D '/'; + has_slash =3D true; } - /* - * If the name is just the parsed event, use the metric-id to - * give a more friendly display version. - */ - if (strstr(ev->name, "metric-id=3D")) { - bool has_slash =3D false; - - zfree(&ev->name); - for (cur =3D strchr(sb.buf, '@') ; cur; cur =3D strchr(++cur, '@')) { - *cur =3D '/'; - has_slash =3D true; - } =20 - if (modifier) { - if (!has_slash && !strchr(sb.buf, ':')) { - ret =3D strbuf_addch(&sb, ':'); - if (ret) - break; - } - ret =3D strbuf_addstr(&sb, modifier); + if (modifier) { + if (!has_slash && !strchr(sb.buf, ':')) { + ret =3D strbuf_addch(&sb, ':'); if (ret) break; } - ev->name =3D strdup(sb.buf); - if (!ev->name) { - ret =3D -ENOMEM; + ret =3D strbuf_addstr(&sb, modifier); + if (ret) break; - } + } + ev->name =3D strdup(sb.buf); + if (!ev->name) { + ret =3D -ENOMEM; + break; } } strbuf_release(&sb); @@ -787,7 +731,7 @@ static int metricgroup__build_event_string(struct strbu= f *events, ret =3D strbuf_addstr(events, "/metric-id=3D"); RETURN_IF_NON_ZERO(ret); } - ret =3D encode_metric_id(events, id); + ret =3D parse_events__encode_metric_id(events, id); RETURN_IF_NON_ZERO(ret); ret =3D strbuf_addstr(events, "/"); RETURN_IF_NON_ZERO(ret); @@ -1514,7 +1458,7 @@ static int parse_ids(bool metric_no_merge, bool fake_= pmu, parse_events_error__print(&parse_error, events.buf); goto err_out; } - ret =3D decode_all_metric_ids(parsed_evlist, modifier); + ret =3D set_metric_event_names(parsed_evlist, modifier); if (ret) goto err_out; =20 diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 0044b6c27ea6..b54ab0604e4e 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -171,6 +171,73 @@ static void fix_raw(struct parse_events_terms *config_= terms, struct perf_pmu *pm } } =20 +/* + * Characters that can't appear in a metric-id term value are encoded as '= !' + * followed by the character's index in this string. + */ +static const char *metric_id_code_characters =3D ",-=3D@"; + +/** + * parse_events__encode_metric_id - Encode a metric identifier as a metric= -id + * term value. + * @sb: The buffer the encoded identifier is appended to. + * @x: The metric identifier to encode. + * + * Return: 0 on success or the strbuf error. + */ +int parse_events__encode_metric_id(struct strbuf *sb, const char *x) +{ + int ret =3D 0; + + for (; *x; x++) { + const char *c =3D strchr(metric_id_code_characters, *x); + + if (c) { + ret =3D strbuf_addch(sb, '!'); + if (ret) + break; + + ret =3D strbuf_addch(sb, '0' + (c - metric_id_code_characters)); + if (ret) + break; + } else { + ret =3D strbuf_addch(sb, *x); + if (ret) + break; + } + } + return ret; +} + +/* + * Decode a metric-id term value encoded by parse_events__encode_metric_id= () + * into @sb. + */ +static int parse_events__decode_metric_id(struct strbuf *sb, const char *x) +{ + const char *orig =3D x; + size_t i; + char c; + int ret; + + for (; *x; x++) { + c =3D *x; + if (*x =3D=3D '!') { + x++; + i =3D *x - '0'; + if (i >=3D strlen(metric_id_code_characters)) { + pr_err("Bad metric-id encoding in: '%s'", orig); + return -EINVAL; + } + c =3D metric_id_code_characters[i]; + } + ret =3D strbuf_addch(sb, c); + if (ret) + return ret; + } + return 0; +} + static struct evsel * __add_event(struct list_head *list, int *idx, struct perf_event_attr *attr, @@ -261,7 +328,12 @@ __add_event(struct list_head *list, int *idx, } =20 if (metric_id) { - evsel->metric_id =3D strdup(metric_id); + struct strbuf sb =3D STRBUF_INIT; + int ret =3D parse_events__decode_metric_id(&sb, metric_id); + + if (!ret) + evsel->metric_id =3D strdup(sb.buf); + strbuf_release(&sb); if (!evsel->metric_id) goto out_err; } @@ -668,6 +740,7 @@ int parse_events_add_breakpoint(struct parse_events_sta= te *parse_state, struct perf_event_attr attr; LIST_HEAD(config_terms); const char *name; + int ret; =20 memset(&attr, 0, sizeof(attr)); attr.bp_addr =3D addr; @@ -698,8 +771,11 @@ int parse_events_add_breakpoint(struct parse_events_st= ate *parse_state, =20 name =3D get_config_name(head_config); =20 - return add_event(list, parse_state, &attr, name, /*mertic_id=3D*/NULL, + ret =3D add_event(list, parse_state, &attr, name, /*mertic_id=3D*/NULL, &config_terms, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX); + /* Free the config terms if they weren't moved to an evsel. */ + free_config_terms(&config_terms); + return ret; } =20 static int check_type_val(struct parse_events_term *term, @@ -1624,6 +1700,8 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, &config_terms, first_wildcard_match, term_cpu, alternate_hw_config); perf_cpu_map__put(term_cpu); if (!evsel) { + /* The config terms weren't moved to an evsel. */ + free_config_terms(&config_terms); parse_events_terms__exit(&parsed_terms); return -ENOMEM; } diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 8df8e3cb69fe..68efbd24df9b 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -245,6 +245,7 @@ int parse_events_add_breakpoint(struct parse_events_sta= te *parse_state, struct evsel *parse_events__add_event(int idx, struct perf_event_attr *att= r, const char *name, const char *metric_id, struct perf_pmu *pmu); +int parse_events__encode_metric_id(struct strbuf *sb, const char *x); =20 int parse_events_multi_pmu_add(struct parse_events_state *parse_state, const char *event_name, --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212928; b=LnsiiOdknsJdFrYXtntzjVJzIoM7Kt7bEBZhdQk1unFWyO7jqGfvZNn1sBYlnPE7hSBk jhpMRGQDbJoHvMoAMdzSHkNLsMUADAtUDmETdsD86bIGo94WEFXf81SKsghre4bz3/6g5 gypk2IF+KKUJkiW6576B1RNsMmHJUJebRSK04POQNUvwtiPSythwWvZcND2u5UDfLcMtc zNuHyzvgN+kYqi93BoxZcAsGkQNUDJg4jzeSW4655X/mQs8UamyeCaHSNud774Wck1Qzk aZplUAkmlYlFa4Tx5mjqW2eCZ5p2ycl/Zuvwp+10tgtftGvWBCzNbL1yPWqVju1gwjg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212928; 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=SyTzmLttmqWaLUhVsT8hI7SZTZpFJfaInzF7iBR8ZGo=; b=pzJCTIB7Fv0FdQ/baWFlUmDyDILXQbFxN5qD07GblyUfQlvAzSyDkFI1xGIeD2Z7ylxg jJP5sJRJpdN2kXXPgsvFCtb6VYbev5uWU5IYcwMqvKRpqVuQY+Wp3CASZEfW7vnVOcpvN g6W3M873eICiHdCKBSiRtsBAXajmr7CS9JqGw0b4uzntebllRobj2Q9yRu8PTFXxqB1bg H0pJ44o83TmWMTsF0RtJKF1J3MT8ggb5GH1wmsCasyLA2D4DKU1wAc6Qi8pEZYmMUceqC +VGnVZcIWX7Ej2bacuJc2UTq/IN37G9Vzw5dfoudub8E8bM87J+eJcXftYsiLBK5D+w== 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=AckoxmvG; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (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 9F65D606A6A0 for ; Fri, 20 Nov 2026 22:15:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9F65D606A6A0 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=AckoxmvG Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-16bc6f3fdb1so949356c88.0 for ; Fri, 20 Nov 2026 14:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212925; x=1795817725; 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=SyTzmLttmqWaLUhVsT8hI7SZTZpFJfaInzF7iBR8ZGo=; b=AckoxmvGuFrpna/LZGaskcvCbhH8hPjXQJ3u4lrBMCzUVvBLZHNTaiSnxjOy/azUKD 5qCwdgOWsUiNMj5H0fNwO7khLKJeJ63qMjrAYjpj5zFM0f7GAE6f5NfK1WP+qQcT7XxQ ATnOT9WgwUJiUPjY2wUBCCfO4gko344DmH+PjtVcs0uwAH27iowjBZW2ds2BZ/2nIei8 0syeVblotkhTRLHBOkFRON69p7siyilYoM+tH9WmyCgsPyxe8ZPlI27x8ure8D35c+S6 5hqiNWYgdGSNRelRJtPdV1gdcXwPm4SnvInBhguIFbaSbK5UWAsThrWskMu0Mna6Onzc LmXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212925; x=1795817725; 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=SyTzmLttmqWaLUhVsT8hI7SZTZpFJfaInzF7iBR8ZGo=; b=hVGiF0NlbLhGYNu6oi3G1meoSHyxpda+O2IImcrbVcFTHkDEUI8o4uoqoIZK49qNZI Prf37WPepKFSfbU8/8rtdUuBFI65AHiiQWtdF8vO9CkOC+GWaNQdWgfXeIiiWfLXvZgK BpTxZfgGqOWONi+cx7mH/hVFNX9Mph0OrRzfDAi0LMYvxdsj/JNDp4KBGnvkZLn26rOl d+pcfJRDEaYAtPrHCC/fSa7qbfjEOB/omtxf5T7WfLQS2VNxVCuY3Iop7fl/EXdPF4pW BxpZtzW91dtY8Dd001xkqZ3IdUISnhFuveS2KuhdU8AOJBWVm1nYGzui463qC6VEBQO8 wlhw== X-Forwarded-Encrypted: i=1; AL+hErHq+ZDvCoWWdU+WwfWihKv+fB4FUwIb10kJm55kqe6cv7XDzGPk9BCHU77wzW51e38JOc6Nhwnmyvzw/PM=@vger.kernel.org X-Gm-Message-State: APuU4Z+RTFDqh9ufjyJViKTG02JCZ1hYoJv3iDreC7VREnjMJjXtT6+z DLG1kIj1NE8aSo/VvoNe8fPqgSqrgSHWy4iHXyiDbabAwT7I9zv6+lWtXaK5G75bU67cd5wbCLn MRG1MkNtBAA== X-Received: from dlbrc10.prod.google.com ([2002:a05:7022:ea8a:b0:1ac:f9af:14c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3a0c:b0:1b3:ac23:611f with SMTP id a92af1059eb24-1b5568d120cmr4594298c88.28.1795212924191; Fri, 20 Nov 2026 14:15:24 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:22 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-22-irogers@google.com> Subject: [PATCH v3 21/30] perf evsel: Generalize metric_id to a set of metric_ids 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When computing a metric the value of each of its events is added to the expression context using the event's metric_id. When metrics are matched to events read from a perf.data file, different metrics may refer to the same recorded event by different names, for example "instructions" and "cpu_core@INST_RETIRED.ANY@", but an evsel previously stored only a single metric_id. Replace evsel::metric_id with a NULL-terminated evsel::metric_ids set of unique identifier strings. Keep the representation private to evsel.c behind helpers: evsel__add_metric_id(), evsel__copy_metric_ids(), evsel__has_metric_id(), evsel__shares_metric_id() and evsel__for_each_metric_id(), a callback based iterator. Use the iterator in prepare_metric() to populate the expression parse context with all metric_ids of each metric event. The metric_ids are freed by evsel__exit(), so make the __add_event() error path use evsel__put() rather than freeing the evsel's strings itself. Remove evsel__metric_id() as returning a single identifier from the set is error prone. Its uses either iterate over the set, test membership, or, when finding duplicate events for a metric leader, test whether two evsels share an identifier. Tool events without an explicit metric-id keep their tool event name as an implicit identifier, other events without a metric-id no longer have the identifier "unknown". Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 172 ++++++++++++++++++++++++++++++--- tools/perf/util/evsel.h | 10 +- tools/perf/util/metricgroup.c | 98 ++++++++++++------- tools/perf/util/parse-events.c | 8 +- tools/perf/util/python.c | 34 +++++-- tools/perf/util/stat-shadow.c | 37 +++++-- 6 files changed, 291 insertions(+), 68 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 3122721a3f15..73687e24f5bc 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -577,11 +577,8 @@ struct evsel *evsel__clone(struct evsel *orig) if (evsel->filter =3D=3D NULL) goto out_err; } - if (orig->metric_id) { - evsel->metric_id =3D strdup(orig->metric_id); - if (evsel->metric_id =3D=3D NULL) - goto out_err; - } + if (evsel__copy_metric_ids(evsel, orig)) + goto out_err; evsel->cgrp =3D cgroup__get(orig->cgrp); #ifdef HAVE_LIBTRACEEVENT if (orig->tp_sys) { @@ -1029,15 +1026,166 @@ bool evsel__name_is(struct evsel *evsel, const cha= r *name) return !strcmp(evsel__name(evsel), name); } =20 -const char *evsel__metric_id(const struct evsel *evsel) +/* + * Tool events, such as duration_time, are identified by their tool event = name. + * Other events without a metric-id have no identifier. The evsel's PMU ma= y not + * be set yet, such as during parsing or cloning, so check the attribute's= type + * rather than using evsel__is_tool(). + */ +static const char *evsel__implicit_metric_id(const struct evsel *evsel) { - if (evsel->metric_id) - return evsel->metric_id; + if (evsel->core.attr.type !=3D PERF_PMU_TYPE_TOOL) + return NULL; =20 - if (evsel__is_tool(evsel)) - return evsel__tool_pmu_event_name(evsel); + return evsel__tool_pmu_event_name(evsel); +} =20 - return "unknown"; +/** + * evsel__for_each_metric_id - Call @cb for each metric identifier of @evs= el. + * @evsel: The evsel holding the metric identifiers. + * @cb: Callback passed each identifier, a non-zero return stops the itera= tion. + * @data: Passed to @cb. + * + * Explicit identifiers are visited in the order they were added, followed= by + * the tool event name for tool events. Tool events without explicit + * identifiers have their tool event name as the only identifier. + * + * Return: 0 or the first non-zero value returned by @cb. + */ +int evsel__for_each_metric_id(const struct evsel *evsel, + int (*cb)(const char *metric_id, void *data), + void *data) +{ + const char *implicit_id; + + if (!evsel->metric_ids) { + implicit_id =3D evsel__implicit_metric_id(evsel); + return implicit_id ? cb(implicit_id, data) : 0; + } + + for (size_t i =3D 0; evsel->metric_ids[i]; i++) { + int ret =3D cb(evsel->metric_ids[i], data); + + if (ret) + return ret; + } + return 0; +} + +static int metric_id_equal_cb(const char *metric_id, void *data) +{ + return !strcmp(metric_id, data); +} + +/** + * evsel__has_metric_id - Is @metric_id an identifier of @evsel? + * @evsel: The evsel holding the metric identifiers. + * @metric_id: The identifier to search for, may be NULL. + * + * Return: true if @metric_id is in the set of metric identifiers of @evse= l. + */ +bool evsel__has_metric_id(const struct evsel *evsel, const char *metric_id) +{ + if (!metric_id) + return false; + + return evsel__for_each_metric_id(evsel, metric_id_equal_cb, (void *)metri= c_id); +} + +static int has_metric_id_cb(const char *metric_id, void *data) +{ + const struct evsel *evsel =3D data; + + return evsel__has_metric_id(evsel, metric_id); +} + +/** + * evsel__shares_metric_id - Do two evsels have a metric identifier in com= mon? + * @a: The first evsel. + * @b: The second evsel. + * + * Return: true if the intersection of the metric identifier sets of @a an= d @b + * isn't empty. + */ +bool evsel__shares_metric_id(const struct evsel *a, const struct evsel *b) +{ + return evsel__for_each_metric_id(a, has_metric_id_cb, (void *)b); +} + +/** + * evsel__add_metric_id - Add an identifier to the metric identifiers of @= evsel. + * @evsel: The evsel to add the metric identifier to. + * @metric_id: The identifier to add, ignored if NULL or already present. + * + * The first explicit identifier added to a tool event is followed by the = tool + * event name so that the tool event name remains an identifier. + * + * Return: 0 on success or -ENOMEM. + */ +int evsel__add_metric_id(struct evsel *evsel, const char *metric_id) +{ + const char *implicit_id; + char **new_ids; + size_t nr =3D 0; + + if (!metric_id || evsel__has_metric_id(evsel, metric_id)) + return 0; + + implicit_id =3D evsel->metric_ids ? NULL : evsel__implicit_metric_id(evse= l); + while (evsel->metric_ids && evsel->metric_ids[nr]) + nr++; + + new_ids =3D realloc(evsel->metric_ids, (nr + 3) * sizeof(*new_ids)); + if (!new_ids) + return -ENOMEM; + evsel->metric_ids =3D new_ids; + + new_ids[nr] =3D strdup(metric_id); + if (!new_ids[nr]) + goto out_err; + new_ids[nr + 1] =3D NULL; + if (implicit_id) { + new_ids[nr + 1] =3D strdup(implicit_id); + if (!new_ids[nr + 1]) + goto out_err; + new_ids[nr + 2] =3D NULL; + } + return 0; +out_err: + zfree(&new_ids[nr]); + if (!nr) + zfree(&evsel->metric_ids); + return -ENOMEM; +} + +static int add_metric_id_cb(const char *metric_id, void *data) +{ + struct evsel *evsel =3D data; + + return evsel__add_metric_id(evsel, metric_id); +} + +/** + * evsel__copy_metric_ids - Add the metric identifiers of @src to @dst. + * @dst: The evsel to add the metric identifiers to. + * @src: The evsel whose metric identifiers are copied. + * + * Return: 0 on success or -ENOMEM. + */ +int evsel__copy_metric_ids(struct evsel *dst, const struct evsel *src) +{ + return evsel__for_each_metric_id(src, add_metric_id_cb, dst); +} + +/** + * evsel__free_metric_ids - Free the metric identifiers of @evsel. + * @evsel: The evsel whose metric identifiers are freed. + */ +static void evsel__free_metric_ids(struct evsel *evsel) +{ + for (size_t i =3D 0; evsel->metric_ids && evsel->metric_ids[i]; i++) + zfree(&evsel->metric_ids[i]); + zfree(&evsel->metric_ids); } =20 const char *evsel__group_name(struct evsel *evsel) @@ -2092,7 +2240,7 @@ static void evsel__exit(struct evsel *evsel) zfree(&evsel->filter); zfree(&evsel->group_pmu_name); zfree(&evsel->unit); - zfree(&evsel->metric_id); + evsel__free_metric_ids(evsel); evsel__zero_per_pkg(evsel); hashmap__free(evsel->per_pkg_mask); evsel->per_pkg_mask =3D NULL; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 53f3641b66e1..e776ca60a9a5 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -74,7 +74,7 @@ struct evsel { double scale; const char *unit; struct cgroup *cgrp; - const char *metric_id; + char **metric_ids; =20 /* The PMU the event is from. Used for missing_features, PMU name, etc. = */ struct perf_pmu *pmu; @@ -336,7 +336,13 @@ int arch_evsel__hw_name(struct evsel *evsel, char *bf,= size_t size); int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf= , size_t size); const char *evsel__name(struct evsel *evsel); bool evsel__name_is(struct evsel *evsel, const char *name); -const char *evsel__metric_id(const struct evsel *evsel); +int evsel__for_each_metric_id(const struct evsel *evsel, + int (*cb)(const char *metric_id, void *data), + void *data); +bool evsel__has_metric_id(const struct evsel *evsel, const char *metric_id= ); +bool evsel__shares_metric_id(const struct evsel *a, const struct evsel *b); +int evsel__add_metric_id(struct evsel *evsel, const char *metric_id); +int evsel__copy_metric_ids(struct evsel *dst, const struct evsel *src); =20 static inline bool evsel__is_retire_lat(const struct evsel *evsel) { diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 08679392bf6d..f25709750c3e 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -275,12 +275,34 @@ static bool contains_metric_id(struct evsel **metric_= events, int num_events, int i; =20 for (i =3D 0; i < num_events; i++) { - if (!strcmp(evsel__metric_id(metric_events[i]), metric_id)) + if (evsel__has_metric_id(metric_events[i], metric_id)) return true; } return false; } =20 +struct match_metric_id_args { + /* The events already matched to IDs of the metric. */ + struct evsel **metric_events; + size_t num_events; + /* The metric IDs to match. */ + struct hashmap *ids; + /* The newly matched ID. */ + const char *matched_id; +}; + +static int match_metric_id_cb(const char *metric_id, void *data) +{ + struct match_metric_id_args *args =3D data; + + if (contains_metric_id(args->metric_events, args->num_events, metric_id)) + return 0; + if (!hashmap__find(args->ids, metric_id, NULL)) + return 0; + args->matched_id =3D metric_id; + return 1; +} + /** * setup_metric_events - Find a group of events in metric_evlist that corr= espond * to the IDs from a parsed metric expression. @@ -293,8 +315,8 @@ static int setup_metric_events(const char *pmu, struct = hashmap *ids, struct evlist *metric_evlist, struct evsel ***out_metric_events) { + struct match_metric_id_args args =3D { .ids =3D ids, }; struct evsel **metric_events; - const char *metric_id; struct evsel *ev; size_t ids_size, matched_events, i; struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(metric_evlist) ?: h= ost_env()); @@ -310,9 +332,8 @@ static int setup_metric_events(const char *pmu, struct = hashmap *ids, return -ENOMEM; =20 matched_events =3D 0; + args.metric_events =3D metric_events; evlist__for_each_entry(metric_evlist, ev) { - struct expr_id_data *val_ptr; - /* Don't match events for the wrong hybrid PMU. */ if (!all_pmus && ev->pmu && evsel__is_hybrid(ev) && strcmp(ev->pmu->name, pmu)) @@ -323,21 +344,19 @@ static int setup_metric_events(const char *pmu, struc= t hashmap *ids, * events per socket on skylakex. Only the first such * event is placed in metric_events. */ - metric_id =3D evsel__metric_id(ev); - if (contains_metric_id(metric_events, matched_events, metric_id)) - continue; /* * Does this event belong to the parse context? For * combined or shared groups, this metric may not care * about this event. */ - if (hashmap__find(ids, metric_id, &val_ptr)) { - pr_debug("Matched metric-id %s to %s\n", metric_id, evsel__name(ev)); + args.num_events =3D matched_events; + if (evsel__for_each_metric_id(ev, match_metric_id_cb, &args)) { + pr_debug("Matched metric-id %s to %s\n", + args.matched_id, evsel__name(ev)); metric_events[matched_events++] =3D ev; - - if (matched_events >=3D ids_size) - break; } + if (matched_events >=3D ids_size) + break; } if (matched_events < ids_size) { free(metric_events); @@ -359,9 +378,8 @@ static int setup_metric_events(const char *pmu, struct = hashmap *ids, * of such events to be the event that appears in * metric_events. */ - metric_id =3D evsel__metric_id(ev); evlist__for_each_entry_continue(metric_evlist, ev) { - if (!strcmp(evsel__metric_id(ev), metric_id)) + if (evsel__shares_metric_id(ev, metric_events[i])) ev->metric_leader =3D metric_events[i]; } } @@ -469,13 +487,6 @@ static struct evsel *map_metric_evsel_to_evlist(struct= evlist *data_evlist, continue; if (metric_evsel->core.attr.config !=3D data_evsel->core.attr.config) continue; - /* Skip if the data event has a metric_id that doesn't match. */ - if (data_evsel->metric_id && - strcmp(evsel__metric_id(metric_evsel), evsel__metric_id(data_evsel))= ) { - pr_debug("Skipping matching evsel due to differing metric ids '%s' vs '= %s'\n", - evsel__metric_id(metric_evsel), evsel__metric_id(data_evsel)); - continue; - } return data_evsel; } return NULL; @@ -516,9 +527,8 @@ static int find_matching_metrics_cb(const struct pmu_me= tric *pm, struct evsel *new_metric_leader; =20 if (!data_evsel) { - pr_debug("Skipping metric '%s' as evsel '%s' / '%s' is missing\n", - pm->metric_name, evsel__name(metric_evsel), - evsel__metric_id(metric_evsel)); + pr_debug("Skipping metric '%s' as evsel '%s' is missing\n", + pm->metric_name, evsel__name(metric_evsel)); goto out; } =20 @@ -535,8 +545,8 @@ static int find_matching_metrics_cb(const struct pmu_me= tric *pm, /* Mismatching evsel leaders. */ if (data_evsel->metric_leader !=3D new_metric_leader) { pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders = '%s' vs '%s'\n", - pm->metric_name, evsel__metric_id(metric_evsel), - evsel__metric_id(data_evsel)); + pm->metric_name, evsel__name(metric_evsel), + evsel__name(data_evsel)); goto out; } } @@ -552,10 +562,8 @@ static int find_matching_metrics_cb(const struct pmu_m= etric *pm, metric_evsel, /*create=3D*/false); =20 - if (data_evsel->metric_id =3D=3D NULL) { - data_evsel->metric_id =3D metric_evsel->metric_id; - metric_evsel->metric_id =3D NULL; - } + if (evsel__copy_metric_ids(data_evsel, metric_evsel)) + goto out; =20 if (data_evsel->metric_leader =3D=3D NULL) { if (metric_evsel->metric_leader =3D=3D metric_evsel) { @@ -610,6 +618,17 @@ int metricgroup__add_matching_metrics(struct evlist *e= vlist, bool system_wide) find_matching_metrics_cb, &args); } =20 +static int append_metric_id_cb(const char *metric_id, void *data) +{ + struct strbuf *sb =3D data; + + /* + * Stop after the first, the metric-id of a parsed metric event is its + * first identifier. + */ + return strbuf_addstr(sb, metric_id) ?: 1; +} + /* * The metric events are parsed with a metric-id holding the identifier us= ed in * the metric expression. If the evsel name is just the parsed event, which @@ -626,16 +645,19 @@ static int set_metric_event_names(struct evlist *perf= _evlist, const char *modifi evlist__for_each_entry(perf_evlist, ev) { bool has_slash =3D false; =20 - if (!ev->metric_id || !strstr(ev->name, "metric-id=3D")) + if (!strstr(ev->name, "metric-id=3D")) continue; =20 ret =3D strbuf_setlen(&sb, 0); if (ret) break; =20 - ret =3D strbuf_addstr(&sb, ev->metric_id); - if (ret) + ret =3D evsel__for_each_metric_id(ev, append_metric_id_cb, &sb); + if (ret < 0) break; + if (ret =3D=3D 0) + continue; + ret =3D 0; =20 zfree(&ev->name); for (cur =3D strchr(sb.buf, '@') ; cur; cur =3D strchr(++cur, '@')) { @@ -1471,15 +1493,21 @@ static int parse_ids(bool metric_no_merge, bool fak= e_pmu, return ret; } =20 +static int pctx_has_metric_id_cb(const char *metric_id, void *data) +{ + struct hashmap *ids =3D data; + + return hashmap__find(ids, metric_id, NULL) ? 1 : 0; +} + /* How many times will a given evsel be used in a set of metrics? */ static int count_uses(struct list_head *metric_list, struct evsel *evsel) { - const char *metric_id =3D evsel__metric_id(evsel); struct metric *m; int uses =3D 0; =20 list_for_each_entry(m, metric_list, nd) { - if (hashmap__find(m->pctx->ids, metric_id, NULL)) + if (evsel__for_each_metric_id(evsel, pctx_has_metric_id_cb, m->pctx->ids= )) uses++; } return uses; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index b54ab0604e4e..b70ba9b58c43 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -332,9 +332,9 @@ __add_event(struct list_head *list, int *idx, int ret =3D parse_events__decode_metric_id(&sb, metric_id); =20 if (!ret) - evsel->metric_id =3D strdup(sb.buf); + ret =3D evsel__add_metric_id(evsel, sb.buf); strbuf_release(&sb); - if (!evsel->metric_id) + if (ret) goto out_err; } =20 @@ -362,9 +362,7 @@ __add_event(struct list_head *list, int *idx, out_err: perf_cpu_map__put(cpus); perf_cpu_map__put(pmu_cpus); - zfree(&evsel->name); - zfree(&evsel->metric_id); - free(evsel); + evsel__put(evsel); return NULL; } =20 diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index f1de03ee3e24..00dd114816d8 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -3071,6 +3071,27 @@ static PyObject *pyrf_evlist__metrics(struct pyrf_ev= list *pevlist) return list; } =20 +struct add_metric_id_val_args { + struct expr_parse_ctx *pctx; + double val; + int source_count; +}; + +static int add_metric_id_val_cb(const char *metric_id, void *data) +{ + struct add_metric_id_val_args *args =3D data; + char *n =3D strdup(metric_id); + int ret; + + if (!n) + return -ENOMEM; + + ret =3D expr__add_id_val_source_count(args->pctx, n, args->val, args->sou= rce_count); + if (ret) + free(n); + return ret; +} + static int prepare_metric(const struct metric_expr *mexp, const struct evsel *evsel, struct expr_parse_ctx *pctx, @@ -3078,6 +3099,7 @@ static int prepare_metric(const struct metric_expr *m= exp, { struct evsel * const *metric_events =3D mexp->metric_events; struct metric_ref *metric_refs =3D mexp->metric_refs; + struct add_metric_id_val_args args =3D { .pctx =3D pctx, }; =20 for (int i =3D 0; metric_events[i]; i++) { struct evsel *cur =3D metric_events[i]; @@ -3125,15 +3147,11 @@ static int prepare_metric(const struct metric_expr = *mexp, =20 if (ena !=3D run && run !=3D 0) val =3D val * ena / run; - char *n =3D strdup(evsel__metric_id(cur)); - - if (!n) - return -ENOMEM; - ret =3D expr__add_id_val_source_count(pctx, n, val, source_count); - if (ret) { - free(n); + args.val =3D val; + args.source_count =3D source_count; + ret =3D evsel__for_each_metric_id(cur, add_metric_id_val_cb, &args); + if (ret) return ret; - } } =20 for (int i =3D 0; metric_refs && metric_refs[i].metric_name; i++) { diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c index f5ae399bf336..587db3e2a05b 100644 --- a/tools/perf/util/stat-shadow.c +++ b/tools/perf/util/stat-shadow.c @@ -45,6 +45,29 @@ static bool tool_pmu__is_time_event(const struct perf_st= at_config *config, return true; } =20 +struct add_metric_id_val_args { + struct expr_parse_ctx *pctx; + double val; + int source_count; + int aggr_nr; +}; + +static int add_metric_id_val_cb(const char *metric_id, void *data) +{ + struct add_metric_id_val_args *args =3D data; + char *n =3D strdup(metric_id); + int ret; + + if (!n) + return -ENOMEM; + + ret =3D expr__add_id_val_source_count_aggr_nr(args->pctx, n, args->val, + args->source_count, args->aggr_nr); + if (ret) + free(n); + return ret; +} + static int prepare_metric(struct perf_stat_config *config, const struct metric_expr *mexp, const struct evsel *evsel, @@ -53,15 +76,16 @@ static int prepare_metric(struct perf_stat_config *conf= ig, { struct evsel * const *metric_events =3D mexp->metric_events; struct metric_ref *metric_refs =3D mexp->metric_refs; + struct add_metric_id_val_args args =3D { .pctx =3D pctx, }; int i; =20 for (i =3D 0; metric_events[i]; i++) { int source_count =3D 0, tool_aggr_idx; int aggr_nr =3D 1; + int ret; bool is_tool_time =3D tool_pmu__is_time_event(config, metric_events[i], &tool_aggr_idx); struct perf_stat_evsel *ps =3D metric_events[i]->stats; - char *n; double val; =20 /* @@ -114,11 +138,12 @@ static int prepare_metric(struct perf_stat_config *co= nfig, aggr_nr =3D aggr->nr ?: 1; } } - n =3D strdup(evsel__metric_id(metric_events[i])); - if (!n) - return -ENOMEM; - - expr__add_id_val_source_count_aggr_nr(pctx, n, val, source_count, aggr_n= r); + args.val =3D val; + args.source_count =3D source_count; + args.aggr_nr =3D aggr_nr; + ret =3D evsel__for_each_metric_id(metric_events[i], add_metric_id_val_cb= , &args); + if (ret) + return ret; } =20 for (int j =3D 0; metric_refs && metric_refs[j].metric_name; j++) { --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212936; b=vEZUeYUMXbQ1Dm+qrG8/tb4BGxiRNtLztLEHbkC3lbv1fWQNEP3X9ZvuPflP91M7T6E+ 3vTk4pBal/9nZRxjouRzbzhATeuFgl4f9JQqHwXXz53X6IwvDO8dsW7T5gyHvEEwGgZuY buF8X6zU7ApPOTSitQn5pv2chfTHHSpHdtnUXrlfSt6kMb4NPNnTULfzfE6cZxQfp/W6J HZMt5BDyfIwTlrkaPMIarEatB0hSYQtt+seAxrLice+H0LGTQakMj7E0AJWmDAIfNd71t qolvc+dOgWWDzYWwUShY1VbW8LumcwJN15hE1Ny1oV62bMFpiqSBDR7AU3Zq/vOfDUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212936; 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=R1UF6Qvsf9rHsUsHz66iYR6vOcaKtGQgcscSxSyNv1o=; b=BCmtvjz7Am/A1+cG6dGIZnxL+HQlxIoUMntqAfUzRnAU2UOQASg2FSJ6iZfDxDrD98bI j/wmKJL754bGbVyufYwDIsmsYteZIePUjsEqei8/zoNeZsfQB+NNTDF4s0Fx9b6zPvaAM ZY/WasEqXgFlAqISMbdXKaVraciD5F4FVLn3rThqGzMcsUi5+D8SPsbkJQ5Kh8bWaE9qd koE42mf5tIZ9SwcwLv8qMHCtMS3ELQGrryiL6Nnv1ym3ketGT7Xr/BnDJQbK/1x4iUmD2 YYQCC83BacC8qpNfAjNqL/Bpi6RSXeX542EWDHzL2GRYRaGvZhgixAereXjFMztR3Mg== 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="P+3/6gRg"; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 4B53B606A6B8 for ; Fri, 20 Nov 2026 22:15:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4B53B606A6B8 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=P+3/6gRg Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f03a137f6aso13096475ad.3 for ; Fri, 20 Nov 2026 14:15:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212931; x=1795817731; 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=R1UF6Qvsf9rHsUsHz66iYR6vOcaKtGQgcscSxSyNv1o=; b=P+3/6gRg5lZ7wIGWytIZNKVTK1ZAP2ZvC9t+VN4USARKft3HrbqMFZRNBDACGUyH2K NcLwKOh/jrrBHxG/6p7H8sDSKCXtNboXOnw6LsDItS2aDGf8A8EhCjTY/ihvw1YnvJC0 eeMYb5+XMGce6CPKxZ7p0rHxOgkoWW6ZN/tH0ZlU365n720HmJz8cI2w4mSB5z1ZRZPB X8lRg9RTdQREFFlzoMbAUT9Q5q5zzXwhYlUraqst2u2g9SwjEKzzwKoxXMscBgrBXxA8 avgDbDZTo6Ky8kDyPfXkdxVGm8N9d5pWD1XgpV1JkBMaXkM67ZplhOQXbw4xttlzjAPS rUpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212931; x=1795817731; 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=R1UF6Qvsf9rHsUsHz66iYR6vOcaKtGQgcscSxSyNv1o=; b=p2tRFXd+NsOEh7POt73FhNT4YIb+0EsUDFOW+VY2E4R1xOY8TOIU8uoOKwWsZE1IBE sXWWnKJz3jyBB52O3bf1Jl7PqC9omr3AJp5SS/i9l3CU+qsMTekhWBGhPnzYq+e3P/Gs P1ZDRaVE/WFHBDAdC2n/iPwpzLtZeY6cOZBCyfOFkZiMHjwJV3UjEMWAbL3Tf+O4nwi5 iSCXqwP6Lxq+gfu6ivJVeS4E+QA35XorRnFSCr40oC0hG9sFREcDTd9HIhCyxEohg3UH RlSfqwVKAKpenZYtW2qxXLlln7aihoK5gKUt2dNNQcejDSFns+FlovODUlMdtXjxSD3g Qq2Q== X-Forwarded-Encrypted: i=1; AL+hErGKPSud85DL88a/g62kicAhyaC+/CfjMp0tG4Z8rQpJHqUewqk6Rla/GkPbIMjAhfzfUMOKAWGx/EKXYPA=@vger.kernel.org X-Gm-Message-State: APuU4Z8o/NlG3Hh1fQo2YXHqL6Wyf999zC7dV09wWmtwNPbDAcO9s3T8 XG+f1HzGQ3b+HR44Eno93j6cSwT3Ewe1JpYbLIeGwTwV0kpMSr9rSNwMgfWQes72FA7MUQQhwJs +OecyGQ/tTg== X-Received: from plhc2.prod.google.com ([2002:a17:903:2342:b0:2f0:3edd:f273]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:37c4:b0:2f0:422a:222d with SMTP id d9443c01a7336-2f0422a30dbmr40073615ad.22.1795212931273; Fri, 20 Nov 2026 14:15:31 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:23 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-23-irogers@google.com> Subject: [PATCH v3 22/30] perf pmu: Add perf_pmu__for_each_name_from_config() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_pmu__name_from_config() returns the first event alias of a PMU whose parsed config matches, ignoring the perf_event_attr type. So a legacy hardware event alias can be returned for a raw config of the same value, only one of possibly several names is found, and the names of legacy events whose aliases were replaced by sysfs or json events of the same name can't be found. Add perf_pmu__for_each_name_from_config() that calls a callback for each event name of a PMU whose encoding has a given type and config, ignoring the extended PMU type of legacy encodings and, for core PMUs, also searching the legacy hardware and cache event tables. Make perf_pmu__name_from_config() return the first such name for the PMU's type. Also fix pmu_config_term() leaking help_msg when err is NULL, as called by perf_pmu__parse_terms_to_attr(). Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 106 +++++++++++++++++++++++++++++++++++++++--- tools/perf/util/pmu.h | 3 ++ 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index d319ef8ffbb8..b4541fbfc9c0 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1617,6 +1617,7 @@ static int pmu_config_term(const struct perf_pmu *pmu, } else { pr_debug("%s (%s)\n", unknown_term, help_msg); free(unknown_term); + free(help_msg); } free(pmu_term); return -EINVAL; @@ -2821,25 +2822,116 @@ void perf_pmu__delete(struct perf_pmu *pmu) free(pmu); } =20 -const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) +static bool pmu_config_equal(__u32 type, u64 a, u64 b) +{ + /* Ignore the extended PMU type in the upper bits of legacy encodings. */ + if (type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D PERF_TYPE_HW_CACHE) + return (a & PERF_HW_EVENT_MASK) =3D=3D (b & PERF_HW_EVENT_MASK); + return a =3D=3D b; +} + +struct legacy_name_args { + __u32 type; + u64 config; + int (*cb)(const char *name, void *data); + void *data; +}; + +static int pmu_legacy_name_cb(const struct pmu_event *pe, + const struct pmu_events_table *table __maybe_unused, + void *data) +{ + static const char hw_prefix[] =3D "legacy-hardware-config=3D"; + static const char cache_prefix[] =3D "legacy-cache-config=3D"; + struct legacy_name_args *args =3D data; + const char *prefix =3D args->type =3D=3D PERF_TYPE_HARDWARE ? hw_prefix := cache_prefix; + + if (!pe->name || !pe->event || !strstarts(pe->event, prefix)) + return 0; + if (!pmu_config_equal(args->type, strtoull(pe->event + strlen(prefix), NU= LL, 0), + args->config)) + return 0; + return args->cb(pe->name, args->data); +} + +/** + * perf_pmu__for_each_name_from_config - Call @cb for event names of an en= coding. + * @pmu: The PMU whose event names are searched. + * @type: The perf_event_attr type of the encoding, such as the PMU's type= or + * PERF_TYPE_HARDWARE for a legacy hardware event. + * @config: The perf_event_attr config of the encoding. + * @cb: Called with each event name of @pmu with the encoding, a non-zero = return + * stops the iteration. + * @data: Passed to @cb. + * + * As event aliases from sysfs or json replace legacy event aliases of the= same + * name, the names of legacy hardware and cache events of core PMUs are fo= und + * from the legacy event tables. @cb may be called more than once for a na= me. + * + * Return: 0 or the first non-zero value returned by @cb. + */ +int perf_pmu__for_each_name_from_config(struct perf_pmu *pmu, __u32 type, = u64 config, + int (*cb)(const char *name, void *data), + void *data) { struct hashmap_entry *entry; size_t bkt; + int ret; =20 if (!pmu) - return NULL; + return 0; =20 pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); hashmap__for_each_entry(pmu->aliases, entry, bkt) { struct perf_pmu_alias *event =3D entry->pvalue; - struct perf_event_attr attr =3D {.config =3D 0,}; - int ret =3D perf_pmu__parse_terms_to_attr(pmu, event->terms, &attr); + struct perf_event_attr attr =3D { .type =3D pmu->type, }; =20 - if (ret =3D=3D 0 && config =3D=3D attr.config) - return event->name; + if (perf_pmu__parse_terms_to_attr(pmu, event->terms, &attr) || + attr.type !=3D type || !pmu_config_equal(type, attr.config, config)) + continue; + ret =3D cb(event->name, data); + if (ret) + return ret; } - return NULL; + if (pmu->is_core && (type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D PERF_T= YPE_HW_CACHE)) { + struct legacy_name_args args =3D { + .type =3D type, + .config =3D config, + .cb =3D cb, + .data =3D data, + }; + + return pmu_events_table__for_each_event(perf_pmu__default_core_events_ta= ble(), + /*pmu=3D*/NULL, pmu_legacy_name_cb, &args); + } + return 0; +} + +static int first_name_cb(const char *name, void *data) +{ + const char **first =3D data; + + *first =3D name; + return 1; +} + +/** + * perf_pmu__name_from_config - The first event name of @pmu with @config. + * @pmu: The PMU whose event names are searched. + * @config: The perf_event_attr config of an event of @pmu's type. + * + * Return: An event name or NULL if none was found. + */ +const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) +{ + const char *name =3D NULL; + + if (!pmu) + return NULL; + + perf_pmu__for_each_name_from_config(pmu, pmu->type, config, first_name_cb= , &name); + return name; } =20 bool perf_pmu__reads_only_on_cpu_idx0(const struct perf_event_attr *attr) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 7605c3859337..ab7123964423 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -365,6 +365,9 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmu= s, int dirfd, const char struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *c= ore_pmus); void perf_pmu__delete(struct perf_pmu *pmu); =20 +int perf_pmu__for_each_name_from_config(struct perf_pmu *pmu, __u32 type, = u64 config, + int (*cb)(const char *name, void *data), + void *data); const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config); bool perf_pmu__is_fake(const struct perf_pmu *pmu); =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212943; b=3CAvODXMjXxId++kpedGAQ0Yb8zeuYcbEIRZoQdDR09UaaYBxEyEB1kDmThyvGmbgiv3 cmZYCw6++sNXPqd5Nwo2kY3dnq6Y8DCrE9Vlp+nNwMyGhqUgN/rayGiW+ekEmxuy5CuPe 2HTM8A43qhmlsJ/jD43+sCtwlzoKG/gpuNt3wV6BHIacpgoTkrNLUGMqX3YbbWDWuyHf+ uTv2Gz+YQQMVKQabjRcuNO2o7hi4YFS+xiYbC3tXkqDhRkI7R0454m8FZQhK2Zoh2vSEt 50wBoNCiVcvPGwDytV6P/G/MXMeXjhtNO9M7JMx7HdG5GAGOVYpYu+mScoFjyuIO1Aw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212943; 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=y2M61PDfzAued7gvPsWr6Gd3iBG0t9dkkNVjAr3WZZ8=; b=e7c5Pqn7wK4uKqIb8ND+exhS7mHG2QGR5m50M6Q14oZzx5oozxsvk0I44BEIq4AIqe9y jSpXwI7jlRZIHrTKKum1qrtZDkgYv9JtUyU9WzlTOIDf/pMHQ4DFZIa4nvyEfEpdhWZRA QXbK2iCGrb/+AR1t3PKmOsLCnxPmNG/NNMQVQgl0lR9wB5wtJ5GIFJYIHAH+yGl0Ks0tt R+oY7zjD6LsUHWDC228+6misy97RoOUxlJpjmfLbktU25VQlzdJEKj/lloy4CdLvE6SCZ UgdTEjdVbyvyZ6E7PN0WILW2RZ2mvzbYcT1YsSuT8FYgITww0VGTuTh+cGBx5EMhRbg== 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=kc32zFmy; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 D855F606A6A1 for ; Fri, 20 Nov 2026 22:15:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D855F606A6A1 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=kc32zFmy Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-375bac6bd21so1480615eec.0 for ; Fri, 20 Nov 2026 14:15:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212934; x=1795817734; 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=y2M61PDfzAued7gvPsWr6Gd3iBG0t9dkkNVjAr3WZZ8=; b=kc32zFmyawv2DeBE9NO/+a0Q633n5mYFhiOLuzrfI9d8ABy2G+pYk2qmTdM/4G3Q64 MwlpavACACySQoGl7TqEtK5VnSfRalAss2eeRlQGPA/C/SzEXfBwUsgMWlzD6sMIzsjs GZ+o6gmgQQGM98KtLXEGAiFKK9nupe0IQG9NT9eyRvF1F4igUnLm6xps/JSv1zSix9Sc hSuYFqS4hZOVbwdnw7AYuJ/jBd6bXv5ADlIgTCkgo50bZW3y0h+Jx0peMTu0TuuQe7QC Mw7TWi8N0BZ75e+zqfjTMxOlKaTnrMTINFAlifhpcDaj/iND/Ney6RAIRYzh6ELfsqwr Yngg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212934; x=1795817734; 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=y2M61PDfzAued7gvPsWr6Gd3iBG0t9dkkNVjAr3WZZ8=; b=tPB5c0txCtL/itB8vWB68xzVOCrJc8jqjo2bEWU99gK3mt6fplh9hrhuWqpOrZlryz Ved2ENjBRdzaIK0zh7E2sMoftNTtwP7bA2dtaMcLCzboezlYVHNZG4YdNMtdXtClYqDz pzq6XFLCko1vrPXqT6/Kwe4+Y7Fy5JGKVXqXSAAwfaRD2YOyVG3Zaa+O1xRrdico9LCR gS4xXAGn/a35MsNhrMQldxuRy9Tnly6fTK/2W1I3vYuQ92yNZ/FhqIrOoQDnjF49d7CU jYD7At/MtCDIW9c8iNQV5SInWPqsl7EjUUSj/zAnWadgIrStyTZ5pEnvBnnG6aRMtIKi dTRA== X-Forwarded-Encrypted: i=1; AL+hErGRLVkDLN1FPkPbRr+feishD1MSal1hIJ7r+DeLjN+xiAwOEsmBuFjJHushEOxh39l4yTUt/OEOUHeF1zw=@vger.kernel.org X-Gm-Message-State: APuU4Z8vtoTMSugPyUJaJh4O4ofulPT3hHD43CdrzDMWjlsuN9E65JlV 02MthHuFBPJ4P6iUQEzRklrr2i+1eBYF0HlBSIK4kArTzyNHA91a2gno69k5etvCG2uZzj09//C UJRS9YgKqCw== X-Received: from dyjz27.prod.google.com ([2002:a05:7300:b1b:b0:381:fb4c:34e1]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:5098:b0:381:5fe2:a079 with SMTP id 5a478bee46e88-3856c6b010amr4284417eec.1.1795212933517; Fri, 20 Nov 2026 14:15:33 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:24 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-24-irogers@google.com> Subject: [PATCH v3 23/30] perf metricgroup: Share metric events by their counter encoding 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Metrics share events to reduce multiplexing. Events were shared by the names of the identifiers in the metric expressions: the identifiers of ungrouped metrics were combined into one set of events, and a grouped metric reused the events of an earlier metric whose identifiers were a superset of its own. Events spelled differently but counting the same thing, such as "slots" and "cpu_core/TOPDOWN.SLOTS/" or "cycles" and "cpu-cycles", weren't shared, and events were only shared within the metrics of one metricgroup__parse_groups() call. Add an evsel matcher that compares a key of the counter an evsel programs: the PMU name, the perf_event_attr type and config fields, the exclusion bits and other modifiers, the CPUs, and the cgroup. Legacy hardware events of core PMUs are compared using the encoding of the equivalent sysfs or json event. Parse the events of each metric and then move them to the evlist, unless the evlist already has an event counting the same thing with compatible metric leadership, such as from an earlier metric, an earlier metricgroup__parse_groups() call or the command line, in which case that event is used. Events of a grouped metric are only shared when all of them are found in one group; when grouping was only forced by the architecture (such as x86 topdown slots and perf-metrics events), new members are appended to an existing forced group. Ungrouped events only match ungrouped events so that a metric's events aren't split across multiplexing groups. The metric ids of a metric's events are added to the shared events, and the metric's events refer to the shared events. --metric-no-merge disables the sharing. Remove build_combined_expr_ctx(), find_tool_events(), which added every tool event to every metric so that the identifier sets could be supersets, and expr__subset_of_ids(), now unused. For example, on an Alder Lake system "--cputype=3Dcore -e '{cpu_core/instructions/,cpu_core/cpu-cycles/}' -M tma_info_thread_ipc" now shares that 2-event group on cpu_core rather than also opening "{INST_RETIRED.ANY,CPU_CLK_UNHALTED.THREAD}". Signed-off-by: Ian Rogers --- tools/perf/util/expr.c | 15 - tools/perf/util/expr.h | 2 - tools/perf/util/metricgroup.c | 1216 +++++++++++++++++++++++++++------ 3 files changed, 1016 insertions(+), 217 deletions(-) diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index 09ced31ef59e..09dad66e3835 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -235,21 +235,6 @@ int expr__get_id(struct expr_parse_ctx *ctx, const cha= r *id, return hashmap__find(ctx->ids, id, data) ? 0 : -1; } =20 -bool expr__subset_of_ids(struct expr_parse_ctx *haystack, - struct expr_parse_ctx *needles) -{ - struct hashmap_entry *cur; - size_t bkt; - struct expr_id_data *data; - - hashmap__for_each_entry(needles->ids, cur, bkt) { - if (expr__get_id(haystack, cur->pkey, &data)) - return false; - } - return true; -} - - int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id, struct expr_id_data **datap) { diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h index ed12e4007d2d..7e22a1ea6ce1 100644 --- a/tools/perf/util/expr.h +++ b/tools/perf/util/expr.h @@ -42,8 +42,6 @@ int expr__add_id_val_source_count_aggr_nr(struct expr_par= se_ctx *ctx, const char int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref); int expr__get_id(struct expr_parse_ctx *ctx, const char *id, struct expr_id_data **data); -bool expr__subset_of_ids(struct expr_parse_ctx *haystack, - struct expr_parse_ctx *needles); int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id, struct expr_id_data **datap); =20 diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index f25709750c3e..5641a3676063 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -155,11 +156,15 @@ struct metric { /** Show events even if in the Default metric group. */ bool default_show_events; /** - * Parsed events for the metric. Optional as events may be taken from a - * different metric whose group contains all the IDs necessary for this - * one. + * Events parsed for the metric before they are moved to, or replaced by + * equivalent events in, the evlist the metrics are added to. */ struct evlist *evlist; + /** + * The NULL terminated events of the metric's expression, placed in a + * metric_expr. + */ + struct evsel **metric_events; }; =20 static void metric__watchdog_constraint_hint(const char *name, bool foot) @@ -215,6 +220,7 @@ static void metric__free(struct metric *m) expr__ctx_free(m->pctx); zfree(&m->modifier); evlist__put(m->evlist); + free(m->metric_events); free(m); } =20 @@ -379,6 +385,9 @@ static int setup_metric_events(const char *pmu, struct = hashmap *ids, * metric_events. */ evlist__for_each_entry_continue(metric_evlist, ev) { + if (!all_pmus && ev->pmu && evsel__is_hybrid(ev) && + strcmp(ev->pmu->name, pmu)) + continue; if (evsel__shares_metric_id(ev, metric_events[i])) ev->metric_leader =3D metric_events[i]; } @@ -471,6 +480,607 @@ int metricgroup__for_each_metric(struct perf_pmus *pm= us, const struct pmu_metric return pmu_for_each_sys_metric(metricgroup__sys_event_iter, &sys_data); } =20 +/* + * A key identifying the counter an evsel programs, used to find evsels co= unting + * the same thing, such as when metrics share events. + */ +struct counter_key { + /** @pmu_name: Name of the evsel's PMU, or NULL if unknown. */ + const char *pmu_name; + /** @cpus: CPUs the evsel counts. */ + const struct perf_cpu_map *cpus; + /** @owned_cpus: Reference-counted CPU map owned by this key, or NULL. */ + struct perf_cpu_map *owned_cpus; + /** @cgrp: The cgroup the evsel counts. */ + const struct cgroup *cgrp; + /** @type: perf_event_attr type, PERF_TYPE_RAW for any dynamic PMU type. = */ + __u32 type; + /** @config: perf_event_attr config without legacy extended type bits. */ + __u64 config; + /** @config1: perf_event_attr config1. */ + __u64 config1; + /** @config2: perf_event_attr config2. */ + __u64 config2; + /** @config3: perf_event_attr config3. */ + __u64 config3; + /** @config4: perf_event_attr config4. */ + __u64 config4; + /** @exclude: perf_event_attr exclude bits, a mask of COUNTER_KEY_EXCLUDE= _*. */ + unsigned int exclude; + /** + * @modifiers: Other perf_event_attr and evsel modifiers affecting what + * is counted, a mask of COUNTER_KEY_MOD_*. + */ + unsigned int modifiers; + /** @precise_ip: perf_event_attr precise_ip. */ + unsigned int precise_ip; +}; + +#define COUNTER_KEY_EXCLUDE_USER BIT(0) +#define COUNTER_KEY_EXCLUDE_KERNEL BIT(1) +#define COUNTER_KEY_EXCLUDE_HV BIT(2) +#define COUNTER_KEY_EXCLUDE_HOST BIT(3) +#define COUNTER_KEY_EXCLUDE_GUEST BIT(4) +#define COUNTER_KEY_EXCLUDE_IDLE BIT(5) + +#define COUNTER_KEY_MOD_PINNED BIT(0) +#define COUNTER_KEY_MOD_EXCLUSIVE BIT(1) +#define COUNTER_KEY_MOD_PERCORE BIT(2) +#define COUNTER_KEY_MOD_RETIRE_LAT BIT(3) +#define COUNTER_KEY_MOD_PRECISE_MAX BIT(4) +#define COUNTER_KEY_MOD_BPF BIT(5) + +/* Cached sysfs encodings of legacy hardware events on a PMU. */ +struct legacy_hw_key { + struct list_head list; + char *pmu_name; + __u64 hw_config; + struct counter_key key; +}; + +struct evsel_matcher_entry { + struct evsel *evsel; + struct counter_key key; +}; + +/** + * struct evsel_matcher - Find the evsel, in a set of evsels, counting the= same + * thing as another evsel. + */ +struct evsel_matcher { + /** @evlist: Target evlist whose events are being matched. */ + struct evlist *evlist; + /** @entries: The evsels that may be matched and their counter keys. */ + struct evsel_matcher_entry *entries; + /** @nr_entries: Number of entries in use. */ + size_t nr_entries; + /** @max_entries: Number of entries allocated. */ + size_t max_entries; + /** @legacy_hw_keys: Cache of legacy_hw_key. */ + struct list_head legacy_hw_keys; +}; + +static void counter_key__exit(struct counter_key *key) +{ + perf_cpu_map__put(key->owned_cpus); + key->owned_cpus =3D NULL; +} + +static void counter_key__init_attr(struct counter_key *key, const char *pm= u_name, + const struct perf_event_attr *attr) +{ + memset(key, 0, sizeof(*key)); + key->pmu_name =3D pmu_name; + key->type =3D attr->type; + key->config =3D attr->config; + key->config1 =3D attr->config1; + key->config2 =3D attr->config2; + key->config3 =3D attr->config3; + key->config4 =3D attr->config4; + key->exclude =3D (attr->exclude_user ? COUNTER_KEY_EXCLUDE_USER : 0) | + (attr->exclude_kernel ? COUNTER_KEY_EXCLUDE_KERNEL : 0) | + (attr->exclude_hv ? COUNTER_KEY_EXCLUDE_HV : 0) | + (attr->exclude_host ? COUNTER_KEY_EXCLUDE_HOST : 0) | + (attr->exclude_guest ? COUNTER_KEY_EXCLUDE_GUEST : 0) | + (attr->exclude_idle ? COUNTER_KEY_EXCLUDE_IDLE : 0); + key->modifiers =3D (attr->pinned ? COUNTER_KEY_MOD_PINNED : 0) | + (attr->exclusive ? COUNTER_KEY_MOD_EXCLUSIVE : 0); + key->precise_ip =3D attr->precise_ip; + if (attr->type =3D=3D PERF_TYPE_HARDWARE || attr->type =3D=3D PERF_TYPE_H= W_CACHE) { + key->config &=3D PERF_HW_EVENT_MASK; + } else if (attr->type >=3D PERF_TYPE_MAX && pmu_name) { + /* + * Dynamic PMU type numbers may differ between machines, compare + * the PMU name instead. Without a PMU name keep the type so + * that the event doesn't match the same config on any PMU. + */ + key->type =3D PERF_TYPE_RAW; + } +} + +/* Copy the counter encoding, but not the PMU name or modifiers, of @src. = */ +static void counter_key__set_counter(struct counter_key *key, const struct= counter_key *src) +{ + key->type =3D src->type; + key->config =3D src->config; + key->config1 =3D src->config1; + key->config2 =3D src->config2; + key->config3 =3D src->config3; + key->config4 =3D src->config4; +} + +/* + * Parse "//" and, if the encoding isn't a legacy hardware + * event, such as because sysfs or json provide an encoding that is prefer= red + * by the perf tool, set @key to the encoding. + */ +static bool core_event_key(const char *pmu_name, const char *name, struct = counter_key *key) +{ + struct parse_events_error err; + struct evlist *evlist; + struct evsel *evsel; + char *str; + bool found =3D false; + + if (asprintf(&str, "%s/%s/", pmu_name, name) < 0) + return false; + + evlist =3D evlist__new(); + if (!evlist) { + free(str); + return false; + } + parse_events_error__init(&err); + if (!parse_events(evlist, str, &err) && evlist__nr_entries(evlist) =3D=3D= 1) { + evsel =3D evlist__first(evlist); + if (evsel->core.attr.type !=3D PERF_TYPE_HARDWARE) { + counter_key__init_attr(key, pmu_name, &evsel->core.attr); + found =3D true; + } + } + parse_events_error__exit(&err); + evlist__put(evlist); + free(str); + return found; +} + +struct legacy_hw_name_args { + const char *pmu_name; + struct counter_key *key; +}; + +/* Is there a non-legacy encoding of the legacy hardware event @name? */ +static int legacy_hw_name_cb(const char *name, void *data) +{ + struct legacy_hw_name_args *args =3D data; + + return core_event_key(args->pmu_name, name, args->key) ? 1 : 0; +} + +/* + * Legacy hardware events, such as "cycles" in "perf record -e cycles", are + * commonly also available as a sysfs or json event of the core PMU, such = as + * "cpu_core/cpu-cycles/", whose encoding is preferred by the perf tool an= d is + * the encoding metrics use. Find the names of the legacy hardware event a= nd + * compute the key of the preferred encoding using the live host PMU of th= e same + * name so that the two encodings match. + */ +static bool legacy_hw_key(struct evsel_matcher *matcher, const char *pmu_n= ame, + __u64 hw_config, struct counter_key *key) +{ + struct legacy_hw_key *pos; + struct legacy_hw_name_args name_args; + struct perf_pmu *pmu; + + list_for_each_entry(pos, &matcher->legacy_hw_keys, list) { + if (pos->hw_config =3D=3D hw_config && !strcmp(pos->pmu_name, pmu_name))= { + if (pos->key.type =3D=3D PERF_TYPE_HARDWARE) + return false; + counter_key__set_counter(key, &pos->key); + return true; + } + } + pos =3D zalloc(sizeof(*pos)); + if (!pos) + return false; + pos->pmu_name =3D strdup(pmu_name); + if (!pos->pmu_name) { + free(pos); + return false; + } + pos->hw_config =3D hw_config; + pos->key.type =3D PERF_TYPE_HARDWARE; + list_add(&pos->list, &matcher->legacy_hw_keys); + + pmu =3D perf_pmus__find(perf_env__pmus(host_env()), pmu_name); + if (!pmu || !pmu->is_core) + return false; + + name_args.pmu_name =3D pos->pmu_name; + name_args.key =3D &pos->key; + if (perf_pmu__for_each_name_from_config(pmu, PERF_TYPE_HARDWARE, hw_confi= g, + legacy_hw_name_cb, &name_args) !=3D 1) { + pos->key.type =3D PERF_TYPE_HARDWARE; + return false; + } + pos->key.pmu_name =3D pos->pmu_name; + counter_key__set_counter(key, &pos->key); + return true; +} + +/* + * Compute the CPU map @evsel will have once added to @evlist when + * @evlist->needs_map_propagation is set, matching __perf_evlist__propagat= e_maps(). + */ +static struct perf_cpu_map *evsel_propagated_cpus(const struct perf_evlist= *evlist, + const struct evsel *evsel) +{ + struct perf_cpu_map *pmu_cpus =3D evsel->core.pmu_cpus; + struct perf_cpu_map *cpus =3D perf_cpu_map__get(evsel->core.cpus); + bool put_pmu_cpus =3D false; + + if (perf_cpu_map__is_empty(cpus)) { + perf_cpu_map__put(cpus); + if (perf_cpu_map__is_empty(pmu_cpus)) { + pmu_cpus =3D perf_cpu_map__new_online_cpus(); + put_pmu_cpus =3D true; + } + if (evlist->has_user_cpus && !evsel->core.system_wide) + cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); + else + cpus =3D perf_cpu_map__get(pmu_cpus); + } + if (evsel->core.requires_cpu && perf_cpu_map__has_any_cpu(cpus)) { + perf_cpu_map__put(cpus); + cpus =3D perf_cpu_map__get(pmu_cpus); + } + if (evlist->has_user_cpus && !evsel->core.system_wide && + !perf_cpu_map__equal(cpus, evlist->user_requested_cpus)) { + perf_cpu_map__put(cpus); + cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); + } + if (!evsel->core.is_pmu_uncore && !perf_cpu_map__has_any_cpu(cpus) && + !perf_cpu_map__is_subset(pmu_cpus, cpus)) { + struct perf_cpu_map *tmp =3D perf_cpu_map__intersect(pmu_cpus, cpus); + + perf_cpu_map__put(cpus); + cpus =3D tmp; + } + if (!evsel->core.system_wide && !evsel->core.requires_cpu && + perf_cpu_map__equal(cpus, pmu_cpus) && + perf_cpu_map__has_any_cpu(evlist->user_requested_cpus)) { + perf_cpu_map__put(cpus); + cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); + } + if (evsel->core.reads_only_on_cpu_idx0 && perf_cpu_map__nr(cpus) > 0) { + struct perf_cpu_map *srcs[3] =3D { + evlist->all_cpus, + evlist->user_requested_cpus, + pmu_cpus, + }; + + for (size_t i =3D 0; i < ARRAY_SIZE(srcs); i++) { + if (!srcs[i]) + continue; + perf_cpu_map__put(cpus); + cpus =3D perf_cpu_map__new_int(perf_cpu_map__cpu(srcs[i], 0).cpu); + break; + } + } + if (put_pmu_cpus) + perf_cpu_map__put(pmu_cpus); + return cpus; +} + +static void evsel_matcher__key(struct evsel_matcher *matcher, struct evsel= *evsel, + struct counter_key *key) +{ + struct perf_pmu *pmu =3D evsel__find_pmu(evsel); + + counter_key__init_attr(key, pmu ? pmu->name : NULL, &evsel->core.attr); + if (matcher->evlist && evlist__core(matcher->evlist)->needs_map_propagati= on && + evsel->evlist !=3D matcher->evlist) { + key->owned_cpus =3D evsel_propagated_cpus(evlist__core(matcher->evlist),= evsel); + key->cpus =3D key->owned_cpus; + } else { + key->cpus =3D evsel->core.cpus; + } + key->cgrp =3D evsel->cgrp; + if (evsel->percore) + key->modifiers |=3D COUNTER_KEY_MOD_PERCORE; + if (evsel__is_retire_lat(evsel)) + key->modifiers |=3D COUNTER_KEY_MOD_RETIRE_LAT; + if (evsel->precise_max) + key->modifiers |=3D COUNTER_KEY_MOD_PRECISE_MAX; + if (evsel->bpf_counter) + key->modifiers |=3D COUNTER_KEY_MOD_BPF; + if (key->type =3D=3D PERF_TYPE_HARDWARE && pmu && pmu->is_core) + legacy_hw_key(matcher, pmu->name, key->config, key); +} + +/* Do @a and @b count the same thing? */ +static bool counter_key__equal(const struct counter_key *a, const struct c= ounter_key *b) +{ + if (a->type !=3D b->type || a->config !=3D b->config || + a->config1 !=3D b->config1 || a->config2 !=3D b->config2 || + a->config3 !=3D b->config3 || a->config4 !=3D b->config4) + return false; + + if ((a->type =3D=3D PERF_TYPE_RAW || a->type >=3D PERF_TYPE_MAX) && + !a->pmu_name !=3D !b->pmu_name) + return false; + + if (a->pmu_name && b->pmu_name && strcmp(a->pmu_name, b->pmu_name)) + return false; + + return a->exclude =3D=3D b->exclude && a->modifiers =3D=3D b->modifiers && + a->precise_ip =3D=3D b->precise_ip && a->cgrp =3D=3D b->cgrp && + perf_cpu_map__equal(a->cpus, b->cpus); +} + +static bool evsel__is_grouped(const struct evsel *evsel) +{ + return evsel__leader(evsel)->core.nr_members > 1 || + (evsel->core.attr.read_format & PERF_FORMAT_GROUP); +} + +/** + * evsel_matcher__add() - Add @evsel to the evsels that may be matched. + * @matcher: The matcher. + * @evsel: The evsel to add. + */ +static int evsel_matcher__add(struct evsel_matcher *matcher, struct evsel = *evsel) +{ + struct evsel_matcher_entry *entry; + + if (matcher->nr_entries =3D=3D matcher->max_entries) { + size_t max_entries =3D matcher->max_entries ? matcher->max_entries * 2 := 16; + struct evsel_matcher_entry *entries =3D + realloc(matcher->entries, max_entries * sizeof(*entries)); + + if (!entries) + return -ENOMEM; + matcher->entries =3D entries; + matcher->max_entries =3D max_entries; + } + entry =3D &matcher->entries[matcher->nr_entries++]; + entry->evsel =3D evsel; + evsel_matcher__key(matcher, evsel, &entry->key); + return 0; +} + +/** + * evsel_matcher__init() - Initialize a matcher of the evsels in @evlist. + * @matcher: The matcher to initialize. + * @evlist: The evsels that may be matched, more may be added with + * evsel_matcher__add(). + */ +static int evsel_matcher__init(struct evsel_matcher *matcher, struct evlis= t *evlist) +{ + struct evsel *evsel; + + memset(matcher, 0, sizeof(*matcher)); + matcher->evlist =3D evlist; + INIT_LIST_HEAD(&matcher->legacy_hw_keys); + evlist__for_each_entry(evlist, evsel) { + int ret =3D evsel_matcher__add(matcher, evsel); + + if (ret) + return ret; + } + return 0; +} + +static void evsel_matcher__exit(struct evsel_matcher *matcher) +{ + struct legacy_hw_key *pos, *tmp; + + list_for_each_entry_safe(pos, tmp, &matcher->legacy_hw_keys, list) { + list_del(&pos->list); + counter_key__exit(&pos->key); + free(pos->pmu_name); + free(pos); + } + for (size_t i =3D 0; i < matcher->nr_entries; i++) + counter_key__exit(&matcher->entries[i].key); + zfree(&matcher->entries); + matcher->nr_entries =3D matcher->max_entries =3D 0; +} + +static bool evsel__is_forced_group(struct evsel *leader) +{ + struct evsel *pos; + + if (leader->dont_regroup) + return false; + + for_each_group_evsel(pos, leader) { + if (!arch_evsel__must_be_in_group(pos)) + return false; + } + return true; +} + +/* + * Does @pos have a metric_leader compatible with @evsel when @evsel's met= ric + * leader has @metric_sources events and was mapped to @mapped_metric_lead= er? + */ +static bool metric_leader_compatible(const struct evsel *evsel, int metric= _sources, + const struct evsel *mapped_metric_leader, + const struct evsel *pos) +{ + if (!evsel->metric_leader || metric_sources =3D=3D 0) + return true; + + if (evsel->metric_leader =3D=3D evsel) { + if (!pos->metric_leader) + return true; + return pos->metric_leader =3D=3D pos && evsel__source_count(pos) =3D=3D = metric_sources; + } + + if (!mapped_metric_leader) + return false; + if (!mapped_metric_leader->metric_leader) + return !pos->metric_leader && pos !=3D mapped_metric_leader; + return pos->metric_leader =3D=3D mapped_metric_leader; +} + +static void own_evsel__metric_match_info(struct evsel **own, struct evsel = **map, int nr, + const struct evsel *ev, int *metric_sources, + const struct evsel **mapped_metric_leader) +{ + int count =3D 0; + + *metric_sources =3D 0; + *mapped_metric_leader =3D NULL; + if (!ev->metric_leader) + return; + + for (int i =3D 0; i < nr; i++) { + if (own[i]->metric_leader =3D=3D ev->metric_leader) + count++; + if (own[i] =3D=3D ev->metric_leader) + *mapped_metric_leader =3D map[i]; + } + *metric_sources =3D count; +} + +enum evsel_group_match { + EVSEL_GROUP_MATCH_UNGROUPED, + EVSEL_GROUP_MATCH_ANY, + EVSEL_GROUP_MATCH_FORCED_LEADER, +}; + +/** + * evsel_matcher__find_for_metric() - Find the evsel counting the same thi= ng as + * @evsel with compatible metric leader= ship. + * @matcher: The matcher. + * @evsel: The evsel to match. + * @leader: If not NULL, the group leader of the matched evsel. + * @metric_sources: Number of events in @evsel's metric_leader family, or = 0. + * @mapped_metric_leader: Replacement of @evsel->metric_leader when @evsel= is + * not its own metric leader. + * @group_match: Whether grouped events may be matched when @leader is NUL= L. + * + * The same counter may be present more than once, such as when the counte= r is + * in different groups. Prefer an evsel with the same name and the same gr= oup + * membership. + */ +static struct evsel *evsel_matcher__find_for_metric(struct evsel_matcher *= matcher, + struct evsel *evsel, + const struct evsel *leader, + int metric_sources, + const struct evsel *mapped_metric_leader, + enum evsel_group_match group_match) +{ + struct evsel *best =3D NULL; + struct counter_key key; + bool grouped =3D evsel__is_grouped(evsel); + bool fake =3D evsel->pmu && perf_pmu__is_fake(evsel->pmu); + int best_score =3D -1; + + evsel_matcher__key(matcher, evsel, &key); + for (size_t i =3D 0; i < matcher->nr_entries; i++) { + struct evsel_matcher_entry *entry =3D &matcher->entries[i]; + struct evsel *pos =3D entry->evsel; + int score; + + if (pos =3D=3D evsel || (leader && evsel__leader(pos) !=3D leader)) + continue; + + /* + * Don't match an ungrouped event to a member of a group, as the + * group may multiplex differently from the metric's other + * events. + */ + if (!leader && group_match =3D=3D EVSEL_GROUP_MATCH_UNGROUPED && + evsel__is_grouped(pos)) + continue; + + if (group_match =3D=3D EVSEL_GROUP_MATCH_FORCED_LEADER && + (!evsel__is_group_leader(pos) || !evsel__is_forced_group(pos))) + continue; + + /* Skip if the evsels don't program the same counter. */ + if (!counter_key__equal(&key, &entry->key)) + continue; + + if (!metric_leader_compatible(evsel, metric_sources, mapped_metric_leade= r, pos)) + continue; + + /* The encoding of events on a fake PMU, used in testing, is meaningless= . */ + if ((fake || (pos->pmu && perf_pmu__is_fake(pos->pmu))) && + !evsel__shares_metric_id(evsel, pos)) + continue; + + score =3D (!strcmp(evsel__name(evsel), evsel__name(pos)) ? 2 : 0) + + (grouped =3D=3D evsel__is_grouped(pos) ? 1 : 0); + if (score > best_score) { + best =3D pos; + best_score =3D score; + } + } + counter_key__exit(&key); + return best; +} + +static __maybe_unused struct evsel *evsel_matcher__find(struct evsel_match= er *matcher, + struct evsel *evsel, + const struct evsel *leader) +{ + return evsel_matcher__find_for_metric(matcher, evsel, leader, + /*metric_sources=3D*/0, + /*mapped_metric_leader=3D*/NULL, + EVSEL_GROUP_MATCH_UNGROUPED); +} + +/** + * evsel_matcher__find_group() - Find a group of evsels counting the same = thing + * as the members of the group led by @leade= r. + * @matcher: The matcher. + * @leader: The leader of the group to match. + * @own: Events of the metric being matched. + * @map: Matched replacements of @own. + * @nr: Number of events in @own. + * @out: Array, in the order of the group members, of the matched evsels. + */ +static bool evsel_matcher__find_group(struct evsel_matcher *matcher, struc= t evsel *leader, + struct evsel **own, struct evsel **map, int nr, + struct evsel **out) +{ + for (size_t i =3D 0; i < matcher->nr_entries; i++) { + struct evsel *pos_leader =3D matcher->entries[i].evsel; + struct evsel *member; + bool found =3D true; + int j =3D 0; + + if (!evsel__is_group_leader(pos_leader) || + pos_leader->core.nr_members < leader->core.nr_members) + continue; + + for_each_group_evsel(member, leader) { + int metric_sources; + const struct evsel *mapped_leader; + + own_evsel__metric_match_info(own, map, nr, member, + &metric_sources, &mapped_leader); + out[j] =3D evsel_matcher__find_for_metric(matcher, member, pos_leader, + metric_sources, mapped_leader, + EVSEL_GROUP_MATCH_ANY); + if (!out[j]) { + for (int k =3D 0; k < j; k++) + out[k] =3D NULL; + found =3D false; + break; + } + j++; + } + if (found) + return true; + } + return false; +} + struct find_matching_metrics_args { struct evlist *evlist; bool system_wide; @@ -1335,89 +1945,20 @@ static void metricgroup__free_metrics(struct list_h= ead *metric_list) } } =20 -/** - * find_tool_events - Search for the pressence of tool events in metric_li= st. - * @metric_list: List to take metrics from. - * @tool_events: Array of false values, indices corresponding to tool even= ts set - * to true if tool event is found. - */ -static void find_tool_events(const struct list_head *metric_list, - bool tool_events[TOOL_PMU__EVENT_MAX]) -{ - struct metric *m; - - list_for_each_entry(m, metric_list, nd) { - int i; - - tool_pmu__for_each_event(i) { - struct expr_id_data *data; - - if (!tool_events[i] && - !expr__get_id(m->pctx, tool_pmu__event_to_str(i), &data)) - tool_events[i] =3D true; - } - } -} - -/** - * build_combined_expr_ctx - Make an expr_parse_ctx with all !group_events - * metric IDs, as the IDs are held in a set, - * duplicates will be removed. - * @metric_list: List to take metrics from. - * @combined: Out argument for result. - */ -static int build_combined_expr_ctx(const struct list_head *metric_list, - struct expr_parse_ctx **combined) -{ - struct hashmap_entry *cur; - size_t bkt; - struct metric *m; - char *dup; - int ret; - - *combined =3D expr__ctx_new(); - if (!*combined) - return -ENOMEM; - - list_for_each_entry(m, metric_list, nd) { - if (!m->group_events && !m->modifier) { - hashmap__for_each_entry(m->pctx->ids, cur, bkt) { - dup =3D strdup(cur->pkey); - if (!dup) { - ret =3D -ENOMEM; - goto err_out; - } - ret =3D expr__add_id(*combined, dup); - if (ret) - goto err_out; - } - } - } - return 0; -err_out: - expr__ctx_free(*combined); - *combined =3D NULL; - return ret; -} - /** * parse_ids - Build the event string for the ids and parse them creating = an * evlist. The encoded metric_ids are decoded. - * @metric_no_merge: is metric sharing explicitly disabled. * @fake_pmu: use a fake PMU when testing metrics not supported by the cur= rent CPU. * @ids: the event identifiers parsed from a metric. * @modifier: any modifiers added to the events. * @group_events: should events be placed in a weak group. - * @tool_events: entries set true if the tool event of index could be pres= ent in - * the overall list of metrics. * @out_evlist: the created list of events. + * @filter_pmu: only parse events of this PMU, or NULL for all PMUs. + * @cputype_filter: is @filter_pmu from --cputype. */ -static int parse_ids(bool metric_no_merge, bool fake_pmu, - struct expr_parse_ctx *ids, const char *modifier, - bool group_events, const bool tool_events[TOOL_PMU__EVENT_MAX], - struct evlist **out_evlist, - const char *filter_pmu, - bool cputype_filter) +static int parse_ids(bool fake_pmu, struct expr_parse_ctx *ids, const char= *modifier, + bool group_events, struct evlist **out_evlist, + const char *filter_pmu, bool cputype_filter) { struct parse_events_error parse_error; struct evlist *parsed_evlist; @@ -1425,39 +1966,18 @@ static int parse_ids(bool metric_no_merge, bool fak= e_pmu, int ret; =20 *out_evlist =3D NULL; - if (!metric_no_merge || hashmap__size(ids->ids) =3D=3D 0) { - bool added_event =3D false; - int i; + if (hashmap__size(ids->ids) =3D=3D 0) { /* - * We may fail to share events between metrics because a tool - * event isn't present in one metric. For example, a ratio of - * cache misses doesn't need duration_time but the same events - * may be used for a misses per second. Events without sharing - * implies multiplexing, that is best avoided, so place - * all tool events in every group. - * - * Also, there may be no ids/events in the expression parsing - * context because of constant evaluation, e.g.: + * There may be no ids/events in the expression parsing context + * because of constant evaluation, e.g.: * event1 if #smt_on else 0 * Add a tool event to avoid a parse error on an empty string. */ - tool_pmu__for_each_event(i) { - if (tool_events[i]) { - char *tmp =3D strdup(tool_pmu__event_to_str(i)); + char *tmp =3D strdup("duration_time"); =20 - if (!tmp) - return -ENOMEM; - ids__insert(ids->ids, tmp); - added_event =3D true; - } - } - if (!added_event && hashmap__size(ids->ids) =3D=3D 0) { - char *tmp =3D strdup("duration_time"); - - if (!tmp) - return -ENOMEM; - ids__insert(ids->ids, tmp); - } + if (!tmp) + return -ENOMEM; + ids__insert(ids->ids, tmp); } ret =3D metricgroup__build_event_string(&events, ids, modifier, group_events); @@ -1546,6 +2066,359 @@ static struct evsel *pick_display_evsel(struct list= _head *metric_list, return selected; } =20 +/* Find @evsel in @own and return its replacement in @map, or itself if no= ne. */ +static struct evsel *own_evsel__mapped(struct evsel **own, struct evsel **= map, int nr, + struct evsel *evsel) +{ + for (int i =3D 0; i < nr; i++) { + if (own[i] =3D=3D evsel) + return map[i] ?: evsel; + } + return evsel; +} + +/* Add @evsel to the end of the group in @evlist led by @leader. */ +static void evlist__add_to_group(struct evlist *evlist, struct evsel *lead= er, + struct evsel *evsel) +{ + struct evsel *pos, *last =3D leader; + + for_each_group_evsel(pos, leader) + last =3D pos; + + evlist__add(evlist, evsel); + if (list_empty(&evsel->core.node)) + return; + + evsel__set_leader(evsel, leader); + evsel->core.nr_members =3D 0; + if (leader->core.nr_members < 1) + leader->core.nr_members =3D 1; + leader->core.nr_members++; + + if (list_next_entry(last, core.node) !=3D evsel) { + int idx =3D 0; + + list_move(&evsel->core.node, &last->core.node); + evlist__for_each_entry(evlist, pos) { + pos->core.idx =3D idx++; + pos->tracking =3D !pos->core.idx; + } + } +} + +/* + * Match a group created for arch_evsel__must_be_in_group() events, such a= s x86 + * topdown slots and perf-metrics events, to an existing such group, marki= ng any + * new members to be appended to it so ungrouped topdown metrics share one= group. + */ +static void metric__match_forced_group(struct evsel_matcher *matcher, stru= ct evsel *ev, + struct evsel **own, struct evsel **map, + struct evsel **extend_leader, int nr, int start) +{ + int metric_sources; + const struct evsel *mapped_leader; + struct evsel *pos_leader; + + own_evsel__metric_match_info(own, map, nr, ev, &metric_sources, &mapped_l= eader); + pos_leader =3D evsel_matcher__find_for_metric(matcher, ev, /*leader=3D*/N= ULL, + metric_sources, mapped_leader, + EVSEL_GROUP_MATCH_FORCED_LEADER); + if (!pos_leader) + return; + + map[start] =3D pos_leader; + for (int j =3D start + 1; j < nr && evsel__leader(own[j]) =3D=3D ev; j++)= { + own_evsel__metric_match_info(own, map, nr, own[j], + &metric_sources, &mapped_leader); + map[j] =3D evsel_matcher__find_for_metric(matcher, own[j], pos_leader, + metric_sources, mapped_leader, + EVSEL_GROUP_MATCH_ANY); + if (map[j]) + continue; + if (evsel_matcher__find(matcher, own[j], pos_leader)) { + for (int k =3D start; k < j; k++) { + map[k] =3D NULL; + extend_leader[k] =3D NULL; + } + return; + } + extend_leader[j] =3D pos_leader; + } +} + +/* Match an ungrouped event (and any multi-PMU siblings) to an earlier eve= nt of this metric. */ +static bool metric__match_ungrouped_intra(struct evsel_matcher *matcher, s= truct evsel **own, + struct evsel **map, struct evsel **extend_leader, + int nr, int idx) +{ + struct evsel *ev =3D own[idx]; + const struct evsel *mapped_leader; + struct evsel *cand; + int metric_sources; + + if (!evsel__is_group_leader(ev) || ev->core.nr_members > 1 || + (ev->metric_leader && ev->metric_leader !=3D ev)) + return false; + + own_evsel__metric_match_info(own, map, nr, ev, &metric_sources, &mapped_l= eader); + cand =3D evsel_matcher__find_for_metric(matcher, ev, /*leader=3D*/NULL, + metric_sources, mapped_leader, + EVSEL_GROUP_MATCH_UNGROUPED); + if (!cand) + return false; + + for (int j =3D idx + 1; metric_sources > 1 && j < nr; j++) { + if (own[j]->metric_leader !=3D ev) + continue; + if (map[j] || extend_leader[j] || !evsel__is_group_leader(own[j]) || + own[j]->core.nr_members > 1 || + !evsel_matcher__find_for_metric(matcher, own[j], /*leader=3D*/NULL, + metric_sources, cand, + EVSEL_GROUP_MATCH_UNGROUPED)) + return false; + } + + map[idx] =3D cand; + for (int j =3D idx + 1; metric_sources > 1 && j < nr; j++) { + if (own[j]->metric_leader =3D=3D ev) { + map[j] =3D evsel_matcher__find_for_metric(matcher, own[j], /*leader=3D*= /NULL, + metric_sources, cand, + EVSEL_GROUP_MATCH_UNGROUPED); + } + } + return true; +} + +/** + * metric__adopt_events - Move the events parsed for @m to @perf_evlist. + * @m: The metric whose events, in m->evlist, are moved. + * @perf_evlist: The evlist the metric's events are added to. + * @matcher: If not NULL, a matcher of the events of @perf_evlist. An even= t of + * @perf_evlist counting the same thing as an event of the metri= c, + * such as from an earlier metric, an earlier metric group or the + * command line, is used instead of the metric's event. A group = of + * the metric is only replaced if all of its events are found in= one + * group of @perf_evlist, unless grouping was only forced by the + * architecture (e.g. x86 topdown perf-metrics), in which case n= ew + * members are added to the existing forced group. + * + * The metric's metric_events, and the metric leaders of the events, are + * updated to refer to the events used in @perf_evlist. + */ +static int metric__adopt_events(struct metric *m, struct evlist *perf_evli= st, + struct evsel_matcher *matcher) +{ + int nr =3D evlist__nr_entries(m->evlist); + struct evsel **own, **map, **extend_leader; + struct evsel *ev, *first_hw_leader =3D NULL; + bool changed, single_hw_metric =3D true; + int i =3D 0, j, ret =3D 0; + + if (!nr) + return 0; + + own =3D calloc(nr, sizeof(*own)); + map =3D calloc(nr, sizeof(*map)); + extend_leader =3D calloc(nr, sizeof(*extend_leader)); + if (!own || !map || !extend_leader) { + ret =3D -ENOMEM; + goto out; + } + evlist__for_each_entry(m->evlist, ev) { + if (!evsel__is_tool(ev)) { + struct evsel *ml =3D ev->metric_leader ?: ev; + + if (!first_hw_leader) + first_hw_leader =3D ml; + else if (ml !=3D first_hw_leader) + single_hw_metric =3D false; + } + own[i++] =3D ev; + } + + for (i =3D 0; matcher && i < nr; i++) { + int metric_sources; + const struct evsel *mapped_leader; + + ev =3D own[i]; + if (!evsel__is_group_leader(ev)) + continue; + + if (ev->core.nr_members > 1) { + if (!evsel_matcher__find_group(matcher, ev, own, map, nr, &map[i]) && + !m->group_events && evsel__is_forced_group(ev)) { + metric__match_forced_group(matcher, ev, own, map, + extend_leader, nr, i); + } + } else { + if (!m->group_events && evsel__is_forced_group(ev)) { + metric__match_forced_group(matcher, ev, own, map, + extend_leader, nr, i); + } + if (!map[i]) { + enum evsel_group_match group_match =3D + (m->group_events || evsel__is_forced_group(ev)) && + single_hw_metric && !evsel__is_tool(ev) + ? EVSEL_GROUP_MATCH_ANY + : EVSEL_GROUP_MATCH_UNGROUPED; + + own_evsel__metric_match_info(own, map, nr, ev, + &metric_sources, &mapped_leader); + map[i] =3D evsel_matcher__find_for_metric(matcher, ev, + /*leader=3D*/NULL, + metric_sources, + mapped_leader, + group_match); + } + } + } + + /* + * All events sharing a metric_leader across PMUs must either be + * replaced together or kept together so metric_leader and source_count + * remain consistent. + */ + do { + changed =3D false; + for (i =3D 0; i < nr; i++) { + struct evsel *ml =3D own[i]->metric_leader; + int l_idx =3D -1; + + if (!ml || ml =3D=3D own[i]) + continue; + for (j =3D 0; j < nr; j++) { + if (own[j] =3D=3D ml) { + l_idx =3D j; + break; + } + } + if (l_idx < 0 || !map[i] =3D=3D !map[l_idx]) + continue; + for (j =3D 0; j < nr; j++) { + struct evsel *grp; + + if (own[j]->metric_leader !=3D ml) + continue; + grp =3D evsel__leader(own[j]); + for (int k =3D 0; k < nr; k++) { + if (evsel__leader(own[k]) =3D=3D grp && + (map[k] || extend_leader[k])) { + map[k] =3D NULL; + extend_leader[k] =3D NULL; + changed =3D true; + } + } + } + } + } while (changed); + + /* Move the events not replaced, making them available to match. */ + for (i =3D 0; i < nr; i++) { + if (map[i]) + continue; + if (matcher && extend_leader[i]) { + int metric_sources; + const struct evsel *mapped_leader; + + own_evsel__metric_match_info(own, map, nr, own[i], + &metric_sources, &mapped_leader); + map[i] =3D evsel_matcher__find_for_metric(matcher, own[i], extend_leade= r[i], + metric_sources, mapped_leader, + EVSEL_GROUP_MATCH_ANY); + if (map[i]) { + extend_leader[i] =3D NULL; + continue; + } + } else if (matcher && + metric__match_ungrouped_intra(matcher, own, map, extend_leader, nr, = i)) { + continue; + } + evlist__remove(m->evlist, own[i]); + if (extend_leader[i]) + evlist__add_to_group(perf_evlist, extend_leader[i], own[i]); + else + evlist__add(perf_evlist, own[i]); + if (matcher && !list_empty(&own[i]->core.node)) { + ret =3D evsel_matcher__add(matcher, own[i]); + if (ret) + goto out; + } + } + + /* + * Update first_wildcard_match on kept events if the first match was + * replaced and will be freed. + */ + for (i =3D 0; i < nr; i++) { + struct evsel *fwm =3D own[i]->first_wildcard_match; + + if (map[i] || !fwm) + continue; + if (own_evsel__mapped(own, map, nr, fwm) =3D=3D fwm) + continue; + own[i]->first_wildcard_match =3D NULL; + for (j =3D i + 1; j < nr; j++) { + if (own[j]->first_wildcard_match =3D=3D fwm) + own[j]->first_wildcard_match =3D own[i]; + } + } + + /* Fix up the metric ids and metric leaders of the events. */ + for (i =3D 0; i < nr; i++) { + struct evsel *leader =3D own[i]->metric_leader; + + if (leader) + leader =3D own_evsel__mapped(own, map, nr, leader); + if (!map[i]) { + own[i]->metric_leader =3D leader; + continue; + } + pr_debug("Using event %s for metric %s's event %s\n", + evsel__name(map[i]), m->metric_name, evsel__name(own[i])); + ret =3D evsel__copy_metric_ids(map[i], own[i]); + if (ret) + goto out; + if (own[i]->collect_stat) + map[i]->collect_stat =3D true; + if (leader && !map[i]->metric_leader) + map[i]->metric_leader =3D leader; + } + + /* + * Different events of the metric may be replaced by the same event, + * whose metric ids are those of both. Only keep the first so the + * event's value isn't counted more than once. + */ + for (i =3D 0, j =3D 0; m->metric_events[i]; i++) { + struct evsel *mapped =3D own_evsel__mapped(own, map, nr, m->metric_event= s[i]); + bool dup =3D false; + + for (int k =3D 0; k < j; k++) { + if (m->metric_events[k] =3D=3D mapped) { + dup =3D true; + break; + } + } + if (!dup) + m->metric_events[j++] =3D mapped; + } + m->metric_events[j] =3D NULL; + + /* Free the replaced events. */ + for (i =3D 0; i < nr; i++) { + if (map[i]) { + evlist__remove(m->evlist, own[i]); + evsel__put(own[i]); + } + } +out: + free(own); + free(map); + free(extend_leader); + return ret; +} + static int parse_groups(struct evlist *perf_evlist, const char *pmu, bool cputype_filter, const char *str, bool metric_no_group, @@ -1557,13 +2430,21 @@ static int parse_groups(struct evlist *perf_evlist, const struct pmu_metrics_table *table) { struct perf_pmus *pmus =3D perf_env__pmus(evlist__env(perf_evlist) ?: hos= t_env()); - struct evlist *combined_evlist =3D NULL; + struct evsel_matcher matcher; LIST_HEAD(metric_list); struct metric *m; - bool tool_events[TOOL_PMU__EVENT_MAX] =3D {false}; bool is_default =3D !strcmp(str, "Default"); + const char *filter_pmu =3D (pmu && strcmp(pmu, "all") =3D=3D 0) ? NULL : = pmu; int ret; =20 + /* + * Unless sharing is disabled, metrics use the events of other metrics, + * and those already in perf_evlist, that count the same thing. + */ + ret =3D evsel_matcher__init(&matcher, perf_evlist); + if (ret) + goto out; + ret =3D metricgroup__add_metric_list(pmus, pmu, str, metric_no_group, met= ric_no_threshold, user_requested_cpu_list, system_wide, fake_pmu, @@ -1571,28 +2452,27 @@ static int parse_groups(struct evlist *perf_evlist, if (ret) goto out; =20 - /* Sort metrics from largest to smallest. */ + /* + * Sort metrics from largest to smallest so that the groups of smaller + * metrics can be found within the groups of larger ones. + */ list_sort(NULL, &metric_list, metric_list_cmp); =20 - if (!metric_no_merge) { - struct expr_parse_ctx *combined =3D NULL; - - find_tool_events(&metric_list, tool_events); - - ret =3D build_combined_expr_ctx(&metric_list, &combined); + list_for_each_entry(m, &metric_list, nd) { + ret =3D parse_ids(fake_pmu, m->pctx, m->modifier, m->group_events, &m->e= vlist, + filter_pmu, cputype_filter); + if (ret) + goto out; =20 - if (!ret && combined && hashmap__size(combined->ids)) { - ret =3D parse_ids(metric_no_merge, fake_pmu, combined, - /*modifier=3D*/NULL, - /*group_events=3D*/false, - tool_events, - &combined_evlist, - (pmu && strcmp(pmu, "all") =3D=3D 0) ? NULL : pmu, - cputype_filter); + ret =3D setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids, + m->evlist, &m->metric_events); + if (ret) { + pr_err("Cannot resolve IDs for %s: %s\n", + m->metric_name, m->metric_expr); + goto out; } - if (combined) - expr__ctx_free(combined); =20 + ret =3D metric__adopt_events(m, perf_evlist, metric_no_merge ? NULL : &m= atcher); if (ret) goto out; } @@ -1602,71 +2482,19 @@ static int parse_groups(struct evlist *perf_evlist, =20 list_for_each_entry(m, &metric_list, nd) { struct metric_event *me; - struct evsel **metric_events; - struct evlist *metric_evlist =3D NULL; - struct metric *n; struct metric_expr *expr; =20 - if (combined_evlist && !m->group_events) { - metric_evlist =3D combined_evlist; - } else if (!metric_no_merge) { - /* - * See if the IDs for this metric are a subset of an - * earlier metric. - */ - list_for_each_entry(n, &metric_list, nd) { - if (m =3D=3D n) - break; - - if (n->evlist =3D=3D NULL) - continue; - - if ((!m->modifier && n->modifier) || - (m->modifier && !n->modifier) || - (m->modifier && n->modifier && - strcmp(m->modifier, n->modifier))) - continue; - - if ((!m->pmu && n->pmu) || - (m->pmu && !n->pmu) || - (m->pmu && n->pmu && strcmp(m->pmu, n->pmu))) - continue; - - if (expr__subset_of_ids(n->pctx, m->pctx)) { - pr_debug("Events in '%s' fully contained within '%s'\n", - m->metric_name, n->metric_name); - metric_evlist =3D n->evlist; - break; - } - - } - } - if (!metric_evlist) { - ret =3D parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier, - m->group_events, tool_events, &m->evlist, - (pmu && strcmp(pmu, "all") =3D=3D 0) ? NULL : pmu, - cputype_filter); - if (ret) - goto out; - - metric_evlist =3D m->evlist; - } - ret =3D setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids, - metric_evlist, &metric_events); - if (ret) { - pr_err("Cannot resolve IDs for %s: %s\n", - m->metric_name, m->metric_expr); - goto out; - } - me =3D metricgroup__lookup(evlist__metric_events(perf_evlist), - pick_display_evsel(&metric_list, metric_events), + pick_display_evsel(&metric_list, m->metric_events), /*create=3D*/true); + if (!me) { + ret =3D -ENOMEM; + goto out; + } =20 expr =3D malloc(sizeof(struct metric_expr)); if (!expr) { ret =3D -ENOMEM; - free(metric_events); goto out; } =20 @@ -1685,44 +2513,32 @@ static int parse_groups(struct evlist *perf_evlist, =20 if (!expr->metric_name) { ret =3D -ENOMEM; + free(expr->metric_refs); free(expr); - free(metric_events); goto out; } if (m->default_show_events) { struct evsel *pos; =20 - for (int i =3D 0; metric_events[i]; i++) - metric_events[i]->default_show_events =3D true; - evlist__for_each_entry(metric_evlist, pos) { + for (int i =3D 0; m->metric_events[i]; i++) + m->metric_events[i]->default_show_events =3D true; + evlist__for_each_entry(perf_evlist, pos) { if (pos->metric_leader && pos->metric_leader->default_show_events) pos->default_show_events =3D true; } } expr->metric_threshold =3D m->metric_threshold; expr->metric_unit =3D m->metric_unit; - expr->metric_events =3D metric_events; + expr->metric_events =3D m->metric_events; + m->metric_events =3D NULL; expr->runtime =3D m->pctx->sctx.runtime; expr->default_metricgroup_name =3D m->default_metricgroup_name; me->is_default =3D is_default; list_add(&expr->nd, &me->head); } =20 - - if (combined_evlist) { - evlist__splice_list_tail(perf_evlist, &evlist__core(combined_evlist)->en= tries); - evlist__put(combined_evlist); - combined_evlist =3D NULL; - } - - list_for_each_entry(m, &metric_list, nd) { - if (m->evlist) - evlist__splice_list_tail(perf_evlist, &evlist__core(m->evlist)->entries= ); - } - out: - if (combined_evlist) - evlist__put(combined_evlist); + evsel_matcher__exit(&matcher); metricgroup__free_metrics(&metric_list); return ret; } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212953; b=nOcouZXZQtVYdVfBLbq5GLIU00MmANTSISRFqNvO5Jh1atmCq8TyAD48zBI1dFBFm15c zSWlUK1ddryludks6bgujPbtD46FtGFC1xLgC7Fr9VBPMCOTyhNsjhfON+h6/VSENt0+G cVdQNj6bpJwIKeBmM9Kv2fBes0sg1/uUrTek0JXj4c17xmKNc5EJn+FvQp9Ukevr/5R6X s7Fv+PS4i2BFWv37MHP5aDFyESJX5wXcZSKzDIGiAD1f32J197IDNsE9BzYUQvNaef3tO RCM2g5SyUBuNcQ247ueIj60XL4KVryHCk2UWGmXYcOyYgv2N4rOf8mJa3PqhAfMujWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212953; 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=WR4sp0IgBSLp48NAmbVB6Wvzuz5GP2e/9tmn2eNf8a8=; b=0CG6SiyNEb9A1G7s9gmyQoKqFeYZ7bSjGogbQCgahiDSrQTr6ilcnMERuUI+bvMwSvee Va+iuYsZ37EG9dLRDJYY8eDEFpnwdXyc2qzhF70RMPEagDzAZ2f4aViNdHvCD9F2PbPVD a5nlQGY6gX0bjzK7qdK8d/5qT6+0X+m860awyiG5MvZ0zPZpDUq+SodC+B7gYnQ4orVsS wY1I+yXIMhSpWQbgjXb3HmUno9bKUjqTghC6oGqAv57Cz9Pv30k7R4AYjFgJgfGYrMe6o Y4f3JBlj+KpoI7v9VKB5sMhBcpUOvZjD53KOTxBTo3Uhbml3wBuoAxpa2xLpQQOSveg== 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=WROmI8t1; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 DADA1606A6B7 for ; Fri, 20 Nov 2026 22:15:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DADA1606A6B7 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=WROmI8t1 Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-383ec96a7deso1546010eec.1 for ; Fri, 20 Nov 2026 14:15:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212941; x=1795817741; 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=WR4sp0IgBSLp48NAmbVB6Wvzuz5GP2e/9tmn2eNf8a8=; b=WROmI8t1ONeuTS4dThJBhirUwZP+ArYxlfWnYBTd4ONUCKxCThl9rhDiYN4A/c4sVa HTmujYhiWqsbWcEEYtNwaGvR1hc3QjNoC83ZuHRm24SgSHfuWm6hwv9fbNNrJrwOz+uH ZbNTSNwPuSDkI3RQMbc7Q1B6nKgT869fNMJXsWTJcmuu1nouSW+7tX8VxRH9mQtrJNbr pUnUWL4vEFGhiPKFo4HJIBJI7JH2OvMPxJailjblO0BJEKWZBh2yX4y4OOV3RILvtQg7 aJcElNQVkux2KxcfKc7tG5pdxThO2YKXn7UotBqqv/0P/sxs0Wkxhb/NgPw5FaQnUDFl 42Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212941; x=1795817741; 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=WR4sp0IgBSLp48NAmbVB6Wvzuz5GP2e/9tmn2eNf8a8=; b=ZPoiLwJK+lm6Ixoezu4pjlXXtj01RRs2vwjTfIQ1YMXd5AlGiIZun7CpslOXeNNKT3 k7ROAewh6f2oh731STatB9CjMk4ombrM+GMxkKj2//FFGC1ptVFtRhbrbdWfEE0HLcJy QU/1gPsE2mAlx1PkK+wOcj48vZ/xjsnZ5QGTqNwobdQUUNiWFuHcPdMOowOCeAf/EsJ4 /wrn8uDa58q4kpPvnfGaw2pbYDJMl8KKyqWgJv8qxtEJwdfS1Edzf0X/Ur5yZJEDpCQn bPDUZSuhNd6elMDO/SIue44ktn604hgN5Wp4z3Fby3KkI7eB19uqpJUjRMb3gymAywqd 8hog== X-Forwarded-Encrypted: i=1; AL+hErFLHA4Kw6yEqVh4QuL/5lwqJRm9TYAZAPas+QQm9dsa/KyIZtJIOjEjf4Qy9da7IC4vJ3+kpKlAC7QCF2M=@vger.kernel.org X-Gm-Message-State: APuU4Z/dApMpopbJCeQtuPllXoV6XE/kL4qS/4vKJckKvfUq0UrNGp9O ZLWehsfswJWMvzMU+QqpkyeSjDOXLrtn9Z0LSYj8rb8SvRd7IWbXtmwpxxyHSi1awz9qhGvnyqt hynRsZIJvdA== X-Received: from dyee6.prod.google.com ([2002:a05:693c:6286:b0:386:6850:930a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:4201:b0:385:b8f4:755d with SMTP id 5a478bee46e88-385b8f47ad3mr3501940eec.20.1795212940199; Fri, 20 Nov 2026 14:15:40 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:25 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-25-irogers@google.com> Subject: [PATCH v3 24/30] perf stat: Share events between metrics and command line events 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The metrics added by -T, --smi-cost, --topdown and the Default metric groups were parsed into a separate evlist and then added to the events from the command line, so they didn't share events with them or, for the Default metric groups, with each other. Parse them directly into evsel_list so that the evsel matcher of metricgroup__parse_groups() shares events counting the same thing. Only mark the newly added events as skippable and keep the evsel indices in list order after sorting the Default metric events, as required when writing the group descriptions of a perf.data file. This removes the copying of the metric events from the separate evlist with metricgroup__copy_metric_events(), which crashed for "perf stat -T" on an Alder Lake system. For example, "perf stat -e duration_time -T" now uses the duration_time event from the command line for the transaction metrics. Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 72 +++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index f81192cec481..2b563382cf43 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1975,6 +1975,15 @@ static int default_evlist_evsel_cmp(void *priv __may= be_unused, evsel__name((struct evsel *)rhs_leader)); } =20 +static bool evsel_in_array(const struct evsel *evsel, struct evsel * const= *evsels, int nr) +{ + for (int i =3D 0; i < nr; i++) { + if (evsels[i] =3D=3D evsel) + return true; + } + return false; +} + /* * Add default events, if there were no attributes specified or * if -d/--detailed, -d -d or -d -d -d is used: @@ -1984,18 +1993,31 @@ static int add_default_events(void) const char *pmu =3D parse_events_option_args.pmu_filter ?: "all"; struct perf_pmus *pmus =3D perf_env__pmus(host_env()); struct parse_events_error err; - struct evlist *evlist =3D evlist__new(); + /* + * Add the metrics directly to evsel_list so that they share events + * with the events and metrics already there. + */ + struct evlist *evlist =3D evsel_list; + int nr_orig_entries =3D evlist__nr_entries(evlist); + struct evsel **orig_evsels =3D NULL; struct evsel *evsel; - int ret =3D 0; + int idx =3D 0, ret =3D 0; =20 - if (!evlist) - return -ENOMEM; + /* Set attrs if no event is selected and !null_run: */ + if (stat_config.null_run) + return 0; =20 parse_events_error__init(&err); =20 - /* Set attrs if no event is selected and !null_run: */ - if (stat_config.null_run) - goto out; + if (nr_orig_entries > 0) { + orig_evsels =3D calloc(nr_orig_entries, sizeof(*orig_evsels)); + if (!orig_evsels) { + ret =3D -ENOMEM; + goto out; + } + evlist__for_each_entry(evlist, evsel) + orig_evsels[idx++] =3D evsel; + } =20 if (transaction_run) { /* Handle -T as -M transaction. Once platform specific metrics @@ -2101,7 +2123,7 @@ static int add_default_events(void) if (!stat_config.topdown_level) stat_config.topdown_level =3D 1; =20 - if (!evlist__nr_entries(evlist) && !evlist__nr_entries(evsel_list)) { + if (!evlist__nr_entries(evlist)) { /* * Add Default metrics. To minimize multiplexing, don't request * threshold computation, but it will be computed if the events @@ -2112,8 +2134,6 @@ static int add_default_events(void) }; =20 for (size_t i =3D 0; i < ARRAY_SIZE(default_metricgroup_names); i++) { - struct evlist *metric_evlist; - if (!metricgroup__has_metric_or_groups(pmus, pmu, default_metricgroup_names[i])) continue; @@ -2121,12 +2141,7 @@ static int add_default_events(void) if ((int)i > detailed_run) break; =20 - metric_evlist =3D evlist__new(); - if (!metric_evlist) { - ret =3D -ENOMEM; - break; - } - if (metricgroup__parse_groups(metric_evlist, pmu, + if (metricgroup__parse_groups(evlist, pmu, parse_events_option_args.cputype_filter, default_metricgroup_names[i], /*metric_no_group=3D*/false, @@ -2135,26 +2150,29 @@ static int add_default_events(void) stat_config.user_requested_cpu_list, stat_config.system_wide, stat_config.hardware_aware_grouping) < 0) { - evlist__put(metric_evlist); ret =3D -1; break; } =20 - evlist__for_each_entry(metric_evlist, evsel) + /* evsel_list was empty so all the events are for the Default metrics. = */ + evlist__for_each_entry(evlist, evsel) evsel->default_metricgroup =3D true; - - evlist__splice_list_tail(evlist, &evlist__core(metric_evlist)->entries); - metricgroup__copy_metric_events(evlist, /*cgrp=3D*/NULL, - evlist__metric_events(evlist), - evlist__metric_events(metric_evlist)); - evlist__put(metric_evlist); } list_sort(/*priv=3D*/NULL, &evlist__core(evlist)->entries, default_evlis= t_evsel_cmp); + /* Keep the indices in list order, as required by the perf.data header. = */ + idx =3D 0; + evlist__for_each_entry(evlist, evsel) { + evsel->core.idx =3D idx++; + evsel->tracking =3D !evsel->core.idx; + } =20 } out: if (!ret) { evlist__for_each_entry(evlist, evsel) { + /* Only events added here, not those from the command line. */ + if (evsel_in_array(evsel, orig_evsels, nr_orig_entries)) + continue; /* * Make at least one event non-skippable so fatal errors are visible. * 'cycles' always used to be default and non-skippable, so use that. @@ -2163,12 +2181,8 @@ static int add_default_events(void) evsel->skippable =3D true; } } + free(orig_evsels); parse_events_error__exit(&err); - evlist__splice_list_tail(evsel_list, &evlist__core(evlist)->entries); - metricgroup__copy_metric_events(evsel_list, /*cgrp=3D*/NULL, - evlist__metric_events(evsel_list), - evlist__metric_events(evlist)); - evlist__put(evlist); return ret; } =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212950; b=lyL2Q/UnqaUl1gqlQ5pRu25MXe2A8SsBcVhvlduD+h9vc4MO9k9PeksWX+ibF719g6Mk Tj6U84JfPBWqGhk3hyTy2i5tUJ/zrrP8YCtUusNS6u04Df4KDVBYG/v39C37ySLDo2TGU spmTmi73i2R9g2/f4kzbgX6ndJ8S6t10e1U7FWx17hd/iXBhuY3bwmoJbKEUl4lLRbUkb P18NVqjbxZX3G1GMVoj9cJ1tnBwWkKypQohnRNFUmqm5yONIDlFVG+D6clkmaJ/xvGh3p dLaY+wJ3M0ESwknXHilaIjB0SihoMpycSizho2uozCUFk82WIK4pZc7JzbaAbyoLIeA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212950; 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=VdpcP9WP0vVl+tdJg7sdWbUAUkv/2aKzpjU1W/XHBiI=; b=uBVfDvgFQHExr1lkWBQfZBIeY/6KLHQPvBBtzyUmzNvN8njx2n5/6KE1ekiqpeQljntw Nl4+ncawV6ciY3Bxif88rq5vXQ1ZYJiYZ63DGDr1yJJz+bAFU5Ec8WyQD/FHhBp1r9uSX cefDCVkekL98rc8TMGSAqJ854abvaAwxdU7IUN5/DLbj+yLMng0i0YU5dVSdgvHQBZGLg SkEhAMHvhl17XJjODLoIbS8wFnFlUg58ZoKSqp2s9P8O8foOAQgkslnAghkFWTAbdZzzX uXVn4vWll5/Q4DShta6Bn0YZ8dT60edJQ1AQGlgsSJW/dh9vze+IAQyIebKFySyKUwQ== 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=dRNAO7pv; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 DB897606A33D for ; Fri, 20 Nov 2026 22:15:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DB897606A33D 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=dRNAO7pv Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-38531c1a566so3260244eec.1 for ; Fri, 20 Nov 2026 14:15:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212945; x=1795817745; 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=VdpcP9WP0vVl+tdJg7sdWbUAUkv/2aKzpjU1W/XHBiI=; b=dRNAO7pvO5sHlaZSfZHkcK+1/qjhi9FF9pKDrMzGmDUkztTDwBD2HkGvbcXEjVX7vN mhdzvJ+V0+EJI4qNokiY0vy6SQyzWExF7pzFMz4GKHooqsFEu13awhrP2S7bDr/hq6ve YMk6pWkREedNeZOLSKK9nVrflCbch4uCB+bCNznajevZxrjBQqE6NZkNGSDuH2oq8OHa +/GGiPWnj2AxwbWAlQCaMOlZlR6733nuCwBWWAqRQD4XD+dy1eme2RAMxmUDXarftKA/ 7vsWI5dLjaurdVBti8cCgRV2uZmkkRlrBNTOYXFia/tPjpmrwBVgTup+jgzMdD9jH5kV Jd/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212945; x=1795817745; 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=VdpcP9WP0vVl+tdJg7sdWbUAUkv/2aKzpjU1W/XHBiI=; b=uGR/bs3uNP5YoMUDTR6K978ORk4/pxi3KgD9+KOK7cyu5AVvRXNW1x73nLEw1kRsEc anAiMD+Jq1Uak/D8+1gBOyWLTXwzq/QJKr0D5XD+WcuLm/GUJ/7XEr93bu+eViMAOIHZ ZI5ZrYFXDO/tGc4Rr09RW46CybSd44XOWxnZRL561fOZhnMybQzPRhuVsaMvyQFsqW1q GplHUZuL/l1gHFEO2f5bNAAojSkpLIK9sXXRPn0ciocZEMvQ9JcMCzA2d8Lq+mQC9Qys yFEpVDPYAUwr4bx/ANXLM8tAZ+3lt0b2VP+UqK764D55ghybUTycXDNn4qO01YcYktH2 6IHA== X-Forwarded-Encrypted: i=1; AL+hErGDTes3KdbJ+xlGrsypspo2EjLJ79PYFnCIJEpaQ+F27nP4gmXaFjJy810NGbPsUuv4LpCKpz7m/6FEcvg=@vger.kernel.org X-Gm-Message-State: APuU4Z9aG/U8OBM+WKvRR+tWUv+AW16eRQvWnEuWdI9YAcDXW6jjjW4N BbG08YQY09clxThFPQd5mSK2Neu2S2ug4lhVuLbKfffRelaS4BAC2Yu92nYpZa0cJv/+ajEcljR 2Kt/yRHONmw== X-Received: from dyu12.prod.google.com ([2002:a05:693c:810c:b0:35d:1771:4816]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:d583:b0:380:d99d:ce1d with SMTP id 5a478bee46e88-386992b3fdemr876018eec.15.1795212944703; Fri, 20 Nov 2026 14:15:44 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:26 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-26-irogers@google.com> Subject: [PATCH v3 25/30] perf pmus: Add perf_pmus__read_from_env() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add perf_pmus__read_from_env() and perf_pmu__add_cap() to reconstruct core_pmus and other_pmus lists and their capabilities in a struct perf_pmus from HEADER_PMU_MAPPINGS, HEADER_HYBRID_TOPOLOGY, HEADER_PMU_CAPS, and HEADER_CPU_PMU_CAPS in a perf.data header environment, and lazily populate env->pmus in perf_env__pmus(). An environment without any PMU header metadata, such as one used while recording, falls back to the live host's PMUs rather than re-reading sysfs into its own copy. Ensure core_pmus is never left empty by creating a fallback "cpu" core PMU when no core PMU was found in the headers. Because perf.data headers do not record sysfs format definitions, only attach core_table to PMUs with matching entries and teach perf_pmu__for_each_name_from_config() to delegate to a matching live host PMU or fall back to legacy hardware and cache event names. Signed-off-by: Ian Rogers --- tools/perf/util/env.c | 19 +++ tools/perf/util/env.h | 5 + tools/perf/util/header.c | 23 ++++ tools/perf/util/pmu.c | 56 ++++++++- tools/perf/util/pmu.h | 9 ++ tools/perf/util/pmus.c | 247 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/pmus.h | 15 ++- 7 files changed, 372 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index fec6a5a8ecb6..a6f0baef3ccf 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -341,6 +341,25 @@ struct perf_env *host_env(void) =20 struct perf_pmus *perf_env__pmus(struct perf_env *env) { + struct perf_env *host =3D host_env(); + + if (env =3D=3D host || + (env->pmus.read_pmu_types =3D=3D 0 && + !(env->nr_pmu_mappings || env->nr_hybrid_nodes || + env->nr_cpu_pmu_caps || env->nr_pmus_with_caps))) { + /* No PMU metadata of its own, use the live host's PMUs. */ + return &host->pmus; + } + + mutex_lock(&env->lock); + if (!env->pmus.core_pmus.next || !env->pmus.other_pmus.next) + perf_pmus__init(&env->pmus); + mutex_unlock(&env->lock); + + mutex_lock(&env->pmus.lock); + if (env->pmus.read_pmu_types =3D=3D 0) + perf_pmus__read_from_env(&env->pmus, env); + mutex_unlock(&env->pmus.lock); return &env->pmus; } =20 diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index e38e953060c9..ef583161583f 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -238,6 +238,11 @@ struct perf_env *host_env(void); * perf_env__pmus() - Return the struct perf_pmus associated with @env. * @env: Environment whose PMU collection to retrieve. * + * For non-host environments with PMU header metadata (HEADER_PMU_MAPPINGS, + * HEADER_HYBRID_TOPOLOGY, HEADER_CPU_PMU_CAPS or HEADER_PMU_CAPS), lazily + * populates @env->pmus from the perf.data header on first access. If @env= has + * no PMU metadata of its own, falls back to the live host's PMU collectio= n. + * * Return: Pointer to the struct perf_pmus for @env. */ struct perf_pmus *perf_env__pmus(struct perf_env *env); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index c1f77babcb70..48b7a86f6f9d 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -5762,6 +5762,29 @@ int perf_event__process_feature(const struct perf_to= ol *tool __maybe_unused, goto out; } =20 + switch (feat) { + case HEADER_ARCH: + case HEADER_CPUID: + case HEADER_NRCPUS: + case HEADER_PMU_MAPPINGS: + case HEADER_CPU_PMU_CAPS: + case HEADER_HYBRID_TOPOLOGY: + case HEADER_PMU_CAPS: + case HEADER_E_MACHINE: + mutex_lock(&header->env.pmus.lock); + if (session->evlist) { + struct evsel *evsel; + + evlist__for_each_entry(session->evlist, evsel) + evsel->pmu =3D NULL; + } + perf_pmus__exit(&header->env.pmus); + mutex_unlock(&header->env.pmus.lock); + break; + default: + break; + } + if (session->tool->show_feat_hdr) { if (!feat_ops[feat].full_only || session->tool->show_feat_hdr >=3D SHOW_FEAT_HEADER_FULL_INFO) { diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index b4541fbfc9c0..1d3cfa082347 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -2250,7 +2250,7 @@ static bool perf_pmu_alias__check_deprecated(struct p= erf_pmu *pmu, struct perf_p const char *check_terms; bool has_legacy_config; =20 - if (alias->legacy_deprecated_checked) + if (pmu->from_env || alias->legacy_deprecated_checked) return alias->deprecated; =20 alias->legacy_deprecated_checked =3D true; @@ -2583,6 +2583,37 @@ static int perf_pmu__new_caps(struct list_head *list= , char *name, char *value) return -ENOMEM; } =20 +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 =3D zalloc(sizeof(*caps)); + if (!caps) + return -ENOMEM; + + caps->name =3D strdup(name); + if (!caps->name) { + free(caps); + return -ENOMEM; + } + caps->value =3D 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 =3D atoi(value); + return 0; +} + static void perf_pmu__del_caps(struct perf_pmu *pmu) { struct perf_pmu_caps *caps, *tmp; @@ -2881,6 +2912,28 @@ int perf_pmu__for_each_name_from_config(struct perf_= pmu *pmu, __u32 type, u64 co if (!pmu) return 0; =20 + /* + * PMUs reconstructed from perf.data headers lack sysfs format files, so + * format-based JSON aliases cannot be parsed directly. Delegate to the + * matching live host PMU when available, otherwise only resolve legacy + * hardware and cache encodings. + */ + if (pmu->from_env && list_empty(&pmu->format) && + pmu->events_table !=3D perf_pmu__default_core_events_table()) { + struct perf_pmu *host_pmu =3D + perf_pmus__find(perf_env__pmus(host_env()), pmu->name); + + if (host_pmu && !host_pmu->from_env && + host_pmu->events_table =3D=3D pmu->events_table && + (pmu->events_table || !pmu->is_core)) { + __u32 match_type =3D (type =3D=3D pmu->type) ? host_pmu->type : type; + + return perf_pmu__for_each_name_from_config(host_pmu, match_type, + config, cb, data); + } + goto legacy_names; + } + pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); hashmap__for_each_entry(pmu->aliases, entry, bkt) { @@ -2894,6 +2947,7 @@ int perf_pmu__for_each_name_from_config(struct perf_p= mu *pmu, __u32 type, u64 co if (ret) return ret; } +legacy_names: if (pmu->is_core && (type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D PERF_T= YPE_HW_CACHE)) { struct legacy_name_args args =3D { .type =3D type, diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index ab7123964423..b556ec51fdeb 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -334,6 +334,15 @@ bool pmu_uncore_identifier_match(const char *compat, c= onst char *id); int perf_pmu__convert_scale(const char *scale, char **end, double *sval); =20 struct perf_pmu_caps *perf_pmu__get_cap(struct perf_pmu *pmu, const char *= name); +/** + * perf_pmu__add_cap() - Add a name/value capability entry to @pmu->caps. + * @pmu: The PMU to add the capability to. + * @name: Capability name. + * @value: Capability value string. + * + * Return: 0 on success, negative error code on failure. + */ +int perf_pmu__add_cap(struct perf_pmu *pmu, const char *name, const char *= value); =20 int perf_pmu__caps_parse(struct perf_pmu *pmu); =20 diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 1c3cb53c3d4f..c5ef22ef00dc 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -7,13 +7,16 @@ #include #include #include +#include #include +#include #include #include #include #include "cpumap.h" #include "debug.h" #include "drm_pmu.h" +#include "dwarf-regs.h" #include "env.h" #include "evsel.h" #include "pmus.h" @@ -1004,3 +1007,247 @@ struct perf_pmu *perf_pmus__fake_pmu(void) =20 return &fake; } + +/* + * Determine whether a PMU name reconstructed from perf.data headers is a + * core PMU, checking common core PMU prefixes, hybrid topology nodes, and + * the host architecture's is_pmu_core() when architectures match. + */ +static bool env_is_pmu_core(const char *name, struct perf_env *env) +{ + if (!strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || + !strcmp(name, "default_core") || !strcmp(name, "arc_pct") || + strstarts(name, "cpu_") || strstarts(name, "armv") || + (strstarts(name, "apple_") && strstr(name, "_pmu"))) + return true; + + if (env && env->hybrid_nodes) { + for (int i =3D 0; i < env->nr_hybrid_nodes; i++) { + const char *pmu_name =3D env->hybrid_nodes[i].pmu_name; + + if (pmu_name && !strcmp(name, pmu_name)) + return true; + } + return false; + } + + if (!env || e_machine__same_arch(perf_env__e_machine(env, /*e_flags=3D*/N= ULL), + perf_env__e_machine(host_env(), /*e_flags=3D*/NULL))) + return is_pmu_core(name); + + return false; +} + +/* + * Look up @name in @pmus or allocate and initialize a new perf_env-backed + * struct perf_pmu, upgrading PERF_PMU_TYPE_FAKE if a real type is later + * discovered. + */ +static struct perf_pmu *env_pmu_find_or_add(struct perf_pmus *pmus, + const char *name, __u32 type, + struct perf_env *env, + const struct pmu_events_table *core_table) +{ + struct perf_pmu *pmu =3D pmu_find(pmus, name); + + if (type =3D=3D PERF_PMU_TYPE_FAKE) { + if (!strcmp(name, "software")) + type =3D PERF_TYPE_SOFTWARE; + else if (!strcmp(name, "tracepoint")) + type =3D PERF_TYPE_TRACEPOINT; + else if (!strcmp(name, "breakpoint")) + type =3D PERF_TYPE_BREAKPOINT; + else if (!strcmp(name, "tool")) + type =3D PERF_PMU_TYPE_TOOL; + } + + if (pmu) { + if (pmu->type =3D=3D PERF_PMU_TYPE_FAKE && type !=3D PERF_PMU_TYPE_FAKE)= { + pmu->type =3D type; + if (!pmu->is_core && type =3D=3D PERF_TYPE_RAW) { + pmu->is_core =3D true; + list_move_tail(&pmu->list, &pmus->core_pmus); + } + pmu->is_uncore =3D !pmu->is_core && !pmu->auxtrace && + !perf_pmu__is_software(pmu) && + perf_pmu__kind(pmu) =3D=3D PERF_PMU_KIND_PE; + if (!pmu->is_core && !pmu->is_uncore && + pmu->events_table =3D=3D core_table) { + pmu->events_table =3D NULL; + } else if (!pmu->events_table && (pmu->is_core || pmu->is_uncore) && + pmu_events_table__num_events(core_table, pmu) > 0) { + pmu->events_table =3D core_table; + } + } + return pmu; + } + + pmu =3D zalloc(sizeof(*pmu)); + if (!pmu) + return NULL; + + if (perf_pmu__init(pmu, type, name, /*from_env=3D*/true) !=3D 0) { + perf_pmu__delete(pmu); + return NULL; + } + + pmu->pmus =3D pmus; + pmu->sysfs_aliases_loaded =3D true; + pmu->caps_initialized =3D true; + pmu->max_precise =3D -1; + pmu->is_core =3D type =3D=3D PERF_TYPE_RAW || env_is_pmu_core(name, env); + pmu->auxtrace =3D !strcmp(name, "intel_pt") || + !strcmp(name, "intel_bts") || + !strcmp(name, "cs_etm") || + strstarts(name, "arm_spe") || + strstarts(name, "hisi_ptt"); + pmu->is_uncore =3D !pmu->is_core && !pmu->auxtrace && + !perf_pmu__is_software(pmu) && + (perf_pmu__kind(pmu) =3D=3D PERF_PMU_KIND_PE || + pmu->type =3D=3D PERF_PMU_TYPE_FAKE); + + if (!strcmp(name, "software") || !strcmp(name, "tool")) + pmu->events_table =3D perf_pmu__default_core_events_table(); + else if ((pmu->is_core || pmu->is_uncore) && + pmu_events_table__num_events(core_table, pmu) > 0) + pmu->events_table =3D core_table; + + list_add_tail(&pmu->list, pmu->is_core ? &pmus->core_pmus : &pmus->other_= pmus); + return pmu; +} + +/* Parse "key=3Dvalue" capability strings from perf_env into @pmu->caps. */ +static void env_pmu_add_caps_strings(struct perf_pmu *pmu, int nr_caps, ch= ar **caps) +{ + if (!pmu || nr_caps <=3D 0 || !caps) + return; + + for (int i =3D 0; i < nr_caps; i++) { + const char *eq; + + if (!caps[i]) + continue; + eq =3D strchr(caps[i], '=3D'); + + if (eq) { + char *key =3D strndup(caps[i], eq - caps[i]); + + if (key) { + perf_pmu__add_cap(pmu, key, eq + 1); + free(key); + } + } else { + perf_pmu__add_cap(pmu, caps[i], ""); + } + } +} + +int perf_pmus__read_from_env(struct perf_pmus *pmus, struct perf_env *env) +{ + const struct pmu_events_table *core_table =3D NULL; + struct perf_pmu *pmu; + + if (!pmus || !env) + return -EINVAL; + + perf_pmus__exit(pmus); + pmus->read_pmu_types =3D PERF_TOOL_PMU_TYPE_ALL_MASK; + + if (env->cpuid) + core_table =3D find_core_events_table(perf_env__e_machine(env, /*e_flags= =3D*/NULL), + env->cpuid); + + /* 1. Populate from HEADER_PMU_MAPPINGS */ + if (env->nr_pmu_mappings > 0 && env->pmu_mappings) { + char *str =3D env->pmu_mappings; + + for (int i =3D 0; i < env->nr_pmu_mappings; i++) { + char *colon =3D NULL; + unsigned long type =3D strtoul(str, &colon, 0); + + if (!colon || *colon !=3D ':') + break; + str =3D colon + 1; + env_pmu_find_or_add(pmus, str, (__u32)type, env, core_table); + str +=3D strlen(str) + 1; + } + } + + /* 2. Populate/enrich from HEADER_HYBRID_TOPOLOGY */ + for (int i =3D 0; i < env->nr_hybrid_nodes; i++) { + struct hybrid_node *hn =3D &env->hybrid_nodes[i]; + + if (!hn->pmu_name) + continue; + pmu =3D env_pmu_find_or_add(pmus, hn->pmu_name, PERF_PMU_TYPE_FAKE, + env, core_table); + if (!pmu) + continue; + if (!pmu->is_core) { + pmu->is_core =3D true; + pmu->is_uncore =3D false; + if (!pmu->events_table && + pmu_events_table__num_events(core_table, pmu) > 0) + pmu->events_table =3D core_table; + list_move_tail(&pmu->list, &pmus->core_pmus); + } + if (hn->cpus && !pmu->cpus) + pmu->cpus =3D perf_cpu_map__new(hn->cpus); + } + + /* 3. Populate/enrich from HEADER_PMU_CAPS */ + for (int i =3D 0; i < env->nr_pmus_with_caps; i++) { + struct pmu_caps *pc =3D &env->pmu_caps[i]; + + if (!pc->pmu_name) + continue; + pmu =3D env_pmu_find_or_add(pmus, pc->pmu_name, PERF_PMU_TYPE_FAKE, + env, core_table); + if (pmu) + env_pmu_add_caps_strings(pmu, pc->nr_caps, pc->caps); + } + + /* + * 4. Populate/enrich from HEADER_CPU_PMU_CAPS. + * HEADER_CPU_PMU_CAPS holds the capabilities of the first core PMU, + * named "cpu" on x86 but e.g. "armv8_pmuv3_0" or "cpum_cf" elsewhere. + * On hybrid systems it duplicates the first core PMU's entry in + * HEADER_PMU_CAPS. Attach the capabilities to "cpu" if present, + * otherwise to the first core PMU when it has no capabilities yet, + * creating a "cpu" PMU only if no core PMUs exist. + */ + if (env->nr_cpu_pmu_caps > 0 && env->cpu_pmu_caps) { + pmu =3D pmu_find(pmus, "cpu"); + if (!pmu && !list_empty(&pmus->core_pmus)) { + /* Match the writer's perf_pmus__find_core_pmu() order. */ + list_sort(NULL, &pmus->core_pmus, pmus_cmp); + pmu =3D list_first_entry(&pmus->core_pmus, struct perf_pmu, list); + if (pmu->nr_caps > 0) + pmu =3D NULL; + } else if (!pmu) { + pmu =3D env_pmu_find_or_add(pmus, "cpu", PERF_TYPE_RAW, + env, core_table); + } + if (pmu) + env_pmu_add_caps_strings(pmu, env->nr_cpu_pmu_caps, env->cpu_pmu_caps); + } + + /* Ensure core_pmus is non-empty, matching pmu_read_sysfs()'s placeholder= . */ + if (list_empty(&pmus->core_pmus)) + env_pmu_find_or_add(pmus, "cpu", PERF_TYPE_RAW, env, core_table); + + /* 5. Set default core CPUs on non-hybrid systems */ + if (env->nr_cpus_avail > 0 && env->nr_hybrid_nodes =3D=3D 0) { + char buf[64]; + + snprintf(buf, sizeof(buf), "0-%d", env->nr_cpus_avail - 1); + list_for_each_entry(pmu, &pmus->core_pmus, list) { + if (!pmu->cpus) + pmu->cpus =3D perf_cpu_map__new(buf); + } + } + + list_sort(NULL, &pmus->core_pmus, pmus_cmp); + list_sort(NULL, &pmus->other_pmus, pmus_cmp); + return 0; +} diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index 6e7c9f857aaf..2b784cf648f3 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -8,6 +8,7 @@ #include "mutex.h" =20 struct perf_event_attr; +struct perf_env; struct perf_pmu; struct print_callbacks; =20 @@ -49,7 +50,10 @@ struct perf_pmus { * encoding in perf_event_attr::config is supported. */ enum perf_pmus_extended_type extended_type; - /** @lock: Protects lazy initialization of @extended_type. */ + /** + * @lock: Protects lazy initialization of @extended_type and + * perf_pmus__read_from_env(). + */ struct mutex lock; }; =20 @@ -75,6 +79,15 @@ void perf_pmus__init(struct perf_pmus *pmus); */ void perf_pmus__exit(struct perf_pmus *pmus); =20 +/** + * perf_pmus__read_from_env() - Populate @pmus from perf.data header metad= ata. + * @pmus: The PMU collection to populate. + * @env: The perf_env containing header PMU mappings, hybrid topology, and= caps. + * + * Return: 0 on success, negative error code on failure. + */ +int perf_pmus__read_from_env(struct perf_pmus *pmus, struct perf_env *env); + /** * perf_pmus__find() - Find a PMU by name in @pmus. * @pmus: The PMU collection to search. --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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=1795212953; b=XFM4jDjT+gO0YGF1UDLu2l/KgZFRc2PmPu0+MRUg8cMj3RcJpj+JQPOiZiDM/rIySKOL SkwzndiDX1s9LYD7lItWl0AiE1x90jYphcwC1cJVSEAn7Zv70zb8Z5Qqt49S1hT1mbvvS 8naUtmJzyrW7S2FEhz/0CdOAFQi+yAJcoLBZtzXWOz9eVHgdjwfLbHHMicL+QQQRqLotz 5vUlYJRaJ6oKpAd0uoTl5Ll730NWuNmxlsiQlV/cxEeRGtEmz6ilGoPx9EM79CCkWq370 lI2A+W03MSPO4zQDkDF+a9aHl2tbL6/Aiw5Eo988nmKMtPzaVtMSOdpLCIB5NvuTdfg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212953; 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=5oSAS5rTMZpKmndtpKW47xjlBULmJlmzTFbWhRT19C4=; b=sV6TG+SpLhS+K+0v55lGfM+jczfgTmNcBC0DNjjwYsi5QrCjT4FxjfcEvuRpWUZ517XM +Hg9zctItrBki6IyxUqfEzc0PKjYB1hYVX1CHyqRI8NohHeSvVzhgmurZ82WoaTHdQsB8 vMQui67oyfjrXi1s3aUfgjYQKjVnyotgX9TCHgmaDs3Tg71qJFFEbVOq66YH9CTWX4fKd z8+LPe29lHWAubjfT9t4qmkS9fKxu79rHZmEkHAkVJ6B71JSN6qL//DIQ0DbAIz/ZgOyU 6u8uUL+Lb7PDZcmmfc9OrbxlBTaVsZgUXpt9BOnOvniOixU8+QKvRaUA+cOe0T9EswA== 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=HzH+Gntp; 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 79840606A6BE for ; Fri, 20 Nov 2026 22:15:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 79840606A6BE 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=HzH+Gntp Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-139b62317d0so3612598c88.0 for ; Fri, 20 Nov 2026 14:15:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212949; x=1795817749; 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=5oSAS5rTMZpKmndtpKW47xjlBULmJlmzTFbWhRT19C4=; b=HzH+GntpY/4eLrBPc6AV+MPbeOC5TAlF9UF7Eh1809RoUtm+6w9YJEg+1VagWLKeGy T57ZEdn81BEE02PPrQBDm5sDGMGocKZvSTLJl4+81SYA+RGevcVPMag4A2906SrqfqeB lZpflk3/yZipj+7v2F6uhSnxD0RVwCmHvNPAs0zhI4hooYgptJvCFOeG00Z98lJsjrQz LukGrsYkB0oLzkkkpiLRPOATRemSIfSN05PeTBYLldI0aK0lzcOkXgsAZ2JDmOwLbLDP sDQDDnwNE1AIXAP5VRlQgshdFCFgMYGa617rLzwcVIIffikSMsNCWkMSF8DvkyZ9yzLf mOYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212949; x=1795817749; 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=5oSAS5rTMZpKmndtpKW47xjlBULmJlmzTFbWhRT19C4=; b=gLlsouClOIfedmTA5uJVCEZ59n/oCpsz8T8+eLoLAe3/5n9Yirhu1cpU6tFU28U/ez jG3zayPnjEvDUh5lrkEG8NktyF+et6Z1qLusojpkFMH2C5Cc8vVKK4zAfc09ipnxnv8Y tQ2MxaXg7I7ayPCLovU1PsfxYlqk2VJlm8GC90KUSRHhxAV03memCqQaDIBF7XsubDqH w7/6MFN1OaP13bozw8zEFIMw42GjKFzbaf/6A2VZ7CsZoWyy69B7y16v2SO1HElcjclf 52i+2l2w/ENgTIqx2Wlyg5HH5r3r2tlEwm+xjDBRh9LlJ/HON9tJko9+V7bX0b1KgwlP eADg== X-Forwarded-Encrypted: i=1; AL+hErEZCBbVJ5K9sY2iaYCg1Z2uF1pMAKoxO939syWVQG2oPGDj3WAwoBY7mz5jF2BOXpy988tRPEjO5691nRs=@vger.kernel.org X-Gm-Message-State: APuU4Z8TmJkbzJTEQb7n7Z9dBRVsYK+ZZ3XUgK8VzEchl770xTnH8Kdt z5mmC+r3hbZVq7zfoR4a06T8ltQEPTTpQsjZM+5MGQAfrkeqgyGnA0ctUoChftDgz+2ZQ1iJhWe Qf8HBZxIzGw== X-Received: from dlgx9-n2.prod.google.com ([2002:a05:701a:ccc9:20b0:1b2:6c20:7e86]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:b044:10b0:19b:39f4:88c9 with SMTP id a92af1059eb24-1b5568a12e7mr4611387c88.10.1795212948246; Fri, 20 Nov 2026 14:15:48 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:27 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-27-irogers@google.com> Subject: [PATCH v3 26/30] perf metricgroup: Improve matching of metrics to recorded events 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Matching metrics to the events of a perf.data file compared only the perf_event_attr type and config of events, which failed for common cases: - Legacy hardware events, such as "cycles" with "perf record -e cycles", didn't match the sysfs event, such as "cpu_core/cpu-cycles/", that metrics are parsed to. Use the evsel matcher, as used for metrics sharing events, which compares the PMU name and counter encoding, converting legacy hardware events of core PMUs to the equivalent sysfs event's encoding. Add an EVSEL_MATCH_RECORDED mode to the matcher that tolerates the exclusion bits evsel__fallback() may have changed when recording, requires equal CPUs when either event explicitly restricts CPUs (e.g. cpu=3Dcpu_core), skips unsupported events and prefers events with counts. - A recorded event could be used by only one metric leader, as metrics set the event's metric_leader and later metrics with a different leader were skipped. Instead attach a copy of each metric expression to the recorded event of each PMU the metric applies to, adding the metric's event identifiers to the matched recorded evsels. - When the same counter was recorded more than once, such as for the default metrics of perf stat in different groups, the first was matched. Prefer a supported event with the same name, exclusion bits, counts, CPUs and, when the metric's events are grouped, the same group membership, as the matcher does. - On hybrid systems a metric was skipped unless the events of every core PMU were recorded. Add the metric for each PMU whose events were all recorded. Also avoid adding duplicate metrics, avoid counting a recorded event twice when different metric events match it, and avoid event grouping warnings when parsing the metrics. Metrics of a single event are kept as they may usefully scale the event, for example from cache lines to bytes. For example, on an Alder Lake system with legacy cycles and instructions events recorded: $ perf record -e '{cycles,instructions}:S' -a sleep 1 $ perf script -vv -F comm,metric 2>&1 | grep "Found metric" | \ sed 's/ whose.*//' | sort | uniq Found metric 'insn_per_cycle' Found metric 'tma_info_core_cpi' Found metric 'tma_info_core_ipc' Found metric 'tma_info_inst_mix_instructions' Found metric 'tma_info_system_mux' Found metric 'tma_info_thread_clks' Found metric 'tma_info_thread_cpi' Found metric 'tma_info_thread_ipc' where the json metrics use sysfs and json encodings of the events, such as cpu_core/INST_RETIRED.ANY/, that now match the recorded legacy events. Signed-off-by: Ian Rogers # Conflicts: # tools/perf/util/metricgroup.c --- tools/perf/util/evsel.c | 1 + tools/perf/util/evsel.h | 1 + tools/perf/util/metricgroup.c | 666 ++++++++++++++++++++++++++------- tools/perf/util/metricgroup.h | 5 +- tools/perf/util/parse-events.c | 1 + 5 files changed, 543 insertions(+), 131 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 73687e24f5bc..46443e9019d8 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -619,6 +619,7 @@ struct evsel *evsel__clone(struct evsel *orig) evsel->use_config_name =3D orig->use_config_name; evsel->skippable =3D orig->skippable; evsel->dont_regroup =3D orig->dont_regroup; + evsel->has_user_cpus =3D orig->has_user_cpus; evsel->default_metricgroup =3D orig->default_metricgroup; evsel->default_show_events =3D orig->default_show_events; =20 diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index e776ca60a9a5..2e18630acaea 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -99,6 +99,7 @@ struct evsel { bool skippable; bool retire_lat; bool dont_regroup; + bool has_user_cpus; bool default_metricgroup; /* A member of the Default metricgroup */ bool default_show_events; /* If a default group member, show the event= */ struct list_head config_terms; diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 5641a3676063..72b552829659 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -8,15 +8,20 @@ #include "metricgroup.h" #include "debug.h" #include "evlist.h" +#include "env.h" #include "evsel.h" +#include "cpumap.h" +#include "counts.h" #include "strbuf.h" #include "pmu.h" #include "pmus.h" +#include "parse-events.h" #include "print-events.h" #include "smt.h" #include "tool_pmu.h" #include "expr.h" #include "rblist.h" +#include #include #include #include "strlist.h" @@ -88,18 +93,24 @@ static struct rb_node *metric_event_new(struct rblist *= rblist __maybe_unused, return &me->nd; } =20 +static void metric_expr__delete(struct metric_expr *expr) +{ + if (!expr) + return; + zfree(&expr->metric_name); + zfree(&expr->metric_refs); + zfree(&expr->metric_events); + free(expr); +} + static void metric_event_delete(struct rblist *rblist __maybe_unused, struct rb_node *rb_node) { struct metric_event *me =3D container_of(rb_node, struct metric_event, nd= ); struct metric_expr *expr, *tmp; =20 - list_for_each_entry_safe(expr, tmp, &me->head, nd) { - zfree(&expr->metric_name); - zfree(&expr->metric_refs); - zfree(&expr->metric_events); - free(expr); - } + list_for_each_entry_safe(expr, tmp, &me->head, nd) + metric_expr__delete(expr); =20 free(me); } @@ -482,7 +493,8 @@ int metricgroup__for_each_metric(struct perf_pmus *pmus= , const struct pmu_metric =20 /* * A key identifying the counter an evsel programs, used to find evsels co= unting - * the same thing, such as when metrics share events. + * the same thing, such as when metrics share events or when matching evse= ls + * parsed from a metric with those read from a perf.data file. */ struct counter_key { /** @pmu_name: Name of the evsel's PMU, or NULL if unknown. */ @@ -491,7 +503,7 @@ struct counter_key { const struct perf_cpu_map *cpus; /** @owned_cpus: Reference-counted CPU map owned by this key, or NULL. */ struct perf_cpu_map *owned_cpus; - /** @cgrp: The cgroup the evsel counts. */ + /** @cgrp: The cgroup the evsel counts, compared by EVSEL_MATCH_LIVE. */ const struct cgroup *cgrp; /** @type: perf_event_attr type, PERF_TYPE_RAW for any dynamic PMU type. = */ __u32 type; @@ -512,8 +524,10 @@ struct counter_key { * is counted, a mask of COUNTER_KEY_MOD_*. */ unsigned int modifiers; - /** @precise_ip: perf_event_attr precise_ip. */ + /** @precise_ip: perf_event_attr precise_ip, compared by EVSEL_MATCH_LIVE= . */ unsigned int precise_ip; + /** @has_user_cpus: Were @cpus restricted by an explicit cpu=3D term? */ + bool has_user_cpus; }; =20 #define COUNTER_KEY_EXCLUDE_USER BIT(0) @@ -522,6 +536,8 @@ struct counter_key { #define COUNTER_KEY_EXCLUDE_HOST BIT(3) #define COUNTER_KEY_EXCLUDE_GUEST BIT(4) #define COUNTER_KEY_EXCLUDE_IDLE BIT(5) +#define COUNTER_KEY_EXCLUDE_PRIV (COUNTER_KEY_EXCLUDE_USER | COUNTER_KEY_E= XCLUDE_KERNEL | \ + COUNTER_KEY_EXCLUDE_HV) =20 #define COUNTER_KEY_MOD_PINNED BIT(0) #define COUNTER_KEY_MOD_EXCLUSIVE BIT(1) @@ -538,6 +554,21 @@ struct legacy_hw_key { struct counter_key key; }; =20 +/** enum evsel_match_mode - How strictly evsels must agree to be matched. = */ +enum evsel_match_mode { + /** + * @EVSEL_MATCH_LIVE: Evsels parsed by this perf command, such as for + * metrics sharing events, must agree on all modifiers. + */ + EVSEL_MATCH_LIVE, + /** + * @EVSEL_MATCH_RECORDED: Evsels read from a perf.data file may have had + * modifiers changed by evsel__fallback() and the counts of the evsels + * are preferred. + */ + EVSEL_MATCH_RECORDED, +}; + struct evsel_matcher_entry { struct evsel *evsel; struct counter_key key; @@ -558,6 +589,13 @@ struct evsel_matcher { size_t max_entries; /** @legacy_hw_keys: Cache of legacy_hw_key. */ struct list_head legacy_hw_keys; + /** @mode: How strictly evsels must agree to be matched. */ + enum evsel_match_mode mode; + /** + * @match_grouping: Prefer evsels whose group membership is the same + * as the evsel being matched. + */ + bool match_grouping; }; =20 static void counter_key__exit(struct counter_key *key) @@ -772,19 +810,77 @@ static struct perf_cpu_map *evsel_propagated_cpus(con= st struct perf_evlist *evli return cpus; } =20 +/* + * Recorded evsels read from perf.data do not set evsel->has_user_cpus, an= d for + * PMUs without a sysfs cpumask (such as "msr") PERF_EVENT_UPDATE__CPUS re= cords + * all online CPUs rather than an event's cpu=3D term. Recover any cpu=3D = terms + * from the recorded event's name so per-PMU events like "msr/tsc,cpu=3Dcp= u_core/" + * and "msr/tsc,cpu=3Dcpu_atom/" match only their corresponding CPU subset= s. + */ +static struct perf_cpu_map *recorded_evsel_user_cpus(struct evsel *evsel) +{ + const char *name =3D evsel->name; + const char *slash =3D name ? strchr(name, '/') : NULL; + const char *end, *tok; + struct perf_pmus *pmus; + struct perf_cpu_map *cpus =3D NULL; + + if (!slash) + return NULL; + + end =3D strchr(slash + 1, '/'); + if (!end) + end =3D slash + strlen(slash); + + pmus =3D perf_env__pmus(evsel__env(evsel) ?: host_env()); + for (tok =3D slash + 1; tok < end; ) { + const char *comma =3D memchr(tok, ',', end - tok); + const char *tok_end =3D comma ?: end; + + if (tok_end - tok > 4 && strstarts(tok, "cpu=3D")) { + char *val =3D strndup(tok + 4, tok_end - (tok + 4)); + struct perf_cpu_map *term_cpus =3D NULL; + struct perf_pmu *pmu; + + if (!val) { + perf_cpu_map__put(cpus); + return NULL; + } + pmu =3D perf_pmus__find(pmus, val); + if (!pmu || perf_cpu_map__is_empty(pmu->cpus)) + pmu =3D perf_pmus__find(perf_env__pmus(host_env()), val); + if (pmu) { + term_cpus =3D pmu->is_core && perf_cpu_map__is_empty(pmu->cpus) + ? cpu_map__online() + : perf_cpu_map__get(pmu->cpus); + } else { + term_cpus =3D perf_cpu_map__new(val); + } + free(val); + perf_cpu_map__merge(&cpus, term_cpus); + perf_cpu_map__put(term_cpus); + } + tok =3D comma ? comma + 1 : end; + } + return cpus; +} + static void evsel_matcher__key(struct evsel_matcher *matcher, struct evsel= *evsel, struct counter_key *key) { struct perf_pmu *pmu =3D evsel__find_pmu(evsel); =20 counter_key__init_attr(key, pmu ? pmu->name : NULL, &evsel->core.attr); - if (matcher->evlist && evlist__core(matcher->evlist)->needs_map_propagati= on && + if (matcher->mode =3D=3D EVSEL_MATCH_LIVE && matcher->evlist && + evlist__core(matcher->evlist)->needs_map_propagation && evsel->evlist !=3D matcher->evlist) { key->owned_cpus =3D evsel_propagated_cpus(evlist__core(matcher->evlist),= evsel); key->cpus =3D key->owned_cpus; } else { - key->cpus =3D evsel->core.cpus; + key->cpus =3D !perf_cpu_map__is_empty(evsel->core.cpus) + ? evsel->core.cpus : evsel->core.pmu_cpus; } + key->has_user_cpus =3D evsel->has_user_cpus; key->cgrp =3D evsel->cgrp; if (evsel->percore) key->modifiers |=3D COUNTER_KEY_MOD_PERCORE; @@ -798,9 +894,12 @@ static void evsel_matcher__key(struct evsel_matcher *m= atcher, struct evsel *evse legacy_hw_key(matcher, pmu->name, key->config, key); } =20 -/* Do @a and @b count the same thing? */ -static bool counter_key__equal(const struct counter_key *a, const struct c= ounter_key *b) +/* Do @a, from the evsel being matched, and @b count the same thing? */ +static bool counter_key__equal(const struct counter_key *a, const struct c= ounter_key *b, + enum evsel_match_mode mode) { + unsigned int a_priv, b_priv; + if (a->type !=3D b->type || a->config !=3D b->config || a->config1 !=3D b->config1 || a->config2 !=3D b->config2 || a->config3 !=3D b->config3 || a->config4 !=3D b->config4) @@ -813,9 +912,67 @@ static bool counter_key__equal(const struct counter_ke= y *a, const struct counter if (a->pmu_name && b->pmu_name && strcmp(a->pmu_name, b->pmu_name)) return false; =20 - return a->exclude =3D=3D b->exclude && a->modifiers =3D=3D b->modifiers && - a->precise_ip =3D=3D b->precise_ip && a->cgrp =3D=3D b->cgrp && - perf_cpu_map__equal(a->cpus, b->cpus); + if ((a->modifiers ^ b->modifiers) & COUNTER_KEY_MOD_RETIRE_LAT) + return false; + + if (mode =3D=3D EVSEL_MATCH_LIVE) { + return a->exclude =3D=3D b->exclude && a->modifiers =3D=3D b->modifiers = && + a->precise_ip =3D=3D b->precise_ip && a->cgrp =3D=3D b->cgrp && + perf_cpu_map__equal(a->cpus, b->cpus); + } + + if ((a->has_user_cpus || b->has_user_cpus) && + !perf_cpu_map__equal(a->cpus, b->cpus)) + return false; + + /* + * If the metric event specified privilege exclusion bits (e.g. :k or + * :u), require the recorded event to match. Otherwise allow matching a + * recorded event whose exclude_kernel/exclude_hv bits were set by + * evsel__fallback() when perf_event_paranoid > 1. + */ + a_priv =3D a->exclude & COUNTER_KEY_EXCLUDE_PRIV; + b_priv =3D b->exclude & COUNTER_KEY_EXCLUDE_PRIV; + if (a_priv !=3D b_priv && (a_priv !=3D 0 || (b_priv & COUNTER_KEY_EXCLUDE= _USER) !=3D 0)) + return false; + + /* + * Guest only events (:G) and host only events (:H) must match, except a + * recorded event may exclude the guest when the metric event doesn't if + * exclude_guest was set by evsel__fallback() on EOPNOTSUPP, such as on + * Apple M1. + */ + if ((a->exclude ^ b->exclude) & COUNTER_KEY_EXCLUDE_HOST) + return false; + if ((a->exclude & COUNTER_KEY_EXCLUDE_GUEST) && !(b->exclude & COUNTER_KE= Y_EXCLUDE_GUEST)) + return false; + if ((a->exclude ^ b->exclude) & COUNTER_KEY_EXCLUDE_IDLE) + return false; + + return true; +} + +/* Were any counts read for @evsel, for example, from a perf stat record r= ound? */ +static int evsel__count_score(struct evsel *evsel) +{ + int score =3D 0; + + if (!evsel->counts) + return 0; + + for (int cpu_idx =3D 0; cpu_idx < xyarray__max_x(evsel->counts->values); = cpu_idx++) { + for (int thread =3D 0; thread < xyarray__max_y(evsel->counts->values); t= hread++) { + struct perf_counts_values *cnt =3D + perf_counts(evsel->counts, cpu_idx, thread); + + if (cnt->run) { + if (cnt->val) + return 2; + score =3D 1; + } + } + } + return score; } =20 static bool evsel__is_grouped(const struct evsel *evsel) @@ -846,6 +1003,16 @@ static int evsel_matcher__add(struct evsel_matcher *m= atcher, struct evsel *evsel entry =3D &matcher->entries[matcher->nr_entries++]; entry->evsel =3D evsel; evsel_matcher__key(matcher, evsel, &entry->key); + if (matcher->mode =3D=3D EVSEL_MATCH_RECORDED && !entry->key.has_user_cpu= s) { + struct perf_cpu_map *user_cpus =3D recorded_evsel_user_cpus(evsel); + + if (user_cpus) { + counter_key__exit(&entry->key); + entry->key.owned_cpus =3D user_cpus; + entry->key.cpus =3D user_cpus; + entry->key.has_user_cpus =3D true; + } + } return 0; } =20 @@ -854,14 +1021,20 @@ static int evsel_matcher__add(struct evsel_matcher *= matcher, struct evsel *evsel * @matcher: The matcher to initialize. * @evlist: The evsels that may be matched, more may be added with * evsel_matcher__add(). + * @mode: How strictly evsels must agree to be matched. + * @match_grouping: Prefer evsels whose group membership is the same as the + * evsel being matched. */ -static int evsel_matcher__init(struct evsel_matcher *matcher, struct evlis= t *evlist) +static int evsel_matcher__init(struct evsel_matcher *matcher, struct evlis= t *evlist, + enum evsel_match_mode mode, bool match_grouping) { struct evsel *evsel; =20 memset(matcher, 0, sizeof(*matcher)); matcher->evlist =3D evlist; INIT_LIST_HEAD(&matcher->legacy_hw_keys); + matcher->mode =3D mode; + matcher->match_grouping =3D match_grouping; evlist__for_each_entry(evlist, evsel) { int ret =3D evsel_matcher__add(matcher, evsel); =20 @@ -962,9 +1135,10 @@ enum evsel_group_match { * not its own metric leader. * @group_match: Whether grouped events may be matched when @leader is NUL= L. * - * The same counter may be present more than once, such as when the counte= r is - * in different groups. Prefer an evsel with the same name and the same gr= oup - * membership. + * The same counter may be present more than once, such as when different + * metrics were measured in different groups. Prefer an evsel with the same + * name, as a metric would have been parsed to, the same exclusion bits, c= ounts + * and, if requested, the same group membership. */ static struct evsel *evsel_matcher__find_for_metric(struct evsel_matcher *= matcher, struct evsel *evsel, @@ -989,11 +1163,12 @@ static struct evsel *evsel_matcher__find_for_metric(= struct evsel_matcher *matche continue; =20 /* - * Don't match an ungrouped event to a member of a group, as the - * group may multiplex differently from the metric's other - * events. + * When measuring live events, don't match an ungrouped event to + * a member of a group, as the group may multiplex differently + * from the metric's other events. */ - if (!leader && group_match =3D=3D EVSEL_GROUP_MATCH_UNGROUPED && + if (matcher->mode =3D=3D EVSEL_MATCH_LIVE && !leader && + group_match =3D=3D EVSEL_GROUP_MATCH_UNGROUPED && evsel__is_grouped(pos)) continue; =20 @@ -1001,8 +1176,11 @@ static struct evsel *evsel_matcher__find_for_metric(= struct evsel_matcher *matche (!evsel__is_group_leader(pos) || !evsel__is_forced_group(pos))) continue; =20 + if (matcher->mode =3D=3D EVSEL_MATCH_RECORDED && !pos->supported) + continue; + /* Skip if the evsels don't program the same counter. */ - if (!counter_key__equal(&key, &entry->key)) + if (!counter_key__equal(&key, &entry->key, matcher->mode)) continue; =20 if (!metric_leader_compatible(evsel, metric_sources, mapped_metric_leade= r, pos)) @@ -1013,8 +1191,11 @@ static struct evsel *evsel_matcher__find_for_metric(= struct evsel_matcher *matche !evsel__shares_metric_id(evsel, pos)) continue; =20 - score =3D (!strcmp(evsel__name(evsel), evsel__name(pos)) ? 2 : 0) + - (grouped =3D=3D evsel__is_grouped(pos) ? 1 : 0); + score =3D (!strcmp(evsel__name(evsel), evsel__name(pos)) ? 32 : 0) + + (key.exclude =3D=3D entry->key.exclude ? 16 : 0) + + evsel__count_score(pos) * 4 + + (perf_cpu_map__equal(key.cpus, entry->key.cpus) ? 2 : 0) + + (matcher->match_grouping && grouped =3D=3D evsel__is_grouped(pos) ? 1 := 0); if (score > best_score) { best =3D pos; best_score =3D score; @@ -1024,9 +1205,8 @@ static struct evsel *evsel_matcher__find_for_metric(s= truct evsel_matcher *matche return best; } =20 -static __maybe_unused struct evsel *evsel_matcher__find(struct evsel_match= er *matcher, - struct evsel *evsel, - const struct evsel *leader) +static struct evsel *evsel_matcher__find(struct evsel_matcher *matcher, st= ruct evsel *evsel, + const struct evsel *leader) { return evsel_matcher__find_for_metric(matcher, evsel, leader, /*metric_sources=3D*/0, @@ -1082,150 +1262,375 @@ static bool evsel_matcher__find_group(struct evse= l_matcher *matcher, struct evse } =20 struct find_matching_metrics_args { + /** @evlist: The evlist of the perf.data file. */ struct evlist *evlist; + /** @matcher: Matcher of evlist's evsels. */ + struct evsel_matcher matcher; bool system_wide; }; =20 -static struct evsel *map_metric_evsel_to_evlist(struct evlist *data_evlist, - struct evsel *metric_evsel) +/* + * Find the evsel in @metric_evlist counting @ev, or an event merged with = it as + * its metric leader, on @pmu. + */ +static struct evsel *metric_evsel_on_pmu(struct evlist *metric_evlist, str= uct evsel *ev, + const struct perf_pmu *pmu) { - struct evsel *data_evsel; + struct evsel *pos; =20 - evlist__for_each_entry(data_evlist, data_evsel) { - /* Skip if perf_event_attr differ. */ - if (metric_evsel->core.attr.type !=3D data_evsel->core.attr.type) - continue; - if (metric_evsel->core.attr.config !=3D data_evsel->core.attr.config) - continue; - return data_evsel; + if (ev->pmu =3D=3D pmu) + return ev; + + evlist__for_each_entry(metric_evlist, pos) { + if (pos->pmu =3D=3D pmu && + (pos->metric_leader =3D=3D ev || + evsel__shares_metric_id(pos, ev))) + return pos; } - return NULL; + if (ev->pmu && ev->pmu->is_core && pmu && pmu->is_core) + return NULL; + return ev; } =20 -static int find_matching_metrics_cb(const struct pmu_metric *pm, - const struct pmu_metrics_table *table __maybe_unused, - void *data) +/* + * Copy @expr, whose events are in @metric_evlist, replacing each event wi= th the + * corresponding event on @pmu and then with the matching evsel of the data + * evlist. The metric identifiers of the events are added to the matched d= ata + * evsel as different metrics may refer to the same recorded event by diff= erent + * names. + */ +static struct metric_expr *metric_expr__dup_for_data(struct find_matching_= metrics_args *args, + struct evlist *metric_evlist, + const struct metric_expr *expr, + const struct perf_pmu *pmu, + struct evsel *target_data_evsel) { - struct find_matching_metrics_args *args =3D data; - struct evlist *data_evlist =3D args->evlist; - struct evlist *metric_evlist =3D evlist__new(); - struct evsel *metric_evsel; - int ret =3D metricgroup__parse_groups(metric_evlist, - /*pmu=3D*/"all", - /*cputype_filter=3D*/false, - pm->metric_name, - /*metric_no_group=3D*/false, - /*metric_no_merge=3D*/false, - /*metric_no_threshold=3D*/true, - /*user_requested_cpu_list=3D*/NULL, - args->system_wide, - /*hardware_aware_grouping=3D*/false); + struct evsel *data_group =3D evsel__leader(target_data_evsel); + struct metric_expr *copy =3D zalloc(sizeof(*copy)); + int nr =3D 0; =20 - if (ret) { - /* Metric parsing failed but continue the search. */ - goto out; + if (data_group->core.nr_members <=3D 1) + data_group =3D NULL; + + if (!copy) + return NULL; + + copy->metric_expr =3D expr->metric_expr; + copy->metric_threshold =3D expr->metric_threshold; + copy->metric_unit =3D expr->metric_unit; + copy->default_metricgroup_name =3D expr->default_metricgroup_name; + copy->runtime =3D expr->runtime; + copy->metric_name =3D strdup(expr->metric_name); + if (!copy->metric_name) + goto err; + + if (expr->metric_refs) { + int nr_refs =3D 0; + + while (expr->metric_refs[nr_refs].metric_name) + nr_refs++; + copy->metric_refs =3D calloc(nr_refs + 1, sizeof(*copy->metric_refs)); + if (!copy->metric_refs) + goto err; + memcpy(copy->metric_refs, expr->metric_refs, nr_refs * sizeof(*copy->met= ric_refs)); } =20 + while (expr->metric_events[nr]) + nr++; + copy->metric_events =3D calloc(nr + 1, sizeof(*copy->metric_events)); + if (!copy->metric_events) + goto err; + /* - * Check the data_evlist has an entry for each metric_evlist entry. If - * the data evsel was already set up avoid changing data that may - * break it. + * Map every event before modifying any data evsel so that a failure + * doesn't leave metric ids or leaders on data evsels of an unused copy. */ - evlist__for_each_entry(metric_evlist, metric_evsel) { - struct evsel *data_evsel =3D - map_metric_evsel_to_evlist(data_evlist, metric_evsel); - struct evsel *new_metric_leader; + for (int i =3D 0; i < nr; i++) { + struct evsel *ev =3D metric_evsel_on_pmu(metric_evlist, expr->metric_eve= nts[i], pmu); + + if (!ev) + goto err; + if (data_group && evsel__is_grouped(ev)) + copy->metric_events[i] =3D evsel_matcher__find(&args->matcher, ev, + data_group); + if (!copy->metric_events[i]) + copy->metric_events[i] =3D evsel_matcher__find(&args->matcher, ev, + /*leader=3D*/NULL); + if (!copy->metric_events[i]) + goto err; + } + for (int i =3D 0, j =3D 0; i < nr; i++) { + struct evsel *data_evsel =3D copy->metric_events[i]; + bool dup =3D false; =20 - if (!data_evsel) { - pr_debug("Skipping metric '%s' as evsel '%s' is missing\n", - pm->metric_name, evsel__name(metric_evsel)); - goto out; + if (evsel__copy_metric_ids(data_evsel, expr->metric_events[i])) + goto err; + /* + * Different metric events may map to the same data evsel, whose + * metric ids now include those of both. Only keep the first so + * that the data evsel's value isn't counted more than once. + */ + for (int k =3D 0; k < j; k++) { + if (copy->metric_events[k] =3D=3D data_evsel) { + dup =3D true; + break; + } } - - if (data_evsel->metric_leader =3D=3D NULL) + copy->metric_events[i] =3D NULL; + if (dup) continue; + copy->metric_events[j++] =3D data_evsel; + /* + * As in setup_metric_events(), a matched event without a metric + * leader leads itself so that evsel__source_count() counts it. + */ + if (!data_evsel->metric_leader) + data_evsel->metric_leader =3D data_evsel; + } + return copy; +err: + metric_expr__delete(copy); + return NULL; +} =20 - if (metric_evsel->metric_leader =3D=3D metric_evsel) { - new_metric_leader =3D data_evsel; - } else { - new_metric_leader =3D - map_metric_evsel_to_evlist(data_evlist, - metric_evsel->metric_leader); +static bool metric_evsel__has_pmu_siblings(struct evlist *metric_evlist, + const struct evsel *ev) +{ + struct evsel *pos; + + evlist__for_each_entry(metric_evlist, pos) { + if (pos !=3D ev && pos->pmu !=3D ev->pmu && + pos->metric_leader =3D=3D ev && + evsel__shares_metric_id(pos, ev)) + return true; + } + return false; +} + +/* + * Pick the event in @metric_evlist whose per-PMU instances @expr should be + * duplicated and displayed for. pick_display_evsel() may select a single-= PMU + * event, such as software/cpu-clock/ in branch_frequency, when another ev= ent in + * @expr was expanded across multiple PMUs (e.g. hybrid core PMUs); prefer= the + * multi-PMU event so @expr is matched for each PMU. + */ +static struct evsel *metric_expr__leader_evsel(struct evlist *metric_evlis= t, + struct evsel *metric_evsel, + const struct metric_expr *expr) +{ + if (metric_evsel__has_pmu_siblings(metric_evlist, metric_evsel)) + return metric_evsel; + + for (int i =3D 0; expr->metric_events[i]; i++) { + struct evsel *ev =3D expr->metric_events[i]; + + if (metric_evsel__has_pmu_siblings(metric_evlist, ev)) + return ev; + } + return metric_evsel; +} + +/* Does @metric_events already contain a metric equivalent to @expr? */ +static bool metric_events__contains(struct rblist *metric_events, const st= ruct metric_expr *expr) +{ + for (unsigned int i =3D 0; i < rblist__nr_entries(metric_events); i++) { + struct rb_node *nd =3D rblist__entry(metric_events, i); + struct metric_event *me =3D container_of(nd, struct metric_event, nd); + const struct metric_expr *pos; + + list_for_each_entry(pos, &me->head, nd) { + int j; + + if (strcmp(pos->metric_name, expr->metric_name)) + continue; + for (j =3D 0; pos->metric_events[j] && expr->metric_events[j]; j++) { + if (pos->metric_events[j] !=3D expr->metric_events[j]) + break; + } + if (!pos->metric_events[j] && !expr->metric_events[j]) + return true; } - /* Mismatching evsel leaders. */ - if (data_evsel->metric_leader !=3D new_metric_leader) { - pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders = '%s' vs '%s'\n", - pm->metric_name, evsel__name(metric_evsel), - evsel__name(data_evsel)); - goto out; + } + return false; +} + +static int add_matching_metric_evlist(struct find_matching_metrics_args *a= rgs, + struct evlist *metric_evlist, + bool default_metricgroup) +{ + struct evlist *data_evlist =3D args->evlist; + struct evsel *metric_evsel; + + if (default_metricgroup) { + evlist__for_each_entry(metric_evlist, metric_evsel) { + if (metric_evsel->default_show_events) { + struct evsel *data_evsel =3D + evsel_matcher__find(&args->matcher, metric_evsel, + /*leader=3D*/NULL); + + if (data_evsel) + data_evsel->default_show_events =3D true; + } } } + /* - * Metric events match those in the data evlist, copy metric evsel - * data into the data evlist. + * Metric expressions are associated with a metric leader event and + * computed for each PMU of the events led by it. Rather than share + * metric leaders between metrics, which may conflict, attach a copy of + * the expression to each such data event using the data events of its + * PMU. */ evlist__for_each_entry(metric_evlist, metric_evsel) { - struct evsel *data_evsel =3D - map_metric_evsel_to_evlist(data_evlist, metric_evsel); struct metric_event *metric_me =3D metricgroup__lookup(evlist__metric_events(metric_evlist), metric_evsel, /*create=3D*/false); + struct metric_expr *expr; =20 - if (evsel__copy_metric_ids(data_evsel, metric_evsel)) - goto out; + if (!metric_me || metric_me->evsel !=3D metric_evsel) + continue; + + list_for_each_entry(expr, &metric_me->head, nd) { + struct evsel *leader =3D + metric_expr__leader_evsel(metric_evlist, metric_evsel, expr); + struct evsel *pos; + + evlist__for_each_entry(metric_evlist, pos) { + struct evsel *data_evsel; + struct metric_event *data_me; + struct metric_expr *copy; =20 - if (data_evsel->metric_leader =3D=3D NULL) { - if (metric_evsel->metric_leader =3D=3D metric_evsel) { + if (pos !=3D leader && + (pos->metric_leader !=3D leader || + !evsel__shares_metric_id(pos, leader))) + continue; + + data_evsel =3D evsel_matcher__find(&args->matcher, pos, + /*leader=3D*/NULL); + if (!data_evsel) + continue; + + copy =3D metric_expr__dup_for_data(args, metric_evlist, expr, + pos->pmu, data_evsel); + if (!copy) + continue; + if (metric_events__contains(evlist__metric_events(data_evlist), + copy)) { + metric_expr__delete(copy); + continue; + } + data_me =3D metricgroup__lookup(evlist__metric_events(data_evlist), + data_evsel, /*create=3D*/true); + if (!data_me) { + metric_expr__delete(copy); + return -ENOMEM; + } data_evsel->metric_leader =3D data_evsel; - } else { - data_evsel->metric_leader =3D - map_metric_evsel_to_evlist(data_evlist, - metric_evsel->metric_leader); + if (default_metricgroup && metric_me->is_default) + data_me->is_default =3D true; + list_add_tail(©->nd, &data_me->head); + pr_debug("Found metric '%s' whose evsels match those in the perf data\= n", + copy->metric_name); } } + } + return 0; +} =20 - if (metric_me) { - struct metric_expr *expr; - struct metric_event *data_me =3D - metricgroup__lookup(evlist__metric_events(data_evlist), - data_evsel, - /*create=3D*/true); - - if (!data_me) { - /* - * As the metric_expr is created, the only - * failure is a lack of memory. - */ - goto out; - } - list_splice_init(&metric_me->head, &data_me->head); - list_for_each_entry(expr, &data_me->head, nd) { - for (int i =3D 0; expr->metric_events[i]; i++) { - expr->metric_events[i] =3D - map_metric_evsel_to_evlist(data_evlist, - expr->metric_events[i]); - } - } +static int find_matching_metrics_cb(const struct pmu_metric *pm, + const struct pmu_metrics_table *table __maybe_unused, + void *data) +{ + struct find_matching_metrics_args *args =3D data; + struct evlist *metric_evlist =3D evlist__new(); + struct evsel *metric_evsel; + bool found; + int ret; + + if (!metric_evlist) + return -ENOMEM; + + evlist__set_session(metric_evlist, evlist__session(args->evlist)); + /* + * Event grouping doesn't affect the matching and avoids warnings about + * grouping constraints. + */ + ret =3D metricgroup__parse_groups(metric_evlist, + /*pmu=3D*/"all", + /*cputype_filter=3D*/false, + pm->metric_name, + /*metric_no_group=3D*/true, + /*metric_no_merge=3D*/false, + /*metric_no_threshold=3D*/true, + /*user_requested_cpu_list=3D*/NULL, + args->system_wide, + /*hardware_aware_grouping=3D*/false); + if (ret) { + /* Metric parsing failed but continue the search. */ + ret =3D 0; + goto out; + } + + /* + * Skip metrics none of whose events were recorded. Copies of the + * metric for each PMU, such as on hybrid systems where only one core + * PMU's events may have been recorded, are only added if all their + * events match. + */ + found =3D false; + evlist__for_each_entry(metric_evlist, metric_evsel) { + if (evsel_matcher__find(&args->matcher, metric_evsel, /*leader=3D*/NULL)= ) { + found =3D true; + break; } } - pr_debug("Found metric '%s' whose evsels match those of in the perf data\= n", - pm->metric_name); + if (!found) { + pr_debug("Skipping metric '%s' as no evsels are in the data\n", pm->metr= ic_name); + goto out; + } + + ret =3D add_matching_metric_evlist(args, metric_evlist, + /*default_metricgroup=3D*/false); out: evlist__put(metric_evlist); - return 0; + return ret; +} + +static int find_matching_metrics_args__init(struct find_matching_metrics_a= rgs *args, + struct evlist *evlist, bool system_wide, + bool match_grouping) +{ + args->evlist =3D evlist; + args->system_wide =3D system_wide; + return evsel_matcher__init(&args->matcher, evlist, EVSEL_MATCH_RECORDED, = match_grouping); +} + +static void find_matching_metrics_args__exit(struct find_matching_metrics_= args *args) +{ + evsel_matcher__exit(&args->matcher); } =20 int metricgroup__add_matching_metrics(struct evlist *evlist, bool system_w= ide) { struct perf_env *env =3D evlist__env(evlist) ?: host_env(); - struct find_matching_metrics_args args =3D { - .evlist =3D evlist, - .system_wide =3D system_wide, - }; + struct find_matching_metrics_args args; + int ret; =20 - return metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_tabl= e__find(), - find_matching_metrics_cb, &args); + /* + * The metrics are parsed without event grouping, so don't prefer + * recorded events by their grouping. + */ + ret =3D find_matching_metrics_args__init(&args, evlist, system_wide, + /*match_grouping=3D*/false); + if (ret) + goto out; + + ret =3D metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_tab= le__find(), + find_matching_metrics_cb, &args); +out: + find_matching_metrics_args__exit(&args); + return ret; } =20 static int append_metric_id_cb(const char *metric_id, void *data) @@ -2441,7 +2846,8 @@ static int parse_groups(struct evlist *perf_evlist, * Unless sharing is disabled, metrics use the events of other metrics, * and those already in perf_evlist, that count the same thing. */ - ret =3D evsel_matcher__init(&matcher, perf_evlist); + ret =3D evsel_matcher__init(&matcher, perf_evlist, EVSEL_MATCH_LIVE, + /*match_grouping=3D*/true); if (ret) goto out; =20 diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index 2069211913dc..25398eafd6e2 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -104,7 +104,10 @@ int metricgroup__for_each_metric(struct perf_pmus *pmu= s, const struct pmu_metric * * For every metric whose events all have a corresponding evsel in @evlist, * add the metric to the metric events of @evlist so that it is computed a= nd - * displayed along with the counts of the evsels. + * displayed along with the counts of the evsels. Evsels correspond when t= hey + * program the same counter on a PMU of the same name, with legacy hardware + * events matching the equivalent sysfs event of the PMU. A metric already= added + * for the same evsels isn't added again. * * Return: 0 on success or a negative error code. */ diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index b70ba9b58c43..fa528adf42cb 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -341,6 +341,7 @@ __add_event(struct list_head *list, int *idx, (*idx)++; evsel->core.cpus =3D cpus; evsel->core.pmu_cpus =3D pmu_cpus; + evsel->has_user_cpus =3D has_user_cpus; evsel->core.requires_cpu =3D pmu ? pmu->is_uncore : false; evsel->core.is_pmu_core =3D is_pmu_core; evsel->core.is_pmu_uncore =3D pmu ? pmu->is_uncore : false; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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=1795212960; b=aXoRDg8U8X/uTMla9HqQAjLcQhxlRvC9kBO0r0u/9J0yDO40ySAsJPGxYdZvep3sTRcm XMrkkgUJg9yZCJhxsj5/+QZtq7/pjJGUdrC7gBHONf+ErrT/TCAdX5b6j/ayqRXaRJT3u 0VFmZYTc4Ch7yLjdHS64oZTCjMkhM6+I5iRCW+TdfvfwghuDFt+UH9JCC6WG4ZT/Tf4vE PmKI7u0Gb9VeNtSQG8CDIuspR8MOy8lM5IZR6LuUGjGPmm3BSrvlpTZjaSVkCP6x0hiCA 3wdq2fPd9nbeRuAPfeScPCbfZaF46AFhbFUbTciu+SiZH6lqtsGsCZ9FTJ6xxrHvkvA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212960; 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=Smuc5oSE4Rk1CEz3klTX+3kN4HJeddocJmM5xW2MgiQ=; b=Glm7YyRLN8tHLmQ8ZuN5b3CjwkqrsMYP73ZE2s62AuD9B/sBTC0MK7xnILqDOQE74D1i FP033Kb9H27j7NQp/D51DUrhqwYErgKZzJTyKcoo41JFtq+wUrOYT9cwmu0MnjygTZgIF ROijbyszNY0EFOaEaF6rp+ayABqR/UA36agQ8Bcs0pYEM7SwLvBdNLOIDApyRLT9Ds/fd 99S5AcMVQSYy0moT8dzg5weU/0yYimtP6c7tuc3wgmrgZQ8j78bzGPnnc3bzC4bpU7WtG XRGz9BlnzELrKXsBUo4VbZ0nNnVHQUwsbOY/gjpUM79j1E1jnHysjz8gwJnYBen9o2g== 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=MrEQjImY; 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 1555360A2627 for ; Fri, 20 Nov 2026 22:15:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1555360A2627 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=MrEQjImY Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-139b62317d0so3612812c88.0 for ; Fri, 20 Nov 2026 14:15:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212955; x=1795817755; 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=Smuc5oSE4Rk1CEz3klTX+3kN4HJeddocJmM5xW2MgiQ=; b=MrEQjImYFyv3JtbxTHqmlX3EmtEZPFkPi1VTaqdBWo8uXW8YrQt4q3BiNB3Emf3aXQ f/JvMyhaWPG8DWmxclAQnsydB7zKyjUg/+sasHrEtiquvvGW1kcxNAgxvoZj4w3hgK9j xgtzESSr4fUOpQarp/DnyeRzX12/xu2YzKN9e5t+SUVgKM9gObsS6dt5ouRvflFoa+6q vWorX8tk5KwFox1PHVzSMnAK3eUZ8sNe4qxOjEwTeuSJdE9uNz/lHJlKJWmp0aVmYsrl acnT38qvbUpaxtQq7+nLiY+GSbKqoGpBA28k1vxTIdDpW1ET5iiOXuIWiBLKluQzDTlm +l3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212955; x=1795817755; 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=Smuc5oSE4Rk1CEz3klTX+3kN4HJeddocJmM5xW2MgiQ=; b=0lgBUFL+L6OqSnJaQ0oeUPnsY+RWNtieLVztIDLZTprk2E37L6nOfwsMb+/mKYEJ+V Gke+bg8MM1sQuQA3vBpQvRUKBF/4liDumk4C7A1rZHY7hfyFV68FXJ2aRjM/bsUosPTA ksCkQMeRniJJ4NQgLIUP9vcyW7MANs8YtHoOxUsJ5oQJaZ5dJTODtgsSp82fowMuYHRC U8fuEaLWDfkxugWrWqpbQCOiDcbYKU6l7SJkqwrCNT7bf7GOigGZGSjsiGzfXCSTx7qu 0B5vDof3g17ev1q48de8Dby3AMOHNn1tz6XqVAgc5kxlu6WaLQ8m5Sotzu6ywJdCRwv1 lKOw== X-Forwarded-Encrypted: i=1; AL+hErFlEDEGYgJDkmQyIYBZJ1wiYbPHdQRIYYIFyapbh+kN3tQnxcGq2MhBfaSEIwrEPABbuIKRQVyqPSolVvI=@vger.kernel.org X-Gm-Message-State: APuU4Z86mnbHHPNoDFlkXDujFmU8V3aM25juqsHulwcA0nFu+qslARuR NsWA1PWR3Piv4NSj0gSLaSEOBUf0JqYhosZ9asLYikUKYFT096jmzzk8c4d/qzenuub8dOYk/2/ bKwpQrYn04g== X-Received: from dldyr5-n2.prod.google.com ([2002:a05:701b:4505:20b0:1b5:3561:2fe9]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:ed02:b0:14d:cb67:afc4 with SMTP id a92af1059eb24-1b55691d6e4mr4397957c88.41.1795212954654; Fri, 20 Nov 2026 14:15:54 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:28 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-28-irogers@google.com> Subject: [PATCH v3 27/30] perf metricgroup: Add metricgroup__add_recorded_metrics() 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add metricgroup__add_matching_metric_groups() and metricgroup__add_recorded_metrics() to parse metrics or metricgroups requested on a recorded perf stat command line (or the Default metricgroups when no events or metrics were explicitly specified) and match them against the recorded events in the evlist. When reporting commands such as 'perf stat report' or 'perf script' inspect a perf.data file recorded by 'perf stat record', this allows reconstructing the exact metrics requested at record time (including -M, --transaction, --smi-cost, --topdown, -d, --cputype, and --pmu-filter) or optionally adding all matching JSON metrics when --all-metrics is enabled. Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 358 ++++++++++++++++++++++++++++++++-- tools/perf/util/metricgroup.h | 62 ++++++ 2 files changed, 407 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 72b552829659..d4038cb5b3e0 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -21,6 +21,7 @@ #include "tool_pmu.h" #include "expr.h" #include "rblist.h" +#include #include #include #include @@ -1454,25 +1455,35 @@ static bool metric_events__contains(struct rblist *= metric_events, const struct m return false; } =20 +static void evsel_matcher__mark_default(struct evsel_matcher *matcher, str= uct evsel *metric_evsel) +{ + struct counter_key key; + bool fake =3D metric_evsel->pmu && perf_pmu__is_fake(metric_evsel->pmu); + + evsel_matcher__key(matcher, metric_evsel, &key); + for (size_t i =3D 0; i < matcher->nr_entries; i++) { + struct evsel_matcher_entry *entry =3D &matcher->entries[i]; + struct evsel *pos =3D entry->evsel; + + if (!counter_key__equal(&key, &entry->key, matcher->mode)) + continue; + if ((fake || (pos->pmu && perf_pmu__is_fake(pos->pmu))) && + !evsel__shares_metric_id(metric_evsel, pos)) + continue; + pos->default_metricgroup =3D true; + if (metric_evsel->default_show_events) + pos->default_show_events =3D true; + } + counter_key__exit(&key); +} + static int add_matching_metric_evlist(struct find_matching_metrics_args *a= rgs, struct evlist *metric_evlist, bool default_metricgroup) { struct evlist *data_evlist =3D args->evlist; struct evsel *metric_evsel; - - if (default_metricgroup) { - evlist__for_each_entry(metric_evlist, metric_evsel) { - if (metric_evsel->default_show_events) { - struct evsel *data_evsel =3D - evsel_matcher__find(&args->matcher, metric_evsel, - /*leader=3D*/NULL); - - if (data_evsel) - data_evsel->default_show_events =3D true; - } - } - } + bool any_matched =3D false; =20 /* * Metric expressions are associated with a metric leader event and @@ -1515,6 +1526,7 @@ static int add_matching_metric_evlist(struct find_mat= ching_metrics_args *args, pos->pmu, data_evsel); if (!copy) continue; + any_matched =3D true; if (metric_events__contains(evlist__metric_events(data_evlist), copy)) { metric_expr__delete(copy); @@ -1535,6 +1547,17 @@ static int add_matching_metric_evlist(struct find_ma= tching_metrics_args *args, } } } + + if (default_metricgroup && any_matched) { + /* + * Mark all recorded events counting the same thing as a Default + * metric event, including duplicates across groups, so + * perf_stat__skip_metric_event() hides unused duplicates while + * leaving unrelated events visible. + */ + evlist__for_each_entry(metric_evlist, metric_evsel) + evsel_matcher__mark_default(&args->matcher, metric_evsel); + } return 0; } =20 @@ -1633,6 +1656,315 @@ int metricgroup__add_matching_metrics(struct evlist= *evlist, bool system_wide) return ret; } =20 +int metricgroup__add_matching_metric_groups(struct evlist *evlist, + const char *pmu, + bool cputype_filter, + const char *str, + bool metric_no_group, + bool metric_no_merge, + bool metric_no_threshold, + bool system_wide, + bool default_metricgroup) +{ + struct find_matching_metrics_args args; + struct evlist *metric_evlist =3D NULL; + int ret; + + ret =3D find_matching_metrics_args__init(&args, evlist, system_wide, + /*match_grouping=3D*/!metric_no_group); + if (ret) + goto out; + + metric_evlist =3D evlist__new(); + if (!metric_evlist) { + ret =3D -ENOMEM; + goto out; + } + evlist__set_session(metric_evlist, evlist__session(evlist)); + + ret =3D metricgroup__parse_groups(metric_evlist, + pmu ?: "all", + cputype_filter, + str, + metric_no_group, + metric_no_merge, + metric_no_threshold, + /*user_requested_cpu_list=3D*/NULL, + system_wide, + /*hardware_aware_grouping=3D*/false); + if (!ret) { + ret =3D add_matching_metric_evlist(&args, metric_evlist, + default_metricgroup); + } + +out: + evlist__put(metric_evlist); + find_matching_metrics_args__exit(&args); + return ret; +} + +static void append_recorded_metric_str(char **rec_metrics, const char *m_a= rg) +{ + char *tmp =3D NULL; + + if (!m_arg) + return; + if (!*rec_metrics) { + *rec_metrics =3D strdup(m_arg); + } else if (asprintf(&tmp, "%s,%s", *rec_metrics, m_arg) >=3D 0) { + free(*rec_metrics); + *rec_metrics =3D tmp; + } +} + +void metricgroup__add_recorded_metrics(struct evlist *evlist, + const char *metrics, + const char *pmu_filter, + bool cputype_filter, + bool metric_no_group, + bool metric_no_merge, + bool metric_no_threshold, + bool all_metrics, + bool system_wide, + bool default_metricgroup, + bool *metric_only) +{ + struct perf_env *env =3D evlist__env(evlist) ?: host_env(); + struct perf_pmus *pmus =3D perf_env__pmus(env); + const char **argv =3D env->cmdline_argv; + int argc =3D env->nr_cmdline; + char *rec_metrics =3D NULL; + bool rec_transaction =3D false, rec_smi =3D false, rec_topdown =3D false; + bool rec_null =3D false, has_events =3D false, had_rec_metrics =3D false; + bool rec_metric_only =3D false, rec_force_metric_only =3D false; + bool rec_no_group =3D metric_no_group; + bool rec_no_merge =3D metric_no_merge; + bool rec_no_threshold =3D metric_no_threshold; + bool seen_record =3D false; + unsigned int rec_td_level =3D 0; + int rec_detailed =3D 0; + + if (metrics) { + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, metrics, + metric_no_group, + metric_no_merge, + metric_no_threshold, + system_wide, + /*default_metricgroup=3D*/false); + if (all_metrics) + metricgroup__add_matching_metrics(evlist, system_wide); + return; + } + + if (!argv || argc < 2 || strcmp(argv[1], "stat")) { + if (all_metrics) + metricgroup__add_matching_metrics(evlist, system_wide); + return; + } + + for (int i =3D 2; i < argc && argv[i]; i++) { + const char *arg =3D argv[i]; + + if (!strcmp(arg, "--")) + break; + + if (arg[0] !=3D '-') { + if (!seen_record && strlen(arg) > 2 && strstarts("record", arg)) { + seen_record =3D true; + continue; + } + break; + } + + if (arg[1] !=3D '-') { + for (const char *p =3D arg + 1; *p; p++) { + if (*p =3D=3D 'd') { + rec_detailed++; + } else if (*p =3D=3D 'T') { + rec_transaction =3D true; + } else if (*p =3D=3D 'n') { + rec_null =3D true; + } else if (*p =3D=3D 'e') { + has_events =3D true; + if (!p[1]) + i++; + break; + } else if (*p =3D=3D 'M') { + const char *m_arg =3D p[1] ? p + 1 : NULL; + + if (!m_arg && i + 1 < argc) + m_arg =3D argv[++i]; + append_recorded_metric_str(&rec_metrics, m_arg); + break; + } else if (strchr("ptbrCxGoID", *p)) { + if (!p[1]) + i++; + break; + } + } + continue; + } + + if (!strcmp(arg, "--metrics") || strstarts(arg, "--metrics=3D")) { + const char *m_arg =3D arg[9] =3D=3D '=3D' ? arg + 10 + : (i + 1 < argc ? argv[++i] : NULL); + + append_recorded_metric_str(&rec_metrics, m_arg); + } else if (!strcmp(arg, "--transaction")) { + rec_transaction =3D true; + } else if (!strcmp(arg, "--smi-cost")) { + rec_smi =3D true; + } else if (!strcmp(arg, "--topdown")) { + rec_topdown =3D true; + } else if (!strcmp(arg, "--td-level") || strstarts(arg, "--td-level=3D")= ) { + const char *td_arg =3D arg[10] =3D=3D '=3D' ? arg + 11 + : (i + 1 < argc ? argv[++i] : NULL); + + if (td_arg) + rec_td_level =3D strtoul(td_arg, NULL, 10); + } else if (!strcmp(arg, "--detailed")) { + rec_detailed++; + } else if (!strcmp(arg, "--null")) { + rec_null =3D true; + } else if (!strcmp(arg, "--metric-only")) { + rec_metric_only =3D true; + rec_force_metric_only =3D true; + } else if (!strcmp(arg, "--no-metric-only")) { + rec_metric_only =3D false; + rec_force_metric_only =3D true; + } else if (!strcmp(arg, "--metric-no-group")) { + rec_no_group =3D true; + } else if (!strcmp(arg, "--no-metric-no-group")) { + rec_no_group =3D false; + } else if (!strcmp(arg, "--metric-no-merge")) { + rec_no_merge =3D true; + } else if (!strcmp(arg, "--no-metric-no-merge")) { + rec_no_merge =3D false; + } else if (!strcmp(arg, "--metric-no-threshold")) { + rec_no_threshold =3D true; + } else if (!strcmp(arg, "--no-metric-no-threshold")) { + rec_no_threshold =3D false; + } else if (!strcmp(arg, "--cputype") || strstarts(arg, "--cputype=3D")) { + const char *cpu_arg =3D arg[9] =3D=3D '=3D' ? arg + 10 + : (i + 1 < argc ? argv[++i] : NULL); + + if (cpu_arg) { + const struct perf_pmu *pmu =3D + perf_pmus__pmu_for_pmu_filter(pmus, cpu_arg); + + pmu_filter =3D pmu ? pmu->name : cpu_arg; + cputype_filter =3D true; + } + } else if (!strcmp(arg, "--pmu-filter") || strstarts(arg, "--pmu-filter= =3D")) { + pmu_filter =3D arg[12] =3D=3D '=3D' ? arg + 13 + : (i + 1 < argc ? argv[++i] : NULL); + cputype_filter =3D false; + } else if (!strcmp(arg, "--event") || strstarts(arg, "--event=3D") || + !strcmp(arg, "--pfm-events") || strstarts(arg, "--pfm-events=3D")) { + has_events =3D true; + if (!strchr(arg, '=3D')) + i++; + } else if (!strcmp(arg, "--iostat") || strstarts(arg, "--iostat=3D")) { + has_events =3D true; + } else if (!strchr(arg, '=3D') && + (!strcmp(arg, "--filter") || !strcmp(arg, "--pid") || + !strcmp(arg, "--tid") || !strcmp(arg, "--bpf-prog") || + !strcmp(arg, "--bpf-attr-map") || !strcmp(arg, "--repeat") || + !strcmp(arg, "--cpu") || !strcmp(arg, "--field-separator") || + !strcmp(arg, "--cgroup") || !strcmp(arg, "--for-each-cgroup") || + !strcmp(arg, "--output") || !strcmp(arg, "--log-fd") || + !strcmp(arg, "--pre") || !strcmp(arg, "--post") || + !strcmp(arg, "--interval-print") || !strcmp(arg, "--interval-count"= ) || + !strcmp(arg, "--timeout") || !strcmp(arg, "--delay") || + !strcmp(arg, "--tpebs-mode") || !strcmp(arg, "--control"))) { + i++; + } + } + + if (rec_force_metric_only && metric_only) + *metric_only =3D rec_metric_only; + + if (rec_metrics) { + had_rec_metrics =3D true; + if (!all_metrics) { + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, rec_metrics, + rec_no_group, rec_no_merge, + rec_no_threshold, system_wide, + /*default_metricgroup=3D*/false); + } + zfree(&rec_metrics); + } + + if (rec_transaction) { + if (!all_metrics) { + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, "transaction", + rec_no_group, rec_no_merge, + rec_no_threshold, system_wide, + /*default_metricgroup=3D*/false); + } + } else if (rec_smi) { + if (metric_only && !rec_force_metric_only && !all_metrics) + *metric_only =3D true; + if (!all_metrics) { + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, "smi", + rec_no_group, rec_no_merge, + rec_no_threshold, system_wide, + /*default_metricgroup=3D*/false); + } + } else if (rec_topdown) { + char str[] =3D "TopdownL1"; + + if (metric_only && !rec_force_metric_only && !all_metrics) + *metric_only =3D true; + if (!rec_td_level) + rec_td_level =3D 1; + str[8] =3D rec_td_level + '0'; + if (!all_metrics) { + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, str, + /*metric_no_group=3D*/false, + /*metric_no_merge=3D*/false, + /*metric_no_threshold=3D*/true, + system_wide, + /*default_metricgroup=3D*/false); + } + } else if (!has_events && !had_rec_metrics && !rec_null) { + static const char * const default_names[] =3D { + "Default", "Default2", "Default3", "Default4", + }; + struct evsel *evsel; + + if (default_metricgroup) { + evlist__for_each_entry(evlist, evsel) { + if (!evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) + evsel->skippable =3D true; + } + } + for (size_t i =3D 0; !all_metrics && i < ARRAY_SIZE(default_names); i++)= { + if ((int)i > rec_detailed) + break; + if (!metricgroup__has_metric_or_groups(pmus, pmu_filter ?: "all", + default_names[i])) + continue; + metricgroup__add_matching_metric_groups(evlist, pmu_filter, + cputype_filter, + default_names[i], + /*metric_no_group=3D*/false, + /*metric_no_merge=3D*/false, + /*metric_no_threshold=3D*/true, + system_wide, + default_metricgroup); + } + } + + if (all_metrics) + metricgroup__add_matching_metrics(evlist, system_wide); +} + static int append_metric_id_cb(const char *metric_id, void *data) { struct strbuf *sb =3D data; diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index 25398eafd6e2..20fb4ee3cacc 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -112,6 +112,68 @@ int metricgroup__for_each_metric(struct perf_pmus *pmu= s, const struct pmu_metric * Return: 0 on success or a negative error code. */ int metricgroup__add_matching_metrics(struct evlist *evlist, bool system_w= ide); +/** + * metricgroup__add_matching_metric_groups() - Parse @str and attach metri= cs + * whose events match @evlist. + * @evlist: The recorded evlist to attach matching metric expressions to. + * @pmu: PMU filter name or "all". + * @cputype_filter: Was @pmu specified via --cputype? + * @str: Metric or metricgroup names to parse. + * @metric_no_group: Do not group metric events. + * @metric_no_merge: Do not merge shared metric events. + * @metric_no_threshold: Ignore metric threshold expressions. + * @system_wide: Were the events recorded system-wide? + * @default_metricgroup: Mark matched events with Default metricgroup disp= lay flags. + * + * Return: 0 on success or a negative error code. + */ +int metricgroup__add_matching_metric_groups(struct evlist *evlist, + const char *pmu, + bool cputype_filter, + const char *str, + bool metric_no_group, + bool metric_no_merge, + bool metric_no_threshold, + bool system_wide, + bool default_metricgroup); +/** + * metricgroup__add_recorded_metrics() - Add metrics for a recorded perf.d= ata + * evlist. + * @evlist: The recorded evlist to attach metrics to. + * @metrics: Explicit metric names requested by the reporting command, or = NULL + * to inspect the recorded perf stat command line in perf_env. + * @pmu_filter: Optional PMU filter string. + * @cputype_filter: Was @pmu_filter specified via --cputype? + * @metric_no_group: Default metric_no_group setting. + * @metric_no_merge: Default metric_no_merge setting. + * @metric_no_threshold: Default metric_no_threshold setting. + * @all_metrics: Also add all JSON metrics whose events match @evlist. + * @system_wide: Were the events recorded system-wide? + * @default_metricgroup: Enable Default metricgroup header/skip display fl= ags. + * @metric_only: Optional pointer updated if the recorded command line for= ced + * --metric-only or --no-metric-only. + */ +void metricgroup__add_recorded_metrics(struct evlist *evlist, + const char *metrics, + const char *pmu_filter, + bool cputype_filter, + bool metric_no_group, + bool metric_no_merge, + bool metric_no_threshold, + bool all_metrics, + bool system_wide, + bool default_metricgroup, + bool *metric_only); +/** + * metricgroup__has_metric_or_groups() - Check if a metric or metricgroup + * named @metric_or_groups exists. + * @pmus: The PMUs to search. + * @pmu: PMU filter name or "all". + * @metric_or_groups: A metric or metricgroup name. + * + * Return: true if a metric on @pmu is named @metric_or_groups or is in the + * metricgroup @metric_or_groups. + */ bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char = *pmu, const char *metric_or_groups); unsigned int metricgroups__topdown_max_level(void); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212965; b=3a/c/+S7je939ruv4Rp8f9anyVZQlSFVQwKwRmyXV/sg4yFqC8spDGhDskrzv24gQSbL McW4+kDSekaIS7Kurwlpuxh/TH9sh1zRdhFPZSPo1QUe7lkrzvmfrgPhGBjmL1d6kbmLJ V6yyrtwyVgOgHnDX7FKX14D8tGSmaLzAPE0ZLshWCAhMvqtN5wlcwcI7dvSai5xUfbVzl RZTWbqpAZ+rlezjYBZr+imNP4BjRFZo9sOHNgnYOF1A13GrKeZFL9hbSUagzwkWIB3NhI EspiQUzyVG2zAinUMuGeX3TujpuPsnFg9xRofRunU/WbUrIXgHo+aeJw5uAsKm6atPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212965; 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=2HEN6VUdgvdF1e1P4lzoTvsLs3mhrZspRNp4JiB/L5I=; b=2FCR123n/UL2P/6v88rediJbd313zTSZRDbMhnsXdorD27tUFhxhsVXwAESKc+/TMYWN 5loTI4ZHJ8xyal1vdVEjuF7SlAkEo7Ih0gy7Z/e0wi39VtXEEWVTHSRWpG+O+VXPFv8ii LaQFGitVEzmJf4X7lDwLKeHjjSJjk3qdx6bG9seDTpc4P3jucE1oBi2RoOOlQ1+4OmdtC lwHyuU3OoOyKZH7oibJfcBOl6IOnDSzgAqyweZYd9HWUqBxlZ/BEOI9fH7jlKISymbQIx 5uA9Gco/rJhhs+o3iXA9ywrRg88naG8JnpqEGlEG7M5PivxnQXsZb376u8noMjJyXPg== 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=VgtTw4XP; arc=none smtp.remote-ip=209.85.214.197 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (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 6E28D60A2610 for ; Fri, 20 Nov 2026 22:16:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6E28D60A2610 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=VgtTw4XP Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2edd25534afso11667165ad.2 for ; Fri, 20 Nov 2026 14:16:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212962; x=1795817762; 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=2HEN6VUdgvdF1e1P4lzoTvsLs3mhrZspRNp4JiB/L5I=; b=VgtTw4XP3Tea/OPo+j/xL5RWam/c6pQbvxUCKaIsinEksk+QAnx7FvPVCnQ4asjWHS DVnJvsjJvBcbimAFeXgKI7N8qJdApmcHf/n7BJiotla/R7ksKwW69/CfOHqTJL6zince eVzmoHlFwO5RPPH1EzEexSSHQsFkBOnF2Uwh1CH8c9KbdG4IK66Q7+1hxNj0GaSrZ8D1 RGbL+jy5+mlBdOfsIkuNKxwkBgGdWu88VzEgPCdXmWDtroEhuFGTeoZnRA4L7vxW0SZS a4HJBr7Qa16T58e+zVRrJ6SXCWSqERFxPTOz5dg6loQ6rqy4ZUx7rq/ZezqfSka5oRP+ KnYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212962; x=1795817762; 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=2HEN6VUdgvdF1e1P4lzoTvsLs3mhrZspRNp4JiB/L5I=; b=em6Wvhhq6IXpReDS4UJGr6vy5i0ecP8D9i76NyOnmQJMtqnqrTSc0MBrxE+O8s9y1h 6BAhHrcIi6SiXJQcgDqvD1y/yJXWW31Dx3EV/vIvzyZMnQfajuCj72S4juCr8ZXsW8B0 T4OMdE2dmjRvRQKpIFDwQ3T1cpB34xaGUSCa0FG4DRoop2IhI0nM/2gjP/kIuOYI2kyO txlwY33YIbu/g2mN/kstk9PmI+sO+z388GQk6wNelm7SY8RJVcOetzjIAEX1C4Ua86PG jmkObvVcALI2WGIHA5c9/B4VD0z4YDw0lhmmlpQtK2SUDlbQEQTDdHn1LYG1oNnj7R1R frsg== X-Forwarded-Encrypted: i=1; AL+hErGrhYMdKbuG+HFU2FrAjWgQu8AoZAYevAEdAOSP+rsFFV8YPo6igfXNHaqUZwt3K7qqfn42DilLBW5WYLc=@vger.kernel.org X-Gm-Message-State: APuU4Z/Xuf50QkPzmSktYen2rSnMAnKME8koyi5ElL5BVq+qst7DU6Ed iFc2csqTvE/VDwwDLRYQmV8l69C8TG3NdPbp5UnKuC6jZ9+WTVf+4fRLINgjQl7UYk03R+oUomi wcx3ozqIY5g== X-Received: from plhc7.prod.google.com ([2002:a17:903:2347:b0:2e4:afb6:7889]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c401:b0:2ef:e9df:2429 with SMTP id d9443c01a7336-2f03f17d1afmr51904595ad.21.1795212961653; Fri, 20 Nov 2026 14:16:01 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:29 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-29-irogers@google.com> Subject: [PATCH v3 28/30] perf stat report: Display metrics for recorded events 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf stat report displays the recorded counts but not the metrics that perf stat record displayed, as metrics aren't recorded in the perf.data file. Inspect the recorded command line in struct perf_env and display the metrics requested on the perf stat record command line (or the default metrics when no events or metrics were explicitly specified), for example: $ perf stat record -M insn_per_cycle -a sleep 1 $ perf stat report ... 784,599,279 cpu_atom/cpu-cycles/ # 0.7 instructions insn_per_cycle 586,070,692 cpu_atom/instructions/ 776,737,361 cpu_core/cpu-cycles/ # 1.3 instructions insn_per_cycle 1,011,699,468 cpu_core/instructions/ 1.004467030 seconds time elapsed Also add --all-metrics (and the stat.all-metrics config option) to display all metrics whose events were recorded in the perf.data file, as well as -M/--metrics and --metric-only options to perf stat report. Set stat_config.system_wide from the recorded thread map as metrics such as CPUs_utilized depend upon it. Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-config.txt | 7 +++ tools/perf/Documentation/perf-stat.txt | 5 +++ tools/perf/builtin-stat.c | 54 +++++++++++++++++++++--- tools/perf/util/config.c | 3 ++ tools/perf/util/stat.h | 1 + 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/tools/perf/Documentation/perf-config.txt b/tools/perf/Document= ation/perf-config.txt index 6cff6f26af8d..906e59c7e9d5 100644 --- a/tools/perf/Documentation/perf-config.txt +++ b/tools/perf/Documentation/perf-config.txt @@ -713,6 +713,13 @@ stat.*:: (boolean) Change the default for "--big-num". To make "--no-big-num" the default, set "stat.big-num=3Dfalse". =20 + stat.all-metrics:: + (boolean) Change the default for "--all-metrics" in + "perf stat report" and "perf script". When true, all metrics + whose events were recorded in the data file are displayed rather + than only the metrics requested on the "perf stat record" + command line. + intel-pt.*:: =20 intel-pt.cache-divisor:: diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentat= ion/perf-stat.txt index 8ea17da89976..7258ec280770 100644 --- a/tools/perf/Documentation/perf-stat.txt +++ b/tools/perf/Documentation/perf-stat.txt @@ -426,6 +426,11 @@ information at the boundary of the level 3 cache in th= e system. --per-core:: Aggregate counts per physical processor for system-wide mode measurements. =20 +--all-metrics:: +Display all metrics that match the recorded events in the perf data file, +rather than only the metrics requested on the 'perf stat record' command l= ine. +Default setting can be changed with "perf config stat.all-metrics=3Dtrue". + -M:: --metrics:: Print metrics or metricgroups specified in a comma separated list. diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 2b563382cf43..64883e7cb6aa 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2246,6 +2246,29 @@ static int process_stat_round_event(const struct per= f_tool *tool __maybe_unused, struct perf_env *env =3D perf_session__env(session); const char **argv =3D env->cmdline_argv; int argc =3D env->nr_cmdline; + static bool metrics_added; + + if (!metrics_added) { + struct perf_thread_map *threads =3D evlist__core(session->evlist)->threa= ds; + bool system_wide =3D !threads || perf_thread_map__nr(threads) < 1 || + perf_thread_map__pid(threads, /*idx=3D*/0) =3D=3D -1; + + /* Metrics may use system_wide, such as for #target_cpu. */ + stat_config.system_wide =3D system_wide; + metricgroup__add_recorded_metrics(session->evlist, metrics, + parse_events_option_args.pmu_filter, + parse_events_option_args.cputype_filter, + stat_config.metric_no_group, + stat_config.metric_no_merge, + stat_config.metric_no_threshold, + stat_config.all_metrics, + system_wide, + /*default_metricgroup=3D*/true, + force_metric_only ? NULL + : &stat_config.metric_only); + zfree(&metrics); + metrics_added =3D true; + } =20 process_counters(); =20 @@ -2296,6 +2319,8 @@ int process_stat_config_event(const struct perf_tool = *tool, =20 static int set_maps(struct perf_stat *st) { + struct evsel *counter; + if (!st->cpus || !st->threads) return 0; =20 @@ -2307,6 +2332,9 @@ static int set_maps(struct perf_stat *st) if (evlist__alloc_stats(&stat_config, evsel_list, /*alloc_raw=3D*/true)) return -ENOMEM; =20 + evlist__for_each_entry(evsel_list, counter) + counter->supported =3D false; + st->maps_allocated =3D true; return 0; } @@ -2365,7 +2393,7 @@ static int __cmd_report(int argc, const char **argv) { struct perf_session *session; struct opt_aggr_mode opt_mode =3D {}; - const struct option options[] =3D { + struct option options[] =3D { OPT_STRING('i', "input", &input_name, "file", "input file name"), OPT_BOOLEAN(0, "per-thread", &opt_mode.thread, "aggregate counts per thre= ad"), OPT_BOOLEAN(0, "per-socket", &opt_mode.socket, @@ -2381,11 +2409,20 @@ static int __cmd_report(int argc, const char **argv) OPT_BOOLEAN(0, "per-node", &opt_mode.node, "aggregate counts per numa nod= e"), OPT_BOOLEAN('A', "no-aggr", &opt_mode.no_aggr, "disable aggregation across CPUs or PMUs"), + OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list", + "display specified metrics or metric groups (separated by ,)", + append_metric_groups), + OPT_BOOLEAN(0, "all-metrics", &stat_config.all_metrics, + "display all metrics matching recorded events"), + OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL, + "Only print computed metrics. No raw values", + enable_metric_only), OPT_END() }; struct stat st; int ret; =20 + set_option_flag(options, 'M', "metrics", PARSE_OPT_NONEG); argc =3D parse_options(argc, argv, options, stat_report_usage, 0); =20 perf_stat.aggr_mode =3D opt_aggr_mode_to_aggr_mode(&opt_mode); @@ -2412,8 +2449,10 @@ static int __cmd_report(int argc, const char **argv) perf_stat.tool.stat_round =3D process_stat_round_event; =20 session =3D perf_session__new(&perf_stat.data, &perf_stat.tool); - if (IS_ERR(session)) - return PTR_ERR(session); + if (IS_ERR(session)) { + ret =3D PTR_ERR(session); + goto out; + } =20 perf_stat.session =3D session; stat_config.output =3D stderr; @@ -2422,10 +2461,13 @@ static int __cmd_report(int argc, const char **argv) =20 ret =3D perf_session__process_events(session); if (ret) - return ret; + goto out; =20 perf_session__delete(session); - return 0; +out: + /* Freed when processing the first stat round, if there was one. */ + zfree(&metrics); + return ret; } =20 static void setup_system_wide(int forks) @@ -2603,6 +2645,8 @@ int cmd_stat(int argc, const char **argv) OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list", "monitor specified metrics or metric groups (separated by ,)", append_metric_groups), + OPT_BOOLEAN(0, "all-metrics", &stat_config.all_metrics, + "display all metrics matching recorded events (report only)"), OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel, "Configure all used events to run in kernel space.", PARSE_OPT_EXCLUSIVE), diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index 287401f159e6..d64d0db51d8e 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -514,6 +514,9 @@ static int perf_stat_config(const char *var, const char= *value) if (!strcmp(var, "stat.bpf-counter-events")) evsel__bpf_counter_events =3D strdup(value); =20 + if (!strcmp(var, "stat.all-metrics")) + stat_config.all_metrics =3D perf_config_bool(var, value); + /* Add other config variables here. */ return 0; } diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h index e3598037a6aa..a957479804c1 100644 --- a/tools/perf/util/stat.h +++ b/tools/perf/util/stat.h @@ -82,6 +82,7 @@ struct perf_stat_config { bool metric_no_group; bool metric_no_merge; bool metric_no_threshold; + bool all_metrics; bool hardware_aware_grouping; bool stop_read_counter; bool iostat_run; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 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=1795212970; b=wIWE2M/5ZKfgncm4/gKZzUmx9hw8PuJI+eNHLsRTa1ZJ4zxJJCqDtU3RQqLhaUfm6CGG nGFB2P81Q41x1FMIZXnKpSWEeNUVyPFYuO+vHqvNAei2FDvb3FH4c6+jUNmAdlPC+94PD f4dxibyGUi6lZ4dxKMR/XQEZVX7roW8R1drlnkmg8U96NtdtSls9bPxlM3GQGFJZ8FDAp DOuoRH/JMdE9bVMjI5IQ96q6ZUmkVZ2Ay7tdJr+I8s9Rdw+bbTqVxce0B0CgzrUG9KUdZ +/j46Kr1lOB1GD8xaFnHdIuz0w4v4YlxoYd1JCKpQl/PVEoXilSEqI2fh97Hm8SLx4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212970; 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=+3GA/Hx+rJjH8NEt3R7rrCgNjWYX47icXT6CUIFco3s=; b=zz14kAPGjRnLnnaCkC4nLzHTdV9n4aIaZ08l/6IS5UFTUNUnz1ik0QWsOf96Dm7KGkgZ oOy8g8i0YqIJmIb/BBVq3O+Jp9j5pyUScm+tHYMxy59UOhL14AK/NU5ZR/TIe+vAfNC/L qEHsZUdATLIgANmLOMtxP7Qkt4AGoxYefJ+Swcwbj6dXgIq4D8HFoLLFbQGxiiaPblXnC 4VNqEVER8JTvJOWvbPDUgy1JpDrgZt3ab+j0CQ5l6soJNA+tmK6zTodjCm2nTYzlPckR9 l7qO8C0VnPZ56KMz44f6Ht1yr6LskBHHX9a9QEWbylXG8u9dckr02fVdIdpw5z1MKGQ== 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=KTciVtpz; 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 86FAD606A32A for ; Fri, 20 Nov 2026 22:16:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 86FAD606A32A 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=KTciVtpz Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-3846d5f54c1so1754466eec.1 for ; Fri, 20 Nov 2026 14:16:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212968; x=1795817768; 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=+3GA/Hx+rJjH8NEt3R7rrCgNjWYX47icXT6CUIFco3s=; b=KTciVtpzt/ADkq/HTagr56OcyxiJJqKipleAe0Xhbws8JaU6XZYxtRiEftwyh3LUBq trQkCxfPIX4aRoKcPmeBrhe1tlm6NEx+KSQRfm25E60hRo+/VQ82sTf+r/kJTenTpOMx /hzqWPOyx47HLFUZu7mvoHFUgFY70+UsSlP5GrWN1W//iAKj5r3pzVMNY4UHfplSlggn 3J0GdfnWMK+iOe3WmSl0alfcbPGW0tN+vdfVQKjO2ctGHiMq+ZZRs6n0WwCXTKYlNeIz HcHakFhQc1EWRgSV9XPBEHnuVmSYa6s0Bb8EOY2y+hORhQjBxeDoZ1Z69rMntRlXR1SR Y0Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212968; x=1795817768; 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=+3GA/Hx+rJjH8NEt3R7rrCgNjWYX47icXT6CUIFco3s=; b=BkP/31fwC3nIuRJhgo4XK5lUpQ84IMB76sWpTqZmjF3RLHL4u+7BDB03m1MSvIbdD5 Q5PtQVTH72ZEjIC0IzRh0oXGgILqSBrFOYoI4ohr7e8l+Q0zOdU+pVtwHRhii15yu6cC 45inPXa9SpBm8U56YlnYdxqFyJCvij3O0IU3PpflJevfh+22he7YQzh5A6R0ggb69E6c rLkhsonS2F4bint2ti4ttU0Q9e4B1Ilhvn2f7fkFXMe1JAZdmuUqtVIQ615YyA9O3HYE 1/CwRs8sTqTDfEDaoHvQaN6UYDSjlpUVQd9fQOA5aNogJq/2swV/ADjk4G3aKid7TV1E PBSA== X-Forwarded-Encrypted: i=1; AL+hErFxWPjc/lHoIDR5F72SfOX8qEucaB9f3F9/J2shdNdNsNy0Jo5PfY/Q5z+5F8LL8WhoThK1QGxzeT5fukE=@vger.kernel.org X-Gm-Message-State: APuU4Z+VmCe2wcbhtm0850AZxJTXIUae3zhZteYOfU0S+YlPs5g/a56z gl1AShsjFv21kY3Sa/dPOtcd0yu02463NJxYDG63EI/r2DmazePydH+9PfpVKWMmRssiV0Aodv7 jsqLafsU+jg== X-Received: from dyba9.prod.google.com ([2002:a05:693c:63c9:b0:36d:8068:9180]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:f401:b0:385:e4d9:e0e7 with SMTP id 5a478bee46e88-385e4d9e3f5mr2821138eec.5.1795212967274; Fri, 20 Nov 2026 14:16:07 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:30 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-30-irogers@google.com> Subject: [PATCH v3 29/30] perf script: Display metrics for perf stat record data 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf script displays the counts of each stat round read from a perf stat record perf.data file. Also display, after each round, the metrics requested on the perf stat record command line (or all matching metrics when --all-metrics or stat.all-metrics=3Dtrue is used), computed from the counts aggregated across CPUs and threads, for example: $ perf stat record -M insn_per_cycle -a sleep 1 $ perf script CPU THREAD VAL ENA RUN TIME EVENT ... cpu_atom/cpu-cycles/ metric: 0.7 instructions insn_per_cycle cpu_core/cpu-cycles/ metric: 1.3 instructions insn_per_cycle Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-script.txt | 6 ++ tools/perf/builtin-script.c | 107 ++++++++++++++++++++++- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Document= ation/perf-script.txt index f0228e784ced..bfd69d411996 100644 --- a/tools/perf/Documentation/perf-script.txt +++ b/tools/perf/Documentation/perf-script.txt @@ -468,6 +468,12 @@ include::itrace.txt[] default behavior. If you want to see separate CALLCHAIN_DEFERRED records for some reason, use --no-merge-callchains explicitly. =20 +--all-metrics:: + Display all metrics that match the recorded stat events in the perf data + file, rather than only the metrics requested on the 'perf stat record' + command line. Default setting can be changed with + "perf config stat.all-metrics=3Dtrue". + :GMEXAMPLECMD: script :GMEXAMPLESUBCMD: include::guest-files.txt[] diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index afed2353ef80..135ec34494bd 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2334,8 +2334,13 @@ static void script_print_metric(struct perf_stat_con= fig *config __maybe_unused, =20 if (!fmt) return; - perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel, - PERF_RECORD_SAMPLE, mctx->fp); + if (mctx->sample) { + perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel, + PERF_RECORD_SAMPLE, mctx->fp); + } else { + /* Metric computed from the counts of a perf stat record round. */ + fputs(evsel__name(mctx->evsel), mctx->fp); + } fputs("\tmetric: ", mctx->fp); if (color) color_fprintf(mctx->fp, color, fmt, val); @@ -3795,26 +3800,112 @@ static void script__setup_sample_type(struct perf_= script *script) } } =20 +/* + * Set up the computation of metrics whose events were all recorded by per= f stat + * record. Counts are aggregated globally for the metrics. + */ +static bool script_stat_metrics_init(struct evlist *evlist) +{ + struct perf_thread_map *threads =3D evlist__core(evlist)->threads; + struct evsel *counter; + + evlist__for_each_entry(evlist, counter) { + if (!counter->stats) + return false; + } + /* Metrics may use system_wide, such as for #target_cpu. */ + stat_config.system_wide =3D !threads || perf_thread_map__nr(threads) < 1 = || + perf_thread_map__pid(threads, /*idx=3D*/0) =3D=3D -1; + metricgroup__add_recorded_metrics(evlist, /*metrics=3D*/NULL, + /*pmu_filter=3D*/NULL, + /*cputype_filter=3D*/false, + stat_config.metric_no_group, + stat_config.metric_no_merge, + stat_config.metric_no_threshold, + stat_config.all_metrics, + stat_config.system_wide, + /*default_metricgroup=3D*/false, + /*metric_only=3D*/NULL); + + if (rblist__empty(evlist__metric_events(evlist))) + return false; + + if (!stat_config.aggr_map) { + stat_config.aggr_get_id =3D script_aggr_cpu_id_get; + stat_config.aggr_map =3D + cpu_aggr_map__new(evlist__core(evlist)->user_requested_cpus, + aggr_cpu_id__global, /*data=3D*/NULL, + /*needs_sort=3D*/false); + if (!stat_config.aggr_map) + return false; + } + /* + * The aggregated stats may already have been allocated, with the + * aggr_map, for metrics computed from samples. + */ + if (!evlist__first(evlist)->stats->aggr && + evlist__alloc_aggr_stats(evlist, stat_config.aggr_map->nr)) + return false; + + if (!stat_config.output) + stat_config.output =3D stdout; + stat_config.aggr_mode =3D AGGR_GLOBAL; + return true; +} + +static void script_print_stat_metrics(struct evlist *evlist) +{ + struct evsel *counter; + int aggr_idx; + + evlist__for_each_entry(evlist, counter) { + struct perf_stat_output_ctx ctx =3D { + .print_metric =3D script_print_metric, + .new_line =3D script_new_line, + .ctx =3D &(struct metric_ctx) { + .evsel =3D counter, + .fp =3D stdout, + }, + .force_header =3D false, + }; + + cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) + perf_stat__print_shadow_stats(&stat_config, counter, aggr_idx, &ctx); + } +} + static int process_stat_round_event(const struct perf_tool *tool __maybe_u= nused, struct perf_session *session, union perf_event *event) { struct perf_record_stat_round *round =3D &event->stat_round; struct evsel *counter; + static int show_metrics =3D -1; + + if (show_metrics < 0) + show_metrics =3D script_stat_metrics_init(session->evlist) ? 1 : 0; + + if (show_metrics) + evlist__reset_aggr_stats(session->evlist); =20 evlist__for_each_entry(session->evlist, counter) { perf_stat_process_counter(&stat_config, counter); process_stat(counter, round->time); } =20 + if (show_metrics) + script_print_stat_metrics(session->evlist); + process_stat_interval(round->time); return 0; } =20 static int process_stat_config_event(const struct perf_tool *tool __maybe_= unused, - struct perf_session *session __maybe_unused, + struct perf_session *session, union perf_event *event) { + struct evsel *counter; + perf_event__read_stat_config(&stat_config, &event->stat_config); =20 /* @@ -3823,6 +3914,13 @@ static int process_stat_config_event(const struct pe= rf_tool *tool __maybe_unused */ stat_config.aggr_mode =3D AGGR_NONE; =20 + /* + * Mark counters unsupported until a PERF_RECORD_STAT event is + * processed for them by perf_event__process_stat_event(). + */ + evlist__for_each_entry(session->evlist, counter) + counter->supported =3D false; + return 0; } =20 @@ -4206,6 +4304,8 @@ int cmd_script(int argc, const char **argv) "Enable LBR callgraph stitching approach"), OPT_BOOLEAN('\0', "merge-callchains", &merge_deferred_callchains, "Enable merge deferred user callchains"), + OPT_BOOLEAN(0, "all-metrics", &stat_config.all_metrics, + "Display all metrics matching recorded stat events"), OPTS_EVSWITCH(&script.evswitch), OPT_END() }; @@ -4577,6 +4677,7 @@ int cmd_script(int argc, const char **argv) =20 zstd_fini(&(session->zstd_data)); evlist__free_stats(session->evlist); + zfree(&stat_config.aggr_map); perf_session__delete(session); perf_script__exit(&script); =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212975; b=t1Y8qW1oOihTt8RJFs6F1Ly8TfMRTvpNbaRqDbBMtFxXHcT3sZ3cQznqHkstmcx2KyP5 F3/KRR5thuZVUlaCfAAtFMnQHZ3axWbF3ovNw61WQiOZHU1FHngniK6zEEn8ZowHxPf+E N6eIPG2lZcOYlXP6mDgDCDQPmlW5cYD6z3nKpIFLsTuMcYWr7cE4a2HQex4+V+6xWYcEK fxivSejfkVIhvd2h34721UocL68hMDmk/gPi/Nv+0Mn0oZiTD4ItDDux81uktMEi21qWh WtH/pX/9xMdl8QwltixBwAadHpjY3Y9pyyVF9NeJhLLfdvXqbrASquLMt9y8hmJBXig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212975; 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=oHsRoybgY+Gjh4tRFzKxL6MY+fBFhB444U/1ojQ4iqU=; b=2iFiXoOQkJaY09HMhT33cRs3HIuMdH9ZWxdYMKkBgCgBHGNQi9nnx2zBfWdH9ztNSNqY cg2ImoMqQ1FTvWVuhWPUGvPvJNDAwnzVdBjee3sM1YqewjfuGmeSSRhG0YVXjC3b9OgLr N2GAqHtdRS7OSPdFINyVVWyHYJJJj/CYpdhvfZg2ibz1Wn4IC7tvnYm4x+uVQX3M37PSe fRI0MXl/OH/d7y5huwIrQ0W4HReX+Ep+cLs0KZodgFwH2Q9C95xoqzNfXyMsUqlUPNeB6 KpcZ6xW4bBWB2mNC7dRt7SJpRvoyX3FxBySkr/XvjEwuLWpy0U55qq7GICFhXtby7Xw== 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=ZMX5UBTX; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (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 DB50C60A2620 for ; Fri, 20 Nov 2026 22:16:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DB50C60A2620 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=ZMX5UBTX Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1383723dfddso1465346c88.0 for ; Fri, 20 Nov 2026 14:16:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795212972; x=1795817772; 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=oHsRoybgY+Gjh4tRFzKxL6MY+fBFhB444U/1ojQ4iqU=; b=ZMX5UBTXxuSn9MZdULk5Boi+06w35jYg5kNVDlf23q8wAcj1XW8lydHenKwr/mg9EU NQ6luQhkTaxLIG/kqPGe3x8fNf6am5dZ+s5vAKx212qW3jrLhp0L4ccBgoETGCCIZIMa i3CKILUwdPcMs7U4fQJODz4cXtonAXqX0q2VYBNB/TklxG75M2vy411JHsOBO8Crs894 Ps7UqnvEdzzCLDIo0dQRCH/7fRQXO6KAcF0p7++G9qqMZZqsA4QMknOxVJ+ACfNLCONq AKZHmOHCiXn5dOQp82dIsBjWgw9w5DF91GcIjsvoOZAcOJ6Iy/u57eDtE9c3SbIYuhFi pGpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212972; x=1795817772; 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=oHsRoybgY+Gjh4tRFzKxL6MY+fBFhB444U/1ojQ4iqU=; b=LHW6imYqfFBzA/QkXT5sqYrdDlTgI9RUgKHWE7UJPIzz5VKh710qhgvnMpkjQQ1GsR YACxVSsgHqv8wryR15WVjLDd9+O8lrV8eWRYD4uwlY72Da3sGKr8taS1JGxwfs2UmMrh De6vZCFf8xlnAnlbmHqvQQLxmwWlc1d2M5Un9cVkw0uAIEfWm9WiPu0lBuEZfxJd+5y2 UMA7LA780dDjIC/67D7C/fmDZy9JFb8kIb5nmhKFZYbAjKKqveRBL31sND26ptUE13Mk ikZoMExxDJaD5rls047MibG5eJTp/HZTLnQpVOQY0/fZYqvg/N2CCQIVRBoXb7dqqVDn PpHw== X-Forwarded-Encrypted: i=1; AL+hErGVW4fY22HNERDr1bsqezBLf3JjSHlWNfntx1IwrpMlLmpDp/QwEOAsTkcPF/T2YXFduwm92E3IJsnbCUM=@vger.kernel.org X-Gm-Message-State: APuU4Z+ImrgGQEjhQMkKd92ZeglLZp87lZkC9wd4esA43y5omK8rEIgY cWzqhXJx+3lGQYsMXt/7fPybm1z9JcEUXRlrlasAoy4siv9yUFQev0FCYizfjh+UZcfqRF6u1aE mps9Rbytztw== X-Received: from dlbrx25.prod.google.com ([2002:a05:7022:1719:b0:15f:5ef4:b896]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3a0b:b0:1a2:fa1d:1de5 with SMTP id a92af1059eb24-1b5568d861cmr4068875c88.20.1795212971463; Fri, 20 Nov 2026 14:16:11 -0800 (PST) Date: Fri, 20 Nov 2026 14:13:31 -0800 In-Reply-To: <20261120221332.2245191-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: <20261120221332.2245191-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261120221332.2245191-31-irogers@google.com> Subject: [PATCH v3 30/30] perf test: Add stat record metrics test 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test that metrics measured by perf stat record are displayed by perf stat report and perf script, and that events recorded with -e only display matching JSON metrics in perf stat report and perf script when --all-metrics, stat.all-metrics=3Dtrue, or -M is used. Signed-off-by: Ian Rogers --- tools/perf/tests/shell/stat.sh | 133 +++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index ac9e031831da..2e1d5978d55a 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -6,9 +6,25 @@ set -e =20 err=3D0 stat_output=3D$(mktemp /tmp/perf-stat-test-output.XXXXX) +stat_data=3D"" +stat_config=3D"" + +stat_record_metrics_cleanup() { + if [ -n "${stat_data}" ] + then + rm -f "${stat_data}" "${stat_data}.old" + stat_data=3D"" + fi + if [ -n "${stat_config}" ] + then + rm -f "${stat_config}" + stat_config=3D"" + fi +} =20 cleanup() { rm -f "${stat_output}" + stat_record_metrics_cleanup trap - EXIT TERM INT } =20 @@ -92,6 +108,122 @@ test_stat_record_script() { echo "stat record and script test [Success]" } =20 +test_stat_record_metrics() { + echo "stat record metrics test" + if ! perf stat -M insn_per_cycle true 2>&1 | grep -E -q "[0-9]+.*[[:spac= e:]]+insn_per_cycle" + then + echo "stat record metrics test [Skipped insn_per_cycle metric unsuppor= ted]" + return + fi + stat_data=3D$(mktemp /tmp/perf-stat-test-data.XXXXX) + if ! perf stat record -o "${stat_data}" -M insn_per_cycle true > /dev/nu= ll 2>&1 + then + echo "stat record metrics test [Failed record]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle" + then + echo "stat record metrics test [Failed missing metric in perf stat rep= ort]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_= cycle" + then + echo "stat record metrics test [Failed missing metric in perf script]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf stat record -o "${stat_data}" -e instructions,cycles true > /d= ev/null 2>&1 + then + echo "stat record metrics test [Failed record with events]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle" + then + echo "stat record metrics test [Failed unexpected metric in perf stat = report]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf stat report --all-metrics -i "${stat_data}" 2>&1 | grep -q "in= sn_per_cycle" + then + echo "stat record metrics test [Failed missing metric in perf stat rep= ort --all-metrics]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_cy= cle" + then + echo "stat record metrics test [Failed unexpected metric in perf scrip= t]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf script --all-metrics -i "${stat_data}" 2>&1 | grep -E -q "metr= ic:.*insn_per_cycle" + then + echo "stat record metrics test [Failed missing metric in perf script -= -all-metrics]" + stat_record_metrics_cleanup + err=3D1 + return + fi + stat_config=3D$(mktemp /tmp/perf-stat-test-config.XXXXX) + printf "[stat]\nall-metrics =3D true\n" > "${stat_config}" + if ! PERF_CONFIG=3D"${stat_config}" perf stat report -i "${stat_data}" 2= >&1 | \ + grep -q "insn_per_cycle" + then + echo "stat record metrics test [Failed stat report stat.all-metrics=3D= true]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! PERF_CONFIG=3D"${stat_config}" perf script -i "${stat_data}" 2>&1 |= \ + grep -E -q "metric:.*insn_per_cycle" + then + echo "stat record metrics test [Failed perf script stat.all-metrics=3D= true]" + stat_record_metrics_cleanup + err=3D1 + return + fi + rm -f "${stat_config}" + stat_config=3D"" + if ! perf stat report -M insn_per_cycle -i "${stat_data}" 2>&1 | grep -q= "insn_per_cycle" + then + echo "stat record metrics test [Failed missing metric in perf stat rep= ort -M insn_per_cycle]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf stat record -o "${stat_data}" true > /dev/null 2>&1 + then + echo "stat record metrics test [Failed default record]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle" + then + echo "stat record metrics test [Failed missing default metric in perf = stat report]" + stat_record_metrics_cleanup + err=3D1 + return + fi + if ! perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_= cycle" + then + echo "stat record metrics test [Failed missing default metric in perf = script]" + stat_record_metrics_cleanup + err=3D1 + return + fi + stat_record_metrics_cleanup + echo "stat record metrics test [Success]" +} + test_stat_repeat_weak_groups() { echo "stat repeat weak groups test" if ! perf stat -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cyc= les,cycles,cycles}' \ @@ -601,6 +733,7 @@ test_null_stat test_offline_cpu_stat test_stat_record_report test_stat_record_script +test_stat_record_metrics test_stat_repeat_weak_groups test_topdown_groups test_topdown_weak_groups --=20 2.56.1.750.gf70dc682cf-goog