X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=balloon.c;h=e321f2c6886dae5c96bc7a38c4ffa0f45590d45e;hb=d15de15ca01fa990544b015fb972f2d04ab4d2d0;hp=f56fdc1c4b5018d1cf3fec1132e122d78e6940c5;hpb=e06516f565dca3636a4cbef770b191a5fcdf63ab;p=qemu.git diff --git a/balloon.c b/balloon.c index f56fdc1c4..e321f2c68 100644 --- a/balloon.c +++ b/balloon.c @@ -24,13 +24,13 @@ * THE SOFTWARE. */ -#include "monitor.h" -#include "qjson.h" -#include "qint.h" -#include "cpu-common.h" -#include "kvm.h" -#include "balloon.h" +#include "monitor/monitor.h" +#include "exec/cpu-common.h" +#include "sysemu/kvm.h" +#include "sysemu/balloon.h" #include "trace.h" +#include "qmp-commands.h" +#include "qapi/qmp/qjson.h" static QEMUBalloonEvent *balloon_event_fn; static QEMUBalloonStatus *balloon_stat_fn; @@ -52,6 +52,16 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, return 0; } +void qemu_remove_balloon_handler(void *opaque) +{ + if (balloon_opaque != opaque) { + return; + } + balloon_event_fn = NULL; + balloon_stat_fn = NULL; + balloon_opaque = NULL; +} + static int qemu_balloon(ram_addr_t target) { if (!balloon_event_fn) { @@ -62,103 +72,61 @@ static int qemu_balloon(ram_addr_t target) return 1; } -static int qemu_balloon_status(MonitorCompletion cb, void *opaque) +static int qemu_balloon_status(BalloonInfo *info) { if (!balloon_stat_fn) { return 0; } - balloon_stat_fn(balloon_opaque, cb, opaque); + balloon_stat_fn(balloon_opaque, info); return 1; } -static void print_balloon_stat(const char *key, QObject *obj, void *opaque) +void qemu_balloon_changed(int64_t actual) { - Monitor *mon = opaque; + QObject *data; - if (strcmp(key, "actual")) { - monitor_printf(mon, ",%s=%" PRId64, key, - qint_get_int(qobject_to_qint(obj))); - } -} + data = qobject_from_jsonf("{ 'actual': %" PRId64 " }", + actual); -void monitor_print_balloon(Monitor *mon, const QObject *data) -{ - QDict *qdict; + monitor_protocol_event(QEVENT_BALLOON_CHANGE, data); - qdict = qobject_to_qdict(data); - if (!qdict_haskey(qdict, "actual")) { - return; - } - monitor_printf(mon, "balloon: actual=%" PRId64, - qdict_get_int(qdict, "actual") >> 20); - qdict_iter(qdict, print_balloon_stat, mon); - monitor_printf(mon, "\n"); + qobject_decref(data); } -/** - * do_info_balloon(): Balloon information - * - * Make an asynchronous request for balloon info. When the request completes - * a QDict will be returned according to the following specification: - * - * - "actual": current balloon value in bytes - * The following fields may or may not be present: - * - "mem_swapped_in": Amount of memory swapped in (bytes) - * - "mem_swapped_out": Amount of memory swapped out (bytes) - * - "major_page_faults": Number of major faults - * - "minor_page_faults": Number of minor faults - * - "free_mem": Total amount of free and unused memory (bytes) - * - "total_mem": Total amount of available memory (bytes) - * - * Example: - * - * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0, - * "major_page_faults": 142, "minor_page_faults": 239245, - * "free_mem": 1014185984, "total_mem": 1044668416 } - */ -int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque) + +BalloonInfo *qmp_query_balloon(Error **errp) { - int ret; + BalloonInfo *info; if (kvm_enabled() && !kvm_has_sync_mmu()) { - qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); - return -1; + error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); + return NULL; } - ret = qemu_balloon_status(cb, opaque); - if (!ret) { - qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon"); - return -1; + info = g_malloc0(sizeof(*info)); + + if (qemu_balloon_status(info) == 0) { + error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon"); + qapi_free_BalloonInfo(info); + return NULL; } - return 0; + return info; } -/** - * do_balloon(): Request VM to change its memory allocation - */ -int do_balloon(Monitor *mon, const QDict *params, - MonitorCompletion cb, void *opaque) +void qmp_balloon(int64_t value, Error **errp) { - int64_t target; - int ret; - if (kvm_enabled() && !kvm_has_sync_mmu()) { - qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); - return -1; + error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); + return; } - target = qdict_get_int(params, "value"); - if (target <= 0) { - qerror_report(QERR_INVALID_PARAMETER_VALUE, "target", "a size"); - return -1; + if (value <= 0) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size"); + return; } - ret = qemu_balloon(target); - if (ret == 0) { - qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon"); - return -1; + + if (qemu_balloon(value) == 0) { + error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon"); } - - cb(opaque, NULL); - return 0; }