From nobody Sun Feb 8 09:59:08 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 E1CC520B807 for ; Thu, 23 Jan 2025 07:47:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618428; cv=none; b=h+wAuzEZfOaxy7GCfziiWyUdwTqJtVTqxEidCXaX1XxpvLNkXuQf+4N7wgniefbPWPrKI9ep5YvFmEcgEzRlLT040+THXCH9zgeMjNswCIDLaEC2gLV0gjzfbbtPCUYBs8gky6XPcv38Q5AkQ3uQhGVPoS3WHOMbM98Nkb2hh9s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618428; c=relaxed/simple; bh=AuTsH0YHOa++0a0gEpAflS+m3ZAbu91QpN5DGy2CKOY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=mOYr0sdtdv1+uG9yvQ/zzhIV3/RMRVxWtOgyyU4MLtT5LqqlIYeTnEGWQvhQxgmPWYBXvB0Mnumq3fma1FBomhAWAS0npZz+4dDuWV4XGEYMetNwfKVTjHUUPuf1nMmssFZpFUEHICVieJgRSNjO9Th7yGSvLunuK480RI/PoIQ= 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=d0n6VckD; arc=none smtp.client-ip=209.85.219.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="d0n6VckD" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e54da6701d2so1765588276.0 for ; Wed, 22 Jan 2025 23:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737618425; x=1738223225; 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=I/EdS3qTY+09X5JRTcKpgEWVJ9FaQ49oSPLBxs6KryA=; b=d0n6VckDOmVUjBwJ9wbfU4ZId0T4/SkFqtcB70evFI5v6qf6k2t++kzLhr3b0u9iOT RDoQBwrQSRJ9rDt0HliXagRNmeJBmilu7dfFJ/y9/EISW2Uo31rn2cZElMFJ1k17J172 S/o32wvUmQBUZ/zDtfpkARe1L806MmQP22YcsiPy8fy7HZd6AovgFmncGMFis5YxPUvj gCgk9GkDde3t+p+QAq3OSYXQ3wmqAOkPI3/+sp2V7QiVctvD1eHG4JP5N6g9QVYGmlJG iFIGeuxtMtkKY3qFWiq6nOjYUuYLJ+AAB204iDMaMmzkeNsvJ4Kzx1ksDCkQY3mzjQmc owIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737618425; x=1738223225; 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=I/EdS3qTY+09X5JRTcKpgEWVJ9FaQ49oSPLBxs6KryA=; b=Ptr12vRk4si4DONPqOQrj3DmdZ9ggVee/Ekt0snO8jC+Es9nPaHUBV2SQhtbsGazIy HN/9V6Zck9yW22L73sCBHn9g4V3JVEV1PA3OC2fp1uyENoPfVa9PiVOtXDfy8B6SAZq7 iBLmmACGfUCdm9iozm7FmakgM+eu2LBgCEpZgSj50wTrOBreQh2n8+8bJvJXZdLnFO7R j8YpknvFI8dNT4lj/pVohktfnLvzB3E5WOvevflWMACEcLkIzWJqC7mbuCKz0nG3etmC Um+HqWu3IEU7o0tHn56B9yaO0VQFUjatwATr4YZ3leAzkwysIb6qrcbjN/OWOjKtn8Jl 5ZUg== X-Forwarded-Encrypted: i=1; AJvYcCVIQZVjNtaHXQW+Pg7xVWHQ7zikjjT11LzH17F1R7CCNgwVZt/bdoQt6Z7Ucs2e9lqxCrUnbmu6HJ9qv1U=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5jpF6+AgiL45V8opdVAXdl+z/8kiuNfPtjbmrqSyNMiq3S5kj 9BpkjMxoiU6PNFjKhdHhkZPdDFHamAvI+6ubRIZrfw1K/A+QNn/RHtNGvkMGN/NIkx1Io7aRkYt J3CeCzg== X-Google-Smtp-Source: AGHT+IHMAQdwdbEHDozJ5QOQVR6FPmtsPooPKLG29MtxeD9qLiFGcCi8tX3yvDrPEqCugmxty5Z+mz7wStTg X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:2fda:d811:ae83:77e0]) (user=irogers job=sendgmr) by 2002:a81:a884:0:b0:6ee:b693:f752 with SMTP id 00721157ae682-6f6eb519f22mr487327b3.0.1737618424725; Wed, 22 Jan 2025 23:47:04 -0800 (PST) Date: Wed, 22 Jan 2025 23:46:55 -0800 In-Reply-To: <20250123074659.698123-1-irogers@google.com> Message-Id: <20250123074659.698123-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250123074659.698123-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v2 1/5] perf evsel: Reduce scanning core PMUs in is_hybrid From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ze Gao , Weilin Wang , Jean-Philippe Romain , Junhao He , Yicong Yang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" evsel__is_hybrid returns true if there are multiple core PMUs and the evsel is for a core PMU. Determining the number of core PMUs can require loading/scanning PMUs. There's no point doing the scanning if evsel for the is_hybrid test isn't core so reorder the tests to reduce PMU scanning. Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index bc144388f892..ae20691bbfae 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -3856,10 +3856,10 @@ void evsel__zero_per_pkg(struct evsel *evsel) */ bool evsel__is_hybrid(const struct evsel *evsel) { - if (perf_pmus__num_core_pmus() =3D=3D 1) + if (!evsel->core.is_pmu_core) return false; =20 - return evsel->core.is_pmu_core; + return perf_pmus__num_core_pmus() > 1; } =20 struct evsel *evsel__leader(const struct evsel *evsel) --=20 2.48.1.262.g85cc9f2d1e-goog From nobody Sun Feb 8 09:59:08 2026 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 EC2CF20C003 for ; Thu, 23 Jan 2025 07:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618429; cv=none; b=AGb4jnsc/mQGW6WuY5SBmMJrMLeYDvDXbxCNif7isxFe4tMfMmS1H9IR3u5Suerm50SagBDhKMcYoffZpNNSfzmub8wIl3Fl9tB/ssTo69uTMxP17ZfJCPXwYgK6AnNi4oTlM3Mt4MDU4vwG7dvnRkLb6kHQw2zEtI1+xb0VP+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618429; c=relaxed/simple; bh=+M5w/T+041A89vZWNITIEfjyLsENoF3hrRVxva2X7m8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=uAI7reh1t2sOtwf3zhiAzcCIekZzmW4Jyui8etL7LnOcrWFgbuD69O49iTJnhTAa6flY5AIXmjSKoj74LdXvlhFgCVP/Tfchk9eD98OAw2IvxoEPG/+DiCEku9BALin56jLhQQDPy9cdZjeRIq7z99ECgXiZvnlUmcxcQ+g878I= 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=xk7b2dHX; arc=none smtp.client-ip=209.85.219.202 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="xk7b2dHX" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e572f6dee18so1733408276.0 for ; Wed, 22 Jan 2025 23:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737618427; x=1738223227; 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=5wYn6WdcSm32njaJYNLZydBVESxh4P8CNAbIPH/J3a4=; b=xk7b2dHXaa+AwoZ+Frp+DQlO9Dh7agw9lwY05iYdfbzoiCjgiMXObJzANITPxTA6W1 uhxZN0cvxmjrvuyrMttIjrONEfhEeiJwWyh0nvKqbvpVIneEudfX+tKQvJIiuNXfPRAV BtMOm7BzLcKIj1/jxcS1LiQLw1YksglSrLbPuKMDG/AbQvw6onozS7mpc/k6F+CTupUv ybvyUZwSStUaaZUK+KHXNX0FMP2FEEenfeZcv5NGDJCoRTa7GpDCOJYbPsY6pxA2Ufll 50+ykujCB+hlmgOcYkvBBgbaRJuuhMkye9Zj4aeKv84i6kuI+pvI03FgT7jdbN5h21Pd X14Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737618427; x=1738223227; 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=5wYn6WdcSm32njaJYNLZydBVESxh4P8CNAbIPH/J3a4=; b=itAdtms3tztV8YVQVgQcEopDIoIUibuUktLv2rHjM5I8hTQ4GhKNw5bMtv82bUZR1Y 91bM7zxA4CF3wrnmwNf21OOj+F2PFE+HODCKpE1EJfThgOivCNp8xAx96/cHlHN0mqJv 4UkKA9yUbayW8Toa3uVNbyDmxEdD+waEnEhD/wrzCQKPl9rJZS/2CdWMHU8W4ygmj41q XS+5ldAastVQA73wVV6Yn1fxnHwwpQ6Ue4W13tEDka1wFLfroXTYG6dUqRkZ5+jtPvfZ s33ob0EhjKQdq9oyS4aTNG0O5B1q1YoWM7FWX+bzIgcLUVyibKu8YCnhlnZZtVbtnV5r AABA== X-Forwarded-Encrypted: i=1; AJvYcCWkIt5O8qkxds8j7X1mc3cZ5uO26vC6ro4J2T6fchd+KDZbxm9RjyK2J7mjaDZesIBiMq8Lm/+rOoDuiUY=@vger.kernel.org X-Gm-Message-State: AOJu0YzdRcS9S4UMYdeiSNn0nGnDyBWGd01fnFKICwtauAhrQ03XZaRd 7DKpcM44QR9mH6exFi5UxpoFt9eGQXrYl7X4KFZQLTwv6my0rMOXq8IdEito9KTgGEdMx+ushHu 1sWVSHA== X-Google-Smtp-Source: AGHT+IEMVGcccS9YIusJJY8SexHd3ZHVSgJHMDqjabZRZ2BXaosQ36nKl77U5VibVbpUDExMmdKebuFxi+4n X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:2fda:d811:ae83:77e0]) (user=irogers job=sendgmr) by 2002:a05:690c:288f:b0:6e7:e3e4:9d83 with SMTP id 00721157ae682-6f6eb94745amr436007b3.8.1737618426958; Wed, 22 Jan 2025 23:47:06 -0800 (PST) Date: Wed, 22 Jan 2025 23:46:56 -0800 In-Reply-To: <20250123074659.698123-1-irogers@google.com> Message-Id: <20250123074659.698123-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250123074659.698123-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v2 2/5] perf pmus: Restructure pmu_read_sysfs to scan fewer PMUs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ze Gao , Weilin Wang , Jean-Philippe Romain , Junhao He , Yicong Yang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than scanning core or all PMUs, allow pmu_read_sysfs to read some combination of core, other, hwmon and tool PMUs. The PMUs that should be read and are already read are held as bitmaps. It is known that a "hwmon_" prefix is necessary for a hwmon PMU's name, similarly with "tool", so only scan those PMUs in situations the PMU name or the PMU's type number make sense to. The number of openat system calls reduces from 276 to 98 for a hwmon event. The number of openats for regular perf events isn't changed. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.h | 2 + tools/perf/util/pmus.c | 144 ++++++++++++++++++++++++++--------------- 2 files changed, 95 insertions(+), 51 deletions(-) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index dbed6c243a5e..edd36c20aedc 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -37,6 +37,8 @@ struct perf_pmu_caps { }; =20 enum { + PERF_PMU_TYPE_PE_START =3D 0, + PERF_PMU_TYPE_PE_END =3D 0xFFFEFFFF, PERF_PMU_TYPE_HWMON_START =3D 0xFFFF0000, PERF_PMU_TYPE_HWMON_END =3D 0xFFFFFFFD, PERF_PMU_TYPE_TOOL =3D 0xFFFFFFFE, diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index b493da0d22ef..3e3ffafcad71 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -37,10 +37,23 @@ */ static LIST_HEAD(core_pmus); static LIST_HEAD(other_pmus); -static bool read_sysfs_core_pmus; -static bool read_sysfs_all_pmus; +enum perf_tool_pmu_type { + PERF_TOOL_PMU_TYPE_PE_CORE, +#define PERF_TOOL_PMU_TYPE_PE_CORE_MASK (1 << PERF_TOOL_PMU_TYPE_PE_CORE) + PERF_TOOL_PMU_TYPE_PE_OTHER, +#define PERF_TOOL_PMU_TYPE_PE_OTHER_MASK (1 << PERF_TOOL_PMU_TYPE_PE_OTHER) + PERF_TOOL_PMU_TYPE_TOOL, +#define PERF_TOOL_PMU_TYPE_TOOL_MASK (1 << PERF_TOOL_PMU_TYPE_TOOL) + PERF_TOOL_PMU_TYPE_HWMON, +#define PERF_TOOL_PMU_TYPE_HWMON_MASK (1 << PERF_TOOL_PMU_TYPE_HWMON) +#define PERF_TOOL_PMU_TYPE_ALL_MASK (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | \ + PERF_TOOL_PMU_TYPE_PE_OTHER_MASK | \ + PERF_TOOL_PMU_TYPE_TOOL_MASK | \ + PERF_TOOL_PMU_TYPE_HWMON_MASK) +}; +static unsigned int read_pmu_types; =20 -static void pmu_read_sysfs(bool core_only); +static void pmu_read_sysfs(unsigned int to_read_pmus); =20 size_t pmu_name_len_no_suffix(const char *str) { @@ -102,8 +115,7 @@ void perf_pmus__destroy(void) =20 perf_pmu__delete(pmu); } - read_sysfs_core_pmus =3D false; - read_sysfs_all_pmus =3D false; + read_pmu_types =3D 0; } =20 static struct perf_pmu *pmu_find(const char *name) @@ -129,6 +141,7 @@ struct perf_pmu *perf_pmus__find(const char *name) struct perf_pmu *pmu; int dirfd; bool core_pmu; + unsigned int to_read_pmus =3D 0; =20 /* * Once PMU is loaded it stays in the list, @@ -139,11 +152,11 @@ struct perf_pmu *perf_pmus__find(const char *name) if (pmu) return pmu; =20 - if (read_sysfs_all_pmus) + if (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) return NULL; =20 core_pmu =3D is_pmu_core(name); - if (core_pmu && read_sysfs_core_pmus) + if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) return NULL; =20 dirfd =3D perf_pmu__event_source_devices_fd(); @@ -151,15 +164,27 @@ struct perf_pmu *perf_pmus__find(const char *name) /*eager_load=3D*/false); close(dirfd); =20 - if (!pmu) { - /* - * Looking up an inidividual PMU failed. This may mean name is - * an alias, so read the PMUs from sysfs and try to find again. - */ - pmu_read_sysfs(core_pmu); + if (pmu) + return pmu; + + /* Looking up an individual perf event PMU failed, check if a tool PMU sh= ould be read. */ + if (!strncmp(name, "hwmon_", 6)) + to_read_pmus |=3D PERF_TOOL_PMU_TYPE_HWMON_MASK; + else if (!strcmp(name, "tool")) + to_read_pmus |=3D PERF_TOOL_PMU_TYPE_TOOL_MASK; + + if (to_read_pmus) { + pmu_read_sysfs(to_read_pmus); pmu =3D pmu_find(name); + if (pmu) + return pmu; } - return pmu; + /* Read all necessary PMUs from sysfs and see if the PMU is found. */ + to_read_pmus =3D PERF_TOOL_PMU_TYPE_PE_CORE_MASK; + if (!core_pmu) + to_read_pmus |=3D PERF_TOOL_PMU_TYPE_PE_OTHER_MASK; + pmu_read_sysfs(to_read_pmus); + return pmu_find(name); } =20 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) @@ -176,11 +201,11 @@ static struct perf_pmu *perf_pmu__find2(int dirfd, co= nst char *name) if (pmu) return pmu; =20 - if (read_sysfs_all_pmus) + if (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK) return NULL; =20 core_pmu =3D is_pmu_core(name); - if (core_pmu && read_sysfs_core_pmus) + if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK)) return NULL; =20 return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name, @@ -197,52 +222,60 @@ static int pmus_cmp(void *priv __maybe_unused, } =20 /* Add all pmus in sysfs to pmu list: */ -static void pmu_read_sysfs(bool core_only) +static void pmu_read_sysfs(unsigned int to_read_types) { - int fd; - DIR *dir; - struct dirent *dent; struct perf_pmu *tool_pmu; =20 - if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) + if ((read_pmu_types & to_read_types) =3D=3D to_read_types) { + /* All requested PMU types have been read. */ return; + } =20 - fd =3D perf_pmu__event_source_devices_fd(); - if (fd < 0) - return; + if (to_read_types & (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | PERF_TOOL_PMU_TYPE= _PE_OTHER_MASK)) { + int fd =3D perf_pmu__event_source_devices_fd(); + DIR *dir; + struct dirent *dent; + bool core_only =3D (to_read_types & PERF_TOOL_PMU_TYPE_PE_OTHER_MASK) = =3D=3D 0; =20 - dir =3D fdopendir(fd); - if (!dir) { - close(fd); - return; - } + if (fd < 0) + goto skip_pe_pmus; =20 - while ((dent =3D readdir(dir))) { - if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) - continue; - if (core_only && !is_pmu_core(dent->d_name)) - continue; - /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */ - perf_pmu__find2(fd, dent->d_name); - } + dir =3D fdopendir(fd); + if (!dir) { + close(fd); + goto skip_pe_pmus; + } =20 - closedir(dir); - if (list_empty(&core_pmus)) { + while ((dent =3D readdir(dir))) { + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) + continue; + if (core_only && !is_pmu_core(dent->d_name)) + continue; + /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */ + perf_pmu__find2(fd, dent->d_name); + } + + closedir(dir); + } +skip_pe_pmus: + if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&core= _pmus)) { if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) pr_err("Failure to set up any core PMUs\n"); } list_sort(NULL, &core_pmus, pmus_cmp); - if (!core_only) { + + if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) !=3D 0 && + (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) =3D=3D 0) { tool_pmu =3D perf_pmus__tool_pmu(); list_add_tail(&tool_pmu->list, &other_pmus); - perf_pmus__read_hwmon_pmus(&other_pmus); } + if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) !=3D 0 && + (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) =3D=3D 0) + perf_pmus__read_hwmon_pmus(&other_pmus); + list_sort(NULL, &other_pmus, pmus_cmp); - if (!list_empty(&core_pmus)) { - read_sysfs_core_pmus =3D true; - if (!core_only) - read_sysfs_all_pmus =3D true; - } + + read_pmu_types |=3D to_read_types; } =20 static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type) @@ -263,12 +296,21 @@ static struct perf_pmu *__perf_pmus__find_by_type(uns= igned int type) =20 struct perf_pmu *perf_pmus__find_by_type(unsigned int type) { + unsigned int to_read_pmus; struct perf_pmu *pmu =3D __perf_pmus__find_by_type(type); =20 - if (pmu || read_sysfs_all_pmus) + if (pmu || (read_pmu_types =3D=3D PERF_TOOL_PMU_TYPE_ALL_MASK)) return pmu; =20 - pmu_read_sysfs(/*core_only=3D*/false); + if (type >=3D PERF_PMU_TYPE_PE_START && type <=3D PERF_PMU_TYPE_PE_END) { + to_read_pmus =3D PERF_TOOL_PMU_TYPE_PE_CORE_MASK | + PERF_TOOL_PMU_TYPE_PE_OTHER_MASK; + } else if (type >=3D PERF_PMU_TYPE_HWMON_START && type <=3D PERF_PMU_TYPE= _HWMON_END) { + to_read_pmus =3D PERF_TOOL_PMU_TYPE_HWMON_MASK; + } else { + to_read_pmus =3D PERF_TOOL_PMU_TYPE_TOOL_MASK; + } + pmu_read_sysfs(to_read_pmus); pmu =3D __perf_pmus__find_by_type(type); return pmu; } @@ -282,7 +324,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) bool use_core_pmus =3D !pmu || pmu->is_core; =20 if (!pmu) { - pmu_read_sysfs(/*core_only=3D*/false); + pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK); pmu =3D list_prepare_entry(pmu, &core_pmus, list); } if (use_core_pmus) { @@ -300,7 +342,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) { if (!pmu) { - pmu_read_sysfs(/*core_only=3D*/true); + pmu_read_sysfs(PERF_TOOL_PMU_TYPE_PE_CORE_MASK); return list_first_entry_or_null(&core_pmus, typeof(*pmu), list); } list_for_each_entry_continue(pmu, &core_pmus, list) @@ -316,7 +358,7 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates= (struct perf_pmu *pmu) const char *last_pmu_name =3D (pmu && pmu->name) ? pmu->name : ""; =20 if (!pmu) { - pmu_read_sysfs(/*core_only=3D*/false); + pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK); pmu =3D list_prepare_entry(pmu, &core_pmus, list); } else last_pmu_name_len =3D pmu_name_len_no_suffix(pmu->name ?: ""); --=20 2.48.1.262.g85cc9f2d1e-goog From nobody Sun Feb 8 09:59:08 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 43A8F20C026 for ; Thu, 23 Jan 2025 07:47:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618432; cv=none; b=T7oT0nBEPgmUM4Ee3rnoRDqofFrvjco0C8zzrztzUxA7uCp+9jNV0BDw8KMEi9niExQ83VtGqUIRnVspbLqR0Ul5pGE3vfO8zW0b/s1R05cBhsvKk1jVMfwWPvWo8TFNPMg1KVVxC9qorCI6WsI90RO1IPsxlB1p9qJvO4OReFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618432; c=relaxed/simple; bh=jG2kCGtG2rJ+lWrCljx5EeQQSURoS4xAWJbH0BrpahE=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=udlBTIbgCQg70x3ASZdr3dmTrDCVY4Q5STFcm+0zA6tTCJNUv4a9gEGTcwRBpMHZikekS3YJtVV12lSBfhhgwhzxLjnvK8WGgGn/LGLf1y4NwJjdyqUK6vtjgll58TjPFuhW6xceAUWduNsTb0bnqZSKidonjNDUBaq1qxV3QUA= 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=JplR9cUi; arc=none smtp.client-ip=209.85.219.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="JplR9cUi" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e5789a8458eso1702520276.2 for ; Wed, 22 Jan 2025 23:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737618429; x=1738223229; 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=8K/v49xnMr2JokPhET0jiUYUlMMHx0qCh890UpITDmw=; b=JplR9cUiqRTVuilRNyeBoyfb1HYH8TXAyQTsTFsXCmImQ5SsfyAilCoaikD/jHvoMy xd4KGBH5ZqTcJuYD27EuRQgUYNeamorp3iG7H6crDsPLwqgzJqB4zRSxYpjvTDt1R3YW 0Jn/YdgNUHA2PBJovzwjJ9qPlTI5/ZvH0FthUr95slunEYNmXLTlx11Pvob3ag8CjZzJ djuGb441yB4bMCROACPyK/RHp26I0WP64tN+Vjaz5l/1g7iVykHm4k2bM08HHxCt1zm+ q0J/MmHskXWoSAc7JFA/VsRFMPixIhAccu5W5gtr8DFUXXJXoqXhp+2jioy+ds3vhJpu Dayw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737618429; x=1738223229; 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=8K/v49xnMr2JokPhET0jiUYUlMMHx0qCh890UpITDmw=; b=DNfUKMdkbLwy2sZveHnIB0VNrDggWHJxnSe/6X0tlLYnbqmggt54Cn4PZPMgUjRk2h ELbyHkhRbv26qYahLKT7hEzCv+IAGxwKgn1cFy1xYs6t7ExsS/aJ0Y2ZKh125MfY1kyU hlGscDVBJGO3eUhfG3EufCWiwgdf6tcLjhqKQ37WxyAjAAFVFYtHPrkYm7Jq2OgPloEv pq/XqZGLXvqnmsbIIldhW+HodS5E0gECW0IywU8GQQo8mzkFI43Yww9btawyibt82tap LE+2u0mfFy/LHnDrq3Pz9+m57FmkJE8Fl9WJh896dwe1VE1hr/Q6izKCK+L+6ImBfAVb BIyg== X-Forwarded-Encrypted: i=1; AJvYcCVc0/G+1bcAsBxBKkwgTcQ9ShDzlL49hDkk2QAecQJb8nTciiGsyV1cT23sLxgsyuOzhkDH2lL+x7piBcc=@vger.kernel.org X-Gm-Message-State: AOJu0Yx86JwxA2BFmJbbL7MJ2RPXznLdvJWyA+4NcIEPpNeBBJ8kK0EV 29Td+Jayf2rWjcmLygV4/hzEAhrwR3zqX4JtitzYa9pdCdIBhaJSuOiprqtR/3YcEDNz/8yp2q3 QXFjxDA== X-Google-Smtp-Source: AGHT+IEA8Rin5J5saf18kShWbD2NCJItOy8BXMhjHou6Fu5F1sEbwSCsWcr5pln+nxLEi2kXkx9PlvUPs2R1 X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:2fda:d811:ae83:77e0]) (user=irogers job=sendgmr) by 2002:a25:c78c:0:b0:e57:cedb:ba19 with SMTP id 3f1490d57ef6-e57cedbbc59mr35959276.4.1737618429168; Wed, 22 Jan 2025 23:47:09 -0800 (PST) Date: Wed, 22 Jan 2025 23:46:57 -0800 In-Reply-To: <20250123074659.698123-1-irogers@google.com> Message-Id: <20250123074659.698123-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250123074659.698123-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v2 3/5] perf pmu: Rename name matching for no suffix or wildcard variants From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ze Gao , Weilin Wang , Jean-Philippe Romain , Junhao He , Yicong Yang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Wildcard PMU naming will match a name like pmu_1 to a PMU name like pmu_10 but not to a PMU name like pmu_2 as the suffix forms part of the match. No suffix matching will match pmu_10 to either pmu_1 or pmu_2. Add or rename matching functions on PMU to make it clearer what kind of matching is being performed. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 252 ++++++++++++++++------- tools/perf/util/pmu.h | 5 +- 6 files changed, 231 insertions(+), 129 deletions(-) diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-even= ts/empty-pmu-events.c index 1c7a2cfa321f..0cb7ba7912e8 100644 --- a/tools/perf/pmu-events/empty-pmu-events.c +++ b/tools/perf/pmu-events/empty-pmu-events.c @@ -422,7 +422,7 @@ int pmu_events_table__for_each_event(const struct pmu_e= vents_table *table, const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; int ret; =20 - if (pmu && !pmu__name_match(pmu, pmu_name)) + if (pmu && !perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__for_each_event_pmu(table, table_= pmu, fn, data); @@ -443,7 +443,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 (!pmu__name_match(pmu, pmu_name)) + if (!perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__find_event_pmu(table, table_pmu,= name, fn, data); @@ -462,7 +462,7 @@ size_t pmu_events_table__num_events(const struct pmu_ev= ents_table *table, const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; =20 - if (pmu__name_match(pmu, pmu_name)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) count +=3D table_pmu->num_entries; } return count; @@ -581,7 +581,7 @@ const struct pmu_events_table *perf_pmu__find_events_ta= ble(struct perf_pmu *pmu) 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)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) return &map->event_table; } return NULL; diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jeven= ts.py index 3e204700b59a..7499a35bfadd 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -945,7 +945,7 @@ int pmu_events_table__for_each_event(const struct pmu_e= vents_table *table, const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; int ret; =20 - if (pmu && !pmu__name_match(pmu, pmu_name)) + if (pmu && !perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__for_each_event_pmu(table, table_= pmu, fn, data); @@ -966,7 +966,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 (!pmu__name_match(pmu, pmu_name)) + if (!perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; =20 ret =3D pmu_events_table__find_event_pmu(table, table_pmu,= name, fn, data); @@ -985,7 +985,7 @@ size_t pmu_events_table__num_events(const struct pmu_ev= ents_table *table, const struct pmu_table_entry *table_pmu =3D &table->pmus[i= ]; const char *pmu_name =3D &big_c_string[table_pmu->pmu_name= .offset]; =20 - if (pmu__name_match(pmu, pmu_name)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) count +=3D table_pmu->num_entries; } return count; @@ -1104,7 +1104,7 @@ const struct pmu_events_table *perf_pmu__find_events_= table(struct perf_pmu *pmu) 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)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) return &map->event_table; } return NULL; diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index 6a681e3fb552..4a9f8e090cf4 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -452,9 +452,9 @@ static int test__name_cmp(struct test_suite *test __may= be_unused, int subtest __ } =20 /** - * Test perf_pmu__match() that's used to search for a PMU given a name pas= sed + * Test perf_pmu__wildcard_match() that's used to search for a PMU given a= name passed * on the command line. The name that's passed may also be a filename type= glob - * match. If the name does not match, perf_pmu__match() attempts to match = the + * match. If the name does not match, perf_pmu__wildcard_match() attempts = to match the * alias of the PMU, if provided. */ static int test__pmu_match(struct test_suite *test __maybe_unused, int sub= test __maybe_unused) @@ -463,41 +463,44 @@ static int test__pmu_match(struct test_suite *test __= maybe_unused, int subtest _ .name =3D "pmuname", }; =20 - TEST_ASSERT_EQUAL("Exact match", perf_pmu__match(&test_pmu, "pmuname"), = true); - TEST_ASSERT_EQUAL("Longer token", perf_pmu__match(&test_pmu, "longertoken= "), false); - TEST_ASSERT_EQUAL("Shorter token", perf_pmu__match(&test_pmu, "pmu"), = false); +#define TEST_PMU_MATCH(msg, to_match, expect) \ + TEST_ASSERT_EQUAL(msg, perf_pmu__wildcard_match(&test_pmu, to_match), exp= ect) + + TEST_PMU_MATCH("Exact match", "pmuname", true); + TEST_PMU_MATCH("Longer token", "longertoken", false); + TEST_PMU_MATCH("Shorter token", "pmu", false); =20 test_pmu.name =3D "pmuname_10"; - TEST_ASSERT_EQUAL("Diff suffix_", perf_pmu__match(&test_pmu, "pmuname_2")= , false); - TEST_ASSERT_EQUAL("Sub suffix_", perf_pmu__match(&test_pmu, "pmuname_1")= , true); - TEST_ASSERT_EQUAL("Same suffix_", perf_pmu__match(&test_pmu, "pmuname_10"= ), true); - TEST_ASSERT_EQUAL("No suffix_", perf_pmu__match(&test_pmu, "pmuname"), = true); - TEST_ASSERT_EQUAL("Underscore_", perf_pmu__match(&test_pmu, "pmuname_"),= true); - TEST_ASSERT_EQUAL("Substring_", perf_pmu__match(&test_pmu, "pmuna"), = false); + TEST_PMU_MATCH("Diff suffix_", "pmuname_2", false); + TEST_PMU_MATCH("Sub suffix_", "pmuname_1", true); + TEST_PMU_MATCH("Same suffix_", "pmuname_10", true); + TEST_PMU_MATCH("No suffix_", "pmuname", true); + TEST_PMU_MATCH("Underscore_", "pmuname_", true); + TEST_PMU_MATCH("Substring_", "pmuna", false); =20 test_pmu.name =3D "pmuname_ab23"; - TEST_ASSERT_EQUAL("Diff suffix hex_", perf_pmu__match(&test_pmu, "pmuname= _2"), false); - TEST_ASSERT_EQUAL("Sub suffix hex_", perf_pmu__match(&test_pmu, "pmuname= _ab"), true); - TEST_ASSERT_EQUAL("Same suffix hex_", perf_pmu__match(&test_pmu, "pmuname= _ab23"), true); - TEST_ASSERT_EQUAL("No suffix hex_", perf_pmu__match(&test_pmu, "pmuname= "), true); - TEST_ASSERT_EQUAL("Underscore hex_", perf_pmu__match(&test_pmu, "pmuname= _"), true); - TEST_ASSERT_EQUAL("Substring hex_", perf_pmu__match(&test_pmu, "pmuna")= , false); + TEST_PMU_MATCH("Diff suffix hex_", "pmuname_2", false); + TEST_PMU_MATCH("Sub suffix hex_", "pmuname_ab", true); + TEST_PMU_MATCH("Same suffix hex_", "pmuname_ab23", true); + TEST_PMU_MATCH("No suffix hex_", "pmuname", true); + TEST_PMU_MATCH("Underscore hex_", "pmuname_", true); + TEST_PMU_MATCH("Substring hex_", "pmuna", false); =20 test_pmu.name =3D "pmuname10"; - TEST_ASSERT_EQUAL("Diff suffix", perf_pmu__match(&test_pmu, "pmuname2"), = false); - TEST_ASSERT_EQUAL("Sub suffix", perf_pmu__match(&test_pmu, "pmuname1"), = true); - TEST_ASSERT_EQUAL("Same suffix", perf_pmu__match(&test_pmu, "pmuname10"),= true); - TEST_ASSERT_EQUAL("No suffix", perf_pmu__match(&test_pmu, "pmuname"), = true); - TEST_ASSERT_EQUAL("Underscore", perf_pmu__match(&test_pmu, "pmuname_"), = false); - TEST_ASSERT_EQUAL("Substring", perf_pmu__match(&test_pmu, "pmuna"), = false); + TEST_PMU_MATCH("Diff suffix", "pmuname2", false); + TEST_PMU_MATCH("Sub suffix", "pmuname1", true); + TEST_PMU_MATCH("Same suffix", "pmuname10", true); + TEST_PMU_MATCH("No suffix", "pmuname", true); + TEST_PMU_MATCH("Underscore", "pmuname_", false); + TEST_PMU_MATCH("Substring", "pmuna", false); =20 test_pmu.name =3D "pmunameab23"; - TEST_ASSERT_EQUAL("Diff suffix hex", perf_pmu__match(&test_pmu, "pmuname2= "), false); - TEST_ASSERT_EQUAL("Sub suffix hex", perf_pmu__match(&test_pmu, "pmunamea= b"), true); - TEST_ASSERT_EQUAL("Same suffix hex", perf_pmu__match(&test_pmu, "pmunamea= b23"), true); - TEST_ASSERT_EQUAL("No suffix hex", perf_pmu__match(&test_pmu, "pmuname"= ), true); - TEST_ASSERT_EQUAL("Underscore hex", perf_pmu__match(&test_pmu, "pmuname_= "), false); - TEST_ASSERT_EQUAL("Substring hex", perf_pmu__match(&test_pmu, "pmuna"),= false); + TEST_PMU_MATCH("Diff suffix hex", "pmuname2", false); + TEST_PMU_MATCH("Sub suffix hex", "pmunameab", true); + TEST_PMU_MATCH("Same suffix hex", "pmunameab23", true); + TEST_PMU_MATCH("No suffix hex", "pmuname", true); + TEST_PMU_MATCH("Underscore hex", "pmuname_", false); + TEST_PMU_MATCH("Substring hex", "pmuna", false); =20 /* * 2 hex chars or less are not considered suffixes so it shouldn't be @@ -505,7 +508,7 @@ static int test__pmu_match(struct test_suite *test __ma= ybe_unused, int subtest _ * false results here than above. */ test_pmu.name =3D "pmuname_a3"; - TEST_ASSERT_EQUAL("Diff suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuna= me_2"), false); + TEST_PMU_MATCH("Diff suffix 2 hex_", "pmuname_2", false); /* * This one should be false, but because pmuname_a3 ends in 3 which is * decimal, it's not possible to determine if it's a short hex suffix or @@ -513,19 +516,19 @@ static int test__pmu_match(struct test_suite *test __= maybe_unused, int subtest _ * length of decimal suffix. Run the test anyway and expect the wrong * result. And slightly fuzzy matching shouldn't do too much harm. */ - TEST_ASSERT_EQUAL("Sub suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuna= me_a"), true); - TEST_ASSERT_EQUAL("Same suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuna= me_a3"), true); - TEST_ASSERT_EQUAL("No suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuna= me"), false); - TEST_ASSERT_EQUAL("Underscore 2 hex_", perf_pmu__match(&test_pmu, "pmuna= me_"), false); - TEST_ASSERT_EQUAL("Substring 2 hex_", perf_pmu__match(&test_pmu, "pmuna= "), false); + TEST_PMU_MATCH("Sub suffix 2 hex_", "pmuname_a", true); + TEST_PMU_MATCH("Same suffix 2 hex_", "pmuname_a3", true); + TEST_PMU_MATCH("No suffix 2 hex_", "pmuname", false); + TEST_PMU_MATCH("Underscore 2 hex_", "pmuname_", false); + TEST_PMU_MATCH("Substring 2 hex_", "pmuna", false); =20 test_pmu.name =3D "pmuname_5"; - TEST_ASSERT_EQUAL("Glob 1", perf_pmu__match(&test_pmu, "pmu*"), true); - TEST_ASSERT_EQUAL("Glob 2", perf_pmu__match(&test_pmu, "nomatch*"), fa= lse); - TEST_ASSERT_EQUAL("Seq 1", perf_pmu__match(&test_pmu, "pmuname_[12345]")= , true); - TEST_ASSERT_EQUAL("Seq 2", perf_pmu__match(&test_pmu, "pmuname_[67890]")= , false); - TEST_ASSERT_EQUAL("? 1", perf_pmu__match(&test_pmu, "pmuname_?"), t= rue); - TEST_ASSERT_EQUAL("? 2", perf_pmu__match(&test_pmu, "pmuname_1?"), = false); + TEST_PMU_MATCH("Glob 1", "pmu*", true); + TEST_PMU_MATCH("Glob 2", "nomatch*", false); + TEST_PMU_MATCH("Seq 1", "pmuname_[12345]", true); + TEST_PMU_MATCH("Seq 2", "pmuname_[67890]", false); + TEST_PMU_MATCH("? 1", "pmuname_?", true); + TEST_PMU_MATCH("? 2", "pmuname_1?", false); =20 return TEST_OK; } diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 1e23faa364b1..6c36b98875bc 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1660,7 +1660,7 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pars= e_events_state *parse_state /* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */ while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { if (!parse_events__filter_pmu(parse_state, pmu) && - perf_pmu__match(pmu, event_or_pmu)) { + perf_pmu__wildcard_match(pmu, event_or_pmu)) { bool auto_merge_stats =3D perf_pmu__auto_merge_stats(pmu); =20 if (!parse_events_add_pmu(parse_state, *listp, pmu, diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 6206c8fe2bf9..4c6f9a1a9461 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -847,21 +847,23 @@ static size_t pmu_deduped_name_len(const struct perf_= pmu *pmu, const char *name, } =20 /** - * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring = any - * trailing suffix? The Suffix must be i= n form - * tok_{digits}, or tok{digits}. + * perf_pmu__match_wildcard - Does the pmu_name start with tok and is then= only + * followed by nothing or a suffix? tok may con= tain + * part of a suffix. * @pmu_name: The pmu_name with possible suffix. - * @tok: The possible match to pmu_name without suffix. + * @tok: The wildcard argument to match. */ -static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const ch= ar *tok) +static bool perf_pmu__match_wildcard(const char *pmu_name, const char *tok) { const char *p, *suffix; bool has_hex =3D false; + size_t tok_len =3D strlen(tok); =20 - if (strncmp(pmu_name, tok, strlen(tok))) + /* Check start of pmu_name for equality. */ + if (strncmp(pmu_name, tok, tok_len)) return false; =20 - suffix =3D p =3D pmu_name + strlen(tok); + suffix =3D p =3D pmu_name + tok_len; if (*p =3D=3D 0) return true; =20 @@ -887,60 +889,80 @@ static bool perf_pmu__match_ignoring_suffix(const cha= r *pmu_name, const char *to } =20 /** - * pmu_uncore_alias_match - does name match the PMU name? - * @pmu_name: the json struct pmu_event name. This may lack a suffix (which + * perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ig= noring + * any trailing suffix on pmu_nam= e and + * tok? The Suffix must be in fo= rm + * tok_{digits}, or tok{digits}. + * @pmu_name: The pmu_name with possible suffix. + * @tok: The possible match to pmu_name. + */ +static bool perf_pmu__match_ignoring_suffix_uncore(const char *pmu_name, c= onst char *tok) +{ + size_t pmu_name_len, tok_len; + + /* uncore_ prefixes are ignored. */ + if (!strncmp(pmu_name, "uncore_", 7)) + pmu_name +=3D 7; + if (!strncmp(tok, "uncore_", 7)) + tok +=3D 7; + + pmu_name_len =3D pmu_name_len_no_suffix(pmu_name); + tok_len =3D pmu_name_len_no_suffix(tok); + if (pmu_name_len !=3D tok_len) + return false; + + return strncmp(pmu_name, tok, pmu_name_len) =3D=3D 0; +} + + +/** + * perf_pmu__match_wildcard_uncore - does to_match match the PMU's name? + * @pmu_name: The pmu->name or pmu->alias to match against. + * @to_match: the json struct pmu_event name. This may lack a suffix (which * matches) or be of the form "socket,pmuname" which will match * "socketX_pmunameY". - * @name: a real full PMU name as from sysfs. */ -static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) +static bool perf_pmu__match_wildcard_uncore(const char *pmu_name, const ch= ar *to_match) { - char *tmp =3D NULL, *tok, *str; - bool res; + char *mutable_to_match, *tok, *tmp; =20 - if (strchr(pmu_name, ',') =3D=3D NULL) - return perf_pmu__match_ignoring_suffix(name, pmu_name); - - str =3D strdup(pmu_name); - if (!str) + if (!pmu_name) return false; =20 - /* - * uncore alias may be from different PMU with common prefix - */ - tok =3D strtok_r(str, ",", &tmp); - if (strncmp(pmu_name, tok, strlen(tok))) { - res =3D false; - goto out; - } + /* uncore_ prefixes are ignored. */ + if (!strncmp(pmu_name, "uncore_", 7)) + pmu_name +=3D 7; + if (!strncmp(to_match, "uncore_", 7)) + to_match +=3D 7; =20 - /* - * Match more complex aliases where the alias name is a comma-delimited - * list of tokens, orderly contained in the matching PMU name. - * - * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we - * match "socket" in "socketX_pmunameY" and then "pmuname" in - * "pmunameY". - */ - while (1) { - char *next_tok =3D strtok_r(NULL, ",", &tmp); + if (strchr(to_match, ',') =3D=3D NULL) + return perf_pmu__match_wildcard(pmu_name, to_match); =20 - name =3D strstr(name, tok); - if (!name || - (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) { - res =3D false; - goto out; + /* Process comma separated list of PMU name components. */ + mutable_to_match =3D strdup(to_match); + if (!mutable_to_match) + return false; + + tok =3D strtok_r(mutable_to_match, ",", &tmp); + while (tok) { + size_t tok_len =3D strlen(tok); + + if (strncmp(pmu_name, tok, tok_len)) { + /* Mismatch between part of pmu_name and tok. */ + free(mutable_to_match); + return false; } - if (!next_tok) - break; - tok =3D next_tok; - name +=3D strlen(tok); + /* Move pmu_name forward over tok and suffix. */ + pmu_name +=3D tok_len; + while (*pmu_name !=3D '\0' && isdigit(*pmu_name)) + pmu_name++; + if (*pmu_name =3D=3D '_') + pmu_name++; + + tok =3D strtok_r(NULL, ",", &tmp); } - - res =3D true; -out: - free(str); - return res; + free(mutable_to_match); + return *pmu_name =3D=3D '\0'; } =20 bool pmu_uncore_identifier_match(const char *compat, const char *id) @@ -1003,11 +1025,19 @@ static int pmu_add_sys_aliases_iter_fn(const struct= pmu_event *pe, { struct perf_pmu *pmu =3D vdata; =20 - if (!pe->compat || !pe->pmu) + if (!pe->compat || !pe->pmu) { + /* No data to match. */ return 0; + } + + if (!perf_pmu__match_wildcard_uncore(pmu->name, pe->pmu) && + !perf_pmu__match_wildcard_uncore(pmu->alias_name, pe->pmu)) { + /* PMU name/alias_name don't match. */ + return 0; + } =20 - if (pmu_uncore_alias_match(pe->pmu, pmu->name) && - pmu_uncore_identifier_match(pe->compat, pmu->id)) { + if (pmu_uncore_identifier_match(pe->compat, pmu->id)) { + /* Id matched. */ perf_pmu__new_alias(pmu, pe->name, pe->desc, @@ -1016,7 +1046,6 @@ static int pmu_add_sys_aliases_iter_fn(const struct p= mu_event *pe, pe, EVENT_SRC_SYS_JSON); } - return 0; } =20 @@ -1974,15 +2003,82 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, = bool skip_duplicate_pmus, return ret; } =20 -bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name) +static bool perf_pmu___name_match(const struct perf_pmu *pmu, const char *= to_match, bool wildcard) { - return !strcmp(pmu->name, pmu_name) || - (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) || + const char *names[2] =3D { + pmu->name, + pmu->alias_name, + }; + if (pmu->is_core) { + for (size_t i =3D 0; i < ARRAY_SIZE(names); i++) { + const char *name =3D names[i]; + + if (!name) + continue; + + if (!strcmp(name, to_match)) { + /* Exact name match. */ + return true; + } + } + if (!strcmp(to_match, "default_core")) { + /* + * jevents and tests use default_core as a marker for any core + * PMU as the PMU name varies across architectures. + */ + return true; + } + return false; + } + if (!pmu->is_uncore) { /* - * jevents and tests use default_core as a marker for any core - * PMU as the PMU name varies across architectures. + * PMU isn't core or uncore, some kind of broken CPU mask + * situation. Only match exact name. */ - (pmu->is_core && !strcmp(pmu_name, "default_core")); + for (size_t i =3D 0; i < ARRAY_SIZE(names); i++) { + const char *name =3D names[i]; + + if (!name) + continue; + + if (!strcmp(name, to_match)) { + /* Exact name match. */ + return true; + } + } + return false; + } + for (size_t i =3D 0; i < ARRAY_SIZE(names); i++) { + const char *name =3D names[i]; + + if (wildcard && perf_pmu__match_wildcard_uncore(name, to_match)) + return true; + if (!wildcard && perf_pmu__match_ignoring_suffix_uncore(name, to_match)) + return true; + } + return false; +} + +/** + * perf_pmu__name_wildcard_match - Called by the jevents generated code to= see + * if pmu matches the json to_match string. + * @pmu: The pmu whose name/alias to match. + * @to_match: The possible match to pmu_name. + */ +bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char = *to_match) +{ + return perf_pmu___name_match(pmu, to_match, /*wildcard=3D*/true); +} + +/** + * perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignorin= g any + * trailing suffix on the pmu_name and/or= tok? + * @pmu: The pmu whose name/alias to match. + * @to_match: The possible match to pmu_name. + */ +bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char= *to_match) +{ + return perf_pmu___name_match(pmu, to_match, /*wildcard=3D*/false); } =20 bool perf_pmu__is_software(const struct perf_pmu *pmu) @@ -2229,29 +2325,31 @@ void perf_pmu__warn_invalid_config(struct perf_pmu = *pmu, __u64 config, name ?: "N/A", buf, config_name, config); } =20 -bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok) +bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wild= card_to_match) { - const char *name =3D pmu->name; - bool need_fnmatch =3D strisglob(tok); + const char *names[2] =3D { + pmu->name, + pmu->alias_name, + }; + bool need_fnmatch =3D strisglob(wildcard_to_match); =20 - if (!strncmp(tok, "uncore_", 7)) - tok +=3D 7; - if (!strncmp(name, "uncore_", 7)) - name +=3D 7; + if (!strncmp(wildcard_to_match, "uncore_", 7)) + wildcard_to_match +=3D 7; =20 - if (perf_pmu__match_ignoring_suffix(name, tok) || - (need_fnmatch && !fnmatch(tok, name, 0))) - return true; + for (size_t i =3D 0; i < ARRAY_SIZE(names); i++) { + const char *pmu_name =3D names[i]; =20 - name =3D pmu->alias_name; - if (!name) - return false; + if (!pmu_name) + continue; =20 - if (!strncmp(name, "uncore_", 7)) - name +=3D 7; + if (!strncmp(pmu_name, "uncore_", 7)) + pmu_name +=3D 7; =20 - return perf_pmu__match_ignoring_suffix(name, tok) || - (need_fnmatch && !fnmatch(tok, name, 0)); + if (perf_pmu__match_wildcard(pmu_name, wildcard_to_match) || + (need_fnmatch && !fnmatch(wildcard_to_match, pmu_name, 0))) + return true; + } + return false; } =20 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index edd36c20aedc..f5306428c03f 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -240,7 +240,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const c= har *name); size_t perf_pmu__num_events(struct perf_pmu *pmu); int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmu= s, void *state, pmu_event_callback cb); -bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name); +bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char = *to_match); +bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char= *to_match); =20 /** * perf_pmu_is_software - is the PMU a software PMU as in it uses the @@ -275,7 +276,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu= , __u64 config, const char *config_name); void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu); =20 -bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok); +bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wild= card_to_match); =20 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size, --=20 2.48.1.262.g85cc9f2d1e-goog From nobody Sun Feb 8 09:59:08 2026 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 4A81D20C038 for ; Thu, 23 Jan 2025 07:47:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618433; cv=none; b=geFjQPMpwUHaURhiLuUWKVLif7TbL+4yrXQM7GvvbCULmEXlnYQzQq7x8l4RYvV0QOVkAnDIbmeimDYNcf1x1FVbrljGnADYY+E6y456JQzR+8nSmKufFID+5qVrulmogYvIpk6om4XKv6WX25u3YYWX3GOXsMK0AGgWf/v3lHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618433; c=relaxed/simple; bh=ev6gOwSw4jl6V/iZ6caKVvKq7JDregz/9FV23n2B/xQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=d1e05I8yuIYTI2i9ErbIjUTS9LbhPfsoWhP9Pu6I9ssrvGrZh2NmA3nSk4yDjRDK09HNbM3oWLCLZy5L+4CJYRMRc9Q+M0v8hpPLo3J+kCxT/MLYyyuffMfqMuTssCG94gCoCaBCMYiotpM6elEwyBUMYo2MVybrYA1OELAdio0= 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=Ibg7BCld; arc=none smtp.client-ip=209.85.219.202 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="Ibg7BCld" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e3c61a11a40so1758097276.2 for ; Wed, 22 Jan 2025 23:47:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737618431; x=1738223231; 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=DibDykcBruUh/0Lt2YF7KYtu0dPCuDqFOq2uHrXam2E=; b=Ibg7BCldAHS2tCAGfd57iKs+XqfgwLnhGPx27Xts+ZQiHgG5h+69/RCRf1yfnGX1zt gcTDrFZA313hjYiH/Q+OVegch8TygzzaIDyEN95pvZFs2LRYOgtEpD+R6fwxeAaGaINL SMsu8UB4QY/KYnptL0A1uUzKjfDO+sbFePI6/L9fgV6VNq4KOZqkZ2A78ndiRg8MRk8a GZSdDYV7vO4USaFD/Dfw5yuv+UKIxef6n8tJPH2Jyv0Y3gxicpVRIo0kT7N6OX+lnW+U 7WIfW48FlodbpI3f9ATDXQhEzRGgp/4MaJ4t93dWABamn5OTEx5egdOWu6A3saXYzA48 GLog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737618431; x=1738223231; 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=DibDykcBruUh/0Lt2YF7KYtu0dPCuDqFOq2uHrXam2E=; b=wNjuSumACwO5eXxQiSd/ZjfWvsADH01kbn2ZZbZ/SmIegxqOvXuO7OPiDUThcLEbb6 MU13IWTdvUF957klQeGCqB5ebakIpS7o5QZ93rwP3oXbEyHm+ICiqoz/m+Kh9dp2906B Kpsjn3YxmJ7POmONhjVfDRTYy2S5gpeG611ZqwMUbdo5GD23A/wJQNpv5dq4OdAxNIkQ BGV47a38S3Bs1kDcPulaiJSiSKANXQV7M/KrHW5owjrDLXmqzo0SvuSdZ8H4hOJpdP58 tebsaMllIGFDTv0UTm0jQMhj3ArjMvbY/A73mBSxQmxbxq6amWNMSRQ/Xpe3weY9GjQH bv7g== X-Forwarded-Encrypted: i=1; AJvYcCULcDCU+P6+jrXnaPa/beTf3WrHH9vCtckIljQAXsi0nSSRDS6yI0Q13CXG6HrZcLaR9vWSkAoJLRhgLGs=@vger.kernel.org X-Gm-Message-State: AOJu0YxPzl3IvRvI+ZhbWG57KQ6HJhDm0bo98LWQa1TPPwKlkJDtYfkD hukZdGRhpI6FXTe3FMV+Xzn4DSaHuuBnLhfnXWgKUrrSz4OWgUBsr1PAR724Mh9B7fw+Cynq/aW JvfgEcw== X-Google-Smtp-Source: AGHT+IE87jbT8I4D3MgQZvsZ2qLI+fhLyD63f8Gc0ijFyB8cgfhVoXEDc1maMbMo27Is6nL5mI8QFidgNe10 X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:2fda:d811:ae83:77e0]) (user=irogers job=sendgmr) by 2002:a05:690c:6307:b0:6ef:7c63:b104 with SMTP id 00721157ae682-6f6eb949f2emr577327b3.7.1737618431203; Wed, 22 Jan 2025 23:47:11 -0800 (PST) Date: Wed, 22 Jan 2025 23:46:58 -0800 In-Reply-To: <20250123074659.698123-1-irogers@google.com> Message-Id: <20250123074659.698123-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250123074659.698123-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v2 4/5] perf stat: Don't merge counters purely on name From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ze Gao , Weilin Wang , Jean-Philippe Romain , Junhao He , Yicong Yang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Counter merging was added in commit 942c5593393d ("perf stat: Add perf_stat_merge_counters()"), however, it merges events with the same name on different PMUs regardless of whether the different PMUs are actually of the same type (ie they differ only in the suffix on the PMU). For hwmon events there may be a temp1 event on every PMU, but the PMU names are all unique and don't differ just by a suffix. The merging is over eager and will merge all the hwmon counters together meaning an aggregated and very large temp1 value is shown. The same would be true for say cache events and memory controller events where the PMUs differ but the event names are the same. Fix the problem by correctly saying two PMUs alias when they differ only by suffix. Note, there is an overlap with evsel's merged_stat with aggregation and the evsel's metric_leader where aggregation happens for metrics. Fixes: 942c5593393d ("perf stat: Add perf_stat_merge_counters()") Signed-off-by: Ian Rogers --- tools/perf/util/stat.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 7c2ccdcc3fdb..1f7abd8754c7 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -535,7 +535,10 @@ static int evsel__merge_aggr_counters(struct evsel *ev= sel, struct evsel *alias) =20 return 0; } -/* events should have the same name, scale, unit, cgroup but on different = PMUs */ +/* + * Events should have the same name, scale, unit, cgroup but on different = core + * PMUs or on different but matching uncore PMUs. + */ static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b) { if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b))) @@ -553,7 +556,13 @@ static bool evsel__is_alias(struct evsel *evsel_a, str= uct evsel *evsel_b) if (evsel__is_clock(evsel_a) !=3D evsel__is_clock(evsel_b)) return false; =20 - return evsel_a->pmu !=3D evsel_b->pmu; + if (evsel_a->pmu =3D=3D evsel_b->pmu || evsel_a->pmu =3D=3D NULL || evsel= _b->pmu =3D=3D NULL) + return false; + + if (evsel_a->pmu->is_core) + return evsel_b->pmu->is_core; + + return perf_pmu__name_no_suffix_match(evsel_a->pmu, evsel_b->pmu->name); } =20 static void evsel__merge_aliases(struct evsel *evsel) --=20 2.48.1.262.g85cc9f2d1e-goog From nobody Sun Feb 8 09:59:08 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 961C620C47F for ; Thu, 23 Jan 2025 07:47:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618437; cv=none; b=TT8yn0tsIbcIOLR9WJ3CxNJfOGvz2zGJyVCtgoFgGHImoNNhIh+i5Yutr5K60UonNNCN3iYKbun8BtUv5MXuIHNdwgRTYW3WRjs0eqR5ZDcsI9EqHlBLBRbIET6XwJlq0hACO19PQncAcw3zzZJuI34UNF9KVtKxeBYNUrtBimg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737618437; c=relaxed/simple; bh=fLRFvGEGqKtFQGLXLVLSNIBik5FhXuDM0tMa3Nsu98Y=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=CsbmDN10MQXDug0ErqCODWybN2vpgmkb7g6BC4Jfy39LOzgnWTBPPFuqCyAnhzFBzR4hCNQEnt6uKeVcnfLbkgkN5MPGqaG5jyb763ilxUJ02oYxzgHOUEnJUR/WP1VAPmTWM8QMCavJ82mSOQduF2PdoRKJrVXZv2ZmNHmBkbk= 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=uYvn44cs; arc=none smtp.client-ip=209.85.219.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="uYvn44cs" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e54cb50c3baso1845576276.0 for ; Wed, 22 Jan 2025 23:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737618434; x=1738223234; 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=76ttAHRF3rIX3juKfMVrnox6BeyYpMdEkjNVR8LNQkM=; b=uYvn44csAmSxUA6CYG4GQzo4WpeWdP1vO/5EEjqmMm4DVTx9wI2PJj8jadLfT577OC 0ORhX3RBacKmWL0q8MN4C1B2R1dndlEl3JtwdhfAGzoqRZsfeIBHFHdu1vPv0+xFXeya q2z+F7MDtiWWWIB3ZSjJRoFKWYpe3URG6Vmx48POJtmelU38uOuX/XI59iHXrt6JW8QU Txc/vNybSAbnPOblAYAiY8vUjIKyNSGe3JDi2q75YBCKtX7xxaP2R+CAiYoXOzYKN1Nz fpFbY9MmZ7JvgfKkqy8De11t+FiaWqBB/F8jlLHcLdwu8x5bT2iToeLCWbGWirPeSV2J ZxfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737618434; x=1738223234; 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=76ttAHRF3rIX3juKfMVrnox6BeyYpMdEkjNVR8LNQkM=; b=XPlb9ZtglYa5MI5nWhs9B8Oa2Q/ZL4qdz6NZmsDrUN2LCNNv7s4k71pcJZNJHs1kz8 Jb22fITzZv+btgYckpO2xwU5d4pnp4ixCwnqsML4nQK2Pm6/TmFj2pDU1+QjX/EWHjvc k1QVT9Ktk1b7LrHhoIgOOPxVYuHn423L2aJ2NvlwoLx7HqsnfyBhSnPJvH+9Y9+k6DCk HzRCNwvDjj38LFWsoertRyVYVLg1t/+5uRiotGLfUpfz52hcGo43IM/tQR60Wkvo+5qc 66Ome4McAdcUfoja/jPDKis6U0AAtmdM0DmndLJ3ME3GwhZh4VeHWZYM+tc8aPOKBHHb zi0g== X-Forwarded-Encrypted: i=1; AJvYcCWT7pd6KknMlMbHxIZ/NnlYot2giPnVF1B5Wz/fXlTJzsk7VyiHNgm94UylyY8+FNJ27hFr1km/5XcEWD4=@vger.kernel.org X-Gm-Message-State: AOJu0YzJYgqswmKqCSLCg8E0KflU0FzdIyXVhHsFiDus+26DbhVR26RC W0b0gj/mVVJ8NXwmatK4FIcWyjCv+8W1rRimpnS0BFhlg0lL1FxsIgtkcxCg67oGAHEjbvrbnVh 4TPBtEA== X-Google-Smtp-Source: AGHT+IE9R9Fh4lUJwDFvHC3tyBYYt2+JIwpoi2K88c9BoJjp6yVFbFpdGz4GeMyh+mZoFlBYDHvT/MZkAxDd X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:2fda:d811:ae83:77e0]) (user=irogers job=sendgmr) by 2002:a25:d8d4:0:b0:e57:f46a:98ce with SMTP id 3f1490d57ef6-e58259f769dmr7324276.6.1737618433366; Wed, 22 Jan 2025 23:47:13 -0800 (PST) Date: Wed, 22 Jan 2025 23:46:59 -0800 In-Reply-To: <20250123074659.698123-1-irogers@google.com> Message-Id: <20250123074659.698123-6-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250123074659.698123-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v2 5/5] perf stat: Changes to event name uniquification From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ze Gao , Weilin Wang , Jean-Philippe Romain , Junhao He , Yicong Yang , 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 logic would disable uniquification on an evlist or enable it per evsel, this is unfortunate as uniquification is most needed when events have the same name and so the whole evlist must be considered. Change the initial disable uniquify on an evlist processing to also set a needs_uniquify flag, for cases like the matching event names. This must be done as an initial pass as uniquification of an event name will change the behavior of the check. Keep the per counter uniquification but now only uniquify event names when the needs_uniquify flag is set. Before this change a hwmon like temp1 wouldn't be uniquified and afterwards it will (ie the PMU is added to the temp1 event's name). Signed-off-by: Ian Rogers --- tools/perf/util/evsel.h | 1 + tools/perf/util/stat-display.c | 111 +++++++++++++++++++++++---------- 2 files changed, 79 insertions(+), 33 deletions(-) diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 5e789fa80590..d91621b6c8c2 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -119,6 +119,7 @@ struct evsel { bool errored; bool needs_auxtrace_mmap; bool default_metricgroup; /* A member of the Default metricgroup */ + bool needs_uniquify; struct hashmap *per_pkg_mask; int err; int script_output_type; diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index ba79f73e1cf5..e65c7e9f15d1 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -929,12 +929,16 @@ static void printout(struct perf_stat_config *config,= struct outstate *os, } } =20 -static void uniquify_event_name(struct evsel *counter) +static void evsel__uniquify_counter(struct evsel *counter) { const char *name, *pmu_name; char *new_name, *config; int ret; =20 + /* No uniquification necessary. */ + if (!counter->needs_uniquify) + return; + /* The evsel was already uniquified. */ if (counter->uniquified_name) return; @@ -942,19 +946,6 @@ static void uniquify_event_name(struct evsel *counter) /* Avoid checking to uniquify twice. */ counter->uniquified_name =3D true; =20 - /* The evsel has a "name=3D" config term or is from libpfm. */ - if (counter->use_config_name || counter->is_libpfm_event) - return; - - /* Legacy no PMU event, don't uniquify. */ - if (!counter->pmu || - (counter->pmu->type < PERF_TYPE_MAX && counter->pmu->type !=3D PERF_= TYPE_RAW)) - return; - - /* A sysfs or json event replacing a legacy event, don't uniquify. */ - if (counter->pmu->is_core && counter->alternate_hw_config !=3D PERF_COUNT= _HW_MAX) - return; - name =3D evsel__name(counter); pmu_name =3D counter->pmu->name; /* Already prefixed by the PMU name. */ @@ -993,17 +984,6 @@ static void uniquify_event_name(struct evsel *counter) } } =20 -static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *= config) -{ - return evsel__is_hybrid(evsel) && !config->hybrid_merge; -} - -static void uniquify_counter(struct perf_stat_config *config, struct evsel= *counter) -{ - if (config->aggr_mode =3D=3D AGGR_NONE || hybrid_uniquify(counter, config= )) - uniquify_event_name(counter); -} - /** * should_skip_zero_count() - Check if the event should print 0 values. * @config: The perf stat configuration (including aggregation mode). @@ -1089,7 +1069,7 @@ static void print_counter_aggrdata(struct perf_stat_c= onfig *config, if (counter->merged_stat) return; =20 - uniquify_counter(config, counter); + evsel__uniquify_counter(counter); =20 val =3D aggr->counts.val; ena =3D aggr->counts.ena; @@ -1670,7 +1650,8 @@ static void print_cgroup_counter(struct perf_stat_con= fig *config, struct evlist print_metric_end(config, os); } =20 -static void disable_uniquify(struct evlist *evlist) +/* Should uniquify be disabled for the evlist? */ +static bool evlist__disable_uniquify(const struct evlist *evlist) { struct evsel *counter; struct perf_pmu *last_pmu =3D NULL; @@ -1679,20 +1660,84 @@ static void disable_uniquify(struct evlist *evlist) evlist__for_each_entry(evlist, counter) { /* If PMUs vary then uniquify can be useful. */ if (!first && counter->pmu !=3D last_pmu) - return; + return false; first =3D false; if (counter->pmu) { /* Allow uniquify for uncore PMUs. */ if (!counter->pmu->is_core) - return; + return false; /* Keep hybrid event names uniquified for clarity. */ if (perf_pmus__num_core_pmus() > 1) - return; + return false; + } + } + return true; +} + +static void evsel__set_needs_uniquify(struct evsel *counter, const struct = perf_stat_config *config) +{ + struct evsel *evsel; + + if (counter->merged_stat) { + /* Counter won't be shown. */ + return; + } + + if (counter->use_config_name || counter->is_libpfm_event) { + /* Original name will be used. */ + return; + } + + if (counter->core.attr.type < PERF_TYPE_MAX && counter->core.attr.type != =3D PERF_TYPE_RAW) { + /* Legacy event, don't uniquify. */ + return; + } + + if (counter->pmu && counter->pmu->is_core && + counter->alternate_hw_config !=3D PERF_COUNT_HW_MAX) { + /* A sysfs or json event replacing a legacy event, don't uniquify. */ + return; + } + + if (config->aggr_mode =3D=3D AGGR_NONE) { + /* Always unique with no aggregation. */ + counter->needs_uniquify =3D true; + return; + } + + if (!config->hybrid_merge && evsel__is_hybrid(counter)) { + /* Unique hybrid counters necessary. */ + counter->needs_uniquify =3D true; + return; + } + + /* + * Do other non-merged events in the evlist have the same name? If so + * uniquify is necessary. + */ + evlist__for_each_entry(counter->evlist, evsel) { + if (evsel =3D=3D counter || evsel->merged_stat) + continue; + + if (evsel__name_is(counter, evsel__name(evsel))) { + counter->needs_uniquify =3D true; + return; } } - evlist__for_each_entry_continue(evlist, counter) { - counter->uniquified_name =3D true; +} + +static void evlist__set_needs_uniquify(struct evlist *evlist, const struct= perf_stat_config *config) +{ + struct evsel *counter; + + if (evlist__disable_uniquify(evlist)) { + evlist__for_each_entry(evlist, counter) + counter->uniquified_name =3D true; + return; } + + evlist__for_each_entry(evlist, counter) + evsel__set_needs_uniquify(counter, config); } =20 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config= *config, @@ -1706,7 +1751,7 @@ void evlist__print_counters(struct evlist *evlist, st= ruct perf_stat_config *conf .first =3D true, }; =20 - disable_uniquify(evlist); + evlist__set_needs_uniquify(evlist, config); =20 if (config->iostat_run) evlist->selected =3D evlist__first(evlist); --=20 2.48.1.262.g85cc9f2d1e-goog