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

[libvirt] [PATCH 1/2 v3] Python: Refactoring virTypedParameter conversion for NUMA tuning APIs



        *virDomainSetNumaParameters
        *virDomainGetNumaParameters
---
 python/Makefile.am              |    4 +-
 python/libvirt-override-api.xml |   13 ++
 python/libvirt-override.c       |  314 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 330 insertions(+), 1 deletions(-)

diff --git a/python/Makefile.am b/python/Makefile.am
index 3068eee..4302fa5 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -8,6 +8,8 @@ SUBDIRS= . tests
 INCLUDES = \
 	$(PYTHON_INCLUDES) \
 	-I$(top_srcdir)/include \
+        -I$(top_srcdir)/src \
+        -I$(top_srcdir)/gnulib/lib \
 	-I$(top_builddir)/include \
 	-I$(top_builddir)/$(subdir) \
 	$(GETTEXT_CPPFLAGS)
@@ -42,7 +44,7 @@ all-local: libvirt.py libvirt_qemu.py
 
 pyexec_LTLIBRARIES = libvirtmod.la libvirtmod_qemu.la
 
-libvirtmod_la_SOURCES = libvirt-override.c typewrappers.c
+libvirtmod_la_SOURCES = libvirt-override.c typewrappers.c ../src/util/virtypedparam.c
 nodist_libvirtmod_la_SOURCES = libvirt.c libvirt.h
 # Python <= 2.4 header files contain a redundant decl, hence we
 # need extra flags here
diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 704fee9..748aa17 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -248,6 +248,19 @@
       <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
       <arg name='flags' type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
     </function>
+    <function name='virDomainSetNumaParameters' file='python'>
+      <info>Change the NUMA tunables</info>
+      <return type='int' info='-1 in case of error, 0 in case of success.'/>
+      <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
+      <arg name='params' type='virTypedParameterPtr' info='pointer to numa tunable objects'/>
+      <arg name='flags'  type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
+    </function>
+    <function name='virDomainGetNumaParameters' file='python'>
+      <info>Get the NUMA parameters</info>
+      <return type='int' info='returns a dictionary of params in case of success, -1 in case of error'/>
+      <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
+      <arg name='flags' type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
+    </function>
     <function name='virConnectListStoragePools' file='python'>
       <info>list the storage pools, stores the pointers to the names in @names</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 d2aad0f..5f9d83e 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -21,6 +21,7 @@
 #include "libvirt/virterror.h"
 #include "typewrappers.h"
 #include "libvirt.h"
+#include "util/virtypedparam.h"
 
 #ifndef __CYGWIN__
 extern void initlibvirtmod(void);
@@ -61,6 +62,208 @@ static char *py_str(PyObject *obj)
     return PyString_AsString(str);
 }
 
+/* Two helper functions to help the conversions between C to Python
+ * for the virTypedParameter used in the following APIs. */
+static PyObject *
+getPyVirTypedParameter(virTypedParameterPtr params, int nparams)
+{
+    PyObject *info;
+    PyObject *key, *val;
+    PyObject *ret = NULL;
+    int i;
+
+    if (!params)
+        return ret;
+
+    /* convert to a Python tuple of long objects */
+    if ((info = PyDict_New()) == NULL) {
+        return ret;
+    }
+
+    for (i = 0 ; i < nparams ; i++) {
+        switch (params[i].type) {
+        case VIR_TYPED_PARAM_INT:
+            val = PyInt_FromLong((long)params[i].value.i);
+            break;
+
+        case VIR_TYPED_PARAM_UINT:
+            val = PyInt_FromLong((unsigned long)params[i].value.ui);
+            break;
+
+        case VIR_TYPED_PARAM_LLONG:
+            val = PyLong_FromLongLong((long long)params[i].value.l);
+            break;
+
+        case VIR_TYPED_PARAM_ULLONG:
+            val = PyLong_FromUnsignedLongLong((unsigned long long)params[i].value.ul);
+            break;
+
+        case VIR_TYPED_PARAM_DOUBLE:
+            val = PyFloat_FromDouble((double)params[i].value.d);
+            break;
+
+        case VIR_TYPED_PARAM_BOOLEAN:
+            val = PyBool_FromLong((long)params[i].value.b);
+            break;
+
+        case VIR_TYPED_PARAM_STRING:
+            val = libvirt_constcharPtrWrap(params[i].value.s);
+            break;
+
+        default:
+            Py_DECREF(info);
+            return ret;
+        }
+
+        key = libvirt_constcharPtrWrap(params[i].field);
+        if (!key || !val)
+            goto fail;
+
+        if (PyDict_SetItem(info, key, val) < 0)
+            goto fail;
+
+        Py_DECREF(key);
+        Py_DECREF(val);
+    }
+    return info;
+fail:
+    Py_XDECREF(info);
+    Py_XDECREF(key);
+    Py_XDECREF(val);
+    return ret;
+}
+
+static PyObject *
+setPyVirTypedParameter(PyObject *info, virTypedParameterPtr params, int nparams)
+{
+    PyObject *key, *val;
+    PyObject *ret = NULL;
+    int i;
+
+    if (!info || !params)
+        return ret;
+
+    /* convert to a Python tuple of long objects */
+    for (i = 0; i < nparams; i++) {
+        key = libvirt_constcharPtrWrap(params[i].field);
+        val = PyDict_GetItem(info, key);
+        Py_DECREF(key);
+
+        if (val == NULL)
+            continue;
+
+        switch (params[i].type) {
+        case VIR_TYPED_PARAM_INT:
+            {
+                long long_val;
+                if (PyInt_Check(val)) {
+                    long_val = PyInt_AsLong(val);
+                    if ((long_val == -1) && PyErr_Occurred())
+                        return ret;
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be int");
+                    return ret;
+                }
+                params[i].value.i = (int)long_val;
+            }
+            break;
+        case VIR_TYPED_PARAM_UINT:
+            {
+                long long_val;
+                if (PyInt_Check(val)) {
+                    long_val = PyInt_AsLong(val);
+                    if ((long_val == -1) && PyErr_Occurred())
+                        return ret;
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be int");
+                    return ret;
+                }
+                params[i].value.ui = (unsigned int)long_val;
+            }
+            break;
+        case VIR_TYPED_PARAM_LLONG:
+            {
+                long long llong_val;
+                if (PyLong_Check(val)) {
+                    llong_val = PyLong_AsLongLong(val);
+                    if ((llong_val == -1) && PyErr_Occurred())
+                        return ret;
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be long");
+                    return ret;
+                }
+                params[i].value.l = llong_val;
+            }
+            break;
+        case VIR_TYPED_PARAM_ULLONG:
+            {
+                unsigned long long ullong_val;
+                if (PyLong_Check(val)) {
+                    ullong_val = PyLong_AsUnsignedLongLong(val);
+                    if ((ullong_val == -1) && PyErr_Occurred())
+                        return ret;
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be long");
+                    return ret;
+
+                }
+                params[i].value.ul = ullong_val;
+            }
+            break;
+        case VIR_TYPED_PARAM_DOUBLE:
+            {
+                double double_val;
+                if (PyFloat_Check(val)) {
+                    double_val = PyFloat_AsDouble(val);
+                    if ((double_val == -1) && PyErr_Occurred())
+                        return ret;
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be float");
+                    return ret;
+                }
+                params[i].value.d = double_val;
+            }
+            break;
+        case VIR_TYPED_PARAM_BOOLEAN:
+            {
+                /* Hack - Python's definition of Py_True breaks strict
+                 * aliasing rules, so can't directly compare
+                 */
+                if (PyBool_Check(val)) {
+                    PyObject *hacktrue = PyBool_FromLong(1);
+                    params[i].value.b = hacktrue == val ? 1: 0;
+                    Py_DECREF(hacktrue);
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be bool");
+                    return ret;
+                }
+            }
+            break;
+        case VIR_TYPED_PARAM_STRING:
+            {
+                if (PyString_Check(val)) {
+                    free(params[i].value.s);
+                    params[i].value.s = PyString_AsString(val);
+                } else {
+                    PyErr_SetString(PyExc_TypeError,
+                                    "Attribute value type must be string");
+                    return ret;
+                }
+            }
+            break;
+        default:
+            return ret;
+        }
+    }
+    return VIR_PY_NONE;
+}
+
 /************************************************************************
  *									*
  *		Statistics						*
@@ -1000,6 +1203,115 @@ libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
 }
 
 static PyObject *
+libvirt_virDomainSetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
+                                     PyObject *args)
+{
+    virDomainPtr domain;
+    PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
+    int i_retval;
+    int nparams = 0;
+    unsigned int flags;
+    virTypedParameterPtr params;
+
+    if (!PyArg_ParseTuple(args,
+                          (char *)"OOi:virDomainSetNumaParameters",
+                          &pyobj_domain, &info, &flags))
+        return(NULL);
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0)
+        return VIR_PY_INT_FAIL;
+
+    if ((params = malloc(sizeof(*params)*nparams)) == NULL)
+        return PyErr_NoMemory();
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, params, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
+    }
+
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainSetNumaParameters(domain, params, nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
+    }
+
+    /* The string generated by PyString_AsString
+     * must not be deallocated */
+    free(params);
+    return VIR_PY_INT_SUCCESS;
+fail:
+    /*same as above*/
+    free(params);
+    return ret;
+}
+
+static PyObject *
+libvirt_virDomainGetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
+                                     PyObject *args)
+{
+    virDomainPtr domain;
+    PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
+    int i_retval;
+    int nparams = 0;
+    unsigned int flags;
+    virTypedParameterPtr params;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetNumaParameters",
+                          &pyobj_domain, &flags))
+        return(NULL);
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0)
+        return VIR_PY_INT_FAIL;
+
+    if ((params = malloc(sizeof(*params)*nparams)) == NULL)
+        return PyErr_NoMemory();
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, params, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
+    }
+
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
+
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return info;
+
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return ret;
+}
+
+static PyObject *
 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
                           PyObject *args) {
     virDomainPtr domain;
@@ -5162,6 +5474,8 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetBlkioParameters", libvirt_virDomainGetBlkioParameters, METH_VARARGS, NULL},
     {(char *) "virDomainSetMemoryParameters", libvirt_virDomainSetMemoryParameters, METH_VARARGS, NULL},
     {(char *) "virDomainGetMemoryParameters", libvirt_virDomainGetMemoryParameters, METH_VARARGS, NULL},
+    {(char *) "virDomainSetNumaParameters", libvirt_virDomainSetNumaParameters, METH_VARARGS, NULL},
+    {(char *) "virDomainGetNumaParameters", libvirt_virDomainGetNumaParameters, METH_VARARGS, NULL},
     {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
     {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
     {(char *) "virDomainPinVcpuFlags", libvirt_virDomainPinVcpuFlags, METH_VARARGS, NULL},
-- 
1.7.7.5


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