perf jevents: Parse metrics during conversion
Ian Rogers
reviewerPATCH 1/1 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 and removing unnecessary whitespace. On x86 this reduces the string size by 2,823bytes (0.06%).
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.
--- tools/perf/pmu-events/Build | 2 +- tools/perf/pmu-events/jevents.py | 11 +- tools/perf/pmu-events/metric.py | 459 +++++++++++++++++++++++++++ tools/perf/pmu-events/metric_test.py | 143 +++++++++ 4 files changed, 611 insertions(+), 4 deletions(-) create mode 100644 tools/perf/pmu-events/metric.py create mode 100644 tools/perf/pmu-events/metric_test.py
tools/perf/pmu-events/Build +1 -1
@@ -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
tools/perf/pmu-events/jevents.py +8 -3
@@ -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,9 @@ 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.get('MetricExpr')) 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 +323,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
tools/perf/pmu-events/metric.py new file +459 -0
459 lines not on this page · raw message
tools/perf/pmu-events/metric_test.py new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog