mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: John Garry <john.garry@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
	<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
	<jolsa@redhat.com>, <namhyung@kernel.org>, <will@kernel.org>,
	<ak@linux.intel.com>
Cc: <linuxarm@huawei.com>, <linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>, <suzuki.poulose@arm.com>,
	<james.clark@arm.com>, <zhangshaokun@hisilicon.com>,
	<robin.murphy@arm.com>, John Garry <john.garry@huawei.com>
Subject: [PATCH RFC 5/7] perf pmu: Support matching by sysid
Date: Fri, 24 Jan 2020 22:35:03 +0800	[thread overview]
Message-ID: <1579876505-113251-6-git-send-email-john.garry@huawei.com> (raw)
In-Reply-To: <1579876505-113251-1-git-send-email-john.garry@huawei.com>

Match system or uncore PMU aliases by system id, SYSID.

We use a SYSID read from sysfs or from an env variable to match against
uncore or system PMU events.

For x86, they want to match uncore events with cpuid - this still works
fine for x86 as it would not have system event tables for uncore PMUs.

Signed-off-by: John Garry <john.garry@huawei.com>
---
 tools/perf/util/pmu.c | 105 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 94 insertions(+), 11 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 569aba4cec89..4d4fe0c1ae22 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -672,11 +672,78 @@ static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
 	return cpuid;
 }
 
+static char *get_sysid_str(void)
+{
+	char *buf = NULL;
+	char path[PATH_MAX];
+	const char *sysfs = sysfs__mountpoint();
+	FILE *file;
+	int s, i;
+
+	if (!sysfs)
+		return NULL;
+
+	buf = malloc(PATH_MAX);
+	if (!buf) {
+		pr_err("%s alloc failed\n", __func__);
+		return NULL;
+	}
+
+	scnprintf(path, PATH_MAX, "%s/devices/soc0/machine", sysfs);
+
+	file = fopen(path, "r");
+	if (!file) {
+		pr_debug("fopen failed for file %s\n", path);
+		free(buf);
+		return NULL;
+	}
+
+	if (!fgets(buf, PATH_MAX, file)) {
+		fclose(file);
+		pr_debug("gets failed for file %s\n", path);
+		free(buf);
+		return NULL;
+	}
+	fclose(file);
+
+	/* Remove any whitespace, this could be from ACPI HID */
+	s = strlen(buf);
+	for (i = 0; i < s; i++) {
+		if (buf[i] == ' ') {
+			buf[i] = 0;
+			break;
+		};
+	}
+
+	return buf;
+}
+
+static char *perf_pmu__getsysid(void)
+{
+	char *sysid;
+	static bool printed;
+
+	sysid = getenv("PERF_SYSID");
+	if (sysid)
+		sysid = strdup(sysid);
+
+	if (!sysid)
+		sysid = get_sysid_str();
+	if (!sysid)
+		return NULL;
+
+	if (!printed) {
+		pr_debug("Using SYSID %s\n", sysid);
+		printed = true;
+	}
+	return sysid;
+}
+
 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
 {
-	struct pmu_events_map *map;
+	struct pmu_events_map *map, *found_map = NULL;
 	char *cpuid = perf_pmu__getcpuid(pmu);
-	int i;
+	char *sysid;
 
 	/* on some platforms which uses cpus map, cpuid can be NULL for
 	 * PMUs other than CORE PMUs.
@@ -684,19 +751,35 @@ struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
 	if (!cpuid)
 		return NULL;
 
-	i = 0;
-	for (;;) {
-		map = &pmu_events_map[i++];
-		if (!map->table) {
-			map = NULL;
-			break;
+	sysid = perf_pmu__getsysid();
+
+	/*
+	 * Match sysid as first perference for uncore/sys PMUs.
+	 *
+	 * x86 uncore events match by cpuid, but we would not have map->socid
+	 * set for that arch (so any matching here would fail for that).
+	 */
+	if (pmu && pmu_is_uncore_or_sys(pmu->name) &&
+	    !is_arm_pmu_core(pmu->name) && sysid) {
+		for (map = &pmu_events_map[0]; map->table; map++) {
+			if (map->sysid && !strcmp(map->sysid, sysid)) {
+				found_map = map;
+				goto out;
+			}
 		}
+	}
 
-		if (!strcmp_cpuid_str(map->cpuid, cpuid))
-			break;
+	for (map = &pmu_events_map[0]; map->table; map++) {
+		if (map->cpuid && cpuid &&
+		    !strcmp_cpuid_str(map->cpuid, cpuid)) {
+			found_map = map;
+			goto out;
+		}
 	}
+out:
 	free(cpuid);
-	return map;
+	free(sysid); /* Can safely handle is sysid is NULL */
+	return found_map;
 }
 
 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
-- 
2.17.1


  parent reply	other threads:[~2020-01-24 14:39 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-24 14:34 [PATCH RFC 0/7] perf pmu-events: Support event aliasing for system PMUs John Garry
2020-01-24 14:34 ` [PATCH RFC 1/7] perf jevents: Add support for an extra directory level John Garry
2020-02-10 12:07   ` Jiri Olsa
2020-02-10 15:47     ` John Garry
2020-01-24 14:35 ` [PATCH RFC 2/7] perf vendor events arm64: Relocate hip08 core events John Garry
2020-01-24 14:35 ` [PATCH RFC 3/7] perf jevents: Add support for a system events PMU John Garry
2020-02-10 12:07   ` Jiri Olsa
2020-02-10 12:07   ` Jiri Olsa
2020-02-10 15:55     ` John Garry
2020-02-11 14:46       ` Jiri Olsa
2020-01-24 14:35 ` [PATCH RFC 4/7] perf pmu: Rename uncore symbols to include system PMUs John Garry
2020-02-10 12:07   ` Jiri Olsa
2020-02-10 15:44     ` John Garry
2020-02-11 14:43       ` Jiri Olsa
2020-02-11 15:36         ` John Garry
2020-02-12 12:08           ` Jiri Olsa
2020-01-24 14:35 ` John Garry [this message]
2020-02-10 12:07   ` [PATCH RFC 5/7] perf pmu: Support matching by sysid Jiri Olsa
2020-02-10 16:22     ` John Garry
2020-02-11 13:47       ` Jiri Olsa
2020-02-11 15:07         ` John Garry
2020-02-12 10:08           ` John Garry
2020-02-12 12:16             ` Jiri Olsa
2020-02-12 12:24               ` John Garry
2020-01-24 14:35 ` [PATCH RFC 6/7] perf vendor events arm64: Relocate uncore events for hip08 John Garry
2020-01-24 14:35 ` [PATCH RFC 7/7] perf vendor events arm64: Add hip08 SMMUv3 PMCG IMP DEF events John Garry
2020-02-11 15:24 ` [PATCH RFC 0/7] perf pmu-events: Support event aliasing for system PMUs James Clark
2020-02-11 15:41   ` John Garry
2020-02-18 12:57 ` Will Deacon
2020-02-18 13:24   ` John Garry
2020-02-18 13:39     ` Will Deacon
2020-02-18 16:19       ` John Garry
2020-02-18 17:08         ` Mark Rutland
2020-02-18 17:58           ` John Garry
2020-02-18 18:13             ` Mark Rutland
2020-02-19  1:55               ` Joakim Zhang
2020-02-19  8:44                 ` John Garry
2020-02-19 12:40                   ` Joakim Zhang
2020-02-19 14:28                     ` John Garry
2020-02-19  8:50               ` John Garry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1579876505-113251-6-git-send-email-john.garry@huawei.com \
    --to=john.garry@huawei.com \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=james.clark@arm.com \
    --cc=jolsa@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxarm@huawei.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=robin.murphy@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=zhangshaokun@hisilicon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®