Skip to contentSkip to conversation

perf stat: Remove dead code

PATCH In review Ian Rogers 3 participants 8 messages Sep 10, 2020 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v2 1/3 perf stat: Remove dead code

No need to set os.evsel twice.

---
 tools/perf/util/stat-display.c | 1 -
 1 file changed, 1 deletion(-)
tools/perf/util/stat-display.c +0 -1
index 493ec372fdec..4b57c0c07632 100644
@@ -946,7 +946,6 @@ static void print_metric_headers(struct perf_stat_config *config,
 		out.print_metric = print_metric_header;
 		out.new_line = new_line_metric;
 		out.force_header = true;
-		os.evsel = counter;
 		perf_stat__print_shadow_stats(config, counter, 0,
 					      0,
 					      &out,
Signature · 1 line
2.28.0.526.ge36021eeef-goog
author reviewer
#1 lore raw

PATCH v2 2/3 perf metricgroup: Fix typo in comment.

Add missing character.

---
 tools/perf/util/metricgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/metricgroup.c +1 -1
index 8831b964288f..662f4e8777d5 100644
@@ -150,7 +150,7 @@ static void expr_ids__exit(struct expr_ids *ids)
 }
 
 /**
- * Find a group of events in perf_evlist that correpond to those from a parsed
+ * Find a group of events in perf_evlist that correspond to those from a parsed
  * metric expression. Note, as find_evsel_group is called in the same order as
  * perf_evlist was constructed, metric_no_merge doesn't need to test for
  * underfilling a group.
Signature · 1 line
2.28.0.526.ge36021eeef-goog
maintainer Acked-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] perf metricgroup: Fix typo in comment.

Quoted 4 lines · Ian Rogers

Add missing character.

Thanks
Namhyung

Quoted 20 lines
---
 tools/perf/util/metricgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/metricgroup.c +1 -2
index 8831b964288f..662f4e8777d5 100644
@@ -150,7 +150,7 @@ static void expr_ids__exit(struct expr_ids *ids)
 }
 
 /**
- * Find a group of events in perf_evlist that correpond to those from a parsed
+ * Find a group of events in perf_evlist that correspond to those from a parsed
  * metric expression. Note, as find_evsel_group is called in the same order as
  * perf_evlist was constructed, metric_no_merge doesn't need to test for
  * underfilling a group.
--

2.28.0.526.ge36021eeef-goog

#3 lore raw

Re: [PATCH v2 2/3] perf metricgroup: Fix typo in comment.

Em Thu, Sep 10, 2020 at 02:38:58PM +0900, Namhyung Kim escreveu:

Quoted 7 lines · Ian Rogers
Quoted 4 lines

Add missing character.

Applied this and the first one, continuing...

- Arnaldo

Quoted 23 lines

Thanks
Namhyung

Quoted 20 lines
---
 tools/perf/util/metricgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/metricgroup.c +1 -2
index 8831b964288f..662f4e8777d5 100644
@@ -150,7 +150,7 @@ static void expr_ids__exit(struct expr_ids *ids)
 }
 
 /**
- * Find a group of events in perf_evlist that correpond to those from a parsed
+ * Find a group of events in perf_evlist that correspond to those from a parsed
  * metric expression. Note, as find_evsel_group is called in the same order as
  * perf_evlist was constructed, metric_no_merge doesn't need to test for
  * underfilling a group.
--

2.28.0.526.ge36021eeef-goog

Signature · 1 line
- Arnaldo
author reviewer
#4 lore raw

PATCH v2 3/3 perf metricgroup: Fix uncore metric expressions

In reply to Ian Rogers

A metric like DRAM_BW_Use has on SkylakeX events uncore_imc/cas_count_read/ and uncore_imc/case_count_write/. These events open 6 events per socket with pmu names of uncore_imc_[0-5]. The current metric setup code in find_evsel_group assumes one ID will map to 1 event to be recorded in metric_events. For events with multiple matches, the first event is recorded in metric_events (avoiding matching >1 event with the same name) and the evlist_used updated so that duplicate events aren't removed when the evlist has unused events removed.

Before this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

             41.14 MiB  uncore_imc/cas_count_read/
     1,002,614,251 ns   duration_time

       1.002614251 seconds time elapsed

After this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

            157.47 MiB  uncore_imc/cas_count_read/ #     0.00 DRAM_BW_Use
            126.97 MiB  uncore_imc/cas_count_write/
     1,003,019,728 ns   duration_time

v2. avoids iterating over the whole evlist as suggested by
[email protected]. It also fixes the metric_leader computation that was broken in the same commits.

Erroneous duplication introduced in:
commit 2440689d62e9 ("perf metricgroup: Remove duped metric group events").

Fixes ded80bda8bc9 ("perf expr: Migrate expr ids table to a hashmap").
---
 tools/perf/util/metricgroup.c | 45 ++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)
tools/perf/util/metricgroup.c +42 -3
index 662f4e8777d5..79080de9217d 100644
@@ -206,6 +206,18 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
 				sizeof(struct evsel *) * idnum);
 			current_leader = ev->leader;
 		}
+		/*
+		 * Check for duplicate events with the same name. For example,
+		 * uncore_imc/cas_count_read/ will turn into 6 events per socket
+		 * on skylakex. Only the first such event is placed in
+		 * metric_events.
+		 */
+		for (i = 0; i < matched_events; i++) {
+			if (!strcmp(metric_events[i]->name, ev->name))
+				break;
+		}
+		if (i != matched_events)
+			continue;
 		if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
 			if (has_constraint) {
 				/*
@@ -245,9 +257,36 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
 	metric_events[idnum] = NULL;
 
 	for (i = 0; i < idnum; i++) {
-		ev = metric_events[i];
-		ev->metric_leader = ev;
-		set_bit(ev->idx, evlist_used);
+		/* Don't free used events. */
+		set_bit(metric_events[i]->idx, evlist_used);
+		/*
+		 * The metric leader points to the identically named event in
+		 * metric_events.
+		 */
+		metric_events[i]->metric_leader = metric_events[i];
+		/*
+		 * Mark two events with identical names in the same group (or
+		 * globally) as being in use as uncore events may be duplicated
+		 * for each pmu. Set the metric leader to be the event that
+		 * appears in metric_events.
+		 */
+		if (!has_constraint) {
+			for_each_group_evsel(ev, metric_events[i]->leader) {
+				if (ev != metric_events[i] &&
+				    !strcmp(metric_events[i]->name, ev->name)) {
+					set_bit(ev->idx, evlist_used);
+					ev->metric_leader = metric_events[i];
+				}
+			}
+		} else {
+			evlist__for_each_entry(perf_evlist, ev) {
+				if (ev != metric_events[i] &&
+				    !strcmp(metric_events[i]->name, ev->name)) {
+					set_bit(ev->idx, evlist_used);
+					ev->metric_leader = metric_events[i];
+				}
+			}
+		}
 	}
 
 	return metric_events[0];
Signature · 1 line
2.28.0.526.ge36021eeef-goog
maintainer
#5 lore raw

Re: [PATCH v2 3/3] perf metricgroup: Fix uncore metric expressions

Quoted 53 lines · Ian Rogers

A metric like DRAM_BW_Use has on SkylakeX events uncore_imc/cas_count_read/ and uncore_imc/case_count_write/. These events open 6 events per socket with pmu names of uncore_imc_[0-5]. The current metric setup code in find_evsel_group assumes one ID will map to 1 event to be recorded in metric_events. For events with multiple matches, the first event is recorded in metric_events (avoiding matching >1 event with the same name) and the evlist_used updated so that duplicate events aren't removed when the evlist has unused events removed.

Before this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

             41.14 MiB  uncore_imc/cas_count_read/
     1,002,614,251 ns   duration_time

       1.002614251 seconds time elapsed

After this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

            157.47 MiB  uncore_imc/cas_count_read/ #     0.00 DRAM_BW_Use
            126.97 MiB  uncore_imc/cas_count_write/
     1,003,019,728 ns   duration_time

v2. avoids iterating over the whole evlist as suggested by
[email protected]. It also fixes the metric_leader computation that was broken in the same commits.

Erroneous duplication introduced in:
commit 2440689d62e9 ("perf metricgroup: Remove duped metric group events").

Fixes ded80bda8bc9 ("perf expr: Migrate expr ids table to a hashmap").
---
 tools/perf/util/metricgroup.c | 45 ++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)
tools/perf/util/metricgroup.c +2 -0
index 662f4e8777d5..79080de9217d 100644
@@ -206,6 +206,18 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
                                sizeof(struct evsel *) * idnum);
                        current_leader = ev->leader;
                }
+               /*
+                * Check for duplicate events with the same name. For example,

tools/perf/util/metricgroup.c static struct evsel *find_evsel_group(struct evlist *perf_evlist, · line 220

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                * uncore_imc/cas_count_read/ will turn into 6 events per socket
+                * on skylakex. Only the first such event is placed in
+                * metric_events.
+                */
+               for (i = 0; i < matched_events; i++) {
+                       if (!strcmp(metric_events[i]->name, ev->name))
+                               break;
+               }
+               if (i != matched_events)
+                       continue;

We have the same logic in the below. Maybe it'd better to factor out..

Quoted 13 lines
tools/perf/util/metricgroup.c +3 -3
                if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
                        if (has_constraint) {
                                /*
@@ -245,9 +257,36 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
        metric_events[idnum] = NULL;
 
        for (i = 0; i < idnum; i++) {
-               ev = metric_events[i];
-               ev->metric_leader = ev;
-               set_bit(ev->idx, evlist_used);
+               /* Don't free used events. */
+               set_bit(metric_events[i]->idx, evlist_used);
+               /*

tools/perf/util/metricgroup.c static struct evsel *find_evsel_group(struct evlist *perf_evlist, · line 272

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                * The metric leader points to the identically named event in
+                * metric_events.
+                */
+               metric_events[i]->metric_leader = metric_events[i];
+               /*
+                * Mark two events with identical names in the same group (or
+                * globally) as being in use as uncore events may be duplicated
+                * for each pmu. Set the metric leader to be the event that
+                * appears in metric_events.
+                */

I thought this again, and it's not guaranteed that the metric leader is a group leader so below won't work IMHO. Instead we should iterate evlist always, but started from the metric leader with the evlist__for_each_entry_continue.

Thanks
Namhyung

Quoted 23 lines
tools/perf/util/metricgroup.c +17 -1
+               if (!has_constraint) {
+                       for_each_group_evsel(ev, metric_events[i]->leader) {
+                               if (ev != metric_events[i] &&
+                                   !strcmp(metric_events[i]->name, ev->name)) {
+                                       set_bit(ev->idx, evlist_used);
+                                       ev->metric_leader = metric_events[i];
+                               }
+                       }
+               } else {
+                       evlist__for_each_entry(perf_evlist, ev) {
+                               if (ev != metric_events[i] &&
+                                   !strcmp(metric_events[i]->name, ev->name)) {
+                                       set_bit(ev->idx, evlist_used);
+                                       ev->metric_leader = metric_events[i];
+                               }
+                       }
+               }
        }
 
        return metric_events[0];
--

2.28.0.526.ge36021eeef-goog

author reviewer
#6 lore raw

Re: [PATCH v2 3/3] perf metricgroup: Fix uncore metric expressions

Quoted 59 lines · Namhyung Kim
Quoted 57 lines · Ian Rogers

A metric like DRAM_BW_Use has on SkylakeX events uncore_imc/cas_count_read/ and uncore_imc/case_count_write/. These events open 6 events per socket with pmu names of uncore_imc_[0-5]. The current metric setup code in find_evsel_group assumes one ID will map to 1 event to be recorded in metric_events. For events with multiple matches, the first event is recorded in metric_events (avoiding matching >1 event with the same name) and the evlist_used updated so that duplicate events aren't removed when the evlist has unused events removed.

Before this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

             41.14 MiB  uncore_imc/cas_count_read/
     1,002,614,251 ns   duration_time

       1.002614251 seconds time elapsed

After this change:

$ /tmp/perf/perf stat -M DRAM_BW_Use -a -- sleep 1

Performance counter stats for 'system wide':

            157.47 MiB  uncore_imc/cas_count_read/ #     0.00 DRAM_BW_Use
            126.97 MiB  uncore_imc/cas_count_write/
     1,003,019,728 ns   duration_time

v2. avoids iterating over the whole evlist as suggested by
[email protected]. It also fixes the metric_leader computation that was broken in the same commits.

Erroneous duplication introduced in:
commit 2440689d62e9 ("perf metricgroup: Remove duped metric group events").

Fixes ded80bda8bc9 ("perf expr: Migrate expr ids table to a hashmap").
---
 tools/perf/util/metricgroup.c | 45 ++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)
tools/perf/util/metricgroup.c +6 -0
index 662f4e8777d5..79080de9217d 100644
@@ -206,6 +206,18 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
                                sizeof(struct evsel *) * idnum);
                        current_leader = ev->leader;
                }
+               /*
+                * Check for duplicate events with the same name. For example,
+                * uncore_imc/cas_count_read/ will turn into 6 events per socket
+                * on skylakex. Only the first such event is placed in
+                * metric_events.
+                */
Quoted 8 lines

tools/perf/util/metricgroup.c static struct evsel *find_evsel_group(struct evlist *perf_evlist, · line 220

Quoted 6 lines
tools/perf/util/metricgroup.c +6 -0
+               for (i = 0; i < matched_events; i++) {
+                       if (!strcmp(metric_events[i]->name, ev->name))
+                               break;
+               }
+               if (i != matched_events)
+                       continue;

We have the same logic in the below. Maybe it'd better to factor out..

You're right, actually with this loop the below is redundant.

Quoted 21 lines
Quoted 21 lines
tools/perf/util/metricgroup.c +10 -3
                if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
                        if (has_constraint) {
                                /*
@@ -245,9 +257,36 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,
        metric_events[idnum] = NULL;
 
        for (i = 0; i < idnum; i++) {
-               ev = metric_events[i];
-               ev->metric_leader = ev;
-               set_bit(ev->idx, evlist_used);
+               /* Don't free used events. */
+               set_bit(metric_events[i]->idx, evlist_used);
+               /*
+                * The metric leader points to the identically named event in
+                * metric_events.
+                */
+               metric_events[i]->metric_leader = metric_events[i];
+               /*
+                * Mark two events with identical names in the same group (or
+                * globally) as being in use as uncore events may be duplicated
Quoted 8 lines

tools/perf/util/metricgroup.c static struct evsel *find_evsel_group(struct evlist *perf_evlist, · line 272

Quoted 3 lines
tools/perf/util/metricgroup.c +3 -0
+                * for each pmu. Set the metric leader to be the event that
+                * appears in metric_events.
+                */

I thought this again, and it's not guaranteed that the metric leader is a group leader so below won't work IMHO. Instead we should iterate evlist always, but started from the metric leader with the evlist__for_each_entry_continue.

Thanks for pointing out evlist__for_each_entry_continue! For the constraint case it avoids iterating a bunch of list elements. For the sibling group case we do know that all the metric's events are siblings with the exception of duration time that is pulled out specially and not at risk of being aliased.

Thanks,
Ian

Quoted 27 lines

Thanks
Namhyung

Quoted 23 lines
tools/perf/util/metricgroup.c +17 -1
+               if (!has_constraint) {
+                       for_each_group_evsel(ev, metric_events[i]->leader) {
+                               if (ev != metric_events[i] &&
+                                   !strcmp(metric_events[i]->name, ev->name)) {
+                                       set_bit(ev->idx, evlist_used);
+                                       ev->metric_leader = metric_events[i];
+                               }
+                       }
+               } else {
+                       evlist__for_each_entry(perf_evlist, ev) {
+                               if (ev != metric_events[i] &&
+                                   !strcmp(metric_events[i]->name, ev->name)) {
+                                       set_bit(ev->idx, evlist_used);
+                                       ev->metric_leader = metric_events[i];
+                               }
+                       }
+               }
        }
 
        return metric_events[0];
--

2.28.0.526.ge36021eeef-goog

maintainer Acked-by on v2 1/3
#7 lore raw
In reply to Ian Rogers

Hi Ian,

Quoted 4 lines · Ian Rogers

No need to set os.evsel twice.

Btw, I think setting the 'out' variable can be moved out of the loop.

Thanks
Namhyung

Quoted 19 lines
---
 tools/perf/util/stat-display.c | 1 -
 1 file changed, 1 deletion(-)
tools/perf/util/stat-display.c +0 -2
index 493ec372fdec..4b57c0c07632 100644
@@ -946,7 +946,6 @@ static void print_metric_headers(struct perf_stat_config *config,
                out.print_metric = print_metric_header;
                out.new_line = new_line_metric;
                out.force_header = true;
-               os.evsel = counter;
                perf_stat__print_shadow_stats(config, counter, 0,
                                              0,
                                              &out,
--

2.28.0.526.ge36021eeef-goog