Re: [PATCH v4] perf jevents: Parse metrics during conversion

From: Arnaldo Carvalho de Melo
Date: Mon Dec 12 2022 - 10:46:17 EST


Em Thu, Dec 08, 2022 at 09:41:55AM -0800, Ian Rogers escreveu:
> Apologies, I found an issue with a double-if. Could you add the 1 line
> fix plus test:

Had to apply manually, the patch was munged, end result should be the
same:


diff --git a/tools/perf/pmu-events/metric.py b/tools/perf/pmu-events/metric.py
index cc451a26575185f4..4797ed4fd817b1a0 100644
--- a/tools/perf/pmu-events/metric.py
+++ b/tools/perf/pmu-events/metric.py
@@ -462,6 +462,7 @@ class _RewriteIfExpToSelect(ast.NodeTransformer):

def visit_IfExp(self, node):
# pylint: disable=invalid-name
+ self.generic_visit(node)
call = ast.Call(
func=ast.Name(id='Select', ctx=ast.Load()),
args=[node.body, node.test, node.orelse],
diff --git a/tools/perf/pmu-events/metric_test.py b/tools/perf/pmu-events/metric_test.py
index 4741b7b6612dc268..15315d0f716caf4e 100644
--- a/tools/perf/pmu-events/metric_test.py
+++ b/tools/perf/pmu-events/metric_test.py
@@ -87,6 +87,10 @@ class TestMetricExpressions(unittest.TestCase):
after = r'min((a + b if c > 1 else c + d), e + f)'
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)

+ before =3D r'a if b else c if d else e'
+ after =3D r'(a if b else (c if d else e))'
+ self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
+
def test_ToPython(self):
# pylint: disable=eval-used
# Based on an example of a real metric.

> diff --git a/tools/perf/pmu-events/metric.py b/tools/perf/pmu-events/metric.py
> index cc451a265751..4797ed4fd817 100644
> --- a/tools/perf/pmu-events/metric.py
> +++ b/tools/perf/pmu-events/metric.py
> @@ -462,6 +462,7 @@ class _RewriteIfExpToSelect(ast.NodeTransformer):
>
> def visit_IfExp(self, node):
> # pylint: disable=invalid-name
> + self.generic_visit(node)
> call = ast.Call(
> func=ast.Name(id='Select', ctx=ast.Load()),
> args=[node.body, node.test, node.orelse],
> diff --git a/tools/perf/pmu-events/metric_test.py
> b/tools/perf/pmu-events/metric_test.py
> index 4741b7b6612d..6980f452df0a 100644
> --- a/tools/perf/pmu-events/metric_test.py
> +++ b/tools/perf/pmu-events/metric_test.py
> @@ -87,6 +87,10 @@ class TestMetricExpressions(unittest.TestCase):
> after = r'min((a + b if c > 1 else c + d), e + f)'
> self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
>
> + before = r'a if b else c if d else e'
> + after = r'(a if b else (c if d else e))'
> + self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
> +
> def test_ToPython(self):
> # pylint: disable=eval-used
> # Based on an example of a real metric.
>
> Thanks,
> Ian
>
> On Wed, Dec 7, 2022 at 9:47 AM Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:
> >
> > Em Wed, Dec 07, 2022 at 05:42:52PM +0000, John Garry escreveu:
> > > On 07/12/2022 17:40, Arnaldo Carvalho de Melo wrote:
> > > > Em Tue, Dec 06, 2022 at 09:59:08PM -0800, Ian Rogers escreveu:
> > > > > Currently the 'MetricExpr' json value is passed from the json
> > > > > file to the pmu-events.c. This change introduces an expression
> > > > > tree that is parsed into. The parsing is done largely by using
> > > > > operator overloading and python's 'eval' function. Two advantages
> > > > > in doing this are:
> > > > John, what tag can I get from you? 😄
> > > >
> > > > - Arnaldo
> > > Sure,
> > > Reviewed-by: John Garry <john.g.garry@xxxxxxxxxx>
> >
> > Thanks a lot!
> >
> > - Arnaldo

--

- Arnaldo