Some bug fixes for perf stat metrics
Fix some bugs and regressions in perf stat --metrics support.
Also available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc perf/metric-fixes-1
PATCH 1/4 perf stat: Make metric event lookup more robust
From: Andi Kleen <[email protected]>
After setting up metric groups through the event parser, the metricgroup code looks them up again in the event list.
Make sure we only look up events that haven't been used by some other metric. The data structures currently cannot handle more than one metric per event. This avoids problems with multiple events partially overlapping.
--- tools/perf/util/stat-shadow.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/stat-shadow.c +3 -2
@@ -304,7 +304,7 @@ static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,struct perf_evsel *c2; evlist__for_each_entry (evsel_list, c2) { -if (!strcasecmp (c2->name, name)) +if (!strcasecmp (c2->name, name) && !c2->collect_stat)return c2; }return NULL ;
@@ -343,7 +343,8 @@ void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)if (leader) {/* Search in group */ for_each_group_member (oc, leader) { -if (!strcasecmp (oc->name, metric_names[i])) { +if (!strcasecmp (oc->name, metric_names[i]) && + !oc->collect_stat) { found = true;break ; }
Signature · 1 line
2.20.1
tip-bot for Andi Kleen
merge bot[tip:perf/core] perf stat: Make metric event lookup more robust
perf stat: Make metric event lookup more robust
Commit-ID: 145c407c808352acd625be793396fd4f33c794f8 Gitweb: https://git.kernel.org/tip/145c407c808352acd625be793396fd4f33c794f8 Author: Andi Kleen <[email protected]> AuthorDate: Mon, 24 Jun 2019 12:37:08 -0700 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 1 Jul 2019 22:50:41 -0300
perf stat: Make metric event lookup more robust
After setting up metric groups through the event parser, the metricgroup code looks them up again in the event list.
Make sure we only look up events that haven't been used by some other metric. The data structures currently cannot handle more than one metric per event. This avoids problems with multiple events partially overlapping.
--- tools/perf/util/stat-shadow.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/stat-shadow.c +3 -2
@@ -304,7 +304,7 @@ static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,struct perf_evsel *c2; evlist__for_each_entry (evsel_list, c2) { -if (!strcasecmp (c2->name, name)) +if (!strcasecmp (c2->name, name) && !c2->collect_stat)return c2; }return NULL ;
@@ -343,7 +343,8 @@ void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)if (leader) {/* Search in group */ for_each_group_member (oc, leader) { -if (!strcasecmp (oc->name, metric_names[i])) { +if (!strcasecmp (oc->name, metric_names[i]) && + !oc->collect_stat) { found = true;break ; }
PATCH 2/4 perf stat: Don't merge events in the same PMU
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Event merging is mainly to collapse similar events in lots of different duplicated PMUs.
It can break metric displaying. It's possible for two metrics to have the same event, and when the two events happen in a row the second wouldn't be displayed. This would also not show the second metric.
To avoid this don't merge events in the same PMU. This makes sense, if we have multiple events in the same PMU there is likely some reason for it (e.g. using multiple groups) and we better not merge them.
While in theory it would be possible to construct metrics that have events with the same name in different PMU no current metrics have this problem.
This is the fix for perf stat -M UPI,IPC (needs also another bug fix to completely work)
--- tools/perf/util/stat-display.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-display.c +2 -1
@@ -555,7 +555,8 @@ static void collect_all_aliases(struct perf_stat_config *config, struct perf_evs alias->scale != counter->scale || alias->cgrp != counter->cgrp ||strcmp (alias->unit, counter->unit) || - perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter)) +perf_evsel__is_clock (alias) !=perf_evsel__is_clock (counter) || + !strcmp(alias->pmu_name, counter->pmu_name)) break; alias->merged_stat = true;cb (config, alias, data, false);
Signature · 1 line
2.20.1
tip-bot for Andi Kleen
merge bot[tip:perf/core] perf stat: Don't merge events in the same PMU
perf stat: Don't merge events in the same PMU
Commit-ID: 6c5f4e5cb35b4694dc035d91092d23f596ecd06a Gitweb: https://git.kernel.org/tip/6c5f4e5cb35b4694dc035d91092d23f596ecd06a Author: Andi Kleen <[email protected]> AuthorDate: Mon, 24 Jun 2019 12:37:09 -0700 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 1 Jul 2019 22:50:41 -0300
perf stat: Don't merge events in the same PMU
Event merging is mainly to collapse similar events in lots of different duplicated PMUs.
It can break metric displaying. It's possible for two metrics to have the same event, and when the two events happen in a row the second wouldn't be displayed. This would also not show the second metric.
To avoid this don't merge events in the same PMU. This makes sense, if we have multiple events in the same PMU there is likely some reason for it (e.g. using multiple groups) and we better not merge them.
While in theory it would be possible to construct metrics that have events with the same name in different PMU no current metrics have this problem.
This is the fix for perf stat -M UPI,IPC (needs also another bug fix to completely work)
--- tools/perf/util/stat-display.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-display.c +2 -1
@@ -554,7 +554,8 @@ static void collect_all_aliases(struct perf_stat_config *config, struct perf_evs alias->scale != counter->scale || alias->cgrp != counter->cgrp ||strcmp (alias->unit, counter->unit) || - perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter)) +perf_evsel__is_clock (alias) !=perf_evsel__is_clock (counter) || + !strcmp(alias->pmu_name, counter->pmu_name)) break; alias->merged_stat = true;cb (config, alias, data, false);
PATCH 3/4 perf stat: Fix group lookup for metric group
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
The metric group code tries to find a group it added earlier in the evlist. Fix the lookup to handle groups with partially overlaps correctly. When a sub string match fails and we reset the match, we have to compare the first element again.
I also renamed the find_evsel function to find_evsel_group to make its purpose clearer.
With the earlier changes this fixes:
Before:
% perf stat -M UPI,IPC sleep 1
...
1,032,922 uops_retired.retire_slots # 1.1 UPI
1,896,096 inst_retired.any
1,896,096 inst_retired.any
1,177,254 cpu_clk_unhalted.threadAfter:
% perf stat -M UPI,IPC sleep 1
...
1,013,193 uops_retired.retire_slots # 1.1 UPI
932,033 inst_retired.any
932,033 inst_retired.any # 0.9 IPC
1,091,245 cpu_clk_unhalted.thread--- tools/perf/util/metricgroup.c | 47 ++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-)
tools/perf/util/metricgroup.c +35 -12
@@ -85,26 +85,49 @@ struct egroup {const char *metric_expr; }; -static struct perf_evsel *find_evsel (struct perf_evlist *perf_evlist, -const char **ids, -int idnum, -struct perf_evsel **metric_events) +static bool record_evsel (int *ind,struct perf_evsel **start, +int idnum, +struct perf_evsel **metric_events, +struct perf_evsel *ev) +{ + metric_events[*ind] = ev; +if (*ind ==0 ) + *start = ev; +if (++*ind == idnum) { + metric_events[*ind] =NULL ; +return true; + } +return false; +} + +static struct perf_evsel *find_evsel_group (struct perf_evlist *perf_evlist, +const char **ids, +int idnum, +struct perf_evsel **metric_events) {struct perf_evsel *ev, *start =NULL ;int ind =0 ; evlist__for_each_entry (perf_evlist, ev) { +if (ev->collect_stat) +continue ;if (!strcmp (ev->name, ids[ind])) { - metric_events[ind] = ev; -if (ind ==0 ) - start = ev; -if (++ind == idnum) { - metric_events[ind] =NULL ; +if (record_evsel (&ind, &start, idnum, + metric_events, ev))return start; - } }else { +/* +* We saw some other event that is not +* in our list of events. Discard +* the whole match and start again. +*/ ind =0 ; start =NULL ; +if (!strcmp (ev->name, ids[ind])) { +if (record_evsel (&ind, &start, idnum, + metric_events, ev)) +return start; + } } }/*
@@ -134,8 +157,8 @@ static int metricgroup__setup_events(struct list_head *groups, ret = -ENOMEM ;break ; } - evsel =find_evsel (perf_evlist, eg->ids, eg->idnum, - metric_events); + evsel =find_evsel_group (perf_evlist, eg->ids, eg->idnum, + metric_events);if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr);
Signature · 1 line
2.20.1
tip-bot for Andi Kleen
merge bot[tip:perf/core] perf stat: Fix group lookup for metric group
perf stat: Fix group lookup for metric group
Commit-ID: 2f87f33f4226523df9c9cc28f9874ea02fcc3d3f Gitweb: https://git.kernel.org/tip/2f87f33f4226523df9c9cc28f9874ea02fcc3d3f Author: Andi Kleen <[email protected]> AuthorDate: Mon, 24 Jun 2019 12:37:10 -0700 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 1 Jul 2019 22:50:41 -0300
perf stat: Fix group lookup for metric group
The metric group code tries to find a group it added earlier in the evlist. Fix the lookup to handle groups with partially overlaps correctly. When a sub string match fails and we reset the match, we have to compare the first element again.
I also renamed the find_evsel function to find_evsel_group to make its purpose clearer.
With the earlier changes this fixes:
Before:
% perf stat -M UPI,IPC sleep 1
...
1,032,922 uops_retired.retire_slots # 1.1 UPI
1,896,096 inst_retired.any
1,896,096 inst_retired.any
1,177,254 cpu_clk_unhalted.threadAfter:
% perf stat -M UPI,IPC sleep 1
...
1,013,193 uops_retired.retire_slots # 1.1 UPI
932,033 inst_retired.any
932,033 inst_retired.any # 0.9 IPC
1,091,245 cpu_clk_unhalted.thread--- tools/perf/util/metricgroup.c | 47 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-)
tools/perf/util/metricgroup.c +35 -12
@@ -85,26 +85,49 @@ struct egroup {const char *metric_expr; }; -static struct perf_evsel *find_evsel (struct perf_evlist *perf_evlist, -const char **ids, -int idnum, -struct perf_evsel **metric_events) +static bool record_evsel (int *ind,struct perf_evsel **start, +int idnum, +struct perf_evsel **metric_events, +struct perf_evsel *ev) +{ + metric_events[*ind] = ev; +if (*ind ==0 ) + *start = ev; +if (++*ind == idnum) { + metric_events[*ind] =NULL ; +return true; + } +return false; +} + +static struct perf_evsel *find_evsel_group (struct perf_evlist *perf_evlist, +const char **ids, +int idnum, +struct perf_evsel **metric_events) {struct perf_evsel *ev, *start =NULL ;int ind =0 ; evlist__for_each_entry (perf_evlist, ev) { +if (ev->collect_stat) +continue ;if (!strcmp (ev->name, ids[ind])) { - metric_events[ind] = ev; -if (ind ==0 ) - start = ev; -if (++ind == idnum) { - metric_events[ind] =NULL ; +if (record_evsel (&ind, &start, idnum, + metric_events, ev))return start; - } }else { +/* +* We saw some other event that is not +* in our list of events. Discard +* the whole match and start again. +*/ ind =0 ; start =NULL ; +if (!strcmp (ev->name, ids[ind])) { +if (record_evsel (&ind, &start, idnum, + metric_events, ev)) +return start; + } } }/*
@@ -134,8 +157,8 @@ static int metricgroup__setup_events(struct list_head *groups, ret = -ENOMEM ;break ; } - evsel =find_evsel (perf_evlist, eg->ids, eg->idnum, - metric_events); + evsel =find_evsel_group (perf_evlist, eg->ids, eg->idnum, + metric_events);if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr);
PATCH 4/4 perf stat: Fix metrics with --no-merge
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Since 8c5421c016a4 ("perf pmu: Display pmu name when printing ...") using --no-merge adds the PMU name to the evsel name. This breaks the metric value lookup because the parser doesn't know about this.
Remove the extra postfixes for the metric evaluation.
--- tools/perf/util/stat-shadow.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +17 -1
@@ -724,6 +724,7 @@ static void generic_metric(struct perf_stat_config *config,double ratio;int i;void *ctxp = out->ctx; +char *n, *pn;expr__ctx_init (&pctx);expr__add_id (&pctx, name, avg);
@@ -743,7 +744,19 @@ static void generic_metric(struct perf_stat_config *config, stats = &v->stats; scale =1.0 ; } -expr__add_id (&pctx, metric_events[i]->name,avg_stats (stats)*scale); + + n =strdup (metric_events[i]->name); +if (!n) +return ; +/* +* This display code with --no-merge adds [cpu] postfixes. +* These are not supported by the parser. Remove everything +* after the space. +*/ + pn =strchr (n,' ' ); +if (pn) + *pn =0 ; +expr__add_id (&pctx, n,avg_stats (stats)*scale); }if (!metric_events[i]) {const char *p = metric_expr;
@@ -760,6 +773,9 @@ static void generic_metric(struct perf_stat_config *config, (metric_name ? metric_name : name) : "", 0); }else print_metric (config, ctxp,NULL ,NULL ,"" ,0 ); + +for (i =1 ; i < pctx.num_ids; i++) +free ((void *)pctx.ids[i].name); } void perf_stat__print_shadow_stats(struct perf_stat_config *config,
Signature · 1 line
2.20.1
tip-bot for Andi Kleen
merge bot[tip:perf/core] perf stat: Fix metrics with --no-merge
perf stat: Fix metrics with --no-merge
Commit-ID: e3a9427323a53ceee540276a74af7706f350d052 Gitweb: https://git.kernel.org/tip/e3a9427323a53ceee540276a74af7706f350d052 Author: Andi Kleen <[email protected]> AuthorDate: Mon, 24 Jun 2019 12:37:11 -0700 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 1 Jul 2019 22:50:41 -0300
perf stat: Fix metrics with --no-merge
Since Fixes: 8c5421c016a4 ("perf pmu: Display pmu name when printing unmerged events in stat") using --no-merge adds the PMU name to the evsel name.
This breaks the metric value lookup because the parser doesn't know about this.
Remove the extra postfixes for the metric evaluation.
--- tools/perf/util/stat-shadow.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +17 -1
@@ -724,6 +724,7 @@ static void generic_metric(struct perf_stat_config *config,double ratio;int i;void *ctxp = out->ctx; +char *n, *pn;expr__ctx_init (&pctx);expr__add_id (&pctx, name, avg);
@@ -743,7 +744,19 @@ static void generic_metric(struct perf_stat_config *config, stats = &v->stats; scale =1.0 ; } -expr__add_id (&pctx, metric_events[i]->name,avg_stats (stats)*scale); + + n =strdup (metric_events[i]->name); +if (!n) +return ; +/* +* This display code with --no-merge adds [cpu] postfixes. +* These are not supported by the parser. Remove everything +* after the space. +*/ + pn =strchr (n,' ' ); +if (pn) + *pn =0 ; +expr__add_id (&pctx, n,avg_stats (stats)*scale); }if (!metric_events[i]) {const char *p = metric_expr;
@@ -760,6 +773,9 @@ static void generic_metric(struct perf_stat_config *config, (metric_name ? metric_name : name) : "", 0); }else print_metric (config, ctxp,NULL ,NULL ,"" ,0 ); + +for (i =1 ; i < pctx.num_ids; i++) +free ((void *)pctx.ids[i].name); } void perf_stat__print_shadow_stats(struct perf_stat_config *config,
Jiri Olsa
Acked-byQuoted 5 lines · Andi Kleen
Fix some bugs and regressions in perf stat --metrics support.
Also available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-miscperf/metric-fixes-1
looks good to me
thanks,
jirka
Arnaldo Carvalho de Melo
AppliedEm Tue, Jun 25, 2019 at 11:23:17AM +0200, Jiri Olsa escreveu:
Quoted 10 lines · Andi Kleen
Quoted 5 lines
Fix some bugs and regressions in perf stat --metrics support.
Also available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-miscperf/metric-fixes-1looks good to me
Acked-by Jiri Olsa <[email protected]>
Thanks, applied.
- Arnaldo