Skip to contentSkip to conversation

perf tools: Ensure event leader stays at head of evlist after sorting

PATCH Superseded Chun-Tse Shao 3 participants 4 messages May 1 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf tools: Ensure event leader stays at head of evlist after sorting

From: Ian Rogers <[email protected]>

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.

---
 tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/parse-events.c +27 -0
index 1497e1f2a08c..be825617321d 100644
@@ -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);
 
Signature · 1 line
2.54.0.545.g6539524ca2-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Chun-Tse Shao

From: Ian Rogers <[email protected]>

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.

Quoted 8 lines

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.

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

Thanks!
Ian

Quoted 45 lines
---
 tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/parse-events.c +27 -1
index 1497e1f2a08c..be825617321d 100644
@@ -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

maintainer
#2 lore raw
In reply to Chun-Tse Shao
Quoted 11 lines · Chun-Tse Shao

From: Ian Rogers <[email protected]>

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.

Quoted 6 lines

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?

Quoted 2 lines

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

Quoted 12 lines
---
 tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/parse-events.c +0 -0
index 1497e1f2a08c..be825617321d 100644
@@ -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);
 	}

tools/perf/util/parse-events.c static int parse_events__sort_events_and_fix_groups(struct list_head *list) · line 2263

Quoted 10 lines
tools/perf/util/parse-events.c +10 -0
+
+	/*
+	 * 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

Quoted 23 lines
tools/perf/util/parse-events.c +17 -0
+
+		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);
 
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#3 lore raw

Hi Namhyung,

Quoted 13 lines · Namhyung Kim
Quoted 11 lines · Chun-Tse Shao

From: Ian Rogers <[email protected]>

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.

Quoted 8 lines
Quoted 6 lines

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

Quoted 5 lines
Quoted 3 lines

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?

Quoted 18 lines
Quoted 18 lines
---
 tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/parse-events.c +5 -0
index 1497e1f2a08c..be825617321d 100644
@@ -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
Quoted 8 lines

tools/perf/util/parse-events.c static int parse_events__sort_events_and_fix_groups(struct list_head *list) · line 2263

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+      * 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.

Quoted 24 lines

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/parse-events.c +17 -0
+
+             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);
 
Quoted 3 lines
Quoted 3 lines

--
2.54.0.545.g6539524ca2-goog

Thanks,
CT