Introduce virCPUProbeHost

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@redhat.com>
This commit is contained in:
Martin Kletzander 2017-03-15 09:07:38 +01:00
parent 532788a840
commit 272d78a5ef
6 changed files with 25 additions and 38 deletions

View File

@ -41,21 +41,6 @@
VIR_LOG_INIT("bhyve.bhyve_capabilities"); 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 virCapsPtr
virBhyveCapsBuild(void) virBhyveCapsBuild(void)
@ -77,8 +62,8 @@ virBhyveCapsBuild(void)
NULL, NULL, 0, NULL) == NULL) NULL, NULL, 0, NULL) == NULL)
goto error; goto error;
if (virBhyveCapsInitCPU(caps, virArchFromHost()) < 0) if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch)))
VIR_WARN("Failed to get host CPU: %s", virGetLastErrorMessage()); VIR_WARN("Failed to get host CPU");
return caps; return caps;

View File

@ -26,6 +26,7 @@
#include "virlog.h" #include "virlog.h"
#include "viralloc.h" #include "viralloc.h"
#include "virxml.h" #include "virxml.h"
#include "nodeinfo.h"
#include "cpu.h" #include "cpu.h"
#include "cpu_map.h" #include "cpu_map.h"
#include "cpu_x86.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: * cpuBaselineXML:
* *

View File

@ -183,6 +183,9 @@ virCPUGetHost(virArch arch,
const char **models, const char **models,
unsigned int nmodels); unsigned int nmodels);
virCPUDefPtr
virCPUProbeHost(virArch arch);
char * char *
cpuBaselineXML(const char **xmlCPUs, cpuBaselineXML(const char **xmlCPUs,
unsigned int ncpus, unsigned int ncpus,

View File

@ -1018,6 +1018,7 @@ virCPUDataNew;
virCPUDataParse; virCPUDataParse;
virCPUGetHost; virCPUGetHost;
virCPUGetModels; virCPUGetModels;
virCPUProbeHost;
virCPUTranslate; virCPUTranslate;
virCPUUpdate; virCPUUpdate;
virCPUUpdateLive; virCPUUpdateLive;

View File

@ -1067,19 +1067,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 virCPUDefPtr
virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps, virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
@ -1142,7 +1129,7 @@ virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache)
VIR_WARN("Failed to query host NUMA topology, disabling NUMA capabilities"); 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"); VIR_WARN("Failed to get host CPU");
/* Add the power management features of the host */ /* Add the power management features of the host */

View File

@ -61,7 +61,6 @@ vmwareCapsInit(void)
{ {
virCapsPtr caps = NULL; virCapsPtr caps = NULL;
virCapsGuestPtr guest = NULL; virCapsGuestPtr guest = NULL;
virCPUDefPtr cpu = NULL;
if ((caps = virCapabilitiesNew(virArchFromHost(), if ((caps = virCapabilitiesNew(virArchFromHost(),
false, false)) == NULL) false, false)) == NULL)
@ -81,9 +80,9 @@ vmwareCapsInit(void)
VIR_DOMAIN_VIRT_VMWARE, VIR_DOMAIN_VIRT_VMWARE,
NULL, NULL, 0, NULL) == NULL) NULL, NULL, 0, NULL) == NULL)
goto error; goto error;
guest = NULL;
if (!(cpu = virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST, if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch)))
NULL, NULL, 0)))
goto error; goto error;
/* x86_64 guests are supported if /* x86_64 guests are supported if
@ -92,9 +91,9 @@ vmwareCapsInit(void)
* - Host CPU is x86_64 with virtualization extensions * - Host CPU is x86_64 with virtualization extensions
*/ */
if (caps->host.arch == VIR_ARCH_X86_64 || if (caps->host.arch == VIR_ARCH_X86_64 ||
(virCPUCheckFeature(cpu->arch, cpu, "lm") && (virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "lm") &&
(virCPUCheckFeature(cpu->arch, cpu, "vmx") || (virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "vmx") ||
virCPUCheckFeature(cpu->arch, cpu, "svm")))) { virCPUCheckFeature(caps->host.cpu->arch, caps->host.cpu, "svm")))) {
if ((guest = virCapabilitiesAddGuest(caps, if ((guest = virCapabilitiesAddGuest(caps,
VIR_DOMAIN_OSTYPE_HVM, VIR_DOMAIN_OSTYPE_HVM,
@ -109,7 +108,6 @@ vmwareCapsInit(void)
} }
cleanup: cleanup:
virCPUDefFree(cpu);
return caps; return caps;
error: error: