perf test: Make metric testing more robust
authorIan Rogers <irogers@google.com>
Thu, 23 Dec 2021 18:56:22 +0000 (10:56 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 15 Feb 2022 20:15:33 +0000 (17:15 -0300)
When testing metric expressions we fake counter values from 1 going
upward. For some metrics this can yield negative values that are clipped
to zero, and then cause divide by zero failures.

Such clipping is questionable but may be a result of tools automatically
generating metrics. A workaround for this case is to try a second time
with counter values going in the opposite direction.

This case was seen in a metric like:
  event1 / max(event2 - event3, 0)
But it may also happen in more sensible metrics like:
  event1 / (event2 + event3 - 1 - event4)

Reviewed-by: John Garry <john.garry@huawei.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20211223185622.3435128-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/pmu-events.c

index 1c695fb..299a215 100644 (file)
@@ -994,8 +994,18 @@ static int test__parsing(struct test_suite *test __maybe_unused,
                        }
 
                        if (expr__parse(&result, ctx, pe->metric_expr)) {
-                               expr_failure("Parse failed", map, pe);
-                               ret++;
+                               /*
+                                * Parsing failed, make numbers go from large to
+                                * small which can resolve divide by zero
+                                * issues.
+                                */
+                               k = 1024;
+                               hashmap__for_each_entry(ctx->ids, cur, bkt)
+                                       expr__add_id_val(ctx, strdup(cur->key), k--);
+                               if (expr__parse(&result, ctx, pe->metric_expr)) {
+                                       expr_failure("Parse failed", map, pe);
+                                       ret++;
+                               }
                        }
                }
        }
@@ -1054,10 +1064,20 @@ static int metric_parse_fake(const char *str)
                }
        }
 
-       if (expr__parse(&result, ctx, str))
-               pr_err("expr__parse failed\n");
-       else
-               ret = 0;
+       ret = 0;
+       if (expr__parse(&result, ctx, str)) {
+               /*
+                * Parsing failed, make numbers go from large to small which can
+                * resolve divide by zero issues.
+                */
+               i = 1024;
+               hashmap__for_each_entry(ctx->ids, cur, bkt)
+                       expr__add_id_val(ctx, strdup(cur->key), i--);
+               if (expr__parse(&result, ctx, str)) {
+                       pr_err("expr__parse failed\n");
+                       ret = -1;
+               }
+       }
 
 out:
        expr__ctx_free(ctx);