Skip to content

Commit 1e1a873

Browse files
kjain101acmel
authored andcommitted
perf metricgroups: Enhance JSON/metric infrastructure to handle "?"
Patch enhances current metric infrastructure to handle "?" in the metric expression. The "?" can be use for parameters whose value not known while creating metric events and which can be replace later at runtime to the proper value. It also add flexibility to create multiple events out of single metric event added in JSON file. Patch adds function 'arch_get_runtimeparam' which is a arch specific function, returns the count of metric events need to be created. By default it return 1. This infrastructure needed for hv_24x7 socket/chip level events. "hv_24x7" chip level events needs specific chip-id to which the data is requested. Function 'arch_get_runtimeparam' implemented in header.c which extract number of sockets from sysfs file "sockets" under "/sys/devices/hv_24x7/interface/". With this patch basically we are trying to create as many metric events as define by runtime_param. For that one loop is added in function 'metricgroup__add_metric', which create multiple events at run time depend on return value of 'arch_get_runtimeparam' and merge that event in 'group_list'. To achieve that we are actually passing this parameter value as part of `expr__find_other` function and changing "?" present in metric expression with this value. As in our JSON file, there gonna be single metric event, and out of which we are creating multiple events. To understand which data count belongs to which parameter value, we also printing param value in generic_metric function. For example, command:# ./perf stat -M PowerBUS_Frequency -C 0 -I 1000 1.000101867 9,356,933 hv_24x7/pm_pb_cyc,chip=0/ # 2.3 GHz PowerBUS_Frequency_0 1.000101867 9,366,134 hv_24x7/pm_pb_cyc,chip=1/ # 2.3 GHz PowerBUS_Frequency_1 2.000314878 9,365,868 hv_24x7/pm_pb_cyc,chip=0/ # 2.3 GHz PowerBUS_Frequency_0 2.000314878 9,366,092 hv_24x7/pm_pb_cyc,chip=1/ # 2.3 GHz PowerBUS_Frequency_1 So, here _0 and _1 after PowerBUS_Frequency specify parameter value. Signed-off-by: Kajol Jain <kjain@linux.ibm.com> Acked-by: Jiri Olsa <jolsa@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Anju T Sudhakar <anju@linux.vnet.ibm.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Jin Yao <yao.jin@linux.intel.com> Cc: Joe Mario <jmario@redhat.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Madhavan Srinivasan <maddy@linux.vnet.ibm.com> Cc: Mamatha Inamdar <mamatha4@linux.vnet.ibm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michael Petlan <mpetlan@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@ozlabs.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com> Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linuxppc-dev@lists.ozlabs.org Link: http://lore.kernel.org/lkml/20200401203340.31402-5-kjain@linux.ibm.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 454a8be commit 1e1a873

8 files changed

Lines changed: 79 additions & 27 deletions

File tree

‎tools/perf/arch/powerpc/util/header.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <string.h>
88
#include <linux/stringify.h>
99
#include "header.h"
10+
#include "metricgroup.h"
11+
#include <api/fs/fs.h>
1012

1113
#define mfspr(rn) ({unsigned long rval; \
1214
asm volatile("mfspr %0," __stringify(rn) \
@@ -44,3 +46,9 @@ get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
4446

4547
return bufp;
4648
}
49+
50+
int arch_get_runtimeparam(void)
51+
{
52+
int count;
53+
return sysfs__read_int("/devices/hv_24x7/interface/sockets", &count) < 0 ? 1 : count;
54+
}

‎tools/perf/tests/expr.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
1010
{
1111
double val;
1212

13-
if (expr__parse(&val, ctx, e))
13+
if (expr__parse(&val, ctx, e, 1))
1414
TEST_ASSERT_VAL("parse test failed", 0);
1515
TEST_ASSERT_VAL("unexpected value", val == val2);
1616
return 0;
@@ -44,15 +44,15 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
4444
return ret;
4545

4646
p = "FOO/0";
47-
ret = expr__parse(&val, &ctx, p);
47+
ret = expr__parse(&val, &ctx, p, 1);
4848
TEST_ASSERT_VAL("division by zero", ret == -1);
4949

5050
p = "BAR/";
51-
ret = expr__parse(&val, &ctx, p);
51+
ret = expr__parse(&val, &ctx, p, 1);
5252
TEST_ASSERT_VAL("missing operand", ret == -1);
5353

5454
TEST_ASSERT_VAL("find other",
55-
expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other) == 0);
55+
expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other, 1) == 0);
5656
TEST_ASSERT_VAL("find other", num_other == 3);
5757
TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR"));
5858
TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ"));

‎tools/perf/util/expr.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ void expr__ctx_init(struct expr_parse_ctx *ctx)
2727

2828
static int
2929
__expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
30-
int start)
30+
int start, int runtime)
3131
{
3232
struct expr_scanner_ctx scanner_ctx = {
3333
.start_token = start,
34+
.runtime = runtime,
3435
};
3536
YY_BUFFER_STATE buffer;
3637
void *scanner;
@@ -54,9 +55,9 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
5455
return ret;
5556
}
5657

57-
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr)
58+
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime)
5859
{
59-
return __expr__parse(final_val, ctx, expr, EXPR_PARSE) ? -1 : 0;
60+
return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0;
6061
}
6162

6263
static bool
@@ -74,13 +75,13 @@ already_seen(const char *val, const char *one, const char **other,
7475
}
7576

7677
int expr__find_other(const char *expr, const char *one, const char ***other,
77-
int *num_other)
78+
int *num_other, int runtime)
7879
{
7980
int err, i = 0, j = 0;
8081
struct expr_parse_ctx ctx;
8182

8283
expr__ctx_init(&ctx);
83-
err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER);
84+
err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER, runtime);
8485
if (err)
8586
return -1;
8687

‎tools/perf/util/expr.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ struct expr_parse_ctx {
1717

1818
struct expr_scanner_ctx {
1919
int start_token;
20+
int runtime;
2021
};
2122

2223
void expr__ctx_init(struct expr_parse_ctx *ctx);
2324
void expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val);
24-
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr);
25+
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime);
2526
int expr__find_other(const char *expr, const char *one, const char ***other,
26-
int *num_other);
27+
int *num_other, int runtime);
2728

2829
#endif

‎tools/perf/util/expr.l‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int value(yyscan_t scanner, int base)
3535
* Allow @ instead of / to be able to specify pmu/event/ without
3636
* conflicts with normal division.
3737
*/
38-
static char *normalize(char *str)
38+
static char *normalize(char *str, int runtime)
3939
{
4040
char *ret = str;
4141
char *dst = str;
@@ -45,6 +45,19 @@ static char *normalize(char *str)
4545
*dst++ = '/';
4646
else if (*str == '\\')
4747
*dst++ = *++str;
48+
else if (*str == '?') {
49+
char *paramval;
50+
int i = 0;
51+
int size = asprintf(&paramval, "%d", runtime);
52+
53+
if (size < 0)
54+
*dst++ = '0';
55+
else {
56+
while (i < size)
57+
*dst++ = paramval[i++];
58+
free(paramval);
59+
}
60+
}
4861
else
4962
*dst++ = *str;
5063
str++;
@@ -54,16 +67,16 @@ static char *normalize(char *str)
5467
return ret;
5568
}
5669

57-
static int str(yyscan_t scanner, int token)
70+
static int str(yyscan_t scanner, int token, int runtime)
5871
{
5972
YYSTYPE *yylval = expr_get_lval(scanner);
6073
char *text = expr_get_text(scanner);
6174

62-
yylval->str = normalize(strdup(text));
75+
yylval->str = normalize(strdup(text), runtime);
6376
if (!yylval->str)
6477
return EXPR_ERROR;
6578

66-
yylval->str = normalize(yylval->str);
79+
yylval->str = normalize(yylval->str, runtime);
6780
return token;
6881
}
6982
%}
@@ -72,8 +85,8 @@ number [0-9]+
7285

7386
sch [-,=]
7487
spec \\{sch}
75-
sym [0-9a-zA-Z_\.:@]+
76-
symbol {spec}*{sym}*{spec}*{sym}*
88+
sym [0-9a-zA-Z_\.:@?]+
89+
symbol {spec}*{sym}*{spec}*{sym}*{spec}*{sym}
7790

7891
%%
7992
struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
@@ -93,7 +106,7 @@ if { return IF; }
93106
else { return ELSE; }
94107
#smt_on { return SMT_ON; }
95108
{number} { return value(yyscanner, 10); }
96-
{symbol} { return str(yyscanner, ID); }
109+
{symbol} { return str(yyscanner, ID, sctx->runtime); }
97110
"|" { return '|'; }
98111
"^" { return '^'; }
99112
"&" { return '&'; }

‎tools/perf/util/metricgroup.c‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct egroup {
9090
const char *metric_name;
9191
const char *metric_expr;
9292
const char *metric_unit;
93+
int runtime;
9394
};
9495

9596
static struct evsel *find_evsel_group(struct evlist *perf_evlist,
@@ -202,6 +203,7 @@ static int metricgroup__setup_events(struct list_head *groups,
202203
expr->metric_name = eg->metric_name;
203204
expr->metric_unit = eg->metric_unit;
204205
expr->metric_events = metric_events;
206+
expr->runtime = eg->runtime;
205207
list_add(&expr->nd, &me->head);
206208
}
207209

@@ -485,15 +487,20 @@ static bool metricgroup__has_constraint(struct pmu_event *pe)
485487
return false;
486488
}
487489

490+
int __weak arch_get_runtimeparam(void)
491+
{
492+
return 1;
493+
}
494+
488495
static int __metricgroup__add_metric(struct strbuf *events,
489-
struct list_head *group_list, struct pmu_event *pe)
496+
struct list_head *group_list, struct pmu_event *pe, int runtime)
490497
{
491498

492499
const char **ids;
493500
int idnum;
494501
struct egroup *eg;
495502

496-
if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum) < 0)
503+
if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum, runtime) < 0)
497504
return -EINVAL;
498505

499506
if (events->len > 0)
@@ -513,6 +520,7 @@ static int __metricgroup__add_metric(struct strbuf *events,
513520
eg->metric_name = pe->metric_name;
514521
eg->metric_expr = pe->metric_expr;
515522
eg->metric_unit = pe->unit;
523+
eg->runtime = runtime;
516524
list_add_tail(&eg->nd, group_list);
517525

518526
return 0;
@@ -540,7 +548,21 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,
540548

541549
pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
542550

543-
ret = __metricgroup__add_metric(events, group_list, pe);
551+
if (!strstr(pe->metric_expr, "?")) {
552+
ret = __metricgroup__add_metric(events, group_list, pe, 1);
553+
} else {
554+
int j, count;
555+
556+
count = arch_get_runtimeparam();
557+
558+
/* This loop is added to create multiple
559+
* events depend on count value and add
560+
* those events to group_list.
561+
*/
562+
563+
for (j = 0; j < count; j++)
564+
ret = __metricgroup__add_metric(events, group_list, pe, j);
565+
}
544566
if (ret == -ENOMEM)
545567
break;
546568
}

‎tools/perf/util/metricgroup.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct metric_expr {
2222
const char *metric_name;
2323
const char *metric_unit;
2424
struct evsel **metric_events;
25+
int runtime;
2526
};
2627

2728
struct metric_event *metricgroup__lookup(struct rblist *metric_events,
@@ -34,4 +35,5 @@ int metricgroup__parse_groups(const struct option *opt,
3435
void metricgroup__print(bool metrics, bool groups, char *filter,
3536
bool raw, bool details);
3637
bool metricgroup__has_metric(const char *metric);
38+
int arch_get_runtimeparam(void);
3739
#endif

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void perf_stat__collect_metric_expr(struct evlist *evsel_list)
336336
metric_events = counter->metric_events;
337337
if (!metric_events) {
338338
if (expr__find_other(counter->metric_expr, counter->name,
339-
&metric_names, &num_metric_names) < 0)
339+
&metric_names, &num_metric_names, 1) < 0)
340340
continue;
341341

342342
metric_events = calloc(sizeof(struct evsel *),
@@ -723,6 +723,7 @@ static void generic_metric(struct perf_stat_config *config,
723723
char *name,
724724
const char *metric_name,
725725
const char *metric_unit,
726+
int runtime,
726727
double avg,
727728
int cpu,
728729
struct perf_stat_output_ctx *out,
@@ -777,7 +778,7 @@ static void generic_metric(struct perf_stat_config *config,
777778
}
778779

779780
if (!metric_events[i]) {
780-
if (expr__parse(&ratio, &pctx, metric_expr) == 0) {
781+
if (expr__parse(&ratio, &pctx, metric_expr, runtime) == 0) {
781782
char *unit;
782783
char metric_bf[64];
783784

@@ -786,9 +787,13 @@ static void generic_metric(struct perf_stat_config *config,
786787
&unit, &scale) >= 0) {
787788
ratio *= scale;
788789
}
789-
790-
scnprintf(metric_bf, sizeof(metric_bf),
790+
if (strstr(metric_expr, "?"))
791+
scnprintf(metric_bf, sizeof(metric_bf),
792+
"%s %s_%d", unit, metric_name, runtime);
793+
else
794+
scnprintf(metric_bf, sizeof(metric_bf),
791795
"%s %s", unit, metric_name);
796+
792797
print_metric(config, ctxp, NULL, "%8.1f",
793798
metric_bf, ratio);
794799
} else {
@@ -1022,7 +1027,7 @@ void perf_stat__print_shadow_stats(struct perf_stat_config *config,
10221027
print_metric(config, ctxp, NULL, NULL, name, 0);
10231028
} else if (evsel->metric_expr) {
10241029
generic_metric(config, evsel->metric_expr, evsel->metric_events, evsel->name,
1025-
evsel->metric_name, NULL, avg, cpu, out, st);
1030+
evsel->metric_name, NULL, 1, avg, cpu, out, st);
10261031
} else if (runtime_stat_n(st, STAT_NSECS, 0, cpu) != 0) {
10271032
char unit = 'M';
10281033
char unit_buf[10];
@@ -1051,7 +1056,7 @@ void perf_stat__print_shadow_stats(struct perf_stat_config *config,
10511056
out->new_line(config, ctxp);
10521057
generic_metric(config, mexp->metric_expr, mexp->metric_events,
10531058
evsel->name, mexp->metric_name,
1054-
mexp->metric_unit, avg, cpu, out, st);
1059+
mexp->metric_unit, mexp->runtime, avg, cpu, out, st);
10551060
}
10561061
}
10571062
if (num == 0)

0 commit comments

Comments
 (0)