Re: [libvirt] [PATCH v5 01/15] qemu_monitor: refactor cpu model expansion

2019-10-02 Thread Jiri Denemark
On Thu, Sep 19, 2019 at 16:24:52 -0400, Collin Walling wrote:
> Refactor some code in qemuMonitorJSONGetCPUModelExpansion to be later
> used for the comparison and baseline functions.
> 
> Signed-off-by: Collin Walling 
> Reviewed-by: Bjoern Walk 
> Reviewed-by: Daniel Henrique Barboza 
> ---
>  src/qemu/qemu_monitor_json.c | 143 
> ---
>  1 file changed, 94 insertions(+), 49 deletions(-)

Reviewed-by: Jiri Denemark 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v5 01/15] qemu_monitor: refactor cpu model expansion

2019-09-19 Thread Collin Walling
Refactor some code in qemuMonitorJSONGetCPUModelExpansion to be later
used for the comparison and baseline functions.

Signed-off-by: Collin Walling 
Reviewed-by: Bjoern Walk 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_monitor_json.c | 143 ---
 1 file changed, 94 insertions(+), 49 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index e4404f0..7219d14 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5780,6 +5780,96 @@ qemuMonitorJSONParseCPUModelProperty(const char *key,
 return 0;
 }
 
+
+static virJSONValuePtr
+qemuMonitorJSONMakeCPUModel(const char *model_name,
+bool migratable)
+{
+virJSONValuePtr model = NULL;
+virJSONValuePtr props = NULL;
+
+if (!(model = virJSONValueNewObject()))
+goto error;
+
+if (virJSONValueObjectAppendString(model, "name", model_name) < 0)
+goto error;
+
+if (!migratable) {
+if (!(props = virJSONValueNewObject()) ||
+virJSONValueObjectAppendBoolean(props, "migratable", false) < 0 ||
+virJSONValueObjectAppend(model, "props", props) < 0)
+goto error;
+props = NULL;
+}
+
+return model;
+
+ error:
+virJSONValueFree(model);
+virJSONValueFree(props);
+return NULL;
+}
+
+
+static int
+qemuMonitorJSONParseCPUModelData(virJSONValuePtr data,
+ virJSONValuePtr *cpu_model,
+ virJSONValuePtr *cpu_props,
+ const char **cpu_name)
+{
+if (!(*cpu_model = virJSONValueObjectGetObject(data, "model"))) {
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("query-cpu-model-expansion reply data was missing 
'model'"));
+return -1;
+}
+
+if (!(*cpu_name = virJSONValueObjectGetString(*cpu_model, "name"))) {
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("query-cpu-model-expansion reply data was missing 
'name'"));
+return -1;
+}
+
+if (!(*cpu_props = virJSONValueObjectGetObject(*cpu_model, "props"))) {
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("query-cpu-model-expansion reply data was missing 
'props'"));
+return -1;
+}
+
+return 0;
+}
+
+
+static int
+qemuMonitorJSONParseCPUModel(const char *cpu_name,
+ virJSONValuePtr cpu_props,
+ qemuMonitorCPUModelInfoPtr *model_info)
+{
+qemuMonitorCPUModelInfoPtr machine_model = NULL;
+int ret = -1;
+
+if (VIR_ALLOC(machine_model) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(machine_model->name, cpu_name) < 0)
+goto cleanup;
+
+if (VIR_ALLOC_N(machine_model->props, 
virJSONValueObjectKeysNumber(cpu_props)) < 0)
+goto cleanup;
+
+if (virJSONValueObjectForeachKeyValue(cpu_props,
+  qemuMonitorJSONParseCPUModelProperty,
+  machine_model) < 0)
+goto cleanup;
+
+VIR_STEAL_PTR(*model_info, machine_model);
+ret = 0;
+
+ cleanup:
+qemuMonitorCPUModelInfoFree(machine_model);
+return ret;
+}
+
+
 int
 qemuMonitorJSONGetCPUModelExpansion(qemuMonitorPtr mon,
 qemuMonitorCPUModelExpansionType type,
@@ -5789,32 +5879,19 @@ qemuMonitorJSONGetCPUModelExpansion(qemuMonitorPtr mon,
 {
 int ret = -1;
 virJSONValuePtr model = NULL;
-virJSONValuePtr props = NULL;
 virJSONValuePtr cmd = NULL;
 virJSONValuePtr reply = NULL;
 virJSONValuePtr data;
 virJSONValuePtr cpu_model;
 virJSONValuePtr cpu_props;
-qemuMonitorCPUModelInfoPtr machine_model = NULL;
 char const *cpu_name;
 const char *typeStr = "";
 
 *model_info = NULL;
 
-if (!(model = virJSONValueNewObject()))
-goto cleanup;
-
-if (virJSONValueObjectAppendString(model, "name", model_name) < 0)
+if (!(model = qemuMonitorJSONMakeCPUModel(model_name, migratable)))
 goto cleanup;
 
-if (!migratable) {
-if (!(props = virJSONValueNewObject()) ||
-virJSONValueObjectAppendBoolean(props, "migratable", false) < 0 ||
-virJSONValueObjectAppend(model, "props", props) < 0)
-goto cleanup;
-props = NULL;
-}
-
  retry:
 switch (type) {
 case QEMU_MONITOR_CPU_MODEL_EXPANSION_STATIC:
@@ -5850,11 +5927,9 @@ qemuMonitorJSONGetCPUModelExpansion(qemuMonitorPtr mon,
 
 data = virJSONValueObjectGetObject(reply, "return");
 
-if (!(cpu_model = virJSONValueObjectGetObject(data, "model"))) {
-virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-   _("query-cpu-model-expansion reply data was missing 
'model'"));
+if (qemuMonitorJSONParseCPUModelData(data,
+ _model, _props, _name) < 
0)
 goto cleanup;
-