public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting
@ 2026-05-01  4:35 Chun-Tse Shao
  2026-05-01 13:44 ` Ian Rogers
  2026-05-01 21:23 ` Namhyung Kim
  0 siblings, 2 replies; 4+ messages in thread
From: Chun-Tse Shao @ 2026-05-01  4:35 UTC (permalink / raw)
  To: linux-kernel
  Cc: Chun-Tse Shao, peterz, mingo, acme, namhyung, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	thomas.falcon, tmricht, linux-perf-users

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting
  2026-05-01  4:35 [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting Chun-Tse Shao
@ 2026-05-01 13:44 ` Ian Rogers
  2026-05-01 21:23 ` Namhyung Kim
  1 sibling, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2026-05-01 13:44 UTC (permalink / raw)
  To: Chun-Tse Shao
  Cc: linux-kernel, peterz, mingo, acme, namhyung, mark.rutland,
	alexander.shishkin, jolsa, adrian.hunter, james.clark,
	thomas.falcon, tmricht, linux-perf-users

On Thu, Apr 30, 2026 at 9:35 PM Chun-Tse Shao <ctshao@google.com> wrote:
>
> 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>

Fixes: 347c2f0a0988 ("perf parse-events: Sort and group parsed events")

Reviewed-by: Ian Rogers <irogers@google.com>

Thanks!
Ian

> ---
>  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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting
  2026-05-01  4:35 [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting Chun-Tse Shao
  2026-05-01 13:44 ` Ian Rogers
@ 2026-05-01 21:23 ` Namhyung Kim
  2026-05-01 22:17   ` Chun-Tse Shao
  1 sibling, 1 reply; 4+ messages in thread
From: Namhyung Kim @ 2026-05-01 21:23 UTC (permalink / raw)
  To: Chun-Tse Shao
  Cc: linux-kernel, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	thomas.falcon, tmricht, linux-perf-users

On Thu, Apr 30, 2026 at 09:35:10PM -0700, Chun-Tse Shao wrote:
> 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.

DUT?

> 
> Signed-off-by: Chun-Tse Shao <ctshao@google.com>

If this commit is from Ian, there should be Ian's sign-off before yours.

> ---
>  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;

Why 'l'?  It's not conventional and can be confusing with capital 'I'.
Can it be just 'evsel', 'first' or 'leader'?

Thanks,
Namhyung

> +
> +		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
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting
  2026-05-01 21:23 ` Namhyung Kim
@ 2026-05-01 22:17   ` Chun-Tse Shao
  0 siblings, 0 replies; 4+ messages in thread
From: Chun-Tse Shao @ 2026-05-01 22:17 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: linux-kernel, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	thomas.falcon, tmricht, linux-perf-users

Hi Namhyung,

On Fri, May 1, 2026 at 2:23 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Thu, Apr 30, 2026 at 09:35:10PM -0700, Chun-Tse Shao wrote:
> > 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.
>
> DUT?

Device under test, it seems like not a good fit in that sentence..
>
> >
> > Signed-off-by: Chun-Tse Shao <ctshao@google.com>
>
> If this commit is from Ian, there should be Ian's sign-off before yours.

Can maintainer help me to do this while merge in or I should submit a new patch?
>
> > ---
> >  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;
>
> Why 'l'?  It's not conventional and can be confusing with capital 'I'.
> Can it be just 'evsel', 'first' or 'leader'?

That makes sense, I will submit a new patch.
>
> Thanks,
> Namhyung
>
> > +
> > +             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
> >

Thanks,
CT

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-05-01 22:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-01  4:35 [PATCH] perf tools: Ensure event leader stays at head of evlist after sorting Chun-Tse Shao
2026-05-01 13:44 ` Ian Rogers
2026-05-01 21:23 ` Namhyung Kim
2026-05-01 22:17   ` Chun-Tse Shao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox