perf metric: Avoid events for an 'if' constant result
authorIan Rogers <irogers@google.com>
Thu, 23 Sep 2021 07:46:16 +0000 (00:46 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 29 Sep 2021 16:51:04 +0000 (13:51 -0300)
For a metric like:

  CONST if expr else CONST

if the values of CONST are identical then expr doesn't need evaluating,
and events, in order to compute a result.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: John Garry <john.garry@huawei.com>
Acked-by: Jiri 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-14-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/expr.c
tools/perf/util/expr.y

index 2879893..f1d8411 100644 (file)
@@ -143,6 +143,13 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
                                                  smt_on() ? "EVENT1" : "EVENT2",
                                                  (void **)&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.0 if EVENT1 > 100.0 else 1.0",
+                       NULL, ctx, 0) == 0);
+       TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
+
        expr__ctx_free(ctx);
 
        return 0;
index 5b878f0..ba7d3b6 100644 (file)
@@ -142,6 +142,16 @@ if_expr: expr IF expr ELSE expr
                $$.ids = $1.ids;
                ids__free($3.ids);
                ids__free($5.ids);
+       } else if ($1.val == $5.val) {
+               /*
+                * LHS == RHS, so both are an identical constant. No need to
+                * evaluate any events.
+                */
+               $$.val = $1.val;
+               $$.ids = NULL;
+               ids__free($1.ids);
+               ids__free($3.ids);
+               ids__free($5.ids);
        } else {
                /*
                 * Value is either the LHS or RHS and we need the IF expression