[libvirt] [PATCH v4] python: refactoring virTypedParameter conversion for NUMA tuning APIs

Guannan Ren gren at redhat.com
Wed Feb 8 12:20:50 UTC 2012


          *getPyVirTypedParameter
          *setPyVirTypedParameter
          *virDomainSetNumaParameters
          *virDomainGetNumaParameters
---
 python/libvirt-override-api.xml |   13 ++
 python/libvirt-override.c       |  385 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 398 insertions(+), 0 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index b2b8152..8eed0bb 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'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'ed set of virDomainModificationImpact'/>
+    </function>
+    <function name='virDomainGetNumaParameters' file='python'>
+      <info>Get the NUMA parameters</info>
+      <return type='str *' info='returns a dictionary of params in case of success, None in case of error'/>
+      <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
+      <arg name='flags' type='int' info='an OR'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 33a841d..3c6020b 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -22,6 +22,7 @@
 #include "typewrappers.h"
 #include "libvirt.h"
 #include "memory.h"
+#include "virtypedparam.h"
 
 #ifndef __CYGWIN__
 extern void initlibvirtmod(void);
@@ -62,6 +63,263 @@ 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 int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+getPyVirTypedParameter(PyObject **info,
+                       const virTypedParameterPtr params, int nparams)
+{
+    PyObject *key, *val;
+    int i;
+
+    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:
+            return 0;
+        }
+
+        key = libvirt_constcharPtrWrap(params[i].field);
+        if (!key || !val)
+            goto cleanup;
+
+        if (PyDict_SetItem(*info, key, val) < 0)
+            goto cleanup;
+
+        Py_DECREF(key);
+        Py_DECREF(val);
+    }
+    return 1;
+cleanup:
+    Py_XDECREF(key);
+    Py_XDECREF(val);
+    return -1;
+}
+
+/* Allocate a new typed parameter array with the same contents and
+ * length as info, and using the array params of length nparams as
+ * hints on what types to use when creating the new array. Return
+ * NULL on failure. */
+static virTypedParameterPtr ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+setPyVirTypedParameter(PyObject *info,
+                       const virTypedParameterPtr params, int nparams)
+{
+    PyObject *key, *value;
+    Py_ssize_t pos = 0;
+    virTypedParameterPtr temp = NULL, ret = NULL;
+    int size, i;
+
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(ret, size) < 0) {
+        PyErr_NoMemory();
+        return NULL;
+    }
+
+    temp = &ret[0];
+    while (PyDict_Next(info, &pos, &key, &value)) {
+        char *keystr = NULL;
+        bool found = false;
+
+        if (PyString_Check(key)) {
+            if ((keystr = PyString_AsString(key)) == NULL)
+                goto cleanup;
+        } else {
+            PyErr_Format(PyExc_TypeError,
+                            "Attribut name must be string");
+            goto cleanup;
+        }
+
+        for (i = 0; i < nparams; i++) {
+            if (STREQ(params[i].field, keystr)) {
+                found = true;
+                strncpy(temp->field, params[i].field,
+                        sizeof(temp->field));
+                temp->type = params[i].type;
+
+                switch(params[i].type) {
+                case VIR_TYPED_PARAM_INT:
+                    {
+                        long long_val;
+                        if (PyInt_Check(value)) {
+                            long_val = PyInt_AsLong(value);
+                            if ((long_val == -1) && PyErr_Occurred())
+                                goto cleanup;
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                        "The value type of "
+                                        "attribute \"%s\" must be int", keystr);
+                            goto cleanup;
+                        }
+
+                        if ((int)long_val == long_val) {
+                            temp->value.i = (int)long_val;
+                        } else {
+                            PyErr_Format(PyExc_ValueError,
+                                         "The value of "
+                                         "attribute \"%s\" is out of int range", keystr);
+                            goto cleanup;
+                       }
+                    }
+                    break;
+                case VIR_TYPED_PARAM_UINT:
+                    {
+                        long long_val;
+                        if (PyInt_Check(value)) {
+                            long_val = PyInt_AsLong(value);
+                            if ((long_val == -1) && PyErr_Occurred())
+                                goto cleanup;
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                         "The value type of "
+                                         "attribute \"%s\" must be int", keystr);
+                            goto cleanup;
+                        }
+
+                        if ((unsigned int)long_val == long_val) {
+                            temp->value.ui = (unsigned int)long_val;
+                        } else {
+                            PyErr_Format(PyExc_ValueError,
+                                         "The value of "
+                                         "attribute \"%s\" is out of int range", keystr);
+                            goto cleanup;
+                        }
+                    }
+                    break;
+                case VIR_TYPED_PARAM_LLONG:
+                    {
+                        long long llong_val;
+                        if (PyLong_Check(value)) {
+                            llong_val = PyLong_AsLongLong(value);
+                            if ((llong_val == -1) && PyErr_Occurred())
+                                goto cleanup;
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                         "The value type of "
+                                         "attribute \"%s\" must be long", keystr);
+                            goto cleanup;
+                        }
+                        temp->value.l = llong_val;
+                    }
+                    break;
+                case VIR_TYPED_PARAM_ULLONG:
+                    {
+                        unsigned long long ullong_val;
+                        if (PyLong_Check(value)) {
+                            ullong_val = PyLong_AsUnsignedLongLong(value);
+                            if ((ullong_val == -1) && PyErr_Occurred())
+                                goto cleanup;
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                         "The value type of "
+                                         "attribute \"%s\" must be long", keystr);
+                            goto cleanup;
+
+                        }
+                        temp->value.ul = ullong_val;
+                    }
+                    break;
+                case VIR_TYPED_PARAM_DOUBLE:
+                    {
+                        double double_val;
+                        if (PyFloat_Check(value)) {
+                            double_val = PyFloat_AsDouble(value);
+                            if ((double_val == -1) && PyErr_Occurred())
+                                goto cleanup;
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                        "The value type of "
+                                        "attribute \"%s\" must be float", keystr);
+                           goto cleanup;
+                        }
+                        temp->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(value)) {
+                            PyObject *hacktrue = PyBool_FromLong(1);
+                            temp->value.b = hacktrue == value ? 1: 0;
+                            Py_DECREF(hacktrue);
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                         "The value type of "
+                                         "attribute \"%s\" must be bool", keystr);
+                            goto cleanup;
+                        }
+                    }
+                    break;
+                case VIR_TYPED_PARAM_STRING:
+                    {
+                        char *string_val;
+                        if (PyString_Check(value)) {
+                            if ((string_val = PyString_AsString(value)) == NULL)
+                                goto cleanup;
+                            if ((temp->value.s = strdup(string_val)) == NULL) {
+                                PyErr_NoMemory();
+                                goto cleanup;
+                            }
+                        } else {
+                            PyErr_Format(PyExc_TypeError,
+                                         "The value type of "
+                                         "attribute \"%s\" must be string", keystr);
+                            goto cleanup;
+                        }
+                    }
+                    break;
+                default:
+                    goto cleanup;
+                }
+            }
+        }
+
+        if (i == nparams && !found) {
+                PyErr_Format(PyExc_LookupError,
+                            "Attribute name \"%s\" could not be recognized", keystr);
+                goto cleanup;
+        }
+
+        temp++;
+    }
+    return ret;
+
+cleanup:
+    virTypedParameterArrayClear(ret, size);
+    VIR_FREE(ret);
+    return NULL;
+}
+
 /************************************************************************
  *									*
  *		Statistics						*
@@ -1007,6 +1265,131 @@ 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, size = 0;
+    unsigned int flags;
+    virTypedParameterPtr params, new_params;
+
+    if (!PyArg_ParseTuple(args,
+                          (char *)"OOi:virDomainSetNumaParameters",
+                          &pyobj_domain, &info, &flags))
+        return NULL;
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0 || !nparams)
+        return VIR_PY_INT_FAIL;
+
+    if (VIR_ALLOC_N(params, nparams) < 0)
+        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 cleanup;
+    }
+
+    new_params = setPyVirTypedParameter(info, params, nparams);
+    if (!new_params)
+        goto cleanup;
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainSetNumaParameters(domain, new_params, size, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto cleanup;
+    }
+
+    ret = VIR_PY_INT_SUCCESS;
+
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
+    VIR_FREE(params);
+    virTypedParameterArrayClear(new_params, size);
+    VIR_FREE(new_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);
+
+    if ((info = PyDict_New()) == NULL) {
+        return ret;
+    }
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0)
+        return VIR_PY_NONE;
+
+    if (!nparams)
+        return info;
+
+    if (VIR_ALLOC_N(params, nparams) < 0)
+        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_NONE;
+        goto cleanup;
+    }
+
+    i_retval = getPyVirTypedParameter(&info, params, nparams);
+
+    if (i_retval < 0) {
+        Py_XDECREF(info);
+        goto cleanup;
+    } else if (i_retval == 0) {
+        Py_XDECREF(info);
+        ret = VIR_PY_NONE;
+        goto cleanup;
+    } else {
+        ret = info;
+    }
+
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
+    VIR_FREE(params);
+    return ret;
+}
+
+static PyObject *
 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
                           PyObject *args) {
     virDomainPtr domain;
@@ -5203,6 +5586,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




More information about the libvir-list mailing list