Skip to content

Commit 4a4a9bf

Browse files
captain5050namhyung
authored andcommitted
perf expr: Add has_event function
Some events are dependent on firmware/kernel enablement. Allow such events to be detected when the metric is parsed so that the metric's event parsing doesn't fail. Signed-off-by: Ian Rogers <[email protected]> Tested-by: Namhyung Kim <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: Sohom Datta <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Caleb Biggers <[email protected]> Cc: Edward Baker <[email protected]> Cc: Perry Taylor <[email protected]> Cc: Samantha Alt <[email protected]> Cc: Weilin Wang <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Jing Zhang <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Kan Liang <[email protected]> Cc: Zhengjun Xing <[email protected]> Cc: John Garry <[email protected]> Cc: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 36cee69 commit 4a4a9bf

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

tools/perf/tests/expr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
254254
TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
255255
TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
256256

257+
/* has_event returns 1 when an event exists. */
258+
expr__add_id_val(ctx, strdup("cycles"), 2);
259+
ret = test(ctx, "has_event(cycles)", 1);
260+
257261
expr__ctx_free(ctx);
258262

259263
return 0;

tools/perf/util/expr.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cpumap.h"
99
#include "cputopo.h"
1010
#include "debug.h"
11+
#include "evlist.h"
1112
#include "expr.h"
1213
#include "expr-bison.h"
1314
#include "expr-flex.h"
@@ -474,3 +475,23 @@ double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx
474475
pr_debug2("literal: %s = %f\n", literal, result);
475476
return result;
476477
}
478+
479+
/* Does the event 'id' parse? Determine via ctx->ids if possible. */
480+
double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
481+
{
482+
struct evlist *tmp;
483+
double ret;
484+
485+
if (hashmap__find(ctx->ids, id, /*value=*/NULL))
486+
return 1.0;
487+
488+
if (!compute_ids)
489+
return 0.0;
490+
491+
tmp = evlist__new();
492+
if (!tmp)
493+
return NAN;
494+
ret = parse_event(tmp, id) ? 0 : 1;
495+
evlist__delete(tmp);
496+
return ret;
497+
}

tools/perf/util/expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ int expr__find_ids(const char *expr, const char *one,
5454
double expr_id_data__value(const struct expr_id_data *data);
5555
double expr_id_data__source_count(const struct expr_id_data *data);
5656
double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx);
57+
double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id);
5758

5859
#endif

tools/perf/util/expr.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ min { return MIN; }
113113
if { return IF; }
114114
else { return ELSE; }
115115
source_count { return SOURCE_COUNT; }
116+
has_event { return HAS_EVENT; }
116117
{literal} { return literal(yyscanner, sctx); }
117118
{number} { return value(yyscanner); }
118119
{symbol} { return str(yyscanner, ID, sctx->runtime); }

tools/perf/util/expr.y

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
} ids;
3838
}
3939

40-
%token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT EXPR_ERROR
40+
%token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT EXPR_ERROR
4141
%left MIN MAX IF
4242
%left '|'
4343
%left '^'
@@ -199,6 +199,12 @@ expr: NUMBER
199199
}
200200
| ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
201201
| SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
202+
| HAS_EVENT '(' ID ')'
203+
{
204+
$$.val = expr__has_event(ctx, compute_ids, $3);
205+
$$.ids = NULL;
206+
free($3);
207+
}
202208
| expr '|' expr
203209
{
204210
if (is_const($1.val) && is_const($3.val)) {

0 commit comments

Comments
 (0)