From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5ECAFC54731 for ; Tue, 27 Aug 2024 16:48:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=aPD7TpdyXbi0JlZVmP89N+miXKTP0ssLSvSWyFRW84c=; b=RO4DMrs9RUlcNxDhoNXODEnDLG RZay1flecmYjDgmO8W54geqQCv1BoutxqBj2zOi0L60pyIMqNwr8kkCqSSAX6MsrAJp902uFQPedU 7sVLQDaXbumb1ZIzzHkbGzp7YZaRZNm3ZPIY6X8LACpwTKk0Bu0aqNHcgzY5vilGFRXMxvfndKWez uT9EN/SinWEAvBXeMuYNc1doYQ6/Zzfl37CO0V53beXu7KzHorqd7Pdqp+UyBjDtQ1TtdoT4hzJbk qDvigsZAwQNiK3qCDN3Zl4cuXbgrgc3rdbpZrSu1uvatTYHNZmTKH8Iu8BvJOo4VByz4QH/nOML9c ZZlEnWyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sizMO-0000000C8WR-08Aj; Tue, 27 Aug 2024 16:48:00 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sizJA-0000000C7X1-3Zn4 for linux-arm-kernel@lists.infradead.org; Tue, 27 Aug 2024 16:44:42 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8FC1F11FB; Tue, 27 Aug 2024 09:45:06 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 422B63F762; Tue, 27 Aug 2024 09:44:38 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Will Deacon , Mark Rutland , Suzuki K Poulose , Mike Leach , James Clark , John Garry , Namhyung Kim , Ian Rogers , Adrian Hunter , "Liang, Kan" , Jonathan Cameron , Yicong Yang , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-perf-users@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 3/9] perf auxtrace arm: Introduce find_auxtrace_pmus_by_name() Date: Tue, 27 Aug 2024 17:44:11 +0100 Message-Id: <20240827164417.3309560-4-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240827164417.3309560-1-leo.yan@arm.com> References: <20240827164417.3309560-1-leo.yan@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240827_094441_017568_C548E853 X-CRM114-Status: GOOD ( 20.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- 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 #include #include +#include #include #include @@ -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