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 3/9] perf auxtrace arm: Introduce find_auxtrace_pmus_by_name()
Date: Tue, 27 Aug 2024 17:44:11 +0100 [thread overview]
Message-ID: <20240827164417.3309560-4-leo.yan@arm.com> (raw)
In-Reply-To: <20240827164417.3309560-1-leo.yan@arm.com>
Arm auxtrace searches the opened PMU events for Arm SPE and Hisilicon
HTT. The current approach is to first iterate all PMU devices under the
sysfs folder and then match the PMU event on the evlist.
Since the evlist has sufficient info for the PMU name and corresponding
PMU pointer, it is no need to browse the PMU devices from sysfs nodes.
Alternatively, by traversing the evlist and comparing prefixes for PMU
names, we can directly gather the PMU pointers and save them into an
array. Following the idea, this patch introduces a new function
find_auxtrace_pmus_by_name().
find_auxtrace_pmus_by_name() returns a PMU pointer array or NULL if no
any PMU is found. This simplifies the auxtrace_record__init() function,
as the PMU array pointers are for found PMU events. The local variables
'found_{etm|spe|ptt}' and relevant code are redundant, so remove them.
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/arch/arm/util/auxtrace.c | 158 ++++++++--------------------
1 file changed, 41 insertions(+), 117 deletions(-)
diff --git a/tools/perf/arch/arm/util/auxtrace.c b/tools/perf/arch/arm/util/auxtrace.c
index 74630d2d81dc..2fca16659858 100644
--- a/tools/perf/arch/arm/util/auxtrace.c
+++ b/tools/perf/arch/arm/util/auxtrace.c
@@ -7,6 +7,7 @@
#include <dirent.h>
#include <stdbool.h>
#include <linux/coresight-pmu.h>
+#include <linux/string.h>
#include <linux/zalloc.h>
#include <api/fs/fs.h>
@@ -19,144 +20,66 @@
#include "arm-spe.h"
#include "hisi-ptt.h"
-static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)
+static struct perf_pmu **
+find_auxtrace_pmus_by_name(struct evlist *evlist, const char *name, int *nr_pmu)
{
- struct perf_pmu **arm_spe_pmus = NULL;
- int ret, i, nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
- /* arm_spe_xxxxxxxxx\0 */
- char arm_spe_pmu_name[sizeof(ARM_SPE_PMU_NAME) + 10];
-
- arm_spe_pmus = zalloc(sizeof(struct perf_pmu *) * nr_cpus);
- if (!arm_spe_pmus) {
- pr_err("spes alloc failed\n");
- *err = -ENOMEM;
- return NULL;
- }
-
- for (i = 0; i < nr_cpus; i++) {
- ret = sprintf(arm_spe_pmu_name, "%s%d", ARM_SPE_PMU_NAME, i);
- if (ret < 0) {
- pr_err("sprintf failed\n");
- *err = -ENOMEM;
- return NULL;
- }
+ struct perf_pmu **pmus = NULL;
+ struct evsel *evsel;
+ int i = 0, nr = 0;
- arm_spe_pmus[*nr_spes] = perf_pmus__find(arm_spe_pmu_name);
- if (arm_spe_pmus[*nr_spes]) {
- pr_debug2("%s %d: arm_spe_pmu %d type %d name %s\n",
- __func__, __LINE__, *nr_spes,
- arm_spe_pmus[*nr_spes]->type,
- arm_spe_pmus[*nr_spes]->name);
- (*nr_spes)++;
- }
- }
+ assert(name);
+ assert(nr_pmu);
- return arm_spe_pmus;
-}
+ *nr_pmu = 0;
-static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)
-{
- struct perf_pmu **hisi_ptt_pmus = NULL;
- struct dirent *dent;
- char path[PATH_MAX];
- DIR *dir = NULL;
- int idx = 0;
-
- perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
- dir = opendir(path);
- if (!dir) {
- pr_err("can't read directory '%s'\n", path);
- *err = -EINVAL;
- return NULL;
- }
-
- while ((dent = readdir(dir))) {
- if (strstr(dent->d_name, HISI_PTT_PMU_NAME))
- (*nr_ptts)++;
+ evlist__for_each_entry(evlist, evsel) {
+ if (strstarts(evsel->pmu_name, name))
+ nr++;
}
- if (!(*nr_ptts))
- goto out;
+ if (!nr)
+ return NULL;
- hisi_ptt_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_ptts));
- if (!hisi_ptt_pmus) {
- pr_err("hisi_ptt alloc failed\n");
- *err = -ENOMEM;
- goto out;
+ pmus = zalloc(sizeof(struct perf_pmu *) * nr);
+ if (!pmus) {
+ pr_err("Failed to allocate PMU pointer arrary.\n");
+ return NULL;
}
- rewinddir(dir);
- while ((dent = readdir(dir))) {
- if (strstr(dent->d_name, HISI_PTT_PMU_NAME) && idx < *nr_ptts) {
- hisi_ptt_pmus[idx] = perf_pmus__find(dent->d_name);
- if (hisi_ptt_pmus[idx])
- idx++;
+ evlist__for_each_entry(evlist, evsel) {
+ if (strstarts(evsel->pmu_name, name)) {
+ pmus[i] = evsel->pmu;
+ i++;
}
}
-out:
- closedir(dir);
- return hisi_ptt_pmus;
-}
-
-static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus,
- int pmu_nr, struct evsel *evsel)
-{
- int i;
-
- if (!pmus)
- return NULL;
-
- for (i = 0; i < pmu_nr; i++) {
- if (evsel->core.attr.type == pmus[i]->type)
- return pmus[i];
- }
-
- return NULL;
+ *nr_pmu = nr;
+ return pmus;
}
struct auxtrace_record
*auxtrace_record__init(struct evlist *evlist, int *err)
{
- struct perf_pmu *cs_etm_pmu = NULL;
+ struct perf_pmu **cs_etm_pmu = NULL;
struct perf_pmu **arm_spe_pmus = NULL;
struct perf_pmu **hisi_ptt_pmus = NULL;
- struct evsel *evsel;
- struct perf_pmu *found_etm = NULL;
- struct perf_pmu *found_spe = NULL;
- struct perf_pmu *found_ptt = NULL;
struct auxtrace_record *itr = NULL;
int auxtrace_event_cnt = 0;
- int nr_spes = 0;
- int nr_ptts = 0;
+ int nr_etm = 0;
+ int nr_spe = 0;
+ int nr_ptt = 0;
if (!evlist)
return NULL;
- cs_etm_pmu = perf_pmus__find(CORESIGHT_ETM_PMU_NAME);
- arm_spe_pmus = find_all_arm_spe_pmus(&nr_spes, err);
- hisi_ptt_pmus = find_all_hisi_ptt_pmus(&nr_ptts, err);
-
- evlist__for_each_entry(evlist, evsel) {
- if (cs_etm_pmu && !found_etm)
- found_etm = find_pmu_for_event(&cs_etm_pmu, 1, evsel);
-
- if (arm_spe_pmus && !found_spe)
- found_spe = find_pmu_for_event(arm_spe_pmus, nr_spes, evsel);
-
- if (hisi_ptt_pmus && !found_ptt)
- found_ptt = find_pmu_for_event(hisi_ptt_pmus, nr_ptts, evsel);
- }
-
- if (found_etm)
- auxtrace_event_cnt++;
-
- if (found_spe)
- auxtrace_event_cnt++;
-
- if (found_ptt)
- auxtrace_event_cnt++;
+ cs_etm_pmu =
+ find_auxtrace_pmus_by_name(evlist, CORESIGHT_ETM_PMU_NAME, &nr_etm);
+ arm_spe_pmus =
+ find_auxtrace_pmus_by_name(evlist, ARM_SPE_PMU_NAME, &nr_spe);
+ hisi_ptt_pmus =
+ find_auxtrace_pmus_by_name(evlist, HISI_PTT_PMU_NAME, &nr_ptt);
+ auxtrace_event_cnt = !!nr_etm + !!nr_spe + !!nr_ptt;
if (!auxtrace_event_cnt) {
/*
* Clear 'err' even if we haven't found an event - that way perf
@@ -172,18 +95,19 @@ struct auxtrace_record
goto out;
}
- if (found_etm)
+ if (cs_etm_pmu)
itr = cs_etm_record_init(err);
#if defined(__aarch64__)
- if (found_spe)
- itr = arm_spe_recording_init(err, found_spe);
+ if (arm_spe_pmus)
+ itr = arm_spe_recording_init(err, arm_spe_pmus[0]);
- if (found_ptt)
- itr = hisi_ptt_recording_init(err, found_ptt);
+ if (hisi_ptt_pmus)
+ itr = hisi_ptt_recording_init(err, hisi_ptt_pmus[0]);
#endif
out:
+ free(cs_etm_pmu);
free(arm_spe_pmus);
free(hisi_ptt_pmus);
return itr;
--
2.34.1
next prev parent reply other threads:[~2024-08-27 16:48 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 ` Leo Yan [this message]
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 ` [PATCH v1 8/9] perf arm-spe: Support metadata version 2 Leo Yan
2024-08-27 16:44 ` [PATCH v1 9/9] perf arm-spe: Dump metadata with " 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-4-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