[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v4] python: add virDomainGetCPUStats python binding API



    dom.getCPUStats(True, 0)
      [{'cpu_time': 24699446159L, 'system_time': 10870000000L, 'user_time': 950000000L}]
    dom.getCPUStats(False, 0)
      [{'cpu_time': 8535292289L}, {'cpu_time': 1005395355L}, {'cpu_time': 9351766377L}, {'cpu_time': 5813545649L}]

    *generator.py Add a new naming rule
    *libvirt-override-api.xml The API function description
    *libvirt-override.c Implement it.
---
 python/generator.py             |    5 +-
 python/libvirt-override-api.xml |   13 ++++
 python/libvirt-override.c       |  147 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 164 insertions(+), 1 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index 98072f0..4f95cc9 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -423,7 +423,7 @@ skip_impl = (
     'virDomainGetBlockIoTune',
     'virDomainSetInterfaceParameters',
     'virDomainGetInterfaceParameters',
-    'virDomainGetCPUStats',  # not implemented now.
+    'virDomainGetCPUStats',
     'virDomainGetDiskErrors',
 )
 
@@ -966,6 +966,9 @@ def nameFixup(name, classe, type, file):
     elif name[0:19] == "virStorageVolLookup":
         func = name[3:]
         func = string.lower(func[0:1]) + func[1:]
+    elif name[0:20] == "virDomainGetCPUStats":
+        func = name[9:]
+        func = string.lower(func[0:1]) + func[1:]
     elif name[0:12] == "virDomainGet":
         func = name[12:]
         func = string.lower(func[0:1]) + func[1:]
diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index ab8f33a..26d9902 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -149,6 +149,19 @@
       <arg name='path' type='char *' info='the path for the block device'/>
       <arg name='flags' type='int' info='flags (unused; pass 0)'/>
     </function>
+    <function name='virDomainGetCPUStats' file='python'>
+      <info>Extracts CPU statistics for a running domain. On success it will
+   return a list of data of dictionary type. If boolean total is False, the
+   first element of the list refers to CPU0 on the host, second element is
+   CPU1, and so on. The format of data struct is as follows:
+   [{cpu_time:xxx}, {cpu_time:xxx}, ...]
+   If it is True, it returns total domain CPU statistics in the format of
+   [{cpu_time:xxx, user_time:xxx, system_time:xxx}]</info>
+     <return type='str *' info='returns a list of dictionary in case of success, None in case of error'/>
+     <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
+     <arg name='total' type='bool' info='on true, return total domain CPU statistics, false return per-cpu info'/>
+     <arg name='flags' type='int' info='flags (unused; pass 0)'/>
+    </function>
     <function name='virDomainInterfaceStats' file='python'>
       <info>Extracts interface device statistics for a domain</info>
       <return type='virDomainInterfaceStats' info='a tuple of statistics'/>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 792cfa3..5920456 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -378,6 +378,152 @@ cleanup:
 }
 
 static PyObject *
+libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
+{
+    virDomainPtr domain;
+    PyObject *pyobj_domain, *totalbool;
+    PyObject *cpu, *total;
+    PyObject *ret = NULL;
+    PyObject *error = NULL;
+    int ncpus = -1, start_cpu = 0;
+    int sumparams = 0, nparams = -1;
+    int i, i_retval;
+    unsigned int flags, totalflag;
+    virTypedParameterPtr params = NULL, cpuparams;
+
+    if (!PyArg_ParseTuple(args, (char *)"OOi:virDomainGetCPUStats",
+                          &pyobj_domain, &totalbool, &flags))
+        return NULL;
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    if (!PyBool_Check(totalbool)) {
+        PyErr_Format(PyExc_TypeError,
+                    "The \"total\" attribute must be bool");
+        return NULL;
+    } else {
+        /* Hack - Python's definition of Py_True breaks strict
+         * aliasing rules, so can't directly compare
+         */
+        PyObject *hacktrue = PyBool_FromLong(1);
+        totalflag = hacktrue == totalbool ? 1 : 0;
+        Py_DECREF(hacktrue);
+    }
+
+    if ((ret = PyList_New(0)) == NULL)
+        return NULL;
+
+    if (!totalflag) {
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        ncpus = virDomainGetCPUStats(domain, NULL, 0, 0, 0, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (ncpus < 0) {
+            error = VIR_PY_NONE;
+            goto error;
+        }
+
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        nparams = virDomainGetCPUStats(domain, NULL, 0, 0, 1, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (nparams < 0) {
+            error = VIR_PY_NONE;
+            goto error;
+        }
+
+        sumparams = nparams * MIN(ncpus, 128);
+
+        if (VIR_ALLOC_N(params, sumparams) < 0) {
+            error = PyErr_NoMemory();
+            goto error;
+        }
+
+        while (ncpus) {
+            int queried_ncpus = MIN(ncpus, 128);
+            if (nparams) {
+
+                LIBVIRT_BEGIN_ALLOW_THREADS;
+                i_retval = virDomainGetCPUStats(domain, params,
+                                                nparams, start_cpu, queried_ncpus, flags);
+                LIBVIRT_END_ALLOW_THREADS;
+
+                if (i_retval < 0) {
+                    error = VIR_PY_NONE;
+                    goto error;
+                }
+            } else {
+                i_retval = 0;
+            }
+
+            for (i = 0; i < queried_ncpus; i++) {
+                cpuparams = &params[i * nparams];
+                if ((cpu = getPyVirTypedParameter(cpuparams, i_retval)) == NULL) {
+                    goto error;
+                }
+
+                if (PyList_Append(ret, cpu) < 0) {
+                    Py_DECREF(cpu);
+                    goto error;
+                }
+                Py_DECREF(cpu);
+            }
+
+            start_cpu += queried_ncpus;
+            ncpus -= queried_ncpus;
+            virTypedParameterArrayClear(params, sumparams);
+        }
+    } else {
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        nparams = virDomainGetCPUStats(domain, NULL, 0, -1, 1, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (nparams < 0) {
+            error = VIR_PY_NONE;
+            goto error;
+        }
+
+        if (nparams) {
+            sumparams = nparams;
+
+            if (VIR_ALLOC_N(params, nparams) < 0) {
+                error = PyErr_NoMemory();
+                goto error;
+            }
+
+            LIBVIRT_BEGIN_ALLOW_THREADS;
+            i_retval = virDomainGetCPUStats(domain, params, nparams, -1, 1, flags);
+            LIBVIRT_END_ALLOW_THREADS;
+
+            if (i_retval < 0) {
+                error = VIR_PY_NONE;
+                goto error;
+            }
+        } else {
+            i_retval = 0;
+        }
+
+        if ((total = getPyVirTypedParameter(params, i_retval)) == NULL) {
+            goto error;
+        }
+        if (PyList_Append(ret, total) < 0) {
+            Py_DECREF(total);
+            goto error;
+        }
+        Py_DECREF(total);
+    }
+
+    virTypedParameterArrayClear(params, sumparams);
+    VIR_FREE(params);
+    return ret;
+
+error:
+    virTypedParameterArrayClear(params, sumparams);
+    VIR_FREE(params);
+    Py_DECREF(ret);
+    return error;
+}
+
+static PyObject *
 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     virDomainPtr domain;
     PyObject *pyobj_domain;
@@ -5366,6 +5512,7 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
     {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
     {(char *) "virDomainBlockStatsFlags", libvirt_virDomainBlockStatsFlags, METH_VARARGS, NULL},
+    {(char *) "virDomainGetCPUStats", libvirt_virDomainGetCPUStats, METH_VARARGS, NULL},
     {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
     {(char *) "virDomainMemoryStats", libvirt_virDomainMemoryStats, METH_VARARGS, NULL},
     {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
-- 
1.7.7.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]