From: Chun-Tse Shao <ctshao@google.com>
To: linux-kernel@vger.kernel.org
Cc: Chun-Tse Shao <ctshao@google.com>,
peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
namhyung@kernel.org, mark.rutland@arm.com,
alexander.shishkin@linux.intel.com, jolsa@kernel.org,
irogers@google.com, adrian.hunter@intel.com,
james.clark@linaro.org, thomas.falcon@intel.com,
tmricht@linux.ibm.com, linux-perf-users@vger.kernel.org
Subject: [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting
Date: Thu, 30 Apr 2026 21:35:10 -0700 [thread overview]
Message-ID: <20260501043532.790711-1-ctshao@google.com> (raw)
From: Ian Rogers <irogers@google.com>
For evlist of a certain event/metric, the HEAD should be the event
leader. In some scenarios where uncore_xxx_0 does not exist, the event
leader is not the first element after sorting. For example, on my test
machine uncore_iio_0 does not exist, the event leader is uncore_iio_2.
However, in `evlist__cmp`, it was reordered based on the PMU name, which
makes uncore_iio_1 the HEAD of evlist, breaking the following merge
logic in `evsel__merge_aliases`.
The patch adds a loop at the end of
`parse_events__sort_events_and_fix_groups` to make sure the first
wildcard match is the earliest entry in the list, updating pointers
accordingly without breaking reordering detection.
Tested on DUT lacks uncore_iio_0, and `perf test` looks good.
Signed-off-by: Chun-Tse Shao <ctshao@google.com>
---
tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 1497e1f2a08c..be825617321d 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2251,6 +2251,33 @@ static int parse_events__sort_events_and_fix_groups(struct list_head *list)
}
last_event_was_forced_leader = (force_grouped_leader == pos);
}
+
+ /*
+ * Make sure the first wildcard match is the earliest entry in the list.
+ * Since list_sort might have reordered the aliases, the original leader
+ * might not be at the head of the list anymore. We find the first
+ * alias in the sorted list and make it the new leader, and redirect
+ * all other aliases to it.
+ */
+ list_for_each_entry(pos, list, core.node) {
+ struct evsel *l = pos->first_wildcard_match;
+
+ if (!l)
+ continue;
+
+ if (l->first_wildcard_match) {
+ /* Original leader was redirected to a new leader */
+ pos->first_wildcard_match = l->first_wildcard_match;
+ } else if (pos->core.idx < l->core.idx) {
+ /*
+ * We are earlier than the original leader in sorted order,
+ * and no earlier alias has claimed leadership yet.
+ */
+ l->first_wildcard_match = pos;
+ pos->first_wildcard_match = NULL;
+ }
+ }
+
list_for_each_entry(pos, list, core.node) {
struct evsel *pos_leader = evsel__leader(pos);
--
2.54.0.545.g6539524ca2-goog
next reply other threads:[~2026-05-01 4:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 4:35 Chun-Tse Shao [this message]
2026-05-01 13:44 ` [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting Ian Rogers
2026-05-01 21:23 ` Namhyung Kim
2026-05-01 22:17 ` Chun-Tse Shao
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=20260501043532.790711-1-ctshao@google.com \
--to=ctshao@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--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=thomas.falcon@intel.com \
--cc=tmricht@linux.ibm.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