Skip to content

Commit 55c36a9

Browse files
Andi Kleenacmel
authored andcommitted
perf stat: Support new per thread TopDown metrics
Icelake has support for reporting per thread TopDown metrics. These are reported differently than the previous TopDown support, each metric is standalone, but scaled to pipeline "slots". We don't need to do anything special for HyperThreading anymore. Teach perf stat --topdown to handle these new metrics and print them in the same way as the previous TopDown metrics. The restrictions of only being able to report information per core is gone. Signed-off-by: Andi Kleen <ak@linux.intel.com> Co-developed-by: Kan Liang <kan.liang@linux.intel.com> Acked-by: Jiri Olsa <jolsa@redhat.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lore.kernel.org/lkml/20200911144808.27603-4-kan.liang@linux.intel.com Signed-off-by: Kan Liang <kan.liang@linux.intel.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent acb6515 commit 55c36a9

5 files changed

Lines changed: 134 additions & 4 deletions

File tree

‎tools/perf/Documentation/perf-stat.txt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ if the workload is actually bound by the CPU and not by something else.
363363
For best results it is usually a good idea to use it with interval
364364
mode like -I 1000, as the bottleneck of workloads can change often.
365365

366+
This enables --metric-only, unless overridden with --no-metric-only.
367+
368+
The following restrictions only apply to older Intel CPUs and Atom,
369+
on newer CPUs (IceLake and later) TopDown can be collected for any thread:
370+
366371
The top down metrics are collected per core instead of per
367372
CPU thread. Per core mode is automatically enabled
368373
and -a (global monitoring) is needed, requiring root rights or
@@ -374,8 +379,6 @@ echo 0 > /proc/sys/kernel/nmi_watchdog
374379
for best results. Otherwise the bottlenecks may be inconsistent
375380
on workload with changing phases.
376381

377-
This enables --metric-only, unless overridden with --no-metric-only.
378-
379382
To interpret the results it is usually needed to know on which
380383
CPUs the workload runs on. If needed the CPUs can be forced using
381384
taskset.

‎tools/perf/builtin-stat.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ static const char * topdown_attrs[] = {
128128
NULL,
129129
};
130130

131+
static const char *topdown_metric_attrs[] = {
132+
"slots",
133+
"topdown-retiring",
134+
"topdown-bad-spec",
135+
"topdown-fe-bound",
136+
"topdown-be-bound",
137+
NULL,
138+
};
139+
131140
static const char *smi_cost_attrs = {
132141
"{"
133142
"msr/aperf/,"
@@ -1677,6 +1686,24 @@ static int add_default_attributes(void)
16771686
char *str = NULL;
16781687
bool warn = false;
16791688

1689+
if (!force_metric_only)
1690+
stat_config.metric_only = true;
1691+
1692+
if (topdown_filter_events(topdown_metric_attrs, &str, 1) < 0) {
1693+
pr_err("Out of memory\n");
1694+
return -1;
1695+
}
1696+
if (topdown_metric_attrs[0] && str) {
1697+
if (!stat_config.interval && !stat_config.metric_only) {
1698+
fprintf(stat_config.output,
1699+
"Topdown accuracy may decrease when measuring long periods.\n"
1700+
"Please print the result regularly, e.g. -I1000\n");
1701+
}
1702+
goto setup_metrics;
1703+
}
1704+
1705+
zfree(&str);
1706+
16801707
if (stat_config.aggr_mode != AGGR_GLOBAL &&
16811708
stat_config.aggr_mode != AGGR_CORE) {
16821709
pr_err("top down event configuration requires --per-core mode\n");
@@ -1688,8 +1715,6 @@ static int add_default_attributes(void)
16881715
return -1;
16891716
}
16901717

1691-
if (!force_metric_only)
1692-
stat_config.metric_only = true;
16931718
if (topdown_filter_events(topdown_attrs, &str,
16941719
arch_topdown_check_group(&warn)) < 0) {
16951720
pr_err("Out of memory\n");
@@ -1698,6 +1723,7 @@ static int add_default_attributes(void)
16981723
if (topdown_attrs[0] && str) {
16991724
if (warn)
17001725
arch_topdown_group_warn();
1726+
setup_metrics:
17011727
err = parse_events(evsel_list, str, &errinfo);
17021728
if (err) {
17031729
fprintf(stderr,

‎tools/perf/util/stat-shadow.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
241241
else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
242242
update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES,
243243
ctx, cpu, count);
244+
else if (perf_stat_evsel__is(counter, TOPDOWN_RETIRING))
245+
update_runtime_stat(st, STAT_TOPDOWN_RETIRING,
246+
ctx, cpu, count);
247+
else if (perf_stat_evsel__is(counter, TOPDOWN_BAD_SPEC))
248+
update_runtime_stat(st, STAT_TOPDOWN_BAD_SPEC,
249+
ctx, cpu, count);
250+
else if (perf_stat_evsel__is(counter, TOPDOWN_FE_BOUND))
251+
update_runtime_stat(st, STAT_TOPDOWN_FE_BOUND,
252+
ctx, cpu, count);
253+
else if (perf_stat_evsel__is(counter, TOPDOWN_BE_BOUND))
254+
update_runtime_stat(st, STAT_TOPDOWN_BE_BOUND,
255+
ctx, cpu, count);
244256
else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
245257
update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT,
246258
ctx, cpu, count);
@@ -705,6 +717,47 @@ static double td_be_bound(int ctx, int cpu, struct runtime_stat *st)
705717
return sanitize_val(1.0 - sum);
706718
}
707719

720+
/*
721+
* Kernel reports metrics multiplied with slots. To get back
722+
* the ratios we need to recreate the sum.
723+
*/
724+
725+
static double td_metric_ratio(int ctx, int cpu,
726+
enum stat_type type,
727+
struct runtime_stat *stat)
728+
{
729+
double sum = runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, ctx, cpu) +
730+
runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, ctx, cpu) +
731+
runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, ctx, cpu) +
732+
runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, ctx, cpu);
733+
double d = runtime_stat_avg(stat, type, ctx, cpu);
734+
735+
if (sum)
736+
return d / sum;
737+
return 0;
738+
}
739+
740+
/*
741+
* ... but only if most of the values are actually available.
742+
* We allow two missing.
743+
*/
744+
745+
static bool full_td(int ctx, int cpu,
746+
struct runtime_stat *stat)
747+
{
748+
int c = 0;
749+
750+
if (runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, ctx, cpu) > 0)
751+
c++;
752+
if (runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, ctx, cpu) > 0)
753+
c++;
754+
if (runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, ctx, cpu) > 0)
755+
c++;
756+
if (runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, ctx, cpu) > 0)
757+
c++;
758+
return c >= 2;
759+
}
760+
708761
static void print_smi_cost(struct perf_stat_config *config,
709762
int cpu, struct evsel *evsel,
710763
struct perf_stat_output_ctx *out,
@@ -1073,6 +1126,42 @@ void perf_stat__print_shadow_stats(struct perf_stat_config *config,
10731126
be_bound * 100.);
10741127
else
10751128
print_metric(config, ctxp, NULL, NULL, name, 0);
1129+
} else if (perf_stat_evsel__is(evsel, TOPDOWN_RETIRING) &&
1130+
full_td(ctx, cpu, st)) {
1131+
double retiring = td_metric_ratio(ctx, cpu,
1132+
STAT_TOPDOWN_RETIRING, st);
1133+
1134+
if (retiring > 0.7)
1135+
color = PERF_COLOR_GREEN;
1136+
print_metric(config, ctxp, color, "%8.1f%%", "retiring",
1137+
retiring * 100.);
1138+
} else if (perf_stat_evsel__is(evsel, TOPDOWN_FE_BOUND) &&
1139+
full_td(ctx, cpu, st)) {
1140+
double fe_bound = td_metric_ratio(ctx, cpu,
1141+
STAT_TOPDOWN_FE_BOUND, st);
1142+
1143+
if (fe_bound > 0.2)
1144+
color = PERF_COLOR_RED;
1145+
print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
1146+
fe_bound * 100.);
1147+
} else if (perf_stat_evsel__is(evsel, TOPDOWN_BE_BOUND) &&
1148+
full_td(ctx, cpu, st)) {
1149+
double be_bound = td_metric_ratio(ctx, cpu,
1150+
STAT_TOPDOWN_BE_BOUND, st);
1151+
1152+
if (be_bound > 0.2)
1153+
color = PERF_COLOR_RED;
1154+
print_metric(config, ctxp, color, "%8.1f%%", "backend bound",
1155+
be_bound * 100.);
1156+
} else if (perf_stat_evsel__is(evsel, TOPDOWN_BAD_SPEC) &&
1157+
full_td(ctx, cpu, st)) {
1158+
double bad_spec = td_metric_ratio(ctx, cpu,
1159+
STAT_TOPDOWN_BAD_SPEC, st);
1160+
1161+
if (bad_spec > 0.1)
1162+
color = PERF_COLOR_RED;
1163+
print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
1164+
bad_spec * 100.);
10761165
} else if (evsel->metric_expr) {
10771166
generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL,
10781167
evsel->name, evsel->metric_name, NULL, 1, cpu, out, st);

‎tools/perf/util/stat.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
9595
ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
9696
ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
9797
ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
98+
ID(TOPDOWN_RETIRING, topdown-retiring),
99+
ID(TOPDOWN_BAD_SPEC, topdown-bad-spec),
100+
ID(TOPDOWN_FE_BOUND, topdown-fe-bound),
101+
ID(TOPDOWN_BE_BOUND, topdown-be-bound),
98102
ID(SMI_NUM, msr/smi/),
99103
ID(APERF, msr/aperf/),
100104
};

‎tools/perf/util/stat.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ enum perf_stat_evsel_id {
2828
PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
2929
PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
3030
PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
31+
PERF_STAT_EVSEL_ID__TOPDOWN_RETIRING,
32+
PERF_STAT_EVSEL_ID__TOPDOWN_BAD_SPEC,
33+
PERF_STAT_EVSEL_ID__TOPDOWN_FE_BOUND,
34+
PERF_STAT_EVSEL_ID__TOPDOWN_BE_BOUND,
3135
PERF_STAT_EVSEL_ID__SMI_NUM,
3236
PERF_STAT_EVSEL_ID__APERF,
3337
PERF_STAT_EVSEL_ID__MAX,
@@ -82,6 +86,10 @@ enum stat_type {
8286
STAT_TOPDOWN_SLOTS_RETIRED,
8387
STAT_TOPDOWN_FETCH_BUBBLES,
8488
STAT_TOPDOWN_RECOVERY_BUBBLES,
89+
STAT_TOPDOWN_RETIRING,
90+
STAT_TOPDOWN_BAD_SPEC,
91+
STAT_TOPDOWN_FE_BOUND,
92+
STAT_TOPDOWN_BE_BOUND,
8593
STAT_SMI_NUM,
8694
STAT_APERF,
8795
STAT_MAX

0 commit comments

Comments
 (0)