[libvirt] [PATCH 02/23] Introduce virCPUProbeHost

Martin Kletzander mkletzan at redhat.com
Fri Mar 24 18:59:50 UTC 2017


Both QEMU and bhyve are using the same function for setting up the CPU
in virCapabilities, so de-duplicate it, save code and time, and help
other drivers adopt it.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/bhyve/bhyve_capabilities.c | 19 ++-----------------
 src/cpu/cpu.c                  | 13 +++++++++++++
 src/cpu/cpu.h                  |  3 +++
 src/libvirt_private.syms       |  1 +
 src/qemu/qemu_capabilities.c   | 15 +--------------
 src/vmware/vmware_conf.c       | 12 +++++-------
 6 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c
index 4bf1d84fafc5..169f3644bcbc 100644
--- a/src/bhyve/bhyve_capabilities.c
+++ b/src/bhyve/bhyve_capabilities.c
@@ -41,21 +41,6 @@

 VIR_LOG_INIT("bhyve.bhyve_capabilities");

-static int
-virBhyveCapsInitCPU(virCapsPtr caps,
-                    virArch arch)
-{
-    virNodeInfo nodeinfo;
-
-    if (nodeGetInfo(&nodeinfo))
-        return -1;
-
-    if (!(caps->host.cpu = virCPUGetHost(arch, VIR_CPU_TYPE_HOST,
-                                         &nodeinfo, NULL, 0)))
-        return -1;
-
-    return 0;
-}

 virCapsPtr
 virBhyveCapsBuild(void)
@@ -77,8 +62,8 @@ virBhyveCapsBuild(void)
                                       NULL, NULL, 0, NULL) == NULL)
         goto error;

-    if (virBhyveCapsInitCPU(caps, virArchFromHost()) < 0)
-        VIR_WARN("Failed to get host CPU: %s", virGetLastErrorMessage());
+    if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch)))
+        VIR_WARN("Failed to get host CPU");

     return caps;

diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index 1461190bac2a..1bbc2b2f849e 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -26,6 +26,7 @@
 #include "virlog.h"
 #include "viralloc.h"
 #include "virxml.h"
+#include "nodeinfo.h"
 #include "cpu.h"
 #include "cpu_map.h"
 #include "cpu_x86.h"
@@ -462,6 +463,18 @@ virCPUGetHost(virArch arch,
 }


+virCPUDefPtr
+virCPUProbeHost(virArch arch)
+{
+    virNodeInfo nodeinfo;
+
+    if (nodeGetInfo(&nodeinfo))
+        return NULL;
+
+    return virCPUGetHost(arch, VIR_CPU_TYPE_HOST, &nodeinfo, NULL, 0);
+}
+
+
 /**
  * cpuBaselineXML:
  *
diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h
index d23409a4f068..c8e6b1846382 100644
--- a/src/cpu/cpu.h
+++ b/src/cpu/cpu.h
@@ -183,6 +183,9 @@ virCPUGetHost(virArch arch,
               const char **models,
               unsigned int nmodels);

+virCPUDefPtr
+virCPUProbeHost(virArch arch);
+
 char *
 cpuBaselineXML(const char **xmlCPUs,
                unsigned int ncpus,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a71b56d7ba9c..7627be9956eb 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1016,6 +1016,7 @@ virCPUDataNew;
 virCPUDataParse;
 virCPUGetHost;
 virCPUGetModels;
+virCPUProbeHost;
 virCPUTranslate;
 virCPUUpdate;
 virCPUUpdateLive;
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 8bd39c729b6b..8e7b20ba187c 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1065,19 +1065,6 @@ virQEMUCapsInitGuestFromBinary(virCapsPtr caps,
 }


-static virCPUDefPtr
-virQEMUCapsProbeHostCPU(virCapsPtr caps)
-{
-    virNodeInfo nodeinfo;
-
-    if (nodeGetInfo(&nodeinfo))
-        return NULL;
-
-    return virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST,
-                         &nodeinfo, NULL, 0);
-}
-
-
 virCPUDefPtr
 virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps,
                                    virQEMUCapsPtr qemuCaps,
@@ -1140,7 +1127,7 @@ virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache)
         VIR_WARN("Failed to query host NUMA topology, disabling NUMA capabilities");
     }

-    if (!(caps->host.cpu = virQEMUCapsProbeHostCPU(caps)))
+    if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch)))
         VIR_WARN("Failed to get host CPU");

     /* Add the power management features of the host */
diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c
index 659c4737a301..0c2b0f4c0be3 100644
--- a/src/vmware/vmware_conf.c
+++ b/src/vmware/vmware_conf.c
@@ -61,7 +61,6 @@ vmwareCapsInit(void)
 {
     virCapsPtr caps = NULL;
     virCapsGuestPtr guest = NULL;
-    virCPUDefPtr cpu = NULL;

     if ((caps = virCapabilitiesNew(virArchFromHost(),
                                    false, false)) == NULL)
@@ -81,9 +80,9 @@ vmwareCapsInit(void)
                                       VIR_DOMAIN_VIRT_VMWARE,
                                       NULL, NULL, 0, NULL) == NULL)
         goto error;
+    guest = NULL;

-    if (!(cpu = virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST,
-                              NULL, NULL, 0)))
+    if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch)))
         goto error;

     /* x86_64 guests are supported if
@@ -92,9 +91,9 @@ vmwareCapsInit(void)
      *  - Host CPU is x86_64 with virtualization extensions
      */
     if (caps->host.arch == VIR_ARCH_X86_64 ||
-        (virCPUCheckFeature(cpu->arch, cpu, "lm") &&
-         (virCPUCheckFeature(cpu->arch, cpu, "vmx") ||
-          virCPUCheckFeature(cpu->arch, cpu, "svm")))) {
+        (virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "lm") &&
+         (virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "vmx") ||
+          virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "svm")))) {

         if ((guest = virCapabilitiesAddGuest(caps,
                                              VIR_DOMAIN_OSTYPE_HVM,
@@ -109,7 +108,6 @@ vmwareCapsInit(void)
     }

  cleanup:
-    virCPUDefFree(cpu);
     return caps;

  error:
-- 
2.12.0




More information about the libvir-list mailing list