From nobody Sun Dec 14 23:11:07 2025 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 9A92C215798 for ; Tue, 14 Jan 2025 23:13:38 +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=1736896420; cv=none; b=ryxv4dQJkUsqPfrOsG3nSWOV51gmgkr52rJxAq+Mg29qwx8urHBijnSSq9gFRe0wR9o739B75qsx7NtiBPsWAmQI2YIdTaezp3ugSo5cWG61l0r1pb1xFNVRa6tkCoh4yj3DSfAbMFrWX29lx4HQssETxf5TIp2nl7zMLXpWHp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736896420; c=relaxed/simple; bh=A/eR8uR1yvM5eqZV/QlIrCo0vWCFnc2aKBHW8D8fiZw=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=PGs8HKvNK+3YJtDrZ/HlBX53kUsVoIUzgB/BVH4yn/LcfK7TvYVUyXRSAwrXioEVs5VV94quHSAvHWMEdt+BoOaUf68gkpoOYU7hQo6o/1fVCbQV0sRXPwK1NWnlYUyhwVkQIWmPq0WGL4tfnI2XWlXUUt/DcgQUjnKm/yEQ39g= 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=vJII4XFE; 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="vJII4XFE" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e549e7072fbso11553096276.2 for ; Tue, 14 Jan 2025 15:13:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736896417; x=1737501217; 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=LDCtpsQOxkSPlmEY/69U0PEtnOYFr9Y2An71RRy2l+s=; b=vJII4XFEosutUic40r1P1ee5Xpoiq3H+NgXbxDHq2i6sv0OJesTLW/wPVEaygjbUWN eJpKUY+TQ6y+1MEUCwYqDL2oMTtOzlwi9mT5t6cQSwXWk0NIvj/lLQ5Ky9cSjKui28t/ 4BWaAwJPAHyLKePW941RqFYqqO0CrO2IoRrM2y2NJiK/pbRdYHsmFSyCdc1KP/dPZFxk lewKwHWqKUqlUI8hmefANKJoQ7f21vZ+WJcYjSJOF9f+iUNCGySZ4tnDbb3SpHKxlhmK WeoYLjrtoRLP4G40nZc0m714DxqLOCvTWj1F226QQbSwwSDRsU8/w4sCoVfV2ZxbpopK VenQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736896417; x=1737501217; 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=LDCtpsQOxkSPlmEY/69U0PEtnOYFr9Y2An71RRy2l+s=; b=StXFnKJX4ih5Z2EM7Ac3yCFedxaiUXmSnUL3jHFxDTbKgYQaC+sQqU6HKAeTpDTZNQ 0xft927qREcoS0G052KSvAvbATmVjNJh4no5Wzj6RtN9Svpq47IltntM6BJn11NRS4t6 QB2OTUQnqW1D8aMUgPlLstUCMph5rzYkZiwBDkDCky6EqqcTD5CIib2sY2jO9D0qptoI utXclcv04BDm2IZumgxR1xk6MIxN4/Ev2GryVFQ0tNo/Ado9KuuQMfPK8ueay1Jl69KO he6oNgauGfMKTPWuxzr9hFrDm/2o5NystLtqt/TY5gB9WlCDdv0809JIIH4ujSKiOC4K 1pCA== X-Forwarded-Encrypted: i=1; AJvYcCWZ6z62U5LTpTNYDOKWRBhP+e2QZTsG1WRwdrHoU1xG4NRCGabOsYBL8MA/58DqEnJF8J0IKU91HnZzglY=@vger.kernel.org X-Gm-Message-State: AOJu0YyMo/LM//SUuzFftdKIG63h+ogPiJM8+8UrW7mrvC/nTmSsudYb MLw1Lv8oMGF2qlYkhbGRx7rd2FS4Jhox87/d+Wjpcqq4QSiHul+iNvqt5hrQ99/x6ON3Nf3izVp BW+kiBQ== X-Google-Smtp-Source: AGHT+IHXFfkYqmyJHnA919HVCDNWdFFmo0150gT0EPvhDiX6howpfgKkpYwXQYkO3qFXWT31gATzIw/H9nFh X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:b3e8:96da:3fa1:5494]) (user=irogers job=sendgmr) by 2002:a05:690c:4b85:b0:6f2:8819:2318 with SMTP id 00721157ae682-6f53130b447mr711217b3.5.1736896417686; Tue, 14 Jan 2025 15:13:37 -0800 (PST) Date: Tue, 14 Jan 2025 15:13:29 -0800 In-Reply-To: <20250114231332.13419-1-irogers@google.com> Message-Id: <20250114231332.13419-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: <20250114231332.13419-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v1 1/4] 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.0.rc2.279.g1de40edade-goog From nobody Sun Dec 14 23:11:07 2025 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 B9B0121D594 for ; Tue, 14 Jan 2025 23:13:40 +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=1736896422; cv=none; b=pFUYc7rNiDmCfavQriSwmwVO9UYCRb4IxVh4268H45jJ4LTQFxvHm0cuEWw2vVP5R0K2EpowhkiYqGs/TZpxpdXN9y/JGmz5rwiVLzvfn9qUrxNFlEV9E2R+L0UAsKc/3KymeERdZmCTKpkBIqDgsEONUZk4LNLeMUvQ0qnkymU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736896422; c=relaxed/simple; bh=btl4qXL5evLxqcHl1FvdrUG8RJ5rjIlNvcEpLlfshFg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=i5OLrdTKk3aLu4TzDBKe4FEIvD7TD6p8RZZ2VsLnEk6ERnA+XxcxD9UadfySigV8/sTd66Vo6GQZG7XGX1TyUeud6N2hPkP8pOWDDaFiHhu9oLE4gn9j0OmCwJdxNyI4BQx5iKE4kCpn89cFdEVoo4rzOZM3kPxBtDi3IVzKah0= 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=1cHF8amj; 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="1cHF8amj" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e575f472438so6365756276.1 for ; Tue, 14 Jan 2025 15:13:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736896420; x=1737501220; 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=Ua/j8ZQhkKWj7ygdra9SGaNhS8kdmsUQSxwWnqi04Hs=; b=1cHF8amjmE1GxbH0Yu03jTHlhs1n8Yx79kNjQ6wbS9a9pv1FVDIROweeW71P3Ekvyt XEzE0cLO1TBzGf5BPcfmyMjNkX8LfSk43ndV2g6GVjk96Gxv1ju/0XCazUvKl6NOcrk0 u35OhrDySrMhmNOMxwpJ5H5agen8qfIQ0PhFLriYe3Rh8XwjcPzlUe4e9kRRnoYVMKPc Rq5BvD1PViyWcINcSoAponIhGKK/Y/8Bk4ByKlpoa7JEf5bW4PxuNeft3GejPYtZHGt1 Pr6e2e5IpAiVXGJFBhX+b7ugiUhjEBSnknG2m3+a5mcNSZIYR3iyfeizL+VPISlrf82D LlMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736896420; x=1737501220; 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=Ua/j8ZQhkKWj7ygdra9SGaNhS8kdmsUQSxwWnqi04Hs=; b=QxCH6K2AKwBMO8edQXbthCufwqsEWLUcbroz1lPonC6FbdlKWQ2y56k79SfTu1Lex+ 9BmQsXvqZySSvtjJrKWTkEqEe2BWq/Mr8AapH9J0v6KQPV8ENZStA1JIKoKqoq4IskVj 71sTX77QSx2NV+hGSvricdJs3C26/PcEcx70FUp5+j3RmszZ/LBpx1tU8bZbY6Yw9zwf UBoLIpoiVUh0meniKko/YQn3sEGzOjNPcLlyNaIChiKKnZY8J0BsiwLa5IqBtTcojWIS QM48NPskNcCcY/kFK3Plf421ZxTah6JX+H+1oQ8vUezzWkb5zIoTy4hf7fXxzD5PtKV3 pmmw== X-Forwarded-Encrypted: i=1; AJvYcCVqL6BCPhVQVSVAhhmb016Zjvpu7tsnpvYfzMD1TzPY6rBh51+dbt3mrv5w12GzvdTIRS5lCQA4+C0AW4c=@vger.kernel.org X-Gm-Message-State: AOJu0YyaN9cvEmtnRoMVAaSDqnmEP5xu7o6lPwzM02SxEBxPYqlCReTL 97KCg0zD1DPKaJHKuqBMlV373WIORmVSrFAPLZbJUV77J5+nww9kU/LmNFILNMhpp01D6Ly9emw q5dDduA== X-Google-Smtp-Source: AGHT+IGmnTme9h/rpITxWUBz+vW/Nmh8/Ox6gBif+8JaTIBSnE+1wJDVCGBgW9tRmSZ2O4/OTmEwKTVVeGk3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:b3e8:96da:3fa1:5494]) (user=irogers job=sendgmr) by 2002:a5b:642:0:b0:e39:6fdc:5567 with SMTP id 3f1490d57ef6-e54eddd285bmr71645276.0.1736896419977; Tue, 14 Jan 2025 15:13:39 -0800 (PST) Date: Tue, 14 Jan 2025 15:13:30 -0800 In-Reply-To: <20250114231332.13419-1-irogers@google.com> Message-Id: <20250114231332.13419-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: <20250114231332.13419-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v1 2/4] 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.0.rc2.279.g1de40edade-goog From nobody Sun Dec 14 23:11:07 2025 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 16BFC222587 for ; Tue, 14 Jan 2025 23:13:42 +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=1736896425; cv=none; b=mQRHpwAGdyDxbA7ejTK06tZYOvW4FfTOeTlauompXxyFDT2FGZefG0FjoRr9XcrwCtO6PbmfREi0XySGhZNTg8f7veVK5x0718ywXFDLAu2X79bRmB6uhoiOZtW68DND8jVW1+qUA4GogCDqHSLJYkI1+vwOuInirFVXPAYCqX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736896425; c=relaxed/simple; bh=TK5jsPUddoVqcWXgIyzdTH/6UP97uh46LEr347Po1cA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=AlLz1H4btz7yyH1YIZUsdHihJG67toh3TZjHnOE8tbkEDSA9eCPGXmvPiPwFFXCO4+vWk5BP5FzNgyqErHZvjYDuwCW7BoY0H6Qt95G+w1yfTJeUgmMrO1poQdh1dNhHpR2mZhbbl6LaW2HH1pqSSTtsiNavu5jxsAPvKGTrFjc= 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=1ER2/FXL; 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="1ER2/FXL" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e54d9b54500so14025743276.3 for ; Tue, 14 Jan 2025 15:13:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736896422; x=1737501222; 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=N1xjM3wphMNYu1Ks6TVH7fA4hJ036I4dNmv6CTOW45Q=; b=1ER2/FXL/7UCxzaN40Ds9MA0VVhLmxOF9U3yhHXxo9UVSLwzb9CGOvoTMJXeqT0/69 XE8bIYFcAkVewOnOn1U8QV0k3l9lttW4VeN6SkULz854PJ0GDJWN9jT4tE2g49W5/ryx 84JRrNtX6ozGWiF6uW1pINvxN/YQVU1gt+4+FZFb9I3iTreq4NKzycEy25d4NbhvBgR5 GJJKyJq3jIqhBn9mHcPfBfARQVIApC2Y/RI+QJgwucbHYze/aha84EjIz1wpxGpGiyYC 9X/RDG3wXN/RUWXuUjFiQzgQW4f6sAysfMIQZvJhL6XZeItw/JyQYNii65ZhabO/YLFr QAIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736896422; x=1737501222; 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=N1xjM3wphMNYu1Ks6TVH7fA4hJ036I4dNmv6CTOW45Q=; b=bZz5FN2Psf4HH5PTHUMcmehEUG4GejWVA+7cYW4G85Ffk4O5NF5GqJ5v9is7eIDJq6 n4GuuoETOx3mvpdo8BnEjROwcPJew/s9WXsVkHiNrBJMBxGT/pexZtr4g4CNXJEqRS/8 XmVRMRorfjoHVvrPj4SSag/d/LAoGKS7jb13nI9mbaspvz1oEj+tSakGMRJJd4eeUruO O0bc4KvEpTyIPUddOgR5coQYP4neCuGPbHJSXq9CRRSShZbyi1XQcoApMbYIBoLTB/fQ dP1Ibo58sO13r8yWsIJNnA50yxrBzcdFsOOgKV+jsfbp35sZtvzZoHG5xSu9Fp2D1JYs 8lEA== X-Forwarded-Encrypted: i=1; AJvYcCUj/2IpGUMAzJMDkdqGjMDTe9ddO6IicT0rd5+hU8rCrJWnYC5Ch5/pZD7GBEGRyubvhm1JHEdOMpGA5cI=@vger.kernel.org X-Gm-Message-State: AOJu0YzSj/sYrVhatSC+z/S9LqMzWxTi/23/C0hO/cnRW8Hm17v5h/eH pZA0uePZCbzfnpzec8uN7d3wRWpsgsYb/g+cLpDfdlkrrjiHGm2O2TWP9MLd1f1kv3sbTqV6QdD 1OiXSKA== X-Google-Smtp-Source: AGHT+IE6vfJevlH8r7yu7Dpu1i9qXKvuoPLqRHx5pq+3+JCv3a3eukT2BmKSixvpexe5/MBkmW1GzX281wQl X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:b3e8:96da:3fa1:5494]) (user=irogers job=sendgmr) by 2002:a25:c7d4:0:b0:e57:3e87:8c17 with SMTP id 3f1490d57ef6-e573e878d01mr27597276.5.1736896422209; Tue, 14 Jan 2025 15:13:42 -0800 (PST) Date: Tue, 14 Jan 2025 15:13:31 -0800 In-Reply-To: <20250114231332.13419-1-irogers@google.com> Message-Id: <20250114231332.13419-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: <20250114231332.13419-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v1 3/4] 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..cfdb6d55df9b 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 pmu__name_match(evsel_a->pmu, evsel_b->pmu->name); } =20 static void evsel__merge_aliases(struct evsel *evsel) --=20 2.48.0.rc2.279.g1de40edade-goog From nobody Sun Dec 14 23:11:07 2025 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 4C2B9223321 for ; Tue, 14 Jan 2025 23:13:45 +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=1736896427; cv=none; b=QpmI2dK9e7x6WJPMsjE3myc7SzI+yUvVjgx2gUUTsTDMG4b6DtrHsXytIvmbYdAX2wRkKULTffzKOH9/aqZykw0RmniIdPo7LstCI3enuckusaVz3CSygmTfu/7cMhCDH4vh/pWXMuicAmmofLrtWO5aZ2QYto2P+PdfQTNZSD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736896427; c=relaxed/simple; bh=0Hx4NSZJe5ECE65y1lry5FP/Xw26WaUY/hr9480+KXQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=hsBUIurpLQyAk2dqg4CduUIPoIS0UOumOpaEdL6OBCCem6/e+jgVMQpD76pwDb3lGVTjbyjFsg42Joe2DZ7EOLliF31r3cOW+jQSD6HtaPXmq+GKgWpvFC1uvw0AtOe52g+O9XcToLKhkQH36g1KruAytZY2Zo83bMaEEODjUPg= 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=MpoDm+OJ; 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="MpoDm+OJ" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e549de22484so14779686276.2 for ; Tue, 14 Jan 2025 15:13:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736896424; x=1737501224; 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=ePVlEVv5m1tigC/CdRn/0kZjaG1+iIHc/7tw87zaT8Q=; b=MpoDm+OJNDhJdjYCWgVUIvvSWjsmfW9j3qZIaYsKsLYLfmQc21ghxvoJrPybLnOoot WZamlWvuoantqWeaOYocgOgcXbsZqJi72EM0wTMVlTuh/EUbz7jGjHKYPFcCY3l14UsO WMPhu4r7HnVrlpwKcO9o6sPttk9On62a0hStFaGjAv20s171lMc31mfSepQ/TTwcDpL5 BJeafykm4ck6aaFgydnFrne64ulETkCIXDCpFfTVY2t++OE6qO/d5qhqlX456ylYlULM dW7aI33TpAeY5IJtGNpLocOO2DD8WxEMCkLXgYv8XHGvSTfCKx61c9kr24NA3apc9+er DaYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736896424; x=1737501224; 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=ePVlEVv5m1tigC/CdRn/0kZjaG1+iIHc/7tw87zaT8Q=; b=j0v0YhRMaSmoCeea8L3MtkBhSANaBlKGX7O/wm87M/VBDMeFtuzspO7O1lxO9hmb/p cj+KxrpfDxejWBzxh8isU/4QiCSnSyBjQEw5URGBumOrgY+JLYrKejTVJoNLDjRkZmIP bZQMU7Iht74sfyaAO78gkTa4dOvR9If9dVNhHkViF658Ctq/TEqR7pwH/60tcluHfA73 rb1EWEwuDsfW/6RoiORsvulR6qDO7REZ3GhXkY4KvG5C6f76qouBiVZHxJBud9XiVs0G spMiHR4VwLlQEfKibrDfcEFQYliBQzQf2qyhiVWSpxwBVzJ7+mGLgsr9LuGrHDykj/z4 Z3oA== X-Forwarded-Encrypted: i=1; AJvYcCU5FP8X2c2yEdn6wukv4aonBpaSga+K2PrDrJ5kMi/t6IXurkvUmQRkurSkMRbV9jX3HHr46/mLMJ+jxUs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6+lC6u45jkKfrd2qEhNr0OUZNaPG8/6WNHVo9Y1abdCDeVVFv Hc7aHQG3kGIXL1KneqHPY5J9iCZNvcS7H7xle34SluN7624oPirxbP5KMhECkT8Nv4OT6tlJONy CKTB7RQ== X-Google-Smtp-Source: AGHT+IHLbP7GZ3eL9yLxHK1VGBTGA/27h/UjLaxoc/tls292SQSUjIcvbNoP47GkJvG5cCmpTxrAfRk7NQmn X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:b3e8:96da:3fa1:5494]) (user=irogers job=sendgmr) by 2002:a05:690c:2099:b0:6ee:4b9d:df44 with SMTP id 00721157ae682-6f531323dc9mr524617b3.8.1736896424377; Tue, 14 Jan 2025 15:13:44 -0800 (PST) Date: Tue, 14 Jan 2025 15:13:32 -0800 In-Reply-To: <20250114231332.13419-1-irogers@google.com> Message-Id: <20250114231332.13419-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: <20250114231332.13419-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v1 4/4] 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.0.rc2.279.g1de40edade-goog