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

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



   dom.cPUStats(True, 0)
      [{'cpu_time': 370942649768L, 'system_time': 11640000000L, 'user_time': 890000000L}]

   dom.cPUStats(False, 0)
      [{'cpu_time': 38378374721L}, {'cpu_time': 12475521087L}, {'cpu_time': 127388622598L}, {'cpu_time': 192720388324L}]
---
 python/libvirt-override-api.xml |   10 +++
 python/libvirt-override.c       |  164 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+), 0 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index ab8f33a..c906cc3 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -149,6 +149,16 @@
       <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 True, 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 like [{cpu_time:xxx},{cpu_time:xxx}, ...]
+   If it is False, it returns total domain CPU statistics like [{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..eb24c60 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -378,6 +378,169 @@ cleanup:
 }
 
 static PyObject *
+libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
+{
+    virDomainPtr domain;
+    PyObject *pyobj_domain, *totalbool;
+    PyObject *ret = NULL;
+    PyObject *cpu, *total;
+    PyObject *error;
+    int i, i_retval;
+    int ncpus = -1;
+    int sumparams, nparams = -1;
+    unsigned int flags;
+    virTypedParameterPtr params, cpuparams;
+
+    if (!PyArg_ParseTuple(args, (char *)"OOi:virDomainGetNumaParameters",
+                          &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;
+    }
+
+    if ((ret = PyList_New(0)) == NULL)
+        return NULL;
+
+    if (totalbool == Py_False) {
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        ncpus = virDomainGetCPUStats(domain, NULL, 0, 0, 0, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (ncpus < 0) {
+            error = VIR_PY_NONE;
+            goto failed;
+        }
+
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        nparams = virDomainGetCPUStats(domain, NULL, 0, 0, 1, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (nparams < 0) {
+            error = VIR_PY_NONE;
+            goto failed;
+        }
+
+        if (!nparams) {
+            if ((cpu = PyDict_New()) == NULL) {
+                error = NULL;
+                goto failed;
+            }
+
+            if (PyList_Append(ret, cpu) < 0) {
+                Py_DECREF(cpu);
+                error = NULL;
+                goto failed;
+            }
+
+            Py_DECREF(cpu);
+            return ret;
+        }
+        sumparams = nparams * ncpus;
+
+        if (VIR_ALLOC_N(params, sumparams) < 0) {
+            error = PyErr_NoMemory();
+            goto failed;
+        }
+
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        i_retval = virDomainGetCPUStats(domain, params, nparams, 0, ncpus, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+
+        if (i_retval < 0) {
+            error = VIR_PY_NONE;
+            goto cleanup;
+        }
+
+        for (i = 0; i < ncpus; i++) {
+            if (params[i * nparams].type == 0)
+                continue;
+
+            cpuparams = &params[i * nparams];
+            if ((cpu = getPyVirTypedParameter(cpuparams, nparams)) == NULL) {
+                error = NULL;
+                goto cleanup;
+            }
+            if (PyList_Append(ret, cpu) < 0) {
+                Py_DECREF(cpu);
+                error = NULL;
+                goto cleanup;
+            }
+            Py_DECREF(cpu);
+        }
+
+        virTypedParameterArrayClear(params, sumparams);
+        VIR_FREE(params);
+        return ret;
+    } 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 failed;
+        }
+
+        if (!nparams) {
+            if ((total = PyDict_New()) == NULL) {
+                error = NULL;
+                goto failed;
+            }
+            if (PyList_Append(ret, total) < 0) {
+               Py_DECREF(total);
+               error = NULL;
+               goto failed;
+            }
+
+            Py_DECREF(total);
+            return ret;
+        }
+        sumparams = nparams;
+
+        if (VIR_ALLOC_N(params, nparams) < 0) {
+            error = PyErr_NoMemory();
+            goto failed;
+        }
+
+        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 cleanup;
+        }
+
+        if ((total = getPyVirTypedParameter(params, nparams)) == NULL) {
+            error = NULL;
+            goto cleanup;
+        }
+        if (PyList_Append(ret, total) < 0) {
+            Py_DECREF(total);
+            error = NULL;
+            goto cleanup;
+        }
+        Py_DECREF(total);
+
+        virTypedParameterArrayClear(params, sumparams);
+        VIR_FREE(params);
+        return ret;
+    }
+
+cleanup:
+    virTypedParameterArrayClear(params, sumparams);
+    VIR_FREE(params);
+
+failed:
+    Py_DECREF(ret);
+    return error;
+}
+
+static PyObject *
 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     virDomainPtr domain;
     PyObject *pyobj_domain;
@@ -5366,6 +5529,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]