From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43569) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aIeFi-0001kF-5B for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:19:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aIeFf-0001ux-7C for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:19:26 -0500 Received: from e06smtp06.uk.ibm.com ([195.75.94.102]:59362) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aIeFe-0001uP-Ru for qemu-devel@nongnu.org; Mon, 11 Jan 2016 10:19:23 -0500 Received: from localhost by e06smtp06.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 11 Jan 2016 15:19:21 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id D02532190069 for ; Mon, 11 Jan 2016 15:19:07 +0000 (GMT) Received: from d06av05.portsmouth.uk.ibm.com (d06av05.portsmouth.uk.ibm.com [9.149.37.229]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u0BFJIdQ11338230 for ; Mon, 11 Jan 2016 15:19:18 GMT Received: from d06av05.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av05.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u0BFJIXD018851 for ; Mon, 11 Jan 2016 08:19:18 -0700 From: Janosch Frank Date: Mon, 11 Jan 2016 16:17:50 +0100 Message-Id: <1452525484-32309-21-git-send-email-frankja@linux.vnet.ibm.com> In-Reply-To: <1452525484-32309-1-git-send-email-frankja@linux.vnet.ibm.com> References: <1452525484-32309-1-git-send-email-frankja@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH v2 20/34] scripts/kvm/kvm_stat: Cleanup cpu list retrieval List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: pbonzini@redhat.com Cc: frankja@linux.vnet.ibm.com, qemu-devel@nongnu.org Reading /sys/devices/system/cpu/online makes opening the cpu directories unnecessary and works on more/older systems. Signed-off-by: Janosch Frank --- scripts/kvm/kvm_stat | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat index 032e491..083dd2f 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) -- 2.3.0