[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 25/34] scripts/kvm/kvm_stat: Group arch specific dat
From: |
Janosch Frank |
Subject: |
[Qemu-devel] [PATCH 25/34] scripts/kvm/kvm_stat: Group arch specific data |
Date: |
Thu, 10 Dec 2015 13:12:55 +0100 |
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
- [Qemu-devel] [PATCH 06/34] scripts/kvm/kvm_stat: Invert dictionaries, (continued)
- [Qemu-devel] [PATCH 06/34] scripts/kvm/kvm_stat: Invert dictionaries, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 15/34] scripts/kvm/kvm_stat: Cleanup of platform detection, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 19/34] scripts/kvm/kvm_stat: Cleanup of TracepointProvider, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 31/34] scripts/kvm/kvm_stat: Read event values as u64, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 30/34] scripts/kvm/kvm_stat: Cleanup and pre-init perf_event_attr, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 28/34] scripts/kvm/kvm_stat: Fix output formatting, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 08/34] scripts/kvm/kvm_stat: Improve debugfs access checking, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 20/34] scripts/kvm/kvm_stat: Cleanup cpu list retrieval, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 09/34] scripts/kvm/kvm_stat: Introduce main function, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 07/34] scripts/kvm/kvm_stat: Cleanup of path variables, Janosch Frank, 2015/12/10
- [Qemu-devel] [PATCH 25/34] scripts/kvm/kvm_stat: Group arch specific data,
Janosch Frank <=
- [Qemu-devel] [PATCH 04/34] scripts/kvm/kvm_stat: Removed unneeded PERF constants, Janosch Frank, 2015/12/10
- Re: [Qemu-devel] [PATCH 00/34] kvm_stat: Cleanup and fixup, Cornelia Huck, 2015/12/15