From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44589) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7073-0003Pn-IM for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a706z-0003wB-Fv for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:21 -0500 Received: from e06smtp05.uk.ibm.com ([195.75.94.101]:49386) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a706z-0003va-83 for qemu-devel@nongnu.org; Thu, 10 Dec 2015 07:14:17 -0500 Received: from localhost by e06smtp05.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 10 Dec 2015 12:14:16 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id C360117D806C for ; Thu, 10 Dec 2015 12:14:30 +0000 (GMT) Received: from d06av06.portsmouth.uk.ibm.com (d06av06.portsmouth.uk.ibm.com [9.149.37.217]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id tBACDwIG3670514 for ; Thu, 10 Dec 2015 12:13:58 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 tBACDvU3025428 for ; Thu, 10 Dec 2015 05:13:57 -0700 From: Janosch Frank Date: Thu, 10 Dec 2015 13:12:55 +0100 Message-Id: <1449749584-23214-26-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 25/34] scripts/kvm/kvm_stat: Group arch specific data 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 Using global variables and multiple initialization functions for arch specific data makes the code hard to read. By grouping them in the Arch class we encapsulate and initialize them in one place. --- scripts/kvm/kvm_stat | 106 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat index 70a27da..d81e7b6 100755 --- a/scripts/kvm/kvm_stat +++ b/scripts/kvm/kvm_stat @@ -213,62 +213,59 @@ X86_EXIT_REASONS = { 'svm': SVM_EXIT_REASONS, } -SC_PERF_EVT_OPEN = None -EXIT_REASONS = None -IOCTL_NUMBERS = { - 'SET_FILTER' : 0x40082406, - 'ENABLE' : 0x00002400, - 'DISABLE' : 0x00002401, - 'RESET' : 0x00002403, -} +class Arch(object): + """Class that encapsulates global architecture specific data like + syscall and ioctl numbers. -def x86_init(flag): - global SC_PERF_EVT_OPEN - global EXIT_REASONS + """ + def __init__(self): + self.exit_reasons = None + self.sc_perf_evt_open = None + self.ioctl_numbers = { + 'SET_FILTER': 0x40082406, + 'ENABLE': 0x00002400, + 'DISABLE': 0x00002401, + 'RESET': 0x00002403, + } + self.set_arch_data() - SC_PERF_EVT_OPEN = 298 - EXIT_REASONS = X86_EXIT_REASONS[flag] + def set_arch_data(self): + machine = os.uname()[4] -def s390_init(): - global SC_PERF_EVT_OPEN + if machine.startswith('ppc'): + self.sc_perf_evt_open = 319 + self.ioctl_numbers['ENABLE'] = 0x20002400 + self.ioctl_numbers['DISABLE'] = 0x20002401 - SC_PERF_EVT_OPEN = 331 + # PPC comes in 32 and 64 bit and some generated ioctl + # numbers depend on the wordsize. + char_ptr_size = ctypes.sizeof(ctypes.c_char_p) + self.ioctl_numbers['SET_FILTER'] = 0x80002406 | char_ptr_size << 16 -def ppc_init(): - global SC_PERF_EVT_OPEN - global IOCTL_NUMBERS + elif machine.startswith('aarch64'): + self.sc_perf_evt_open = 241 + self.exit_reasons = AARCH64_EXIT_REASONS - SC_PERF_EVT_OPEN = 319 + elif machine.startswith('s390'): + self.sc_perf_evt_open = 331 - IOCTL_NUMBERS['ENABLE'] = 0x20002400 - IOCTL_NUMBERS['DISABLE'] = 0x20002401 - IOCTL_NUMBERS['SET_FILTER'] = 0x80002406 | (ctypes.sizeof(ctypes.c_char_p) - << 16) + else: + # X86_64 + for line in open('/proc/cpuinfo'): + if not line.startswith('flags'): + continue -def aarch64_init(): - global SC_PERF_EVT_OPEN - global EXIT_REASONS + self.sc_perf_evt_open = 298 - SC_PERF_EVT_OPEN = 241 - EXIT_REASONS = AARCH64_EXIT_REASONS + flags = line.split() + if 'vmx' in flags: + self.exit_reasons = VMX_EXIT_REASONS + if 'svm' in flags: + self.exit_reasons = SVM_EXIT_REASONS + return -def detect_platform(): - machine = os.uname()[4] - - if machine.startswith('ppc'): - ppc_init() - elif machine.startswith('aarch64'): - aarch64_init() - elif machine.startswith('s390'): - s390_init() - else: - for line in file('/proc/cpuinfo').readlines(): - if line.startswith('flags'): - for flag in line.split(): - if flag in X86_EXIT_REASONS: - x86_init(flag) - return +ARCH = Arch() def walkdir(path): @@ -298,11 +295,10 @@ def get_online_cpus(): def get_filters(): - detect_platform() filters = {} filters['kvm_userspace_exit'] = ('reason', USERSPACE_EXIT_REASONS) - if EXIT_REASONS: - filters['kvm_exit'] = ('exit_reason', EXIT_REASONS) + if ARCH.exit_reasons: + filters['kvm_exit'] = ('exit_reason', ARCH.exit_reasons) return filters libc = ctypes.CDLL('libc.so.6', use_errno=True) @@ -322,9 +318,9 @@ class perf_event_attr(ctypes.Structure): ('bp_len', ctypes.c_uint64), ] def perf_event_open(attr, pid, cpu, group_fd, flags): - return syscall(SC_PERF_EVT_OPEN, ctypes.pointer(attr), ctypes.c_int(pid), - ctypes.c_int(cpu), ctypes.c_int(group_fd), - ctypes.c_long(flags)) + return syscall(ARCH.sc_perf_evt_open, ctypes.pointer(attr), + ctypes.c_int(pid), ctypes.c_int(cpu), + ctypes.c_int(group_fd), ctypes.c_long(flags)) PERF_TYPE_TRACEPOINT = 2 PERF_FORMAT_GROUP = 1 << 3 @@ -380,19 +376,19 @@ class Event(object): 'while calling sys_perf_event_open().') if event_data['filter']: - fcntl.ioctl(fd, IOCTL_NUMBERS['SET_FILTER'], + fcntl.ioctl(fd, ARCH.ioctl_numbers['SET_FILTER'], event_data['filter']) self.fd = fd def enable(self): - fcntl.ioctl(self.fd, IOCTL_NUMBERS['ENABLE'], 0) + fcntl.ioctl(self.fd, ARCH.ioctl_numbers['ENABLE'], 0) def disable(self): - fcntl.ioctl(self.fd, IOCTL_NUMBERS['DISABLE'], 0) + fcntl.ioctl(self.fd, ARCH.ioctl_numbers['DISABLE'], 0) def reset(self): - fcntl.ioctl(self.fd, IOCTL_NUMBERS['RESET'], 0) + fcntl.ioctl(self.fd, ARCH.ioctl_numbers['RESET'], 0) class TracepointProvider(object): def __init__(self): -- 2.3.0