From d16d3bc79454a2f0eec955d3fc4c4360edec21f6 Mon Sep 17 00:00:00 2001 From: Steven Skeard Date: Mon, 10 Jun 2024 11:35:41 -0400 Subject: [PATCH 1/2] CNF-13015: Add helper function to run `lscpu` and parse result using regex parameters - This is similar to the cpuinfo function that reads from /proc/cpuinfo - Unfortunately that function was insufficient for our needs as we need to identify both the Vendor and Architecture reliably --- .../functions/function_lscpu_check.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tuned/profiles/functions/function_lscpu_check.py diff --git a/tuned/profiles/functions/function_lscpu_check.py b/tuned/profiles/functions/function_lscpu_check.py new file mode 100644 index 00000000..c6437876 --- /dev/null +++ b/tuned/profiles/functions/function_lscpu_check.py @@ -0,0 +1,32 @@ +import re +import tuned.logs +from . import base + +log = tuned.logs.get() + +class lscpu_check(base.Function): + """ + Checks regexes against the output of lscpu. Accepts arguments in the + following form: REGEX1, STR1, REGEX2, STR2, ...[, STR_FALLBACK] + If REGEX1 matches something in the output it expands to STR1, + if REGEX2 matches it expands to STR2. It stops on the first match, + i.e. if REGEX1 matches, no more regexes are processed. If none + regex matches it expands to STR_FALLBACK. If there is no fallback, + it expands to empty string. + """ + def __init__(self): + # unlimited number of arguments, min 2 arguments + super(lscpu_check, self).__init__("lscpu_check", 0, 2) + + def execute(self, args): + if not super(lscpu_check, self).execute(args): + return None + lscpu = self._cmd.execute("lscpu") + for i in range(0, len(args), 2): + if i + 1 < len(args): + if re.search(args[i], lscpu, re.MULTILINE): + return args[i + 1] + if len(args) % 2: + return args[-1] + else: + return "" From b651198dbfd83d54007b04b66edefaa389db1711 Mon Sep 17 00:00:00 2001 From: Steven Skeard Date: Wed, 12 Jun 2024 14:37:57 -0400 Subject: [PATCH 2/2] CNF-13015: Fix helper function not accessing the correct return result --- tuned/profiles/functions/function_lscpu_check.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tuned/profiles/functions/function_lscpu_check.py b/tuned/profiles/functions/function_lscpu_check.py index c6437876..c4eae9a6 100644 --- a/tuned/profiles/functions/function_lscpu_check.py +++ b/tuned/profiles/functions/function_lscpu_check.py @@ -21,7 +21,8 @@ def __init__(self): def execute(self, args): if not super(lscpu_check, self).execute(args): return None - lscpu = self._cmd.execute("lscpu") + # Stdout is the 2nd result from the execute call + _, lscpu = self._cmd.execute("lscpu") for i in range(0, len(args), 2): if i + 1 < len(args): if re.search(args[i], lscpu, re.MULTILINE):