[PATCH 10/10] perf, tools: Add support for MetricName JSON attribute

From: Andi Kleen
Date: Thu Feb 23 2017 - 19:14:12 EST


From: Andi Kleen <ak@xxxxxxxxxxxxxxx>

Add support for a new JSON event attribute to name MetricExpr for better output
in perf stat.
If the event has no MetricName it uses the normal event name instead to describe
the metric.

So far no names are added.

Signed-off-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
---
tools/perf/pmu-events/jevents.c | 14 +++++++++++---
tools/perf/pmu-events/jevents.h | 3 ++-
tools/perf/pmu-events/pmu-events.h | 1 +
tools/perf/util/evsel.c | 1 +
tools/perf/util/evsel.h | 1 +
tools/perf/util/parse-events.c | 1 +
tools/perf/util/pmu.c | 15 ++++++++++++---
tools/perf/util/pmu.h | 2 ++
tools/perf/util/stat-shadow.c | 4 +++-
9 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/tools/perf/pmu-events/jevents.c b/tools/perf/pmu-events/jevents.c
index 0735dc2a167a..81f2ef3b15cf 100644
--- a/tools/perf/pmu-events/jevents.c
+++ b/tools/perf/pmu-events/jevents.c
@@ -292,7 +292,8 @@ static void print_events_table_prefix(FILE *fp, const char *tblname)
static int print_events_table_entry(void *data, char *name, char *event,
char *desc, char *long_desc,
char *pmu, char *unit, char *perpkg,
- char *metric_expr)
+ char *metric_expr,
+ char *metric_name)
{
struct perf_entry_data *pd = data;
FILE *outfp = pd->outfp;
@@ -318,6 +319,8 @@ static int print_events_table_entry(void *data, char *name, char *event,
fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
if (metric_expr)
fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
+ if (metric_name)
+ fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
fprintf(outfp, "},\n");

return 0;
@@ -366,7 +369,8 @@ int json_events(const char *fn,
int (*func)(void *data, char *name, char *event, char *desc,
char *long_desc,
char *pmu, char *unit, char *perpkg,
- char *metric_expr),
+ char *metric_expr,
+ char *metric_name),
void *data)
{
int err = -EIO;
@@ -393,6 +397,7 @@ int json_events(const char *fn,
char *perpkg = NULL;
char *unit = NULL;
char *metric_expr = NULL;
+ char *metric_name = NULL;
unsigned long long eventcode = 0;
struct msrmap *msr = NULL;
jsmntok_t *msrval = NULL;
@@ -469,6 +474,8 @@ int json_events(const char *fn,
addfield(map, &unit, "", "", val);
} else if (json_streq(map, field, "PerPkg")) {
addfield(map, &perpkg, "", "", val);
+ } else if (json_streq(map, field, "MetricName")) {
+ addfield(map, &metric_name, "", "", val);
} else if (json_streq(map, field, "MetricExpr")) {
addfield(map, &metric_expr, "", "", val);
for (s = metric_expr; *s; s++)
@@ -497,7 +504,7 @@ int json_events(const char *fn,
fixname(name);

err = func(data, name, real_event(name, event), desc, long_desc,
- pmu, unit, perpkg, metric_expr);
+ pmu, unit, perpkg, metric_expr, metric_name);
free(event);
free(desc);
free(name);
@@ -508,6 +515,7 @@ int json_events(const char *fn,
free(perpkg);
free(unit);
free(metric_expr);
+ free(metric_name);
if (err)
break;
tok += j;
diff --git a/tools/perf/pmu-events/jevents.h b/tools/perf/pmu-events/jevents.h
index 57e111bf2168..611fac01913d 100644
--- a/tools/perf/pmu-events/jevents.h
+++ b/tools/perf/pmu-events/jevents.h
@@ -5,7 +5,8 @@ int json_events(const char *fn,
int (*func)(void *data, char *name, char *event, char *desc,
char *long_desc,
char *pmu,
- char *unit, char *perpkg, char *metric_expr),
+ char *unit, char *perpkg, char *metric_expr,
+ char *metric_name),
void *data);
char *get_cpu_str(void);

diff --git a/tools/perf/pmu-events/pmu-events.h b/tools/perf/pmu-events/pmu-events.h
index d046e3a4ce46..569eab3688dd 100644
--- a/tools/perf/pmu-events/pmu-events.h
+++ b/tools/perf/pmu-events/pmu-events.h
@@ -14,6 +14,7 @@ struct pmu_event {
const char *unit;
const char *perpkg;
const char *metric_expr;
+ const char *metric_name;
};

/*
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index a25dc688ab74..c16a1e1253a2 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -237,6 +237,7 @@ void perf_evsel__init(struct perf_evsel *evsel,
perf_evsel__calc_id_pos(evsel);
evsel->cmdline_group_boundary = false;
evsel->metric_expr = NULL;
+ evsel->metric_name = NULL;
evsel->metric_events = NULL;
evsel->collect_stat = false;
}
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 8f1f61826fdf..d101695c482c 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -133,6 +133,7 @@ struct perf_evsel {
int bpf_fd;
bool merged_stat;
const char * metric_expr;
+ const char * metric_name;
struct perf_evsel **metric_events;
bool collect_stat;
};
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index fcb0b6854642..4b063ff01410 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -1255,6 +1255,7 @@ int parse_events_add_pmu(struct parse_events_evlist *data,
evsel->per_pkg = info.per_pkg;
evsel->snapshot = info.snapshot;
evsel->metric_expr = info.metric_expr;
+ evsel->metric_name = info.metric_name;
}

return evsel ? 0 : -ENOMEM;
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 2cb463ababb8..633530fbfe1e 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -232,7 +232,8 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
char *desc, char *val,
char *long_desc, char *topic,
char *unit, char *perpkg,
- char *metric_expr)
+ char *metric_expr,
+ char *metric_name)
{
struct perf_pmu_alias *alias;
int ret;
@@ -267,6 +268,7 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
}

alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
+ alias->metric_name = metric_name ? strdup(metric_name): NULL;
alias->desc = desc ? strdup(desc) : NULL;
alias->long_desc = long_desc ? strdup(long_desc) :
desc ? strdup(desc) : NULL;
@@ -296,7 +298,7 @@ static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FI
buf[ret] = 0;

return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
- NULL, NULL);
+ NULL, NULL, NULL);
}

static inline bool pmu_alias_info_file(char *name)
@@ -567,7 +569,8 @@ static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
(char *)pe->desc, (char *)pe->event,
(char *)pe->long_desc, (char *)pe->topic,
(char *)pe->unit, (char *)pe->perpkg,
- (char *)pe->metric_expr);
+ (char *)pe->metric_expr,
+ (char *)pe->metric_name);
}

out:
@@ -986,6 +989,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
info->scale = 0.0;
info->snapshot = false;
info->metric_expr = NULL;
+ info->metric_name = NULL;

list_for_each_entry_safe(term, h, head_terms, list) {
alias = pmu_find_alias(pmu, term);
@@ -1002,6 +1006,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
if (alias->per_pkg)
info->per_pkg = true;
info->metric_expr = alias->metric_expr;
+ info->metric_name = alias->metric_name;

list_del(&term->list);
free(term);
@@ -1097,6 +1102,7 @@ struct sevent {
char *str;
char *pmu;
char *metric_expr;
+ char *metric_name;
};

static int cmp_sevent(const void *a, const void *b)
@@ -1196,6 +1202,7 @@ void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
aliases[j].str = alias->str;
aliases[j].pmu = pmu->name;
aliases[j].metric_expr = alias->metric_expr;
+ aliases[j].metric_name = alias->metric_name;
j++;
}
if (pmu->selectable &&
@@ -1232,6 +1239,8 @@ void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
printf("]\n");
if (verbose) {
printf("%*sPerf: %s/%s/", 8, "", aliases[j].pmu, aliases[j].str);
+ if (aliases[j].metric_name)
+ printf(" MetricName: %s", aliases[j].metric_name);
if (aliases[j].metric_expr)
printf(" MetricExpr: %s", aliases[j].metric_expr);
putchar('\n');
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 27f078ccc594..3d4b703f5d89 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -32,6 +32,7 @@ struct perf_pmu {
struct perf_pmu_info {
const char *unit;
const char *metric_expr;
+ const char *metric_name;
double scale;
bool per_pkg;
bool snapshot;
@@ -52,6 +53,7 @@ struct perf_pmu_alias {
bool per_pkg;
bool snapshot;
char *metric_expr;
+ char *metric_name;
};

struct perf_pmu *perf_pmu__find(const char *name);
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index e8e76744fa32..6883f429c742 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -796,7 +796,9 @@ void perf_stat__print_shadow_stats(struct perf_evsel *evsel,

if (expr__parse(&ratio, &pctx, &p) == 0)
print_metric(ctxp, NULL, "%8.1f",
- out->force_header ? evsel->name : "",
+ evsel->metric_name ?
+ evsel->metric_name :
+ out->force_header ? evsel->name : "",
ratio);
else
print_metric(ctxp, NULL, NULL, "", 0);
--
2.9.3