Commit 40769665 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf jevents: Parse metrics during conversion

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:

1) Broken metrics fail at compile time rather than relying on
   `perf test` to detect. `perf test` remains relevant for checking
   event encoding and actual metric use.

2) The conversion to a string from the tree can minimize the metric's
   string size, for example, preferring 1e6 over 1000000, avoiding
   multiplication by 1 and removing unnecessary whitespace. On x86
   this reduces the string size by 2,930bytes (0.07%).

In future changes it would be possible to programmatically
generate the json expressions (a single line of text and so a
pain to write manually) for an architecture using the expression
tree. This could avoid copy-pasting metrics for all architecture
variants.

v4. Doesn't simplify "0*SLOTS" to 0, as the pattern is used to fix
    Intel metrics with topdown events.
v3. Avoids generic types on standard types like set that aren't
    supported until Python 3.9, fixing an issue with Python 3.6
    reported-by John Garry. v3 also fixes minor pylint issues and adds
    a call to Simplify on the read expression tree.
v2. Improvements to type information.

Committer notes:

Added one-line fixer from Ian, see first Link: tag below.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Reviewed-by: default avatarJohn Garry <john.g.garry@oracle.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
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>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Link: https://lore.kernel.org/r/CAP-5=fWa=zNK_ecpWGoGggHCQx7z-oW0eGMQf19Maywg0QK=4g@mail.gmail.com
Link: https://lore.kernel.org/r/20221207055908.1385448-1-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent b8976135
......@@ -21,7 +21,7 @@ $(OUTPUT)pmu-events/pmu-events.c: pmu-events/empty-pmu-events.c
$(call rule_mkdir)
$(Q)$(call echo-cmd,gen)cp $< $@
else
$(OUTPUT)pmu-events/pmu-events.c: $(JSON) $(JSON_TEST) $(JEVENTS_PY)
$(OUTPUT)pmu-events/pmu-events.c: $(JSON) $(JSON_TEST) $(JEVENTS_PY) pmu-events/metric.py
$(call rule_mkdir)
$(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) pmu-events/arch $@
endif
......@@ -4,6 +4,7 @@
import argparse
import csv
import json
import metric
import os
import sys
from typing import (Callable, Dict, Optional, Sequence, Set, Tuple)
......@@ -268,9 +269,10 @@ class JsonEvent:
self.metric_name = jd.get('MetricName')
self.metric_group = jd.get('MetricGroup')
self.metric_constraint = jd.get('MetricConstraint')
self.metric_expr = jd.get('MetricExpr')
if self.metric_expr:
self.metric_expr = self.metric_expr.replace('\\', '\\\\')
self.metric_expr = None
if 'MetricExpr' in jd:
self.metric_expr = metric.ParsePerfJson(jd['MetricExpr']).Simplify()
arch_std = jd.get('ArchStdEvent')
if precise and self.desc and '(Precise Event)' not in self.desc:
extra_desc += ' (Must be precise)' if precise == '2' else (' (Precise '
......@@ -322,6 +324,10 @@ class JsonEvent:
s = ''
for attr in _json_event_attributes:
x = getattr(self, attr)
if x and attr == 'metric_expr':
# Convert parsed metric expressions into a string. Slashes
# must be doubled in the file.
x = x.ToPerfJson().replace('\\', '\\\\')
s += f'{x}\\000' if x else '\\000'
return s
......
This diff is collapsed.
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
import unittest
from metric import Constant
from metric import Event
from metric import ParsePerfJson
class TestMetricExpressions(unittest.TestCase):
def test_Operators(self):
a = Event('a')
b = Event('b')
self.assertEqual((a | b).ToPerfJson(), 'a | b')
self.assertEqual((a ^ b).ToPerfJson(), 'a ^ b')
self.assertEqual((a & b).ToPerfJson(), 'a & b')
self.assertEqual((a < b).ToPerfJson(), 'a < b')
self.assertEqual((a > b).ToPerfJson(), 'a > b')
self.assertEqual((a + b).ToPerfJson(), 'a + b')
self.assertEqual((a - b).ToPerfJson(), 'a - b')
self.assertEqual((a * b).ToPerfJson(), 'a * b')
self.assertEqual((a / b).ToPerfJson(), 'a / b')
self.assertEqual((a % b).ToPerfJson(), 'a % b')
one = Constant(1)
self.assertEqual((a + one).ToPerfJson(), 'a + 1')
def test_Brackets(self):
a = Event('a')
b = Event('b')
c = Event('c')
self.assertEqual((a * b + c).ToPerfJson(), 'a * b + c')
self.assertEqual((a + b * c).ToPerfJson(), 'a + b * c')
self.assertEqual(((a + a) + a).ToPerfJson(), 'a + a + a')
self.assertEqual(((a + b) * c).ToPerfJson(), '(a + b) * c')
self.assertEqual((a + (b * c)).ToPerfJson(), 'a + b * c')
self.assertEqual(((a / b) * c).ToPerfJson(), 'a / b * c')
self.assertEqual((a / (b * c)).ToPerfJson(), 'a / (b * c)')
def test_ParsePerfJson(self):
# Based on an example of a real metric.
before = '(a + b + c + d) / (2 * e)'
after = before
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
# Parsing should handle events with '-' in their name. Note, in
# the json file the '\' are doubled to '\\'.
before = r'topdown\-fe\-bound / topdown\-slots - 1'
after = before
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
# Parsing should handle escaped modifiers. Note, in the json file
# the '\' are doubled to '\\'.
before = r'arb@event\=0x81\,umask\=0x1@ + arb@event\=0x84\,umask\=0x1@'
after = before
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
# Parsing should handle exponents in numbers.
before = r'a + 1e12 + 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'
after = f'({before})'
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
before = r'Event1 if 0 else Event2'
after = f'({before})'
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
before = r'Event1 if 1 else Event2'
after = f'({before})'
self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
# Ensure the select is evaluate last.
before = r'Event1 + 1 if Event2 < 2 else Event3 + 3'
after = (r'Select(Event(r"Event1") + Constant(1), Event(r"Event2") < '
r'Constant(2), Event(r"Event3") + Constant(3))')
self.assertEqual(ParsePerfJson(before).ToPython(), after)
before = r'Event1 > 1 if Event2 < 2 else Event3 > 3'
after = (r'Select(Event(r"Event1") > Constant(1), Event(r"Event2") < '
r'Constant(2), Event(r"Event3") > Constant(3))')
self.assertEqual(ParsePerfJson(before).ToPython(), after)
before = r'min(a + b if c > 1 else c + d, e + f)'
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.
before = '(a + b + c + d) / (2 * e)'
py = ParsePerfJson(before).ToPython()
after = eval(py).ToPerfJson()
self.assertEqual(before, after)
def test_Simplify(self):
before = '1 + 2 + 3'
after = '6'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a + 0'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = '0 + a'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a | 0'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = '0 | a'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a * 0'
after = '0'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = '0 * a'
after = '0'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a * 1'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = '1 * a'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a if 0 else b'
after = 'b'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a if 1 else b'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
before = 'a if b else a'
after = 'a'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
# Pattern used to add a slots event to metrics that require it.
before = '0 * SLOTS'
after = '0 * SLOTS'
self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment