linux/tools/perf/util/expr.y

265 lines
5.7 KiB
Plaintext
Raw Normal View History

/* Simple expression parser */
%{
#define YYDEBUG 1
#include <assert.h>
#include <math.h>
#include "util/debug.h"
#include "smt.h"
#define IN_EXPR_Y 1
#include "expr.h"
%}
%define api.pure full
%parse-param { double *final_val }
%parse-param { struct expr_parse_ctx *ctx }
%parse-param { bool compute_ids }
%parse-param {void *scanner}
%lex-param {void* scanner}
%union {
double num;
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
%left MIN MAX IF
%left '|'
%left '^'
%left '&'
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 <irogers@google.com> Acked-by: Jiri Olsa <jolsa@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Jin Yao <yao.jin@linux.intel.com> Cc: John Garry <john.garry@huawei.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: Stephane Eranian <eranian@google.com> Link: http://lore.kernel.org/lkml/20200610235823.52557-2-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-06-11 07:58:23 +08:00
%left '<' '>'
%left '-' '+'
%left '*' '/' '%'
%left NEG NOT
%type <num> NUMBER
%type <str> ID
%destructor { free ($$); } <str>
%type <ids> expr if_expr
%destructor { ids__free($$.ids); } <ids>
%{
static void expr_error(double *final_val __maybe_unused,
struct expr_parse_ctx *ctx __maybe_unused,
bool compute_ids __maybe_unused,
void *scanner,
const char *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
/* During computing ids, does val represent a constant (non-BOTTOM) value? */
static bool is_const(double val)
{
return isfinite(val);
}
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;
}
/*
* If we're not computing ids or $1 and $3 are constants, compute the new
* constant value using OP. Its invariant that there are no ids. If computing
* ids for non-constants union the set of IDs that must be computed.
*/
#define BINARY_LONG_OP(RESULT, OP, LHS, RHS) \
if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
assert(LHS.ids == NULL); \
assert(RHS.ids == NULL); \
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) \
if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
assert(LHS.ids == NULL); \
assert(RHS.ids == NULL); \
RESULT.val = LHS.val OP RHS.val; \
RESULT.ids = NULL; \
} else { \
RESULT = union_expr(LHS, RHS); \
}
%}
%%
start: if_expr
{
if (compute_ids)
ctx->ids = ids__union($1.ids, ctx->ids);
if (final_val)
*final_val = $1.val;
}
;
if_expr: expr IF expr ELSE expr
{
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: NUMBER
{
$$.val = $1;
$$.ids = NULL;
}
| ID
{
if (!compute_ids) {
/*
* Compute the event's value from ID. If the ID isn't known then
* it isn't used to compute the formula so set to NAN.
*/
struct expr_id_data *data;
$$.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_OP($$, <, $1, $3); }
| expr '>' expr { BINARY_OP($$, >, $1, $3); }
| expr '+' expr { BINARY_OP($$, +, $1, $3); }
| expr '-' expr { BINARY_OP($$, -, $1, $3); }
| expr '*' expr { BINARY_OP($$, *, $1, $3); }
| expr '/' expr
{
if (fpclassify($3.val) == FP_ZERO) {
pr_debug("division by zero\n");
YYABORT;
} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
assert($1.ids == NULL);
assert($3.ids == NULL);
$$.val = $1.val / $3.val;
$$.ids = NULL;
} else {
/* LHS and/or RHS need computing from event IDs so union. */
$$ = union_expr($1, $3);
}
}
| expr '%' expr
{
if (fpclassify($3.val) == FP_ZERO) {
pr_debug("division by zero\n");
YYABORT;
} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
assert($1.ids == NULL);
assert($3.ids == NULL);
$$.val = (long)$1.val % (long)$3.val;
$$.ids = NULL;
} else {
/* LHS and/or RHS need computing from event IDs so union. */
$$ = union_expr($1, $3);
}
}
| D_RATIO '(' expr ',' expr ')'
{
if (fpclassify($5.val) == FP_ZERO) {
/*
* Division by constant zero always yields zero and no events
* are necessary.
*/
assert($5.ids == NULL);
$$.val = 0.0;
$$.ids = NULL;
ids__free($3.ids);
} else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
assert($3.ids == NULL);
assert($5.ids == NULL);
$$.val = $3.val / $5.val;
$$.ids = NULL;
} else {
/* LHS and/or RHS need computing from event IDs so union. */
$$ = union_expr($3, $5);
}
}
| '-' expr %prec NEG
{
$$.val = -$2.val;
$$.ids = $2.ids;
}
| '(' if_expr ')'
{
$$ = $2;
}
| MIN '(' expr ',' expr ')'
{
if (!compute_ids) {
$$.val = $3.val < $5.val ? $3.val : $5.val;
$$.ids = NULL;
} else {
$$ = union_expr($3, $5);
}
}
| MAX '(' expr ',' expr ')'
{
if (!compute_ids) {
$$.val = $3.val > $5.val ? $3.val : $5.val;
$$.ids = NULL;
} else {
$$ = union_expr($3, $5);
}
}
| SMT_ON
{
$$.val = smt_on() > 0 ? 1.0 : 0.0;
$$.ids = NULL;
}
;
%%