Skip to contentSkip to conversation

perf expr: Make the evaluation of & and | logical and lazy

PATCH Applied Ian Rogers 4 participants 8 messages May 4, 2023 – Jun 5, 2023 perf events

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/2 perf expr: Make the evaluation of & and | logical and lazy

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 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.

---
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -17

98 lines not on this page · raw message

Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
author reviewer
#1 lore raw

PATCH 2/2 perf stat: Document --metric-no-threshold and threshold colors

Document the threshold behavior for -M/--metrics.

---
 tools/perf/Documentation/perf-stat.txt | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/perf/Documentation/perf-stat.txt +15 -0
index 29bdcfa93f04..ff71399db738 100644
@@ -353,6 +353,15 @@ small group that need not have multiplexing is lowered. This option
 forbids the event merging logic from sharing events between groups and
 may be used to increase accuracy in this case.
 
+--metric-no-threshold::
+Metric thresholds may increase the number of events necessary to
+compute whether a metric has exceeded its threshold expression. This
+may not be desirable, for example, as the events can introduce
+multiplexing. This option disables the adding of threshold expression
+events for a metric. However, if there are sufficient events to
+compute the threshold then the threshold is still computed and used to
+color the metric's computed value.
+
 --quiet::
 Don't print output, warnings or messages. This is useful with perf stat
 record below to only write data to the perf.data file.
@@ -389,6 +398,12 @@ For a group all metrics from the group are added.
 The events from the metrics are automatically measured.
 See perf list output for the possible metrics and metricgroups.
 
+	When threshold information is available for a metric, the
+	color red is used to signify a metric has exceeded a threshold
+	while green shows it is hasn't. The default color means that
+	no threshold information was available or the threshold
+	couldn't be computed.
+
 -A::
 --no-aggr::
 Do not aggregate counts across all monitored CPUs.
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] perf stat: Document --metric-no-threshold and threshold colors

Quoted 28 lines · Ian Rogers

Document the threshold behavior for -M/--metrics.

---
 tools/perf/Documentation/perf-stat.txt | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/perf/Documentation/perf-stat.txt +9 -0
index 29bdcfa93f04..ff71399db738 100644
@@ -353,6 +353,15 @@ small group that need not have multiplexing is lowered. This option
 forbids the event merging logic from sharing events between groups and
 may be used to increase accuracy in this case.
 
+--metric-no-threshold::
+Metric thresholds may increase the number of events necessary to
+compute whether a metric has exceeded its threshold expression. This
+may not be desirable, for example, as the events can introduce
+multiplexing. This option disables the adding of threshold expression
+events for a metric. However, if there are sufficient events to
+compute the threshold then the threshold is still computed and used to
+color the metric's computed value.
+
 --quiet::
 Don't print output, warnings or messages. This is useful with perf stat
 record below to only write data to the perf.data file.

tools/perf/Documentation/perf-stat.txt For a group all metrics from the group are added. · line 403

Quoted 6 lines
tools/perf/Documentation/perf-stat.txt +3 -0
@@ -389,6 +398,12 @@ For a group all metrics from the group are added.
 The events from the metrics are automatically measured.
 See perf list output for the possible metrics and metricgroups.
 
+	When threshold information is available for a metric, the
+	color red is used to signify a metric has exceeded a threshold
+	while green shows it is hasn't. The default color means that

A typo. "it is hasn't" -> "it hasn't"

Rest looks good to me.

Thanks,
Kan

Quoted 6 lines
tools/perf/Documentation/perf-stat.txt +3 -0
+	no threshold information was available or the threshold
+	couldn't be computed.
+
 -A::
 --no-aggr::
 Do not aggregate counts across all monitored CPUs.
author reviewer
#3 lore raw
In reply to Ian Rogers
Quoted 14 lines · Ian Rogers

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 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.

Quoted 7 lines

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.

Ping.

Thanks,
Ian

Quoted 163 lines
---
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -18
index 250e444bf032..6b110f9f95c9 100644
@@ -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); }
--

2.40.1.521.gf1e218fcd8-goog

Acked-by on v1 1/2
#4 lore raw
In reply to Ian Rogers
Quoted 13 lines · Ian Rogers

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 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.

Quoted 7 lines

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.

lgtm

jirka

Quoted 163 lines
---
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -17
index 250e444bf032..6b110f9f95c9 100644
@@ -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); }
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
Tested-by on v1 1/2
#5 lore raw
In reply to Ian Rogers
Quoted 13 lines · Ian Rogers

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 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.

Quoted 8 lines

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.

---

The patch works well on my machine.

Thanks,
Kan

Quoted 159 lines
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -17
index 250e444bf032..6b110f9f95c9 100644
@@ -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); }
author reviewer
#6 lore raw
Quoted 26 lines · Kan
Quoted 13 lines · Ian Rogers

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 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.

Quoted 8 lines

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.

---

Quoted 6 lines

The patch works well on my machine.

Thanks,
Kan

Arnaldo, could we take this set?

Thanks,
Ian

Quoted 159 lines
Quoted 159 lines
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -17
index 250e444bf032..6b110f9f95c9 100644
@@ -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); }
maintainer Applied
#7 lore raw

Em Mon, Jun 05, 2023 at 10:32:18AM -0700, Ian Rogers escreveu:

Quoted 27 lines · Kan
Quoted 26 lines · Ian Rogers
Quoted 21 lines

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 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.

---

Quoted 8 lines
Quoted 6 lines

The patch works well on my machine.

Thanks,
Kan

Arnaldo, could we take this set?

Thanks, applied.

- Arnaldo

Quoted 162 lines

Thanks,
Ian

Quoted 159 lines
Quoted 159 lines
 tools/perf/tests/expr.c | 40 +++++++++++++++++++
 tools/perf/util/expr.y  | 86 +++++++++++++++++++++++++++++++++--------
 2 files changed, 109 insertions(+), 17 deletions(-)
tools/perf/tests/expr.c +40 -0
index cbf0e0c74906..45c7fedb797a 100644
@@ -184,6 +184,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);
tools/perf/util/expr.y +69 -17
index 250e444bf032..6b110f9f95c9 100644
@@ -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); }
Signature · 1 line
- Arnaldo