[libvirt] [libvirt-python][PATCH] Implement new virNodeGetFreePages API

Michal Privoznik mprivozn at redhat.com
Fri Jun 20 07:14:19 UTC 2014


The API expose information on host's free pages counts. For easier
access, in python this API returns a dictionary such as:

In [4]: conn.getFreePages([2048,1*1024*1024], -1, 5)
Out[4]:
{-1: {2048: 114, 1048576: 4},
 0: {2048: 3, 1048576: 1},
 1: {2048: 100, 1048576: 1},
 2: {2048: 10, 1048576: 1},
 3: {2048: 1, 1048576: 1}}

At the top level of the returned dictionary there's a pair of <NUMA
node> and another dictionary that contains detailed information on
each supported page size. The information then consists of fairs of
<page size> and <count of free pages>.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 generator.py             |   1 +
 libvirt-override-api.xml |   9 ++++
 libvirt-override.c       | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)

diff --git a/generator.py b/generator.py
index bdac877..03027c6 100755
--- a/generator.py
+++ b/generator.py
@@ -462,6 +462,7 @@ skip_impl = (
     'virDomainMigrate3',
     'virDomainMigrateToURI3',
     'virConnectGetCPUModelNames',
+    'virNodeGetFreePages',
 )
 
 lxc_skip_impl = (
diff --git a/libvirt-override-api.xml b/libvirt-override-api.xml
index 935e04d..bbf0ab1 100644
--- a/libvirt-override-api.xml
+++ b/libvirt-override-api.xml
@@ -624,5 +624,14 @@
       <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
       <arg name='flags' type='int' info='unused, pass 0'/>
     </function>
+    <function name="virNodeGetFreePages" file='python'>
+      <info>Returns the number of available pages for a list of cells and page sizes</info>
+      <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
+      <arg name='pages' type='char *' info='list of desired page sizes'/>
+      <arg name='startCell' type='int' info='first cell in the list'/>
+      <arg name='maxCells' type='int' info='number of cell in the list'/>
+      <arg name='flags' type='int' info='unused, pass 0'/>
+      <return type='char *' info='the list available memory in the cells'/>
+    </function>
   </symbols>
 </api>
diff --git a/libvirt-override.c b/libvirt-override.c
index eb1d5e2..04fb4cd 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -7764,6 +7764,110 @@ libvirt_virDomainSetTime(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
 }
 #endif /* LIBVIR_CHECK_VERSION(1, 2, 5) */
 
+
+#if LIBVIR_CHECK_VERSION(1, 2, 6)
+static PyObject *
+libvirt_virNodeGetFreePages(PyObject *self ATTRIBUTE_UNUSED,
+                            PyObject *args) {
+    PyObject *py_retval = NULL;
+    PyObject *pyobj_conn;
+    PyObject *pyobj_pagesize;
+    PyObject *pyobj_counts = NULL;
+    virConnectPtr conn;
+    unsigned int *pages = NULL;
+    int startCell;
+    unsigned int cellCount;
+    unsigned int flags;
+    unsigned long long *counts = NULL;
+    int c_retval;
+    ssize_t pyobj_pagesize_size, i, j;
+
+    if (!PyArg_ParseTuple(args, (char *)"OOiii:virNodeGetFreePages",
+                          &pyobj_conn, &pyobj_pagesize, &startCell,
+                          &cellCount, &flags))
+        return NULL;
+
+    if (!PyList_Check(pyobj_pagesize)) {
+        PyErr_Format(PyExc_TypeError, "pagesize must be list");
+        return NULL;
+    }
+
+    if (cellCount == 0) {
+        PyErr_Format(PyExc_LookupError, "cellCount must not be zero");
+        return NULL;
+    }
+
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    pyobj_pagesize_size = PyList_Size(pyobj_pagesize);
+    if (VIR_ALLOC_N(pages, pyobj_pagesize_size) < 0 ||
+        VIR_ALLOC_N(counts, pyobj_pagesize_size * cellCount) < 0 ||
+        !(pyobj_counts = PyDict_New()))
+        goto cleanup;
+
+    for (i = 0; i < pyobj_pagesize_size; i++) {
+        PyObject *tmp = PyList_GetItem(pyobj_pagesize, i);
+
+        if (libvirt_intUnwrap(tmp, &pages[i]) < 0)
+            goto cleanup;
+    }
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virNodeGetFreePages(conn,
+                                   pyobj_pagesize_size, pages,
+                                   startCell, cellCount,
+                                   counts, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (c_retval < 0) {
+        py_retval = VIR_PY_NONE;
+        goto cleanup;
+    }
+
+    for (i = 0; i < c_retval;) {
+        PyObject *per_node = NULL;
+        PyObject *node = NULL;
+
+        if (!(per_node = PyDict_New()) ||
+            !(node = libvirt_intWrap(startCell + i/pyobj_pagesize_size))) {
+            Py_XDECREF(per_node);
+            Py_XDECREF(node);
+            goto cleanup;
+        }
+
+        for (j = 0; j < pyobj_pagesize_size; j ++) {
+            PyObject *page = NULL;
+            PyObject *count = NULL;
+
+            if (!(page = libvirt_intWrap(pages[j])) ||
+                !(count = libvirt_intWrap(counts[i + j])) ||
+                PyDict_SetItem(per_node, page, count) < 0) {
+                Py_XDECREF(page);
+                Py_XDECREF(count);
+                Py_XDECREF(per_node);
+                Py_XDECREF(node);
+                goto cleanup;
+            }
+        }
+        i += pyobj_pagesize_size;
+
+        if (PyDict_SetItem(pyobj_counts, node, per_node) < 0) {
+            Py_XDECREF(per_node);
+            Py_XDECREF(node);
+            goto cleanup;
+        }
+    }
+
+    py_retval = pyobj_counts;
+    pyobj_counts = NULL;
+ cleanup:
+    Py_XDECREF(pyobj_counts);
+    VIR_FREE(pages);
+    VIR_FREE(counts);
+    return py_retval;
+}
+#endif /* LIBVIR_CHECK_VERSION(1, 2, 6) */
+
 /************************************************************************
  *									*
  *			The registration stuff				*
@@ -7945,6 +8049,9 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetTime", libvirt_virDomainGetTime, METH_VARARGS, NULL},
     {(char *) "virDomainSetTime", libvirt_virDomainSetTime, METH_VARARGS, NULL},
 #endif /* LIBVIR_CHECK_VERSION(1, 2, 5) */
+#if LIBVIR_CHECK_VERSION(1, 2, 6)
+    {(char *) "virNodeGetFreePages", libvirt_virNodeGetFreePages, METH_VARARGS, NULL},
+#endif /* LIBVIR_CHECK_VERSION(1, 2, 6) */
     {NULL, NULL, 0, NULL}
 };
 
-- 
1.8.5.5




More information about the libvir-list mailing list