Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@arm.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Mike Leach <mike.leach@linaro.org>,
	James Clark <james.clark@linaro.org>,
	John Garry <john.g.garry@oracle.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	"Liang, Kan" <kan.liang@linux.intel.com>,
	Jonathan Cameron <jonathan.cameron@huawei.com>,
	Yicong Yang <yangyicong@hisilicon.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, coresight@lists.linaro.org,
	linux-perf-users@vger.kernel.org
Cc: Leo Yan <leo.yan@arm.com>
Subject: [PATCH v1 8/9] perf arm-spe: Support metadata version 2
Date: Tue, 27 Aug 2024 17:44:16 +0100	[thread overview]
Message-ID: <20240827164417.3309560-9-leo.yan@arm.com> (raw)
In-Reply-To: <20240827164417.3309560-1-leo.yan@arm.com>

This commit is to support metadata version 2 and at the meantime it is
backward compatible for version 1's format.

The metadata version 1 doesn't include the ARM_SPE_HEADER_VERSION field.
By checking the header size, it distinguishes the metadata is version 1
or version 2 (and any new versions if later will have). For version 2,
it reads out CPU number and retrieves the metadata info for every CPU.

Signed-off-by: Leo Yan <leo.yan@arm.com>
---
 tools/perf/util/arm-spe.c | 76 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 74 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 1e87342f4bd8..87cf06db765b 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -78,6 +78,10 @@ struct arm_spe {
 
 	unsigned long			num_events;
 	u8				use_ctx_pkt_for_pid;
+
+	u64				**metadata;
+	u64				metadata_ver;
+	u64				metadata_num_cpu;
 };
 
 struct arm_spe_queue {
@@ -1044,12 +1048,16 @@ static void arm_spe_free_events(struct perf_session *session)
 
 static void arm_spe_free(struct perf_session *session)
 {
+	unsigned int i;
 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
 					     auxtrace);
 
 	auxtrace_heap__free(&spe->heap);
 	arm_spe_free_events(session);
 	session->auxtrace = NULL;
+	for (i = 0; i < spe->metadata_num_cpu; i++)
+		zfree(&spe->metadata[i]);
+	zfree(&spe->metadata);
 	free(spe);
 }
 
@@ -1256,24 +1264,81 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
 	return 0;
 }
 
+static bool
+arm_spe_has_metadata_field(struct perf_record_auxtrace_info *auxtrace_info, int pos)
+{
+	return auxtrace_info->header.size >=
+		sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
+}
+
+static u64 *arm_spe__create_meta_blk(u64 *buf, int per_cpu_size)
+{
+	u64 *metadata = NULL;
+
+	metadata = zalloc(sizeof(*metadata) * per_cpu_size);
+	if (!metadata)
+		return NULL;
+
+	memcpy(metadata, buf, per_cpu_size);
+	return metadata;
+}
+
 int arm_spe_process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {
 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
-	size_t min_sz = ARM_SPE_AUXTRACE_V1_PRIV_SIZE;
+	size_t min_sz;
 	struct perf_record_time_conv *tc = &session->time_conv;
 	const char *cpuid = perf_env__cpuid(session->evlist->env);
 	u64 midr = strtol(cpuid, NULL, 16);
 	struct arm_spe *spe;
+	u64 *ptr = NULL;
+	u64 **metadata = NULL;
+	u64 metadata_ver;
+	int num_cpu = 0, i, per_cpu_sz;
 	int err;
 
+	/* First the global part */
+	ptr = (u64 *)auxtrace_info->priv;
+
+	/* Metadata v1 has no the ARM_SPE_HEADER_VERSION field */
+	if (!arm_spe_has_metadata_field(auxtrace_info, ARM_SPE_HEADER_VERSION)) {
+		metadata_ver = 1;
+		num_cpu = 0;
+		min_sz = ARM_SPE_AUXTRACE_V1_PRIV_SIZE;
+		per_cpu_sz = 0;
+		ptr += ARM_SPE_AUXTRACE_V1_PRIV_MAX;
+	} else {
+		metadata_ver = ptr[ARM_SPE_HEADER_VERSION];
+		num_cpu = ptr[ARM_SPE_CPU_NUM];
+		min_sz = ARM_SPE_AUXTRACE_V2_PRIV_SIZE +
+			 ARM_SPE_AUXTRACE_V2_PER_CPU_SIZE * num_cpu;
+		per_cpu_sz = ARM_SPE_AUXTRACE_V2_PER_CPU_SIZE;
+		ptr += ARM_SPE_AUXTRACE_V2_PRIV_MAX;
+	}
+
 	if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
 					min_sz)
 		return -EINVAL;
 
+	if (num_cpu) {
+		metadata = zalloc(sizeof(*metadata) * num_cpu);
+		if (!metadata)
+			return -ENOMEM;
+
+		for (i = 0; i < num_cpu; i++) {
+			metadata[i] = arm_spe__create_meta_blk(ptr, per_cpu_sz);
+			if (!metadata[i]) {
+				err = -ENOMEM;
+				goto err_free_metadata;
+			}
+			ptr += per_cpu_sz / sizeof(u64);
+		}
+	}
+
 	spe = zalloc(sizeof(struct arm_spe));
 	if (!spe)
-		return -ENOMEM;
+		goto err_free_metadata;
 
 	err = auxtrace_queues__init(&spe->queues);
 	if (err)
@@ -1283,6 +1348,9 @@ int arm_spe_process_auxtrace_info(union perf_event *event,
 	spe->machine = &session->machines.host; /* No kvm support */
 	spe->auxtrace_type = auxtrace_info->type;
 	spe->midr = midr;
+	spe->metadata = metadata;
+	spe->metadata_ver = metadata_ver;
+	spe->metadata_num_cpu = num_cpu;
 
 	spe->timeless_decoding = arm_spe__is_timeless_decoding(spe);
 
@@ -1343,5 +1411,9 @@ int arm_spe_process_auxtrace_info(union perf_event *event,
 	session->auxtrace = NULL;
 err_free:
 	free(spe);
+err_free_metadata:
+	for (i = 0; i < num_cpu; i++)
+		zfree(&metadata[i]);
+	zfree(&metadata);
 	return err;
 }
-- 
2.34.1



  parent reply	other threads:[~2024-08-27 16:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-27 16:44 [PATCH v1 0/9] perf arm-spe: Introduce metadata version 2 Leo Yan
2024-08-27 16:44 ` [PATCH v1 1/9] perf: arm_spe: Introduce 'lds' capacity Leo Yan
2024-08-30 10:38   ` Will Deacon
2024-08-30 12:12     ` Leo Yan
2024-08-30 13:09       ` Will Deacon
2024-08-31 11:37         ` Leo Yan
2024-09-04 12:35           ` Will Deacon
2024-09-04 14:02             ` Leo Yan
2024-08-27 16:44 ` [PATCH v1 2/9] perf auxtrace arm: Refactor error handling Leo Yan
2024-08-27 16:44 ` [PATCH v1 3/9] perf auxtrace arm: Introduce find_auxtrace_pmus_by_name() Leo Yan
2024-08-27 16:44 ` [PATCH v1 4/9] perf: arm-spe: Record multiple PMUs Leo Yan
2024-08-27 16:44 ` [PATCH v1 5/9] perf arm-spe: Extend meta data header for version 2 Leo Yan
2024-08-27 16:44 ` [PATCH v1 6/9] perf arm-spe: Calculate meta data size Leo Yan
2024-08-27 16:44 ` [PATCH v1 7/9] perf arm-spe: Save per CPU information in metadata Leo Yan
2024-08-27 16:44 ` Leo Yan [this message]
2024-08-27 16:44 ` [PATCH v1 9/9] perf arm-spe: Dump metadata with version 2 Leo Yan
2024-08-28 16:20   ` James Clark
2024-08-30  7:54     ` Leo Yan

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=20240827164417.3309560-9-leo.yan@arm.com \
    --to=leo.yan@arm.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=coresight@lists.linaro.org \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=john.g.garry@oracle.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mike.leach@linaro.org \
    --cc=namhyung@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=yangyicong@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