aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJanosch Frank <frankja@linux.vnet.ibm.com>2016-01-11 16:17:50 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2016-01-26 15:58:13 +0100
commit357bc1e74fc8af96530148d52dd9ccc8e626f000 (patch)
tree4c4cce7c057ca6d75f1fb3dbb79f2358922180af
parente06715a3632e0b454dca32ffb9bfefe8fd106a7d (diff)
scripts/kvm/kvm_stat: Cleanup cpu list retrieval
Reading /sys/devices/system/cpu/online makes opening the cpu directories unnecessary and works on more/older systems. Signed-off-by: Janosch Frank <frankja@linux.vnet.ibm.com> Message-Id: <1452525484-32309-21-git-send-email-frankja@linux.vnet.ibm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rwxr-xr-xscripts/kvm/kvm_stat31
1 files changed, 20 insertions, 11 deletions
diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat
index 032e491f6b..083dd2f6a3 100755
--- a/scripts/kvm/kvm_stat
+++ b/scripts/kvm/kvm_stat
@@ -280,18 +280,27 @@ def walkdir(path):
return next(os.walk(path))
+def parse_int_list(list_string):
+ """Returns an int list from a string of comma separated integers and
+ integer ranges."""
+ integers = []
+ members = list_string.split(',')
+
+ for member in members:
+ if '-' not in member:
+ integers.append(int(member))
+ else:
+ int_range = member.split('-')
+ integers.extend(range(int(int_range[0]),
+ int(int_range[1]) + 1))
+
+ return integers
+
+
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)))
- return cpulist
+ with open('/sys/devices/system/cpu/online') as cpu_list:
+ cpu_string = cpu_list.readline()
+ return parse_int_list(cpu_string)
filters = {}
filters['kvm_userspace_exit'] = ('reason', USERSPACE_EXIT_REASONS)