[libvirt PATCH 2/3] cpu-data.py: Parse kcpuid output

Tim Wiederhake twiederh at redhat.com
Tue Apr 27 08:25:02 UTC 2021


Linux 5.13 introduces "kcpuid", a tool similar to "cpuid", see
https://lore.kernel.org/lkml/1614928878-86075-1-git-send-email-feng.tang@intel.com/

Output formats of cpuid and kcpuid differ slightly. This adds support
for the latter.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 tests/cputestdata/cpu-data.py | 53 ++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/tests/cputestdata/cpu-data.py b/tests/cputestdata/cpu-data.py
index ac4d0ff8e7..4305aacf35 100755
--- a/tests/cputestdata/cpu-data.py
+++ b/tests/cputestdata/cpu-data.py
@@ -58,6 +58,53 @@ def gather_cpuid_leaves_cpuid(output):
             "edx": int(match.group(6), 0)}
 
 
+def gather_cpuid_leaves_kcpuid(output):
+    leave_pattern = re.compile(
+        "^(0x[0-9a-f]+): "
+        "EAX=(0x[0-9a-f]+), "
+        "EBX=(0x[0-9a-f]+), "
+        "ECX=(0x[0-9a-f]+), "
+        "EDX=(0x[0-9a-f]+)$")
+    branch_pattern_head = re.compile(
+        "^(0x[0-9a-f]+): "
+        "subleafs:$")
+    branch_pattern_body = re.compile(
+        "^\\s*([0-9]+): "
+        "EAX=(0x[0-9a-f]+), "
+        "EBX=(0x[0-9a-f]+), "
+        "ECX=(0x[0-9a-f]+), "
+        "EDX=(0x[0-9a-f]+)$")
+
+    regs = list()
+    eax_in = 0
+    for line in output.split("\n"):
+        match = branch_pattern_head.match(line)
+        if match:
+            eax_in = int(match.group(1), 0)
+            continue
+        match = branch_pattern_body.match(line)
+        if match:
+            regs.append({
+                "eax_in": eax_in,
+                "ecx_in": int(match.group(1), 0),
+                "eax": int(match.group(2), 0),
+                "ebx": int(match.group(3), 0),
+                "ecx": int(match.group(4), 0),
+                "edx": int(match.group(5), 0)})
+            continue
+        match = leave_pattern.match(line)
+        if match:
+            regs.append({
+                "eax_in": int(match.group(1), 0),
+                "ecx_in": 0,
+                "eax": int(match.group(2), 0),
+                "ebx": int(match.group(3), 0),
+                "ecx": int(match.group(4), 0),
+                "edx": int(match.group(5), 0)})
+            continue
+    return regs
+
+
 def gather_cpuid_leaves(args):
     def mask(regs, eax_in, ecx_in, eax_mask, ebx_mask, ecx_mask, edx_mask):
         if regs["eax_in"] == eax_in and regs["ecx_in"] == ecx_in:
@@ -77,7 +124,11 @@ def gather_cpuid_leaves(args):
              "package, you can find the sources or binary packages at "
              "'http://www.etallen.com/cpuid.html'.".format(e.filename))
 
-    reglist = gather_cpuid_leaves_cpuid(output)
+    if "=====" in output:
+        reglist = gather_cpuid_leaves_kcpuid(output)
+    else:
+        reglist = gather_cpuid_leaves_cpuid(output)
+
     for regs in reglist:
         # local apic id. Pretend to always run on logical processor #0.
         mask(regs, 0x01, 0x00, 0xffffffff, 0x00ffffff, 0xffffffff, 0xffffffff)
-- 
2.26.3




More information about the libvir-list mailing list