]> git.proxmox.com Git - mirror_qemu.git/blob - python/qemu/accel.py
3ec6bdcfdb50c09bda951297ed082f1a7af5a633
[mirror_qemu.git] / python / qemu / accel.py
1 """
2 QEMU accel module:
3
4 This module provides utilities for discover and check the availability of
5 accelerators.
6 """
7 # Copyright (C) 2015-2016 Red Hat Inc.
8 # Copyright (C) 2012 IBM Corp.
9 #
10 # Authors:
11 # Fam Zheng <famz@redhat.com>
12 #
13 # This work is licensed under the terms of the GNU GPL, version 2. See
14 # the COPYING file in the top-level directory.
15 #
16
17 import logging
18 import os
19 import subprocess
20
21
22 LOG = logging.getLogger(__name__)
23
24 # Mapping host architecture to any additional architectures it can
25 # support which often includes its 32 bit cousin.
26 ADDITIONAL_ARCHES = {
27 "x86_64": "i386",
28 "aarch64": "armhf",
29 "ppc64le": "ppc64",
30 }
31
32
33 def list_accel(qemu_bin):
34 """
35 List accelerators enabled in the QEMU binary.
36
37 @param qemu_bin (str): path to the QEMU binary.
38 @raise Exception: if failed to run `qemu -accel help`
39 @return a list of accelerator names.
40 """
41 if not qemu_bin:
42 return []
43 try:
44 out = subprocess.check_output([qemu_bin, '-accel', 'help'],
45 universal_newlines=True)
46 except:
47 LOG.debug("Failed to get the list of accelerators in %s", qemu_bin)
48 raise
49 # Skip the first line which is the header.
50 return [acc.strip() for acc in out.splitlines()[1:]]
51
52
53 def kvm_available(target_arch=None, qemu_bin=None):
54 """
55 Check if KVM is available using the following heuristic:
56 - Kernel module is present in the host;
57 - Target and host arches don't mismatch;
58 - KVM is enabled in the QEMU binary.
59
60 @param target_arch (str): target architecture
61 @param qemu_bin (str): path to the QEMU binary
62 @return True if kvm is available, otherwise False.
63 """
64 if not os.access("/dev/kvm", os.R_OK | os.W_OK):
65 return False
66 if target_arch:
67 host_arch = os.uname()[4]
68 if target_arch != host_arch:
69 if target_arch != ADDITIONAL_ARCHES.get(host_arch):
70 return False
71 if qemu_bin and "kvm" not in list_accel(qemu_bin):
72 return False
73 return True
74
75
76 def tcg_available(qemu_bin):
77 """
78 Check if TCG is available.
79
80 @param qemu_bin (str): path to the QEMU binary
81 """
82 return 'tcg' in list_accel(qemu_bin)