[PATCH 09/13] Introduce qemudDomainPinHypervisorFlags and qemudDomainGetHypervisorPinInfo in qemu driver

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

 



Signed-off-by: Tang Chen <tangchen@xxxxxxxxxxxxxx>
---
 src/driver.h           |   13 +++-
 src/qemu/qemu_driver.c |  223 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 235 insertions(+), 1 deletions(-)

diff --git a/src/driver.h b/src/driver.h
index aa7a377..dab21ca 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -296,7 +296,16 @@ typedef int
                                          unsigned char *cpumaps,
                                          int maplen,
                                          unsigned int flags);
-
+typedef int
+        (*virDrvDomainPinHypervisorFlags)     (virDomainPtr domain,
+                                               unsigned char *cpumap,
+                                               int maplen,
+                                               unsigned int flags);
+typedef int
+        (*virDrvDomainGetHypervisorPinInfo)   (virDomainPtr domain,
+                                               unsigned char *cpumaps,
+                                               int maplen,
+                                               unsigned int flags);
 typedef int
         (*virDrvDomainGetVcpus)		(virDomainPtr domain,
                                          virVcpuInfoPtr info,
@@ -908,6 +917,8 @@ struct _virDriver {
     virDrvDomainPinVcpu		domainPinVcpu;
     virDrvDomainPinVcpuFlags    domainPinVcpuFlags;
     virDrvDomainGetVcpuPinInfo  domainGetVcpuPinInfo;
+    virDrvDomainPinHypervisorFlags    domainPinHypervisorFlags;
+    virDrvDomainGetHypervisorPinInfo  domainGetHypervisorPinInfo;
     virDrvDomainGetVcpus		domainGetVcpus;
     virDrvDomainGetMaxVcpus		domainGetMaxVcpus;
     virDrvDomainGetSecurityLabel     domainGetSecurityLabel;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index b0eef80..312b58b 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3788,6 +3788,227 @@ cleanup:
 }
 
 static int
+qemudDomainPinHypervisorFlags(virDomainPtr dom,
+                              unsigned char *cpumap,
+                              int maplen,
+                              unsigned int flags)
+{
+    struct qemud_driver *driver = dom->conn->privateData;
+    virDomainObjPtr vm;
+    virCgroupPtr cgroup_dom = NULL;
+    virCgroupPtr cgroup_hypervisor = NULL;
+    pid_t pid;
+    virDomainDefPtr persistentDef = NULL;
+    int maxcpu, hostcpus;
+    virNodeInfo nodeinfo;
+    int ret = -1;
+    qemuDomainObjPrivatePtr priv;
+    bool canResetting = true;
+    int pcpu;
+
+    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
+                  VIR_DOMAIN_AFFECT_CONFIG, -1);
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    qemuDriverUnlock(driver);
+
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        qemuReportError(VIR_ERR_NO_DOMAIN,
+                        _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (virDomainLiveConfigHelperMethod(driver->caps, vm, &flags,
+                                        &persistentDef) < 0)
+        goto cleanup;
+
+    priv = vm->privateData;
+
+    if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
+        goto cleanup;
+    hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
+    maxcpu = maplen * 8;
+    if (maxcpu > hostcpus)
+        maxcpu = hostcpus;
+    /* pinning to all physical cpus means resetting,
+     * so check if we can reset setting.
+     */
+    for (pcpu = 0; pcpu < hostcpus; pcpu++) {
+        if ((cpumap[pcpu/8] & (1 << (pcpu % 8))) == 0) {
+            canResetting = false;
+            break;
+        }
+    }
+
+    pid = vm->pid;
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+
+        if (priv->vcpupids != NULL) {
+            if (virDomainHypervisorPinAdd(vm->def, cpumap, maplen) < 0) {
+                qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                _("failed to update or add hypervisorpin xml "
+                                  "of a running domain"));
+                goto cleanup;
+            }
+
+            if (qemuCgroupControllerActive(driver,
+                                           VIR_CGROUP_CONTROLLER_CPUSET)) {
+                /*
+                 * Configure the corresponding cpuset cgroup.
+                 * If no cgroup for domain or hypervisor exists, do nothing.
+                 */
+                if (virCgroupForDomain(driver->cgroup, vm->def->name,
+                                       &cgroup_dom, 0) == 0) {
+                    if (virCgroupForHypervisor(cgroup_dom, &cgroup_hypervisor, 0) == 0) {
+                        if (qemuSetupCgroupHypervisorPin(cgroup_hypervisor, vm->def) < 0) {
+                            qemuReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                                            _("failed to set cpuset.cpus in cgroup"
+                                              " for hypervisor threads"));
+                            goto cleanup;
+                        }
+                    }
+                }
+            }
+
+            if (canResetting) {
+                if (virDomainHypervisorPinDel(vm->def) < 0) {
+                    qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                    _("failed to delete hypervisorpin xml of "
+                                      "a running domain"));
+                    goto cleanup;
+                }
+            }
+
+            if (virProcessInfoSetAffinity(pid, cpumap, maplen, maxcpu) < 0) {
+                qemuReportError(VIR_ERR_SYSTEM_ERROR, "%s",
+                                _("failed to set cpu affinity for "
+                                  "hypervisor threads"));
+                goto cleanup;
+            }
+        } else {
+            qemuReportError(VIR_ERR_OPERATION_INVALID,
+                            "%s", _("cpu affinity is not supported"));
+            goto cleanup;
+        }
+
+        if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
+            goto cleanup;
+    }
+
+    if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+
+        if (canResetting) {
+            if (virDomainHypervisorPinDel(persistentDef) < 0) {
+                qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                _("failed to delete hypervisorpin xml of "
+                                  "a persistent domain"));
+                goto cleanup;
+            }
+        } else {
+            if (virDomainHypervisorPinAdd(persistentDef, cpumap, maplen) < 0) {
+                qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                _("failed to update or add hypervisorpin xml "
+                                  "of a persistent domain"));
+                goto cleanup;
+            }
+        }
+
+        ret = virDomainSaveConfig(driver->configDir, persistentDef);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    if (cgroup_hypervisor)
+        virCgroupFree(&cgroup_hypervisor);
+    if (cgroup_dom)
+        virCgroupFree(&cgroup_dom);
+
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+}
+
+static int
+qemudDomainGetHypervisorPinInfo(virDomainPtr dom,
+                                unsigned char *cpumaps,
+                                int maplen,
+                                unsigned int flags)
+{
+    struct qemud_driver *driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virNodeInfo nodeinfo;
+    virDomainDefPtr targetDef = NULL;
+    int ret = -1;
+    int maxcpu, hostcpus, pcpu;
+    virDomainVcpuPinDefPtr hypervisorpin = NULL;
+    char *cpumask = NULL;
+
+    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
+                  VIR_DOMAIN_AFFECT_CONFIG, -1);
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    qemuDriverUnlock(driver);
+
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        qemuReportError(VIR_ERR_NO_DOMAIN,
+                        _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (virDomainLiveConfigHelperMethod(driver->caps, vm, &flags,
+                                        &targetDef) < 0)
+        goto cleanup;
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE)
+        targetDef = vm->def;
+
+    /* Coverity didn't realize that targetDef must be set if we got here. */
+    sa_assert(targetDef);
+
+    if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
+        goto cleanup;
+    hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
+    maxcpu = maplen * 8;
+    if (maxcpu > hostcpus)
+        maxcpu = hostcpus;
+
+    /* initialize cpumaps */
+    memset(cpumaps, 0xff, maplen);
+    if (maxcpu % 8) {
+        cpumaps[maplen - 1] &= (1 << maxcpu % 8) - 1;
+    }
+
+    /* If no hypervisorpin, all cpus should be used */
+    hypervisorpin = targetDef->cputune.hypervisorpin;
+    if (!hypervisorpin) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    cpumask = hypervisorpin->cpumask;
+    for (pcpu = 0; pcpu < maxcpu; pcpu++) {
+        if (cpumask[pcpu] == 0)
+            VIR_UNUSE_CPU(cpumaps, pcpu);
+    }
+
+    ret = 1;
+
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+}
+
+static int
 qemudDomainGetVcpus(virDomainPtr dom,
                     virVcpuInfoPtr info,
                     int maxinfo,
@@ -13026,6 +13247,8 @@ static virDriver qemuDriver = {
     .domainPinVcpu = qemudDomainPinVcpu, /* 0.4.4 */
     .domainPinVcpuFlags = qemudDomainPinVcpuFlags, /* 0.9.3 */
     .domainGetVcpuPinInfo = qemudDomainGetVcpuPinInfo, /* 0.9.3 */
+    .domainPinHypervisorFlags = qemudDomainPinHypervisorFlags, /* 0.9.12 */
+    .domainGetHypervisorPinInfo = qemudDomainGetHypervisorPinInfo, /* 0.9.12 */
     .domainGetVcpus = qemudDomainGetVcpus, /* 0.4.4 */
     .domainGetMaxVcpus = qemudDomainGetMaxVcpus, /* 0.4.4 */
     .domainGetSecurityLabel = qemudDomainGetSecurityLabel, /* 0.6.1 */
-- 
1.7.3.1

--
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]