From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF6E3EE49AF for ; Wed, 23 Aug 2023 08:11:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233666AbjHWIL4 (ORCPT ); Wed, 23 Aug 2023 04:11:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233699AbjHWIJN (ORCPT ); Wed, 23 Aug 2023 04:09:13 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE18EE71 for ; Wed, 23 Aug 2023 01:08:34 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58fbfcb8d90so47611797b3.1 for ; Wed, 23 Aug 2023 01:08:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778114; x=1693382914; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=R5WZqRzaVJvIJMjlwpO1R+wpxZBFSIFOikqyfQZH1kE=; b=vjnAbFlXmxVBv6vhTv/dewJWBXmjwm2OZGZhVUyI+WT5WjEmpIiHXIAnBHZ/9vLEL3 SEf147geT7ig/Nw+0gJeZpKAj6t+oojfrPtsbCVnIiSr+O3Fl3fChvDEfeWUidpZb/RJ stA+nOYj81ZN9/kK0gtg3S8DuPuikRuZyQjIzCDLtMDUz9y6+GktwhMjBQlLDgw853+a xWJoQvT7cSi2BgIA4sFXTxYkYlVkapVypIdwQ863mieqdXjiJ1beJ+FJNUBDIMtjJyz2 8ZFX98FZFa0tk/RH4KDq3Q0inG2o8tI0noe7LJM4t2eoFMSugbojcNsxnvk2Tnjh+cb0 GBiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778114; x=1693382914; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R5WZqRzaVJvIJMjlwpO1R+wpxZBFSIFOikqyfQZH1kE=; b=TU/mU0wG0rNV3R59pK7NnWBOsWV75ZrXG3ndfwTTL2Og1BUyQCXDH3Zh2QM3dXPAOO Mp+zthdCdqCoxVkHRv7n+nFVvdQrccbCVlrkprlFSplLkOggUE9nM4W2fG5OP6bfVMDZ 6mXuctQqykgwVuMlpzX6apUQm0/IGS0KxDR8OhO7iQvhNX+qJb3bH6NUXGW3SeuTKKKk gPUDp2Wcuek/usOmWkDUrN5Q/EtKLSYfUKTKhNkpgK9DnEXVoWsO8UqZoOADnZnvlSor V8MG1A6MSM61/MUj6wBdCzITJbB+iQrk8prJuWwPsj+MsK8+1Fj65Dyx4fRocj7P5AuR ce9w== X-Gm-Message-State: AOJu0YwKkErKMa19/MHpgEa2cTS9S1EQsXYoi0wl1BGUJ4weSl9gNNFC qVtt+Fnu6hRDDejg8a1zBAXR2ltcZ2// X-Google-Smtp-Source: AGHT+IFabHEBo1r4vNQ9MCETBVuXu18lGMDuZDFKBOYUQ77bnKyfuRUJ5IHUoxWgEiaxyrvuDZkQjPMRxYpO X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:ad08:0:b0:58c:a9b4:a64c with SMTP id l8-20020a81ad08000000b0058ca9b4a64cmr147675ywh.1.1692778114142; Wed, 23 Aug 2023 01:08:34 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:04 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-2-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 01/25] perf script ibs: Remove unused include From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Done to reduce dependencies on pmu-events.h. Signed-off-by: Ian Rogers --- tools/perf/util/amd-sample-raw.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/perf/util/amd-sample-raw.c b/tools/perf/util/amd-sample-= raw.c index 6a6ddba76c75..9d0ce88e90e4 100644 --- a/tools/perf/util/amd-sample-raw.c +++ b/tools/perf/util/amd-sample-raw.c @@ -15,7 +15,6 @@ #include "session.h" #include "evlist.h" #include "sample-raw.h" -#include "pmu-events/pmu-events.h" #include "util/sample.h" =20 static u32 cpu_family, cpu_model, ibs_fetch_type, ibs_op_type; --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37FE6EE49A0 for ; Wed, 23 Aug 2023 08:13:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233698AbjHWIM0 (ORCPT ); Wed, 23 Aug 2023 04:12:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233770AbjHWIJT (ORCPT ); Wed, 23 Aug 2023 04:09:19 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81B57E74 for ; Wed, 23 Aug 2023 01:08:37 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58e4d2b7d16so71990177b3.0 for ; Wed, 23 Aug 2023 01:08:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778117; x=1693382917; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=YVPNIKRWrRX85pZfkeIbGqGN+aZoz9nclv8JB/Dx+DY=; b=2maq36UDs0VaiEhSZa+HOihekElrL31Y7dnJsQtE8bZUDmRBFN1lR+rsLege7SaQtM RC+t3k9R4dvAeanDR/0eqeLUy6eDraJwzyW3yUHNYpbCCTMH2eT9TDmEWUFZ84/MWwX1 zxFZvceDTzy0qiEKhDlYcDOepm3nzaCNoJ0OuHLlUkGbMiywB8oGzvw/kpFoak+otjCB Bvep9JpQO5Fx9/PtTYdv/CM+sKZ9PFJD4CBCQFBuHIBxTkhj4kZP9ZX970g49ZXXX5x5 rsI1VPoyeDPgF5Un3KGeDxnrvMZFa5KESm5+vHquELPRCYpdNy2zsdpTQcKGzLRX/7Ge 6oLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778117; x=1693382917; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YVPNIKRWrRX85pZfkeIbGqGN+aZoz9nclv8JB/Dx+DY=; b=XiivqRshKb6rfK+4Wm6MXm/JRRhd+d25kohdI0o6Zy2mYMPJ1WWuR/gs2GehIGM1td cfxbVuRv6XkH+9wFDmous1sIgQAbH6Kx5q0VhXFiQThkAFM+AsGsnL/pSxsfve86orFO NaDE4wF73rmNjT729QtJirmIw6/PRBIHlrP9tLpZmRbDanH9qC1zvC4yWmtreR62nu// sWxuWU1dN7uJOu1h6RoNExnV+UQI8nPEfUdweZZMSY+iJovgb6oOhybRq2rdPDW7Q0oS S7L4kS2B1i33bSZlZXskxHqgrFwPA+lbs1hAFq1Gg/A7aqlJnzsyze7tIUuf+qDEy7pU h/cw== X-Gm-Message-State: AOJu0YzuVEfaqO3yHQfryKYy7KmPMMVa2a9bdo5cB45C560rNklEYNGk 6NdtB8SSuViPh944mjz72wExvVGSiywQ X-Google-Smtp-Source: AGHT+IGNtc+vuUpoFkBQPAmSbNQ4ZcXCetCAAcNdD4zTkg2mp9pmA9kBpq8P8G/UaaTc8ofzxTgEMsGtFGHK X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:691:b0:d4e:e2ae:e0c8 with SMTP id i17-20020a056902069100b00d4ee2aee0c8mr146979ybt.1.1692778116848; Wed, 23 Aug 2023 01:08:36 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:05 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-3-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 02/25] perf pmu: Avoid a path name copy From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than read a base path and append into a 2nd path, read the base path directly into output buffer and append to that. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index a7f05e4dda97..7683c6749d66 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1756,17 +1756,19 @@ int perf_pmu__event_source_devices_fd(void) * then pathname will be filled with * "/sys/bus/event_source/devices/cs_etm/format" * - * Return 0 if the sysfs mountpoint couldn't be found or if no - * characters were written. + * Return 0 if the sysfs mountpoint couldn't be found, if no characters we= re + * written or if the buffer size is exceeded. */ int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename) { - char base_path[PATH_MAX]; + size_t len; =20 - if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path= ))) + len =3D perf_pmu__event_source_devices_scnprintf(buf, size); + if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >=3D size) return 0; - return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); + + return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename); } =20 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags) --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EB9EEE49AF for ; Wed, 23 Aug 2023 08:13:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234056AbjHWINu (ORCPT ); Wed, 23 Aug 2023 04:13:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233783AbjHWIJX (ORCPT ); Wed, 23 Aug 2023 04:09:23 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA06710C1 for ; Wed, 23 Aug 2023 01:08:40 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58cb845f2f2so72838757b3.1 for ; Wed, 23 Aug 2023 01:08:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778119; x=1693382919; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=zbOd4cktnqwY3C7uOx37I8EsWOiY3ojcBmbOEcOxtrg=; b=TVjeVQycUPbwnNb2TI5vTuU5hq4DhJNEjTYI2IlQgwLqdO0PCxXIn80etXfjCr0Gqy rdwS4hbhtA2laZE8pKKUgf/kJFKIIeJw3hbft9tSjDTb0uwa7XoLsoTCDIRTkd/ws75T s1zftdfnQCEfbkM8ssjC0CLnqkw3+HL9h10VbX3wxWp3qC+03Uct6bmSXDHtYt68W97+ vfDrIfUxFdXIDl8LvxYwMz5NxFe2Qwu3QFTLH2UmCHGlIsUhsO3302gdoqMCVq2dECDZ dI8TUMAN3/XgCynHdQU8HN344ydOkFVTKj5ZXaZhEQUsU/WSg551gtpZa4nKIAxkSCU0 6DKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778119; x=1693382919; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zbOd4cktnqwY3C7uOx37I8EsWOiY3ojcBmbOEcOxtrg=; b=j44oL4Q/Qjg8kV3RHAAG0rfn+KywSsFdQo9Ebh9F9cTpiAU7brSErXhSsLYYZaiuqU xcCxHyEv7gNocXk10JLA6xrsXXVR3O6lj2dgcWgSr60zq//mgTWUJmf7wxKGXnfLUjdu Z9HZ/9QCBvbudrMm4n/fs7McUgntSqldNSOmUxrZevMW+oA4tGFjQDEU52Lx/Bu1p776 uBpC0z91vSbCOXEispI1MYIQNdftdKyFInvKfGC1DurNVYXvkWpdz3a5inmuN4qz+Zwi ofCkt3EORWNZFBZtjDQl5ZFc/EyKR1e+hJPbgDu5GNg+sTTUtxLOjwBJ5wK/5mPR6Szs TZSw== X-Gm-Message-State: AOJu0YyQltjvB3jgI/l/Z0McPs1QXx1C5wsQDscQ/o6+bBA91HNckohE zegt/jErOVlNovVSclyuwJunvXEIgId4 X-Google-Smtp-Source: AGHT+IGXKT84Ue6RywhJHDK9fqe7aLr7VA0C6SOXlI2DXTTcq1jLdX9D5qIv5tM8tI38bPx1mkTkKSHNVhmo X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:e70e:0:b0:d74:6a19:f91b with SMTP id e14-20020a25e70e000000b00d746a19f91bmr126206ybh.3.1692778119481; Wed, 23 Aug 2023 01:08:39 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:06 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-4-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 03/25] perf pmu: Move perf_pmu__set_format to pmu.y From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Avoid having the function in the C and header file, as it is only used locally by pmu.y. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 12 ------------ tools/perf/util/pmu.h | 1 - tools/perf/util/pmu.y | 12 ++++++++++++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 7683c6749d66..40999e1fab8f 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1417,18 +1417,6 @@ int perf_pmu__new_format(struct list_head *list, cha= r *name, return 0; } =20 -void perf_pmu__set_format(unsigned long *bits, long from, long to) -{ - long b; - - if (!to) - to =3D from; - - memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); - for (b =3D from; b <=3D to; b++) - __set_bit(b, bits); -} - void perf_pmu__del_formats(struct list_head *formats) { struct perf_pmu_format *fmt, *tmp; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 7ff925224165..9c9ea40b9c71 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -231,7 +231,6 @@ void perf_pmu_error(struct list_head *list, char *name,= void *scanner, char cons =20 int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); -void perf_pmu__set_format(unsigned long *bits, long from, long to); int perf_pmu__format_parse(int dirfd, struct list_head *head); void perf_pmu__del_formats(struct list_head *formats); bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name); diff --git a/tools/perf/util/pmu.y b/tools/perf/util/pmu.y index 3d46cca3bb94..9bd9e30791ff 100644 --- a/tools/perf/util/pmu.y +++ b/tools/perf/util/pmu.y @@ -21,6 +21,18 @@ do { \ YYABORT; \ } while (0) =20 +static void perf_pmu__set_format(unsigned long *bits, long from, long to) +{ + long b; + + if (!to) + to =3D from; + + memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); + for (b =3D from; b <=3D to; b++) + __set_bit(b, bits); +} + %} =20 %token PP_CONFIG --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FB34EE49AE for ; Wed, 23 Aug 2023 08:13:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233915AbjHWIND (ORCPT ); Wed, 23 Aug 2023 04:13:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233730AbjHWIJ2 (ORCPT ); Wed, 23 Aug 2023 04:09:28 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB16010C7 for ; Wed, 23 Aug 2023 01:08:42 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58f9db8bc1dso67671387b3.3 for ; Wed, 23 Aug 2023 01:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778122; x=1693382922; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=sPnFmXeyFkpH92acmVYaB7s7M5HAfFjCQ+0LeXyrC8o=; b=erD6se6wWYNxMRNnxpJKT+CkcnlF9rW5mxGvW3Hrljaky28uFZPiEaO6MslqXK5AOc SVMP3UixpegWhIQIlqbDZs2YOqCJmckqAwLVUWg80OWPflbvvijJeU2tInQGtCCprqrM nF97qa8HGpZjowMwWZI3ydfAH9IJ+0c9epGVA7OlgMnu8HvdPBv1Xc/Klp3ewjXGjZZy Kzc4gVOmYwrJ27ycdR1G0TBREz7yaFJcBjeVj+jRRU14tc9ThkBcmuzv8Pj8IitmmBv8 vq2e0gsecECUiHPr+/FNa6bvqpE55N/yDk8JqlvCndNnbUWZSg3I0SCoy0WhixbhhDI2 YzPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778122; x=1693382922; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sPnFmXeyFkpH92acmVYaB7s7M5HAfFjCQ+0LeXyrC8o=; b=kLDHHHlCtSgPZrJZLhzTKvfEQMqiH6JTvF/JIaY+xsbjN8FM+OLIiMKJZKcE2sk086 kwXT/3LKnvAG92NcEl3inOfJoxnwWl9x7bbCoFRhCmFD8NEkCw0WpZ0ZXUK2ZwF5MM4/ 1WbUJPFfNXFHN3WuScot/E5BovmHIbGGLnt7NMQ33JpHCk/Vmk9/Q64ZGS+abpZKK+LC 0FhWxoBFqv+vpKTKSBseH5TarIGOcKCPTskrZ55CXnEeJvuNgR/4oKuCVDivvv29XR1p 0yst9YUBgxqvQAMgWTsJy1/IE7kM14H38uWiXC0qNFTkE01gme/wQBKar239eHIc0GYr k2xw== X-Gm-Message-State: AOJu0YzZhucUhekzG+z5dTdO2bVYZ4fR2Jd1Z0nfwW1s8ctdxa39STHD 1zjaYAZnIQ+Yg4BYEoN8tV83ZiXNnYS8 X-Google-Smtp-Source: AGHT+IFtJ3nG1pLmJ6VyeysdGuexhwYFJCPbmdMqMcV4rPcyeYllKmisw8+apKwTOg7hrLiSw+0oVAHvOB1/ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:690c:368b:b0:589:dbc8:3d11 with SMTP id fu11-20020a05690c368b00b00589dbc83d11mr169092ywb.9.1692778121920; Wed, 23 Aug 2023 01:08:41 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:07 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-5-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 04/25] perf pmu: Reduce scope of perf_pmu_error From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move declaration from header file to pmu.y and make static. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.h | 1 - tools/perf/util/pmu.y | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 9c9ea40b9c71..5394c85d20b9 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -227,7 +227,6 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct = list_head *head_terms, struct perf_pmu_info *info); struct list_head *perf_pmu__alias(struct perf_pmu *pmu, struct list_head *head_terms); -void perf_pmu_error(struct list_head *list, char *name, void *scanner, cha= r const *msg); =20 int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); diff --git a/tools/perf/util/pmu.y b/tools/perf/util/pmu.y index 9bd9e30791ff..d861a5bfa3bd 100644 --- a/tools/perf/util/pmu.y +++ b/tools/perf/util/pmu.y @@ -21,6 +21,8 @@ do { \ YYABORT; \ } while (0) =20 +static void perf_pmu_error(struct list_head *list, char *name, void *scann= er, char const *msg); + static void perf_pmu__set_format(unsigned long *bits, long from, long to) { long b; @@ -93,7 +95,7 @@ PP_VALUE =20 %% =20 -void perf_pmu_error(struct list_head *list __maybe_unused, +static void perf_pmu_error(struct list_head *list __maybe_unused, char *name __maybe_unused, void *scanner __maybe_unused, char const *msg __maybe_unused) --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93BC0EE49AE for ; Wed, 23 Aug 2023 08:14:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232466AbjHWIOT (ORCPT ); Wed, 23 Aug 2023 04:14:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233793AbjHWIJj (ORCPT ); Wed, 23 Aug 2023 04:09:39 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39C3D10D4 for ; Wed, 23 Aug 2023 01:08:45 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58daaa2ba65so71818157b3.1 for ; Wed, 23 Aug 2023 01:08:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778124; x=1693382924; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=jym/Q9Cr+MhizGwXbSqum8wmQGZbYfoo8L/RYHAVGYI=; b=p6rxfOIeIODcysFAWsHNOWrLMG4rt15KirX2sKKna3VSQWIqC6Bw1i7Ag6AUFqxDks YKs24ByI6lzYE0E+S0P4P/I7eOhR/Q4eU/rW3hK5r36OyRJW5T768FUpMlTIy+/aRbEA tNmnHqE7S8TlEolvBoBjILqvctFTM7xxNzlWMUz9j5OQW+8nyaFP4zV78bwhdmWjQqdj m2rs7x2mqAK8NxBM7MkBBmCvCmpgD2o1HEMk3rgKqovnMOeSWp82+hwbYE5DLqMnQoaR lXAMHrYuDnUHae1YabhaNOuyo14wKoZC46gEn5u5YtdBwVIJGYGgMJa8uZ4i1tAYhY8E +P8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778124; x=1693382924; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jym/Q9Cr+MhizGwXbSqum8wmQGZbYfoo8L/RYHAVGYI=; b=kRTF5zgW0/riGvZ1lwDAecA1gN0I8sOrcIB2CFZjM1e8KdAfQo92TC5yXrS43XBj5I kMEDaHlqO++My0Ho4kXB32JjUNmYYssLW/n6IdMfVnoabHjofBUSQ2s7fxAICd5ieOZG JQaLYm34DpW/wOVgHTXC1BvAibndJln0n0LzuzcyCZuf9sM0kLthiHSavoIT6kq947Qz Ao1go1quauUbCzv1lUnVM1hMA8YpDdQNf/Oyve7FoFmnn+zKFfP1aGHBCZBBQkuLlFIW 90udVEQ306Yn2AIIXEMvQGl3Zr3Poun2oe+NN9UG4es8ShKpFcyeJ6PUC7Zcfe8e+POD R3xg== X-Gm-Message-State: AOJu0YwXLvo248mqwoz4JNF7dsqzcfCfI6Ol4o5W9Lghi1WOeAE3jj9A uSlwGCjkGO/wjNdD/pIgn0lqwLyFI7p/ X-Google-Smtp-Source: AGHT+IEVWyiF5BqffF0E5MhAlyi3ZQbkFU6BCWy7VFF+QeimKO9j+BK7Y5lEsP1+QOqf5PjAnIr7QUwHgF+Z X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:ae55:0:b0:589:a3d3:2f60 with SMTP id g21-20020a81ae55000000b00589a3d32f60mr153110ywk.8.1692778124461; Wed, 23 Aug 2023 01:08:44 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:08 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-6-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 05/25] perf pmu: Avoid passing format list to perf_pmu__config_terms From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Abstract the format list better, hiding it in the PMU, by changing perf_pmu__config_terms the PMU rather than the format list in the PMU. Change the PMU test to pass a dummy PMU for this purpose. Changing the test allows perf_pmu__del_formats to become static. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/intel-pt.c | 30 ++++-------- tools/perf/tests/pmu.c | 76 ++++++++++++++++------------- tools/perf/util/pmu.c | 19 +++----- tools/perf/util/pmu.h | 3 +- 4 files changed, 61 insertions(+), 67 deletions(-) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index 74b70fd379df..0da76f848cbc 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -60,8 +60,7 @@ struct intel_pt_recording { size_t priv_size; }; =20 -static int intel_pt_parse_terms_with_default(const char *pmu_name, - struct list_head *formats, +static int intel_pt_parse_terms_with_default(struct perf_pmu *pmu, const char *str, u64 *config) { @@ -80,8 +79,7 @@ static int intel_pt_parse_terms_with_default(const char *= pmu_name, goto out_free; =20 attr.config =3D *config; - err =3D perf_pmu__config_terms(pmu_name, formats, &attr, terms, true, - NULL); + err =3D perf_pmu__config_terms(pmu, &attr, terms, /*zero=3D*/true, /*err= =3D*/NULL); if (err) goto out_free; =20 @@ -91,12 +89,10 @@ static int intel_pt_parse_terms_with_default(const char= *pmu_name, return err; } =20 -static int intel_pt_parse_terms(const char *pmu_name, struct list_head *fo= rmats, - const char *str, u64 *config) +static int intel_pt_parse_terms(struct perf_pmu *pmu, const char *str, u64= *config) { *config =3D 0; - return intel_pt_parse_terms_with_default(pmu_name, formats, str, - config); + return intel_pt_parse_terms_with_default(pmu, str, config); } =20 static u64 intel_pt_masked_bits(u64 mask, u64 bits) @@ -236,8 +232,7 @@ static u64 intel_pt_default_config(struct perf_pmu *int= el_pt_pmu) =20 pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf); =20 - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, buf, - &config); + intel_pt_parse_terms(intel_pt_pmu, buf, &config); =20 close(dirfd); return config; @@ -348,16 +343,12 @@ static int intel_pt_info_fill(struct auxtrace_record = *itr, if (priv_size !=3D ptr->priv_size) return -EINVAL; =20 - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, - "tsc", &tsc_bit); - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, - "noretcomp", &noretcomp_bit); - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, - "mtc", &mtc_bit); + intel_pt_parse_terms(intel_pt_pmu, "tsc", &tsc_bit); + intel_pt_parse_terms(intel_pt_pmu, "noretcomp", &noretcomp_bit); + intel_pt_parse_terms(intel_pt_pmu, "mtc", &mtc_bit); mtc_freq_bits =3D perf_pmu__format_bits(&intel_pt_pmu->format, "mtc_period"); - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, - "cyc", &cyc_bit); + intel_pt_parse_terms(intel_pt_pmu, "cyc", &cyc_bit); =20 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d); =20 @@ -781,8 +772,7 @@ static int intel_pt_recording_options(struct auxtrace_r= ecord *itr, intel_pt_evsel->core.attr.aux_watermark =3D aux_watermark; } =20 - intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, - "tsc", &tsc_bit); + intel_pt_parse_terms(intel_pt_pmu, "tsc", &tsc_bit); =20 if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit)) have_timing_info =3D true; diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index a4452639a3d4..a4a43db76012 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -7,6 +7,7 @@ #include #include #include +#include =20 /* Simulated format definitions. */ static struct test_format { @@ -141,48 +142,55 @@ static struct list_head *test_terms_list(void) static int test__pmu(struct test_suite *test __maybe_unused, int subtest _= _maybe_unused) { char dir[PATH_MAX]; - char *format =3D test_format_dir_get(dir, sizeof(dir)); - LIST_HEAD(formats); + char *format; struct list_head *terms =3D test_terms_list(); + struct perf_event_attr attr; + struct perf_pmu *pmu; + int fd; int ret; =20 - if (!format) - return -EINVAL; - - do { - struct perf_event_attr attr; - int fd; - - memset(&attr, 0, sizeof(attr)); - - fd =3D open(format, O_DIRECTORY); - if (fd < 0) { - ret =3D fd; - break; - } - ret =3D perf_pmu__format_parse(fd, &formats); - if (ret) - break; - - ret =3D perf_pmu__config_terms("perf-pmu-test", &formats, &attr, - terms, false, NULL); - if (ret) - break; + pmu =3D zalloc(sizeof(*pmu)); + if (!pmu) + return -ENOMEM; =20 - ret =3D -EINVAL; + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->aliases); + INIT_LIST_HEAD(&pmu->caps); + format =3D test_format_dir_get(dir, sizeof(dir)); + if (!format) { + free(pmu); + return -EINVAL; + } =20 - if (attr.config !=3D 0xc00000000002a823) - break; - if (attr.config1 !=3D 0x8000400000000145) - break; - if (attr.config2 !=3D 0x0400000020041d07) - break; + memset(&attr, 0, sizeof(attr)); =20 - ret =3D 0; - } while (0); + fd =3D open(format, O_DIRECTORY); + if (fd < 0) { + ret =3D fd; + goto out; + } =20 - perf_pmu__del_formats(&formats); + pmu->name =3D strdup("perf-pmu-test"); + ret =3D perf_pmu__format_parse(fd, &pmu->format); + if (ret) + goto out; + + ret =3D perf_pmu__config_terms(pmu, &attr, terms, /*zero=3D*/false, /*err= =3D*/NULL); + if (ret) + goto out; + + ret =3D -EINVAL; + if (attr.config !=3D 0xc00000000002a823) + goto out; + if (attr.config1 !=3D 0x8000400000000145) + goto out; + if (attr.config2 !=3D 0x0400000020041d07) + goto out; + + ret =3D 0; +out: test_format_dir_put(format); + perf_pmu__delete(pmu); return ret; } =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 40999e1fab8f..89573a8eaf0b 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1135,8 +1135,7 @@ static char *pmu_formats_string(struct list_head *for= mats) * Setup one of config[12] attr members based on the * user input data - term parameter. */ -static int pmu_config_term(const char *pmu_name, - struct list_head *formats, +static int pmu_config_term(struct perf_pmu *pmu, struct perf_event_attr *attr, struct parse_events_term *term, struct list_head *head_terms, @@ -1160,15 +1159,15 @@ static int pmu_config_term(const char *pmu_name, if (parse_events__is_hardcoded_term(term)) return 0; =20 - format =3D pmu_find_format(formats, term->config); + format =3D pmu_find_format(&pmu->format, term->config); if (!format) { - char *pmu_term =3D pmu_formats_string(formats); + char *pmu_term =3D pmu_formats_string(&pmu->format); char *unknown_term; char *help_msg; =20 if (asprintf(&unknown_term, "unknown term '%s' for pmu '%s'", - term->config, pmu_name) < 0) + term->config, pmu->name) < 0) unknown_term =3D NULL; help_msg =3D parse_events_formats_error_string(pmu_term); if (err) { @@ -1259,7 +1258,7 @@ static int pmu_config_term(const char *pmu_name, return 0; } =20 -int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, +int perf_pmu__config_terms(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, bool zero, struct parse_events_error *err) @@ -1267,8 +1266,7 @@ int perf_pmu__config_terms(const char *pmu_name, stru= ct list_head *formats, struct parse_events_term *term; =20 list_for_each_entry(term, head_terms, list) { - if (pmu_config_term(pmu_name, formats, attr, term, head_terms, - zero, err)) + if (pmu_config_term(pmu, attr, term, head_terms, zero, err)) return -EINVAL; } =20 @@ -1286,8 +1284,7 @@ int perf_pmu__config(struct perf_pmu *pmu, struct per= f_event_attr *attr, { bool zero =3D !!pmu->default_config; =20 - return perf_pmu__config_terms(pmu->name, &pmu->format, attr, - head_terms, zero, err); + return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); } =20 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, @@ -1417,7 +1414,7 @@ int perf_pmu__new_format(struct list_head *list, char= *name, return 0; } =20 -void perf_pmu__del_formats(struct list_head *formats) +static void perf_pmu__del_formats(struct list_head *formats) { struct perf_pmu_format *fmt, *tmp; =20 diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 5394c85d20b9..eb26c8bc079f 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -217,7 +217,7 @@ void pmu_add_sys_aliases(struct list_head *head, struct= perf_pmu *pmu); int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, struct parse_events_error *error); -int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, +int perf_pmu__config_terms(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, bool zero, struct parse_events_error *error); @@ -231,7 +231,6 @@ struct list_head *perf_pmu__alias(struct perf_pmu *pmu, int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); int perf_pmu__format_parse(int dirfd, struct list_head *head); -void perf_pmu__del_formats(struct list_head *formats); bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name); =20 bool is_pmu_core(const char *name); --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C791EE49A0 for ; Wed, 23 Aug 2023 08:17:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233801AbjHWIRK (ORCPT ); Wed, 23 Aug 2023 04:17:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233808AbjHWIJl (ORCPT ); Wed, 23 Aug 2023 04:09:41 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C08F310DE for ; Wed, 23 Aug 2023 01:08:47 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58fc4291239so48525937b3.0 for ; Wed, 23 Aug 2023 01:08:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778127; x=1693382927; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=myrZI6DclPn0eLWcMv9biHZACVNMEh01ZlQXZypDBfA=; b=R2cp0GlpupvjYWd6I5tT/FuSllBfvZSN8fAwQNUIwunrLFPYs7/ZOH4DuouAyEiBFy Alqi4Rn5BSGp03SWnilJZybv1TgbXEyEkCj+KGNwxprJB3Lhnf1SP6TlTldcEC0br92j ptXg5FMeK+QioamO97JPJ2UWIb7i8pSf7pY2VMkp9owd0SKAGvWyv8OUba1avqn5THpQ pBxgSaGsrRVB6m85IF4PPcdwweop/0zNKyh2AFgH85DPWrwxKq66/3/ugVpwHB/EfmZf qoaTlX7g0Dt6No2MnSa+91aFA93m3nDcWtivH2yQ1JTL5Mtqnx7nuVIfRj6wPcQSzLPR 76PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778127; x=1693382927; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=myrZI6DclPn0eLWcMv9biHZACVNMEh01ZlQXZypDBfA=; b=XuTStV47XXHwt/FY+bXb3G3X1NsCQiNCTnIq/YMidCeW5IqbJIDOotAz0n8QueSn4n oeBoO8BwhiASuQ42jNSAz8Yfi/MAyP82HPltkKLJRLdygN4qjfVuh2LGa55TEji24uVy fK4iwYj6qzkbVA4iHvr/KmcDkCQmMs0p0OSauIQu3XqUngmP8Jv4wIcIi/ciUhDWYjnT jNxsyYMt02dvtoIuwoQ7mJ+gyg7wUGx6TpcFDLeOFEvQo3G9nBOatCq8GT226EVu99OR KtLOWaaL17rPxBs0LJLVQP4KUuLGA640irpdg0JEm3VIRP4YRjGK9Vtl+hTcDMmDANgT Jd2Q== X-Gm-Message-State: AOJu0YyL2OPuwWRIbjEpJKzhTiZfBjwK28wzzgs1gK+5dVqlIgYcKkaA 9s0Ok3UT9i95PQ8oKUHCL9y3dB6BADFP X-Google-Smtp-Source: AGHT+IG2CJ7EsJ8ALY4GJ9uZv36SkkpuYxzpWI3WofNFCIJnmuwyxtmrcdEBDTkhQkINfbl+Ybljr0LhO7eP X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:d809:0:b0:d4c:d744:2ad with SMTP id p9-20020a25d809000000b00d4cd74402admr132919ybg.10.1692778127045; Wed, 23 Aug 2023 01:08:47 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:09 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-7-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 06/25] perf pmu: Avoid passing format list to perf_pmu__format_type From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass the pmu so the format list can be better abstracted and later lazily loaded. Signed-off-by: Ian Rogers --- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 4 ++-- tools/perf/util/pmu.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 00a8ec94f5b2..8ede27089766 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1159,7 +1159,7 @@ static int get_config_chgs(struct perf_pmu *pmu, stru= ct list_head *head_config, list_for_each_entry(term, head_config, list) { switch (term->type_term) { case PARSE_EVENTS__TERM_TYPE_USER: - type =3D perf_pmu__format_type(&pmu->format, term->config); + type =3D perf_pmu__format_type(pmu, term->config); if (type !=3D PERF_PMU_FORMAT_VALUE_CONFIG) continue; bits |=3D perf_pmu__format_bits(&pmu->format, term->config); diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 89573a8eaf0b..96189afe54b0 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1038,9 +1038,9 @@ __u64 perf_pmu__format_bits(struct list_head *formats= , const char *name) return bits; } =20 -int perf_pmu__format_type(struct list_head *formats, const char *name) +int perf_pmu__format_type(struct perf_pmu *pmu, const char *name) { - struct perf_pmu_format *format =3D pmu_find_format(formats, name); + struct perf_pmu_format *format =3D pmu_find_format(&pmu->format, name); =20 if (!format) return -1; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index eb26c8bc079f..1ea78d2fa531 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -222,7 +222,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu, struct list_head *head_terms, bool zero, struct parse_events_error *error); __u64 perf_pmu__format_bits(struct list_head *formats, const char *name); -int perf_pmu__format_type(struct list_head *formats, const char *name); +int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, struct perf_pmu_info *info); struct list_head *perf_pmu__alias(struct perf_pmu *pmu, --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5612EE49A0 for ; Wed, 23 Aug 2023 08:15:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233862AbjHWIPo (ORCPT ); Wed, 23 Aug 2023 04:15:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233817AbjHWIJm (ORCPT ); Wed, 23 Aug 2023 04:09:42 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B3DB10E7 for ; Wed, 23 Aug 2023 01:08:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58fb9323a27so50516607b3.1 for ; Wed, 23 Aug 2023 01:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778129; x=1693382929; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OGsb2LJqZ8wAIRiG/qrlIeUwXu2dut6icoftip7LD1w=; b=t7YWKvAyigj8U88vk9f3zOUQnRe0+b3J74n4CDXzBteW3I6pYJXXR3X4wv9hcSCGPK HdLYN1nTnmFe57Yb0PNC7k4jNp63jzNq8DXg95C6tZsPRVzQ+8UjQF0O3Md3Hy6WFbeX v5ruAEg3gWg6ml7nI2MFuQ8cNC+CWl4ukTlx/uGz7efHl6GCm6X//Rs9ldYMbS+xJ057 7F+4xnc6BAObK0Ud5VIGYQBumNhm76UAe6RWs1pGhPeW2fREfsNwRybKcSZjNRVGtRn5 INunjtywsNnnGa3c4T8LhOdkVicDIFucaS74sMk6Wi6CN6Es+bP+zvplupFsEHzJcCKy EK/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778129; x=1693382929; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OGsb2LJqZ8wAIRiG/qrlIeUwXu2dut6icoftip7LD1w=; b=MG2mWNAmb+Ek3fRzU1DvE2g2uGc/GPvKbPHuRXvZGGvoUZ7t5dvI2KAZLTPkBJXblP BdtMCPwXWQUrY5V/K+oZAODmE4sC2KUtUGQavxEiwBskPoAaYysyOcc4Ik6+dZO4CmPQ UEHLwRfALcuoLMgAgMFW2vzpJP4sQBmXIuZhzGTMSjHTQbKTnlquU/uBUZvvw5RRWuAQ rUpS0dA4T+afVBmhF+aTnziwG6Vnsf8hN7kEGHU9AUtALHDoNIPs5ttcUXaGctneeq1H 5qxNUyWNhZOO+SoApcMO15xSXHwX/UwYd+KnQmE99/kET8spAeAnWsk2YdYmzuAiFN0S WsbA== X-Gm-Message-State: AOJu0YzAME+ZWaQI3FKBmhaB2UWHhTFJ0wSlc4MLjd9IZcnjgWmLNdJG 49HquXJCAI6wLSHf0xW7F4FGwj2ZcAK/ X-Google-Smtp-Source: AGHT+IFXZyc0ktV2s4ZqpFXnx//sQPxDg35HOEfPLbOvB8QvWhBnjMa0znczpxLSbhGfW7nZZyGA5vrNKAeR X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:4318:0:b0:57a:141f:b4f7 with SMTP id q24-20020a814318000000b0057a141fb4f7mr150466ywa.6.1692778129476; Wed, 23 Aug 2023 01:08:49 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:10 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-8-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 07/25] perf pmu: Avoid passing format list to perf_pmu__format_bits From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass the pmu so the format list can be better abstracted and later lazily loaded. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/intel-pt.c | 7 +++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 6 +++--- tools/perf/util/pmu.h | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index 0da76f848cbc..27944c15ac14 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -122,7 +122,7 @@ static int intel_pt_read_config(struct perf_pmu *intel_= pt_pmu, const char *str, =20 *res =3D 0; =20 - mask =3D perf_pmu__format_bits(&intel_pt_pmu->format, str); + mask =3D perf_pmu__format_bits(intel_pt_pmu, str); if (!mask) return -EINVAL; =20 @@ -346,8 +346,7 @@ static int intel_pt_info_fill(struct auxtrace_record *i= tr, intel_pt_parse_terms(intel_pt_pmu, "tsc", &tsc_bit); intel_pt_parse_terms(intel_pt_pmu, "noretcomp", &noretcomp_bit); intel_pt_parse_terms(intel_pt_pmu, "mtc", &mtc_bit); - mtc_freq_bits =3D perf_pmu__format_bits(&intel_pt_pmu->format, - "mtc_period"); + mtc_freq_bits =3D perf_pmu__format_bits(intel_pt_pmu, "mtc_period"); intel_pt_parse_terms(intel_pt_pmu, "cyc", &cyc_bit); =20 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d); @@ -502,7 +501,7 @@ static int intel_pt_val_config_term(struct perf_pmu *in= tel_pt_pmu, int dirfd, =20 valid |=3D 1; =20 - bits =3D perf_pmu__format_bits(&intel_pt_pmu->format, name); + bits =3D perf_pmu__format_bits(intel_pt_pmu, name); =20 config &=3D bits; =20 diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 8ede27089766..7d9d687d9191 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1162,7 +1162,7 @@ static int get_config_chgs(struct perf_pmu *pmu, stru= ct list_head *head_config, type =3D perf_pmu__format_type(pmu, term->config); if (type !=3D PERF_PMU_FORMAT_VALUE_CONFIG) continue; - bits |=3D perf_pmu__format_bits(&pmu->format, term->config); + bits |=3D perf_pmu__format_bits(pmu, term->config); break; case PARSE_EVENTS__TERM_TYPE_CONFIG: bits =3D ~(u64)0; diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 96189afe54b0..1839c3668ec5 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1000,7 +1000,7 @@ void evsel__set_config_if_unset(struct perf_pmu *pmu,= struct evsel *evsel, if (term) user_bits =3D term->val.cfg_chg; =20 - bits =3D perf_pmu__format_bits(&pmu->format, config_name); + bits =3D perf_pmu__format_bits(pmu, config_name); =20 /* Do nothing if the user changed the value */ if (bits & user_bits) @@ -1023,9 +1023,9 @@ pmu_find_format(struct list_head *formats, const char= *name) return NULL; } =20 -__u64 perf_pmu__format_bits(struct list_head *formats, const char *name) +__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name) { - struct perf_pmu_format *format =3D pmu_find_format(formats, name); + struct perf_pmu_format *format =3D pmu_find_format(&pmu->format, name); __u64 bits =3D 0; int fbit; =20 diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 1ea78d2fa531..1249fca02ffd 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -221,7 +221,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, bool zero, struct parse_events_error *error); -__u64 perf_pmu__format_bits(struct list_head *formats, const char *name); +__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name); int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, struct perf_pmu_info *info); --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2618EE49AE for ; Wed, 23 Aug 2023 08:16:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233791AbjHWIQ5 (ORCPT ); Wed, 23 Aug 2023 04:16:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233818AbjHWIJm (ORCPT ); Wed, 23 Aug 2023 04:09:42 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF19310F1 for ; Wed, 23 Aug 2023 01:08:52 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7494be34f8so4032048276.2 for ; Wed, 23 Aug 2023 01:08:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778132; x=1693382932; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=sabfrVf+/H49KXlWfGTkb2aVXZBVdOPVhW4XlqabkW4=; b=pUql4HrW8UvddlTCkXzzcPzucwZwUumflJ5ll12QQtykNgLCe5J+pnXppkBNI8LaaI gmLIJ3a1oxi0ea2vnkd8WEFh/91BODVpMOOgXuCEjL/4ENGBNWwu2v9bX8NvrhFBGafA OFneATmpg5HAXJgaWp61lT9Jbu2zRD5KQLQ4V2atvBQn8mVK9F7OFJpkIzaz6X8d1TZv GM+DxTnhfA9yGTKvWBp+R1j4Wmwym34FDVVQlPkelQE2hoNUGP0ddySz2uVNpK0nJdwO c6CjLxk5/OkbRSHX/05CbnzhkFzdkFMHz0gjZZlx0Yo9+fMrKkzvD7M2kEEtBue6pUe/ YRYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778132; x=1693382932; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sabfrVf+/H49KXlWfGTkb2aVXZBVdOPVhW4XlqabkW4=; b=VyRB4kelGxyFR+sJ6Ww/sJt5k+g7dxXPj5Dr3RHmku7IKWOmcS/vgi/UHiNEU4FCak V+mZiAIlI0MXOWzwW8nkxE2WslaxDtTadoyzF5FpuHNT3l4Sulr6OeTfqb7CF+9xQTOO K02uqJEDBsOg0MaHIIOak1I44AloPQ0ETuadXZ3ydOuBHPuU5DjE4PTJKUDo7nAKxFiP Nw47SoaC8/tc4n8D6KAYFVFR64L1Vlp2zPsCg5gFfC6qhqMjjgt3teOiX5Qo2OmU8cdZ Gpn3dWy0Fn37wqgXjc3nGTWS1NUlIsQMaUi6TSL+GtVMDZtIgyutqLtvg4ZuCQgPuluj m65w== X-Gm-Message-State: AOJu0YxIhNpQpvjW0QopGjLAHit6x0+HRtqlocaOoQo86mm6+UGO4esJ PxVVnRo0zdXEkKxaNu+mtk7gMV3J3Pym X-Google-Smtp-Source: AGHT+IH9BbX5Hto4GKSE/bOYFzPSUuVeAAElLyjslxNr7MwWBKqZKC2rpWH2KROancXGtAFBu8a3dkf8E/YE X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:1141:b0:d58:6cea:84de with SMTP id p1-20020a056902114100b00d586cea84demr204699ybu.11.1692778132010; Wed, 23 Aug 2023 01:08:52 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:11 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-9-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 08/25] perf pmu: Pass PMU rather than aliases and format From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass the pmu so the aliases and format list can be better abstracted and later lazily loaded. Signed-off-by: Ian Rogers --- tools/perf/tests/pmu-events.c | 47 ++++++++++-------------- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 69 +++++++++++++++++++---------------- tools/perf/util/pmu.h | 9 ++--- 4 files changed, 62 insertions(+), 65 deletions(-) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 64383fc34ef1..05d6e6e21c6f 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -496,26 +496,13 @@ static int test__pmu_event_table(struct test_suite *t= est __maybe_unused, return 0; } =20 -static struct perf_pmu_alias *find_alias(const char *test_event, struct li= st_head *aliases) -{ - struct perf_pmu_alias *alias; - - list_for_each_entry(alias, aliases, list) - if (!strcmp(test_event, alias->name)) - return alias; - - return NULL; -} - /* Verify aliases are as expected */ static int __test_core_pmu_event_aliases(char *pmu_name, int *count) { struct perf_pmu_test_event const **test_event_table; struct perf_pmu *pmu; - LIST_HEAD(aliases); int res =3D 0; const struct pmu_events_table *table =3D find_core_events_table("testarch= ", "testcpu"); - struct perf_pmu_alias *a, *tmp; =20 if (!table) return -1; @@ -526,14 +513,18 @@ static int __test_core_pmu_event_aliases(char *pmu_na= me, int *count) if (!pmu) return -1; =20 - pmu->name =3D pmu_name; + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->aliases); + INIT_LIST_HEAD(&pmu->caps); + INIT_LIST_HEAD(&pmu->list); + pmu->name =3D strdup(pmu_name); =20 - pmu_add_cpu_aliases_table(&aliases, pmu, table); + pmu_add_cpu_aliases_table(pmu, table); =20 for (; *test_event_table; test_event_table++) { struct perf_pmu_test_event const *test_event =3D *test_event_table; struct pmu_event const *event =3D &test_event->event; - struct perf_pmu_alias *alias =3D find_alias(event->name, &aliases); + struct perf_pmu_alias *alias =3D perf_pmu__find_alias(pmu, event->name); =20 if (!alias) { pr_debug("testing aliases core PMU %s: no alias, alias_table->name=3D%s= \n", @@ -551,12 +542,8 @@ static int __test_core_pmu_event_aliases(char *pmu_nam= e, int *count) pr_debug2("testing aliases core PMU %s: matched event %s\n", pmu_name, alias->name); } + perf_pmu__delete(pmu); =20 - list_for_each_entry_safe(a, tmp, &aliases, list) { - list_del(&a->list); - perf_pmu_free_alias(a); - } - free(pmu); return res; } =20 @@ -568,17 +555,16 @@ static int __test_uncore_pmu_event_aliases(struct per= f_pmu_test_pmu *test_pmu) const char *pmu_name =3D pmu->name; struct perf_pmu_alias *a, *tmp, *alias; const struct pmu_events_table *events_table; - LIST_HEAD(aliases); int res =3D 0; =20 events_table =3D find_core_events_table("testarch", "testcpu"); if (!events_table) return -1; - pmu_add_cpu_aliases_table(&aliases, pmu, events_table); - pmu_add_sys_aliases(&aliases, pmu); + pmu_add_cpu_aliases_table(pmu, events_table); + pmu_add_sys_aliases(pmu); =20 /* Count how many aliases we generated */ - list_for_each_entry(alias, &aliases, list) + list_for_each_entry(alias, &pmu->aliases, list) alias_count++; =20 /* Count how many aliases we expect from the known table */ @@ -592,7 +578,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) goto out; } =20 - list_for_each_entry(alias, &aliases, list) { + list_for_each_entry(alias, &pmu->aliases, list) { bool matched =3D false; =20 for (table =3D &test_pmu->aliases[0]; *table; table++) { @@ -625,7 +611,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) } =20 out: - list_for_each_entry_safe(a, tmp, &aliases, list) { + list_for_each_entry_safe(a, tmp, &pmu->aliases, list) { list_del(&a->list); perf_pmu_free_alias(a); } @@ -732,8 +718,13 @@ static int test__aliases(struct test_suite *test __may= be_unused, } =20 for (i =3D 0; i < ARRAY_SIZE(test_pmus); i++) { - int res =3D __test_uncore_pmu_event_aliases(&test_pmus[i]); + int res; + + INIT_LIST_HEAD(&test_pmus[i].pmu.format); + INIT_LIST_HEAD(&test_pmus[i].pmu.aliases); + INIT_LIST_HEAD(&test_pmus[i].pmu.caps); =20 + res =3D __test_uncore_pmu_event_aliases(&test_pmus[i]); if (res) return res; } diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index a4a43db76012..2c1c349a42e2 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_un= used, int subtest __maybe } =20 pmu->name =3D strdup("perf-pmu-test"); - ret =3D perf_pmu__format_parse(fd, &pmu->format); + ret =3D perf_pmu__format_parse(pmu, fd); if (ret) goto out; =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 1839c3668ec5..42f3249994ab 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -58,7 +58,7 @@ struct perf_pmu_format { * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse(int dirfd, struct list_head *head) +int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd) { struct dirent *evt_ent; DIR *format_dir; @@ -96,7 +96,7 @@ int perf_pmu__format_parse(int dirfd, struct list_head *h= ead) } =20 perf_pmu_set_in(file, scanner); - ret =3D perf_pmu_parse(head, name, scanner); + ret =3D perf_pmu_parse(&pmu->format, name, scanner); perf_pmu_lex_destroy(scanner); fclose(file); } @@ -110,7 +110,7 @@ int perf_pmu__format_parse(int dirfd, struct list_head = *head) * located at: * /sys/bus/event_source/devices//format as sysfs group attributes. */ -static int pmu_format(int dirfd, const char *name, struct list_head *forma= t) +static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name) { int fd; =20 @@ -119,7 +119,7 @@ static int pmu_format(int dirfd, const char *name, stru= ct list_head *format) return 0; =20 /* it'll close the fd */ - if (perf_pmu__format_parse(fd, format)) + if (perf_pmu__format_parse(pmu, fd)) return -1; =20 return 0; @@ -508,7 +508,7 @@ static int pmu_aliases_parse(int dirfd, struct list_hea= d *head) * Reading the pmu event aliases definition, which should be located at: * /sys/bus/event_source/devices//events as sysfs group attributes. */ -static int pmu_aliases(int dirfd, const char *name, struct list_head *head) +static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name) { int fd; =20 @@ -517,7 +517,7 @@ static int pmu_aliases(int dirfd, const char *name, str= uct list_head *head) return 0; =20 /* it'll close the fd */ - if (pmu_aliases_parse(fd, head)) + if (pmu_aliases_parse(fd, &pmu->aliases)) return -1; =20 return 0; @@ -770,11 +770,10 @@ static int pmu_add_cpu_aliases_map_callback(const str= uct pmu_event *pe, * From the pmu_events_table, find the events that correspond to the given * PMU and add them to the list 'head'. */ -void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pm= u, - const struct pmu_events_table *table) +void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_even= ts_table *table) { struct pmu_add_cpu_aliases_map_data data =3D { - .head =3D head, + .head =3D &pmu->aliases, .default_pmu_name =3D perf_pmus__default_pmu_name(), .pmu =3D pmu, }; @@ -783,7 +782,7 @@ void pmu_add_cpu_aliases_table(struct list_head *head, = struct perf_pmu *pmu, free(data.default_pmu_name); } =20 -static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *p= mu) +static void pmu_add_cpu_aliases(struct perf_pmu *pmu) { const struct pmu_events_table *table; =20 @@ -791,7 +790,7 @@ static void pmu_add_cpu_aliases(struct list_head *head,= struct perf_pmu *pmu) if (!table) return; =20 - pmu_add_cpu_aliases_table(head, pmu, table); + pmu_add_cpu_aliases_table(pmu, table); } =20 struct pmu_sys_event_iter_data { @@ -821,10 +820,10 @@ static int pmu_add_sys_aliases_iter_fn(const struct p= mu_event *pe, return 0; } =20 -void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) +void pmu_add_sys_aliases(struct perf_pmu *pmu) { struct pmu_sys_event_iter_data idata =3D { - .head =3D head, + .head =3D &pmu->aliases, .pmu =3D pmu, }; =20 @@ -863,30 +862,33 @@ static int pmu_max_precise(int dirfd, struct perf_pmu= *pmu) struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const= char *lookup_name) { struct perf_pmu *pmu; - LIST_HEAD(format); - LIST_HEAD(aliases); __u32 type; char *name =3D pmu_find_real_name(lookup_name); char *alias_name; =20 + pmu =3D zalloc(sizeof(*pmu)); + if (!pmu) + return NULL; + + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->aliases); + INIT_LIST_HEAD(&pmu->caps); /* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right * now. */ - if (pmu_format(dirfd, name, &format)) + if (pmu_format(pmu, dirfd, name)) { + free(pmu); return NULL; - + } /* * Check the aliases first to avoid unnecessary work. */ - if (pmu_aliases(dirfd, name, &aliases)) - return NULL; - - pmu =3D zalloc(sizeof(*pmu)); - if (!pmu) + if (pmu_aliases(pmu, dirfd, name)) { + free(pmu); return NULL; - + } pmu->is_core =3D is_pmu_core(name); pmu->cpus =3D pmu_cpumask(dirfd, name, pmu->is_core); pmu->name =3D strdup(name); @@ -909,14 +911,8 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pm= us, int dirfd, const char if (pmu->is_uncore) pmu->id =3D pmu_id(name); pmu->max_precise =3D pmu_max_precise(dirfd, pmu); - pmu_add_cpu_aliases(&aliases, pmu); - pmu_add_sys_aliases(&aliases, pmu); - - INIT_LIST_HEAD(&pmu->format); - INIT_LIST_HEAD(&pmu->aliases); - INIT_LIST_HEAD(&pmu->caps); - list_splice(&format, &pmu->format); - list_splice(&aliases, &pmu->aliases); + pmu_add_cpu_aliases(pmu); + pmu_add_sys_aliases(pmu); list_add_tail(&pmu->list, pmus); =20 pmu->default_config =3D perf_pmu__get_default_config(pmu); @@ -1397,6 +1393,17 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, stru= ct list_head *head_terms, return 0; } =20 +struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const ch= ar *event) +{ + struct perf_pmu_alias *alias; + + list_for_each_entry(alias, &pmu->aliases, list) + if (!strcmp(event, alias->name)) + return alias; + + return NULL; +} + int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits) { diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 1249fca02ffd..c4268053c979 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -213,7 +213,7 @@ struct perf_pmu_alias { char *pmu_name; }; =20 -void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu); +void pmu_add_sys_aliases(struct perf_pmu *pmu); int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, struct parse_events_error *error); @@ -225,12 +225,11 @@ __u64 perf_pmu__format_bits(struct perf_pmu *pmu, con= st char *name); int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, struct perf_pmu_info *info); -struct list_head *perf_pmu__alias(struct perf_pmu *pmu, - struct list_head *head_terms); +struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const ch= ar *event); =20 int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); -int perf_pmu__format_parse(int dirfd, struct list_head *head); +int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd); bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name); =20 bool is_pmu_core(const char *name); @@ -255,7 +254,7 @@ bool perf_pmu__file_exists(struct perf_pmu *pmu, const = char *name); int perf_pmu__test(void); =20 struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu); -void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pm= u, +void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table); =20 char *perf_pmu__getcpuid(struct perf_pmu *pmu); --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21FA2EE49A0 for ; Wed, 23 Aug 2023 08:15:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233865AbjHWIPc (ORCPT ); Wed, 23 Aug 2023 04:15:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233831AbjHWIJn (ORCPT ); Wed, 23 Aug 2023 04:09:43 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9849C10F9 for ; Wed, 23 Aug 2023 01:08:55 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58d37b541a2so75445887b3.2 for ; Wed, 23 Aug 2023 01:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778134; x=1693382934; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ktZfPGkZyqox+6WbxffaWE9Heu4siEOaYIAlfm46eow=; b=OOR3mHS2J4kz/WgP1oDG6r9Kv4F00+J1X+t5Y4S5w983n3wkEAR5Fh2bR6voWxAK7M PpEM5N+86CZVEDbqTwnp0YjBqH9OnBwMpUc19TFvj+kf7/tQzdMXq/YjE8cZguTtIt15 5r2TKKE65iqHlcAFnTKta0Wshwz7m+g06dDd0uJH4+POJ4Gewb5LZD93LhELLrPvlhwE ssXxqdTWPVjXfntJ4UMg6AYWEddq49fx3mKbjuaOU4nleX9x5AfvHI6RIOAcMu+W6+E/ /MagC+oT0KbNGjH90Rj6TRS3qA2R0BgAAdH629XCpXrPiYTqXnZchMkPOO1Z9A1GZgKC pS1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778134; x=1693382934; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ktZfPGkZyqox+6WbxffaWE9Heu4siEOaYIAlfm46eow=; b=Bb6SuMky/0rbyZ46vSMpZtLNseNFJzXHR2OffOZB024zhR7f2YiLe2kzyENzdGtwBv PXfkTQyncsppLCMgp6mliMFNqMdP2IiNs5HMssWpvKqbnngEENGVPqzJn2CC23qi6zH0 br8F5xLQGe2lXyr3dWsH0FuBV3g07CmgGPUFnkIY3etX/l8nU8iUJxM6wQUW8ZwSFxBE Qp0EGe/tXax8GN+aWt6EYG6fRzY59wT7ufdSpsxcHZQqhmSb6/ECowIYrFoSBgTYRKiy mBK2dbfZi6Lz3WNuXaQVF24nh+rLGSpYQ/TqX5t4Iys/MnEq4nKNmYXhUeXYaVbl0yNq CoZw== X-Gm-Message-State: AOJu0YyZHcJeKKmBSeLtY3B2a5AWvuO34t75knEZwJvz/ZTGSric9cHO 4Qw3siShBx3WN6vLOrJi4pFOmzpWGEBV X-Google-Smtp-Source: AGHT+IGc4hIwyrjGuDTBAnErsZNRaT3kWDvGsISOZV0nkCiN/Gq/xCp+zN3dvbc3JqC1Je7YVBuzZWDnkYFe X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:290:b0:d48:c04:f256 with SMTP id v16-20020a056902029000b00d480c04f256mr137156ybh.11.1692778134435; Wed, 23 Aug 2023 01:08:54 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:12 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-10-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 09/25] perf pmu: Make the loading of formats lazy From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The sysfs format files are loaded eagerly in a PMU. Add a flag so that we create the format but only load the contents when necessary. Reduce the size of the value in struct perf_pmu_format and avoid holes so there is no additional space requirement. For "perf stat -e cycles true" this reduces the number of openat calls from 648 to 573 (about 12%). The benchmark pmu scan speed is improved by roughly 5%. Before: $ perf bench internals pmu-scan Computing performance of sysfs PMU event scan for 100 times Average core PMU scanning took: 1061.100 usec (+- 9.965 usec) Average PMU scanning took: 4725.300 usec (+- 260.599 usec) After: $ perf bench internals pmu-scan Computing performance of sysfs PMU event scan for 100 times Average core PMU scanning took: 989.170 usec (+- 6.873 usec) Average PMU scanning took: 4520.960 usec (+- 251.272 usec) Signed-off-by: Ian Rogers --- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 140 +++++++++++++++++++++++++++-------------- tools/perf/util/pmu.h | 5 +- tools/perf/util/pmu.y | 20 +++--- 4 files changed, 102 insertions(+), 65 deletions(-) diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index 2c1c349a42e2..c204ed1f1a8b 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_un= used, int subtest __maybe } =20 pmu->name =3D strdup("perf-pmu-test"); - ret =3D perf_pmu__format_parse(pmu, fd); + ret =3D perf_pmu__format_parse(pmu, fd, /*eager_load=3D*/true); if (ret) goto out; =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 42f3249994ab..7c3de51bab08 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -40,6 +40,10 @@ struct perf_pmu perf_pmu__fake; * value=3DPERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. */ struct perf_pmu_format { + /** @list: Element on list within struct perf_pmu. */ + struct list_head list; + /** @bits: Which config bits are set by this format value. */ + DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); /** @name: The modifier/file name. */ char *name; /** @@ -47,18 +51,75 @@ struct perf_pmu_format { * are from PERF_PMU_FORMAT_VALUE_CONFIG to * PERF_PMU_FORMAT_VALUE_CONFIG_END. */ - int value; - /** @bits: Which config bits are set by this format value. */ - DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); - /** @list: Element on list within struct perf_pmu. */ - struct list_head list; + u16 value; + /** @loaded: Has the contents been loaded/parsed. */ + bool loaded; }; =20 +static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list= , char *name) +{ + struct perf_pmu_format *format; + + format =3D zalloc(sizeof(*format)); + if (!format) + return NULL; + + format->name =3D strdup(name); + list_add_tail(&format->list, list); + return format; +} + +/* Called at the end of parsing a format. */ +void perf_pmu_format__set_value(void *vformat, int config, unsigned long *= bits) +{ + struct perf_pmu_format *format =3D vformat; + + format->value =3D config; + memcpy(format->bits, bits, sizeof(format->bits)); +} + +static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *= file) +{ + void *scanner; + int ret; + + ret =3D perf_pmu_lex_init(&scanner); + if (ret) + return; + + perf_pmu_set_in(file, scanner); + ret =3D perf_pmu_parse(format, scanner); + perf_pmu_lex_destroy(scanner); + format->loaded =3D true; +} + +static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_fo= rmat *format) +{ + char path[PATH_MAX]; + FILE *file =3D NULL; + + if (format->loaded) + return; + + if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"= )) + return; + + assert(strlen(path) + strlen(format->name) + 2 < sizeof(path)); + strcat(path, "/"); + strcat(path, format->name); + + file =3D fopen(path, "r"); + if (!file) + return; + __perf_pmu_format__load(format, file); + fclose(file); +} + /* * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd) +int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_loa= d) { struct dirent *evt_ent; DIR *format_dir; @@ -68,37 +129,35 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int d= irfd) if (!format_dir) return -EINVAL; =20 - while (!ret && (evt_ent =3D readdir(format_dir))) { + while ((evt_ent =3D readdir(format_dir)) !=3D NULL) { + struct perf_pmu_format *format; char *name =3D evt_ent->d_name; - int fd; - void *scanner; - FILE *file; =20 if (!strcmp(name, ".") || !strcmp(name, "..")) continue; =20 - - ret =3D -EINVAL; - fd =3D openat(dirfd, name, O_RDONLY); - if (fd < 0) - break; - - file =3D fdopen(fd, "r"); - if (!file) { - close(fd); + format =3D perf_pmu__new_format(&pmu->format, name); + if (!format) { + ret =3D -ENOMEM; break; } =20 - ret =3D perf_pmu_lex_init(&scanner); - if (ret) { + if (eager_load) { + FILE *file; + int fd =3D openat(dirfd, name, O_RDONLY); + + if (fd < 0) { + ret =3D -errno; + break; + } + file =3D fdopen(fd, "r"); + if (!file) { + close(fd); + break; + } + __perf_pmu_format__load(format, file); fclose(file); - break; } - - perf_pmu_set_in(file, scanner); - ret =3D perf_pmu_parse(&pmu->format, name, scanner); - perf_pmu_lex_destroy(scanner); - fclose(file); } =20 closedir(format_dir); @@ -119,7 +178,7 @@ static int pmu_format(struct perf_pmu *pmu, int dirfd, = const char *name) return 0; =20 /* it'll close the fd */ - if (perf_pmu__format_parse(pmu, fd)) + if (perf_pmu__format_parse(pmu, fd, /*eager_load=3D*/false)) return -1; =20 return 0; @@ -962,13 +1021,15 @@ void perf_pmu__warn_invalid_formats(struct perf_pmu = *pmu) if (pmu =3D=3D &perf_pmu__fake) return; =20 - list_for_each_entry(format, &pmu->format, list) + list_for_each_entry(format, &pmu->format, list) { + perf_pmu_format__load(pmu, format); if (format->value >=3D PERF_PMU_FORMAT_VALUE_CONFIG_END) { pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config= %d'" "which is not supported by this version of perf!\n", pmu->name, format->name, format->value); return; } + } } =20 bool evsel__is_aux_event(const struct evsel *evsel) @@ -1041,6 +1102,7 @@ int perf_pmu__format_type(struct perf_pmu *pmu, const= char *name) if (!format) return -1; =20 + perf_pmu_format__load(pmu, format); return format->value; } =20 @@ -1177,7 +1239,7 @@ static int pmu_config_term(struct perf_pmu *pmu, free(pmu_term); return -EINVAL; } - + perf_pmu_format__load(pmu, format); switch (format->value) { case PERF_PMU_FORMAT_VALUE_CONFIG: vp =3D &attr->config; @@ -1403,24 +1465,6 @@ struct perf_pmu_alias *perf_pmu__find_alias(struct p= erf_pmu *pmu, const char *ev =20 return NULL; } - -int perf_pmu__new_format(struct list_head *list, char *name, - int config, unsigned long *bits) -{ - struct perf_pmu_format *format; - - format =3D zalloc(sizeof(*format)); - if (!format) - return -ENOMEM; - - format->name =3D strdup(name); - format->value =3D config; - memcpy(format->bits, bits, sizeof(format->bits)); - - list_add_tail(&format->list, list); - return 0; -} - static void perf_pmu__del_formats(struct list_head *formats) { struct perf_pmu_format *fmt, *tmp; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index c4268053c979..675c9b97f7bf 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -227,9 +227,8 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct = list_head *head_terms, struct perf_pmu_info *info); struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const ch= ar *event); =20 -int perf_pmu__new_format(struct list_head *list, char *name, - int config, unsigned long *bits); -int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd); +int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_loa= d); +void perf_pmu_format__set_value(void *format, int config, unsigned long *b= its); bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name); =20 bool is_pmu_core(const char *name); diff --git a/tools/perf/util/pmu.y b/tools/perf/util/pmu.y index d861a5bfa3bd..600c8c158c8e 100644 --- a/tools/perf/util/pmu.y +++ b/tools/perf/util/pmu.y @@ -1,6 +1,5 @@ %define api.pure full -%parse-param {struct list_head *format} -%parse-param {char *name} +%parse-param {void *format} %parse-param {void *scanner} %lex-param {void* scanner} =20 @@ -21,7 +20,7 @@ do { \ YYABORT; \ } while (0) =20 -static void perf_pmu_error(struct list_head *list, char *name, void *scann= er, char const *msg); +static void perf_pmu_error(void *format, void *scanner, const char *msg); =20 static void perf_pmu__set_format(unsigned long *bits, long from, long to) { @@ -59,16 +58,12 @@ format_term format_term: PP_CONFIG ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - PERF_PMU_FORMAT_VALUE_CONFIG, - $3)); + perf_pmu_format__set_value(format, PERF_PMU_FORMAT_VALUE_CONFIG, $3); } | PP_CONFIG PP_VALUE ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - $2, - $4)); + perf_pmu_format__set_value(format, $2, $4); } =20 bits: @@ -95,9 +90,8 @@ PP_VALUE =20 %% =20 -static void perf_pmu_error(struct list_head *list __maybe_unused, - char *name __maybe_unused, - void *scanner __maybe_unused, - char const *msg __maybe_unused) +static void perf_pmu_error(void *format __maybe_unused, + void *scanner __maybe_unused, + const char *msg __maybe_unused) { } --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7AB6EE49A0 for ; Wed, 23 Aug 2023 08:16:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233974AbjHWIQG (ORCPT ); Wed, 23 Aug 2023 04:16:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233683AbjHWIK2 (ORCPT ); Wed, 23 Aug 2023 04:10:28 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF1291703 for ; Wed, 23 Aug 2023 01:08:57 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d749c6d767dso3916295276.2 for ; Wed, 23 Aug 2023 01:08:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778137; x=1693382937; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=rJLqtuSY8Q4JsOIrhkva4qpZ1COUpzJk5YEmlnu3Qe4=; b=LthV0emuXRJJaV0hXHG/OEXD8HqBRIxK5Vatdw4SuBhO7ul6PrXVFBLb7VCj5hfxYH LzVqPUq/WJG2M8E+aameCD+ErBJjNDqwUbhbwmzLc7JRZX1sP6rjh6gaL6S676xah4mv xCD8S1d73H9upQ2W7rFfghd1Np4aRaUuaaMbNO8T2O8Dn68znCYGva0k3I6f8tXW6RKa BkEK7etJAviboyympbitYhLDNo08j2YC4WtZC9eo/bt4qr5Hn6p4qRfpT0K4JSbiO8ZV H2ceRr0Bzrc6PcZIaSnahPK545JzDQbdu07tHBqlY0NuQwqxCU42jB8dqYvN48XVChxY 3Wug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778137; x=1693382937; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rJLqtuSY8Q4JsOIrhkva4qpZ1COUpzJk5YEmlnu3Qe4=; b=VHwdB2fki84hZEbkS/0/ZdUSiQnIhlY4kLnaGS6DCrmL0MCAGrWr7An2UCJ+gL9nC5 mEBQN8v0K/PbXjWbwkjhp1o37WkpU+L7t0wSn+FANAsn6DHumAoQHb8id14M6xMMLj7v /vMuAR1PQZroKidYUaXb1XhUEBRGp4E4UxE3sBC2fRdMsYNDbcRQqoi7cYZhzeC6oe4x feFCPURFB3qGW4E+aZOY0LwjSPzBZzW6ndwaMOLT1PhVW/8I5bhI4jcmZ73eHicqTzn4 IK7x/AaLK8JxItzTcffBotvHNWC4U6QRZd3GDLyja+fQnPdLmL2cT442Kj6M2XeP8mCB 9+5Q== X-Gm-Message-State: AOJu0Yxitout+ee0+CLlGdf4Fx0k3A8KVN5OGKgwzvSG1JmIaSY/hhgG ZQRaOIsx7u69iQ91xOhaOXvBH6FnjrCF X-Google-Smtp-Source: AGHT+IHmLzhXx9Fa4GUjdjoBS4SOnjJQephaFXFqJF/u+jn8YmcxQnmuCxXSlSF/g+QGzs/TzD14WZwnWRzv X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:aea3:0:b0:d77:d91b:f477 with SMTP id b35-20020a25aea3000000b00d77d91bf477mr9631ybj.13.1692778137108; Wed, 23 Aug 2023 01:08:57 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:13 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-11-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 10/25] perf pmu: Abstract alias/event struct From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to be able to lazily compute aliases/events for a PMU, move the struct perf_pmu_alias into pmu.c. Add perf_pmu__find_event and perf_pmu__for_each_event that take a callback that is called for the found event or for each event. The layout of struct pmu and the event/alias list is unchanged but the API is altered so that aliases are no longer directly accessed, allowing for later changes. Signed-off-by: Ian Rogers --- tools/perf/bench/pmu-scan.c | 8 +- tools/perf/tests/pmu-events.c | 101 +++++++-------- tools/perf/util/parse-events.c | 67 ++++------ tools/perf/util/pmu.c | 212 +++++++++++++++++++++++++++--- tools/perf/util/pmu.h | 71 +++------- tools/perf/util/pmus.c | 230 +++++++++++---------------------- 6 files changed, 366 insertions(+), 323 deletions(-) diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c index c7d207f8e13c..9e4d36486f62 100644 --- a/tools/perf/bench/pmu-scan.c +++ b/tools/perf/bench/pmu-scan.c @@ -57,9 +57,7 @@ static int save_result(void) r->is_core =3D pmu->is_core; r->nr_caps =3D pmu->nr_caps; =20 - r->nr_aliases =3D 0; - list_for_each(list, &pmu->aliases) - r->nr_aliases++; + r->nr_aliases =3D perf_pmu__num_events(pmu); =20 r->nr_formats =3D 0; list_for_each(list, &pmu->format) @@ -98,9 +96,7 @@ static int check_result(bool core_only) return -1; } =20 - nr =3D 0; - list_for_each(list, &pmu->aliases) - nr++; + nr =3D perf_pmu__num_events(pmu); if (nr !=3D r->nr_aliases) { pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n", pmu->name, r->nr_aliases, nr); diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 05d6e6e21c6f..dc87e66fb118 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -341,7 +341,7 @@ static int compare_pmu_events(const struct pmu_event *e= 1, const struct pmu_event return 0; } =20 -static int compare_alias_to_test_event(struct perf_pmu_alias *alias, +static int compare_alias_to_test_event(struct pmu_event_info *alias, struct perf_pmu_test_event const *test_event, char const *pmu_name) { @@ -496,6 +496,23 @@ static int test__pmu_event_table(struct test_suite *te= st __maybe_unused, return 0; } =20 +struct test_core_pmu_event_aliases_cb_args { + struct perf_pmu_test_event const *test_event; + int *count; +}; + +static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_in= fo *alias) +{ + struct test_core_pmu_event_aliases_cb_args *args =3D state; + + if (compare_alias_to_test_event(alias, args->test_event, alias->pmu->name= )) + return -1; + (*args->count)++; + pr_debug2("testing aliases core PMU %s: matched event %s\n", + alias->pmu_name, alias->name); + return 0; +} + /* Verify aliases are as expected */ static int __test_core_pmu_event_aliases(char *pmu_name, int *count) { @@ -522,25 +539,19 @@ static int __test_core_pmu_event_aliases(char *pmu_na= me, int *count) pmu_add_cpu_aliases_table(pmu, table); =20 for (; *test_event_table; test_event_table++) { - struct perf_pmu_test_event const *test_event =3D *test_event_table; - struct pmu_event const *event =3D &test_event->event; - struct perf_pmu_alias *alias =3D perf_pmu__find_alias(pmu, event->name); - - if (!alias) { - pr_debug("testing aliases core PMU %s: no alias, alias_table->name=3D%s= \n", - pmu_name, event->name); - res =3D -1; - break; - } - - if (compare_alias_to_test_event(alias, test_event, pmu_name)) { - res =3D -1; - break; - } - - (*count)++; - pr_debug2("testing aliases core PMU %s: matched event %s\n", - pmu_name, alias->name); + struct perf_pmu_test_event test_event =3D **test_event_table; + struct pmu_event const *event =3D &test_event.event; + struct test_core_pmu_event_aliases_cb_args args =3D { + .test_event =3D &test_event, + .count =3D count, + }; + int err; + + test_event.event.pmu =3D pmu_name; + err =3D perf_pmu__find_event(pmu, event->name, &args, + test_core_pmu_event_aliases_cb); + if (err) + res =3D err; } perf_pmu__delete(pmu); =20 @@ -553,7 +564,6 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) struct perf_pmu_test_event const **table; struct perf_pmu *pmu =3D &test_pmu->pmu; const char *pmu_name =3D pmu->name; - struct perf_pmu_alias *a, *tmp, *alias; const struct pmu_events_table *events_table; int res =3D 0; =20 @@ -564,8 +574,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) pmu_add_sys_aliases(pmu); =20 /* Count how many aliases we generated */ - list_for_each_entry(alias, &pmu->aliases, list) - alias_count++; + alias_count =3D perf_pmu__num_events(pmu); =20 /* Count how many aliases we expect from the known table */ for (table =3D &test_pmu->aliases[0]; *table; table++) @@ -574,33 +583,25 @@ static int __test_uncore_pmu_event_aliases(struct per= f_pmu_test_pmu *test_pmu) if (alias_count !=3D to_match_count) { pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) = vs found (%d)\n", pmu_name, to_match_count, alias_count); - res =3D -1; - goto out; + return -1; } =20 - list_for_each_entry(alias, &pmu->aliases, list) { - bool matched =3D false; - - for (table =3D &test_pmu->aliases[0]; *table; table++) { - struct perf_pmu_test_event const *test_event =3D *table; - struct pmu_event const *event =3D &test_event->event; - - if (!strcmp(event->name, alias->name)) { - if (compare_alias_to_test_event(alias, - test_event, - pmu_name)) { - continue; - } - matched =3D true; - matched_count++; - } - } - - if (matched =3D=3D false) { + for (table =3D &test_pmu->aliases[0]; *table; table++) { + struct perf_pmu_test_event test_event =3D **table; + struct pmu_event const *event =3D &test_event.event; + int err; + struct test_core_pmu_event_aliases_cb_args args =3D { + .test_event =3D &test_event, + .count =3D &matched_count, + }; + + err =3D perf_pmu__find_event(pmu, event->name, &args, + test_core_pmu_event_aliases_cb); + if (err) { + res =3D err; pr_debug("testing aliases uncore PMU %s: could not match alias %s\n", - pmu_name, alias->name); - res =3D -1; - goto out; + pmu_name, event->name); + return -1; } } =20 @@ -609,12 +610,6 @@ static int __test_uncore_pmu_event_aliases(struct perf= _pmu_test_pmu *test_pmu) pmu_name, matched_count, alias_count); res =3D -1; } - -out: - list_for_each_entry_safe(a, tmp, &pmu->aliases, list) { - list_del(&a->list); - perf_pmu_free_alias(a); - } return res; } =20 diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 7d9d687d9191..7cad82a9f578 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -193,38 +193,31 @@ static void fix_raw(struct list_head *config_terms, s= truct perf_pmu *pmu) struct parse_events_term *term; =20 list_for_each_entry(term, config_terms, list) { - struct perf_pmu_alias *alias; - bool matched =3D false; + u64 num; =20 if (term->type_term !=3D PARSE_EVENTS__TERM_TYPE_RAW) continue; =20 - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcmp(alias->name, term->val.str)) { - free(term->config); - term->config =3D term->val.str; - term->type_val =3D PARSE_EVENTS__TERM_TYPE_NUM; - term->type_term =3D PARSE_EVENTS__TERM_TYPE_USER; - term->val.num =3D 1; - term->no_value =3D true; - matched =3D true; - break; - } - } - if (!matched) { - u64 num; - + if (perf_pmu__have_event(pmu, term->val.str)) { free(term->config); - term->config =3D strdup("config"); - errno =3D 0; - num =3D strtoull(term->val.str + 1, NULL, 16); - assert(errno =3D=3D 0); - free(term->val.str); + term->config =3D term->val.str; term->type_val =3D PARSE_EVENTS__TERM_TYPE_NUM; - term->type_term =3D PARSE_EVENTS__TERM_TYPE_CONFIG; - term->val.num =3D num; - term->no_value =3D false; + term->type_term =3D PARSE_EVENTS__TERM_TYPE_USER; + term->val.num =3D 1; + term->no_value =3D true; + continue; } + + free(term->config); + term->config =3D strdup("config"); + errno =3D 0; + num =3D strtoull(term->val.str + 1, NULL, 16); + assert(errno =3D=3D 0); + free(term->val.str); + term->type_val =3D PARSE_EVENTS__TERM_TYPE_NUM; + term->type_term =3D PARSE_EVENTS__TERM_TYPE_CONFIG; + term->val.num =3D num; + term->no_value =3D false; } } =20 @@ -1458,28 +1451,22 @@ int parse_events_multi_pmu_add(struct parse_events_= state *parse_state, INIT_LIST_HEAD(list); =20 while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { - struct perf_pmu_alias *alias; bool auto_merge_stats; =20 if (parse_events__filter_pmu(parse_state, pmu)) continue; =20 - auto_merge_stats =3D perf_pmu__auto_merge_stats(pmu); + if (!perf_pmu__have_event(pmu, str)) + continue; =20 - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcasecmp(alias->name, str)) { - parse_events_copy_term_list(head, &orig_head); - if (!parse_events_add_pmu(parse_state, list, - pmu->name, orig_head, - auto_merge_stats, loc)) { - pr_debug("%s -> %s/%s/\n", str, - pmu->name, alias->str); - parse_state->wild_card_pmus =3D true; - ok++; - } - parse_events_terms__delete(orig_head); - } + auto_merge_stats =3D perf_pmu__auto_merge_stats(pmu); + parse_events_copy_term_list(head, &orig_head); + if (!parse_events_add_pmu(parse_state, list, pmu->name, + orig_head, auto_merge_stats, loc)) { + pr_debug("%s -> %s/%s/\n", str, pmu->name, str); + ok++; } + parse_events_terms__delete(orig_head); } =20 if (parse_state->fake_pmu) { diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 7c3de51bab08..c315f0cecc73 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -31,6 +31,61 @@ =20 struct perf_pmu perf_pmu__fake; =20 +#define UNIT_MAX_LEN 31 /* max length for event unit name */ + +/** + * struct perf_pmu_alias - An event either read from sysfs or builtin in + * pmu-events.c, created by parsing the pmu-events json files. + */ +struct perf_pmu_alias { + /** @name: Name of the event like "mem-loads". */ + char *name; + /** @desc: Optional short description of the event. */ + char *desc; + /** @long_desc: Optional long description. */ + char *long_desc; + /** + * @topic: Optional topic such as cache or pipeline, particularly for + * json events. + */ + char *topic; + /** + * @str: Comma separated parameter list like + * "event=3D0xcd,umask=3D0x1,ldlat=3D0x3". + */ + char *str; + /** @terms: Owned list of the original parsed parameters. */ + struct list_head terms; + /** @list: List element of struct perf_pmu aliases. */ + struct list_head list; + /** @unit: Units for the event, such as bytes or cache lines. */ + char unit[UNIT_MAX_LEN+1]; + /** @scale: Value to scale read counter values by. */ + double scale; + /** + * @per_pkg: Does the file + * /bus/event_source/devices//events/.per-pkg or + * equivalent json value exist and have the value 1. + */ + bool per_pkg; + /** + * @snapshot: Does the file + * /bus/event_source/devices//events/.snapshot + * exist and have the value 1. + */ + bool snapshot; + /** + * @deprecated: Is the event hidden and so not shown in perf list by + * default. + */ + bool deprecated; + /** + * @pmu_name: The name copied from the json struct pmu_event. This can + * differ from the PMU name as it won't have suffixes. + */ + char *pmu_name; +}; + /** * struct perf_pmu_format - Values from a format file read from * /devices/cpu/format/ held in struct perf_pmu. @@ -351,7 +406,7 @@ static void perf_pmu_update_alias(struct perf_pmu_alias= *old, } =20 /* Delete an alias entry. */ -void perf_pmu_free_alias(struct perf_pmu_alias *newalias) +static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) { zfree(&newalias->name); zfree(&newalias->desc); @@ -1345,10 +1400,20 @@ int perf_pmu__config(struct perf_pmu *pmu, struct p= erf_event_attr *attr, return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); } =20 +static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *= pmu, const char *str) +{ + struct perf_pmu_alias *alias; + + list_for_each_entry(alias, &pmu->aliases, list) { + if (!strcasecmp(alias->name, str)) + return alias; + } + return NULL; +} + static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, struct parse_events_term *term) { - struct perf_pmu_alias *alias; char *name; =20 if (parse_events__is_hardcoded_term(term)) @@ -1360,6 +1425,7 @@ static struct perf_pmu_alias *pmu_find_alias(struct p= erf_pmu *pmu, if (pmu_find_format(&pmu->format, term->config)) return NULL; name =3D term->config; + } else if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR) { if (strcasecmp(term->config, "event")) return NULL; @@ -1368,11 +1434,7 @@ static struct perf_pmu_alias *pmu_find_alias(struct = perf_pmu *pmu, return NULL; } =20 - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcasecmp(alias->name, name)) - return alias; - } - return NULL; + return perf_pmu__find_alias(pmu, name); } =20 =20 @@ -1455,16 +1517,33 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, str= uct list_head *head_terms, return 0; } =20 -struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const ch= ar *event) +struct find_event_args { + const char *event; + void *state; + pmu_event_callback cb; +}; + +static int find_event_callback(void *state, struct pmu_event_info *info) { - struct perf_pmu_alias *alias; + struct find_event_args *args =3D state; =20 - list_for_each_entry(alias, &pmu->aliases, list) - if (!strcmp(event, alias->name)) - return alias; + if (!strcmp(args->event, info->name)) + return args->cb(args->state, info); =20 - return NULL; + return 0; } + +int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *st= ate, pmu_event_callback cb) +{ + struct find_event_args args =3D { + .event =3D event, + .state =3D state, + .cb =3D cb, + }; + + return perf_pmu__for_each_event(pmu, &args, find_event_callback); +} + static void perf_pmu__del_formats(struct list_head *formats) { struct perf_pmu_format *fmt, *tmp; @@ -1504,13 +1583,110 @@ bool perf_pmu__auto_merge_stats(const struct perf_= pmu *pmu) =20 bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name) { - struct perf_pmu_alias *alias; + return perf_pmu__find_alias(pmu, name) !=3D NULL; +} =20 - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcmp(alias->name, name)) - return true; +size_t perf_pmu__num_events(const struct perf_pmu *pmu) +{ + struct list_head *list; + size_t nr =3D 0; + + list_for_each(list, &pmu->aliases) + nr++; + + return pmu->selectable ? nr + 1 : nr; +} + +static int sub_non_neg(int a, int b) +{ + if (b > a) + return 0; + return a - b; +} + +static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, + const struct perf_pmu_alias *alias) +{ + struct parse_events_term *term; + int used =3D snprintf(buf, len, "%s/%s", pmu->name, alias->name); + + list_for_each_entry(term, &alias->terms, list) { + if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR) + used +=3D snprintf(buf + used, sub_non_neg(len, used), + ",%s=3D%s", term->config, + term->val.str); } - return false; + + if (sub_non_neg(len, used) > 0) { + buf[used] =3D '/'; + used++; + } + if (sub_non_neg(len, used) > 0) { + buf[used] =3D '\0'; + used++; + } else + buf[len - 1] =3D '\0'; + + return buf; +} + +int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_= event_callback cb) +{ + char buf[1024]; + struct perf_pmu_alias *event; + struct pmu_event_info info =3D { + .pmu =3D pmu, + }; + int ret =3D 0; + + list_for_each_entry(event, &pmu->aliases, list) { + size_t buf_used; + + info.pmu_name =3D event->pmu_name ?: pmu->name; + info.alias =3D NULL; + if (event->desc) { + info.name =3D event->name; + buf_used =3D 0; + } else { + info.name =3D format_alias(buf, sizeof(buf), pmu, event); + if (pmu->is_core) { + info.alias =3D info.name; + info.name =3D event->name; + } + buf_used =3D strlen(buf) + 1; + } + info.scale_unit =3D NULL; + if (strlen(event->unit) || event->scale !=3D 1.0) { + info.scale_unit =3D buf + buf_used; + buf_used +=3D snprintf(buf + buf_used, sizeof(buf) - buf_used, + "%G%s", event->scale, event->unit) + 1; + } + info.desc =3D event->desc; + info.long_desc =3D event->long_desc; + info.encoding_desc =3D buf + buf_used; + buf_used +=3D snprintf(buf + buf_used, sizeof(buf) - buf_used, + "%s/%s/", info.pmu_name, event->str) + 1; + info.topic =3D event->topic; + info.str =3D event->str; + info.deprecated =3D event->deprecated; + ret =3D cb(state, &info); + if (ret) + return ret; + } + if (pmu->selectable) { + info.name =3D buf; + snprintf(buf, sizeof(buf), "%s//", pmu->name); + info.alias =3D NULL; + info.scale_unit =3D NULL; + info.desc =3D NULL; + info.long_desc =3D NULL; + info.encoding_desc =3D NULL; + info.topic =3D NULL; + info.pmu_name =3D pmu->name; + info.deprecated =3D false; + ret =3D cb(state, &info); + } + return ret; } =20 bool perf_pmu__is_software(const struct perf_pmu *pmu) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 675c9b97f7bf..f37e3d75094f 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -158,61 +158,22 @@ struct perf_pmu_info { bool snapshot; }; =20 -#define UNIT_MAX_LEN 31 /* max length for event unit name */ - -/** - * struct perf_pmu_alias - An event either read from sysfs or builtin in - * pmu-events.c, created by parsing the pmu-events json files. - */ -struct perf_pmu_alias { - /** @name: Name of the event like "mem-loads". */ - char *name; - /** @desc: Optional short description of the event. */ - char *desc; - /** @long_desc: Optional long description. */ - char *long_desc; - /** - * @topic: Optional topic such as cache or pipeline, particularly for - * json events. - */ - char *topic; - /** - * @str: Comma separated parameter list like - * "event=3D0xcd,umask=3D0x1,ldlat=3D0x3". - */ - char *str; - /** @terms: Owned list of the original parsed parameters. */ - struct list_head terms; - /** @list: List element of struct perf_pmu aliases. */ - struct list_head list; - /** @unit: Units for the event, such as bytes or cache lines. */ - char unit[UNIT_MAX_LEN+1]; - /** @scale: Value to scale read counter values by. */ - double scale; - /** - * @per_pkg: Does the file - * /bus/event_source/devices//events/.per-pkg or - * equivalent json value exist and have the value 1. - */ - bool per_pkg; - /** - * @snapshot: Does the file - * /bus/event_source/devices//events/.snapshot - * exist and have the value 1. - */ - bool snapshot; - /** - * @deprecated: Is the event hidden and so not shown in perf list by - * default. - */ +struct pmu_event_info { + const struct perf_pmu *pmu; + const char *name; + const char* alias; + const char *scale_unit; + const char *desc; + const char *long_desc; + const char *encoding_desc; + const char *topic; + const char *pmu_name; + const char *str; bool deprecated; - /** - * @pmu_name: The name copied from the json struct pmu_event. This can - * differ from the PMU name as it won't have suffixes. - */ - char *pmu_name; }; =20 +typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info= ); + void pmu_add_sys_aliases(struct perf_pmu *pmu); int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, struct list_head *head_terms, @@ -225,7 +186,7 @@ __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const= char *name); int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, struct perf_pmu_info *info); -struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const ch= ar *event); +int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *st= ate, pmu_event_callback cb); =20 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_loa= d); void perf_pmu_format__set_value(void *format, int config, unsigned long *b= its); @@ -235,6 +196,9 @@ bool is_pmu_core(const char *name); bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu); bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu); bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name); +size_t perf_pmu__num_events(const struct perf_pmu *pmu); +int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_= event_callback cb); + /** * perf_pmu_is_software - is the PMU a software PMU as in it uses the * perf_sw_context in the kernel? @@ -259,7 +223,6 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, char *perf_pmu__getcpuid(struct perf_pmu *pmu); const struct pmu_events_table *pmu_events_table__find(void); const struct pmu_metrics_table *pmu_metrics_table__find(void); -void perf_pmu_free_alias(struct perf_pmu_alias *alias); =20 int perf_pmu__convert_scale(const char *scale, char **end, double *sval); =20 diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index c58ba9fb6a36..4dd5912617ff 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -258,219 +258,145 @@ int __weak perf_pmus__num_mem_pmus(void) struct sevent { /** PMU for event. */ const struct perf_pmu *pmu; - /** - * Optional event for name, desc, etc. If not present then this is a - * selectable PMU and the event name is shown as "//". - */ - const struct perf_pmu_alias *event; - /** Is the PMU for the CPU? */ - bool is_cpu; + const char *name; + const char* alias; + const char *scale_unit; + const char *desc; + const char *long_desc; + const char *encoding_desc; + const char *topic; + const char *pmu_name; + bool deprecated; }; =20 static int cmp_sevent(const void *a, const void *b) { const struct sevent *as =3D a; const struct sevent *bs =3D b; - const char *a_pmu_name =3D NULL, *b_pmu_name =3D NULL; - const char *a_name =3D "//", *a_desc =3D NULL, *a_topic =3D ""; - const char *b_name =3D "//", *b_desc =3D NULL, *b_topic =3D ""; + bool a_iscpu, b_iscpu; int ret; =20 - if (as->event) { - a_name =3D as->event->name; - a_desc =3D as->event->desc; - a_topic =3D as->event->topic ?: ""; - a_pmu_name =3D as->event->pmu_name; - } - if (bs->event) { - b_name =3D bs->event->name; - b_desc =3D bs->event->desc; - b_topic =3D bs->event->topic ?: ""; - b_pmu_name =3D bs->event->pmu_name; - } /* Put extra events last. */ - if (!!a_desc !=3D !!b_desc) - return !!a_desc - !!b_desc; + if (!!as->desc !=3D !!bs->desc) + return !!as->desc - !!bs->desc; =20 /* Order by topics. */ - ret =3D strcmp(a_topic, b_topic); + ret =3D strcmp(as->topic ?: "", bs->topic ?: ""); if (ret) return ret; =20 /* Order CPU core events to be first */ - if (as->is_cpu !=3D bs->is_cpu) - return as->is_cpu ? -1 : 1; + a_iscpu =3D as->pmu ? as->pmu->is_core : true; + b_iscpu =3D bs->pmu ? bs->pmu->is_core : true; + if (a_iscpu !=3D b_iscpu) + return a_iscpu ? -1 : 1; =20 /* Order by PMU name. */ if (as->pmu !=3D bs->pmu) { - a_pmu_name =3D a_pmu_name ?: (as->pmu->name ?: ""); - b_pmu_name =3D b_pmu_name ?: (bs->pmu->name ?: ""); - ret =3D strcmp(a_pmu_name, b_pmu_name); + ret =3D strcmp(as->pmu_name ?: "", bs->pmu_name ?: ""); if (ret) return ret; } =20 /* Order by event name. */ - return strcmp(a_name, b_name); + return strcmp(as->name, bs->name); } =20 -static bool pmu_alias_is_duplicate(struct sevent *alias_a, - struct sevent *alias_b) +static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b) { - const char *a_pmu_name =3D NULL, *b_pmu_name =3D NULL; - const char *a_name =3D "//", *b_name =3D "//"; - - - if (alias_a->event) { - a_name =3D alias_a->event->name; - a_pmu_name =3D alias_a->event->pmu_name; - } - if (alias_b->event) { - b_name =3D alias_b->event->name; - b_pmu_name =3D alias_b->event->pmu_name; - } - /* Different names -> never duplicates */ - if (strcmp(a_name, b_name)) + if (strcmp(a->name ?: "//", b->name ?: "//")) return false; =20 /* Don't remove duplicates for different PMUs */ - a_pmu_name =3D a_pmu_name ?: (alias_a->pmu->name ?: ""); - b_pmu_name =3D b_pmu_name ?: (alias_b->pmu->name ?: ""); - return strcmp(a_pmu_name, b_pmu_name) =3D=3D 0; + return strcmp(a->pmu_name, b->pmu_name) =3D=3D 0; } =20 -static int sub_non_neg(int a, int b) -{ - if (b > a) - return 0; - return a - b; -} +struct events_callback_state { + struct sevent *aliases; + size_t aliases_len; + size_t index; +}; =20 -static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, - const struct perf_pmu_alias *alias) +static int perf_pmus__print_pmu_events__callback(void *vstate, + struct pmu_event_info *info) { - struct parse_events_term *term; - int used =3D snprintf(buf, len, "%s/%s", pmu->name, alias->name); - - list_for_each_entry(term, &alias->terms, list) { - if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR) - used +=3D snprintf(buf + used, sub_non_neg(len, used), - ",%s=3D%s", term->config, - term->val.str); - } + struct events_callback_state *state =3D vstate; + struct sevent *s; =20 - if (sub_non_neg(len, used) > 0) { - buf[used] =3D '/'; - used++; + if (state->index >=3D state->aliases_len) { + pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name); + return 1; } - if (sub_non_neg(len, used) > 0) { - buf[used] =3D '\0'; - used++; - } else - buf[len - 1] =3D '\0'; - - return buf; + s =3D &state->aliases[state->index]; + s->pmu =3D info->pmu; +#define COPY_STR(str) s->str =3D info->str ? strdup(info->str) : NULL + COPY_STR(name); + COPY_STR(alias); + COPY_STR(scale_unit); + COPY_STR(desc); + COPY_STR(long_desc); + COPY_STR(encoding_desc); + COPY_STR(topic); + COPY_STR(pmu_name); +#undef COPY_STR + s->deprecated =3D info->deprecated; + state->index++; + return 0; } =20 void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, v= oid *print_state) { struct perf_pmu *pmu; - struct perf_pmu_alias *event; - char buf[1024]; int printed =3D 0; - int len, j; + int len; struct sevent *aliases; + struct events_callback_state state; =20 pmu =3D NULL; len =3D 0; - while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { - list_for_each_entry(event, &pmu->aliases, list) - len++; - if (pmu->selectable) - len++; - } + while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) + len +=3D perf_pmu__num_events(pmu); + aliases =3D zalloc(sizeof(struct sevent) * len); if (!aliases) { pr_err("FATAL: not enough memory to print PMU events\n"); return; } pmu =3D NULL; - j =3D 0; + state =3D (struct events_callback_state) { + .aliases =3D aliases, + .aliases_len =3D len, + .index =3D 0, + }; while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { - bool is_cpu =3D pmu->is_core; - - list_for_each_entry(event, &pmu->aliases, list) { - aliases[j].event =3D event; - aliases[j].pmu =3D pmu; - aliases[j].is_cpu =3D is_cpu; - j++; - } - if (pmu->selectable) { - aliases[j].event =3D NULL; - aliases[j].pmu =3D pmu; - aliases[j].is_cpu =3D is_cpu; - j++; - } + perf_pmu__for_each_event(pmu, &state, perf_pmus__print_pmu_events__callb= ack); } - len =3D j; qsort(aliases, len, sizeof(struct sevent), cmp_sevent); - for (j =3D 0; j < len; j++) { - const char *name, *alias =3D NULL, *scale_unit =3D NULL, - *desc =3D NULL, *long_desc =3D NULL, - *encoding_desc =3D NULL, *topic =3D NULL, - *pmu_name =3D NULL; - bool deprecated =3D false; - size_t buf_used; - + for (int j =3D 0; j < len; j++) { /* Skip duplicates */ if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) continue; =20 - if (!aliases[j].event) { - /* A selectable event. */ - pmu_name =3D aliases[j].pmu->name; - buf_used =3D snprintf(buf, sizeof(buf), "%s//", pmu_name) + 1; - name =3D buf; - } else { - if (aliases[j].event->desc) { - name =3D aliases[j].event->name; - buf_used =3D 0; - } else { - name =3D format_alias(buf, sizeof(buf), aliases[j].pmu, - aliases[j].event); - if (aliases[j].is_cpu) { - alias =3D name; - name =3D aliases[j].event->name; - } - buf_used =3D strlen(buf) + 1; - } - pmu_name =3D aliases[j].event->pmu_name ?: (aliases[j].pmu->name ?: ""); - if (strlen(aliases[j].event->unit) || aliases[j].event->scale !=3D 1.0)= { - scale_unit =3D buf + buf_used; - buf_used +=3D snprintf(buf + buf_used, sizeof(buf) - buf_used, - "%G%s", aliases[j].event->scale, - aliases[j].event->unit) + 1; - } - desc =3D aliases[j].event->desc; - long_desc =3D aliases[j].event->long_desc; - topic =3D aliases[j].event->topic; - encoding_desc =3D buf + buf_used; - buf_used +=3D snprintf(buf + buf_used, sizeof(buf) - buf_used, - "%s/%s/", pmu_name, aliases[j].event->str) + 1; - deprecated =3D aliases[j].event->deprecated; - } print_cb->print_event(print_state, - pmu_name, - topic, - name, - alias, - scale_unit, - deprecated, + aliases[j].pmu_name, + aliases[j].topic, + aliases[j].name, + aliases[j].alias, + aliases[j].scale_unit, + aliases[j].deprecated, "Kernel PMU event", - desc, - long_desc, - encoding_desc); + aliases[j].desc, + aliases[j].long_desc, + aliases[j].encoding_desc); + zfree(&aliases[j].name); + zfree(&aliases[j].alias); + zfree(&aliases[j].scale_unit); + zfree(&aliases[j].desc); + zfree(&aliases[j].long_desc); + zfree(&aliases[j].encoding_desc); + zfree(&aliases[j].topic); + zfree(&aliases[j].pmu_name); } if (printed && pager_in_use()) printf("\n"); --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3097EE49A0 for ; Wed, 23 Aug 2023 08:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233701AbjHWIQa (ORCPT ); Wed, 23 Aug 2023 04:16:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233739AbjHWIK2 (ORCPT ); Wed, 23 Aug 2023 04:10:28 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C0541707 for ; Wed, 23 Aug 2023 01:09:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d74a012e6a6so3645298276.2 for ; Wed, 23 Aug 2023 01:09:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778139; x=1693382939; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=EjKOCLuFJFgx6ztDnM71PYKJcUuovlvTFeoWlk3ZsFE=; b=jSur6rALwrNCBUMquiewe0adRDNIiOBBenT4Mj3YLqBzYEOVCm8n/ISkTFQucZ3H1s 79TQDb3pbn3og0OzZuPVntUQJxO+ic8P5h002rwJzEmTfA79vCR5bgPaiMvUI2L2TcUT /ppQoqAvI9ArnE4W0ESPHfaBLRuPf/WAC743kaphNVt//Uive4ERLT57en2TZI1GEUVU 7yO3XreaPllhTJAPtYuVV46/QtT/h/0zL5Iz+9W92bTgbU9MqJO2klG3ZTRfIgGiqJEH ZvLVeNYFzrVxYrYK1diWuQmCdYq0rFlXljXAioj6tt3k2enqHWz8yaO1psHYpg93Ef8r mRkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778139; x=1693382939; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EjKOCLuFJFgx6ztDnM71PYKJcUuovlvTFeoWlk3ZsFE=; b=KBUE3Njr3rmeBP06BiGr91zH0in4L0a2aoy+G87y1bOrTruu5r8jDrU7l6rsfrdz9r Ig+Vi73UxPdOQPswxTxbuBx9NL8/bCJhWW8SRu0ZQUDh1cj525oQC08tscxAKkfDsPyM s3Z6SZxDXvtgy5SILozwQPYBz4e/2e3HNNkGNmS4zPeTmQQK2cw1bI4CIbpDLO8FVIIG o01OcuHcru+wiqNnlNQoxgCMaR4NzyrNWceOz5cFHd0LHCIPRFZZLHeLp4lMOQjn8cpi pHVU6+Hs7XZefdnhKK4/dgQ0l+M31T0kffLJgjikJ+/QOWtLNxuK5f/Zk0FBpmuDEMc2 KcfQ== X-Gm-Message-State: AOJu0YwtxfuO88lIskIuoElZpmuJzvuDEinL1+/NA7XE9WR1sv9j5VcO PXgvlW8qpwsTJ38UmjS1HY1cNj58NbW2 X-Google-Smtp-Source: AGHT+IGVExLkde0SFlvgOq7rJp2KB4sQNHPzqEgJoI++UNPQ4wtXdd1qAqaBsYBM+fmSyfkl7rirEn75gp8W X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:12c6:b0:d77:bcce:eb11 with SMTP id j6-20020a05690212c600b00d77bcceeb11mr37370ybu.10.1692778139350; Wed, 23 Aug 2023 01:08:59 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:14 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-12-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 11/25] perf pmu-events: Add extra underscore to function names From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add extra underscore before "for" of pmu_events_table_for_each_event and pmu_metrics_table_for_each_metric. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 10 +++++----- tools/perf/pmu-events/jevents.py | 12 ++++++------ tools/perf/pmu-events/pmu-events.h | 4 ++-- tools/perf/tests/pmu-events.c | 4 ++-- tools/perf/util/metricgroup.c | 10 +++++----- tools/perf/util/pmu.c | 2 +- tools/perf/util/s390-sample-raw.c | 2 +- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index a630c617e879..807f2e55c17c 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -266,7 +266,7 @@ static const struct pmu_sys_events pmu_sys_event_tables= [] =3D { }, }; =20 -int pmu_events_table_for_each_event(const struct pmu_events_table *table, = pmu_event_iter_fn fn, +int pmu_events_table__for_each_event(const struct pmu_events_table *table,= pmu_event_iter_fn fn, void *data) { for (const struct pmu_event *pe =3D &table->entries[0]; pe->name; pe++) { @@ -278,7 +278,7 @@ int pmu_events_table_for_each_event(const struct pmu_ev= ents_table *table, pmu_ev return 0; } =20 -int pmu_metrics_table_for_each_metric(const struct pmu_metrics_table *tabl= e, pmu_metric_iter_fn fn, +int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) { for (const struct pmu_metric *pm =3D &table->entries[0]; pm->metric_expr;= pm++) { @@ -371,7 +371,7 @@ const struct pmu_metrics_table *find_core_metrics_table= (const char *arch, const int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data) { for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->= arch; tables++) { - int ret =3D pmu_events_table_for_each_event(&tables->event_table, fn, da= ta); + int ret =3D pmu_events_table__for_each_event(&tables->event_table, fn, d= ata); =20 if (ret) return ret; @@ -384,7 +384,7 @@ int pmu_for_each_core_metric(pmu_metric_iter_fn fn, voi= d *data) for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - int ret =3D pmu_metrics_table_for_each_metric(&tables->metric_table, fn,= data); + int ret =3D pmu_metrics_table__for_each_metric(&tables->metric_table, fn= , data); =20 if (ret) return ret; @@ -408,7 +408,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *= data) for (const struct pmu_sys_events *tables =3D &pmu_sys_event_tables[0]; tables->name; tables++) { - int ret =3D pmu_events_table_for_each_event(&tables->table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->table, fn, data); =20 if (ret) return ret; diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 98cccc3fcbbd..aae5334099b1 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -753,7 +753,7 @@ static void decompress_metric(int offset, struct pmu_me= tric *pm) _args.output_file.write('\twhile (*p++);') _args.output_file.write("""} =20 -int pmu_events_table_for_each_event(const struct pmu_events_table *table, +int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn, void *data) { @@ -771,7 +771,7 @@ int pmu_events_table_for_each_event(const struct pmu_ev= ents_table *table, return 0; } =20 -int pmu_metrics_table_for_each_metric(const struct pmu_metrics_table *tabl= e, +int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) { @@ -870,7 +870,7 @@ int pmu_for_each_core_event(pmu_event_iter_fn fn, void = *data) for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - int ret =3D pmu_events_table_for_each_event(&tables->event= _table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, fn, data); =20 if (ret) return ret; @@ -883,7 +883,7 @@ int pmu_for_each_core_metric(pmu_metric_iter_fn fn, voi= d *data) for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - int ret =3D pmu_metrics_table_for_each_metric(&tables->met= ric_table, fn, data); + int ret =3D pmu_metrics_table__for_each_metric(&tables->me= tric_table, fn, data); =20 if (ret) return ret; @@ -907,7 +907,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *= data) for (const struct pmu_sys_events *tables =3D &pmu_sys_event_tables= [0]; tables->name; tables++) { - int ret =3D pmu_events_table_for_each_event(&tables->event= _table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, fn, data); =20 if (ret) return ret; @@ -920,7 +920,7 @@ int pmu_for_each_sys_metric(pmu_metric_iter_fn fn, void= *data) for (const struct pmu_sys_events *tables =3D &pmu_sys_event_tables= [0]; tables->name; tables++) { - int ret =3D pmu_metrics_table_for_each_metric(&tables->met= ric_table, fn, data); + int ret =3D pmu_metrics_table__for_each_metric(&tables->me= tric_table, fn, data); =20 if (ret) return ret; diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index caf59f23cd64..6557381b7de1 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -77,9 +77,9 @@ typedef int (*pmu_metric_iter_fn)(const struct pmu_metric= *pm, const struct pmu_metrics_table *table, void *data); =20 -int pmu_events_table_for_each_event(const struct pmu_events_table *table, = pmu_event_iter_fn fn, +int pmu_events_table__for_each_event(const struct pmu_events_table *table,= pmu_event_iter_fn fn, void *data); -int pmu_metrics_table_for_each_metric(const struct pmu_metrics_table *tabl= e, pmu_metric_iter_fn fn, +int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data); =20 const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu= *pmu); diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index dc87e66fb118..5f541eadc088 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -477,12 +477,12 @@ static int test__pmu_event_table(struct test_suite *t= est __maybe_unused, if (!table || !sys_event_table) return -1; =20 - err =3D pmu_events_table_for_each_event(table, test__pmu_event_table_core= _callback, + err =3D pmu_events_table__for_each_event(table, test__pmu_event_table_cor= e_callback, &map_events); if (err) return err; =20 - err =3D pmu_events_table_for_each_event(sys_event_table, test__pmu_event_= table_sys_callback, + err =3D pmu_events_table__for_each_event(sys_event_table, test__pmu_event= _table_sys_callback, &map_events); if (err) return err; diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index a6a5ed44a679..6231044a491e 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -527,7 +527,7 @@ void metricgroup__print(const struct print_callbacks *p= rint_cb, void *print_stat groups.node_delete =3D mep_delete; table =3D pmu_metrics_table__find(); if (table) { - pmu_metrics_table_for_each_metric(table, + pmu_metrics_table__for_each_metric(table, metricgroup__add_to_mep_groups_callback, &groups); } @@ -1069,7 +1069,7 @@ static bool metricgroup__find_metric(const char *pmu, .pm =3D pm, }; =20 - return pmu_metrics_table_for_each_metric(table, metricgroup__find_metric_= callback, &data) + return pmu_metrics_table__for_each_metric(table, metricgroup__find_metric= _callback, &data) ? true : false; } =20 @@ -1255,7 +1255,7 @@ static int metricgroup__add_metric(const char *pmu, c= onst char *metric_name, con * Iterate over all metrics seeing if metric matches either the * name or group. When it does add the metric to the list. */ - ret =3D pmu_metrics_table_for_each_metric(table, metricgroup__add_metric= _callback, + ret =3D pmu_metrics_table__for_each_metric(table, metricgroup__add_metri= c_callback, &data); if (ret) goto out; @@ -1740,7 +1740,7 @@ bool metricgroup__has_metric(const char *pmu, const c= har *metric) if (!table) return false; =20 - return pmu_metrics_table_for_each_metric(table, metricgroup__has_metric_c= allback, &data) + return pmu_metrics_table__for_each_metric(table, metricgroup__has_metric_= callback, &data) ? true : false; } =20 @@ -1770,7 +1770,7 @@ unsigned int metricgroups__topdown_max_level(void) if (!table) return false; =20 - pmu_metrics_table_for_each_metric(table, metricgroup__topdown_max_level_c= allback, + pmu_metrics_table__for_each_metric(table, metricgroup__topdown_max_level_= callback, &max_level); return max_level; } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index c315f0cecc73..95872bee28ac 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -892,7 +892,7 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, co= nst struct pmu_events_tab .pmu =3D pmu, }; =20 - pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, = &data); + pmu_events_table__for_each_event(table, pmu_add_cpu_aliases_map_callback,= &data); free(data.default_pmu_name); } =20 diff --git a/tools/perf/util/s390-sample-raw.c b/tools/perf/util/s390-sampl= e-raw.c index c10b891dbad6..91330c874170 100644 --- a/tools/perf/util/s390-sample-raw.c +++ b/tools/perf/util/s390-sample-raw.c @@ -168,7 +168,7 @@ static const char *get_counter_name(int set, int nr, co= nst struct pmu_events_tab if (!table) return NULL; =20 - pmu_events_table_for_each_event(table, get_counter_name_callback, &data); + pmu_events_table__for_each_event(table, get_counter_name_callback, &data); return data.result; } =20 --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3163DEE49A0 for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233881AbjHWIRk (ORCPT ); Wed, 23 Aug 2023 04:17:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233756AbjHWIKa (ORCPT ); Wed, 23 Aug 2023 04:10:30 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B604B170B for ; Wed, 23 Aug 2023 01:09:02 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7494be34f8so4032125276.2 for ; Wed, 23 Aug 2023 01:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778142; x=1693382942; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=C6U9uX7oueEa74oKkTWivvvrk8HCA+9h1GuvSCMbKhg=; b=yYBx+R1t3YglroUDquFlny5PlT0/aBcphI7ulqGgCebp/Xag8513iO+DA4YohB1M68 AFz1suo0C9rZnRvui20Xe72kfNTMmIu7g6QtXrHn1TB3VGKBTRX71nEYmTUUYAbR/gU4 SZFs/Ydsi6hcuVx2nkLsUPcWzWZ4b/EnHyKDEf41901rH8E10S6caRq598UreQktJ3Za lR8w91RZJpiUZFIL2S5ji3PK3nfiJH5FspBrjS+9jNs86vIZfXyRMYl6CeuMD02rqv/q 20UO5G3oOfzVEGpBkijVk/ONmRUTWDZwoWKdBDVZz2rgK3eGsw5/fR+HEi71w4A24vIX jdrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778142; x=1693382942; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=C6U9uX7oueEa74oKkTWivvvrk8HCA+9h1GuvSCMbKhg=; b=bzw9yOosHM7kdYl2p3xBKl9e7XwO5vF9kVHCVTLUdBa1Udb2aToOpFzVfAw3OdHjYo cgPTbIcBn35UQtszSqhbeUX6QfckZbiDB5r5lXQN5ayKv72Mgk6hMxrMZNjkPU3CsGUp CfdKsBSPwCgcmWUxegQiKJ+j1yR/qDO8oXm0BkFAtawaZZycUwxuwSs+hu6UASMMcFVU TuacKtEpYr6adNEONbRMiXmYNpxCdmnwxWhatogpRkNbuPqNh7rqb2o+hBYX0NvN3EF5 fXJQxMAh1AY5zqNObKDDrBWk7edR7yB0cxq2dlvun/aD3CUrWyvHv0EtBADaTkWD+9GA qqVA== X-Gm-Message-State: AOJu0YxFPCQxplv7Sd2bHgD+blpnEA0tv1bDTV8lxrW8ZODYgyihNPVM oE25GnPrp+WmSuZh0JwcODNX8tnBdMxP X-Google-Smtp-Source: AGHT+IF+h2Y7UaGBjXYnwj4OgVWOQbrCL2WVLcle/ISoLrnwQb8UJvCD3OrXshSwu2NK2chRprariTSNBdBT X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:c0d4:0:b0:d77:df8a:389a with SMTP id c203-20020a25c0d4000000b00d77df8a389amr3089ybf.3.1692778142011; Wed, 23 Aug 2023 01:09:02 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:15 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-13-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 12/25] perf jevents: Group events by PMU From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prior to this change a cpuid would map to a list of events where the PMU would be encoded alongside the event information. This change breaks apart each group of events so that there is a group per PMU. A new table is added with the PMU's name and the list of events, the original table now holding an array of these per PMU tables. These changes are to make it easier to get per PMU information about events, rather than the current approach of scanning all events. The perf binary size with BPF skeletons on x86 is reduced by about 1%. The unidentified PMU is now always expanded to "cpu". Signed-off-by: Ian Rogers --- tools/perf/pmu-events/jevents.py | 181 +++++++++++++++++++++++-------- tools/perf/tests/pmu-events.c | 30 +++-- 2 files changed, 154 insertions(+), 57 deletions(-) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index aae5334099b1..1ad20140114c 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -42,7 +42,7 @@ _metricgroups =3D {} # Order specific JsonEvent attributes will be visited. _json_event_attributes =3D [ # cmp_sevent related attributes. - 'name', 'pmu', 'topic', 'desc', + 'name', 'topic', 'desc', # Seems useful, put it early. 'event', # Short things in alphabetical order. @@ -53,7 +53,7 @@ _json_event_attributes =3D [ =20 # Attributes that are in pmu_metric rather than pmu_event. _json_metric_attributes =3D [ - 'pmu', 'metric_name', 'metric_group', 'metric_expr', 'metric_threshold= ', + 'metric_name', 'metric_group', 'metric_expr', 'metric_threshold', 'desc', 'long_desc', 'unit', 'compat', 'metricgroup_no_group', 'default_metricgroup_name', 'aggr_mode', 'event_grouping' ] @@ -252,7 +252,7 @@ class JsonEvent: def unit_to_pmu(unit: str) -> Optional[str]: """Convert a JSON Unit to Linux PMU name.""" if not unit: - return None + return 'cpu' # Comment brought over from jevents.c: # it's not realistic to keep adding these, we need something more sc= alable ... table =3D { @@ -343,10 +343,13 @@ class JsonEvent: self.desc +=3D extra_desc if self.long_desc and extra_desc: self.long_desc +=3D extra_desc - if self.pmu: - if self.desc and not self.desc.endswith('. '): - self.desc +=3D '. ' - self.desc =3D (self.desc if self.desc else '') + ('Unit: ' + self.pm= u + ' ') + if self.pmu and self.pmu !=3D 'cpu': + if not self.desc: + self.desc =3D 'Unit: ' + self.pmu + else: + if not self.desc.endswith('. '): + self.desc +=3D '. ' + self.desc +=3D 'Unit: ' + self.pmu if arch_std: if arch_std.lower() in _arch_std_events: event =3D _arch_std_events[arch_std.lower()].event @@ -437,13 +440,13 @@ def add_events_table_entries(item: os.DirEntry, topic= : str) -> None: def print_pending_events() -> None: """Optionally close events table.""" =20 - def event_cmp_key(j: JsonEvent) -> Tuple[bool, str, str, str, str]: + def event_cmp_key(j: JsonEvent) -> Tuple[str, str, bool, str, str]: def fix_none(s: Optional[str]) -> str: if s is None: return '' return s =20 - return (j.desc is not None, fix_none(j.topic), fix_none(j.name), fix_n= one(j.pmu), + return (fix_none(j.pmu).replace(',','_'), fix_none(j.name), j.desc is = not None, fix_none(j.topic), fix_none(j.metric_name)) =20 global _pending_events @@ -458,13 +461,36 @@ def print_pending_events() -> None: global event_tables _event_tables.append(_pending_events_tblname) =20 - _args.output_file.write( - f'static const struct compact_pmu_event {_pending_events_tblname}[] = =3D {{\n') - + first =3D True + last_pmu =3D None + pmus =3D set() for event in sorted(_pending_events, key=3Devent_cmp_key): + if event.pmu !=3D last_pmu: + if not first: + _args.output_file.write('};\n') + pmu_name =3D event.pmu.replace(',', '_') + _args.output_file.write( + f'static const struct compact_pmu_event {_pending_events_tblname= }_{pmu_name}[] =3D {{\n') + first =3D False + last_pmu =3D event.pmu + pmus.add((event.pmu, pmu_name)) + _args.output_file.write(event.to_c_string(metric=3DFalse)) _pending_events =3D [] =20 + _args.output_file.write(f""" +}}; + +const struct pmu_table_entry {_pending_events_tblname}[] =3D {{ +""") + for (pmu, tbl_pmu) in sorted(pmus): + pmu_name =3D f"{pmu}\\000" + _args.output_file.write(f"""{{ + .entries =3D {_pending_events_tblname}_{tbl_pmu}, + .num_entries =3D ARRAY_SIZE({_pending_events_tblname}_{tbl_pmu}), + .pmu_name =3D {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +}}, +""") _args.output_file.write('};\n\n') =20 def print_pending_metrics() -> None: @@ -490,13 +516,36 @@ def print_pending_metrics() -> None: global metric_tables _metric_tables.append(_pending_metrics_tblname) =20 - _args.output_file.write( - f'static const struct compact_pmu_event {_pending_metrics_tblname}[]= =3D {{\n') - + first =3D True + last_pmu =3D None + pmus =3D set() for metric in sorted(_pending_metrics, key=3Dmetric_cmp_key): + if metric.pmu !=3D last_pmu: + if not first: + _args.output_file.write('};\n') + pmu_name =3D metric.pmu.replace(',', '_') + _args.output_file.write( + f'static const struct compact_pmu_event {_pending_metrics_tblnam= e}_{pmu_name}[] =3D {{\n') + first =3D False + last_pmu =3D metric.pmu + pmus.add((metric.pmu, pmu_name)) + _args.output_file.write(metric.to_c_string(metric=3DTrue)) _pending_metrics =3D [] =20 + _args.output_file.write(f""" +}}; + +const struct pmu_table_entry {_pending_metrics_tblname}[] =3D {{ +""") + for (pmu, tbl_pmu) in sorted(pmus): + pmu_name =3D f"{pmu}\\000" + _args.output_file.write(f"""{{ + .entries =3D {_pending_metrics_tblname}_{tbl_pmu}, + .num_entries =3D ARRAY_SIZE({_pending_metrics_tblname}_{tbl_pmu}), + .pmu_name =3D {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +}}, +""") _args.output_file.write('};\n\n') =20 def get_topic(topic: str) -> str: @@ -532,6 +581,8 @@ def preprocess_one_file(parents: Sequence[str], item: o= s.DirEntry) -> None: =20 topic =3D get_topic(item.name) for event in read_json_events(item.path, topic): + pmu_name =3D f"{event.pmu}\\000" + _bcs.add(pmu_name) if event.name: _bcs.add(event.build_c_string(metric=3DFalse)) if event.metric_name: @@ -577,14 +628,14 @@ def print_mapping_table(archs: Sequence[str]) -> None: _args.output_file.write(""" /* Struct used to make the PMU event table implementation opaque to caller= s. */ struct pmu_events_table { - const struct compact_pmu_event *entries; - size_t length; + const struct pmu_table_entry *pmus; + uint32_t num_pmus; }; =20 /* Struct used to make the PMU metric table implementation opaque to calle= rs. */ struct pmu_metrics_table { - const struct compact_pmu_event *entries; - size_t length; + const struct pmu_table_entry *pmus; + uint32_t num_pmus; }; =20 /* @@ -614,12 +665,12 @@ const struct pmu_events_map pmu_events_map[] =3D { \t.arch =3D "testarch", \t.cpuid =3D "testcpu", \t.event_table =3D { -\t\t.entries =3D pmu_events__test_soc_cpu, -\t\t.length =3D ARRAY_SIZE(pmu_events__test_soc_cpu), +\t\t.pmus =3D pmu_events__test_soc_cpu, +\t\t.num_pmus =3D ARRAY_SIZE(pmu_events__test_soc_cpu), \t}, \t.metric_table =3D { -\t\t.entries =3D pmu_metrics__test_soc_cpu, -\t\t.length =3D ARRAY_SIZE(pmu_metrics__test_soc_cpu), +\t\t.pmus =3D pmu_metrics__test_soc_cpu, +\t\t.num_pmus =3D ARRAY_SIZE(pmu_metrics__test_soc_cpu), \t} }, """) @@ -649,12 +700,12 @@ const struct pmu_events_map pmu_events_map[] =3D { \t.arch =3D "{arch}", \t.cpuid =3D "{cpuid}", \t.event_table =3D {{ -\t\t.entries =3D {event_tblname}, -\t\t.length =3D {event_size} +\t\t.pmus =3D {event_tblname}, +\t\t.num_pmus =3D {event_size} \t}}, \t.metric_table =3D {{ -\t\t.entries =3D {metric_tblname}, -\t\t.length =3D {metric_size} +\t\t.pmus =3D {metric_tblname}, +\t\t.num_pmus =3D {metric_size} \t}} }}, """) @@ -685,15 +736,15 @@ static const struct pmu_sys_events pmu_sys_event_tabl= es[] =3D { for tblname in _sys_event_tables: _args.output_file.write(f"""\t{{ \t\t.event_table =3D {{ -\t\t\t.entries =3D {tblname}, -\t\t\t.length =3D ARRAY_SIZE({tblname}) +\t\t\t.pmus =3D {tblname}, +\t\t\t.num_pmus =3D ARRAY_SIZE({tblname}) \t\t}},""") metric_tblname =3D _sys_event_table_to_metric_table_mapping[tblname] if metric_tblname in _sys_metric_tables: _args.output_file.write(f""" \t\t.metric_table =3D {{ -\t\t\t.entries =3D {metric_tblname}, -\t\t\t.length =3D ARRAY_SIZE({metric_tblname}) +\t\t\t.pmus =3D {metric_tblname}, +\t\t\t.num_pmus =3D ARRAY_SIZE({metric_tblname}) \t\t}},""") printed_metric_tables.append(metric_tblname) _args.output_file.write(f""" @@ -753,18 +804,56 @@ static void decompress_metric(int offset, struct pmu_= metric *pm) _args.output_file.write('\twhile (*p++);') _args.output_file.write("""} =20 +static int pmu_events_table__for_each_event_pmu(const struct pmu_events_ta= ble *table, + const struct pmu_table_ent= ry *pmu, + pmu_event_iter_fn fn, + void *data) +{ + int ret; + struct pmu_event pe =3D { + .pmu =3D &big_c_string[pmu->pmu_name.offset], + }; + + for (uint32_t i =3D 0; i < pmu->num_entries; i++) { + decompress_event(pmu->entries[i].offset, &pe); + if (!pe.name) + continue; + ret =3D fn(&pe, table, data); + if (ret) + return ret; + } + return 0; + } + int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn, void *data) { - for (size_t i =3D 0; i < table->length; i++) { - struct pmu_event pe; - int ret; + for (size_t i =3D 0; i < table->num_pmus; i++) { + int ret =3D pmu_events_table__for_each_event_pmu(table, &t= able->pmus[i], + fn, data); =20 - decompress_event(table->entries[i].offset, &pe); - if (!pe.name) + if (ret) + return ret; + } + return 0; +} + +static int pmu_metrics_table__for_each_metric_pmu(const struct pmu_metrics= _table *table, + const struct pmu_table_ent= ry *pmu, + pmu_metric_iter_fn fn, + void *data) +{ + int ret; + struct pmu_metric pm =3D { + .pmu =3D &big_c_string[pmu->pmu_name.offset], + }; + + for (uint32_t i =3D 0; i < pmu->num_entries; i++) { + decompress_metric(pmu->entries[i].offset, &pm); + if (!pm.metric_expr) continue; - ret =3D fn(&pe, table, data); + ret =3D fn(&pm, table, data); if (ret) return ret; } @@ -775,14 +864,10 @@ int pmu_metrics_table__for_each_metric(const struct p= mu_metrics_table *table, pmu_metric_iter_fn fn, void *data) { - for (size_t i =3D 0; i < table->length; i++) { - struct pmu_metric pm; - int ret; + for (size_t i =3D 0; i < table->num_pmus; i++) { + int ret =3D pmu_metrics_table__for_each_metric_pmu(table, = &table->pmus[i], + fn, data); =20 - decompress_metric(table->entries[i].offset, &pm); - if (!pm.metric_expr) - continue; - ret =3D fn(&pm, table, data); if (ret) return ret; } @@ -1010,7 +1095,13 @@ such as "arm/cortex-a34".''', #include =20 struct compact_pmu_event { - int offset; + int offset; +}; + +struct pmu_table_entry { + const struct compact_pmu_event *entries; + uint32_t num_entries; + struct compact_pmu_event pmu_name; }; =20 """) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 5f541eadc088..0b6efabc3d20 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -44,6 +44,7 @@ struct perf_pmu_test_pmu { =20 static const struct perf_pmu_test_event bp_l1_btb_correct =3D { .event =3D { + .pmu =3D "cpu", .name =3D "bp_l1_btb_correct", .event =3D "event=3D0x8a", .desc =3D "L1 BTB Correction", @@ -55,6 +56,7 @@ static const struct perf_pmu_test_event bp_l1_btb_correct= =3D { =20 static const struct perf_pmu_test_event bp_l2_btb_correct =3D { .event =3D { + .pmu =3D "cpu", .name =3D "bp_l2_btb_correct", .event =3D "event=3D0x8b", .desc =3D "L2 BTB Correction", @@ -66,6 +68,7 @@ static const struct perf_pmu_test_event bp_l2_btb_correct= =3D { =20 static const struct perf_pmu_test_event segment_reg_loads_any =3D { .event =3D { + .pmu =3D "cpu", .name =3D "segment_reg_loads.any", .event =3D "event=3D0x6,period=3D200000,umask=3D0x80", .desc =3D "Number of segment register loads", @@ -77,6 +80,7 @@ static const struct perf_pmu_test_event segment_reg_loads= _any =3D { =20 static const struct perf_pmu_test_event dispatch_blocked_any =3D { .event =3D { + .pmu =3D "cpu", .name =3D "dispatch_blocked.any", .event =3D "event=3D0x9,period=3D200000,umask=3D0x20", .desc =3D "Memory cluster signals to block micro-op dispatch for any rea= son", @@ -88,6 +92,7 @@ static const struct perf_pmu_test_event dispatch_blocked_= any =3D { =20 static const struct perf_pmu_test_event eist_trans =3D { .event =3D { + .pmu =3D "cpu", .name =3D "eist_trans", .event =3D "event=3D0x3a,period=3D200000,umask=3D0x0", .desc =3D "Number of Enhanced Intel SpeedStep(R) Technology (EIST) trans= itions", @@ -99,6 +104,7 @@ static const struct perf_pmu_test_event eist_trans =3D { =20 static const struct perf_pmu_test_event l3_cache_rd =3D { .event =3D { + .pmu =3D "cpu", .name =3D "l3_cache_rd", .event =3D "event=3D0x40", .desc =3D "L3 cache access, read", @@ -123,7 +129,7 @@ static const struct perf_pmu_test_event uncore_hisi_ddr= c_flux_wcmd =3D { .event =3D { .name =3D "uncore_hisi_ddrc.flux_wcmd", .event =3D "event=3D0x2", - .desc =3D "DDRC write commands. Unit: hisi_sccl,ddrc ", + .desc =3D "DDRC write commands. Unit: hisi_sccl,ddrc", .topic =3D "uncore", .long_desc =3D "DDRC write commands", .pmu =3D "hisi_sccl,ddrc", @@ -137,7 +143,7 @@ static const struct perf_pmu_test_event unc_cbo_xsnp_re= sponse_miss_eviction =3D { .event =3D { .name =3D "unc_cbo_xsnp_response.miss_eviction", .event =3D "event=3D0x22,umask=3D0x81", - .desc =3D "A cross-core snoop resulted from L3 Eviction which misses in = some processor core. Unit: uncore_cbox ", + .desc =3D "A cross-core snoop resulted from L3 Eviction which misses in = some processor core. Unit: uncore_cbox", .topic =3D "uncore", .long_desc =3D "A cross-core snoop resulted from L3 Eviction which misse= s in some processor core", .pmu =3D "uncore_cbox", @@ -151,7 +157,7 @@ static const struct perf_pmu_test_event uncore_hyphen = =3D { .event =3D { .name =3D "event-hyphen", .event =3D "event=3D0xe0,umask=3D0x00", - .desc =3D "UNC_CBO_HYPHEN. Unit: uncore_cbox ", + .desc =3D "UNC_CBO_HYPHEN. Unit: uncore_cbox", .topic =3D "uncore", .long_desc =3D "UNC_CBO_HYPHEN", .pmu =3D "uncore_cbox", @@ -165,7 +171,7 @@ static const struct perf_pmu_test_event uncore_two_hyph= =3D { .event =3D { .name =3D "event-two-hyph", .event =3D "event=3D0xc0,umask=3D0x00", - .desc =3D "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ", + .desc =3D "UNC_CBO_TWO_HYPH. Unit: uncore_cbox", .topic =3D "uncore", .long_desc =3D "UNC_CBO_TWO_HYPH", .pmu =3D "uncore_cbox", @@ -179,7 +185,7 @@ static const struct perf_pmu_test_event uncore_hisi_l3c= _rd_hit_cpipe =3D { .event =3D { .name =3D "uncore_hisi_l3c.rd_hit_cpipe", .event =3D "event=3D0x7", - .desc =3D "Total read hits. Unit: hisi_sccl,l3c ", + .desc =3D "Total read hits. Unit: hisi_sccl,l3c", .topic =3D "uncore", .long_desc =3D "Total read hits", .pmu =3D "hisi_sccl,l3c", @@ -193,7 +199,7 @@ static const struct perf_pmu_test_event uncore_imc_free= _running_cache_miss =3D { .event =3D { .name =3D "uncore_imc_free_running.cache_miss", .event =3D "event=3D0x12", - .desc =3D "Total cache misses. Unit: uncore_imc_free_running ", + .desc =3D "Total cache misses. Unit: uncore_imc_free_running", .topic =3D "uncore", .long_desc =3D "Total cache misses", .pmu =3D "uncore_imc_free_running", @@ -207,7 +213,7 @@ static const struct perf_pmu_test_event uncore_imc_cach= e_hits =3D { .event =3D { .name =3D "uncore_imc.cache_hits", .event =3D "event=3D0x34", - .desc =3D "Total cache hits. Unit: uncore_imc ", + .desc =3D "Total cache hits. Unit: uncore_imc", .topic =3D "uncore", .long_desc =3D "Total cache hits", .pmu =3D "uncore_imc", @@ -232,13 +238,13 @@ static const struct perf_pmu_test_event sys_ddr_pmu_w= rite_cycles =3D { .event =3D { .name =3D "sys_ddr_pmu.write_cycles", .event =3D "event=3D0x2b", - .desc =3D "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ", + .desc =3D "ddr write-cycles event. Unit: uncore_sys_ddr_pmu", .topic =3D "uncore", .pmu =3D "uncore_sys_ddr_pmu", .compat =3D "v8", }, .alias_str =3D "event=3D0x2b", - .alias_long_desc =3D "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ", + .alias_long_desc =3D "ddr write-cycles event. Unit: uncore_sys_ddr_pmu", .matching_pmu =3D "uncore_sys_ddr_pmu", }; =20 @@ -246,13 +252,13 @@ static const struct perf_pmu_test_event sys_ccn_pmu_r= ead_cycles =3D { .event =3D { .name =3D "sys_ccn_pmu.read_cycles", .event =3D "config=3D0x2c", - .desc =3D "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ", + .desc =3D "ccn read-cycles event. Unit: uncore_sys_ccn_pmu", .topic =3D "uncore", .pmu =3D "uncore_sys_ccn_pmu", .compat =3D "0x01", }, .alias_str =3D "config=3D0x2c", - .alias_long_desc =3D "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ", + .alias_long_desc =3D "ccn read-cycles event. Unit: uncore_sys_ccn_pmu", .matching_pmu =3D "uncore_sys_ccn_pmu", }; =20 @@ -403,7 +409,7 @@ static int test__pmu_event_table_core_callback(const st= ruct pmu_event *pe, struct perf_pmu_test_event const **test_event_table; bool found =3D false; =20 - if (pe->pmu) + if (strcmp(pe->pmu, "cpu")) test_event_table =3D &uncore_events[0]; else test_event_table =3D &core_events[0]; --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFBA3EE4993 for ; Wed, 23 Aug 2023 10:16:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233940AbjHWKQC (ORCPT ); Wed, 23 Aug 2023 06:16:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233736AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30307170F for ; Wed, 23 Aug 2023 01:09:05 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c647150c254so10208571276.1 for ; Wed, 23 Aug 2023 01:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778144; x=1693382944; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=PD4Z1HSbPqI+PYDaeSuUcOyp8wCv3Hn6s79y+f2o9yA=; b=5Swm+ynQycOaHY2H3lvYnF5DCgbMCPp2Sv3OWE7qagiXgBCCe1boVE0WGq7nxQvXzj CNn4/Mi646rmZ5jdcfeOMnz+GzOD0RfaQplNuvosFY19OtuHo7JH5+Pq04S0x+QGfoOx /t8MT1BODTjIZnhFR0tnqucNCgXv3Jgfti30qWbWtnItV99BuNvd2QROwAFv/1tpd2fS hMriiLHHyevQS4UxahO9SboMcaXdyk3PQ888uvXfQZkEJvoDIcF0CrJI1q2KLudzmavK hAmsz7IFjIa8bltOi8moEOm6mZiloDrrYmYjgqUj+ExFzlL52mxHv5xKy77xXeYxoOAN qIdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778144; x=1693382944; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PD4Z1HSbPqI+PYDaeSuUcOyp8wCv3Hn6s79y+f2o9yA=; b=bywtLkK6vM7btBp+ylI7ShynBsUx1ybn2SHu20dHcuVlCN37Wx5iCxKOR0aPrkHyAq QvMWE1dpY/SGbpnc+QhBJjCIlG4aR/ubMAAzF2Hgrpf9RwhrAFRvpgDdwaQgfoQEYMFD tK+K/OrLwnGGqdSPrYrebSMkD6vrzJTZEfU/Blcfb6RADSyFEc/JKVd9cYnMbmv1e5FW Ogbp9iyLI+tGj63/lDtHkl5iwT2huGM/oc7mZ9jgrB9eR3+o7oFzcwtKqcfSbGpViUBv lo6g47CdqF2BTeBJJ3IqYNgVr+KGscIn5QeCdkmhmlJf90BTOL7/cbOEY55oOOpuJo2M P9CA== X-Gm-Message-State: AOJu0YwYQniRyH7RHAsO4x7nM8KyrEBVCIWEDvnJwwY7r6UgPTzxtEAm 4inN/qivuEQLM0IBs6Tf1x3+WOwbOeVL X-Google-Smtp-Source: AGHT+IFnVPI8k6B5G+QH3e0Bj9Dymv4VB/FhRNCGu7sBVEICr0moxZQsYBAXUeR9HMtreYF/f10tZbQYtHKi X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:566:b0:d15:53b5:509f with SMTP id a6-20020a056902056600b00d1553b5509fmr185925ybt.2.1692778144480; Wed, 23 Aug 2023 01:09:04 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:16 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-14-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 13/25] perf parse-events: Improve error message for double setting From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Double setting information for an event would produce an error message associated with the PMU rather than the term that was double setting. Improve the error message to be on the term. Before: ``` $ perf stat -e 'cpu/inst_retired.any,inst_retired.any/' true event syntax error: 'cpu/inst_retired.any,inst_retired.any/' \___ Bad event or PMU Unabled to find PMU or event on a PMU of 'cpu' Run 'perf list' for a list of valid events ``` After: ``` $ perf stat -e 'cpu/inst_retired.any,inst_retired.any/' true event syntax error: '..etired.any,inst_retired.any/' \___ Bad event or PMU Unabled to find PMU or event on a PMU of 'cpu' Initial error: event syntax error: '..etired.any,inst_retired.any/' \___ Attempt to set event's scale twice Run 'perf list' for a list of valid events ``` Signed-off-by: Ian Rogers --- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 34 +++++++++++++++++++++++++++------- tools/perf/util/pmu.h | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 7cad82a9f578..781747bedc3e 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1348,7 +1348,7 @@ int parse_events_add_pmu(struct parse_events_state *p= arse_state, return evsel ? 0 : -ENOMEM; } =20 - if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &in= fo)) + if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &in= fo, err)) return -EINVAL; =20 if (verbose > 1) { diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 95872bee28ac..0036e41f6baf 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1439,17 +1439,33 @@ static struct perf_pmu_alias *pmu_find_alias(struct= perf_pmu *pmu, =20 =20 static int check_info_data(struct perf_pmu_alias *alias, - struct perf_pmu_info *info) + struct perf_pmu_info *info, + struct parse_events_error *err, + int column) { /* * Only one term in event definition can * define unit, scale and snapshot, fail * if there's more than one. */ - if ((info->unit && alias->unit[0]) || - (info->scale && alias->scale) || - (info->snapshot && alias->snapshot)) + if (info->unit && alias->unit[0]) { + parse_events_error__handle(err, column, + strdup("Attempt to set event's unit twice"), + NULL); + return -EINVAL; + } + if (info->scale && alias->scale) { + parse_events_error__handle(err, column, + strdup("Attempt to set event's scale twice"), + NULL); + return -EINVAL; + } + if (info->snapshot && alias->snapshot) { + parse_events_error__handle(err, column, + strdup("Attempt to set event snapshot twice"), + NULL); return -EINVAL; + } =20 if (alias->unit[0]) info->unit =3D alias->unit; @@ -1468,7 +1484,7 @@ static int check_info_data(struct perf_pmu_alias *ali= as, * defined for the alias */ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, - struct perf_pmu_info *info) + struct perf_pmu_info *info, struct parse_events_error *err) { struct parse_events_term *term, *h; struct perf_pmu_alias *alias; @@ -1489,10 +1505,14 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, str= uct list_head *head_terms, if (!alias) continue; ret =3D pmu_alias_terms(alias, &term->list); - if (ret) + if (ret) { + parse_events_error__handle(err, term->err_term, + strdup("Failure to duplicate terms"), + NULL); return ret; + } =20 - ret =3D check_info_data(alias, info); + ret =3D check_info_data(alias, info, err, term->err_term); if (ret) return ret; =20 diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index f37e3d75094f..03211de345c1 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -185,7 +185,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu, __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name); int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_ter= ms, - struct perf_pmu_info *info); + struct perf_pmu_info *info, struct parse_events_error *err); int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *st= ate, pmu_event_callback cb); =20 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_loa= d); --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51263EE49AF for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234029AbjHWISS (ORCPT ); Wed, 23 Aug 2023 04:18:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233789AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAF881712 for ; Wed, 23 Aug 2023 01:09:07 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58cf42a3313so68004117b3.0 for ; Wed, 23 Aug 2023 01:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778147; x=1693382947; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=2BhHz8yEC3pLTHC5QxPQtooiXn41z8hQeFyuHW+faxU=; b=HU8CMnAAS/RziILAg1+V2MuiV7wt756cyZBZvpvQQBvwc4jKJgrWKYRZHwYUZSMSAo NmxcfyQGcfrwMnDoxtigjRv7aJi2XO1CuPwiPoAuIG+fJbvYpKHxRGRjoViIi4S86uqN NrBpUPbak9vXU12UBuWxSRiLArx0Dr+yaZ4TXr8ovBXdGTkcYVk3Rq4ozIfLAmkjnyJ0 R7RyTiXoci56hBq7QoUaHY6E3iVJwo8XVU8TxCN6zTurtsHZ03IuSaZpsZpXiC5fiO8g 2x2dCBXG0IcrqxVC7m/ScNLEo7Ax2EajwvjNAtI2cDV3txn8b8Jhr9d2mDfXaNcbTw9N cJCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778147; x=1693382947; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2BhHz8yEC3pLTHC5QxPQtooiXn41z8hQeFyuHW+faxU=; b=DVYTcb+AlGYsbi4K6zVYDVxbvSpBJ/7AmbAJlul2M3n6ec5pCadyueecEQygcQnqtW lbX0Xum9UNPomuvMcHq4zeSkMWKq1IfEA9Bq0Z62bFM5yZyAndCbHDnpDOgCPemoP3n5 7nYUXH3VZ/F+nLbMYWLyU2pfDLf8st2v4smrhJ7EGFZhyI71B6QupqYd8L0KQWPkHuH3 ZnLZeBjDXixovoZvnK6jzPx4eRCD/26pWw5t5LejurSZsoh4C1ZgwL30uUi4ejykBUZN qnlW35GiuDhlZFqwI2UsBjnvnoDaL7+OpfqEqQQzRw1cBsKrU1z4Cj5B2IG6a1tNJ2tL wKBA== X-Gm-Message-State: AOJu0Yxky7EuTCJblIVaDSzuPUkbLqe5wv6cgNqbXsjEQv3uw3FO9lsy vXyJ7MpCLJqBP+N9MDCSxdXHgMTNZikd X-Google-Smtp-Source: AGHT+IHvEGLSuiJK2HNeck85ZiiHP2D3xStux0XexeC+vrw08E08DpocMMf1g+zZgw+G5QzS6QJtJ0hHQQFS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:af0e:0:b0:58c:6ddd:d27c with SMTP id n14-20020a81af0e000000b0058c6dddd27cmr154296ywh.6.1692778147209; Wed, 23 Aug 2023 01:09:07 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:17 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-15-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 14/25] perf s390 s390_cpumcfdg_dump: Don't scan all PMUs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than scanning all PMUs for a counter name, scan the PMU associated with the evsel of the sample. This is done to remove a dependence on pmu-events.h. Signed-off-by: Ian Rogers --- tools/perf/util/s390-sample-raw.c | 50 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/tools/perf/util/s390-sample-raw.c b/tools/perf/util/s390-sampl= e-raw.c index 91330c874170..dc1ed3e95d4d 100644 --- a/tools/perf/util/s390-sample-raw.c +++ b/tools/perf/util/s390-sample-raw.c @@ -27,7 +27,7 @@ #include "color.h" #include "sample-raw.h" #include "s390-cpumcf-kernel.h" -#include "pmu-events/pmu-events.h" +#include "util/pmu.h" #include "util/sample.h" =20 static size_t ctrset_size(struct cf_ctrset_entry *set) @@ -132,56 +132,57 @@ static int get_counterset_start(int setnr) =20 struct get_counter_name_data { int wanted; - const char *result; + char *result; }; =20 -static int get_counter_name_callback(const struct pmu_event *evp, - const struct pmu_events_table *table __maybe_unused, - void *vdata) +static int get_counter_name_callback(void *vdata, struct pmu_event_info *i= nfo) { struct get_counter_name_data *data =3D vdata; int rc, event_nr; + const char *event_str; =20 - if (evp->name =3D=3D NULL || evp->event =3D=3D NULL) + if (info->str =3D=3D NULL) return 0; - rc =3D sscanf(evp->event, "event=3D%x", &event_nr); + + event_str =3D strstr(info->str, "event=3D"); + if (!event_str) + return 0; + + rc =3D sscanf(event_str, "event=3D%x", &event_nr); if (rc =3D=3D 1 && event_nr =3D=3D data->wanted) { - data->result =3D evp->name; + data->result =3D strdup(info->name); return 1; /* Terminate the search. */ } return 0; } =20 -/* Scan the PMU table and extract the logical name of a counter from the - * PMU events table. Input is the counter set and counter number with in t= he - * set. Construct the event number and use this as key. If they match retu= rn - * the name of this counter. +/* Scan the PMU and extract the logical name of a counter from the event. = Input + * is the counter set and counter number with in the set. Construct the ev= ent + * number and use this as key. If they match return the name of this count= er. * If no match is found a NULL pointer is returned. */ -static const char *get_counter_name(int set, int nr, const struct pmu_even= ts_table *table) +static char *get_counter_name(int set, int nr, struct perf_pmu *pmu) { struct get_counter_name_data data =3D { .wanted =3D get_counterset_start(set) + nr, .result =3D NULL, }; =20 - if (!table) + if (!pmu) return NULL; =20 - pmu_events_table__for_each_event(table, get_counter_name_callback, &data); + perf_pmu__for_each_event(pmu, &data, get_counter_name_callback); return data.result; } =20 -static void s390_cpumcfdg_dump(struct perf_sample *sample) +static void s390_cpumcfdg_dump(struct perf_pmu *pmu, struct perf_sample *s= ample) { size_t i, len =3D sample->raw_size, offset =3D 0; unsigned char *buf =3D sample->raw_data; const char *color =3D PERF_COLOR_BLUE; struct cf_ctrset_entry *cep, ce; - const struct pmu_events_table *table; u64 *p; =20 - table =3D pmu_events_table__find(); while (offset < len) { cep =3D (struct cf_ctrset_entry *)(buf + offset); =20 @@ -199,11 +200,12 @@ static void s390_cpumcfdg_dump(struct perf_sample *sa= mple) color_fprintf(stdout, color, " [%#08zx] Counterset:%d" " Counters:%d\n", offset, ce.set, ce.ctr); for (i =3D 0, p =3D (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) { - const char *ev_name =3D get_counter_name(ce.set, i, table); + char *ev_name =3D get_counter_name(ce.set, i, pmu); =20 color_fprintf(stdout, color, "\tCounter:%03d %s Value:%#018lx\n", i, ev_name ?: "", be64_to_cpu(*p)); + free(ev_name); } offset +=3D ctrset_size(&ce); } @@ -216,14 +218,14 @@ static void s390_cpumcfdg_dump(struct perf_sample *sa= mple) */ void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *even= t, struct perf_sample *sample) { - struct evsel *ev_bc000; + struct evsel *evsel; =20 if (event->header.type !=3D PERF_RECORD_SAMPLE) return; =20 - ev_bc000 =3D evlist__event2evsel(evlist, event); - if (ev_bc000 =3D=3D NULL || - ev_bc000->core.attr.config !=3D PERF_EVENT_CPUM_CF_DIAG) + evsel =3D evlist__event2evsel(evlist, event); + if (evsel =3D=3D NULL || + evsel->core.attr.config !=3D PERF_EVENT_CPUM_CF_DIAG) return; =20 /* Display raw data on screen */ @@ -231,5 +233,5 @@ void evlist__s390_sample_raw(struct evlist *evlist, uni= on perf_event *event, str pr_err("Invalid counter set data encountered\n"); return; } - s390_cpumcfdg_dump(sample); + s390_cpumcfdg_dump(evsel->pmu, sample); } --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 633EDEE49B0 for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234120AbjHWISa (ORCPT ); Wed, 23 Aug 2023 04:18:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233790AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 223061BE for ; Wed, 23 Aug 2023 01:09:10 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d7496b91389so6212718276.1 for ; Wed, 23 Aug 2023 01:09:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778149; x=1693382949; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=hTctRXvCXjE3QcZJ1hsp4OH2mXVNnHQp5p4RQcP/Djg=; b=5ifKsNQnfZySr7XTQldnZ06K/BDGMqawhA7oZp0g+3d94s42d+D/ueA+DTYpu05zVy hbIO+BdaZiG6lYL6JYtXfaK7yiUdbxdEr8X9JP+2haRp00oxRxZzry/oy3/fesyIfX/w 8unhuvM5QFScUResmxDUtqzBJd9SMkK9vmA1HsumR2GGhLp+Xx3kV9dl2rqzqGGLyB4K Bod9T4fRvJbuFI8xyu2pyaLpNgWUKEW3OpPqYMj9hbdR8pVcZp4QN4eb1q0ZL46wCwhI zJf+aTskW65PxLTqtNjkYmK14RmQY1b/fV4iNlUv2qWbjkGkERIK+IVyaA4f8pj3FXIL zthg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778149; x=1693382949; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hTctRXvCXjE3QcZJ1hsp4OH2mXVNnHQp5p4RQcP/Djg=; b=KLisBnYXsBa1qOKdrUf7y9RSJkqfcsAxMow/9s36thUP3n2Gc6m+4W9sYAQTmCSzdr LkRxNUlfp9xDAwH+4Oj8dKBssIK2RWKOy6+QprFpgZ4GdqvoWh7WDu29YJf58ZRSca5c 2k6aYJLOb5EoUj3d9G1SeFIg6jzgm6Z2ayXLxeqbdfwHnO0hjxavXsDzQkT8NuDk7IKI WR9LWyWQ6A4CayL3LZg8eOZlY+lH45C5sUMzPRnxGRK1yesHdk9uImykujS7nJOSAzLd k4BFVxJXYTE7x2uLLNHnY7afEyBF5CxIcVjZngEAxZd5VMn+VxRAam2kQ6T/15Am4FXB bcSA== X-Gm-Message-State: AOJu0YxQ6Skvb4vNvKiGz4/sA9HS3OJgkVByR6c2A3tv9CFv5i+z+9cy FbhwJXIR/GX+pJMnb8kAdR3kPyHC8RWq X-Google-Smtp-Source: AGHT+IEXOY3eyUqMIhYlpovQXqhyextxMTfaS7NEseV+dMLLIZwG/jnQ+fqU5QGb1WycaoOp+2ltz2eIwsUV X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:7483:0:b0:d72:a54d:4ab with SMTP id p125-20020a257483000000b00d72a54d04abmr205011ybc.3.1692778149424; Wed, 23 Aug 2023 01:09:09 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:18 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-16-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 15/25] perf pmu-events: Reduce processed events by passing PMU From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass the PMU to pmu_events_table__for_each_event so that entries that don't match don't need to be processed by callback. If a NULL PMU is passed then all PMUs are processed. perf bench internals pmu-scan "Average PMU scanning" performance is reduced by about 5% on an Intel tigerlake. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 15 ++++++---- tools/perf/pmu-events/jevents.py | 18 ++++++++---- tools/perf/pmu-events/pmu-events.h | 4 ++- tools/perf/tests/pmu-events.c | 6 ++-- tools/perf/util/pmu.c | 35 +++++++----------------- tools/perf/util/pmu.h | 1 + 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 807f2e55c17c..2d6f748280ac 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -266,12 +266,16 @@ static const struct pmu_sys_events pmu_sys_event_tabl= es[] =3D { }, }; =20 -int pmu_events_table__for_each_event(const struct pmu_events_table *table,= pmu_event_iter_fn fn, - void *data) +int pmu_events_table__for_each_event(const struct pmu_events_table *table,= struct perf_pmu *pmu, + pmu_event_iter_fn fn, void *data) { for (const struct pmu_event *pe =3D &table->entries[0]; pe->name; pe++) { - int ret =3D fn(pe, table, data); + int ret; =20 + if (pmu && !pmu__name_match(pmu, pe->pmu)) + continue; + + ret =3D fn(pe, table, data); if (ret) return ret; } @@ -371,7 +375,8 @@ const struct pmu_metrics_table *find_core_metrics_table= (const char *arch, const int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data) { for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->= arch; tables++) { - int ret =3D pmu_events_table__for_each_event(&tables->event_table, fn, d= ata); + int ret =3D pmu_events_table__for_each_event(&tables->event_table, + /*pmu=3D*/ NULL, fn, data); =20 if (ret) return ret; @@ -408,7 +413,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *= data) for (const struct pmu_sys_events *tables =3D &pmu_sys_event_tables[0]; tables->name; tables++) { - int ret =3D pmu_events_table__for_each_event(&tables->table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->table, /*pmu=3D*/ = NULL, fn, data); =20 if (ret) return ret; diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 1ad20140114c..396af53e0e45 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -826,14 +826,20 @@ static int pmu_events_table__for_each_event_pmu(const= struct pmu_events_table *t } =20 int pmu_events_table__for_each_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, pmu_event_iter_fn fn, void *data) { for (size_t i =3D 0; i < table->num_pmus; i++) { - int ret =3D pmu_events_table__for_each_event_pmu(table, &t= able->pmus[i], - fn, data); + const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; + const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; + int ret; =20 - if (ret) + if (pmu && !pmu__name_match(pmu, pmu_name)) + continue; + + ret =3D pmu_events_table__for_each_event_pmu(table, table_= pmu, fn, data); + if (pmu || ret) return ret; } return 0; @@ -955,7 +961,8 @@ int pmu_for_each_core_event(pmu_event_iter_fn fn, void = *data) for (const struct pmu_events_map *tables =3D &pmu_events_map[0]; tables->arch; tables++) { - int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, + /*pmu=3D*/ NULL= , fn, data); =20 if (ret) return ret; @@ -992,7 +999,8 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *= data) for (const struct pmu_sys_events *tables =3D &pmu_sys_event_tables= [0]; tables->name; tables++) { - int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, fn, data); + int ret =3D pmu_events_table__for_each_event(&tables->even= t_table, + /*pmu=3D*/ NULL= , fn, data); =20 if (ret) return ret; diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index 6557381b7de1..c0303ba42e97 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -77,7 +77,9 @@ typedef int (*pmu_metric_iter_fn)(const struct pmu_metric= *pm, const struct pmu_metrics_table *table, void *data); =20 -int pmu_events_table__for_each_event(const struct pmu_events_table *table,= pmu_event_iter_fn fn, +int pmu_events_table__for_each_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, + pmu_event_iter_fn fn, void *data); int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data); diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 0b6efabc3d20..92d1f6f0e666 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -483,12 +483,14 @@ static int test__pmu_event_table(struct test_suite *t= est __maybe_unused, if (!table || !sys_event_table) return -1; =20 - err =3D pmu_events_table__for_each_event(table, test__pmu_event_table_cor= e_callback, + err =3D pmu_events_table__for_each_event(table, /*pmu=3D*/ NULL, + test__pmu_event_table_core_callback, &map_events); if (err) return err; =20 - err =3D pmu_events_table__for_each_event(sys_event_table, test__pmu_event= _table_sys_callback, + err =3D pmu_events_table__for_each_event(sys_event_table, /*pmu=3D*/ NULL, + test__pmu_event_table_sys_callback, &map_events); if (err) return err; diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 0036e41f6baf..284962c133b3 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -855,28 +855,14 @@ static bool pmu_uncore_alias_match(const char *pmu_na= me, const char *name) return res; } =20 -struct pmu_add_cpu_aliases_map_data { - /* List being added to. */ - struct list_head *head; - /* If a pmu_event lacks a given PMU the default used. */ - char *default_pmu_name; - /* The PMU that we're searching for events for. */ - struct perf_pmu *pmu; -}; - static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, const struct pmu_events_table *table __maybe_unused, void *vdata) { - struct pmu_add_cpu_aliases_map_data *data =3D vdata; - const char *pname =3D pe->pmu ?: data->default_pmu_name; + struct list_head *head =3D vdata; =20 - if (!strcmp(pname, data->pmu->name) || - (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->pmu->nam= e))) { - /* need type casts to override 'const' */ - __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, - (char *)pe->event, pe); - } + /* need type casts to override 'const' */ + __perf_pmu__new_alias(head, -1, (char *)pe->name, (char *)pe->desc, (char= *)pe->event, pe); return 0; } =20 @@ -886,14 +872,7 @@ static int pmu_add_cpu_aliases_map_callback(const stru= ct pmu_event *pe, */ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_even= ts_table *table) { - struct pmu_add_cpu_aliases_map_data data =3D { - .head =3D &pmu->aliases, - .default_pmu_name =3D perf_pmus__default_pmu_name(), - .pmu =3D pmu, - }; - - pmu_events_table__for_each_event(table, pmu_add_cpu_aliases_map_callback,= &data); - free(data.default_pmu_name); + pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_call= back, &pmu->aliases); } =20 static void pmu_add_cpu_aliases(struct perf_pmu *pmu) @@ -1709,6 +1688,12 @@ int perf_pmu__for_each_event(const struct perf_pmu *= pmu, void *state, pmu_event_ return ret; } =20 +bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name) +{ + return !strcmp(pmu->name, pmu_name) || + (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)); +} + bool perf_pmu__is_software(const struct perf_pmu *pmu) { if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 03211de345c1..2b1730152bc0 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -198,6 +198,7 @@ bool perf_pmu__auto_merge_stats(const struct perf_pmu *= pmu); bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name); size_t perf_pmu__num_events(const struct perf_pmu *pmu); int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_= event_callback cb); +bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name); =20 /** * perf_pmu_is_software - is the PMU a software PMU as in it uses the --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24909EE4993 for ; Wed, 23 Aug 2023 10:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233843AbjHWKPu (ORCPT ); Wed, 23 Aug 2023 06:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233788AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F9DE5D for ; Wed, 23 Aug 2023 01:09:12 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-591138c0978so44186507b3.1 for ; Wed, 23 Aug 2023 01:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778152; x=1693382952; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=thiR0red1e/TKgvXPJgbac9qLA8eyvIz0R2RRqFEpHo=; b=XFUd9593BS1mAF6tiAGLbyXxPCSXxxxdjfNyfOYhijHjpeRcmzdiwAxB33DlKG+yNh PweK+NrBxYkJjUjZzi3y+/Ga1msXD2Id0hFG/Tw2x19I8qM0NCcKz4ZhNZW2L+2WRQJD 5o5H5fuMczruUtj0vjmBpewVES4knxsQxsSnCGv2IyZ5tmJU1nHPWaMf9hV04q31ckiu CznQDyUiCBc2l/Fkp8bhGgQIllS4ROlBJayBFaFWd7tmq/EsRaIsQSdmHTHqO8huqO5C 9BQrSmUn1L98iyPRGe3a16/lQUlWuegbVaDOKKcl3dF/jOyi59aKfFUQ0m1t+ZnAP33O aNBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778152; x=1693382952; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=thiR0red1e/TKgvXPJgbac9qLA8eyvIz0R2RRqFEpHo=; b=bDXID0UWkJVzirrOVKUMx7rdRqajFRskJ/t/Y0erpdxgj4Y+PNpFyCug6aILFNgrwE boqEJt4MUle39uT3QJ0PjnmCzogxlwx7e9dDw1Eauj6qF3OIX3i4v0LtEDuiq1zVUcEP GxAT3Tm1k8aF2uZ1WuGC+gu0IMOmDZHVIHDiYWa5toTTOzEYnGeUpWXA2j+rfZ3M4ppN WNUeGY4u6fK9AqDUKnz60+BZWGYiVOcmsg7Egr75QtGZOd8ktCRsOdh99mDuw9eHUZ9Z BN9ZvBw049flIuBnGjrljlEZBJ72fWfFrCHJHWAhygjtxJwL6qMl8lvbRug7W8ZeMbSk 20Ug== X-Gm-Message-State: AOJu0YyAil7nVn9cli8Wqw0s4kpo3jm94uDVr+1o5UZoIvSEsVAPhhEj j7qhBKw28WSdsNyVUxzNXfJqdVtRvbpR X-Google-Smtp-Source: AGHT+IE8MQVpauvtosSVTTVSAyjLiGbK63QXSjLEmXyjtzfEsdupSKb4Iedwyu+OPz6HeNQoyeTezcF9ZHQS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:ac5a:0:b0:586:4fbc:4367 with SMTP id z26-20020a81ac5a000000b005864fbc4367mr157506ywj.10.1692778151937; Wed, 23 Aug 2023 01:09:11 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:19 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-17-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 16/25] perf pmu-events: Add pmu_events_table__find_event From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" jevents stores events sorted by name. Add a find function that will binary search event names avoiding the need to linearly search through events. Add a test in tests/pmu-events.c. If the PMU or event aren't found -1000 is returned. If the event is found but no callback function given, 0 is returned. This allows the find function also act as a test for existence. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 16 ++++++ tools/perf/pmu-events/jevents.py | 64 ++++++++++++++++++++++++ tools/perf/pmu-events/pmu-events.h | 5 ++ tools/perf/tests/pmu-events.c | 5 ++ 4 files changed, 90 insertions(+) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 2d6f748280ac..ef18d403f25f 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -282,6 +282,22 @@ int pmu_events_table__for_each_event(const struct pmu_= events_table *table, struc return 0; } =20 +int pmu_events_table__find_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, + const char *name, + pmu_event_iter_fn fn, + void *data) +{ + for (const struct pmu_event *pe =3D &table->entries[0]; pe->name; pe++) { + if (pmu && !pmu__name_match(pmu, pe->pmu)) + continue; + + if (!strcasecmp(pe->name, name)) + return fn(pe, table, data); + } + return -1000; +} + int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) { diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 396af53e0e45..991fcf6cca64 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -825,6 +825,49 @@ static int pmu_events_table__for_each_event_pmu(const = struct pmu_events_table *t return 0; } =20 +static int pmu_events_table__find_event_pmu(const struct pmu_events_table = *table, + const struct pmu_table_entry *= pmu, + const char *name, + pmu_event_iter_fn fn, + void *data) +{ + struct pmu_event pe =3D { + .pmu =3D &big_c_string[pmu->pmu_name.offset], + }; + int low =3D 0, high =3D pmu->num_entries - 1; + + while (low <=3D high) { + int cmp, mid =3D (low + high) / 2; + + decompress_event(pmu->entries[mid].offset, &pe); + + if (!pe.name && !name) + goto do_call; + + if (!pe.name && name) { + low =3D mid + 1; + continue; + } + if (pe.name && !name) { + high =3D mid - 1; + continue; + } + + cmp =3D strcasecmp(pe.name, name); + if (cmp < 0) { + low =3D mid + 1; + continue; + } + if (cmp > 0) { + high =3D mid - 1; + continue; + } + do_call: + return fn ? fn(&pe, table, data) : 0; + } + return -1000; +} + int pmu_events_table__for_each_event(const struct pmu_events_table *table, struct perf_pmu *pmu, pmu_event_iter_fn fn, @@ -845,6 +888,27 @@ int pmu_events_table__for_each_event(const struct pmu_= events_table *table, return 0; } =20 +int pmu_events_table__find_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, + const char *name, + pmu_event_iter_fn fn, + void *data) +{ + for (size_t i =3D 0; i < table->num_pmus; i++) { + const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; + const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; + int ret; + + if (!pmu__name_match(pmu, pmu_name)) + continue; + + ret =3D pmu_events_table__find_event_pmu(table, table_pmu,= name, fn, data); + if (ret !=3D -1000) + return ret; + } + return -1000; +} + static int pmu_metrics_table__for_each_metric_pmu(const struct pmu_metrics= _table *table, const struct pmu_table_ent= ry *pmu, pmu_metric_iter_fn fn, diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index c0303ba42e97..9882b7125761 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -81,6 +81,11 @@ int pmu_events_table__for_each_event(const struct pmu_ev= ents_table *table, struct perf_pmu *pmu, pmu_event_iter_fn fn, void *data); +int pmu_events_table__find_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, + const char *name, + pmu_event_iter_fn fn, + void *data); int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data); =20 diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 92d1f6f0e666..34f0de182fa9 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -546,6 +546,11 @@ static int __test_core_pmu_event_aliases(char *pmu_nam= e, int *count) =20 pmu_add_cpu_aliases_table(pmu, table); =20 + res =3D pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NUL= L, NULL); + if (res !=3D 0) { + pr_debug("Missing test event in test architecture"); + return res; + } for (; *test_event_table; test_event_table++) { struct perf_pmu_test_event test_event =3D **test_event_table; struct pmu_event const *event =3D &test_event.event; --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47FD3EE49B0 for ; Wed, 23 Aug 2023 10:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231351AbjHWKP4 (ORCPT ); Wed, 23 Aug 2023 06:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233794AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FE1B1720 for ; Wed, 23 Aug 2023 01:09:15 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d74c58a3dd7so3103513276.0 for ; Wed, 23 Aug 2023 01:09:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778154; x=1693382954; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=tqUDw45wGk5wmr8fIVvlAS7pZKUjU6uqt9APs5CQJVc=; b=qAm/aD6Z6Ljdyyrmm++JzGbt1r/95IixuTi7ANNYuGV1cUcbDq7vIVQUDAOrt1bnYY Xz2KPn1qgtcjduzR5JnEtMgBfkxfUwjyIQnuH3pjTv2xR9lC38hRr+kbdpx3+/fCZHtT U3kjwbYsBiyp5TUEIxhtn9VYYjFk4R6/q3zYnwlH7k7SAjWmvWiAPj8A25T5koGfrVaS reJeXCQciXPy55c8DozAMyYWVjgNaPvFZWLTKv1BOXQb6BABVoJZXfgNOaVHc/v5tRfc 8lzWoehcWUiiheSszlIxRwGJIzdXK11RaWxTspX28AQ8TrEkc3PWRNfito1TlCrkURqe 2XKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778154; x=1693382954; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tqUDw45wGk5wmr8fIVvlAS7pZKUjU6uqt9APs5CQJVc=; b=YHoH16YIzTnuwPh24R/esFVJ9s0ejtrByMzquGHW10jwF2KP7aUMMS9dEjDi+hC/Bk +eXH/pa62doyxfZO6y9x8u3Wu1UgV38KWuEkewOwRyA2jdZU7pGs/pxn0Wku+DLhkE9J 4vxaa+Vvljh6d5rdIp2KzswImH9L21qzxMjVWwwMCSu9ZMYur36Iuh0rkE1F3j5txYt5 pKhbFAfugBLRne6g2N14CRCWWo3rEvYPvmvkO4Uxxgdh9jrSJM5nUFXabZAFAgC/1rfo i6MSBiGrOWARDsTkVLhAK31WalkuUsgNRKVGPdieJEcVU0EIscefompYpAM7+bwnQ4gR eqkA== X-Gm-Message-State: AOJu0Yxijw+ifZARkIJ7Stydo9nfSJaGKduDF47TZ8QHJja/dF4Na+wT eb3YBBspw6nO3ehgcN48YpFnSYbHModd X-Google-Smtp-Source: AGHT+IEkFt4zRg0YzBMzij0vo1EgVVOdMaEe7xJEgiOwgjnFiCLfEgUQjLjA/MUyuB3Q1Lvv+YMYT0PZbypS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:ab26:0:b0:d77:9722:8eda with SMTP id u35-20020a25ab26000000b00d7797228edamr68927ybi.4.1692778154591; Wed, 23 Aug 2023 01:09:14 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:20 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-18-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 17/25] perf pmu: Parse sysfs events directly from a file From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than read a sysfs events file into a 256 byte char buffer, pass the FILE* directly to the lex/yacc parser. This avoids there being a maximum events file size. While changing the API, constify some arguments to remove unnecessary casts. Allocating the read buffer decreases the performance of pmu-scan by around 3%. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/intel-pt.c | 2 +- tools/perf/tests/parse-events.c | 2 +- tools/perf/util/parse-events.c | 18 +++++++---- tools/perf/util/parse-events.h | 3 +- tools/perf/util/pmu.c | 48 ++++++++++------------------- 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index 27944c15ac14..31807791589e 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -74,7 +74,7 @@ static int intel_pt_parse_terms_with_default(struct perf_= pmu *pmu, =20 INIT_LIST_HEAD(terms); =20 - err =3D parse_events_terms(terms, str); + err =3D parse_events_terms(terms, str, /*input=3D*/ NULL); if (err) goto out_free; =20 diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-event= s.c index 658fb9599d95..d86076d575ed 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c @@ -2472,7 +2472,7 @@ static int test_term(const struct terms_test *t) =20 INIT_LIST_HEAD(&terms); =20 - ret =3D parse_events_terms(&terms, t->str); + ret =3D parse_events_terms(&terms, t->str, /*input=3D*/ NULL); if (ret) { pr_debug("failed to parse terms '%s', err %d\n", t->str , ret); diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 781747bedc3e..c71e135d7335 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1715,6 +1715,7 @@ int parse_events_name(struct list_head *list, const c= har *name) } =20 static int parse_events__scanner(const char *str, + FILE *input, struct parse_events_state *parse_state) { YY_BUFFER_STATE buffer; @@ -1725,7 +1726,10 @@ static int parse_events__scanner(const char *str, if (ret) return ret; =20 - buffer =3D parse_events__scan_string(str, scanner); + if (str) + buffer =3D parse_events__scan_string(str, scanner); + else + parse_events_set_in(input, scanner); =20 #ifdef PARSER_DEBUG parse_events_debug =3D 1; @@ -1733,8 +1737,10 @@ static int parse_events__scanner(const char *str, #endif ret =3D parse_events_parse(parse_state, scanner); =20 - parse_events__flush_buffer(buffer, scanner); - parse_events__delete_buffer(buffer, scanner); + if (str) { + parse_events__flush_buffer(buffer, scanner); + parse_events__delete_buffer(buffer, scanner); + } parse_events_lex_destroy(scanner); return ret; } @@ -1742,7 +1748,7 @@ static int parse_events__scanner(const char *str, /* * parse event config string, return a list of event terms. */ -int parse_events_terms(struct list_head *terms, const char *str) +int parse_events_terms(struct list_head *terms, const char *str, FILE *inp= ut) { struct parse_events_state parse_state =3D { .terms =3D NULL, @@ -1750,7 +1756,7 @@ int parse_events_terms(struct list_head *terms, const= char *str) }; int ret; =20 - ret =3D parse_events__scanner(str, &parse_state); + ret =3D parse_events__scanner(str, input, &parse_state); =20 if (!ret) { list_splice(parse_state.terms, terms); @@ -2001,7 +2007,7 @@ int __parse_events(struct evlist *evlist, const char = *str, const char *pmu_filte }; int ret, ret2; =20 - ret =3D parse_events__scanner(str, &parse_state); + ret =3D parse_events__scanner(str, /*input=3D*/ NULL, &parse_state); =20 if (!ret && list_empty(&parse_state.list)) { WARN_ONCE(true, "WARNING: event parser found nothing\n"); diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 411f69b2ac3a..c7f779420723 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -9,6 +9,7 @@ #include #include #include +#include #include =20 struct evsel; @@ -42,7 +43,7 @@ static inline int parse_events(struct evlist *evlist, con= st char *str, =20 int parse_event(struct evlist *evlist, const char *str); =20 -int parse_events_terms(struct list_head *terms, const char *str); +int parse_events_terms(struct list_head *terms, const char *str, FILE *inp= ut); int parse_filter(const struct option *opt, const char *str, int unset); int exclude_perf(const struct option *opt, const char *arg, int unset); =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 284962c133b3..6db14967ded9 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -276,7 +276,7 @@ int perf_pmu__convert_scale(const char *scale, char **e= nd, double *sval) return ret; } =20 -static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, = char *name) +static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, = const char *name) { struct stat st; ssize_t sret; @@ -308,7 +308,7 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias = *alias, int dirfd, char * return ret; } =20 -static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, c= har *name) +static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, c= onst char *name) { char path[PATH_MAX]; ssize_t sret; @@ -339,7 +339,7 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias *= alias, int dirfd, char *n } =20 static int -perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *nam= e) +perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, const cha= r *name) { char path[PATH_MAX]; int fd; @@ -357,7 +357,7 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, i= nt dirfd, char *name) } =20 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, - int dirfd, char *name) + int dirfd, const char *name) { char path[PATH_MAX]; int fd; @@ -450,8 +450,9 @@ static bool perf_pmu_merge_alias(struct perf_pmu_alias = *newalias, return false; } =20 -static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *= name, - char *desc, char *val, const struct pmu_event *pe) +static int perf_pmu__new_alias(struct list_head *list, int dirfd, const ch= ar *name, + const char *desc, const char *val, FILE *val_fd, + const struct pmu_event *pe) { struct parse_events_term *term; struct perf_pmu_alias *alias; @@ -480,7 +481,7 @@ static int __perf_pmu__new_alias(struct list_head *list= , int dirfd, char *name, alias->snapshot =3D false; alias->deprecated =3D deprecated; =20 - ret =3D parse_events_terms(&alias->terms, val); + ret =3D parse_events_terms(&alias->terms, val, val_fd); if (ret) { pr_err("Cannot parse alias %s: %d\n", val, ret); free(alias); @@ -537,23 +538,6 @@ static int __perf_pmu__new_alias(struct list_head *lis= t, int dirfd, char *name, return 0; } =20 -static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *na= me, FILE *file) -{ - char buf[256]; - int ret; - - ret =3D fread(buf, 1, sizeof(buf), file); - if (ret =3D=3D 0) - return -EINVAL; - - buf[ret] =3D 0; - - /* Remove trailing newline from sysfs file */ - strim(buf); - - return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL); -} - static inline bool pmu_alias_info_file(char *name) { size_t len; @@ -609,7 +593,8 @@ static int pmu_aliases_parse(int dirfd, struct list_hea= d *head) continue; } =20 - if (perf_pmu__new_alias(head, dirfd, name, file) < 0) + if (perf_pmu__new_alias(head, dirfd, name, /*desc=3D*/ NULL, + /*val=3D*/ NULL, file, /*pe=3D*/ NULL) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); } @@ -862,7 +847,7 @@ static int pmu_add_cpu_aliases_map_callback(const struc= t pmu_event *pe, struct list_head *head =3D vdata; =20 /* need type casts to override 'const' */ - __perf_pmu__new_alias(head, -1, (char *)pe->name, (char *)pe->desc, (char= *)pe->event, pe); + perf_pmu__new_alias(head, -1, pe->name, pe->desc, pe->event, /*val_fd=3D*= / NULL, pe); return 0; } =20 @@ -903,11 +888,12 @@ static int pmu_add_sys_aliases_iter_fn(const struct p= mu_event *pe, =20 if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - __perf_pmu__new_alias(idata->head, -1, - (char *)pe->name, - (char *)pe->desc, - (char *)pe->event, - pe); + perf_pmu__new_alias(idata->head, -1, + pe->name, + pe->desc, + pe->event, + /*val_fd=3D*/ NULL, + pe); } =20 return 0; --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 891EBEE4993 for ; Wed, 23 Aug 2023 10:16:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232575AbjHWKQK (ORCPT ); Wed, 23 Aug 2023 06:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233713AbjHWIKp (ORCPT ); Wed, 23 Aug 2023 04:10:45 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF1761722 for ; Wed, 23 Aug 2023 01:09:17 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d71f505d21dso6364804276.3 for ; Wed, 23 Aug 2023 01:09:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778157; x=1693382957; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=PBddR/dgewe8GYO0FrkatqTRkhbux9YJuNBE1TDZZC0=; b=oziN7b6v5AfOnHzKPspObBatkOujx0BnpIlhrQJjeaf0TmwdEkszLP6tax+sjFW20d 2YuJTAmLZc5IyVKF8NTcp320rtRYcUmQJcwTyTJQo7vFBCfWVSiwkoGY97qK5vN4+KDZ jGoeAiFMCl4k+NfJb84YnDvpwgQw9F2AWgl8XOxgaLFDO/Je3iqv0Qsx6XBwSsaoNMTc NwIBT9EwANSQlcHNs6FF1zEpxH112+SFmqj2MoTa1ASzE43aUJBvY1t5ZMlkvL1GNEX3 1fg9awA0/hXvI+SHg00x4qXqzk8kLqPTSooGqFjyCoTjyaoS/tHHqaKRA8qp1Q2CkZwJ 6ymQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778157; x=1693382957; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PBddR/dgewe8GYO0FrkatqTRkhbux9YJuNBE1TDZZC0=; b=YAivrT/jaRG3rnrVgd63gIzIcP+eUPnvhq1XLj8e8pw0dxgXYuicwz8Ri0MYn7FkQK 0J+tvhRDcuWMpzruXRF2I7CkIrT2e5bW139E6SZaoO6XuIeWFqGACiUo3o6/LkVBtwCr 7PbLwQF+wiPDgttt4pZvJ/27ocf3l1KaaoD6rzDG43O5Q9DhLWCzag7kh+6mfaIuvuyk nAxIAX236y57lTtOYt6YA/bWq20tfDzbmS1ocH663Dh6bVmCqO5GnitafcEwSvAU7gOO 50duaPC7vjJFxwtNQTJfGsqwMZqXn+E1+PaChvUUB+KwqwrPdpVBlUYrevzd+kY9wjef OJZQ== X-Gm-Message-State: AOJu0Yw4spW1wrRho8Tw0JwhH/CEGW6NFErIXFP3uKliGsrmNxuV9Ep7 ToxClIzzAp4azcat4egFFpalnb/eWKqB X-Google-Smtp-Source: AGHT+IGsvh02i20oTWovc7RDuu05QsrjrwbHMB9S3aGw8G43nY8jhiQG+e0qNu5wNDADshfI0YdsG9U6Ekom X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:25d7:0:b0:d4b:99ce:5e51 with SMTP id l206-20020a2525d7000000b00d4b99ce5e51mr128199ybl.6.1692778157056; Wed, 23 Aug 2023 01:09:17 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:21 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-19-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 18/25] perf pmu: Prefer passing pmu to aliases list From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The aliases list is part of the PMU. Rather than pass the aliases list, pass the full PMU simplifying some callbacks. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 44 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 6db14967ded9..069530b2f79e 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -431,12 +431,12 @@ static void perf_pmu__del_aliases(struct perf_pmu *pm= u) /* Merge an alias, search in alias list. If this name is already * present merge both of them to combine all information. */ -static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, - struct list_head *alist) +static bool perf_pmu_merge_alias(struct perf_pmu *pmu, + struct perf_pmu_alias *newalias) { struct perf_pmu_alias *a; =20 - list_for_each_entry(a, alist, list) { + list_for_each_entry(a, &pmu->aliases, list) { if (!strcasecmp(newalias->name, a->name)) { if (newalias->pmu_name && a->pmu_name && !strcasecmp(newalias->pmu_name, a->pmu_name)) { @@ -450,7 +450,7 @@ static bool perf_pmu_merge_alias(struct perf_pmu_alias = *newalias, return false; } =20 -static int perf_pmu__new_alias(struct list_head *list, int dirfd, const ch= ar *name, +static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char= *name, const char *desc, const char *val, FILE *val_fd, const struct pmu_event *pe) { @@ -532,8 +532,8 @@ static int perf_pmu__new_alias(struct list_head *list, = int dirfd, const char *na alias->str =3D strdup(newval); alias->pmu_name =3D pmu_name ? strdup(pmu_name) : NULL; =20 - if (!perf_pmu_merge_alias(alias, list)) - list_add_tail(&alias->list, list); + if (!perf_pmu_merge_alias(pmu, alias)) + list_add_tail(&alias->list, &pmu->aliases); =20 return 0; } @@ -559,7 +559,7 @@ static inline bool pmu_alias_info_file(char *name) * Process all the sysfs attributes located under the directory * specified in 'dir' parameter. */ -static int pmu_aliases_parse(int dirfd, struct list_head *head) +static int pmu_aliases_parse(struct perf_pmu *pmu, int dirfd) { struct dirent *evt_ent; DIR *event_dir; @@ -593,7 +593,7 @@ static int pmu_aliases_parse(int dirfd, struct list_hea= d *head) continue; } =20 - if (perf_pmu__new_alias(head, dirfd, name, /*desc=3D*/ NULL, + if (perf_pmu__new_alias(pmu, dirfd, name, /*desc=3D*/ NULL, /*val=3D*/ NULL, file, /*pe=3D*/ NULL) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); @@ -616,7 +616,7 @@ static int pmu_aliases(struct perf_pmu *pmu, int dirfd,= const char *name) return 0; =20 /* it'll close the fd */ - if (pmu_aliases_parse(fd, &pmu->aliases)) + if (pmu_aliases_parse(pmu, fd)) return -1; =20 return 0; @@ -844,10 +844,9 @@ static int pmu_add_cpu_aliases_map_callback(const stru= ct pmu_event *pe, const struct pmu_events_table *table __maybe_unused, void *vdata) { - struct list_head *head =3D vdata; + struct perf_pmu *pmu =3D vdata; =20 - /* need type casts to override 'const' */ - perf_pmu__new_alias(head, -1, pe->name, pe->desc, pe->event, /*val_fd=3D*= / NULL, pe); + perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event, /*val_fd=3D*/= NULL, pe); return 0; } =20 @@ -857,7 +856,7 @@ static int pmu_add_cpu_aliases_map_callback(const struc= t pmu_event *pe, */ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_even= ts_table *table) { - pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_call= back, &pmu->aliases); + pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_call= back, pmu); } =20 static void pmu_add_cpu_aliases(struct perf_pmu *pmu) @@ -871,24 +870,18 @@ static void pmu_add_cpu_aliases(struct perf_pmu *pmu) pmu_add_cpu_aliases_table(pmu, table); } =20 -struct pmu_sys_event_iter_data { - struct list_head *head; - struct perf_pmu *pmu; -}; - static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, const struct pmu_events_table *table __maybe_unused, - void *data) + void *vdata) { - struct pmu_sys_event_iter_data *idata =3D data; - struct perf_pmu *pmu =3D idata->pmu; + struct perf_pmu *pmu =3D vdata; =20 if (!pe->compat || !pe->pmu) return 0; =20 if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - perf_pmu__new_alias(idata->head, -1, + perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event, @@ -901,15 +894,10 @@ static int pmu_add_sys_aliases_iter_fn(const struct p= mu_event *pe, =20 void pmu_add_sys_aliases(struct perf_pmu *pmu) { - struct pmu_sys_event_iter_data idata =3D { - .head =3D &pmu->aliases, - .pmu =3D pmu, - }; - if (!pmu->id) return; =20 - pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); + pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu); } =20 struct perf_event_attr * __weak --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3396EE4993 for ; Wed, 23 Aug 2023 10:15:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233830AbjHWKPr (ORCPT ); Wed, 23 Aug 2023 06:15:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233835AbjHWIKy (ORCPT ); Wed, 23 Aug 2023 04:10:54 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C183172C for ; Wed, 23 Aug 2023 01:09:20 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7493303b5cso3899125276.2 for ; Wed, 23 Aug 2023 01:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778159; x=1693382959; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=dMP6WGJ4ddH4lGjG0xqZPHX/81hqViEaPRFSCM6GMJs=; b=LCiF7mKKBNCWe6UA5ZNJNsw+1D1U8GXTvTSEBdxde85gtj+RfhgcAXYkaHmPAx9mz/ phYopS/cZP93lFTVYVOlH7qHr8En6seH5gj3SBwxQWsLRgoaiQM8W3Q5Zxt9XEOFIrVv r8E1m4EmddEunYNWeDreg7RyJC747ySljj32nPOAqkGFDoeUPr/LkaDx4nGe68UIko38 2260GwXz5cIqCgo5sRmfaB8JNezwMSluKw9jyU9MVYl+a7kmwRdwzSExd6ROQEhuXjYJ 7id1lSWoOLSSR+hrTY8EjmNalA7yANqSsGMbagsiilVepKuy4aY0H5XeP7hLeHgg60PQ SZVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778159; x=1693382959; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dMP6WGJ4ddH4lGjG0xqZPHX/81hqViEaPRFSCM6GMJs=; b=i5gNXN00MtLjTJpx5aWwYZjP7Zr0TVWVkRmjZY1nCTlFnbrhi9pQ80BtN4jtIQMe5T 3zPtqOPApfTIL7I0h0in54cgxn5trbA1hnVlnIhvFMuqfE2jSo0bvxwD959+FYzcRmxW 8HThE6hFLLLE39j6EF4fmWOIgklLFyXP9ewkyj1gAraUD2YC5KGcDz/f67iRMnqZQ2TA HMk54EMua51P69SyW2c2rr3fiiqSHCL8af+sRj88vMTq5/2EqiCXnvIRYFh3H14jbKsW LURZbP52XoLfAgZWH0JVSqSwF7vgSADGuFC1/m+c7HSyMbYvyAkalpj04TNAWspzQkSc yjmw== X-Gm-Message-State: AOJu0YzxT7/R18EyWf+32ZTW7YtTKVAJXA6j2pinXhvy21fVDUQxKAMp dhNTmo3imM3NabvbviKYpRJegqc0lp/D X-Google-Smtp-Source: AGHT+IE8NW0Ccx50IbVZq4xCWAoUT4Cy0sveUPZPfyLvneZJNoaHik39B0zDUW5X2csBd2ICVI6kIZS1N6g9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:ce87:0:b0:d3a:f3a6:ee64 with SMTP id x129-20020a25ce87000000b00d3af3a6ee64mr122626ybe.5.1692778159677; Wed, 23 Aug 2023 01:09:19 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:22 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-20-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 19/25] perf pmu: Merge json events with sysfs at load time From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than load all sysfs events then parsing all json events and merging with ones that already exist. When a sysfs event is loaded, look for a corresponding json event and merge immediately. To simplify the logic, early exit the perf_pmu__new_alias function if an alias is attempted to be added twice - as merging has already been explicitly handled. Fix the copying of terms to a merged alias and some ENOMEM paths. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 177 +++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 89 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 069530b2f79e..a7d96e309843 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -373,38 +373,6 @@ static int perf_pmu__parse_snapshot(struct perf_pmu_al= ias *alias, return 0; } =20 -static void perf_pmu_assign_str(char *name, const char *field, char **old_= str, - char **new_str) -{ - if (!*old_str) - goto set_new; - - if (*new_str) { /* Have new string, check with old */ - if (strcasecmp(*old_str, *new_str)) - pr_debug("alias %s differs in field '%s'\n", - name, field); - zfree(old_str); - } else /* Nothing new --> keep old string */ - return; -set_new: - *old_str =3D *new_str; - *new_str =3D NULL; -} - -static void perf_pmu_update_alias(struct perf_pmu_alias *old, - struct perf_pmu_alias *newalias) -{ - perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); - perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, - &newalias->long_desc); - perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); - perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); - old->scale =3D newalias->scale; - old->per_pkg =3D newalias->per_pkg; - old->snapshot =3D newalias->snapshot; - memcpy(old->unit, newalias->unit, sizeof(old->unit)); -} - /* Delete an alias entry. */ static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) { @@ -428,26 +396,58 @@ static void perf_pmu__del_aliases(struct perf_pmu *pm= u) } } =20 -/* Merge an alias, search in alias list. If this name is already - * present merge both of them to combine all information. - */ -static bool perf_pmu_merge_alias(struct perf_pmu *pmu, - struct perf_pmu_alias *newalias) +static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *= pmu, const char *name) { - struct perf_pmu_alias *a; + struct perf_pmu_alias *alias; =20 - list_for_each_entry(a, &pmu->aliases, list) { - if (!strcasecmp(newalias->name, a->name)) { - if (newalias->pmu_name && a->pmu_name && - !strcasecmp(newalias->pmu_name, a->pmu_name)) { - continue; - } - perf_pmu_update_alias(a, newalias); - perf_pmu_free_alias(newalias); - return true; - } + list_for_each_entry(alias, &pmu->aliases, list) { + if (!strcasecmp(alias->name, name)) + return alias; } - return false; + return NULL; +} + +static bool assign_str(const char *name, const char *field, char **old_str, + const char *new_str) +{ + if (!*old_str && new_str) { + *old_str =3D strdup(new_str); + return true; + } + + if (!new_str || !strcasecmp(*old_str, new_str)) + return false; /* Nothing to update. */ + + pr_debug("alias %s differs in field '%s' ('%s' !=3D '%s')\n", + name, field, *old_str, new_str); + zfree(old_str); + *old_str =3D strdup(new_str); + return true; +} + +static int update_alias(const struct pmu_event *pe, + const struct pmu_events_table *table __maybe_unused, + void *vdata) +{ + struct perf_pmu_alias *alias =3D vdata; + int ret =3D 0; + + assign_str(pe->name, "desc", &alias->desc, pe->desc); + assign_str(pe->name, "long_desc", &alias->long_desc, pe->long_desc); + assign_str(pe->name, "topic", &alias->topic, pe->topic); + alias->per_pkg =3D pe->perpkg; + if (assign_str(pe->name, "value", &alias->str, pe->event)) { + parse_events_terms__purge(&alias->terms); + ret =3D parse_events_terms(&alias->terms, pe->event, /*input=3D*/NULL); + } + if (!ret && pe->unit) { + char *unit; + + ret =3D perf_pmu__convert_scale(pe->unit, &unit, &alias->scale); + if (!ret) + snprintf(alias->unit, sizeof(alias->unit), "%s", unit); + } + return ret; } =20 static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char= *name, @@ -461,6 +461,11 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name const char *long_desc =3D NULL, *topic =3D NULL, *unit =3D NULL, *pmu_nam= e =3D NULL; bool deprecated =3D false, perpkg =3D false; =20 + if (perf_pmu__find_alias(pmu, name)) { + /* Alias was already created/loaded. */ + return 0; + } + if (pe) { long_desc =3D pe->long_desc; topic =3D pe->topic; @@ -488,27 +493,6 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name return ret; } =20 - /* Scan event and remove leading zeroes, spaces, newlines, some - * platforms have terms specified as - * event=3D0x0091 (read from files ..//events/ - * and terms specified as event=3D0x91 (read from JSON files). - * - * Rebuild string to make alias->str member comparable. - */ - memset(newval, 0, sizeof(newval)); - ret =3D 0; - list_for_each_entry(term, &alias->terms, list) { - if (ret) - ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, - ","); - if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_NUM) - ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, - "%s=3D%#x", term->config, term->val.num); - else if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR) - ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, - "%s=3D%s", term->config, term->val.str); - } - alias->name =3D strdup(name); if (dirfd >=3D 0) { /* @@ -524,17 +508,43 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, = int dirfd, const char *name alias->long_desc =3D long_desc ? strdup(long_desc) : desc ? strdup(desc) : NULL; alias->topic =3D topic ? strdup(topic) : NULL; + alias->pmu_name =3D pmu_name ? strdup(pmu_name) : NULL; if (unit) { - if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) + if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) { + perf_pmu_free_alias(alias); return -1; + } snprintf(alias->unit, sizeof(alias->unit), "%s", unit); } - alias->str =3D strdup(newval); - alias->pmu_name =3D pmu_name ? strdup(pmu_name) : NULL; + if (!pe) { + /* Update an event from sysfs with json data. */ + const struct pmu_events_table *table =3D perf_pmu__find_events_table(pmu= ); =20 - if (!perf_pmu_merge_alias(pmu, alias)) - list_add_tail(&alias->list, &pmu->aliases); + if (table) + pmu_events_table__find_event(table, pmu, name, update_alias, alias); + } =20 + /* Scan event and remove leading zeroes, spaces, newlines, some + * platforms have terms specified as + * event=3D0x0091 (read from files ..//events/ + * and terms specified as event=3D0x91 (read from JSON files). + * + * Rebuild string to make alias->str member comparable. + */ + ret =3D 0; + list_for_each_entry(term, &alias->terms, list) { + if (ret) + ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, + ","); + if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_NUM) + ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, + "%s=3D%#x", term->config, term->val.num); + else if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR) + ret +=3D scnprintf(newval + ret, sizeof(newval) - ret, + "%s=3D%s", term->config, term->val.str); + } + alias->str =3D strdup(newval); + list_add_tail(&alias->list, &pmu->aliases); return 0; } =20 @@ -940,6 +950,9 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmu= s, int dirfd, const char INIT_LIST_HEAD(&pmu->format); INIT_LIST_HEAD(&pmu->aliases); INIT_LIST_HEAD(&pmu->caps); + pmu->name =3D strdup(name); + if (!pmu->name) + goto err; /* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right @@ -958,9 +971,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmu= s, int dirfd, const char } pmu->is_core =3D is_pmu_core(name); pmu->cpus =3D pmu_cpumask(dirfd, name, pmu->is_core); - pmu->name =3D strdup(name); - if (!pmu->name) - goto err; =20 /* Read type, and ensure that type value is successfully assigned (return= 1) */ if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) !=3D 1) @@ -1353,17 +1363,6 @@ int perf_pmu__config(struct perf_pmu *pmu, struct pe= rf_event_attr *attr, return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); } =20 -static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *= pmu, const char *str) -{ - struct perf_pmu_alias *alias; - - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcasecmp(alias->name, str)) - return alias; - } - return NULL; -} - static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, struct parse_events_term *term) { --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10F4EEE4993 for ; Wed, 23 Aug 2023 10:15:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233806AbjHWKPn (ORCPT ); Wed, 23 Aug 2023 06:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233821AbjHWILH (ORCPT ); Wed, 23 Aug 2023 04:11:07 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA760CED for ; Wed, 23 Aug 2023 01:09:22 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d6412374defso6537738276.0 for ; Wed, 23 Aug 2023 01:09:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778162; x=1693382962; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=tXvCFhL984TX2oQ47qDb/BYaP1EU6FGu4C4k2bcoWxA=; b=d2ctvFcr4XNnEwl476cUwp8Ir8ybnDC28SJSGMpFfm3HWQyw6DeVHNNKOF2qZSfQL/ SFfKkvhanOKg/MhxxNwtbs5iHXcg6yQz+3K1BR/jH7TbgPHZ5W2aDSWuynYNLUENf4vM hdVSpYlpMjLCEwCOP6A88bPMTrLIU4KT1tM95Hq2GQNL5d28EMRcpTQ7vS+PY4QIy5VS Hr+tG9hvmXo+TwCpR/kov9shGLDR4zVThP9yDDLo7ipKpky3dybPMVywktOfFvBBaLLc 8ZwUDltEXVcQ/MNI1LYIZ7Pkf5QOoLXL0lGKRg0ZWmTgQvLq2KRmrGH7QX+u4e/JxafI AMDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778162; x=1693382962; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tXvCFhL984TX2oQ47qDb/BYaP1EU6FGu4C4k2bcoWxA=; b=HZyKz7dQ6JTTUXJ5maExSBzhsuf94rU4Ozle8mehHcvogkzO8pFp3zTd1XfmfP8oXD AIRrbtzTZUTfJ1k9ARIzm5TYbOmCeXCjHZYDPKr7+8nrG2fbn/AGEn26YL49pU5S0yfB 3+M7Z5Bu61W2Cs2mZVuBP5+r2xPcZqbhnTr4RzhyxzPCfXPOTbHEniT8JaHnospYR3Pj iuxjMCl0eL5pvfp5NTOvn19d9LKH+X9RYpIJXa5SHoFHjalqy5zINvuucZKvPqYnzNU5 6MifnC5aDQj0rLZQ3krn7ZrH/kj8jw1l7MHpuD+tXk2d/lVXxi/zODnmW2BqsJ8Sv0ps 7mUg== X-Gm-Message-State: AOJu0YwjX7UVhBPQaWuGnOmskm7IlgS5LgDjl0sp3bbE4bWJXjIUpMoo LiZT20U9NV3EAo6S+ul63vqzhyc7RWhC X-Google-Smtp-Source: AGHT+IHXFsqtea8U7dSOIUqSWapJui7ri3nA09iWxLIct/mbtTiUjfIR5ZaEbwTJ9gFdLqnPC3azMTOFhCFQ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:12c6:b0:d77:bcce:eb11 with SMTP id j6-20020a05690212c600b00d77bcceeb11mr37382ybu.10.1692778162082; Wed, 23 Aug 2023 01:09:22 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:23 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-21-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 20/25] perf pmu: Cache json events table From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Cache the json events table so that finding it isn't done per event/alias. Change the events table find so that when the PMU is given, if the PMU has no json events return null. Update usage to always use the PMU variable. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/jevents.py | 14 ++++++++++++-- tools/perf/tests/pmu-events.c | 2 ++ tools/perf/util/pmu.c | 17 ++++++++--------- tools/perf/util/pmu.h | 4 ++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 991fcf6cca64..f07864fabd54 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -948,7 +948,7 @@ const struct pmu_events_table *perf_pmu__find_events_ta= ble(struct perf_pmu *pmu) { const struct pmu_events_table *table =3D NULL; char *cpuid =3D perf_pmu__getcpuid(pmu); - int i; + size_t i; =20 /* on some platforms which uses cpus map, cpuid can be NULL for * PMUs other than CORE PMUs. @@ -968,7 +968,17 @@ const struct pmu_events_table *perf_pmu__find_events_t= able(struct perf_pmu *pmu) } } free(cpuid); - return table; + if (!pmu) + return table; + + for (i =3D 0; i < table->num_pmus; i++) { + const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; + const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; + + if (pmu__name_match(pmu, pmu_name)) + return table; + } + return NULL; } =20 const struct pmu_metrics_table *perf_pmu__find_metrics_table(struct perf_p= mu *pmu) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 34f0de182fa9..4fcb84fd1f65 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -544,6 +544,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name= , int *count) INIT_LIST_HEAD(&pmu->list); pmu->name =3D strdup(pmu_name); =20 + pmu->events_table =3D table; pmu_add_cpu_aliases_table(pmu, table); =20 res =3D pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NUL= L, NULL); @@ -583,6 +584,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) events_table =3D find_core_events_table("testarch", "testcpu"); if (!events_table) return -1; + pmu->events_table =3D events_table; pmu_add_cpu_aliases_table(pmu, events_table); pmu_add_sys_aliases(pmu); =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index a7d96e309843..dabae2019bd0 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -518,10 +518,10 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, = int dirfd, const char *name } if (!pe) { /* Update an event from sysfs with json data. */ - const struct pmu_events_table *table =3D perf_pmu__find_events_table(pmu= ); - - if (table) - pmu_events_table__find_event(table, pmu, name, update_alias, alias); + if (pmu->events_table) { + pmu_events_table__find_event(pmu->events_table, pmu, name, + update_alias, alias); + } } =20 /* Scan event and remove leading zeroes, spaces, newlines, some @@ -871,13 +871,10 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, = const struct pmu_events_tab =20 static void pmu_add_cpu_aliases(struct perf_pmu *pmu) { - const struct pmu_events_table *table; - - table =3D perf_pmu__find_events_table(pmu); - if (!table) + if (!pmu->events_table) return; =20 - pmu_add_cpu_aliases_table(pmu, table); + pmu_add_cpu_aliases_table(pmu, pmu->events_table); } =20 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, @@ -988,6 +985,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmu= s, int dirfd, const char if (pmu->is_uncore) pmu->id =3D pmu_id(name); pmu->max_precise =3D pmu_max_precise(dirfd, pmu); + pmu->events_table =3D perf_pmu__find_events_table(pmu); pmu_add_cpu_aliases(pmu); pmu_add_sys_aliases(pmu); list_add_tail(&pmu->list, pmus); @@ -1001,6 +999,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pm= us, int dirfd, const char return NULL; } =20 + /* Creates the PMU when sysfs scanning fails. */ struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *c= ore_pmus) { diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 2b1730152bc0..6bf0fbde4e85 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -114,6 +114,10 @@ struct perf_pmu { * from json events in pmu-events.c. */ struct list_head aliases; + /** + * @events_table: The events table for json events in pmu-events.c. + */ + const struct pmu_events_table *events_table; /** @caps_initialized: Has the list caps been initialized? */ bool caps_initialized; /** @nr_caps: The length of the list caps. */ --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6378EE49B0 for ; Wed, 23 Aug 2023 10:15:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233825AbjHWKPf (ORCPT ); Wed, 23 Aug 2023 06:15:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233677AbjHWILc (ORCPT ); Wed, 23 Aug 2023 04:11:32 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93821E72 for ; Wed, 23 Aug 2023 01:09:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d74a012e6a6so3645583276.2 for ; Wed, 23 Aug 2023 01:09:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778165; x=1693382965; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=eBOzUn7kVci/f1ahcDivuamI46IHq10R7u54lzmBAPU=; b=IJSDaIcKFqNAEj4/KIUcavGeF0CnQEdrBrXhtZOra6WXuy6CwIcMxSexfrznViOFlR iXTpOV0ibacY3lRGHKXD+RCcs2XcWG2NdFqHR1PgtduhiF2NCgNUwt6dfGgwGdRE+aoo kEvTkq4yvA1TZMTo5j1C8hSgyFgVKkmnH9drbFMPtqgRIFk5V0P0f+WJ+S2HjHAUdOQ6 NEwj/rBSoO/9DJWAIVgjkao89ac7w6vR344qfjOvmtpPjh/APQi7OCT9GLee+DDtFhY7 xInnVZUXJOvqU9rJ58/Rijhw0tedL7tRKJaK6eWOeNtsx0PLqyADYFK55USOgI4i8y2i HX7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778165; x=1693382965; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eBOzUn7kVci/f1ahcDivuamI46IHq10R7u54lzmBAPU=; b=FSz8ZOK8EtIaPSfqLB1CPewJOcZciWyA98WoMwggu/OZ51/RNMlsSUbVyJOBOKKW0q gDZ2SZhMwWl8dITmOI5DViYmpQp6bpFY2CLGeBnw83PBo2PijNbYSGrGHtajsPoDy1dY dPMgDcnMkqK1A8sovy/tJi8IZG0Fp0/9lDAVDfmiH8e0i/qH4apNh3yq2GvmNUjZJPvb D1n09yzG24OJfGUaGOGgiaqNn9rI6JULEkRLFGDgxIs5p6o3Qv3WS0nKC0XbEHBahgx0 JQEfpN+rb62oIsuovSdEPG6o1ple2Q4e5rG2zBnoiJYdVbxqMlu5+QZ9P6blrTgh6uA3 6j5Q== X-Gm-Message-State: AOJu0Ywo/H0QjVYwWaZQBvN+CL4AC7/DChvxUjDk+eJP9xE6eKdTHWX4 hTQUJCYuZ/jYauYoRA2ty2oQpQdP918f X-Google-Smtp-Source: AGHT+IEO8BuEoHXgZz7PBKUmwF4RjHrASpuqs/bXkO7Zbrhl9XrQAy4w/XWl4BRycEi5RrvKilwspuRKQBKc X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a05:6902:12c6:b0:d77:bcce:eb11 with SMTP id j6-20020a05690212c600b00d77bcceeb11mr37384ybu.10.1692778164548; Wed, 23 Aug 2023 01:09:24 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:24 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-22-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 21/25] perf pmu: Lazily add json events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than scanning all json events and adding them when a PMU is created, add the alias when the json event is needed. Average core PMU scanning run time reduced by 60.2%. Average PMU scanning run time reduced by 15%. Page faults with no events reduced by 74 page faults, 4% of total. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 14 +++++++ tools/perf/pmu-events/jevents.py | 15 +++++++ tools/perf/pmu-events/pmu-events.h | 4 ++ tools/perf/tests/pmu-events.c | 2 + tools/perf/util/pmu.c | 50 ++++++++++++++++++------ tools/perf/util/pmu.h | 15 +++++-- 6 files changed, 85 insertions(+), 15 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index ef18d403f25f..12bd043a05e3 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -298,6 +298,20 @@ int pmu_events_table__find_event(const struct pmu_even= ts_table *table, return -1000; } =20 +size_t pmu_events_table__num_events(const struct pmu_events_table *table, + struct perf_pmu *pmu) +{ + size_t count =3D 0; + + for (const struct pmu_event *pe =3D &table->entries[0]; pe->name; pe++) { + if (pmu && !pmu__name_match(pmu, pe->pmu)) + continue; + + count++; + } + return count; +} + int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) { diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index f07864fabd54..01335a452e70 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -909,6 +909,21 @@ int pmu_events_table__find_event(const struct pmu_even= ts_table *table, return -1000; } =20 +size_t pmu_events_table__num_events(const struct pmu_events_table *table, + struct perf_pmu *pmu) +{ + size_t count =3D 0; + + for (size_t i =3D 0; i < table->num_pmus; i++) { + const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; + const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; + + if (pmu__name_match(pmu, pmu_name)) + count +=3D table_pmu->num_entries; + } + return count; +} + static int pmu_metrics_table__for_each_metric_pmu(const struct pmu_metrics= _table *table, const struct pmu_table_ent= ry *pmu, pmu_metric_iter_fn fn, diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index 9882b7125761..f5aa96f1685c 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -3,6 +3,7 @@ #define PMU_EVENTS_H =20 #include +#include =20 struct perf_pmu; =20 @@ -86,6 +87,9 @@ int pmu_events_table__find_event(const struct pmu_events_= table *table, const char *name, pmu_event_iter_fn fn, void *data); +size_t pmu_events_table__num_events(const struct pmu_events_table *table, + struct perf_pmu *pmu); + int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data); =20 diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 4fcb84fd1f65..9ac893ae5f0d 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -546,6 +546,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name= , int *count) =20 pmu->events_table =3D table; pmu_add_cpu_aliases_table(pmu, table); + pmu->cpu_aliases_added =3D true; =20 res =3D pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NUL= L, NULL); if (res !=3D 0) { @@ -586,6 +587,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) return -1; pmu->events_table =3D events_table; pmu_add_cpu_aliases_table(pmu, events_table); + pmu->cpu_aliases_added =3D true; pmu_add_sys_aliases(pmu); =20 /* Count how many aliases we generated */ diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index dabae2019bd0..81a40fe02d65 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -519,8 +519,9 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, in= t dirfd, const char *name if (!pe) { /* Update an event from sysfs with json data. */ if (pmu->events_table) { - pmu_events_table__find_event(pmu->events_table, pmu, name, - update_alias, alias); + if (pmu_events_table__find_event(pmu->events_table, pmu, name, + update_alias, alias) =3D=3D 0) + pmu->loaded_json_aliases++; } } =20 @@ -544,6 +545,10 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name "%s=3D%s", term->config, term->val.str); } alias->str =3D strdup(newval); + if (!pe) + pmu->sysfs_aliases++; + else + pmu->loaded_json_aliases++; list_add_tail(&alias->list, &pmu->aliases); return 0; } @@ -874,7 +879,11 @@ static void pmu_add_cpu_aliases(struct perf_pmu *pmu) if (!pmu->events_table) return; =20 + if (pmu->cpu_aliases_added) + return; + pmu_add_cpu_aliases_table(pmu, pmu->events_table); + pmu->cpu_aliases_added =3D true; } =20 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, @@ -986,7 +995,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmu= s, int dirfd, const char pmu->id =3D pmu_id(name); pmu->max_precise =3D pmu_max_precise(dirfd, pmu); pmu->events_table =3D perf_pmu__find_events_table(pmu); - pmu_add_cpu_aliases(pmu); pmu_add_sys_aliases(pmu); list_add_tail(&pmu->list, pmus); =20 @@ -1365,6 +1373,7 @@ int perf_pmu__config(struct perf_pmu *pmu, struct per= f_event_attr *attr, static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, struct parse_events_term *term) { + struct perf_pmu_alias *alias; char *name; =20 if (parse_events__is_hardcoded_term(term)) @@ -1385,7 +1394,18 @@ static struct perf_pmu_alias *pmu_find_alias(struct = perf_pmu *pmu, return NULL; } =20 - return perf_pmu__find_alias(pmu, name); + alias =3D perf_pmu__find_alias(pmu, name); + if (alias || pmu->cpu_aliases_added) + return alias; + + /* Alias doesn't exist, try to get it from the json events. */ + if (pmu->events_table && + pmu_events_table__find_event(pmu->events_table, pmu, name, + pmu_add_cpu_aliases_map_callback, + pmu) =3D=3D 0) { + alias =3D perf_pmu__find_alias(pmu, name); + } + return alias; } =20 =20 @@ -1552,18 +1572,23 @@ bool perf_pmu__auto_merge_stats(const struct perf_p= mu *pmu) return !pmu->is_core || perf_pmus__num_core_pmus() =3D=3D 1; } =20 -bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name) +bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) { - return perf_pmu__find_alias(pmu, name) !=3D NULL; + if (perf_pmu__find_alias(pmu, name) !=3D NULL) + return true; + if (pmu->cpu_aliases_added || !pmu->events_table) + return false; + return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, N= ULL) =3D=3D 0; } =20 -size_t perf_pmu__num_events(const struct perf_pmu *pmu) +size_t perf_pmu__num_events(struct perf_pmu *pmu) { - struct list_head *list; - size_t nr =3D 0; + size_t nr =3D pmu->sysfs_aliases; =20 - list_for_each(list, &pmu->aliases) - nr++; + if (pmu->cpu_aliases_added) + nr +=3D pmu->loaded_json_aliases; + else if (pmu->events_table) + nr +=3D pmu_events_table__num_events(pmu->events_table, pmu) - pmu->load= ed_json_aliases; =20 return pmu->selectable ? nr + 1 : nr; } @@ -1601,7 +1626,7 @@ static char *format_alias(char *buf, int len, const s= truct perf_pmu *pmu, return buf; } =20 -int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_= event_callback cb) +int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_= callback cb) { char buf[1024]; struct perf_pmu_alias *event; @@ -1610,6 +1635,7 @@ int perf_pmu__for_each_event(const struct perf_pmu *p= mu, void *state, pmu_event_ }; int ret =3D 0; =20 + pmu_add_cpu_aliases(pmu); list_for_each_entry(event, &pmu->aliases, list) { size_t buf_used; =20 diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 6bf0fbde4e85..288d2908382a 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -118,6 +118,15 @@ struct perf_pmu { * @events_table: The events table for json events in pmu-events.c. */ const struct pmu_events_table *events_table; + /** @sysfs_aliases: Number of sysfs aliases loaded. */ + uint32_t sysfs_aliases; + /** @sysfs_aliases: Number of json event aliases loaded. */ + uint32_t loaded_json_aliases; + /** + * @cpu_aliases_added: Have all json events table entries for the PMU + * been added? + */ + bool cpu_aliases_added; /** @caps_initialized: Has the list caps been initialized? */ bool caps_initialized; /** @nr_caps: The length of the list caps. */ @@ -199,9 +208,9 @@ bool perf_pmu__has_format(const struct perf_pmu *pmu, c= onst char *name); bool is_pmu_core(const char *name); bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu); bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu); -bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name); -size_t perf_pmu__num_events(const struct perf_pmu *pmu); -int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_= event_callback cb); +bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name); +size_t perf_pmu__num_events(struct perf_pmu *pmu); +int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_= callback cb); bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name); =20 /** --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A66CEE49B1 for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234458AbjHWIU0 (ORCPT ); Wed, 23 Aug 2023 04:20:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233692AbjHWILe (ORCPT ); Wed, 23 Aug 2023 04:11:34 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6057D173B for ; Wed, 23 Aug 2023 01:09:28 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d7475f45d31so4664916276.0 for ; Wed, 23 Aug 2023 01:09:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778167; x=1693382967; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=cLOHuaoioxUIPfraGI/RscnO6tlIZddOURBBsgyssoE=; b=Qz42NKTTx6zTspcQcJfd5SPnOrPnd5c00ZQ5+EY52NaVZ0hc3YTjcUVBHf0sfbvLMB ddz3sWDQdyNJneq6OddnEy5ajtknBPl/4FAIK8+S7mEKv1VLLEnxZhCLtDeoLwX7bntT tEP6+qxwhJSe6D08LX5LRtMwUth0drK1B8ITajpkP8ULjjvSiHlW7B+sVsju6oocKIt2 zo6mHAfG/5qPIzXHGBWEwh4b78lxuExssZN+rwTcOUMwZasaDuchoRPODZe8Vy+nbSrN cpBJ1/4809OB7f6y74q/YD3soph+v61YnjKubT/oyhZo2nD2tPJnF2i79mmMLLzhl0tH lkUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778167; x=1693382967; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cLOHuaoioxUIPfraGI/RscnO6tlIZddOURBBsgyssoE=; b=N5gQq1Srwz8mOIXg/u9jGK4JXqO1WmNYegMPQ9H1s8TWtYUG3/6AsVMQ73vz4QvRZd wc5qmrZWLnERpxdUihXaBG2d5t6y43ecJCEXNPagnkOjG08D5cNb1Bl1uFobgDMr5hz/ 7hhLIwc0QWV8BSVE0/9Z7Bzk00Wj/sSKcloisLV3pCk2sMyLK51HpmW3nusT/iPD3z2V inhQP099FscCLDgS8mYUw9EVst8nIYTY0edEV2JoPAEixj0alElPLDwxwDyJNzhKtIbk 6qLOeIgXztfE8wOpOoTnOAeaO4ThVtfejOLhZS8+6ChoswYyW/8TJCpY5nVtuDEPAUVu b9kw== X-Gm-Message-State: AOJu0YzljKA+xuCKTr2dd9Kc5e6qnD1gnkKmK8B5wUieVJ6TC+X1rWxA 022CLrZT3y+JYslzLldC/4OrM7ngQdgB X-Google-Smtp-Source: AGHT+IEe9e7MnCLgVp65s/JZqoRZX2GWP1p1t6uwoZhQMx0RQHxlMSCM5JVnoCRQRuIKliPIlA+3zuOPa1UA X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:d2c4:0:b0:d47:5cc3:9917 with SMTP id j187-20020a25d2c4000000b00d475cc39917mr149953ybg.9.1692778167623; Wed, 23 Aug 2023 01:09:27 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:25 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-23-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 22/25] perf pmu: Scan type early to fail an invalid PMU quickly From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Scan sysfs PMU's type early so that format and aliases aren't attempted to be loaded if the PMU name is invalid. This is the case for event_pmu tokens in parse-events.y where a wildcard name is first assumed to be a PMU name. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 81a40fe02d65..b4ba4f7f5e18 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -953,12 +953,21 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *p= mus, int dirfd, const char if (!pmu) return NULL; =20 - INIT_LIST_HEAD(&pmu->format); - INIT_LIST_HEAD(&pmu->aliases); - INIT_LIST_HEAD(&pmu->caps); pmu->name =3D strdup(name); if (!pmu->name) goto err; + + /* + * Read type early to fail fast if a lookup name isn't a PMU. Ensure + * that type value is successfully assigned (return 1). + */ + if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) !=3D 1) + goto err; + + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->aliases); + INIT_LIST_HEAD(&pmu->caps); + /* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right @@ -978,10 +987,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pm= us, int dirfd, const char pmu->is_core =3D is_pmu_core(name); pmu->cpus =3D pmu_cpumask(dirfd, name, pmu->is_core); =20 - /* Read type, and ensure that type value is successfully assigned (return= 1) */ - if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) !=3D 1) - goto err; - alias_name =3D pmu_find_alias_name(name); if (alias_name) { pmu->alias_name =3D strdup(alias_name); @@ -1007,7 +1012,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *p= mus, int dirfd, const char return NULL; } =20 - /* Creates the PMU when sysfs scanning fails. */ struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *c= ore_pmus) { --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC5B2EE49A0 for ; Wed, 23 Aug 2023 08:22:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234474AbjHWIU4 (ORCPT ); Wed, 23 Aug 2023 04:20:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233695AbjHWILe (ORCPT ); Wed, 23 Aug 2023 04:11:34 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DFDE173D for ; Wed, 23 Aug 2023 01:09:31 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d7496b91389so6213121276.1 for ; Wed, 23 Aug 2023 01:09:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778170; x=1693382970; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=sO0nlbdYRpBXpPj2SCUtbtEV/g4IZLNC6VGcNaYE9js=; b=YVD+4vBEFiQzf4kVYZvEraiaWkZF5WarI2tHI4PfXpfRPLSCo9GX2y3E+LHTZ2RdYT fFNJ17mYrIRSaqkNUcDlKTYS7PF9ssMtGBOofKZQ/HU8TPn6VMHoq8qr4Y+fT8jHyk9y oSM++hKNJwP53jJml6aynN6Jrz0UQ4rTKEbDrtN4CcN4UaDIxfdM5HuodMqvZxOjP7vE 3U0oBdMQA3ZtgLMD6x8Ewua0UfjxXTT80GxJn/Mwsndog9G8eKzIcwEF4M9ECOi7kXWp /ccaBKCzhotlgcGL2HDFAHR21WvwtO+qTd1DtskHmGGkJHeJENRQ9/3ZHclAbuoog3I/ y3Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778170; x=1693382970; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sO0nlbdYRpBXpPj2SCUtbtEV/g4IZLNC6VGcNaYE9js=; b=ND/QPnS52LdgspSL60+CwBzyrXudjEnDGMU3dIu6v/wqogiHwHBcMGMndd/bHt4bZG LqkT22XeUYeo374ng3Zr9YtYw1wxf9w+pkUqYImzVLab8YKwuF+iaTGYu7YP0ZngL0Fc jwyeLH//sq+JRAXXpz0jmuXCfTXD8Hb7iS4q9oDfSwpNp/2VfOw6Qi0OOJDQ+rI/M/16 vPkm2+Bp3qTB8Qt0mIO1KuctMtrSTsfTRmQ9IZqNNgUO7s7VKzOy08Mdjft/a7qXBwQI vjrkOsElpLcmiFdnwpWDPD6dzuDB0TTbexzXbeZg8FRMP0NyNBf093yisyp2N916ETlW AZ1w== X-Gm-Message-State: AOJu0YyStavFlWKzDczTw0d0u18BwrCMy/z8eJy8OuVSOblV/7gT6l3/ Tj103RD9qNDexfVZC6hDifIL3y+iXzRT X-Google-Smtp-Source: AGHT+IFcw6ORSo1i+RD3UKL/v2KyU9JDq2+rO9G6maG86y0uRyKzVhVlAiKR8i2TVGGqsDP2IDAyxs2O5VQh X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a25:e68a:0:b0:d10:a33d:36b6 with SMTP id d132-20020a25e68a000000b00d10a33d36b6mr213223ybh.0.1692778170525; Wed, 23 Aug 2023 01:09:30 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:26 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-24-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 23/25] perf pmu: Be lazy about loading event info files from sysfs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Event info is only needed when an event is parsed or when merging data from an json and sysfs event. Be lazy in its loading to reduce file accesses. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 128 +++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 45 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index b4ba4f7f5e18..c547fe607f9f 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -58,6 +58,11 @@ struct perf_pmu_alias { struct list_head terms; /** @list: List element of struct perf_pmu aliases. */ struct list_head list; + /** + * @pmu_name: The name copied from the json struct pmu_event. This can + * differ from the PMU name as it won't have suffixes. + */ + char *pmu_name; /** @unit: Units for the event, such as bytes or cache lines. */ char unit[UNIT_MAX_LEN+1]; /** @scale: Value to scale read counter values by. */ @@ -79,11 +84,10 @@ struct perf_pmu_alias { * default. */ bool deprecated; - /** - * @pmu_name: The name copied from the json struct pmu_event. This can - * differ from the PMU name as it won't have suffixes. - */ - char *pmu_name; + /** @from_sysfs: Was the alias from sysfs or a json event? */ + bool from_sysfs; + /** @info_loaded: Have the scale, unit and other values been read from di= sk? */ + bool info_loaded; }; =20 /** @@ -276,17 +280,21 @@ int perf_pmu__convert_scale(const char *scale, char *= *end, double *sval) return ret; } =20 -static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, = const char *name) +static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_ali= as *alias) { struct stat st; ssize_t sret; + size_t len; char scale[128]; int fd, ret =3D -1; char path[PATH_MAX]; =20 - scnprintf(path, PATH_MAX, "%s.scale", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.scale", pmu->name, alias= ->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -308,15 +316,20 @@ static int perf_pmu__parse_scale(struct perf_pmu_alia= s *alias, int dirfd, const return ret; } =20 -static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, c= onst char *name) +static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alia= s *alias) { char path[PATH_MAX]; + size_t len; ssize_t sret; int fd; =20 - scnprintf(path, PATH_MAX, "%s.unit", name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.unit", pmu->name, alias-= >name); + + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -339,14 +352,18 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias= *alias, int dirfd, const c } =20 static int -perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, const cha= r *name) +perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias) { char path[PATH_MAX]; + size_t len; int fd; =20 - scnprintf(path, PATH_MAX, "%s.per-pkg", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.per-pkg", pmu->name, ali= as->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -356,15 +373,18 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias,= int dirfd, const char *nam return 0; } =20 -static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, - int dirfd, const char *name) +static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_= alias *alias) { char path[PATH_MAX]; + size_t len; int fd; =20 - scnprintf(path, PATH_MAX, "%s.snapshot", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.snapshot", pmu->name, al= ias->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -425,32 +445,52 @@ static bool assign_str(const char *name, const char *= field, char **old_str, return true; } =20 +static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *a= lias) +{ + if (!alias->from_sysfs || alias->info_loaded) + return; + + /* + * load unit name and scale if available + */ + perf_pmu__parse_unit(pmu, alias); + perf_pmu__parse_scale(pmu, alias); + perf_pmu__parse_per_pkg(pmu, alias); + perf_pmu__parse_snapshot(pmu, alias); +} + +struct update_alias_data { + struct perf_pmu *pmu; + struct perf_pmu_alias *alias; +}; + static int update_alias(const struct pmu_event *pe, const struct pmu_events_table *table __maybe_unused, void *vdata) { - struct perf_pmu_alias *alias =3D vdata; + struct update_alias_data *data =3D vdata; int ret =3D 0; =20 - assign_str(pe->name, "desc", &alias->desc, pe->desc); - assign_str(pe->name, "long_desc", &alias->long_desc, pe->long_desc); - assign_str(pe->name, "topic", &alias->topic, pe->topic); - alias->per_pkg =3D pe->perpkg; - if (assign_str(pe->name, "value", &alias->str, pe->event)) { - parse_events_terms__purge(&alias->terms); - ret =3D parse_events_terms(&alias->terms, pe->event, /*input=3D*/NULL); + read_alias_info(data->pmu, data->alias); + assign_str(pe->name, "desc", &data->alias->desc, pe->desc); + assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc); + assign_str(pe->name, "topic", &data->alias->topic, pe->topic); + data->alias->per_pkg =3D pe->perpkg; + if (assign_str(pe->name, "value", &data->alias->str, pe->event)) { + parse_events_terms__purge(&data->alias->terms); + ret =3D parse_events_terms(&data->alias->terms, pe->event, /*input=3D*/N= ULL); } if (!ret && pe->unit) { char *unit; =20 - ret =3D perf_pmu__convert_scale(pe->unit, &unit, &alias->scale); + ret =3D perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale); if (!ret) - snprintf(alias->unit, sizeof(alias->unit), "%s", unit); + snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit); } return ret; } =20 -static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char= *name, +static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name, const char *desc, const char *val, FILE *val_fd, const struct pmu_event *pe) { @@ -494,16 +534,6 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name } =20 alias->name =3D strdup(name); - if (dirfd >=3D 0) { - /* - * load unit name and scale if available - */ - perf_pmu__parse_unit(alias, dirfd, name); - perf_pmu__parse_scale(alias, dirfd, name); - perf_pmu__parse_per_pkg(alias, dirfd, name); - perf_pmu__parse_snapshot(alias, dirfd, name); - } - alias->desc =3D desc ? strdup(desc) : NULL; alias->long_desc =3D long_desc ? strdup(long_desc) : desc ? strdup(desc) : NULL; @@ -518,9 +548,15 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name } if (!pe) { /* Update an event from sysfs with json data. */ + struct update_alias_data data =3D { + .pmu =3D pmu, + .alias =3D alias, + }; + + alias->from_sysfs =3D true; if (pmu->events_table) { if (pmu_events_table__find_event(pmu->events_table, pmu, name, - update_alias, alias) =3D=3D 0) + update_alias, &data) =3D=3D 0) pmu->loaded_json_aliases++; } } @@ -608,7 +644,7 @@ static int pmu_aliases_parse(struct perf_pmu *pmu, int = dirfd) continue; } =20 - if (perf_pmu__new_alias(pmu, dirfd, name, /*desc=3D*/ NULL, + if (perf_pmu__new_alias(pmu, name, /*desc=3D*/ NULL, /*val=3D*/ NULL, file, /*pe=3D*/ NULL) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); @@ -861,7 +897,7 @@ static int pmu_add_cpu_aliases_map_callback(const struc= t pmu_event *pe, { struct perf_pmu *pmu =3D vdata; =20 - perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event, /*val_fd=3D*/= NULL, pe); + perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=3D*/ NUL= L, pe); return 0; } =20 @@ -897,7 +933,7 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu= _event *pe, =20 if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - perf_pmu__new_alias(pmu, -1, + perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, @@ -1413,11 +1449,13 @@ static struct perf_pmu_alias *pmu_find_alias(struct= perf_pmu *pmu, } =20 =20 -static int check_info_data(struct perf_pmu_alias *alias, +static int check_info_data(struct perf_pmu *pmu, + struct perf_pmu_alias *alias, struct perf_pmu_info *info, struct parse_events_error *err, int column) { + read_alias_info(pmu, alias); /* * Only one term in event definition can * define unit, scale and snapshot, fail @@ -1487,7 +1525,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struc= t list_head *head_terms, return ret; } =20 - ret =3D check_info_data(alias, info, err, term->err_term); + ret =3D check_info_data(pmu, alias, info, err, term->err_term); if (ret) return ret; =20 --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 863D8EE49B2 for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234392AbjHWITk (ORCPT ); Wed, 23 Aug 2023 04:19:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231626AbjHWIMb (ORCPT ); Wed, 23 Aug 2023 04:12:31 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97CE6173F for ; Wed, 23 Aug 2023 01:09:33 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58ee4df08fbso73789267b3.3 for ; Wed, 23 Aug 2023 01:09:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778173; x=1693382973; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=2qaJRxBCyklSMWuPm+izeLXJoqyCIVuwmc1TFOmlkgI=; b=HYbuLJFmM89qjJvmBl3tRTcMNf1fLI3hJZEAeKXBIo84IZ5crxB9gYYh99GOFyv1E2 IExozTzs9hWyqs5K7X6i6Dqj8+JK+abPaMjSnOIlCNU8DFgUq+QUOZSn0Wpc4ulaMUkn +lDYufL22mW0iEVm5peGIBsBp3Zt7eIeftICgATUgukRUH1eC42oN4H1P0pbzTbC5XA1 O7wJttqhKEgm4qGt9mjeX1qjs3/uKiYN8KLqNm7EAvQ+qOqx9J9IcLxhvILqcNYzgaxR wroF2Q9peXqhmmUdSgfwi6nvIij1E+qUA5SO3lnrGIP83PTOh1Ggk/wYXAej36fKxVCX Yzog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778173; x=1693382973; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2qaJRxBCyklSMWuPm+izeLXJoqyCIVuwmc1TFOmlkgI=; b=aCb/ZmwGjpuzrxG8AcKRW26pkNHrSp7EJaY5AUNbpVcqsE8jbzMCXfeaz05UJMwg7E Fth8n79X1OUqE1jOpgyA3KBru6l8gKZ+Ig2ZdQQ83bBjByRFa+SUbCQLp3V9uLCczYJ6 CuheBRsaf6exaNAA+zqW8vOYWZxIxfHZhLfJMtmcox29SA7TWf3Epo+p3ajYdpLhW+Ny 5KMskrIHsgycpZ28x2PFhePUC2XSaKhfijtpTJJECeY3xyhNunB42hE1za4FX397KLHr VsextRU4F5mh8TjhDQL2iTJPyPrH0lJjjjvU+Ln/OcwKIB9YzwIdClV9zvPcO9iqYLdD xa7A== X-Gm-Message-State: AOJu0YwXuL/uoIHklSwtBCR5GGk9SJUP9rVoXr9j2VFO5XO0yUwvJHgH puQvDCiv8fNfx8FJ6IfHNWfJ6FhaWEHQ X-Google-Smtp-Source: AGHT+IFEwGkpQOFrN5AJCbgmqj0oTABtx0SRmjN2OR1zrYEY5KKUgeFpToIA4RVOwawMiSdD1KbEnU9gqbtJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a81:b724:0:b0:583:a3c1:6b5a with SMTP id v36-20020a81b724000000b00583a3c16b5amr140584ywh.4.1692778172847; Wed, 23 Aug 2023 01:09:32 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:27 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-25-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 24/25] perf pmu: Lazily load sysfs aliases From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't load sysfs aliases for a PMU when the PMU is first created, defer until an alias needs to be found. For the pmu-scan benchmark, average core PMU scanning is reduced by 30.8%, and average PMU scanning by 12.6%. Signed-off-by: Ian Rogers --- tools/perf/tests/pmu-events.c | 2 + tools/perf/util/pmu.c | 81 ++++++++++++++++++----------------- tools/perf/util/pmu.h | 2 + 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index 9ac893ae5f0d..3dc1ebee4d9f 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -547,6 +547,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name= , int *count) pmu->events_table =3D table; pmu_add_cpu_aliases_table(pmu, table); pmu->cpu_aliases_added =3D true; + pmu->sysfs_aliases_loaded =3D true; =20 res =3D pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NUL= L, NULL); if (res !=3D 0) { @@ -588,6 +589,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) pmu->events_table =3D events_table; pmu_add_cpu_aliases_table(pmu, events_table); pmu->cpu_aliases_added =3D true; + pmu->sysfs_aliases_loaded =3D true; pmu_add_sys_aliases(pmu); =20 /* Count how many aliases we generated */ diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index c547fe607f9f..fb437b50443f 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -115,6 +115,8 @@ struct perf_pmu_format { bool loaded; }; =20 +static int pmu_aliases_parse(struct perf_pmu *pmu); + static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list= , char *name) { struct perf_pmu_format *format; @@ -416,10 +418,15 @@ static void perf_pmu__del_aliases(struct perf_pmu *pm= u) } } =20 -static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *= pmu, const char *name) +static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, + const char *name, + bool load) { struct perf_pmu_alias *alias; =20 + if (load && !pmu->sysfs_aliases_loaded) + pmu_aliases_parse(pmu); + list_for_each_entry(alias, &pmu->aliases, list) { if (!strcasecmp(alias->name, name)) return alias; @@ -501,7 +508,7 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, co= nst char *name, const char *long_desc =3D NULL, *topic =3D NULL, *unit =3D NULL, *pmu_nam= e =3D NULL; bool deprecated =3D false, perpkg =3D false; =20 - if (perf_pmu__find_alias(pmu, name)) { + if (perf_pmu__find_alias(pmu, name, /*load=3D*/ false)) { /* Alias was already created/loaded. */ return 0; } @@ -607,18 +614,33 @@ static inline bool pmu_alias_info_file(char *name) } =20 /* - * Process all the sysfs attributes located under the directory - * specified in 'dir' parameter. + * Reading the pmu event aliases definition, which should be located at: + * /sys/bus/event_source/devices//events as sysfs group attributes. */ -static int pmu_aliases_parse(struct perf_pmu *pmu, int dirfd) +static int pmu_aliases_parse(struct perf_pmu *pmu) { + char path[PATH_MAX]; struct dirent *evt_ent; DIR *event_dir; - int fd; + size_t len; + int fd, dir_fd; =20 - event_dir =3D fdopendir(dirfd); - if (!event_dir) + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name); + + dir_fd =3D open(path, O_DIRECTORY); + if (dir_fd =3D=3D -1) { + pmu->sysfs_aliases_loaded =3D true; + return 0; + } + + event_dir =3D fdopendir(dir_fd); + if (!event_dir){ + close (dir_fd); return -EINVAL; + } =20 while ((evt_ent =3D readdir(event_dir))) { char *name =3D evt_ent->d_name; @@ -633,7 +655,7 @@ static int pmu_aliases_parse(struct perf_pmu *pmu, int = dirfd) if (pmu_alias_info_file(name)) continue; =20 - fd =3D openat(dirfd, name, O_RDONLY); + fd =3D openat(dir_fd, name, O_RDONLY); if (fd =3D=3D -1) { pr_debug("Cannot open %s\n", name); continue; @@ -651,25 +673,8 @@ static int pmu_aliases_parse(struct perf_pmu *pmu, int= dirfd) } =20 closedir(event_dir); - return 0; -} - -/* - * Reading the pmu event aliases definition, which should be located at: - * /sys/bus/event_source/devices//events as sysfs group attributes. - */ -static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name) -{ - int fd; - - fd =3D perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); - if (fd < 0) - return 0; - - /* it'll close the fd */ - if (pmu_aliases_parse(pmu, fd)) - return -1; - + close (dir_fd); + pmu->sysfs_aliases_loaded =3D true; return 0; } =20 @@ -1013,13 +1018,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *= pmus, int dirfd, const char free(pmu); return NULL; } - /* - * Check the aliases first to avoid unnecessary work. - */ - if (pmu_aliases(pmu, dirfd, name)) { - free(pmu); - return NULL; - } pmu->is_core =3D is_pmu_core(name); pmu->cpus =3D pmu_cpumask(dirfd, name, pmu->is_core); =20 @@ -1434,7 +1432,7 @@ static struct perf_pmu_alias *pmu_find_alias(struct p= erf_pmu *pmu, return NULL; } =20 - alias =3D perf_pmu__find_alias(pmu, name); + alias =3D perf_pmu__find_alias(pmu, name, /*load=3D*/ true); if (alias || pmu->cpu_aliases_added) return alias; =20 @@ -1443,7 +1441,7 @@ static struct perf_pmu_alias *pmu_find_alias(struct p= erf_pmu *pmu, pmu_events_table__find_event(pmu->events_table, pmu, name, pmu_add_cpu_aliases_map_callback, pmu) =3D=3D 0) { - alias =3D perf_pmu__find_alias(pmu, name); + alias =3D perf_pmu__find_alias(pmu, name, /*load=3D*/ false); } return alias; } @@ -1616,7 +1614,7 @@ bool perf_pmu__auto_merge_stats(const struct perf_pmu= *pmu) =20 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) { - if (perf_pmu__find_alias(pmu, name) !=3D NULL) + if (perf_pmu__find_alias(pmu, name, /*load=3D*/ true) !=3D NULL) return true; if (pmu->cpu_aliases_added || !pmu->events_table) return false; @@ -1625,7 +1623,12 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, cons= t char *name) =20 size_t perf_pmu__num_events(struct perf_pmu *pmu) { - size_t nr =3D pmu->sysfs_aliases; + size_t nr; + + if (!pmu->sysfs_aliases_loaded) + pmu_aliases_parse(pmu); + + nr =3D pmu->sysfs_aliases; =20 if (pmu->cpu_aliases_added) nr +=3D pmu->loaded_json_aliases; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 288d2908382a..bae0de3ed7a5 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -122,6 +122,8 @@ struct perf_pmu { uint32_t sysfs_aliases; /** @sysfs_aliases: Number of json event aliases loaded. */ uint32_t loaded_json_aliases; + /** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */ + bool sysfs_aliases_loaded; /** * @cpu_aliases_added: Have all json events table entries for the PMU * been added? --=20 2.42.0.rc1.204.g551eb34607-goog From nobody Wed Dec 17 07:25:57 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74A4CEE49AE for ; Wed, 23 Aug 2023 08:22:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234291AbjHWIS5 (ORCPT ); Wed, 23 Aug 2023 04:18:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233705AbjHWIMc (ORCPT ); Wed, 23 Aug 2023 04:12:32 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0677B1982 for ; Wed, 23 Aug 2023 01:09:35 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58fb9323a27so50522167b3.1 for ; Wed, 23 Aug 2023 01:09:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692778175; x=1693382975; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=7+7xUX+eQzSie7iWr2Aj1jLiysTzidRjiqSjjI4y5Nk=; b=KGzB2PREcs7owuua9gpo5mr9UCgvODDN4jNmDXQ31zJ8kKkY+8Xr/3r2UoXwLjOePS K995kBSDC+RJdR30h2eK8/iNLOhSKskdWyoLgfI1S6WWIvjlf26gpbWQAnUWXcJRJNfY /QCrLJ3LrQQ2lux2AtnVKQDte0QQl/LSPc0RJ7G/8jTqwBphx3j+BkaorZ8qNnEsuVRH ld7PdetckaI4CSGfsLHt67Mn96NdZS2yYrIWZKCiXcojQNNV/rRqZ1MyWvQAvnVSnNDj KVBTNy0H+WpKvBG1L0x8Fw5LdWroU+bq7lBqO+YDy/xx38C09qEmo6l1CzAtNUvAS0/Z +XKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692778175; x=1693382975; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7+7xUX+eQzSie7iWr2Aj1jLiysTzidRjiqSjjI4y5Nk=; b=Z+PAPf1qVvnTMnWmU9zJBpWcEA5z6242CxzNhnbAVrVsbxXPjcpyVuLNd+NuuB2dok oD/DrL/HPQm9Xh4R5qdWDz4B3VdP3RkpfJ8WhYYhfTCJhEwtkXxGwvzWYp1P8ud5dWl5 Um5SUJFinVtozL8nnxzG6KiuJGgDgFeVrWUeoZqpML6lf5YeAGLvfCg9UNH/HT8DSxqU MOH4+7KfyZjmXQw59qeLGE7+bXF0YnFxG00yDBtwwhtjOcuhPix4GSpHeOrfqrN9t82/ tHjISFsImKoPOWZbho9tq5uzGiInCQoyRaqrcGR+13AAhskr/kyqVi8jKhdHFa2SjnPb XgYw== X-Gm-Message-State: AOJu0YzFXuDfz3jfqowiY5MBRy7f26lKR69/RdCNq1vXIvy+P20OmV+u hAFy9afMOf6q/BoAoUtwgTrtFZ/K0YBF X-Google-Smtp-Source: AGHT+IGtVayvCY/26FeTbSge7z228B5Rx9Dif54L8QiVcNmurVKbVuktSmELWXGjAL3aqdwmXD23+nueZQhm X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3971:e84:f508:9a36]) (user=irogers job=sendgmr) by 2002:a0d:ec05:0:b0:586:b332:8619 with SMTP id q5-20020a0dec05000000b00586b3328619mr150399ywn.7.1692778175150; Wed, 23 Aug 2023 01:09:35 -0700 (PDT) Date: Wed, 23 Aug 2023 01:08:28 -0700 In-Reply-To: <20230823080828.1460376-1-irogers@google.com> Message-Id: <20230823080828.1460376-26-irogers@google.com> Mime-Version: 1.0 References: <20230823080828.1460376-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v1 25/25] perf jevents: Sort strings in the big C string to reduce faults From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sort the strings within the big C string based on whether they were for a metric and then by when they were added. This helps group related strings and reduce minor faults by approximately 10 in 1740, about 0.57%. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/jevents.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 01335a452e70..e5bce57f5688 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -113,13 +113,24 @@ class BigCString: strings: Set[str] big_string: Sequence[str] offsets: Dict[str, int] + insert_number: int + insert_point: Dict[str, int] + metrics: Set[str] =20 def __init__(self): self.strings =3D set() + self.insert_number =3D 0; + self.insert_point =3D {} + self.metrics =3D set() =20 - def add(self, s: str) -> None: + def add(self, s: str, metric: bool) -> None: """Called to add to the big string.""" - self.strings.add(s) + if s not in self.strings: + self.strings.add(s) + self.insert_point[s] =3D self.insert_number + self.insert_number +=3D 1 + if metric: + self.metrics.add(s) =20 def compute(self) -> None: """Called once all strings are added to compute the string and offsets= .""" @@ -160,8 +171,11 @@ class BigCString: self.big_string =3D [] self.offsets =3D {} =20 + def string_cmp_key(s: str) -> Tuple[bool, int, str]: + return (s in self.metrics, self.insert_point[s], s) + # Emit all strings that aren't folded in a sorted manner. - for s in sorted(self.strings): + for s in sorted(self.strings, key=3Dstring_cmp_key): if s not in folded_strings: self.offsets[s] =3D big_string_offset self.big_string.append(f'/* offset=3D{big_string_offset} */ "') @@ -574,19 +588,20 @@ def preprocess_one_file(parents: Sequence[str], item:= os.DirEntry) -> None: assert len(mgroup) > 1, parents description =3D f"{metricgroup_descriptions[mgroup]}\\000" mgroup =3D f"{mgroup}\\000" - _bcs.add(mgroup) - _bcs.add(description) + _bcs.add(mgroup, metric=3DTrue) + _bcs.add(description, metric=3DTrue) _metricgroups[mgroup] =3D description return =20 topic =3D get_topic(item.name) for event in read_json_events(item.path, topic): pmu_name =3D f"{event.pmu}\\000" - _bcs.add(pmu_name) if event.name: - _bcs.add(event.build_c_string(metric=3DFalse)) + _bcs.add(pmu_name, metric=3DFalse) + _bcs.add(event.build_c_string(metric=3DFalse), metric=3DFalse) if event.metric_name: - _bcs.add(event.build_c_string(metric=3DTrue)) + _bcs.add(pmu_name, metric=3DTrue) + _bcs.add(event.build_c_string(metric=3DTrue), metric=3DTrue) =20 def process_one_file(parents: Sequence[str], item: os.DirEntry) -> None: """Process a JSON file during the main walk.""" --=20 2.42.0.rc1.204.g551eb34607-goog