From nobody Thu Dec 18 04:42:45 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 84DD3CDB482 for ; Thu, 12 Oct 2023 17:57:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442089AbjJLR5P (ORCPT ); Thu, 12 Oct 2023 13:57:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379691AbjJLR5H (ORCPT ); Thu, 12 Oct 2023 13:57:07 -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 74EC4ED for ; Thu, 12 Oct 2023 10:57:04 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a4f6729d16so19867167b3.0 for ; Thu, 12 Oct 2023 10:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697133423; x=1697738223; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=JWpjgvY6Y26Eimtg00txsKN0n/IUHl2rx+ZiyTR9QgU=; b=01OirAI7Kqwl3hkIaX6EXZO1fYLqbzvm3N6WQtONnav9Hq/SAmtx3GZMMEtTONsrbB XSp96vuEmw3s0kGuQ4mjztC5JI9pxAClDPUNc7bIFf2TJkV0SMTqOxDZKyhm+lWgZ9l9 BDOUrlzJaa7BSHGW8wSYfvAvrDnUFBnfWR8Z1qbvfEA5yiPARwdzjCzVaiuJBRFfWPDD JH+Qwv1UaO1/PFKto+YWN2OnnFOaQXjyJXKPXqHlnqzI36GjlSewl7rOIlyS96n8zIgN ipczdb9InLowA1jKpZA+Btm0zn25otbTCRkBE4WwGBa8+Ikb9qu2Z6eEgn7M2BSqbscI C4AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697133423; x=1697738223; 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=JWpjgvY6Y26Eimtg00txsKN0n/IUHl2rx+ZiyTR9QgU=; b=W1WmPo/2a8JQ/SJOfh+vU6vU6nHQqMxE4n8tgzMPPcVgMMlBV/K7bp2FLKaXSpkfJ2 DdLqk/cNxGMLQMabwmb3Uml5L6NPxKJviinBncmLNagAEdEdVZDWPTRD05R+BNJIsovc 66ik3t22yC0azQzmTNWkSB1OhoHpdD1c/nKjslq59rNlvmxPtqgtFX8qbhtPbFDR8gD+ yNt9zz3zHg4wmaLxH4RY9ZZZV73bh4QmXVN1yXC3iio4svWXnY6/fsjnepEcHFVi7rwS 1M/XVer/PFuG34tA4ji4/3Zk4uZVk0IftdkPDramt3IY1/ra0euZwcHUBigqM0bMf3lk EFzw== X-Gm-Message-State: AOJu0YxhxXga8LtXQcvR/s95DzTRY/yc6m6JFd2Jgu58FZ6hCgt0H8Zp 43nvTFq/iFsjQkEN2tuuWpo1S74cEh+x X-Google-Smtp-Source: AGHT+IE/sEVcjGwGGmZkF6IqM1j7PAPF1hcyhh38d5+Sy/MY8H4WVKC1/tU3vnJPIhx8VDYD3KM+PPxqwX1V X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6a89:babc:124b:e4e6]) (user=irogers job=sendgmr) by 2002:a25:e794:0:b0:d9a:40ed:8d26 with SMTP id e142-20020a25e794000000b00d9a40ed8d26mr200498ybh.0.1697133423623; Thu, 12 Oct 2023 10:57:03 -0700 (PDT) Date: Thu, 12 Oct 2023 10:56:44 -0700 In-Reply-To: <20231012175645.1849503-1-irogers@google.com> Message-Id: <20231012175645.1849503-7-irogers@google.com> Mime-Version: 1.0 References: <20231012175645.1849503-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Subject: [PATCH v2 6/7] perf pmu-events: Remember the perf_events_map for a PMU From: Ian Rogers To: Suzuki K Poulose , Mike Leach , James Clark , Leo Yan , John Garry , Will Deacon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Thomas Richter , Ravi Bangoria , Kajol Jain , Jing Zhang , Kan Liang , Yang Jihong , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, 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" strcmp_cpuid_str performs regular expression comparisons and so per CPUID linear searches over the perf_events_map are expensive. Add a helper function called map_for_pmu that does the search but also caches the map specific to a PMU. As the PMU may differ, also cache the CPUID string so that PMUs with the same CPUID string don't require the linear search and regular expression comparisons. This speeds loading PMUs as the search is done once per PMU to find the appropriate tables. Signed-off-by: Ian Rogers Tested-by: Yang Jihong --- tools/perf/pmu-events/jevents.py | 109 ++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 96dc74c90b20..3c091ab75305 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -976,68 +976,99 @@ int pmu_metrics_table__for_each_metric(const struct p= mu_metrics_table *table, return 0; } =20 -const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu= *pmu) +static const struct pmu_events_map *map_for_pmu(struct perf_pmu *pmu) { - const struct pmu_events_table *table =3D NULL; - char *cpuid =3D perf_pmu__getcpuid(pmu); + static struct { + const struct pmu_events_map *map; + struct perf_pmu *pmu; + } last_result; + static struct { + const struct pmu_events_map *map; + char *cpuid; + } last_map_search; + static bool has_last_result, has_last_map_search; + const struct pmu_events_map *map =3D NULL; + char *cpuid =3D NULL; size_t i; =20 - /* on some platforms which uses cpus map, cpuid can be NULL for + if (has_last_result && last_result.pmu =3D=3D pmu) + return last_result.map; + + cpuid =3D perf_pmu__getcpuid(pmu); + + /* + * On some platforms which uses cpus map, cpuid can be NULL for * PMUs other than CORE PMUs. */ if (!cpuid) - return NULL; + goto out_update_last_result; + + if (has_last_map_search && !strcmp(last_map_search.cpuid, cpuid)) { + map =3D last_map_search.map; + free(cpuid); + } else { + i =3D 0; + for (;;) { + map =3D &pmu_events_map[i++]; + + if (!map->arch) { + map =3D NULL; + break; + } + + if (!strcmp_cpuid_str(map->cpuid, cpuid)) + break; + } + free(last_map_search.cpuid); + last_map_search.cpuid =3D cpuid; + last_map_search.map =3D map; + has_last_map_search =3D true; + } +out_update_last_result: + last_result.pmu =3D pmu; + last_result.map =3D map; + has_last_result =3D true; + return map; +} =20 - i =3D 0; - for (;;) { - const struct pmu_events_map *map =3D &pmu_events_map[i++]; - if (!map->arch) - break; +const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu= *pmu) +{ + const struct pmu_events_map *map =3D map_for_pmu(pmu); =20 - if (!strcmp_cpuid_str(map->cpuid, cpuid)) { - table =3D &map->event_table; - break; - } - } - free(cpuid); - if (!pmu || !table) - return table; + if (!map) + return NULL; =20 - for (i =3D 0; i < table->num_pmus; i++) { - const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; + if (!pmu) + return &map->event_table; + + for (size_t i =3D 0; i < map->event_table.num_pmus; i++) { + const struct pmu_table_entry *table_pmu =3D &map->event_ta= ble.pmus[i]; const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; =20 if (pmu__name_match(pmu, pmu_name)) - return table; + return &map->event_table; } return NULL; } =20 const struct pmu_metrics_table *perf_pmu__find_metrics_table(struct perf_p= mu *pmu) { - const struct pmu_metrics_table *table =3D NULL; - char *cpuid =3D perf_pmu__getcpuid(pmu); - int i; + const struct pmu_events_map *map =3D map_for_pmu(pmu); =20 - /* on some platforms which uses cpus map, cpuid can be NULL for - * PMUs other than CORE PMUs. - */ - if (!cpuid) + if (!map) return NULL; =20 - i =3D 0; - for (;;) { - const struct pmu_events_map *map =3D &pmu_events_map[i++]; - if (!map->arch) - break; + if (!pmu) + return &map->metric_table; =20 - if (!strcmp_cpuid_str(map->cpuid, cpuid)) { - table =3D &map->metric_table; - break; - } + for (size_t i =3D 0; i < map->metric_table.num_pmus; i++) { + const struct pmu_table_entry *table_pmu =3D &map->metric_t= able.pmus[i]; + const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; + + if (pmu__name_match(pmu, pmu_name)) + return &map->metric_table; } - free(cpuid); - return table; + return NULL; } =20 const struct pmu_events_table *find_core_events_table(const char *arch, co= nst char *cpuid) --=20 2.42.0.655.g421f12c284-goog