From 3e21a28a01e227726456a51933ed004c2df31850 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Wed, 10 Jun 2020 16:58:22 -0700 Subject: perf expr: Add d_ratio operation d_ratio avoids division by 0 yielding infinity, such as when a counter doesn't get scheduled. An example usage is: { "BriefDescription": "DCache L1 misses", "MetricExpr": "d_ratio(MEM_LOAD_RETIRED.L1_MISS, MEM_LOAD_RETIRED.L1_HIT + MEM_LOAD_RETIRED.L1_MISS + MEM_LOAD_RETIRED.FB_HIT)", "MetricGroup": "DCache;DCache_L1", "MetricName": "DCache_L1_Miss", "ScaleUnit": "100%", } Signed-off-by: Ian Rogers Acked-by: Jiri Olsa Cc: Alexander Shishkin Cc: Andi Kleen Cc: Jin Yao Cc: John Garry Cc: Kajol Jain Cc: Mark Rutland Cc: Namhyung Kim Cc: Paul Clarke Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lore.kernel.org/lkml/20200610235823.52557-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/expr.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools/perf/tests/expr.c') diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index 1cb02ca2b15f..c4877b36ab58 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -39,6 +39,8 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) ret |= test(&ctx, "1+1 if 3*4 else 0", 2); ret |= test(&ctx, "1.1 + 2.1", 3.2); ret |= test(&ctx, ".1 + 2.", 2.1); + ret |= test(&ctx, "d_ratio(1, 2)", 0.5); + ret |= test(&ctx, "d_ratio(2.5, 0)", 0); if (ret) return ret; -- cgit v1.2.3 From ff1a12f962dff5b490c5ce1c2c4bcd0bf3bf517d Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Wed, 10 Jun 2020 16:58:23 -0700 Subject: perf expr: Add < and > operators These are broadly useful but required to handle TMA metrics. For example encoding Ports_Utilization from: https://download.01.org/perfmon/TMA_Metrics.csv requires '<'. { "BriefDescription": "This metric estimates fraction of cycles the CPU performance was potentially limited due to Core computation issues (non divider-related). Two distinct categories can be attributed into this metric: (1) heavy data-dependency among contiguous instructions would manifest in this metric - such cases are often referred to as low Instruction Level Parallelism (ILP). (2) Contention on some hardware execution unit other than Divider. For example; when there are too many multiply operations.", "MetricExpr": "( ( cpu@EXE_ACTIVITY.EXE_BOUND_0_PORTS@ + cpu@EXE_ACTIVITY.1_PORTS_UTIL@ + ( cpu@EXE_ACTIVITY.2_PORTS_UTIL@ * ( ( ( cpu@UOPS_RETIRED.RETIRE_SLOTS@ ) / ( cpu@CPU_CLK_UNHALTED.THREAD@ ) ) / ( ( 4.000000 ) + 1.000000 ) ) ) ) / ( cpu@CPU_CLK_UNHALTED.THREAD@ ) if ( cpu@ARITH.DIVIDER_ACTIVE\\,cmask\\=1@ < cpu@EXE_ACTIVITY.EXE_BOUND_0_PORTS@ ) else ( ( cpu@EXE_ACTIVITY.EXE_BOUND_0_PORTS@ + cpu@EXE_ACTIVITY.1_PORTS_UTIL@ + ( cpu@EXE_ACTIVITY.2_PORTS_UTIL@ * ( ( ( cpu@UOPS_RETIRED.RETIRE_SLOTS@ ) / ( cpu@CPU_CLK_UNHALTED.THREAD@ ) ) / ( ( 4.000000 ) + 1.000000 ) ) ) ) - cpu@EXE_ACTIVITY.EXE_BOUND_0_PORTS@ ) / ( cpu@CPU_CLK_UNHALTED.THREAD@ ) )", "MetricGroup": "Topdown_Group_Ports_Utilization", "MetricName": "Topdown_Metric_Ports_Utilization" }, Signed-off-by: Ian Rogers Acked-by: Jiri Olsa Cc: Alexander Shishkin Cc: Andi Kleen Cc: Jin Yao Cc: John Garry Cc: Kajol Jain Cc: Mark Rutland Cc: Namhyung Kim Cc: Paul Clarke Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lore.kernel.org/lkml/20200610235823.52557-2-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/expr.c | 6 ++++++ tools/perf/util/expr.l | 2 ++ tools/perf/util/expr.y | 5 ++++- 3 files changed, 12 insertions(+), 1 deletion(-) (limited to 'tools/perf/tests/expr.c') diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index c4877b36ab58..b7e5ef3007fc 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -41,6 +41,12 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) ret |= test(&ctx, ".1 + 2.", 2.1); ret |= test(&ctx, "d_ratio(1, 2)", 0.5); ret |= test(&ctx, "d_ratio(2.5, 0)", 0); + ret |= test(&ctx, "1.1 < 2.2", 1); + ret |= test(&ctx, "2.2 > 1.1", 1); + ret |= test(&ctx, "1.1 < 1.1", 0); + ret |= test(&ctx, "2.2 > 2.2", 0); + ret |= test(&ctx, "2.2 < 1.1", 0); + ret |= test(&ctx, "1.1 > 2.2", 0); if (ret) return ret; diff --git a/tools/perf/util/expr.l b/tools/perf/util/expr.l index 298d86660a96..13e5e3c75f56 100644 --- a/tools/perf/util/expr.l +++ b/tools/perf/util/expr.l @@ -111,6 +111,8 @@ else { return ELSE; } "|" { return '|'; } "^" { return '^'; } "&" { return '&'; } +"<" { return '<'; } +">" { return '>'; } "-" { return '-'; } "+" { return '+'; } "*" { return '*'; } diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y index fe145344bb39..5fcb98800f9c 100644 --- a/tools/perf/util/expr.y +++ b/tools/perf/util/expr.y @@ -41,6 +41,7 @@ static double d_ratio(double val0, double val1) %left '|' %left '^' %left '&' +%left '<' '>' %left '-' '+' %left '*' '/' '%' %left NEG NOT @@ -73,7 +74,7 @@ other: ID | MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ',' | -D_RATIO +'<' | '>' | D_RATIO all_expr: if_expr { *final_val = $1; } ; @@ -94,6 +95,8 @@ expr: NUMBER | expr '|' expr { $$ = (long)$1 | (long)$3; } | expr '&' expr { $$ = (long)$1 & (long)$3; } | expr '^' expr { $$ = (long)$1 ^ (long)$3; } + | expr '<' expr { $$ = $1 < $3; } + | expr '>' expr { $$ = $1 > $3; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } -- cgit v1.2.3 From 2c46f54249c6e86d8d81b5ea2452319f197caf17 Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Sun, 12 Jul 2020 15:26:17 +0200 Subject: perf metric: Rename expr__add_id() to expr__add_val() Rename expr__add_id() to expr__add_val() so we can use expr__add_id() to actually add just the id without any value in following changes. There's no functional change. Signed-off-by: Jiri Olsa Acked-by: Ian Rogers Cc: Alexander Shishkin Cc: Andi Kleen Cc: John Garry Cc: Kajol Jain Cc: Michael Petlan Cc: Namhyung Kim Cc: Paul Clarke Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lore.kernel.org/lkml/20200712132634.138901-2-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/expr.c | 4 ++-- tools/perf/tests/pmu-events.c | 4 ++-- tools/perf/util/expr.c | 2 +- tools/perf/util/expr.h | 2 +- tools/perf/util/expr.y | 2 +- tools/perf/util/stat-shadow.c | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) (limited to 'tools/perf/tests/expr.c') diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index b7e5ef3007fc..d13fc1dfd5ef 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -24,8 +24,8 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) struct expr_parse_ctx ctx; expr__ctx_init(&ctx); - expr__add_id(&ctx, strdup("FOO"), 1); - expr__add_id(&ctx, strdup("BAR"), 2); + expr__add_id_val(&ctx, strdup("FOO"), 1); + expr__add_id_val(&ctx, strdup("BAR"), 2); ret = test(&ctx, "1+1", 2); ret |= test(&ctx, "FOO+BAR", 3); diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index b66b021476ec..eb19f9a0bc15 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -492,7 +492,7 @@ static int test_parsing(void) */ k = 1; hashmap__for_each_entry((&ctx.ids), cur, bkt) - expr__add_id(&ctx, strdup(cur->key), k++); + expr__add_id_val(&ctx, strdup(cur->key), k++); hashmap__for_each_entry((&ctx.ids), cur, bkt) { if (check_parse_cpu(cur->key, map == cpus_map, @@ -547,7 +547,7 @@ static int metric_parse_fake(const char *str) */ i = 1; hashmap__for_each_entry((&ctx.ids), cur, bkt) - expr__add_id(&ctx, strdup(cur->key), i++); + expr__add_id_val(&ctx, strdup(cur->key), i++); hashmap__for_each_entry((&ctx.ids), cur, bkt) { if (check_parse_fake(cur->key)) { diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index e8f777830a23..9116a3a01eea 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -33,7 +33,7 @@ static bool key_equal(const void *key1, const void *key2, } /* Caller must make sure id is allocated */ -int expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val) +int expr__add_id_val(struct expr_parse_ctx *ctx, const char *name, double val) { double *val_ptr = NULL, *old_val = NULL; char *old_key = NULL; diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h index 8a2c1074f90f..bb6bac836b48 100644 --- a/tools/perf/util/expr.h +++ b/tools/perf/util/expr.h @@ -22,7 +22,7 @@ struct expr_scanner_ctx { void expr__ctx_init(struct expr_parse_ctx *ctx); void expr__ctx_clear(struct expr_parse_ctx *ctx); -int expr__add_id(struct expr_parse_ctx *ctx, const char *id, double val); +int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val); int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr); int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime); diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y index 5fcb98800f9c..b2b3420ea6ec 100644 --- a/tools/perf/util/expr.y +++ b/tools/perf/util/expr.y @@ -69,7 +69,7 @@ all_other: all_other other other: ID { - expr__add_id(ctx, $1, 0.0); + expr__add_id_val(ctx, $1, 0.0); } | MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ',' diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c index 8fdef47005e6..fc9ac4b4218e 100644 --- a/tools/perf/util/stat-shadow.c +++ b/tools/perf/util/stat-shadow.c @@ -773,9 +773,9 @@ static int prepare_metric(struct evsel **metric_events, *pn = 0; if (metric_total) - expr__add_id(pctx, n, metric_total); + expr__add_id_val(pctx, n, metric_total); else - expr__add_id(pctx, n, avg_stats(stats)*scale); + expr__add_id_val(pctx, n, avg_stats(stats)*scale); } return i; -- cgit v1.2.3 From 070b3b5ad7bd077e673cad2c591a2ecf49c0b58a Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Sun, 12 Jul 2020 15:26:18 +0200 Subject: perf metric: Add 'struct expr_id_data' to keep expr value Add 'struct expr_id_data' to keep an expr value instead of just a simple double pointer, so we can store more data for ID in the following changes. Signed-off-by: Jiri Olsa Acked-by: Ian Rogers Cc: Alexander Shishkin Cc: Andi Kleen Cc: John Garry Cc: Kajol Jain Cc: Michael Petlan Cc: Namhyung Kim Cc: Paul Clarke Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lore.kernel.org/lkml/20200712132634.138901-3-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/expr.c | 3 ++- tools/perf/util/expr.c | 22 +++++++++++----------- tools/perf/util/expr.h | 4 ++++ tools/perf/util/metricgroup.c | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) (limited to 'tools/perf/tests/expr.c') diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index d13fc1dfd5ef..4d01051951cd 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -18,8 +18,9 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2) int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) { + struct expr_id_data *val_ptr; const char *p; - double val, *val_ptr; + double val; int ret; struct expr_parse_ctx ctx; diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index 9116a3a01eea..5d05f9765ed8 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -33,32 +33,32 @@ static bool key_equal(const void *key1, const void *key2, } /* Caller must make sure id is allocated */ -int expr__add_id_val(struct expr_parse_ctx *ctx, const char *name, double val) +int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val) { - double *val_ptr = NULL, *old_val = NULL; + struct expr_id_data *data_ptr = NULL, *old_data = NULL; char *old_key = NULL; int ret; if (val != 0.0) { - val_ptr = malloc(sizeof(double)); - if (!val_ptr) + data_ptr = malloc(sizeof(*data_ptr)); + if (!data_ptr) return -ENOMEM; - *val_ptr = val; + data_ptr->val = val; } - ret = hashmap__set(&ctx->ids, name, val_ptr, - (const void **)&old_key, (void **)&old_val); + ret = hashmap__set(&ctx->ids, id, data_ptr, + (const void **)&old_key, (void **)&old_data); free(old_key); - free(old_val); + free(old_data); return ret; } int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr) { - double *data; + struct expr_id_data *data; if (!hashmap__find(&ctx->ids, id, (void **)&data)) return -1; - *val_ptr = (data == NULL) ? 0.0 : *data; + *val_ptr = (data == NULL) ? 0.0 : data->val; return 0; } @@ -119,7 +119,7 @@ int expr__parse(double *final_val, struct expr_parse_ctx *ctx, int expr__find_other(const char *expr, const char *one, struct expr_parse_ctx *ctx, int runtime) { - double *old_val = NULL; + struct expr_id_data *old_val = NULL; char *old_key = NULL; int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime); diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h index bb6bac836b48..21fe5bd85718 100644 --- a/tools/perf/util/expr.h +++ b/tools/perf/util/expr.h @@ -15,6 +15,10 @@ struct expr_parse_ctx { struct hashmap ids; }; +struct expr_id_data { + double val; +}; + struct expr_scanner_ctx { int start_token; int runtime; diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 82fecb5a302d..df0356ec120d 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -138,7 +138,7 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist, unsigned long *evlist_used) { struct evsel *ev, *current_leader = NULL; - double *val_ptr; + struct expr_id_data *val_ptr; int i = 0, matched_events = 0, events_to_match; const int idnum = (int)hashmap__size(&pctx->ids); -- cgit v1.2.3