From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Kan Liang <kan.liang@linux.intel.com>,
Ravi Bangoria <ravi.bangoria@amd.com>,
Kajol Jain <kjain@linux.ibm.com>,
John Garry <john.g.garry@oracle.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/3] perf pmus: Sort pmus by name then suffix
Date: Fri, 11 Aug 2023 10:56:20 -0300 [thread overview]
Message-ID: <ZNY+BHUFETc2eNib@kernel.org> (raw)
In-Reply-To: <20230810214952.2934029-2-irogers@google.com>
Em Thu, Aug 10, 2023 at 02:49:50PM -0700, Ian Rogers escreveu:
> Sort PMUs by name. If two PMUs have the same name but differ by
> suffix, sort the suffixes numerically. For example, "breakpoint" comes
> before "cpu", "uncore_imc_free_running_0" comes before
> "uncore_imc_free_running_1".
Why is this needed?
- Arnaldo
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/util/pmus.c | 48 ++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 48 insertions(+)
>
> diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
> index c58ba9fb6a36..3581710667b0 100644
> --- a/tools/perf/util/pmus.c
> +++ b/tools/perf/util/pmus.c
> @@ -1,8 +1,10 @@
> // SPDX-License-Identifier: GPL-2.0
> #include <linux/list.h>
> +#include <linux/list_sort.h>
> #include <linux/zalloc.h>
> #include <subcmd/pager.h>
> #include <sys/types.h>
> +#include <ctype.h>
> #include <dirent.h>
> #include <pthread.h>
> #include <string.h>
> @@ -33,6 +35,31 @@ static LIST_HEAD(other_pmus);
> static bool read_sysfs_core_pmus;
> static bool read_sysfs_all_pmus;
>
> +static int pmu_name_len_no_suffix(const char *str, unsigned long *num)
> +{
> + int orig_len, len;
> +
> + orig_len = len = strlen(str);
> +
> + /* Non-uncore PMUs have their full length, for example, i915. */
> + if (strncmp(str, "uncore_", 7))
> + return len;
> +
> + /*
> + * Count trailing digits and '_', if '_{num}' suffix isn't present use
> + * the full length.
> + */
> + while (len > 0 && isdigit(str[len - 1]))
> + len--;
> +
> + if (len > 0 && len != orig_len && str[len - 1] == '_') {
> + if (num)
> + *num = strtoul(&str[len], NULL, 10);
> + return len - 1;
> + }
> + return orig_len;
> +}
> +
> void perf_pmus__destroy(void)
> {
> struct perf_pmu *pmu, *tmp;
> @@ -122,6 +149,25 @@ static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
> return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
> }
>
> +static int pmus_cmp(void *priv __maybe_unused,
> + const struct list_head *lhs, const struct list_head *rhs)
> +{
> + unsigned long lhs_num, rhs_num;
> + struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
> + struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
> + const char *lhs_pmu_name = lhs_pmu->name ?: "";
> + const char *rhs_pmu_name = rhs_pmu->name ?: "";
> + int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num);
> + int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num);
> + int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
> + lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
> +
> + if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
> + return ret;
> +
> + return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
> +}
> +
> /* Add all pmus in sysfs to pmu list: */
> static void pmu_read_sysfs(bool core_only)
> {
> @@ -156,6 +202,8 @@ static void pmu_read_sysfs(bool core_only)
> 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);
> + list_sort(NULL, &other_pmus, pmus_cmp);
> if (!list_empty(&core_pmus)) {
> read_sysfs_core_pmus = true;
> if (!core_only)
> --
> 2.41.0.640.ga95def55d0-goog
>
--
- Arnaldo
next prev parent reply other threads:[~2023-08-11 13:56 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-10 21:49 [PATCH v2 0/3] perf list: Remove duplicate PMUs Ian Rogers
2023-08-10 21:49 ` [PATCH v2 1/3] perf pmus: Sort pmus by name then suffix Ian Rogers
2023-08-11 13:56 ` Arnaldo Carvalho de Melo [this message]
2023-08-11 15:19 ` Ian Rogers
2023-08-11 15:46 ` Arnaldo Carvalho de Melo
2023-08-11 13:59 ` John Garry
2023-08-11 15:15 ` Ian Rogers
2023-08-10 21:49 ` [PATCH v2 2/3] perf pmus: Add scan that ignores duplicates, use for perf list Ian Rogers
2023-08-11 15:51 ` John Garry
2023-08-14 15:57 ` Ian Rogers
2023-08-14 16:09 ` Ian Rogers
2023-08-15 8:56 ` John Garry
2023-08-10 21:49 ` [PATCH v2 3/3] perf pmus: Don't print duplicate PMU suffix in list by default Ian Rogers
2023-08-11 15:53 ` 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=ZNY+BHUFETc2eNib@kernel.org \
--to=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=kjain@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.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