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

[libvirt] [PATCH 1/2] python: Expose binding for virNodeGetCPUStats()



This patch adds binding for virNodeGetCPUStats method of libvirtd.
Return value is represended as a python dictionary mapping fileld names
to values.
---
 include/libvirt/libvirt.h.in    |    6 +++-
 python/libvirt-override-api.xml |    7 +++++
 python/libvirt-override.c       |   47 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index 0787f18..1291c59 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -361,9 +361,11 @@ struct _virNodeInfo {
 /**
  * VIR_NODE_CPU_STATS_ALL_CPUS:
  *
- * Macro for the total CPU time/utilization
+ * Value for specifying request for the total CPU time/utilization
  */
-#define VIR_NODE_CPU_STATS_ALL_CPUS (-1)
+typedef enum {
+    VIR_NODE_CPU_STATS_ALL_CPUS = -1,
+} virNodeGetCPUStatsAllCPUs;

 /**
  * VIR_NODE_CPU_STATS_KERNEL:
diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index ef02f34..e6dc967 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -77,6 +77,13 @@
       <return type='int *' info='the list of information or None in case of error'/>
       <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
     </function>
+    <function name='virNodeGetCPUStats' file='python'>
+      <info>Extract node's CPU statistics.</info>
+      <return type='virNodeCPUStats' info='dictionary mapping field names to values or None in case of error'/>
+      <arg name='conn' type='virConnectPtr' info='pointer to hypervisor connection'/>
+      <arg name='cpuNum' type='int' info='number of node cpu. (VIR_NODE_CPU_STATS_ALL_CPUS means total cpu statistics)'/>
+      <arg name='flags' type='unsigned int' info='aditional flags'/>
+    </function>
     <function name='virDomainGetUUID' file='python'>
       <info>Extract the UUID unique Identifier of a domain.</info>
       <return type='char *' info='the 16 bytes string or None in case of error'/>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 1759bae..c2abc9c 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -2256,6 +2256,52 @@ libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
     return(py_retval);
 }

+static PyObject *
+libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
+{
+    PyObject *ret;
+    PyObject *pyobj_conn;
+    virConnectPtr conn;
+    unsigned int flags;
+    int cpuNum, c_retval, i;
+    int nparams = 0;
+    virNodeCPUStatsPtr stats = NULL;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCPUStats", &pyobj_conn, &cpuNum, &flags))
+        return(NULL);
+    conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virNodeGetCPUStats(conn, cpuNum, NULL, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+    if (c_retval < 0)
+        return VIR_PY_NONE;
+
+    if (nparams) {
+        if (!(stats = malloc(sizeof(*stats) * nparams)))
+            return VIR_PY_NONE;
+
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        c_retval = virNodeGetCPUStats(conn, cpuNum, stats, &nparams, flags);
+        LIBVIRT_END_ALLOW_THREADS;
+        if (c_retval < 0) {
+            free(stats);
+            return VIR_PY_NONE;
+        }
+    }
+    if (!(ret = PyDict_New())) {
+        free(stats);
+        return VIR_PY_NONE;
+    }
+    for (i = 0; i < nparams; i++) {
+        PyDict_SetItem(ret,
+                       libvirt_constcharPtrWrap(stats[i].field),
+                       libvirt_ulonglongWrap(stats[i].value));
+    }
+
+    free(stats);
+    return ret;
+}

 static PyObject *
 libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
@@ -4773,6 +4819,7 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetControlInfo", libvirt_virDomainGetControlInfo, METH_VARARGS, NULL},
     {(char *) "virDomainGetBlockInfo", libvirt_virDomainGetBlockInfo, METH_VARARGS, NULL},
     {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
+    {(char *) "virNodeGetCPUStats", libvirt_virNodeGetCPUStats, METH_VARARGS, NULL},
     {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
     {(char *) "virDomainGetUUIDString", libvirt_virDomainGetUUIDString, METH_VARARGS, NULL},
     {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
-- 
1.7.3.4


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