Skip to content

Commit

Permalink
Merge pull request intel#161 from edwarddavidbaker/fix-inefficient-regex
Browse files Browse the repository at this point in the history
create_perf_json: Fix inefficient regex warning
  • Loading branch information
edwarddavidbaker authored Mar 27, 2024
2 parents 379ad3d + 5ca8866 commit 4cae807
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-perf-json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- name: Run unittests
working-directory: ./scripts/unittesting
run: python metric_test.py
run: python -m unittest metric_test.py test_create_perf_json.py

- name: Create perf json files
working-directory: ./scripts
Expand Down
15 changes: 11 additions & 4 deletions scripts/create_perf_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,15 @@ def smi_json(self) -> metric.MetricGroup:
])

@staticmethod
def extract_pebs_formula(formula):
MIN_MAX_PEBS = re.compile(r"([A-Za-z0-9\_\.@]+)\*(min|max)\(\s*(\$PEBS)\s*,((\s*([^\s\)])\s*)+)\)")
def extract_pebs_formula(formula: str) -> str:
"""
Convert metric formulas using $PEBS.
Example:
Input: MEM_INST_RETIRED.STLB_HIT_LOADS*min($PEBS, 7) / tma_info_thread_clks + tma_load_stlb_miss
Return: MEM_INST_RETIRED.STLB_HIT_LOADS * min(MEM_INST_RETIRED.STLB_HIT_LOADS:R, 7) / tma_info_thread_clks + tma_load_stlb_miss
"""
MIN_MAX_PEBS = re.compile(r"([A-Za-z0-9_.@]+)\*(min|max)\( *(\$PEBS) *,([a-z0-9_ */+-]+)\)")
NON_REL_OPS = r"(?<!##)(?<!##2)/|[\(\)]+|[\+\-,]|\*(?![\$])|(?<!##)\?| if not | if | else |min\(|max\(| and | or | in | not "
REL_OPS = r"[<>]=?|=="
STR_OPS = r"'[A-Z\-]+'"
Expand All @@ -666,11 +673,10 @@ def extract_pebs_formula(formula):
for m in re.finditer(MIN_MAX_PEBS, formula):
main_event = m.group(1).strip()
min_max = m.group(2)
pebs = m.group(3)
alternative = m.group(4).strip()

mod = 'R' if '@' in main_event else ':R'
new_string = f' {main_event} * {min_max}({main_event}{mod}, {alternative}) '
new_string = f'{main_event} * {min_max}({main_event}{mod}, {alternative})'
new_formula = re.sub(m.re, new_string, new_formula, count=1)

formula_list = re.split(OPS, new_formula)
Expand All @@ -681,6 +687,7 @@ def extract_pebs_formula(formula):
mod = 'R' if '@' in event_name else ':R'
new_element = f'( {event_name} * {event_name}{mod} )'
new_formula = new_formula.replace(element, new_element)

return new_formula

def extract_tma_metrics(self, csvfile: TextIO, pmu_prefix: str,
Expand Down
57 changes: 57 additions & 0 deletions scripts/unittesting/test_create_perf_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

import unittest
import sys
from pathlib import Path

# Add create_perf_json.py directory to the path before importing.
_script_dir = Path(__file__).resolve().parent
sys.path.append(str(_script_dir.parent))

from create_perf_json import Model


class TestModel(unittest.TestCase):

def test_extract_pebs_formula(self):
"""Test formulas which use $PEBS but do not include min() or max()."""
tests = [
(
'EVENT.A*$PEBS',
'( EVENT.A * EVENT.A:R )',
),
(
'EVENT.A + [email protected]@*$PEBS',
'EVENT.A + ( [email protected]@ * [email protected]@R )',
),
]

for input, expected_result in tests:
with self.subTest(input=input, expected_result=expected_result):
self.assertEqual(expected_result, Model.extract_pebs_formula(input))

def test_extract_pebs_formula_with_min_max(self):
"""Test formulas which use $PEBS and also min() or max()."""
tests = [
(
'EVENT.A*min( $PEBS, 4) / EVENT.B',
'EVENT.A * min(EVENT.A:R, 4) / EVENT.B',
),
(
'EVENT.A*min($PEBS, 2) / (1 + EVENT.B*max($PEBS, 8))',
'EVENT.A * min(EVENT.A:R, 2) / (1 + EVENT.B * max(EVENT.B:R, 8))',
),
(
'EVENT.A*min($PEBS, 9 * test_info) * (1 + ([email protected]@ / [email protected]@) / 2) / test_info_2',
'EVENT.A * min(EVENT.A:R, 9 * test_info) * (1 + ([email protected]@ / [email protected]@) / 2) / test_info_2',
),
(
'([email protected]@*min($PEBS, 24 * test_info) + [email protected]@*min($PEBS, 24 - test_info) * (1 - ([email protected]@ / ([email protected]@ + [email protected]@)))) * 5',
'([email protected]@ * min([email protected]@R, 24 * test_info) + [email protected]@ * min([email protected]@R, 24 - test_info) * (1 - ([email protected]@ / ([email protected]@ + [email protected]@)))) * 5',
)
]

for input, expected_result in tests:
with self.subTest(input=input, expected_result=expected_result):
self.assertEqual(expected_result, Model.extract_pebs_formula(input))

0 comments on commit 4cae807

Please sign in to comment.