Implement qemuMonitorQueryDirtyRate which query domain's memory dirty rate calling qmp "query-dirty-rate". Signed-off-by: Hao Wang <wanghao232@xxxxxxxxxx> --- src/qemu/qemu_monitor.c | 12 +++++++ src/qemu/qemu_monitor.h | 14 ++++++++ src/qemu/qemu_monitor_json.c | 66 ++++++++++++++++++++++++++++++++++++ src/qemu/qemu_monitor_json.h | 4 +++ 4 files changed, 96 insertions(+) diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 05c57f5f1d..dcc529b405 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -4754,3 +4754,15 @@ qemuMonitorStartDirtyRateCalc(qemuMonitorPtr mon, return qemuMonitorJSONStartDirtyRateCalc(mon, seconds); } + + +int +qemuMonitorQueryDirtyRate(qemuMonitorPtr mon, + qemuMonitorDirtyRateInfoPtr info) +{ + VIR_DEBUG("info=%p", info); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONQueryDirtyRate(mon, info); +} diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 94813906b4..9958657c2c 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -1530,3 +1530,17 @@ qemuMonitorTransactionBackup(virJSONValuePtr actions, int qemuMonitorStartDirtyRateCalc(qemuMonitorPtr mon, int seconds); + +typedef struct _qemuMonitorDirtyRateInfo qemuMonitorDirtyRateInfo; +typedef qemuMonitorDirtyRateInfo *qemuMonitorDirtyRateInfoPtr; + +struct _qemuMonitorDirtyRateInfo { + char *status; /* the status of last dirtyrate calculation */ + long long dirtyRate; /* the dirtyrate in MiB/s */ + long long startTime; /* the start time of dirtyrate calculation */ + int calcTime; /* the period of dirtyrate calculation */ +}; + +int +qemuMonitorQueryDirtyRate(qemuMonitorPtr mon, + qemuMonitorDirtyRateInfoPtr info); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index e6c127ab61..409fb817bf 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -9505,3 +9505,69 @@ qemuMonitorJSONStartDirtyRateCalc(qemuMonitorPtr mon, return 0; } + + +static int +qemuMonitorJSONExtractDirtyRateInfo(virJSONValuePtr data, + qemuMonitorDirtyRateInfoPtr info) +{ + const char *status; + + if (!(status = virJSONValueObjectGetString(data, "status"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-dirty-rate reply was missing 'status' data")); + return -1; + } + info->status = g_strdup(status); + + /* `query-dirty-rate` replies `dirty-rate` data only if the status of the latest + * calculation is `measured`. + */ + if (STREQ(info->status, "measured") && + (virJSONValueObjectGetNumberLong(data, "dirty-rate", &info->dirtyRate) < 0)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-dirty-rate reply was missing 'dirty-rate' data")); + return -1; + } + + if (virJSONValueObjectGetNumberLong(data, "start-time", &info->startTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-dirty-rate reply was missing 'start-time' data")); + return -1; + } + + if (virJSONValueObjectGetNumberInt(data, "calc-time", &info->calcTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-dirty-rate reply was missing 'calc-time' data")); + return -1; + } + + return 0; +} + + +int +qemuMonitorJSONQueryDirtyRate(qemuMonitorPtr mon, + qemuMonitorDirtyRateInfoPtr info) +{ + g_autoptr(virJSONValue) cmd = NULL; + g_autoptr(virJSONValue) reply = NULL; + virJSONValuePtr data = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("query-dirty-rate", NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + return -1; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + return -1; + + if (!(data = virJSONValueObjectGetObject(reply, "return"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-dirty-rate reply was missing 'return' data")); + return -1; + } + + return qemuMonitorJSONExtractDirtyRateInfo(data, info); +} diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index 32782cf681..8d4232f67b 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -715,3 +715,7 @@ qemuMonitorJSONGetCPUMigratable(qemuMonitorPtr mon, int qemuMonitorJSONStartDirtyRateCalc(qemuMonitorPtr mon, int seconds); + +int +qemuMonitorJSONQueryDirtyRate(qemuMonitorPtr mon, + qemuMonitorDirtyRateInfoPtr info); -- 2.23.0