[libvirt] [PATCH v3 11/20] Add a qemuMonitorGetCPUDefinitions method for QMP query-cpu-definitions command

Jiri Denemark jdenemar at redhat.com
Wed Sep 26 12:47:28 UTC 2012


On Tue, Sep 25, 2012 at 19:00:04 +0100, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
> 
> Add a new qemuMonitorGetCPUDefinitions() method to support invocation
> of the 'query-cpu-definitions' JSON monitor command. No HMP equivalent
> is required, since this will only be present for QEMU >= 1.2
> 
> Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
> ---
>  src/qemu/qemu_monitor.c      | 21 +++++++++++++
>  src/qemu/qemu_monitor.h      |  4 +++
>  src/qemu/qemu_monitor_json.c | 75 ++++++++++++++++++++++++++++++++++++++++++++
>  src/qemu/qemu_monitor_json.h |  4 +++
>  tests/qemumonitorjsontest.c  | 63 +++++++++++++++++++++++++++++++++++++
>  5 files changed, 167 insertions(+)
> 
> diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
> index 581fe41..45bb32e 100644
> --- a/src/qemu/qemu_monitor.c
> +++ b/src/qemu/qemu_monitor.c
> @@ -3051,3 +3051,24 @@ void qemuMonitorMachineInfoFree(qemuMonitorMachineInfoPtr machine)
>      VIR_FREE(machine->alias);
>      VIR_FREE(machine);
>  }
> +
> +int qemuMonitorGetCPUDefinitions(qemuMonitorPtr mon,
> +                                 char ***cpus)
> +{
> +    VIR_DEBUG("mon=%p cpus=%p",
> +              mon, cpus);
> +
> +    if (!mon) {
> +        virReportError(VIR_ERR_INVALID_ARG, "%s",
> +                       _("monitor must not be NULL"));
> +        return -1;
> +    }
> +
> +    if (!mon->json) {
> +        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> +                       _("JSON monitor is required"));
> +        return -1;
> +    }
> +
> +    return qemuMonitorJSONGetCPUDefinitions(mon, cpus);
> +}

Hmm, this starts to be pretty boring. I guess we should come up with a macro
or something that would save us from copying the same code over and over when
adding new monitor commands. Anyway, this does not need to be solved by this
capabilities series.

...
> diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
> index ad095b1..1346441 100644
> --- a/tests/qemumonitorjsontest.c
> +++ b/tests/qemumonitorjsontest.c
> @@ -290,6 +290,68 @@ testQemuMonitorJSONGetMachines(const void *data)
>      CHECK(1, "pc-1.1", false, null);
>      CHECK(2, "pc-1.2", true, "pc");
>  
> +#undef CHECK

Since you added this #undef CHECK twice in this patch, one of them should
rather go into the previous patch.

> +    ret = 0;
> +
> +cleanup:
> +    qemuMonitorTestFree(test);
> +    return ret;
> +}
> +
> +
> +static int
> +testQemuMonitorJSONGetCPUDefinitions(const void *data)
> +{
> +    virCapsPtr caps = (virCapsPtr)data;
> +    qemuMonitorTestPtr test = qemuMonitorTestNew(true, caps);
> +    int ret = -1;
> +    char **cpus = NULL;
> +    int ncpus;
> +
> +    if (!test)
> +        return -1;
> +
> +    if (qemuMonitorTestAddItem(test, "query-cpu-definitions",
> +                               "{ "
> +                               "  \"return\": [ "
> +                               "   { "
> +                               "     \"name\": \"qemu64\" "
> +                               "   }, "
> +                               "   { "
> +                               "     \"name\": \"Opteron_G4\" "
> +                               "   }, "
> +                               "   { "
> +                               "     \"name\": \"Westmere\" "
> +                               "   } "
> +                               "  ]"
> +                               "}") < 0)
> +        goto cleanup;
> +
> +    if ((ncpus = qemuMonitorGetCPUDefinitions(qemuMonitorTestGetMonitor(test),
> +                                              &cpus)) < 0)
> +        goto cleanup;
> +
> +    if (ncpus != 3) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       "ncpus %d is not 3", ncpus);
> +        goto cleanup;
> +    }
> +
> +#define CHECK(i, wantname)                                              \
> +    do {                                                                \
> +        if (STRNEQ(cpus[i], (wantname))) {                              \
> +            virReportError(VIR_ERR_INTERNAL_ERROR,                      \
> +                           "name %s is not %s",                         \
> +                           cpus[i], (wantname));                        \
> +            goto cleanup;                                               \
> +        }                                                               \
> +    } while (0)
> +
> +    CHECK(0, "qemu64");
> +    CHECK(1, "Opteron_G4");
> +    CHECK(2, "Westmere");
> +
> +#undef CHECK
>      ret = 0;
>  
>  cleanup:
> @@ -319,6 +381,7 @@ mymain(void)
>      DO_TEST(GetStatus);
>      DO_TEST(GetVersion);
>      DO_TEST(GetMachines);
> +    DO_TEST(GetCPUDefinitions);
>  
>      virCapabilitiesFree(caps);
>  

ACK

Jirka




More information about the libvir-list mailing list