Re: [PATCH v2 05/12] perf jevents: Support parsing negative exponents

From: Ian Rogers
Date: Thu Mar 14 2024 - 01:35:56 EST


On Tue, Mar 5, 2024 at 11:20 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> On Fri, Mar 1, 2024 at 5:00 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
> >
> > Support negative exponents when parsing from a json metric string by
> > making the numbers after the 'e' optional in the 'Event' insertion fix
> > up.
> >
> > Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> > ---
> > tools/perf/pmu-events/metric.py | 2 +-
> > tools/perf/pmu-events/metric_test.py | 4 ++++
> > 2 files changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/pmu-events/metric.py b/tools/perf/pmu-events/metric.py
> > index 847b614d40d5..31eea2f45152 100644
> > --- a/tools/perf/pmu-events/metric.py
> > +++ b/tools/perf/pmu-events/metric.py
> > @@ -573,7 +573,7 @@ def ParsePerfJson(orig: str) -> Expression:
> > # a double by the Bison parser
> > py = re.sub(r'0Event\(r"[xX]([0-9a-fA-F]*)"\)', r'Event("0x\1")', py)
> > # Convert accidentally converted scientific notation constants back
> > - py = re.sub(r'([0-9]+)Event\(r"(e[0-9]+)"\)', r'\1\2', py)
> > + py = re.sub(r'([0-9]+)Event\(r"(e[0-9]*)"\)', r'\1\2', py)
>
> I don't understand how it can handle negative numbers.
> Why isn't it like Event\(r"(e-?[0-9]+)"\) ?

When something like 3e12 is converted at this point it would be:
3Event("e12")
and so this substitution remove the Event and puts it back to:
3e12
but it expects a number with the "e". For a negative exponent like 3e-12 we get:
3Event("e")-12
and so there's no number and no substitution. Changing the + to a *
means the number in the event next to the "e" becomes optional and so
we match and remove the Event again.

I'm wondering about having this be a bit more of a parser, but then it
kind of defeats using Python's eval as being the parser.

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> > # Convert all the known keywords back from events to just the keyword
> > keywords = ['if', 'else', 'min', 'max', 'd_ratio', 'source_count', 'has_event', 'strcmp_cpuid_str']
> > for kw in keywords:
> > diff --git a/tools/perf/pmu-events/metric_test.py b/tools/perf/pmu-events/metric_test.py
> > index ee22ff43ddd7..8acfe4652b55 100755
> > --- a/tools/perf/pmu-events/metric_test.py
> > +++ b/tools/perf/pmu-events/metric_test.py
> > @@ -61,6 +61,10 @@ class TestMetricExpressions(unittest.TestCase):
> > after = before
> > self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
> >
> > + before = r'a + 3e-12 + b'
> > + after = before
> > + self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
> > +
> > def test_IfElseTests(self):
> > # if-else needs rewriting to Select and back.
> > before = r'Event1 if #smt_on else Event2'
> > --
> > 2.44.0.278.ge034bb2e1d-goog
> >