qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 20/34] scripts/kvm/kvm_stat: Cleanup cpu list retrie


From: Janosch Frank
Subject: [Qemu-devel] [PATCH 20/34] scripts/kvm/kvm_stat: Cleanup cpu list retrieval
Date: Thu, 10 Dec 2015 13:12:50 +0100

Reading /sys/devices/system/cpu/online makes opening the cpu
directories unnecessary and works on more/older systems.
---
 scripts/kvm/kvm_stat | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat
index 7bd76b3..20fc5c9 100755
--- a/scripts/kvm/kvm_stat
+++ b/scripts/kvm/kvm_stat
@@ -282,15 +282,18 @@ def walkdir(path):
 
 def get_online_cpus():
     cpulist = []
-    pattern = r'cpu([0-9]+)'
-    basedir = '/sys/devices/system/cpu'
-    for entry in os.listdir(basedir):
-        match = re.match(pattern, entry)
-        if not match:
-            continue
-        path = os.path.join(basedir, entry, 'online')
-        if os.path.isfile(path) and open(path).read().strip() == '1':
-            cpulist.append(int(match.group(1)))
+
+    with open('/sys/devices/system/cpu/online') as cpu_list:
+        cpu_string = cpu_list.readline()
+        cpus = cpu_string.split(',')
+
+        for cpu in cpus:
+            if '-' not in cpu:
+                cpulist.append(int(cpu))
+            else:
+                cpu_range = cpu.split('-')
+                cpulist.extend(range(int(cpu_range[0]),
+                                     int(cpu_range[1]) + 1))
     return cpulist
 
 filters = {}
-- 
2.3.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]