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

[libvirt] [PATCH 01/11] qemu: extract helper to get the current balloon



Refactor the code to extract an helper method
to get the current balloon settings.

Signed-off-by: Francesco Romani <fromani redhat com>
---
 src/qemu/qemu_driver.c | 98 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 38 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 239a300..bbd16ed 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -168,6 +168,9 @@ static int qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid,
                           const char *path, int oflags,
                           bool *needUnlink, bool *bypassSecurityDriver);
 
+static int qemuDomainGetBalloonMemory(virQEMUDriverPtr driver,
+                                      virDomainObjPtr vm,
+                                      unsigned long *memory);
 
 virQEMUDriverPtr qemu_driver = NULL;
 
@@ -2519,6 +2522,60 @@ static int qemuDomainSendKey(virDomainPtr domain,
     return ret;
 }
 
+static int qemuDomainGetBalloonMemory(virQEMUDriverPtr driver,
+                                      virDomainObjPtr vm,
+                                      unsigned long *memory)
+{
+    int ret = -1;
+    int err = 0;
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+
+    if ((vm->def->memballoon != NULL) &&
+        (vm->def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_NONE)) {
+        *memory = vm->def->mem.max_balloon;
+    } else if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BALLOON_EVENT)) {
+        *memory = vm->def->mem.cur_balloon;
+    } else if (qemuDomainJobAllowed(priv, QEMU_JOB_QUERY)) {
+        unsigned long long balloon;
+
+        if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0)
+            goto cleanup;
+        if (!virDomainObjIsActive(vm))
+            err = 0;
+        else {
+            qemuDomainObjEnterMonitor(driver, vm);
+            err = qemuMonitorGetBalloonInfo(priv->mon, &balloon);
+            qemuDomainObjExitMonitor(driver, vm);
+        }
+        if (!qemuDomainObjEndJob(driver, vm)) {
+            vm = NULL;
+            goto cleanup;
+        }
+
+        if (err < 0) {
+            /* We couldn't get current memory allocation but that's not
+             * a show stopper; we wouldn't get it if there was a job
+             * active either
+             */
+            *memory = vm->def->mem.cur_balloon;
+        } else if (err == 0) {
+            /* Balloon not supported, so maxmem is always the allocation */
+            *memory = vm->def->mem.max_balloon;
+        } else {
+            *memory = balloon;
+        }
+    } else {
+        *memory = vm->def->mem.cur_balloon;
+    }
+
+    ret = 0;
+
+ cleanup:
+    if (vm)
+        virObjectUnlock(vm);
+    return ret;
+}
+
 static int qemuDomainGetInfo(virDomainPtr dom,
                              virDomainInfoPtr info)
 {
@@ -2526,7 +2583,6 @@ static int qemuDomainGetInfo(virDomainPtr dom,
     virDomainObjPtr vm;
     int ret = -1;
     int err;
-    unsigned long long balloon;
 
     if (!(vm = qemuDomObjFromDomain(dom)))
         goto cleanup;
@@ -2549,43 +2605,9 @@ static int qemuDomainGetInfo(virDomainPtr dom,
     info->maxMem = vm->def->mem.max_balloon;
 
     if (virDomainObjIsActive(vm)) {
-        qemuDomainObjPrivatePtr priv = vm->privateData;
-
-        if ((vm->def->memballoon != NULL) &&
-            (vm->def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_NONE)) {
-            info->memory = vm->def->mem.max_balloon;
-        } else if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BALLOON_EVENT)) {
-            info->memory = vm->def->mem.cur_balloon;
-        } else if (qemuDomainJobAllowed(priv, QEMU_JOB_QUERY)) {
-            if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0)
-                goto cleanup;
-            if (!virDomainObjIsActive(vm))
-                err = 0;
-            else {
-                qemuDomainObjEnterMonitor(driver, vm);
-                err = qemuMonitorGetBalloonInfo(priv->mon, &balloon);
-                qemuDomainObjExitMonitor(driver, vm);
-            }
-            if (!qemuDomainObjEndJob(driver, vm)) {
-                vm = NULL;
-                goto cleanup;
-            }
-
-            if (err < 0) {
-                /* We couldn't get current memory allocation but that's not
-                 * a show stopper; we wouldn't get it if there was a job
-                 * active either
-                 */
-                info->memory = vm->def->mem.cur_balloon;
-            } else if (err == 0) {
-                /* Balloon not supported, so maxmem is always the allocation */
-                info->memory = vm->def->mem.max_balloon;
-            } else {
-                info->memory = balloon;
-            }
-        } else {
-            info->memory = vm->def->mem.cur_balloon;
-        }
+        err = qemuDomainGetBalloonMemory(driver, vm, &info->memory);
+        if (err)
+            return err;
     } else {
         info->memory = 0;
     }
-- 
1.9.3


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