From 6f19c16c789db8b1c077ab2dc28344ce1ddb30d8 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 23 Sep 2009 12:29:39 +0100 Subject: [PATCH] Add API for running 'info balloon' monitor command * src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h: Pull old qemudDomainGetMemoryBalloon() code into a new method called qemuMonitorGetBalloonInfo() * src/qemu/qemu_driver.c: Update to call qemuMonitorGetBalloonInfo() and remove qemudDomainGetMemoryBalloon(). --- src/qemu/qemu_driver.c | 62 ++++++------------------------------ src/qemu/qemu_monitor_text.c | 42 ++++++++++++++++++++++++ src/qemu/qemu_monitor_text.h | 2 ++ 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index db8e0d2590..8e09e0c839 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -2999,53 +2999,6 @@ cleanup: } -/* The reply from QEMU contains 'ballon: actual=421' where value is in MB */ -#define BALLOON_PREFIX "balloon: actual=" - -/* - * Returns: 0 if balloon not supported, +1 if balloon query worked - * or -1 on failure - */ -static int qemudDomainGetMemoryBalloon(virConnectPtr conn, - virDomainObjPtr vm, - unsigned long *currmem) { - char *reply = NULL; - int ret = -1; - char *offset; - - if (!virDomainIsActive(vm)) - return 0; - - if (qemudMonitorCommand(vm, "info balloon", &reply) < 0) { - qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED, - "%s", _("could not query memory balloon allocation")); - goto cleanup; - } - - DEBUG ("%s: balloon reply: '%s'", vm->def->name, reply); - if ((offset = strstr(reply, BALLOON_PREFIX)) != NULL) { - unsigned int memMB; - char *end; - offset += strlen(BALLOON_PREFIX); - if (virStrToLong_ui(offset, &end, 10, &memMB) < 0) { - qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED, - "%s", _("could not parse memory balloon allocation")); - goto cleanup; - } - *currmem = memMB * 1024; - ret = 1; - } else { - /* We don't raise an error here, since its to be expected that - * many QEMU's don't support ballooning - */ - ret = 0; - } - -cleanup: - VIR_FREE(reply); - return ret; -} - /* * Returns: 0 if balloon not supported, +1 if balloon query worked * or -1 on failure @@ -3161,7 +3114,7 @@ static int qemudDomainGetInfo(virDomainPtr dom, info->maxMem = vm->def->maxmem; if (virDomainIsActive(vm)) { - err = qemudDomainGetMemoryBalloon(dom->conn, vm, &balloon); + err = qemuMonitorGetBalloonInfo(vm, &balloon); if (err < 0) goto cleanup; @@ -4121,11 +4074,14 @@ static char *qemudDomainDumpXML(virDomainPtr dom, } /* Refresh current memory based on balloon info */ - err = qemudDomainGetMemoryBalloon(dom->conn, vm, &balloon); - if (err < 0) - goto cleanup; - if (err > 0) - vm->def->memory = balloon; + if (virDomainIsActive(vm)) { + err = qemuMonitorGetBalloonInfo(vm, &balloon); + if (err < 0) + goto cleanup; + if (err > 0) + vm->def->memory = balloon; + /* err == 0 indicates no balloon support, so ignore it */ + } ret = virDomainDefFormat(dom->conn, (flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ? diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c index 47843e81d0..518b7c826a 100644 --- a/src/qemu/qemu_monitor_text.c +++ b/src/qemu/qemu_monitor_text.c @@ -547,6 +547,48 @@ error: } + +/* The reply from QEMU contains 'ballon: actual=421' where value is in MB */ +#define BALLOON_PREFIX "balloon: actual=" + +int qemuMonitorGetBalloonInfo(const virDomainObjPtr vm, + unsigned long *currmem) +{ + char *reply = NULL; + int ret = -1; + char *offset; + + if (qemudMonitorCommand(vm, "info balloon", &reply) < 0) { + qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, + "%s", _("could not query memory balloon allocation")); + return -1; + } + + DEBUG ("%s: balloon reply: '%s'", vm->def->name, reply); + if ((offset = strstr(reply, BALLOON_PREFIX)) != NULL) { + unsigned int memMB; + char *end; + offset += strlen(BALLOON_PREFIX); + if (virStrToLong_ui(offset, &end, 10, &memMB) < 0) { + qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, + _("could not parse memory balloon allocation from '%s'"), reply); + goto cleanup; + } + *currmem = memMB * 1024; + ret = 1; + } else { + /* We don't raise an error here, since its to be expected that + * many QEMU's don't support ballooning + */ + ret = 0; + } + +cleanup: + VIR_FREE(reply); + return ret; +} + + int qemuMonitorSetVNCPassword(const virDomainObjPtr vm, const char *password) { diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h index 80c21d8f03..30a69cf9f1 100644 --- a/src/qemu/qemu_monitor_text.h +++ b/src/qemu/qemu_monitor_text.h @@ -73,6 +73,8 @@ int qemuMonitorSystemPowerdown(const virDomainObjPtr vm); int qemuMonitorGetCPUInfo(const virDomainObjPtr vm, int **pids); +int qemuMonitorGetBalloonInfo(const virDomainObjPtr vm, + unsigned long *currmem); int qemuMonitorSetVNCPassword(const virDomainObjPtr vm, const char *password); -- GitLab