perf expr: Allow a double if expression
authorIan Rogers <irogers@google.com>
Tue, 4 Oct 2022 02:15:50 +0000 (19:15 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 6 Oct 2022 11:03:52 +0000 (08:03 -0300)
Some TMA metrics have double if expressions like:

  ( CPU_CLK_UNHALTED.THREAD / 2 ) * ( 1 + CPU_CLK_UNHALTED.ONE_THREAD_ACTIVE / CPU_CLK_UNHALTED.REF_XCLK ) ) if #core_wide < 1 else ( CPU_CLK_UNHALTED.THREAD_ANY / 2 ) if #SMT_on else CPU_CLK_UNHALTED.THREAD

This currently fails to parse as the left hand side if expression needs
to be in parentheses. By allowing the if expression to have a right hand
side that is an if expression we can parse the expression above, with
left to right evaluation order that matches languages like Python.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Ahmad Yasin <ahmad.yasin@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Caleb Biggers <caleb.biggers@intel.com>
Cc: Florian Fischer <florian.fischer@muhq.space>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.garry@huawei.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kshipra Bopardikar <kshipra.bopardikar@intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Miaoqian Lin <linmq006@gmail.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: Thomas Richter <tmricht@linux.ibm.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: https://lore.kernel.org/r/20221004021612.325521-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/expr.c
tools/perf/util/expr.y

index 8bd7197..6512f5e 100644 (file)
@@ -95,6 +95,10 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
        ret |= test(ctx, "min(1,2) + 1", 2);
        ret |= test(ctx, "max(1,2) + 1", 3);
        ret |= test(ctx, "1+1 if 3*4 else 0", 2);
+       ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100);
+       ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200);
+       ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100);
+       ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300);
        ret |= test(ctx, "1.1 + 2.1", 3.2);
        ret |= test(ctx, ".1 + 2.", 2.1);
        ret |= test(ctx, "d_ratio(1, 2)", 0.5);
index a30b825..635e562 100644 (file)
@@ -156,7 +156,7 @@ start: if_expr
 }
 ;
 
-if_expr: expr IF expr ELSE expr
+if_expr: expr IF expr ELSE if_expr
 {
        if (fpclassify($3.val) == FP_ZERO) {
                /*