Openembedded Core Discussions
 help / color / mirror / Atom feed
From: Michael Opdenacker <michael.opdenacker@bootlin.com>
To: openembedded-core@lists.openembedded.org
Cc: Michael Opdenacker <michael.opdenacker@bootlin.com>
Subject: [PATCH] runqemu: automatically turn on "kvm" on x86 CPUs with VT
Date: Thu, 20 Jan 2022 16:21:46 +0100	[thread overview]
Message-ID: <20220120152146.1912559-1-michael.opdenacker@bootlin.com> (raw)

This automatically turns on the "kvm" option when emulating
an x86 system on x86 CPUs with VT capability.

On an Intel i7-5600U CPU at 2.60GHz, using the "kvm"
option is at least 4x faster, booting "core-image-minimal" for qemux86-64.
The performance difference can even be bigger for larger systems.

Rather than changing the documentation to remind users
to use this option, that's easier to enable this option by
default on suitable systems.

A "noautokvm" option is added to disable this default,
and keep the previous behavior.

Signed-off-by: Michael Opdenacker <michael.opdenacker@bootlin.com>
---
 scripts/runqemu | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/scripts/runqemu b/scripts/runqemu
index 4e05c1bb15..b500ba2afa 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -78,8 +78,9 @@ of the following environment variables (in any order):
     serialstdio - enable a serial console on the console (regardless of graphics mode)
     slirp - enable user networking, no root privileges is required
     snapshot - don't write changes to back to images
-    kvm - enable KVM when running x86/x86_64 (VT-capable CPU required)
+    kvm - enable KVM. Enabled by default when running x86/x86_64 with a VT-capable CPU
     kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required)
+    noautokvm - don't enable KVM automatically on x86/x86_64 with a VT-capable CPU
     publicvnc - enable a VNC server open to all hosts
     audio - enable audio
     [*/]ovmf* - OVMF firmware file or base name for booting with UEFI
@@ -170,6 +171,7 @@ class BaseConfig(object):
         self.fstype = ''
         self.kvm_enabled = False
         self.vhost_enabled = False
+        self.noautokvm = False
         self.slirp_enabled = False
         self.net_bridge = None
         self.nfs_instance = 0
@@ -506,6 +508,8 @@ class BaseConfig(object):
                 self.kvm_enabled = True
             elif arg == 'kvm-vhost':
                 self.vhost_enabled = True
+            elif arg == 'noautokvm':
+                self.noautokvm = True
             elif arg == 'slirp':
                 self.slirp_enabled = True
             elif arg.startswith('bridge='):
@@ -550,8 +554,18 @@ class BaseConfig(object):
             if s:
                 self.set("DEPLOY_DIR_IMAGE", s.group(1))
 
+    def kvm_cap_x86(self):
+        with open('/proc/cpuinfo', 'r') as f:
+            return re.search('vmx|svm', "".join(f.readlines()))
+
     def check_kvm(self):
-        """Check kvm and kvm-host"""
+        """Check kvm and kvm-vhost"""
+	# Turn on KVM by default emulating x86 on x86 CPUs with VT
+	# Can be disabled with the "noautokvm" option
+        if self.qemu_system.endswith(('i386', 'x86_64')) and not self.noautokvm:
+            if self.kvm_cap_x86():
+                self.kvm_enabled = True
+
         if not (self.kvm_enabled or self.vhost_enabled):
             self.qemu_opt_script += ' %s %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU'), self.get('QB_SMP'))
             return
@@ -565,9 +579,7 @@ class BaseConfig(object):
         dev_kvm = '/dev/kvm'
         dev_vhost = '/dev/vhost-net'
         if self.qemu_system.endswith(('i386', 'x86_64')):
-            with open('/proc/cpuinfo', 'r') as f:
-                kvm_cap = re.search('vmx|svm', "".join(f.readlines()))
-            if not kvm_cap:
+            if not self.kvm_cap_x86():
                 logger.error("You are trying to enable KVM on a cpu without VT support.")
                 logger.error("Remove kvm from the command-line, or refer:")
                 raise RunQemuError(yocto_kvm_wiki)
-- 
2.25.1



             reply	other threads:[~2022-01-20 15:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-20 15:21 Michael Opdenacker [this message]
2022-01-20 15:28 ` [OE-core] [PATCH] runqemu: automatically turn on "kvm" on x86 CPUs with VT Alexander Kanavin
2022-01-20 18:43   ` Michael Opdenacker
2022-01-20 19:11     ` Alexander Kanavin
2022-01-20 20:46     ` Richard Purdie
2022-01-21 11:43       ` Alexander Kanavin
2022-01-21 13:42         ` Michael Opdenacker
     [not found]       ` <16CC466E2ADD6F99.18230@lists.openembedded.org>
2022-01-21 11:58         ` Alexander Kanavin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220120152146.1912559-1-michael.opdenacker@bootlin.com \
    --to=michael.opdenacker@bootlin.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox