Re: [libvirt] [PATCH] kvm: maxVCPU runtime detection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
On Fri, Aug 22, 2008 at 02:27:58PM +0100, Daniel P. Berrange wrote:
> On Fri, Aug 22, 2008 at 03:16:42PM +0200, Guido G?nther wrote:
> > Hi,
> > with recent linux kernels we can detect the maximum number of virtual
> > cpus at runtime via an ioctl. Possible patch attached. It does this on
> > every call to qemudGetMaxVCPUs. Would you prefer something that does
> > this only once in qemudStartup()?
> > Cheers,
> >  -- Guido
> 
> >  
> > +dnl
> > +dnl check for kvm headers
> > +dnl
> > +AC_CHECK_HEADERS([linux/kvm.h])
> 
> Hmm, I wonder if that's commonly installed by Fedora/Debian RPMs
> for KVM....
> 
> It might be neccessary to just #define the IOCTL constant in
> our own header files if its not available.
Attached patch adds the missing definitions via qemu_driver.h which
seemed close enough to KVM and isn't a public header. This way all
#ifdefs' moved out of qemu_driver.c.
 -- Guido
>From 0da5b6ba3279d80be13b50ba456d2a4e434da7b8 Mon Sep 17 00:00:00 2001
From: Guido Guenther <agx@xxxxxxxxxxx>
Date: Fri, 22 Aug 2008 14:47:49 +0200
Subject: [PATCH] for kvm determine maxVCPUs at runtime

---
 configure.in      |    5 +++++
 src/qemu_driver.c |   24 +++++++++++++++++++++++-
 src/qemu_driver.h |   18 ++++++++++++++++++
 3 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/configure.in b/configure.in
index 430a097..dfb38ea 100644
--- a/configure.in
+++ b/configure.in
@@ -314,6 +314,11 @@ if test "$with_qemu" = "yes" -o "$with_lxc" = "yes" ; then
                    AC_MSG_ERROR([You must install kernel-headers in order to compile libvirt]))
 fi
 
+dnl
+dnl check for kvm headers
+dnl 
+AC_CHECK_HEADERS([linux/kvm.h])
+
 dnl Need to test if pkg-config exists
 PKG_PROG_PKG_CONFIG
 
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index eb4454a..482d988 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -42,6 +42,7 @@
 #include <pwd.h>
 #include <stdio.h>
 #include <sys/wait.h>
+#include <sys/ioctl.h>
 
 #if HAVE_NUMACTL
 #include <numa.h>
@@ -1790,6 +1791,27 @@ static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
     return "QEMU";
 }
 
+
+static int kvmGetMaxVCPUs(void) {
+    int maxvcpus = 1;
+
+    int r, fd;
+    
+    fd = open(KVM_DEVICE, O_RDONLY);
+    if (fd < 0) {
+        qemudLog(QEMUD_WARN, _("Unable to open " KVM_DEVICE ": %s\n"), strerror(errno));
+        return maxvcpus;
+    }
+
+    r = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
+    if (r > 0)
+        maxvcpus = r;
+
+    close(fd);
+    return maxvcpus;
+}
+
+
 static int qemudGetMaxVCPUs(virConnectPtr conn, const char *type) {
     if (!type)
         return 16;
@@ -1800,7 +1822,7 @@ static int qemudGetMaxVCPUs(virConnectPtr conn, const char *type) {
     /* XXX future KVM will support SMP. Need to probe
        kernel to figure out KVM module version i guess */
     if (STRCASEEQ(type, "kvm"))
-        return 1;
+        return kvmGetMaxVCPUs();
 
     if (STRCASEEQ(type, "kqemu"))
         return 1;
diff --git a/src/qemu_driver.h b/src/qemu_driver.h
index dbcca70..e0662e0 100644
--- a/src/qemu_driver.h
+++ b/src/qemu_driver.h
@@ -29,6 +29,24 @@
 
 #include "internal.h"
 
+#if HAVE_LINUX_KVM_H
+#include <linux/kvm.h>
+#endif
+
+/* device for kvm ioctls */
+#define KVM_DEVICE "/dev/kvm"
+
+/* add definitions missing in older linux/kvm.h */
+#ifndef KVMIO
+#  define KVMIO 0xAE
+#endif
+#ifndef KVM_CHECK_EXTENSION
+#  define KVM_CHECK_EXTENSION       _IO(KVMIO,   0x03)
+#endif
+#ifndef KVM_CAP_NR_VCPUS
+#  define KVM_CAP_NR_VCPUS 9       /* returns max vcpus per vm */
+#endif
+
 int qemudRegister(void);
 
 #endif /* QEMUD_DRIVER_H */
-- 
1.5.6.3

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]