From nobody Fri Dec 19 00:20:07 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 414C5255F31 for ; Mon, 12 May 2025 19:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079193; cv=none; b=kuAjNECfxmovV0L+41kJ/GNp7AsU7YuKU40lAf1AzooNpMaNxIaO//XhzJ1H2EmAbjIPMpHuNvHk+LnWPnnGZfPTSUtxDteLkEAO6EDkXaqJnouRnobhMgomArQgdF4OK3yRVJu0PpWcEn6Ejfn329qqONWFowEV6MkjYcT2dRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079193; c=relaxed/simple; bh=H8wmlkybuyRNgIjko9condU2hcvyNOwK+IcMQr5hzcI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=qPgC2w7Ac5ZW8bAgzjqL9blT7MJ3mviJfsn/TJueUsezdsX+arpEMHudYg0jbR2MSxnU145xcyqfAlvDj2kl1pBMFt0SkTKNVaG6q4SyZOI0AmP5pPsa8jikV8DgFoAcWFa5vxuTKXbRd5ydf7YFIjd1ZXr8qdZ/46KGbM5JZ1c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2hxaQSIL; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2hxaQSIL" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2371b50cabso3897654a12.0 for ; Mon, 12 May 2025 12:46:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747079191; x=1747683991; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=XU6UfA2WeHkiiF81WFUDai0gldv4UGsTw49aTIuK2Iw=; b=2hxaQSILD+8wv2ynV1EI0L2fh8ZY06q9uZ8teRsYMbcduWWjR4023RNzg7G2eVPhGb TLpPR7HR+zhNDMVqxvYsEZVCGGLkDx6FxhBoBQ2VaKfeqkKhjN8J7ALIJ1DugsZMedyK g6VWupH6yHu43eUEtj3c4bGo8Im/rElY7IOxdI6v4Wo4IuyXn/CZqbhYrZXPBHBAJWAd j7x+C/ctIV8BcrzmwWTgVVQ/cUnhCQ8Dh5jQ0Jd+2jvyQCsHVN88/qH6RAQigwErpdml LX6XLCICclmA+N9kr/Hmh6bQZFNya2kasTrMjdxOPKJjAWdAB+P7w0/ynlPzlndENJOl LT4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747079191; x=1747683991; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XU6UfA2WeHkiiF81WFUDai0gldv4UGsTw49aTIuK2Iw=; b=UeIgJt8ksRI5z6Nb4WbSzZlNZJhPovLaZusLue2pX9giHfcLMPnJk3KS/9zoubJTTb wL9BYcv+X0iX/gu/trE3pxM9n71cTebMsvBCGTibjrzjQU74H7jw1CmCDpMc0zdQV2vD q1stbDZ/9J2iEViUFGLdeMbY1RULve7XU1iIl4e4I12wM4EDHKRHLqoQsVwJw/4IpJX7 NIaLPxCQ4Xw0x2ZRKTmLsXXMIqiX3rhyjbfYyLplmP1MtPLT1GqktdBAORyEh2CJi3EE kWMLtqPINJyzXjCbWWuRzH5JpzCKj/DDU/9LxvZG2FLW9z08lKr9KfH0fz3RF0QPzHHs Qupg== X-Forwarded-Encrypted: i=1; AJvYcCVtzIepHHyrzwyfeZxx/0DZw6h88wD/UbfuZSVri6j2LLdk+n5fNtCJwY8kADsfxirSfMqGBm/1cWWmiPY=@vger.kernel.org X-Gm-Message-State: AOJu0YwPlbv7PRJrKt2MgUMOKWQ76QAkBKetRiAvSmidVyVCnFBxvwCN jEI9cOPd8M1eqvQqYyHHJKgikNWzVFiW8BdGywt9i/CtVjQVYBMWeXzmaOqQAWqTrjDWZVOjf3d 9RrHKsQ== X-Google-Smtp-Source: AGHT+IGiJhnOWvskRyOedJpWLyi0SwiQRmmUy3W/gJ/dq1ajxD6hnxUUcIwzGnkK49P5rS0XESZPwPuX6TWO X-Received: from plgi6.prod.google.com ([2002:a17:902:cf06:b0:22e:4956:ff79]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e54f:b0:22e:3b65:9286 with SMTP id d9443c01a7336-22fc91a84edmr224460635ad.49.1747079191212; Mon, 12 May 2025 12:46:31 -0700 (PDT) Date: Mon, 12 May 2025 12:46:20 -0700 In-Reply-To: <20250512194622.33258-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250512194622.33258-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: <20250512194622.33258-2-irogers@google.com> Subject: [PATCH v2 1/3] perf fncache: Switch to using hashmap From: Ian Rogers To: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Xu Yang , Thomas Richter , Ravi Bangoria , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The existing fncache can get large in testing situations. As the bucket array is a fixed size this leads to it degrading to O(n) performance. Use a regular hashmap that can dynamically reallocate its array. Before: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m14.132s user 0m17.806s sys 0m0.557s ``` After: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m13.287s user 0m13.026s sys 0m0.532s ``` Signed-off-by: Ian Rogers --- tools/perf/util/fncache.c | 69 +++++++++++++++++++++------------------ tools/perf/util/fncache.h | 1 - tools/perf/util/srccode.c | 4 +-- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/tools/perf/util/fncache.c b/tools/perf/util/fncache.c index 6225cbc52310..bf9559c55c63 100644 --- a/tools/perf/util/fncache.c +++ b/tools/perf/util/fncache.c @@ -1,53 +1,58 @@ // SPDX-License-Identifier: GPL-2.0-only /* Manage a cache of file names' existence */ +#include #include -#include #include -#include +#include +#include #include "fncache.h" +#include "hashmap.h" =20 -struct fncache { - struct hlist_node nd; - bool res; - char name[]; -}; +static struct hashmap *fncache; =20 -#define FNHSIZE 61 +static size_t fncache__hash(long key, void *ctx __maybe_unused) +{ + return str_hash((const char *)key); +} =20 -static struct hlist_head fncache_hash[FNHSIZE]; +static bool fncache__equal(long key1, long key2, void *ctx __maybe_unused) +{ + return strcmp((const char *)key1, (const char *)key2) =3D=3D 0; +} =20 -unsigned shash(const unsigned char *s) +static void fncache__init(void) { - unsigned h =3D 0; - while (*s) - h =3D 65599 * h + *s++; - return h ^ (h >> 16); + fncache =3D hashmap__new(fncache__hash, fncache__equal, /*ctx=3D*/NULL); +} + +static struct hashmap *fncache__get(void) +{ + static pthread_once_t fncache_once =3D PTHREAD_ONCE_INIT; + + pthread_once(&fncache_once, fncache__init); + + return fncache; } =20 static bool lookup_fncache(const char *name, bool *res) { - int h =3D shash((const unsigned char *)name) % FNHSIZE; - struct fncache *n; - - hlist_for_each_entry(n, &fncache_hash[h], nd) { - if (!strcmp(n->name, name)) { - *res =3D n->res; - return true; - } - } - return false; + long val; + + if (!hashmap__find(fncache__get(), name, &val)) + return false; + + *res =3D (val !=3D 0); + return true; } =20 static void update_fncache(const char *name, bool res) { - struct fncache *n =3D malloc(sizeof(struct fncache) + strlen(name) + 1); - int h =3D shash((const unsigned char *)name) % FNHSIZE; - - if (!n) - return; - strcpy(n->name, name); - n->res =3D res; - hlist_add_head(&n->nd, &fncache_hash[h]); + char *old_key =3D NULL, *key =3D strdup(name); + + if (key) { + hashmap__set(fncache__get(), key, res, &old_key, /*old_value*/NULL); + free(old_key); + } } =20 /* No LRU, only use when bounded in some other way. */ diff --git a/tools/perf/util/fncache.h b/tools/perf/util/fncache.h index fe020beaefb1..b6a0f209493e 100644 --- a/tools/perf/util/fncache.h +++ b/tools/perf/util/fncache.h @@ -1,7 +1,6 @@ #ifndef _FCACHE_H #define _FCACHE_H 1 =20 -unsigned shash(const unsigned char *s); bool file_available(const char *name); =20 #endif diff --git a/tools/perf/util/srccode.c b/tools/perf/util/srccode.c index 476e99896d5e..0f4907843ac1 100644 --- a/tools/perf/util/srccode.c +++ b/tools/perf/util/srccode.c @@ -16,7 +16,7 @@ #include "srccode.h" #include "debug.h" #include // page_size -#include "fncache.h" +#include "hashmap.h" =20 #define MAXSRCCACHE (32*1024*1024) #define MAXSRCFILES 64 @@ -92,7 +92,7 @@ static struct srcfile *find_srcfile(char *fn) struct srcfile *h; int fd; unsigned long sz; - unsigned hval =3D shash((unsigned char *)fn) % SRC_HTAB_SZ; + size_t hval =3D str_hash(fn) % SRC_HTAB_SZ; =20 hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) { if (!strcmp(fn, h->fn)) { --=20 2.49.0.1045.g170613ef41-goog From nobody Fri Dec 19 00:20:07 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02B3F298CDF for ; Mon, 12 May 2025 19:46:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079196; cv=none; b=ALlFr0gWlDjCDUpVuyu57s6aSYt93iO4WENI1puHet/WljaVapkXVQAIOJryhmRn8wAKB2UGlShjfEM+2HQI8jhBn5Hm+MTx1VwY0bhwWjnzSwFgAiiLTslgre/1iCDlzmFXnvdfSxqXXdU/o0ar1zhQCI+gj5hYj1chsjaq9/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079196; c=relaxed/simple; bh=YTXjbo2l8ailvad8rbtlbmKkSPBNAlBjwdL9nEGBE6Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=NAkBKDsEZJ4ylQR/aT/baegpj3WSpoDb5ZIS5mggvRJDvB9LvukssSoM7Gbl20sawu9RLL4UCKp5lU7/0gOzvDDiu+QO8GIaYfU4FIfdEZqe5vz1KH0BwBh5vrJrsDLtLrsIvOWNq3lnt1J460RIWKaGUtc9x91WTBVK1b9/1Ic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sPRXq5oX; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sPRXq5oX" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-742512d307bso2878566b3a.3 for ; Mon, 12 May 2025 12:46:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747079193; x=1747683993; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=L4GgSiYcBGsJRco3Bv45eQpmd5dOH26B40zV/W7mrds=; b=sPRXq5oX8utnzgwMwuCIeoRSZWKcrpuFIL2UayGsb3N5Y9mIECaq1FZKVTOTE8rPG9 VIuT5IKt7EeAH1mbngmeCh1Md4c75JUZVPx8h7JwNbmg1cTv1vgeUEW+ga4uy6E1+1Rj Yfmw+ir781z2OXDsOTMNj+/e7+0WFDf1Ao06kYdo9lJEtckAvSUD5wF1G9Us9Q0FE7v2 gmxFVnoH1DbHwrm1SAAwJfs02mMOT1lz5RS2UEXQGr2VZWkBUbjKnvP89W0Hw1DEbDmG AFh0KTWzGoqJPCQe5EWrYjjNE3K/T/Ism3pKqkEhi8t586ERGMjcl+86N3aJJM+8VrBP lsZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747079193; x=1747683993; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=L4GgSiYcBGsJRco3Bv45eQpmd5dOH26B40zV/W7mrds=; b=Zm495ZST1UaLnm4svE4WXigrz9wTw0yp+na8SDGGuU5w2lR1JPZqn/kjGUS6LfW0/A VtGYeRlk1qpoFuoTGDRtGCWWBQyagzFUj+v0rAx4YDWEMqkuMUlymwZFqQVmN03i9Fhd jUB9TI7Zyc47IUQLwgYoNuxCjw+RIlGUXsH0WObz/CDcjRogMDNZRmcsyPfocyUgE/t5 hIlC12el36xnvOZkR/9dUF6/4OUlsw3RXQgNjrUVI6YrvkjD9PqoOgEUk0DnuKQs2kyA mMB66CQzu9/bX4pqGvHLa8VUN5paMGkzRxn4T+SsLnzASreL/fRoLS+infMSB9JYSVS4 n8Tg== X-Forwarded-Encrypted: i=1; AJvYcCW3LwgErIy8Wdbb/ziHh4o7mrPWjaMIZ5crSK4vl+XO3AVUhOepaA/rDkvTyMQKFlv4vklbmZsabAgUoOk=@vger.kernel.org X-Gm-Message-State: AOJu0YzniNXeu1BzppvEXEVgzSvZWtYAFSzdcaDNSeP620yj5aSBf21Q /W1Q1ZTeHIt9egn7F/zPK8cBQfZMeh3vuvQPEa+KeuLlQPMyNdXvnZmC3NnxBynUmsePEHy1z0a oqEwMqA== X-Google-Smtp-Source: AGHT+IF7Cxz7AaDPPAUUIrREgDGAPeqQtLqphReGJwyQV9WsF3Fhys3bJS8QzmUdbTA44gbLMuaj5my/69zn X-Received: from pfbhm16.prod.google.com ([2002:a05:6a00:6710:b0:740:b0f1:1ede]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9286:b0:736:3d7c:236c with SMTP id d2e1a72fcca58-7423be7d6d0mr18962036b3a.14.1747079193120; Mon, 12 May 2025 12:46:33 -0700 (PDT) Date: Mon, 12 May 2025 12:46:21 -0700 In-Reply-To: <20250512194622.33258-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250512194622.33258-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: <20250512194622.33258-3-irogers@google.com> Subject: [PATCH v2 2/3] perf pmu: Change aliases from list to hashmap From: Ian Rogers To: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Xu Yang , Thomas Richter , Ravi Bangoria , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Finding an alias for things like perf_pmu__have_event would need to search the aliases list, whilst this happens relatively infrequently it can be a significant overhead in testing. Switch to using a hashmap. Move common initialization code to perf_pmu__init. Refactor the test strct perf_pmu_test_pmu to not have perf pmu within it to better support the perf_pmu__init function. Before: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m13.287s user 0m13.026s sys 0m0.532s ``` After: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m13.011s user 0m12.885s sys 0m0.485s ``` Signed-off-by: Ian Rogers --- tools/perf/tests/pmu-events.c | 129 +++++++++++++------------- tools/perf/util/hwmon_pmu.c | 43 ++++----- tools/perf/util/pmu.c | 167 ++++++++++++++++++++++------------ tools/perf/util/pmu.h | 4 +- tools/perf/util/tool_pmu.c | 17 +--- 5 files changed, 199 insertions(+), 161 deletions(-) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index db004d26fcb0..815b40097428 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -38,7 +38,9 @@ struct perf_pmu_test_event { }; =20 struct perf_pmu_test_pmu { - struct perf_pmu pmu; + const char *pmu_name; + bool pmu_is_uncore; + const char *pmu_id; struct perf_pmu_test_event const *aliases[10]; }; =20 @@ -553,11 +555,10 @@ static int __test_core_pmu_event_aliases(const char *= pmu_name, int *count) if (!pmu) return -1; =20 - 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); + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, pmu_name) !=3D 0) { + perf_pmu__delete(pmu); + return -1; + } pmu->is_core =3D true; =20 pmu->events_table =3D table; @@ -594,14 +595,30 @@ static int __test_uncore_pmu_event_aliases(struct per= f_pmu_test_pmu *test_pmu) { int alias_count =3D 0, to_match_count =3D 0, matched_count =3D 0; 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 *pmu; const struct pmu_events_table *events_table; int res =3D 0; =20 events_table =3D find_core_events_table("testarch", "testcpu"); if (!events_table) return -1; + + pmu =3D zalloc(sizeof(*pmu)); + if (!pmu) + return -1; + + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, test_pmu->pmu_name) !=3D 0) { + perf_pmu__delete(pmu); + return -1; + } + pmu->is_uncore =3D test_pmu->pmu_is_uncore; + if (test_pmu->pmu_id) { + pmu->id =3D strdup(test_pmu->pmu_id); + if (!pmu->id) { + perf_pmu__delete(pmu); + return -1; + } + } pmu->events_table =3D events_table; pmu_add_cpu_aliases_table(pmu, events_table); pmu->cpu_aliases_added =3D true; @@ -617,7 +634,8 @@ static int __test_uncore_pmu_event_aliases(struct perf_= pmu_test_pmu *test_pmu) =20 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); + pmu->name, to_match_count, alias_count); + perf_pmu__delete(pmu); return -1; } =20 @@ -630,9 +648,10 @@ static int __test_uncore_pmu_event_aliases(struct perf= _pmu_test_pmu *test_pmu) .count =3D &matched_count, }; =20 - if (strcmp(pmu_name, test_event.matching_pmu)) { + if (strcmp(pmu->name, test_event.matching_pmu)) { pr_debug("testing aliases uncore PMU %s: mismatched matching_pmu, %s vs= %s\n", - pmu_name, test_event.matching_pmu, pmu_name); + pmu->name, test_event.matching_pmu, pmu->name); + perf_pmu__delete(pmu); return -1; } =20 @@ -641,34 +660,32 @@ static int __test_uncore_pmu_event_aliases(struct per= f_pmu_test_pmu *test_pmu) if (err) { res =3D err; pr_debug("testing aliases uncore PMU %s: could not match alias %s\n", - pmu_name, event->name); + pmu->name, event->name); + perf_pmu__delete(pmu); return -1; } } =20 if (alias_count !=3D matched_count) { pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs = matched (%d)\n", - pmu_name, matched_count, alias_count); + pmu->name, matched_count, alias_count); res =3D -1; } + perf_pmu__delete(pmu); return res; } =20 static struct perf_pmu_test_pmu test_pmus[] =3D { { - .pmu =3D { - .name =3D "hisi_sccl1_ddrc2", - .is_uncore =3D 1, - }, + .pmu_name =3D "hisi_sccl1_ddrc2", + .pmu_is_uncore =3D 1, .aliases =3D { &uncore_hisi_ddrc_flux_wcmd, }, }, { - .pmu =3D { - .name =3D "uncore_cbox_0", - .is_uncore =3D 1, - }, + .pmu_name =3D "uncore_cbox_0", + .pmu_is_uncore =3D 1, .aliases =3D { &unc_cbo_xsnp_response_miss_eviction, &uncore_hyphen, @@ -676,88 +693,70 @@ static struct perf_pmu_test_pmu test_pmus[] =3D { }, }, { - .pmu =3D { - .name =3D "hisi_sccl3_l3c7", - .is_uncore =3D 1, - }, + .pmu_name =3D "hisi_sccl3_l3c7", + .pmu_is_uncore =3D 1, .aliases =3D { &uncore_hisi_l3c_rd_hit_cpipe, }, }, { - .pmu =3D { - .name =3D "uncore_imc_free_running_0", - .is_uncore =3D 1, - }, + .pmu_name =3D "uncore_imc_free_running_0", + .pmu_is_uncore =3D 1, .aliases =3D { &uncore_imc_free_running_cache_miss, }, }, { - .pmu =3D { - .name =3D "uncore_imc_0", - .is_uncore =3D 1, - }, + .pmu_name =3D "uncore_imc_0", + .pmu_is_uncore =3D 1, .aliases =3D { &uncore_imc_cache_hits, }, }, { - .pmu =3D { - .name =3D "uncore_sys_ddr_pmu0", - .is_uncore =3D 1, - .id =3D "v8", - }, + .pmu_name =3D "uncore_sys_ddr_pmu0", + .pmu_is_uncore =3D 1, + .pmu_id =3D "v8", .aliases =3D { &sys_ddr_pmu_write_cycles, }, }, { - .pmu =3D { - .name =3D "uncore_sys_ccn_pmu4", - .is_uncore =3D 1, - .id =3D "0x01", - }, + .pmu_name =3D "uncore_sys_ccn_pmu4", + .pmu_is_uncore =3D 1, + .pmu_id =3D "0x01", .aliases =3D { &sys_ccn_pmu_read_cycles, }, }, { - .pmu =3D { - .name =3D (char *)"uncore_sys_cmn_pmu0", - .is_uncore =3D 1, - .id =3D (char *)"43401", - }, + .pmu_name =3D "uncore_sys_cmn_pmu0", + .pmu_is_uncore =3D 1, + .pmu_id =3D "43401", .aliases =3D { &sys_cmn_pmu_hnf_cache_miss, }, }, { - .pmu =3D { - .name =3D (char *)"uncore_sys_cmn_pmu0", - .is_uncore =3D 1, - .id =3D (char *)"43602", - }, + .pmu_name =3D "uncore_sys_cmn_pmu0", + .pmu_is_uncore =3D 1, + .pmu_id =3D "43602", .aliases =3D { &sys_cmn_pmu_hnf_cache_miss, }, }, { - .pmu =3D { - .name =3D (char *)"uncore_sys_cmn_pmu0", - .is_uncore =3D 1, - .id =3D (char *)"43c03", - }, + .pmu_name =3D "uncore_sys_cmn_pmu0", + .pmu_is_uncore =3D 1, + .pmu_id =3D "43c03", .aliases =3D { &sys_cmn_pmu_hnf_cache_miss, }, }, { - .pmu =3D { - .name =3D (char *)"uncore_sys_cmn_pmu0", - .is_uncore =3D 1, - .id =3D (char *)"43a01", - }, + .pmu_name =3D "uncore_sys_cmn_pmu0", + .pmu_is_uncore =3D 1, + .pmu_id =3D "43a01", .aliases =3D { &sys_cmn_pmu_hnf_cache_miss, }, @@ -796,10 +795,6 @@ static int test__aliases(struct test_suite *test __may= be_unused, for (i =3D 0; i < ARRAY_SIZE(test_pmus); i++) { int res; =20 - INIT_LIST_HEAD(&test_pmus[i].pmu.format); - INIT_LIST_HEAD(&test_pmus[i].pmu.aliases); - INIT_LIST_HEAD(&test_pmus[i].pmu.caps); - res =3D __test_uncore_pmu_event_aliases(&test_pmus[i]); if (res) return res; diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c index 3cce77fc8004..c25e7296f1c1 100644 --- a/tools/perf/util/hwmon_pmu.c +++ b/tools/perf/util/hwmon_pmu.c @@ -346,42 +346,43 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmu= s, int hwmon_dir, const cha { char buf[32]; struct hwmon_pmu *hwm; + __u32 type =3D PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, = 10); + + if (type > PERF_PMU_TYPE_HWMON_END) { + pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_= name); + return NULL; + } + + snprintf(buf, sizeof(buf), "hwmon_%s", name); + fix_name(buf + 6); =20 hwm =3D zalloc(sizeof(*hwm)); if (!hwm) return NULL; =20 - hwm->hwmon_dir_fd =3D hwmon_dir; - hwm->pmu.type =3D PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NUL= L, 10); - if (hwm->pmu.type > PERF_PMU_TYPE_HWMON_END) { - pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_= name); - goto err_out; + if (perf_pmu__init(&hwm->pmu, type, buf) !=3D 0) { + perf_pmu__delete(&hwm->pmu); + return NULL; } - snprintf(buf, sizeof(buf), "hwmon_%s", name); - fix_name(buf + 6); - hwm->pmu.name =3D strdup(buf); - if (!hwm->pmu.name) - goto err_out; + + hwm->hwmon_dir_fd =3D hwmon_dir; hwm->pmu.alias_name =3D strdup(sysfs_name); - if (!hwm->pmu.alias_name) - goto err_out; + if (!hwm->pmu.alias_name) { + perf_pmu__delete(&hwm->pmu); + return NULL; + } hwm->pmu.cpus =3D perf_cpu_map__new("0"); - if (!hwm->pmu.cpus) - goto err_out; + if (!hwm->pmu.cpus) { + perf_pmu__delete(&hwm->pmu); + return NULL; + } INIT_LIST_HEAD(&hwm->pmu.format); - INIT_LIST_HEAD(&hwm->pmu.aliases); INIT_LIST_HEAD(&hwm->pmu.caps); hashmap__init(&hwm->events, hwmon_pmu__event_hashmap_hash, hwmon_pmu__event_hashmap_equal, /*ctx=3D*/NULL); =20 list_add_tail(&hwm->pmu.list, pmus); return &hwm->pmu; -err_out: - free((char *)hwm->pmu.name); - free(hwm->pmu.alias_name); - free(hwm); - close(hwmon_dir); - return NULL; } =20 void hwmon_pmu__exit(struct perf_pmu *pmu) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 798810704f5b..bc1178234d3b 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -27,6 +27,7 @@ #include #include "parse-events.h" #include "print-events.h" +#include "hashmap.h" #include "header.h" #include "string2.h" #include "strbuf.h" @@ -66,8 +67,6 @@ struct perf_pmu_alias { char *topic; /** @terms: Owned list of the original parsed parameters. */ struct parse_events_terms 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. @@ -418,25 +417,33 @@ static void perf_pmu__parse_snapshot(struct perf_pmu = *pmu, struct perf_pmu_alias } =20 /* Delete an alias entry. */ -static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) +static void perf_pmu_free_alias(struct perf_pmu_alias *alias) { - zfree(&newalias->name); - zfree(&newalias->desc); - zfree(&newalias->long_desc); - zfree(&newalias->topic); - zfree(&newalias->pmu_name); - parse_events_terms__exit(&newalias->terms); - free(newalias); + if (!alias) + return; + + zfree(&alias->name); + zfree(&alias->desc); + zfree(&alias->long_desc); + zfree(&alias->topic); + zfree(&alias->pmu_name); + parse_events_terms__exit(&alias->terms); + free(alias); } =20 static void perf_pmu__del_aliases(struct perf_pmu *pmu) { - struct perf_pmu_alias *alias, *tmp; + struct hashmap_entry *entry; + size_t bkt; =20 - list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { - list_del(&alias->list); - perf_pmu_free_alias(alias); - } + if (!pmu->aliases) + return; + + hashmap__for_each_entry(pmu->aliases, entry, bkt) + perf_pmu_free_alias(entry->pvalue); + + hashmap__free(pmu->aliases); + pmu->aliases =3D NULL; } =20 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, @@ -444,35 +451,37 @@ static struct perf_pmu_alias *perf_pmu__find_alias(st= ruct perf_pmu *pmu, bool load) { struct perf_pmu_alias *alias; + bool has_sysfs_event; + char event_file_name[FILENAME_MAX + 8]; =20 - if (load && !pmu->sysfs_aliases_loaded) { - bool has_sysfs_event; - char event_file_name[FILENAME_MAX + 8]; + if (hashmap__find(pmu->aliases, name, &alias)) + return alias; =20 - /* - * Test if alias/event 'name' exists in the PMU's sysfs/events - * directory. If not skip parsing the sysfs aliases. Sysfs event - * name must be all lower or all upper case. - */ - scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name); - for (size_t i =3D 7, n =3D 7 + strlen(name); i < n; i++) - event_file_name[i] =3D tolower(event_file_name[i]); + if (!load || pmu->sysfs_aliases_loaded) + return NULL; =20 - has_sysfs_event =3D perf_pmu__file_exists(pmu, event_file_name); - if (!has_sysfs_event) { - for (size_t i =3D 7, n =3D 7 + strlen(name); i < n; i++) - event_file_name[i] =3D toupper(event_file_name[i]); + /* + * Test if alias/event 'name' exists in the PMU's sysfs/events + * directory. If not skip parsing the sysfs aliases. Sysfs event + * name must be all lower or all upper case. + */ + scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name); + for (size_t i =3D 7, n =3D 7 + strlen(name); i < n; i++) + event_file_name[i] =3D tolower(event_file_name[i]); =20 - has_sysfs_event =3D perf_pmu__file_exists(pmu, event_file_name); - } - if (has_sysfs_event) - pmu_aliases_parse(pmu); + has_sysfs_event =3D perf_pmu__file_exists(pmu, event_file_name); + if (!has_sysfs_event) { + for (size_t i =3D 7, n =3D 7 + strlen(name); i < n; i++) + event_file_name[i] =3D toupper(event_file_name[i]); =20 + has_sysfs_event =3D perf_pmu__file_exists(pmu, event_file_name); } - list_for_each_entry(alias, &pmu->aliases, list) { - if (!strcasecmp(alias->name, name)) + if (has_sysfs_event) { + pmu_aliases_parse(pmu); + if (hashmap__find(pmu->aliases, name, &alias)) return alias; } + return NULL; } =20 @@ -555,7 +564,7 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, co= nst char *name, const char *desc, const char *val, FILE *val_fd, const struct pmu_event *pe, enum event_source src) { - struct perf_pmu_alias *alias; + struct perf_pmu_alias *alias, *old_alias; int ret =3D 0; const char *long_desc =3D NULL, *topic =3D NULL, *unit =3D NULL, *pmu_nam= e =3D NULL; bool deprecated =3D false, perpkg =3D false; @@ -648,7 +657,8 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, co= nst char *name, break; =20 } - list_add_tail(&alias->list, &pmu->aliases); + hashmap__set(pmu->aliases, alias->name, alias, /*old_key=3D*/ NULL, &old_= alias); + perf_pmu_free_alias(old_alias); return 0; } =20 @@ -1136,43 +1146,77 @@ perf_pmu__arch_init(struct perf_pmu *pmu) pmu->mem_events =3D perf_mem_events; } =20 +/* Variant of str_hash that does tolower on each character. */ +static size_t aliases__hash(long key, void *ctx __maybe_unused) +{ + const char *s =3D (const char *)key; + size_t h =3D 0; + + while (*s) { + h =3D h * 31 + tolower(*s); + s++; + } + return h; +} + +static bool aliases__equal(long key1, long key2, void *ctx __maybe_unused) +{ + return strcasecmp((const char *)key1, (const char *)key2) =3D=3D 0; +} + +int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name) +{ + pmu->type =3D type; + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->caps); + + pmu->name =3D strdup(name); + if (!pmu->name) + return -ENOMEM; + + pmu->aliases =3D hashmap__new(aliases__hash, aliases__equal, /*ctx=3D*/ N= ULL); + if (!pmu->aliases) + return -ENOMEM; + + return 0; +} + struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const= char *name, bool eager_load) { struct perf_pmu *pmu; - __u32 type; =20 pmu =3D zalloc(sizeof(*pmu)); if (!pmu) return NULL; =20 - pmu->name =3D strdup(name); - if (!pmu->name) - goto err; + if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, name) !=3D 0) { + perf_pmu__delete(pmu); + return NULL; + } =20 /* * 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); + if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &pmu->type) !=3D 1) { + perf_pmu__delete(pmu); + return NULL; + } =20 /* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right * now. */ - if (pmu_format(pmu, dirfd, name, eager_load)) - goto err; + if (pmu_format(pmu, dirfd, name, eager_load)) { + perf_pmu__delete(pmu); + return NULL; + } =20 pmu->is_core =3D is_pmu_core(name); pmu->cpus =3D pmu_cpumask(dirfd, name, pmu->is_core); =20 - pmu->type =3D type; pmu->is_uncore =3D pmu_is_uncore(dirfd, name); if (pmu->is_uncore) pmu->id =3D pmu_id(name); @@ -1194,10 +1238,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *= pmus, int dirfd, const char pmu_aliases_parse_eager(pmu, dirfd); =20 return pmu; -err: - zfree(&pmu->name); - free(pmu); - return NULL; } =20 /* Creates the PMU when sysfs scanning fails. */ @@ -1219,7 +1259,7 @@ struct perf_pmu *perf_pmu__create_placeholder_core_pm= u(struct list_head *core_pm pmu->cpus =3D cpu_map__online(); =20 INIT_LIST_HEAD(&pmu->format); - INIT_LIST_HEAD(&pmu->aliases); + pmu->aliases =3D hashmap__new(aliases__hash, aliases__equal, /*ctx=3D*/ N= ULL); INIT_LIST_HEAD(&pmu->caps); list_add_tail(&pmu->list, core_pmus); return pmu; @@ -1979,13 +2019,14 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, = bool skip_duplicate_pmus, void *state, pmu_event_callback cb) { char buf[1024]; - struct perf_pmu_alias *event; struct pmu_event_info info =3D { .pmu =3D pmu, .event_type_desc =3D "Kernel PMU event", }; int ret =3D 0; struct strbuf sb; + struct hashmap_entry *entry; + size_t bkt; =20 if (perf_pmu__is_hwmon(pmu)) return hwmon_pmu__for_each_event(pmu, state, cb); @@ -1993,7 +2034,8 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bo= ol skip_duplicate_pmus, strbuf_init(&sb, /*hint=3D*/ 0); pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); - list_for_each_entry(event, &pmu->aliases, list) { + hashmap__for_each_entry(pmu->aliases, entry, bkt) { + struct perf_pmu_alias *event =3D entry->pvalue; size_t buf_used, pmu_name_len; =20 if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(event->name)) @@ -2461,6 +2503,9 @@ int perf_pmu__pathname_fd(int dirfd, const char *pmu_= name, const char *filename, =20 void perf_pmu__delete(struct perf_pmu *pmu) { + if (!pmu) + return; + if (perf_pmu__is_hwmon(pmu)) hwmon_pmu__exit(pmu); =20 @@ -2478,14 +2523,16 @@ void perf_pmu__delete(struct perf_pmu *pmu) =20 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) { - struct perf_pmu_alias *event; + struct hashmap_entry *entry; + size_t bkt; =20 if (!pmu) return NULL; =20 pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); - list_for_each_entry(event, &pmu->aliases, list) { + hashmap__for_each_entry(pmu->aliases, entry, bkt) { + struct perf_pmu_alias *event =3D entry->pvalue; struct perf_event_attr attr =3D {.config =3D 0,}; =20 int ret =3D perf_pmu__config(pmu, &attr, &event->terms, /*apply_hardcode= d=3D*/true, diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index a1fdd6d50c53..71b8636fd07d 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -14,6 +14,7 @@ #include "mem-events.h" =20 struct evsel_config_term; +struct hashmap; struct perf_cpu_map; struct print_callbacks; =20 @@ -125,7 +126,7 @@ struct perf_pmu { * event read from /bus/event_source/devices//events/ or * from json events in pmu-events.c. */ - struct list_head aliases; + struct hashmap *aliases; /** * @events_table: The events table for json events in pmu-events.c. */ @@ -294,6 +295,7 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, int perf_pmu__event_source_devices_fd(void); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags); =20 +int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name); struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const= char *lookup_name, bool eager_load); struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *c= ore_pmus); diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index 727a10e3f990..4630b8cc8e52 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -502,19 +502,12 @@ struct perf_pmu *tool_pmu__new(void) struct perf_pmu *tool =3D zalloc(sizeof(struct perf_pmu)); =20 if (!tool) - goto out; - tool->name =3D strdup("tool"); - if (!tool->name) { - zfree(&tool); - goto out; - } + return NULL; =20 - tool->type =3D PERF_PMU_TYPE_TOOL; - INIT_LIST_HEAD(&tool->aliases); - INIT_LIST_HEAD(&tool->caps); - INIT_LIST_HEAD(&tool->format); + if (perf_pmu__init(tool, PERF_PMU_TYPE_TOOL, "tool") !=3D 0) { + perf_pmu__delete(tool); + return NULL; + } tool->events_table =3D find_core_events_table("common", "common"); - -out: return tool; } --=20 2.49.0.1045.g170613ef41-goog From nobody Fri Dec 19 00:20:07 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB0F4299935 for ; Mon, 12 May 2025 19:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079197; cv=none; b=EylDHRdhjjn2PPyxHbsq6j4SSFg/3mkcAT4RfhQ9H2X60Lt3dfKx55jlScob4GjGvZyK6mkTNVDAFktfKvBEZiGJxmyHfVQ/eoU59t6DMQSacxQGYhOD80tmWbu4OxdP35v6pOWVyQLMx8BkB4nBdkG9z3xXDioSAHOO1AuGgeQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747079197; c=relaxed/simple; bh=EJq4d6V/TegRo06UFWSmMrSXZ+vsTgE2iby9MZsZ8hk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Taoj09epbv2TTzw5LykICTvqKWslOG7vZcIFIzsJTdowsWzC7LkrCkOdDBW5/0W/8kDfZ1U5pBcrW7N1+tAbN/GeuuOkiTw+Xe9qGAMGvCIMcypTAxbf3y4WHq4nciQJGYZn71TTl2vi0Z6Rg35o+/mDTmpMSVDXocOgyOsKGfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4i2XI67w; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4i2XI67w" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2075419ff6so2635952a12.2 for ; Mon, 12 May 2025 12:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747079195; x=1747683995; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=gw0lJdAlfTHt8Lthll3NS6E8Hn93bhsZ2X/iJcYDreI=; b=4i2XI67wcGZIlxykhSIHwqDX4QZeUDJ2WoWKdQoPqUSLi7/VXlsaqQY7qoUtACmXma /ygu8aeTLEfe3q54C+6y0y1TQTRUd2VEXI+K0KFXM0jxPJ066FQaC7hZoe3znZPnZPC+ i6YffN+DvT5KmtXBAMdnrGKDSFrRri4jp3l3eFda6A0B8PhD5gPJFRBfOnkUvdkc8fu8 suRZAh9tb1uXTSDdERqCjPtOxZCaB6XoVFiXGH0gA+6WPFR22YeRgv/FNY58urILkv/i giGqzRfGQq7x5dqv8nACVraEJsT4bhyVcsSGW+BMbTmxJWrPSCVl5lCgyXrsuFRG/j52 Xruw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747079195; x=1747683995; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gw0lJdAlfTHt8Lthll3NS6E8Hn93bhsZ2X/iJcYDreI=; b=YuAvcL54jXtbp5d/ZNewTBmXbB2jVo/dFbOxZksQv0DU0eBhLa9OUN1aVcBNRXaJKf oTG9wHmmfrvAtNDICardnmMXF/BdnCAR0MA/gSbgtjvFJrbV2E1e4+JGtU/w57KvZGac eyKPMr3bkSIpWG6cAtgT/Oc0mWzyzt4O0/6LZ6V5EdXpdTG3r3wluhTcDidS8lNKJA2/ 8IH9EXwZzJmytU5LJ+JmqEQZECl85JNfKW4fERVK4RFW4dpgMzbPqC3r3PzVyWq/A5Cm eIyWffhKVVVx+kr7i/9zt/sknvfi1C+ffM3CBH/qiZEdZ4xTSZll7VhavPcWPT0CeSfN bu6g== X-Forwarded-Encrypted: i=1; AJvYcCW3JVGdn3jwEQF933C+eL+uXfaVptB8/0RPPzpbsA3EOgXlBxSb6DjdHKzh/6+r3ek9bBncZuLti68qmIU=@vger.kernel.org X-Gm-Message-State: AOJu0YyOth3mOHiz6BV6X1QehWUjiWOn7cKQXQHHiO/BSOYMsfocMt9Q rl89ysFO4SjgBflMHf/okjACP44xx/CsdduXE6yr8x0wYwtOEr/L/hBGBKZM/iTGY36ONLwbacE Z9JYT+Q== X-Google-Smtp-Source: AGHT+IFeclwNkDo16EN497pqyxKZSWdPuUX6eSGCdQy3YwKZjYh4ntKKgx+X8NACMVCsKFLx83JvP47yypmd X-Received: from plar20.prod.google.com ([2002:a17:902:c7d4:b0:230:136b:a034]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:22d2:b0:215:b75f:a1cb with SMTP id d9443c01a7336-22fc8b0fa32mr198814715ad.9.1747079194915; Mon, 12 May 2025 12:46:34 -0700 (PDT) Date: Mon, 12 May 2025 12:46:22 -0700 In-Reply-To: <20250512194622.33258-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250512194622.33258-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: <20250512194622.33258-4-irogers@google.com> Subject: [PATCH v2 3/3] perf metricgroup: Binary search when resolving referred to metrics From: Ian Rogers To: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Xu Yang , Thomas Richter , Ravi Bangoria , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unlike with events, metrics can be matched by name or a list of metric groups. However, when a metric refers to another metric it isn't referring to a group but the singular metric in question. Prior to this change every "id" in a metric expression is checked to see if it is a metric by scanning all the metrics in the metrics table. As the table is sorted my metric name we can speed the search in the resolution case by binary searching for the metric. Rename some of the metricgroup functions to make it clearer whether they match a metric by name or by both name and group. Before: ``` $ time perf test -v 10 10: PMU JSON event tests : 10.1: PMU event table sanity : Ok 10.2: PMU event map aliases : Ok 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok 10.5: Parsing of metric thresholds with fake PMUs : Ok real 0m15.972s user 0m13.176s sys 0m3.001s ``` After: ``` $ time perf test -v 10 10: PMU JSON event tests : 10.1: PMU event table sanity : Ok 10.2: PMU event map aliases : Ok 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok 10.5: Parsing of metric thresholds with fake PMUs : Ok real 0m5.343s user 0m1.871s sys 0m2.128s ``` Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 6 +- tools/perf/pmu-events/empty-pmu-events.c | 66 ++++++++++++++- tools/perf/pmu-events/jevents.py | 66 ++++++++++++++- tools/perf/pmu-events/pmu-events.h | 23 +++-- tools/perf/util/metricgroup.c | 102 +++++++++-------------- tools/perf/util/metricgroup.h | 2 +- 6 files changed, 192 insertions(+), 73 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 300b6393bb41..bf0e5e12d992 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1854,7 +1854,7 @@ static int add_default_events(void) * will use this approach. To determine transaction support * on an architecture test for such a metric name. */ - if (!metricgroup__has_metric(pmu, "transaction")) { + if (!metricgroup__has_metric_or_groups(pmu, "transaction")) { pr_err("Missing transaction metrics\n"); ret =3D -1; goto out; @@ -1888,7 +1888,7 @@ static int add_default_events(void) smi_reset =3D true; } =20 - if (!metricgroup__has_metric(pmu, "smi")) { + if (!metricgroup__has_metric_or_groups(pmu, "smi")) { pr_err("Missing smi metrics\n"); ret =3D -1; goto out; @@ -1978,7 +1978,7 @@ static int add_default_events(void) * Add TopdownL1 metrics if they exist. To minimize * multiplexing, don't request threshold computation. */ - if (metricgroup__has_metric(pmu, "Default")) { + if (metricgroup__has_metric_or_groups(pmu, "Default")) { struct evlist *metric_evlist =3D evlist__new(); =20 if (!metric_evlist) { diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 0361bcc1eb58..d4017007a991 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -449,7 +449,7 @@ int pmu_events_table__find_event(const struct pmu_event= s_table *table, const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; int ret; =20 - if (!perf_pmu__name_wildcard_match(pmu, pmu_name)) + if (pmu && !perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__find_event_pmu(table, table_pmu,= name, fn, data); @@ -495,6 +495,49 @@ static int pmu_metrics_table__for_each_metric_pmu(cons= t struct pmu_metrics_table return 0; } =20 +static int pmu_metrics_table__find_metric_pmu(const struct pmu_metrics_tab= le *table, + const struct pmu_table_entry *= pmu, + const char *metric, + pmu_metric_iter_fn fn, + void *data) +{ + struct pmu_metric pm =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_metric(pmu->entries[mid].offset, &pm); + + if (!pm.metric_name && !metric) + goto do_call; + + if (!pm.metric_name && metric) { + low =3D mid + 1; + continue; + } + if (pm.metric_name && !metric) { + high =3D mid - 1; + continue; + } + + cmp =3D strcmp(pm.metric_name, metric); + if (cmp < 0) { + low =3D mid + 1; + continue; + } + if (cmp > 0) { + high =3D mid - 1; + continue; + } + do_call: + return fn ? fn(&pm, table, data) : 0; + } + return PMU_METRICS__NOT_FOUND; +} + int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) @@ -509,6 +552,27 @@ int pmu_metrics_table__for_each_metric(const struct pm= u_metrics_table *table, return 0; } =20 +int pmu_metrics_table__find_metric(const struct pmu_metrics_table *table, + struct perf_pmu *pmu, + const char *metric, + pmu_metric_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 && !perf_pmu__name_wildcard_match(pmu, pmu_name)) + continue; + + ret =3D pmu_metrics_table__find_metric_pmu(table, table_pm= u, metric, fn, data); + if (ret !=3D PMU_METRICS__NOT_FOUND) + return ret; + } + return PMU_METRICS__NOT_FOUND; +} + static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) { static struct { diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index e3a55486c08e..a1899f35ec74 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -972,7 +972,7 @@ int pmu_events_table__find_event(const struct pmu_event= s_table *table, const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; int ret; =20 - if (!perf_pmu__name_wildcard_match(pmu, pmu_name)) + if (pmu && !perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__find_event_pmu(table, table_pmu,= name, fn, data); @@ -1018,6 +1018,49 @@ static int pmu_metrics_table__for_each_metric_pmu(co= nst struct pmu_metrics_table return 0; } =20 +static int pmu_metrics_table__find_metric_pmu(const struct pmu_metrics_tab= le *table, + const struct pmu_table_entry *= pmu, + const char *metric, + pmu_metric_iter_fn fn, + void *data) +{ + struct pmu_metric pm =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_metric(pmu->entries[mid].offset, &pm); + + if (!pm.metric_name && !metric) + goto do_call; + + if (!pm.metric_name && metric) { + low =3D mid + 1; + continue; + } + if (pm.metric_name && !metric) { + high =3D mid - 1; + continue; + } + + cmp =3D strcmp(pm.metric_name, metric); + if (cmp < 0) { + low =3D mid + 1; + continue; + } + if (cmp > 0) { + high =3D mid - 1; + continue; + } + do_call: + return fn ? fn(&pm, table, data) : 0; + } + return PMU_METRICS__NOT_FOUND; +} + int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data) @@ -1032,6 +1075,27 @@ int pmu_metrics_table__for_each_metric(const struct = pmu_metrics_table *table, return 0; } =20 +int pmu_metrics_table__find_metric(const struct pmu_metrics_table *table, + struct perf_pmu *pmu, + const char *metric, + pmu_metric_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 && !perf_pmu__name_wildcard_match(pmu, pmu_name)) + continue; + + ret =3D pmu_metrics_table__find_metric_pmu(table, table_pm= u, metric, fn, data); + if (ret !=3D PMU_METRICS__NOT_FOUND) + return ret; + } + return PMU_METRICS__NOT_FOUND; +} + static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu) { static struct { diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu= -events.h index a95fee561622..a523936846e0 100644 --- a/tools/perf/pmu-events/pmu-events.h +++ b/tools/perf/pmu-events/pmu-events.h @@ -74,6 +74,7 @@ struct pmu_events_table; struct pmu_metrics_table; =20 #define PMU_EVENTS__NOT_FOUND -1000 +#define PMU_METRICS__NOT_FOUND -1000 =20 typedef int (*pmu_event_iter_fn)(const struct pmu_event *pe, const struct pmu_events_table *table, @@ -88,11 +89,11 @@ int pmu_events_table__for_each_event(const struct pmu_e= vents_table *table, pmu_event_iter_fn fn, void *data); /* - * Search for table and entry matching with pmu__name_match. Each matching= event - * has fn called on it. 0 implies to success/continue the search while non= -zero - * means to terminate. The special value PMU_EVENTS__NOT_FOUND is used to - * indicate no event was found in one of the tables which doesn't terminat= e the - * search of all tables. + * Search for a table and entry matching with pmu__name_wildcard_match or = any + * tables if pmu is NULL. Each matching event has fn called on it. 0 impli= es to + * success/continue the search while non-zero means to terminate. The spec= ial + * value PMU_EVENTS__NOT_FOUND is used to indicate no event was found in o= ne of + * the tables which doesn't terminate the search of all tables. */ int pmu_events_table__find_event(const struct pmu_events_table *table, struct perf_pmu *pmu, @@ -104,6 +105,18 @@ size_t pmu_events_table__num_events(const struct pmu_e= vents_table *table, =20 int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *tab= le, pmu_metric_iter_fn fn, void *data); +/* + * Search for a table and entry matching with pmu__name_wildcard_match or = any + * tables if pmu is NULL. Each matching metric has fn called on it. 0 impl= ies to + * success/continue the search while non-zero means to terminate. The spec= ial + * value PMU_METRICS__NOT_FOUND is used to indicate no metric was found in= one + * of the tables which doesn't terminate the search of all tables. + */ +int pmu_metrics_table__find_metric(const struct pmu_metrics_table *table, + struct perf_pmu *pmu, + const char *metric, + pmu_metric_iter_fn fn, + void *data); =20 const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu= *pmu); const struct pmu_metrics_table *pmu_metrics_table__find(void); diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 46920ebadfd1..126a631686b0 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -353,7 +353,7 @@ static int setup_metric_events(const char *pmu, struct = hashmap *ids, return 0; } =20 -static bool match_metric(const char *metric_or_groups, const char *sought) +static bool match_metric_or_groups(const char *metric_or_groups, const cha= r *sought) { int len; char *m; @@ -369,18 +369,19 @@ static bool match_metric(const char *metric_or_groups= , const char *sought) (metric_or_groups[len] =3D=3D 0 || metric_or_groups[len] =3D=3D ';')) return true; m =3D strchr(metric_or_groups, ';'); - return m && match_metric(m + 1, sought); + return m && match_metric_or_groups(m + 1, sought); } =20 -static bool match_pm_metric(const struct pmu_metric *pm, const char *pmu, = const char *metric) +static bool match_pm_metric_or_groups(const struct pmu_metric *pm, const c= har *pmu, + const char *metric_or_groups) { const char *pm_pmu =3D pm->pmu ?: "cpu"; =20 if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu)) return false; =20 - return match_metric(pm->metric_group, metric) || - match_metric(pm->metric_name, metric); + return match_metric_or_groups(pm->metric_group, metric_or_groups) || + match_metric_or_groups(pm->metric_name, metric_or_groups); } =20 /** struct mep - RB-tree node for building printing information. */ @@ -802,11 +803,6 @@ struct metricgroup_add_iter_data { const struct pmu_metrics_table *table; }; =20 -static bool metricgroup__find_metric(const char *pmu, - const char *metric, - const struct pmu_metrics_table *table, - struct pmu_metric *pm); - static int add_metric(struct list_head *metric_list, const struct pmu_metric *pm, const char *modifier, @@ -818,6 +814,16 @@ static int add_metric(struct list_head *metric_list, const struct visited_metric *visited, const struct pmu_metrics_table *table); =20 +static int metricgroup__find_metric_callback(const struct pmu_metric *pm, + const struct pmu_metrics_table *table __maybe_unused, + void *vdata) +{ + struct pmu_metric *copied_pm =3D vdata; + + memcpy(copied_pm, pm, sizeof(*pm)); + return 0; +} + /** * resolve_metric - Locate metrics within the root metric and recursively = add * references to them. @@ -838,7 +844,7 @@ static int add_metric(struct list_head *metric_list, * architecture perf is running upon. */ static int resolve_metric(struct list_head *metric_list, - const char *pmu, + struct perf_pmu *pmu, const char *modifier, bool metric_no_group, bool metric_no_threshold, @@ -868,7 +874,9 @@ static int resolve_metric(struct list_head *metric_list, hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) { struct pmu_metric pm; =20 - if (metricgroup__find_metric(pmu, cur->pkey, table, &pm)) { + if (pmu_metrics_table__find_metric(table, pmu, cur->pkey, + metricgroup__find_metric_callback, + &pm) !=3D PMU_METRICS__NOT_FOUND) { pending =3D realloc(pending, (pending_cnt + 1) * sizeof(struct to_resolve)); if (!pending) @@ -1019,7 +1027,12 @@ static int __add_metric(struct list_head *metric_lis= t, } if (!ret) { /* Resolve referenced metrics. */ - const char *pmu =3D pm->pmu ?: "cpu"; + struct perf_pmu *pmu; + + if (pm->pmu && pm->pmu[0] !=3D '\0') + pmu =3D perf_pmus__find(pm->pmu); + else + pmu =3D perf_pmus__scan_core(/*pmu=3D*/ NULL); =20 ret =3D resolve_metric(metric_list, pmu, modifier, metric_no_group, metric_no_threshold, user_requested_cpu_list, @@ -1036,44 +1049,6 @@ static int __add_metric(struct list_head *metric_lis= t, return ret; } =20 -struct metricgroup__find_metric_data { - const char *pmu; - const char *metric; - struct pmu_metric *pm; -}; - -static int metricgroup__find_metric_callback(const struct pmu_metric *pm, - const struct pmu_metrics_table *table __maybe_unused, - void *vdata) -{ - struct metricgroup__find_metric_data *data =3D vdata; - const char *pm_pmu =3D pm->pmu ?: "cpu"; - - if (strcmp(data->pmu, "all") && strcmp(pm_pmu, data->pmu)) - return 0; - - if (!match_metric(pm->metric_name, data->metric)) - return 0; - - memcpy(data->pm, pm, sizeof(*pm)); - return 1; -} - -static bool metricgroup__find_metric(const char *pmu, - const char *metric, - const struct pmu_metrics_table *table, - struct pmu_metric *pm) -{ - struct metricgroup__find_metric_data data =3D { - .pmu =3D pmu, - .metric =3D metric, - .pm =3D pm, - }; - - return pmu_metrics_table__for_each_metric(table, metricgroup__find_metric= _callback, &data) - ? true : false; -} - static int add_metric(struct list_head *metric_list, const struct pmu_metric *pm, const char *modifier, @@ -1119,7 +1094,7 @@ static int metricgroup__add_metric_sys_event_iter(con= st struct pmu_metric *pm, struct metricgroup_add_iter_data *d =3D data; int ret; =20 - if (!match_pm_metric(pm, d->pmu, d->metric_name)) + if (!match_pm_metric_or_groups(pm, d->pmu, d->metric_name)) return 0; =20 ret =3D add_metric(d->metric_list, pm, d->modifier, d->metric_no_group, @@ -1200,9 +1175,9 @@ static int metricgroup__add_metric_callback(const str= uct pmu_metric *pm, struct metricgroup__add_metric_data *data =3D vdata; int ret =3D 0; =20 - if (pm->metric_expr && match_pm_metric(pm, data->pmu, data->metric_name))= { + if (pm->metric_expr && match_pm_metric_or_groups(pm, data->pmu, data->met= ric_name)) { bool metric_no_group =3D data->metric_no_group || - match_metric(pm->metricgroup_no_group, data->metric_name); + match_metric_or_groups(pm->metricgroup_no_group, data->metric_name); =20 data->has_match =3D true; ret =3D add_metric(data->list, pm, data->modifier, metric_no_group, @@ -1723,29 +1698,32 @@ int metricgroup__parse_groups_test(struct evlist *e= vlist, =20 struct metricgroup__has_metric_data { const char *pmu; - const char *metric; + const char *metric_or_groups; }; -static int metricgroup__has_metric_callback(const struct pmu_metric *pm, - const struct pmu_metrics_table *table __maybe_unused, - void *vdata) +static int metricgroup__has_metric_or_groups_callback(const struct pmu_met= ric *pm, + const struct pmu_metrics_table *table + __maybe_unused, + void *vdata) { struct metricgroup__has_metric_data *data =3D vdata; =20 - return match_pm_metric(pm, data->pmu, data->metric) ? 1 : 0; + return match_pm_metric_or_groups(pm, data->pmu, data->metric_or_groups) ?= 1 : 0; } =20 -bool metricgroup__has_metric(const char *pmu, const char *metric) +bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric= _or_groups) { const struct pmu_metrics_table *table =3D pmu_metrics_table__find(); struct metricgroup__has_metric_data data =3D { .pmu =3D pmu, - .metric =3D metric, + .metric_or_groups =3D metric_or_groups, }; =20 if (!table) return false; =20 - return pmu_metrics_table__for_each_metric(table, metricgroup__has_metric_= callback, &data) + return pmu_metrics_table__for_each_metric(table, + metricgroup__has_metric_or_groups_callback, + &data) ? true : false; } =20 diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index 779f6ede1b51..a04ac1afa6cc 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -85,7 +85,7 @@ int metricgroup__parse_groups_test(struct evlist *evlist, struct rblist *metric_events); =20 void metricgroup__print(const struct print_callbacks *print_cb, void *prin= t_state); -bool metricgroup__has_metric(const char *pmu, const char *metric); +bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric= _or_groups); unsigned int metricgroups__topdown_max_level(void); int arch_get_runtimeparam(const struct pmu_metric *pm); void metricgroup__rblist_exit(struct rblist *metric_events); --=20 2.49.0.1045.g170613ef41-goog