qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/5] python/qemu: New accel module and improvements
@ 2019-12-16 19:14 Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 1/5] python/qemu: Move kvm_available() to its own module Wainer dos Santos Moschetta
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

On commit abf0bf998dcb John Snow moved some code out of __init__.py
to machine.py. kvm_available() remained in though. So on patch 01
I continue his work by creating a home for that method (the new
'accel' module). Honestly I was unsure about whether move the code
to any existing module or make a new, but since I am adding more
methods related with accelerators then I thought they would deserve a
module.

The patches 02-04 introduce new helpers and make improvements. Later
I intend to use those methods on the acceptance tests such as
to automatically set the accelerator in QEMUMachine VM via Avocado
tags, and skip the test if the accelerator is not available.

Patch 05 just remove unneeded imports in __init__.py

Changes v3 -> v4:
- Cleaned up Review-by statements (patch 02) [crosa]
- Let LOG.debug() format the log message (patch 02) [crosa]

Changes v2 -> v3:
- Refactor subprocess.check_output() call (patch 02) [crosa]
Not using shell=True
Pass universal_newlines=True so don't need to decode() the output
Do not check if returned accelerator's name is empty string
- New patch 05 [crosa]
On patch 01 Cleber suggested to remove unneeded imports in
python/qemu/__init__.py

Changes v1 -> v2:
- Removed 'Based on qmp.py' from python/qemu/accel.py
(patch 01) [alex.bennee]
- logging added only when used on python/qemu/accel.py
(patch 02) [alex.bennee]

Git:
- Tree: https://github.com/wainersm/qemu
- Branch: python_accel_v4

CI:
- Travis (RUNNING): https://travis-ci.org/wainersm/qemu/builds/625827750


Wainer dos Santos Moschetta (5):
  python/qemu: Move kvm_available() to its own module
  python/qemu: accel: Add list_accel() method
  python/qemu: accel: Strengthen kvm_available() checks
  python/qemu: accel: Add tcg_available() method
  python/qemu: Remove unneeded imports in __init__

 python/qemu/__init__.py | 24 -------------
 python/qemu/accel.py    | 77 +++++++++++++++++++++++++++++++++++++++++
 tests/vm/basevm.py      |  2 +-
 3 files changed, 78 insertions(+), 25 deletions(-)
 create mode 100644 python/qemu/accel.py

-- 
2.23.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4 1/5] python/qemu: Move kvm_available() to its own module
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
@ 2019-12-16 19:14 ` Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 2/5] python/qemu: accel: Add list_accel() method Wainer dos Santos Moschetta
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

This creates the 'accel' Python module to be the home for
utilities that deal with accelerators. Also moved kvm_available()
from __init__.py to this new module.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 python/qemu/__init__.py | 20 +-------------------
 python/qemu/accel.py    | 31 +++++++++++++++++++++++++++++++
 tests/vm/basevm.py      |  2 +-
 3 files changed, 33 insertions(+), 20 deletions(-)
 create mode 100644 python/qemu/accel.py

diff --git a/python/qemu/__init__.py b/python/qemu/__init__.py
index 6c919a3d56..eff17a306e 100644
--- a/python/qemu/__init__.py
+++ b/python/qemu/__init__.py
@@ -12,24 +12,6 @@
 # Based on qmp.py.
 #
 
-import logging
-import os
-
 from . import qmp
 from . import machine
-
-LOG = logging.getLogger(__name__)
-
-# Mapping host architecture to any additional architectures it can
-# support which often includes its 32 bit cousin.
-ADDITIONAL_ARCHES = {
-    "x86_64" : "i386",
-    "aarch64" : "armhf"
-}
-
-def kvm_available(target_arch=None):
-    host_arch = os.uname()[4]
-    if target_arch and target_arch != host_arch:
-        if target_arch != ADDITIONAL_ARCHES.get(host_arch):
-            return False
-    return os.access("/dev/kvm", os.R_OK | os.W_OK)
+from . import accel
diff --git a/python/qemu/accel.py b/python/qemu/accel.py
new file mode 100644
index 0000000000..cbeac10dd1
--- /dev/null
+++ b/python/qemu/accel.py
@@ -0,0 +1,31 @@
+"""
+QEMU accel module:
+
+This module provides utilities for discover and check the availability of
+accelerators.
+"""
+# Copyright (C) 2015-2016 Red Hat Inc.
+# Copyright (C) 2012 IBM Corp.
+#
+# Authors:
+#  Fam Zheng <famz@redhat.com>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory.
+#
+
+import os
+
+# Mapping host architecture to any additional architectures it can
+# support which often includes its 32 bit cousin.
+ADDITIONAL_ARCHES = {
+    "x86_64" : "i386",
+    "aarch64" : "armhf"
+}
+
+def kvm_available(target_arch=None):
+    host_arch = os.uname()[4]
+    if target_arch and target_arch != host_arch:
+        if target_arch != ADDITIONAL_ARCHES.get(host_arch):
+            return False
+    return os.access("/dev/kvm", os.R_OK | os.W_OK)
diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
index 91a9226026..3e2b69c96c 100755
--- a/tests/vm/basevm.py
+++ b/tests/vm/basevm.py
@@ -21,7 +21,7 @@ import logging
 import time
 import datetime
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
-from qemu import kvm_available
+from qemu.accel import kvm_available
 from qemu.machine import QEMUMachine
 import subprocess
 import hashlib
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4 2/5] python/qemu: accel: Add list_accel() method
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 1/5] python/qemu: Move kvm_available() to its own module Wainer dos Santos Moschetta
@ 2019-12-16 19:14 ` Wainer dos Santos Moschetta
  2019-12-16 23:31   ` Cleber Rosa
  2019-12-16 19:14 ` [PATCH v4 3/5] python/qemu: accel: Strengthen kvm_available() checks Wainer dos Santos Moschetta
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

Since commit cbe6d6365a48 the command `qemu -accel help` returns
the list of accelerators enabled in the QEMU binary. This adds
the list_accel() method which return that same list.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
---
 python/qemu/accel.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/python/qemu/accel.py b/python/qemu/accel.py
index cbeac10dd1..7395cee793 100644
--- a/python/qemu/accel.py
+++ b/python/qemu/accel.py
@@ -14,7 +14,11 @@ accelerators.
 # the COPYING file in the top-level directory.
 #
 
+import logging
 import os
+import subprocess
+
+LOG = logging.getLogger(__name__)
 
 # Mapping host architecture to any additional architectures it can
 # support which often includes its 32 bit cousin.
@@ -23,6 +27,25 @@ ADDITIONAL_ARCHES = {
     "aarch64" : "armhf"
 }
 
+def list_accel(qemu_bin):
+    """
+    List accelerators enabled in the QEMU binary.
+
+    @param qemu_bin (str): path to the QEMU binary.
+    @raise Exception: if failed to run `qemu -accel help`
+    @return a list of accelerator names.
+    """
+    if not qemu_bin:
+        return []
+    try:
+        out = subprocess.check_output([qemu_bin, '-accel', 'help'],
+                                      universal_newlines=True)
+    except:
+        LOG.debug("Failed to get the list of accelerators in %s", qemu_bin)
+        raise
+    # Skip the first line which is the header.
+    return [acc.strip() for acc in out.splitlines()[1:]]
+
 def kvm_available(target_arch=None):
     host_arch = os.uname()[4]
     if target_arch and target_arch != host_arch:
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4 3/5] python/qemu: accel: Strengthen kvm_available() checks
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 1/5] python/qemu: Move kvm_available() to its own module Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 2/5] python/qemu: accel: Add list_accel() method Wainer dos Santos Moschetta
@ 2019-12-16 19:14 ` Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 4/5] python/qemu: accel: Add tcg_available() method Wainer dos Santos Moschetta
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

Currently kvm_available() checks for the presence of kvm module
and, if target and host arches don't mismatch. This patch adds
an 3rd checking: if QEMU binary was compiled with kvm
support.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Cleber Rosa <crosa@redhat.com>
---
 python/qemu/accel.py | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/python/qemu/accel.py b/python/qemu/accel.py
index 7395cee793..5fce3aa3dd 100644
--- a/python/qemu/accel.py
+++ b/python/qemu/accel.py
@@ -46,9 +46,24 @@ def list_accel(qemu_bin):
     # Skip the first line which is the header.
     return [acc.strip() for acc in out.splitlines()[1:]]
 
-def kvm_available(target_arch=None):
-    host_arch = os.uname()[4]
-    if target_arch and target_arch != host_arch:
-        if target_arch != ADDITIONAL_ARCHES.get(host_arch):
-            return False
-    return os.access("/dev/kvm", os.R_OK | os.W_OK)
+def kvm_available(target_arch=None, qemu_bin=None):
+    """
+    Check if KVM is available using the following heuristic:
+      - Kernel module is present in the host;
+      - Target and host arches don't mismatch;
+      - KVM is enabled in the QEMU binary.
+
+    @param target_arch (str): target architecture
+    @param qemu_bin (str): path to the QEMU binary
+    @return True if kvm is available, otherwise False.
+    """
+    if not os.access("/dev/kvm", os.R_OK | os.W_OK):
+        return False
+    if target_arch:
+        host_arch = os.uname()[4]
+        if target_arch != host_arch:
+            if target_arch != ADDITIONAL_ARCHES.get(host_arch):
+                return False
+    if qemu_bin and "kvm" not in list_accel(qemu_bin):
+        return False
+    return True
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4 4/5] python/qemu: accel: Add tcg_available() method
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
                   ` (2 preceding siblings ...)
  2019-12-16 19:14 ` [PATCH v4 3/5] python/qemu: accel: Strengthen kvm_available() checks Wainer dos Santos Moschetta
@ 2019-12-16 19:14 ` Wainer dos Santos Moschetta
  2019-12-16 19:14 ` [PATCH v4 5/5] python/qemu: Remove unneeded imports in __init__ Wainer dos Santos Moschetta
  2019-12-16 23:44 ` [PATCH v4 0/5] python/qemu: New accel module and improvements Cleber Rosa
  5 siblings, 0 replies; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

This adds a method to check if the tcg accelerator is enabled
in the QEMU binary.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Cleber Rosa <crosa@redhat.com>
---
 python/qemu/accel.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/python/qemu/accel.py b/python/qemu/accel.py
index 5fce3aa3dd..0b38ddf0ab 100644
--- a/python/qemu/accel.py
+++ b/python/qemu/accel.py
@@ -67,3 +67,11 @@ def kvm_available(target_arch=None, qemu_bin=None):
     if qemu_bin and "kvm" not in list_accel(qemu_bin):
         return False
     return True
+
+def tcg_available(qemu_bin):
+    """
+    Check if TCG is available.
+
+    @param qemu_bin (str): path to the QEMU binary
+    """
+    return 'tcg' in list_accel(qemu_bin)
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4 5/5] python/qemu: Remove unneeded imports in __init__
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
                   ` (3 preceding siblings ...)
  2019-12-16 19:14 ` [PATCH v4 4/5] python/qemu: accel: Add tcg_available() method Wainer dos Santos Moschetta
@ 2019-12-16 19:14 ` Wainer dos Santos Moschetta
  2019-12-16 23:44 ` [PATCH v4 0/5] python/qemu: New accel module and improvements Cleber Rosa
  5 siblings, 0 replies; 8+ messages in thread
From: Wainer dos Santos Moschetta @ 2019-12-16 19:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: fam, ehabkost, philmd, jsnow, crosa, alex.bennee

__init_.py import some sub-modules unnecessarily. So let's
clean it up.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Suggested-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
Tested-by: Cleber Rosa <crosa@redhat.com>
---
 python/qemu/__init__.py | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/python/qemu/__init__.py b/python/qemu/__init__.py
index eff17a306e..4ca06c34a4 100644
--- a/python/qemu/__init__.py
+++ b/python/qemu/__init__.py
@@ -9,9 +9,3 @@
 # This work is licensed under the terms of the GNU GPL, version 2.  See
 # the COPYING file in the top-level directory.
 #
-# Based on qmp.py.
-#
-
-from . import qmp
-from . import machine
-from . import accel
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v4 2/5] python/qemu: accel: Add list_accel() method
  2019-12-16 19:14 ` [PATCH v4 2/5] python/qemu: accel: Add list_accel() method Wainer dos Santos Moschetta
@ 2019-12-16 23:31   ` Cleber Rosa
  0 siblings, 0 replies; 8+ messages in thread
From: Cleber Rosa @ 2019-12-16 23:31 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: fam, ehabkost, philmd, qemu-devel, jsnow, alex.bennee

[-- Attachment #1: Type: text/plain, Size: 430 bytes --]

On Mon, Dec 16, 2019 at 04:14:35PM -0300, Wainer dos Santos Moschetta wrote:
> Since commit cbe6d6365a48 the command `qemu -accel help` returns
> the list of accelerators enabled in the QEMU binary. This adds
> the list_accel() method which return that same list.
> 
> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>

Reviewed-by: Cleber Rosa <crosa@redhat.com>
Tested-by: Cleber Rosa <crosa@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v4 0/5] python/qemu: New accel module and improvements
  2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
                   ` (4 preceding siblings ...)
  2019-12-16 19:14 ` [PATCH v4 5/5] python/qemu: Remove unneeded imports in __init__ Wainer dos Santos Moschetta
@ 2019-12-16 23:44 ` Cleber Rosa
  5 siblings, 0 replies; 8+ messages in thread
From: Cleber Rosa @ 2019-12-16 23:44 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: fam, ehabkost, philmd, qemu-devel, jsnow, alex.bennee

[-- Attachment #1: Type: text/plain, Size: 911 bytes --]

On Mon, Dec 16, 2019 at 04:14:33PM -0300, Wainer dos Santos Moschetta wrote:
> On commit abf0bf998dcb John Snow moved some code out of __init__.py
> to machine.py. kvm_available() remained in though. So on patch 01
> I continue his work by creating a home for that method (the new
> 'accel' module). Honestly I was unsure about whether move the code
> to any existing module or make a new, but since I am adding more
> methods related with accelerators then I thought they would deserve a
> module.
> 
> The patches 02-04 introduce new helpers and make improvements. Later
> I intend to use those methods on the acceptance tests such as
> to automatically set the accelerator in QEMUMachine VM via Avocado
> tags, and skip the test if the accelerator is not available.
> 
> Patch 05 just remove unneeded imports in __init__.py
>

FIY, queued all patches on my python-next branch.

- Cleber.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-12-16 23:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-16 19:14 [PATCH v4 0/5] python/qemu: New accel module and improvements Wainer dos Santos Moschetta
2019-12-16 19:14 ` [PATCH v4 1/5] python/qemu: Move kvm_available() to its own module Wainer dos Santos Moschetta
2019-12-16 19:14 ` [PATCH v4 2/5] python/qemu: accel: Add list_accel() method Wainer dos Santos Moschetta
2019-12-16 23:31   ` Cleber Rosa
2019-12-16 19:14 ` [PATCH v4 3/5] python/qemu: accel: Strengthen kvm_available() checks Wainer dos Santos Moschetta
2019-12-16 19:14 ` [PATCH v4 4/5] python/qemu: accel: Add tcg_available() method Wainer dos Santos Moschetta
2019-12-16 19:14 ` [PATCH v4 5/5] python/qemu: Remove unneeded imports in __init__ Wainer dos Santos Moschetta
2019-12-16 23:44 ` [PATCH v4 0/5] python/qemu: New accel module and improvements Cleber Rosa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).