perf expr: Make the evaluation of & and | logical and lazy
authorIan Rogers <irogers@google.com>
Fri, 19 May 2023 06:37:18 +0000 (23:37 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 5 Jun 2023 18:56:14 +0000 (15:56 -0300)
Currently the & and | operators are only used in metric thresholds like
(from the tma_retiring metric):

tma_retiring > 0.7 | tma_heavy_operations > 0.1

Thresholds are always computed when present, but a lack of events may
mean the threshold can't be computed. This happens with the option
--metric-no-threshold for say the metric tma_retiring on Tigerlake model
CPUs.

To fully compute the threshold tma_heavy_operations is needed and it
needs the extra events of IDQ.MS_UOPS, UOPS_DECODED.DEC0,
cpu/UOPS_DECODED.DEC0,cmask=1/ and IDQ.MITE_UOPS. So
--metric-no-threshold is a useful option to reduce the number of events
needed and potentially multiplexing of events.

Rather than just fail threshold computations like this, we may know a
result from just the left or right-hand side. So, for tma_retiring if
its value is "> 0.7" we know it is over the threshold. This allows the
metric to have the threshold coloring, when possible, without all the
counters being programmed.

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Kan Liang <kan.liang@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ahmad Yasin <ahmad.yasin@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Caleb Biggers <caleb.biggers@intel.com>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Edward Baker <edward.baker@intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Perry Taylor <perry.taylor@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Samantha Alt <samantha.alt@intel.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Link: https://lore.kernel.org/r/20230519063719.1029596-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/expr.c
tools/perf/util/expr.y

index 733ead1..3d01eb5 100644 (file)
@@ -185,6 +185,46 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
                        NULL, ctx) == 0);
        TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
 
+       /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+
+       /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+       expr__ctx_clear(ctx);
+       TEST_ASSERT_VAL("find ids",
+                       expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
+       TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
+
        /* Test toplogy constants appear well ordered. */
        expr__ctx_clear(ctx);
        TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
index 4ce931c..f04963e 100644 (file)
@@ -123,20 +123,6 @@ static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
  * 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);                                \
-               if (isnan(LHS.val) || isnan(RHS.val)) {                 \
-                       RESULT.val = NAN;                               \
-               } else {                                                \
-                       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);                                \
@@ -213,9 +199,75 @@ expr: NUMBER
 }
 | ID                           { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
 | SOURCE_COUNT '(' ID ')'      { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
-| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
-| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
-| expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
+| expr '|' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.ids = NULL;
+               $$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
+       } else if (is_const($1.val)) {
+               assert($1.ids == NULL);
+               if (fpclassify($1.val) == FP_ZERO) {
+                       $$ = $3;
+               } else {
+                       $$.val = 1;
+                       $$.ids = NULL;
+                       ids__free($3.ids);
+               }
+       } else if (is_const($3.val)) {
+               assert($3.ids == NULL);
+               if (fpclassify($3.val) == FP_ZERO) {
+                       $$ = $1;
+               } else {
+                       $$.val = 1;
+                       $$.ids = NULL;
+                       ids__free($1.ids);
+               }
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
+| expr '&' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
+               $$.ids = NULL;
+       } else if (is_const($1.val)) {
+               assert($1.ids == NULL);
+               if (fpclassify($1.val) != FP_ZERO) {
+                       $$ = $3;
+               } else {
+                       $$.val = 0;
+                       $$.ids = NULL;
+                       ids__free($3.ids);
+               }
+       } else if (is_const($3.val)) {
+               assert($3.ids == NULL);
+               if (fpclassify($3.val) != FP_ZERO) {
+                       $$ = $1;
+               } else {
+                       $$.val = 0;
+                       $$.ids = NULL;
+                       ids__free($1.ids);
+               }
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
+| expr '^' expr
+{
+       if (is_const($1.val) && is_const($3.val)) {
+               assert($1.ids == NULL);
+               assert($3.ids == NULL);
+               $$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
+               $$.ids = NULL;
+       } else {
+               $$ = union_expr($1, $3);
+       }
+}
 | expr '<' expr { BINARY_OP($$, <, $1, $3); }
 | expr '>' expr { BINARY_OP($$, >, $1, $3); }
 | expr '+' expr { BINARY_OP($$, +, $1, $3); }