Skip to content

Commit 4076966

Browse files
captain5050acmel
authored andcommitted
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: Ian Rogers <[email protected]> Reviewed-by: John Garry <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Sumanth Korikkar <[email protected]> Cc: Thomas Richter <[email protected]> Link: https://lore.kernel.org/r/CAP-5=fWa=zNK_ecpWGoGggHCQx7z-oW0eGMQf19Maywg0QK=4g@mail.gmail.com Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent b897613 commit 4076966

File tree

4 files changed

+669
-4
lines changed

4 files changed

+669
-4
lines changed

tools/perf/pmu-events/Build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $(OUTPUT)pmu-events/pmu-events.c: pmu-events/empty-pmu-events.c
2121
$(call rule_mkdir)
2222
$(Q)$(call echo-cmd,gen)cp $< $@
2323
else
24-
$(OUTPUT)pmu-events/pmu-events.c: $(JSON) $(JSON_TEST) $(JEVENTS_PY)
24+
$(OUTPUT)pmu-events/pmu-events.c: $(JSON) $(JSON_TEST) $(JEVENTS_PY) pmu-events/metric.py
2525
$(call rule_mkdir)
2626
$(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) pmu-events/arch $@
2727
endif

tools/perf/pmu-events/jevents.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import csv
66
import json
7+
import metric
78
import os
89
import sys
910
from typing import (Callable, Dict, Optional, Sequence, Set, Tuple)
@@ -268,9 +269,10 @@ def unit_to_pmu(unit: str) -> Optional[str]:
268269
self.metric_name = jd.get('MetricName')
269270
self.metric_group = jd.get('MetricGroup')
270271
self.metric_constraint = jd.get('MetricConstraint')
271-
self.metric_expr = jd.get('MetricExpr')
272-
if self.metric_expr:
273-
self.metric_expr = self.metric_expr.replace('\\', '\\\\')
272+
self.metric_expr = None
273+
if 'MetricExpr' in jd:
274+
self.metric_expr = metric.ParsePerfJson(jd['MetricExpr']).Simplify()
275+
274276
arch_std = jd.get('ArchStdEvent')
275277
if precise and self.desc and '(Precise Event)' not in self.desc:
276278
extra_desc += ' (Must be precise)' if precise == '2' else (' (Precise '
@@ -322,6 +324,10 @@ def build_c_string(self) -> str:
322324
s = ''
323325
for attr in _json_event_attributes:
324326
x = getattr(self, attr)
327+
if x and attr == 'metric_expr':
328+
# Convert parsed metric expressions into a string. Slashes
329+
# must be doubled in the file.
330+
x = x.ToPerfJson().replace('\\', '\\\\')
325331
s += f'{x}\\000' if x else '\\000'
326332
return s
327333

0 commit comments

Comments
 (0)