Commit 3f965a7d authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf expr: Merge find_ids and regular parsing

Add a new option to parsing that the set of IDs being used should be
computed, this means every action needs to handle the compute_ids and
regular case. This means actions yield a new ids type is a set of ids or
the value being computed. Use the compute_ids case to replace find IDs
parsing.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Tested-by: default avatarJohn Garry <john.garry@huawei.com>
Acked-by: default avatarJiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Clarke <pc@us.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sandeep Dasgupta <sdasgup@google.com>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20210923074616.674826-11-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 762a05c5
...@@ -314,10 +314,9 @@ void expr__ctx_free(struct expr_parse_ctx *ctx) ...@@ -314,10 +314,9 @@ void expr__ctx_free(struct expr_parse_ctx *ctx)
static int static int
__expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
int start, int runtime) bool compute_ids, int runtime)
{ {
struct expr_scanner_ctx scanner_ctx = { struct expr_scanner_ctx scanner_ctx = {
.start_token = start,
.runtime = runtime, .runtime = runtime,
}; };
YY_BUFFER_STATE buffer; YY_BUFFER_STATE buffer;
...@@ -337,7 +336,7 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, ...@@ -337,7 +336,7 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
expr_set_debug(1, scanner); expr_set_debug(1, scanner);
#endif #endif
ret = expr_parse(val, ctx, scanner); ret = expr_parse(val, ctx, compute_ids, scanner);
expr__flush_buffer(buffer, scanner); expr__flush_buffer(buffer, scanner);
expr__delete_buffer(buffer, scanner); expr__delete_buffer(buffer, scanner);
...@@ -348,13 +347,13 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, ...@@ -348,13 +347,13 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
int expr__parse(double *final_val, struct expr_parse_ctx *ctx, int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
const char *expr, int runtime) const char *expr, int runtime)
{ {
return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0; return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false, runtime) ? -1 : 0;
} }
int expr__find_ids(const char *expr, const char *one, int expr__find_ids(const char *expr, const char *one,
struct expr_parse_ctx *ctx, int runtime) struct expr_parse_ctx *ctx, int runtime)
{ {
int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime); int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true, runtime);
if (one) if (one)
expr__del_id(ctx, one); expr__del_id(ctx, one);
......
...@@ -26,7 +26,6 @@ struct expr_parse_ctx { ...@@ -26,7 +26,6 @@ struct expr_parse_ctx {
struct expr_id_data; struct expr_id_data;
struct expr_scanner_ctx { struct expr_scanner_ctx {
int start_token;
int runtime; int runtime;
}; };
......
...@@ -91,15 +91,6 @@ symbol ({spec}|{sym})+ ...@@ -91,15 +91,6 @@ symbol ({spec}|{sym})+
%% %%
struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner); struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
{
int start_token = sctx->start_token;
if (sctx->start_token) {
sctx->start_token = 0;
return start_token;
}
}
d_ratio { return D_RATIO; } d_ratio { return D_RATIO; }
max { return MAX; } max { return MAX; }
min { return MIN; } min { return MIN; }
......
/* Simple expression parser */ /* Simple expression parser */
%{ %{
#define YYDEBUG 1 #define YYDEBUG 1
#include <assert.h>
#include <math.h> #include <math.h>
#include "util/debug.h" #include "util/debug.h"
#include "smt.h" #include "smt.h"
...@@ -12,15 +13,31 @@ ...@@ -12,15 +13,31 @@
%parse-param { double *final_val } %parse-param { double *final_val }
%parse-param { struct expr_parse_ctx *ctx } %parse-param { struct expr_parse_ctx *ctx }
%parse-param { bool compute_ids }
%parse-param {void *scanner} %parse-param {void *scanner}
%lex-param {void* scanner} %lex-param {void* scanner}
%union { %union {
double num; double num;
char *str; char *str;
struct ids {
/*
* When creating ids, holds the working set of event ids. NULL
* implies the set is empty.
*/
struct hashmap *ids;
/*
* The metric value. When not creating ids this is the value
* read from a counter, a constant or some computed value. When
* creating ids the value is either a constant or BOTTOM. NAN is
* used as the special BOTTOM value, representing a "set of all
* values" case.
*/
double val;
} ids;
} }
%token ID NUMBER MIN MAX IF ELSE SMT_ON D_RATIO EXPR_ERROR EXPR_PARSE EXPR_OTHER %token ID NUMBER MIN MAX IF ELSE SMT_ON D_RATIO EXPR_ERROR
%left MIN MAX IF %left MIN MAX IF
%left '|' %left '|'
%left '^' %left '^'
...@@ -32,65 +49,109 @@ ...@@ -32,65 +49,109 @@
%type <num> NUMBER %type <num> NUMBER
%type <str> ID %type <str> ID
%destructor { free ($$); } <str> %destructor { free ($$); } <str>
%type <num> expr if_expr %type <ids> expr if_expr
%destructor { ids__free($$.ids); } <ids>
%{ %{
static void expr_error(double *final_val __maybe_unused, static void expr_error(double *final_val __maybe_unused,
struct expr_parse_ctx *ctx __maybe_unused, struct expr_parse_ctx *ctx __maybe_unused,
bool compute_ids __maybe_unused,
void *scanner, void *scanner,
const char *s) const char *s)
{ {
pr_debug("%s\n", s); pr_debug("%s\n", s);
} }
/*
* During compute ids, the special "bottom" value uses NAN to represent the set
* of all values. NAN is selected as it isn't a useful constant value.
*/
#define BOTTOM NAN
static struct ids union_expr(struct ids ids1, struct ids ids2)
{
struct ids result = {
.val = BOTTOM,
.ids = ids__union(ids1.ids, ids2.ids),
};
return result;
}
#define BINARY_LONG_OP(RESULT, OP, LHS, RHS) \ #define BINARY_LONG_OP(RESULT, OP, LHS, RHS) \
RESULT = (long)LHS OP (long)RHS; if (!compute_ids) { \
RESULT.val = (long)LHS.val OP (long)RHS.val; \
RESULT.ids = NULL; \
} else { \
RESULT = union_expr(LHS, RHS); \
}
#define BINARY_OP(RESULT, OP, LHS, RHS) \ #define BINARY_OP(RESULT, OP, LHS, RHS) \
RESULT = LHS OP RHS; if (!compute_ids) { \
RESULT.val = LHS.val OP RHS.val; \
RESULT.ids = NULL; \
} else { \
RESULT = union_expr(LHS, RHS); \
}
%} %}
%% %%
start: start: if_expr
EXPR_PARSE all_expr
|
EXPR_OTHER all_other
all_other: all_other other
|
other: ID
{ {
expr__add_id(ctx, $1); if (compute_ids)
} ctx->ids = ids__union($1.ids, ctx->ids);
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
|
'<' | '>' | D_RATIO
all_expr: if_expr { *final_val = $1; } if (final_val)
*final_val = $1.val;
}
;
if_expr: expr IF expr ELSE expr if_expr: expr IF expr ELSE expr
{ {
$$ = $3 ? $1 : $5; if (!compute_ids) {
$$.ids = NULL;
if (fpclassify($3.val) == FP_ZERO) {
$$.val = $5.val;
} else {
$$.val = $1.val;
}
} else {
$$ = union_expr($1, union_expr($3, $5));
}
} }
| expr | expr
; ;
expr: NUMBER expr: NUMBER
{ {
$$ = $1; $$.val = $1;
$$.ids = NULL;
} }
| ID | ID
{ {
struct expr_id_data *data; if (!compute_ids) {
/*
$$ = NAN; * Compute the event's value from ID. If the ID isn't known then
if (expr__resolve_id(ctx, $1, &data) == 0) * it isn't used to compute the formula so set to NAN.
$$ = expr_id_data__value(data); */
struct expr_id_data *data;
free($1);
$$.val = NAN;
if (expr__resolve_id(ctx, $1, &data) == 0)
$$.val = expr_id_data__value(data);
$$.ids = NULL;
free($1);
} else {
/*
* Set the value to BOTTOM to show that any value is possible
* when the event is computed. Create a set of just the ID.
*/
$$.val = BOTTOM;
$$.ids = ids__new();
if (!$$.ids || ids__insert($$.ids, $1, ctx->parent))
YYABORT;
}
} }
| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); } | expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); } | expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
...@@ -102,31 +163,47 @@ expr: NUMBER ...@@ -102,31 +163,47 @@ expr: NUMBER
| expr '*' expr { BINARY_OP($$, *, $1, $3); } | expr '*' expr { BINARY_OP($$, *, $1, $3); }
| expr '/' expr | expr '/' expr
{ {
if ($3 == 0) { if (!compute_ids) {
pr_debug("division by zero\n"); if (fpclassify($3.val) == FP_ZERO) {
YYABORT; pr_debug("division by zero\n");
YYABORT;
}
$$.val = $1.val / $3.val;
$$.ids = NULL;
} else {
$$ = union_expr($1, $3);
} }
$$ = $1 / $3;
} }
| expr '%' expr | expr '%' expr
{ {
if ((long)$3 == 0) { if (!compute_ids) {
pr_debug("division by zero\n"); if (fpclassify($3.val) == FP_ZERO) {
YYABORT; pr_debug("division by zero\n");
YYABORT;
}
$$.val = (long)$1.val % (long)$3.val;
$$.ids = NULL;
} else {
$$ = union_expr($1, $3);
} }
$$ = (long)$1 % (long)$3;
} }
| D_RATIO '(' expr ',' expr ')' | D_RATIO '(' expr ',' expr ')'
{ {
if ($5 == 0) { if (!compute_ids) {
$$ = 0; $$.ids = NULL;
if (fpclassify($5.val) == FP_ZERO) {
$$.val = 0.0;
} else {
$$.val = $3.val / $5.val;
}
} else { } else {
$$ = $3 / $5; $$ = union_expr($3, $5);
} }
} }
| '-' expr %prec NEG | '-' expr %prec NEG
{ {
$$ = -$2; $$.val = -$2.val;
$$.ids = $2.ids;
} }
| '(' if_expr ')' | '(' if_expr ')'
{ {
...@@ -134,15 +211,26 @@ expr: NUMBER ...@@ -134,15 +211,26 @@ expr: NUMBER
} }
| MIN '(' expr ',' expr ')' | MIN '(' expr ',' expr ')'
{ {
$$ = $3 < $5 ? $3 : $5; if (!compute_ids) {
$$.val = $3.val < $5.val ? $3.val : $5.val;
$$.ids = NULL;
} else {
$$ = union_expr($3, $5);
}
} }
| MAX '(' expr ',' expr ')' | MAX '(' expr ',' expr ')'
{ {
$$ = $3 > $5 ? $3 : $5; if (!compute_ids) {
$$.val = $3.val > $5.val ? $3.val : $5.val;
$$.ids = NULL;
} else {
$$ = union_expr($3, $5);
}
} }
| SMT_ON | SMT_ON
{ {
$$ = smt_on() > 0 ? 1.0 : 0.0; $$.val = smt_on() > 0 ? 1.0 : 0.0;
$$.ids = NULL;
} }
; ;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment