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