2019-06-24 21:58:27

by Andi Kleen

[permalink] [raw]
Subject: 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



2019-06-24 21:58:29

by Andi Kleen

[permalink] [raw]
Subject: [PATCH v1 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.

Signed-off-by: Andi Kleen <[email protected]>
---
tools/perf/util/stat-shadow.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 027b09aaa4cf..3f8fd127d31e 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -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;
}
--
2.20.1

2019-06-24 21:58:34

by Andi Kleen

[permalink] [raw]
Subject: [PATCH v1 4/4] perf stat: Fix metrics with --no-merge

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

Fixes: 8c5421c016a4 ("perf pmu: Display pmu name when printing ...")
Cc: Agustin Vega-Frias <[email protected]>
Signed-off-by: Andi Kleen <[email protected]>
---
tools/perf/util/stat-shadow.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 3f8fd127d31e..cb891e5c2969 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -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,
--
2.20.1

2019-06-24 21:58:47

by Andi Kleen

[permalink] [raw]
Subject: [PATCH v1 2/4] perf stat: Don't merge events in the same PMU

From: 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)

Fixes: 430daf2dc7af ("perf stat: Collapse identically ...")
Signed-off-by: Andi Kleen <[email protected]>
---
tools/perf/util/stat-display.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index a6b9de3e83fc..91d62fd6607f 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -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);
--
2.20.1

2019-06-25 11:56:36

by Jiri Olsa

[permalink] [raw]
Subject: Re: Some bug fixes for perf stat metrics

On Mon, Jun 24, 2019 at 12:37:07PM -0700, Andi Kleen wrote:
> 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

looks good to me

Acked-by: Jiri Olsa <[email protected]>

thanks,
jirka

2019-06-26 18:44:44

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: Some bug fixes for perf stat metrics

Em Tue, Jun 25, 2019 at 11:23:17AM +0200, Jiri Olsa escreveu:
> On Mon, Jun 24, 2019 at 12:37:07PM -0700, Andi Kleen wrote:
> > 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
>
> looks good to me
>
> Acked-by: Jiri Olsa <[email protected]>

Thanks, applied.

- Arnaldo

Subject: [tip:perf/core] 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.

Signed-off-by: Andi Kleen <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Kan Liang <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/stat-shadow.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 027b09aaa4cf..3f8fd127d31e 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -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;
}

Subject: [tip:perf/core] 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)

Signed-off-by: Andi Kleen <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Kan Liang <[email protected]>
Fixes: 430daf2dc7af ("perf stat: Collapse identically named events")
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/stat-display.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index 90df41169113..58df6a0dbb9f 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -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);

Subject: [tip:perf/core] 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.

Signed-off-by: Andi Kleen <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Agustin Vega-Frias <[email protected]>
Cc: Kan Liang <[email protected]>
Fixes: 8c5421c016a4 ("perf pmu: Display pmu name when printing unmerged events in stat")
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/stat-shadow.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 3f8fd127d31e..cb891e5c2969 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -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,