From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44494) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a706z-0003NC-3e for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a706v-0003tn-95 for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:17 -0500 Received: from e06smtp13.uk.ibm.com ([195.75.94.109]:59974) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a706u-0003tQ-Vy for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:13 -0500 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 10 Dec 2015 12:14:11 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id C5F65219005E for ; Thu, 10 Dec 2015 12:13:49 +0000 (GMT) Received: from d06av06.portsmouth.uk.ibm.com (d06av06.portsmouth.uk.ibm.com [9.149.37.217]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id tBACDvo93473882 for ; Thu, 10 Dec 2015 12:13:57 GMT Received: from d06av06.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id tBACDuoR025345 for ; Thu, 10 Dec 2015 05:13:56 -0700 From: Janosch Frank Date: Thu, 10 Dec 2015 13:12:50 +0100 Message-Id: <1449749584-23214-21-git-send-email-frankja@linux.vnet.ibm.com> In-Reply-To: <1449749584-23214-1-git-send-email-frankja@linux.vnet.ibm.com> References: <1449749584-23214-1-git-send-email-frankja@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 20/34] scripts/kvm/kvm_stat: Cleanup cpu list retrieval List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: cornelia.huck@de.ibm.com, frankja@linux.vnet.ibm.com 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