[libvirt] [PATCH 6/6] python: Add python bindings for virDomainMemoryStats

Matthias Bolte matthias.bolte at googlemail.com
Thu Dec 17 23:41:33 UTC 2009


2009/12/17 Adam Litke <agl at us.ibm.com>:
> Enable virDomainMemoryStats in the python API.  dom.memoryStats() will return a
> dictionary containing the supported statistics.  A dictionary is required
> because the meaining of each quantity cannot be inferred from its index in a
> list.
>
> Signed-off-by: Adam Litke <agl at us.ibm.com>
> To: libvirt list <libvir-list at redhat.com>
> Cc: Daniel Veillard <veillard at redhat.com>
> Cc: Daniel P. Berrange <berrange at redhat.com>
> ---
>  python/generator.py             |    3 +-
>  python/libvirt-override-api.xml |    5 ++++
>  python/libvirt-override.c       |   43 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 49 insertions(+), 2 deletions(-)
>
> diff --git a/python/generator.py b/python/generator.py
> index 06f1ff4..56f8925 100755
> --- a/python/generator.py
> +++ b/python/generator.py
> @@ -161,7 +161,6 @@ def enum(type, name, value):
>  functions_failed = []
>  functions_skipped = [
>     "virConnectListDomains",
> -    "virDomainMemoryStats"
>  ]
>
>  skipped_modules = {
> @@ -171,7 +170,6 @@ skipped_types = {
>  #    'int *': "usually a return type",
>      'virConnectDomainEventCallback': "No function types in python",
>      'virEventAddHandleFunc': "No function types in python",
> -     'virDomainMemoryStatPtr': "Not implemented yet",
>  }
>
>  #######################################################################
> @@ -283,6 +281,7 @@ skip_impl = (
>     'virNetworkGetAutostart',
>     'virDomainBlockStats',
>     'virDomainInterfaceStats',
> +    'virDomainMemoryStats',
>     'virNodeGetCellsFreeMemory',
>     'virDomainGetSchedulerType',
>     'virDomainGetSchedulerParameters',
> diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
> index 96958b5..6ae2742 100644
> --- a/python/libvirt-override-api.xml
> +++ b/python/libvirt-override-api.xml
> @@ -105,6 +105,11 @@
>       <arg name='domain' type='virDomainPtr' info='a domain object'/>
>       <arg name='path' type='char *' info='the path for the interface device'/>
>     </function>
> +    <function name='virDomainMemoryStats' file='python'>
> +      <info>Extracts memory statistics for a domain</info>
> +      <return type='virDomainMemoryStats' info='a dictionary of statistics'/>
> +      <arg name='domain' type='virDomainPtr' info='a domain object'/>
> +    </function>
>     <function name="virNodeGetCellsFreeMemory" file='python'>
>       <info>Returns the available memory for a list of cells</info>
>       <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
> diff --git a/python/libvirt-override.c b/python/libvirt-override.c
> index 6c1e51b..8aa2dad 100644
> --- a/python/libvirt-override.c
> +++ b/python/libvirt-override.c
> @@ -120,6 +120,48 @@ libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
>     return(info);
>  }
>
> +static PyObject *
> +libvirt_virDomainMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
> +    virDomainPtr domain;
> +    PyObject *pyobj_domain;
> +    unsigned int nr_stats, i;
> +    virDomainMemoryStatStruct stats[VIR_MEMSTAT_NR_TAGS];
> +    PyObject *info;
> +
> +    if (!PyArg_ParseTuple(args, (char *)"O:virDomainMemoryStats", &pyobj_domain))
> +        return(NULL);
> +    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
> +
> +    nr_stats = virDomainMemoryStats(domain, stats, VIR_MEMSTAT_NR_TAGS, 0);
> +    if (nr_stats == -1)
> +        return VIR_PY_NONE;
> +
> +    /* convert to a Python dictionary */
> +    if ((info = PyDict_New()) == NULL)
> +        return VIR_PY_NONE;
> +
> +    for (i = 0; i < nr_stats; i++) {
> +        if (stats[i].tag == VIR_MEMSTAT_SWAP_IN)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_in"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +        if (stats[i].tag == VIR_MEMSTAT_SWAP_OUT)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_out"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +        if (stats[i].tag == VIR_MEMSTAT_MAJOR_FAULT)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("major_fault"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +        if (stats[i].tag == VIR_MEMSTAT_MINOR_FAULT)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("minor_fault"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +        if (stats[i].tag == VIR_MEMSTAT_MEM_FREE)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("free_memory"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +        if (stats[i].tag == VIR_MEMSTAT_MEM_TOTAL)
> +            PyDict_SetItem(info, libvirt_constcharPtrWrap("total_memory"),
> +                           PyLong_FromUnsignedLongLong(stats[i].val));
> +    }
> +    return info;
> +}

You're using the old tags (VIR_MEMSTAT_*) and names (free_memory,
total_memory) here.

You could convert the adjacent if blocks into and if/else if block or
a switch block.

You're also silently ignoring unknown tags here, but that should be
handled at the libvirt C API level if it should be handled at all.

Matthias




More information about the libvir-list mailing list