[libvirt] [PATCH 1/4] python: add python binding for virDomainGetBlkioParameters

--- python/libvirt-override-api.xml | 6 +- python/libvirt-override.c | 79 +++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml index a8929b1..00f8e6a 100644 --- a/python/libvirt-override-api.xml +++ b/python/libvirt-override-api.xml @@ -208,10 +208,10 @@ <arg name='params' type='virBlkioParameterPtr' info='pointer to blkio tunable objects'/> </function> <function name='virDomainGetBlkioParameters' file='python'> - <info>Get the blkio parameters, the @params array will be filled with the values.</info> - <return type='int' info='-1 in case of error, 0 in case of success.'/> + <info>Get the blkio parameters</info> + <return type='virSchedParameterPtr' info='None in case of error, returns a dictionary of params'/> <arg name='domain' type='virDomainPtr' info='pointer to domain object'/> - <arg name='params' type='virBlkioParameterPtr' info='pointer to blkio tunable objects'/> + <arg name='flags' type='int' info='an OR'ed set of virDomainModificationImpact'/> </function> <function name='virDomainSetMemoryParameters' file='python'> <info>Change the memory tunables</info> diff --git a/python/libvirt-override.c b/python/libvirt-override.c index 70e0238..beb0969 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -572,11 +572,84 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED, return VIR_PY_INT_FAIL; } -/* FIXME: This is a place holder for the implementation. */ static PyObject * libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED, - PyObject *args ATTRIBUTE_UNUSED) { - return VIR_PY_INT_FAIL; + PyObject *args) { + virDomainPtr domain; + PyObject *pyobj_domain, *info; + int i_retval; + int nparams = 0, i; + unsigned int flags; + virTypedParameterPtr params; + + if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetBlkioParameters", + &pyobj_domain, &flags)) + return(NULL); + domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) + return VIR_PY_NONE; + + if ((params = malloc(sizeof(*params)*nparams)) == NULL) + return VIR_PY_NONE; + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) { + free(params); + return VIR_PY_NONE; + } + + /* convert to a Python tuple of long objects */ + if ((info = PyDict_New()) == NULL) { + free(params); + return VIR_PY_NONE; + } + for (i = 0 ; i < nparams ; i++) { + PyObject *key, *val; + + 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((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_FromLongLong((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; + + default: + free(params); + Py_DECREF(info); + return VIR_PY_NONE; + } + + key = libvirt_constcharPtrWrap(params[i].field); + PyDict_SetItem(info, key, val); + } + free(params); + return(info); } /* FIXME: This is a place holder for the implementation. */ -- 1.7.3.1

--- python/libvirt-override-api.xml | 1 + python/libvirt-override.c | 94 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml index 00f8e6a..928bfb7 100644 --- a/python/libvirt-override-api.xml +++ b/python/libvirt-override-api.xml @@ -206,6 +206,7 @@ <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='virBlkioParameterPtr' info='pointer to blkio tunable objects'/> + <arg name='flags' type='int' info='an OR'ed set of virDomainModificationImpact'/> </function> <function name='virDomainGetBlkioParameters' file='python'> <info>Get the blkio parameters</info> diff --git a/python/libvirt-override.c b/python/libvirt-override.c index beb0969..e53451f 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -565,11 +565,99 @@ libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED, } -/* FIXME: This is a place holder for the implementation. */ static PyObject * libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED, - PyObject *args ATTRIBUTE_UNUSED) { - return VIR_PY_INT_FAIL; + PyObject *args) { + virDomainPtr domain; + PyObject *pyobj_domain, *info; + int i_retval; + int nparams = 0, i; + unsigned int flags; + virTypedParameterPtr params; + + if (!PyArg_ParseTuple(args, + (char *)"OOi:virDomainSetBlkioParameters", + &pyobj_domain, &info, &flags)) + return(NULL); + domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetBlkioParameters(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 VIR_PY_INT_FAIL; + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) { + free(params); + return VIR_PY_INT_FAIL; + } + + /* convert to a Python tuple of long objects */ + for (i = 0; i < nparams; i++) { + PyObject *key, *val; + 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: + params[i].value.i = (int)PyInt_AS_LONG(val); + break; + + case VIR_TYPED_PARAM_UINT: + params[i].value.ui = (unsigned int)PyInt_AS_LONG(val); + break; + + case VIR_TYPED_PARAM_LLONG: + params[i].value.l = (long long)PyLong_AsLongLong(val); + break; + + case VIR_TYPED_PARAM_ULLONG: + params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val); + break; + + case VIR_TYPED_PARAM_DOUBLE: + params[i].value.d = (double)PyFloat_AsDouble(val); + break; + + case VIR_TYPED_PARAM_BOOLEAN: + { + /* Hack - Python's definition of Py_True breaks strict + * aliasing rules, so can't directly compare :-( + */ + PyObject *hacktrue = PyBool_FromLong(1); + params[i].value.b = hacktrue == val ? 1: 0; + Py_DECREF(hacktrue); + } + break; + + default: + free(params); + return VIR_PY_INT_FAIL; + } + } + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainSetBlkioParameters(domain, params, nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + if (i_retval < 0) { + free(params); + return VIR_PY_INT_FAIL; + } + + free(params); + return VIR_PY_INT_SUCCESS; } static PyObject * -- 1.7.3.1

On 27.07.2011 04:13, Hu Tao wrote:
--- python/libvirt-override-api.xml | 1 + python/libvirt-override.c | 94 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-)
ACK, but again, we need update of virDomainSetBlkioParameters description. I'll collect all of these and push it then as one patch. Michal

On Thu, Jul 28, 2011 at 10:15:21AM +0200, Michal Privoznik wrote:
On 27.07.2011 04:13, Hu Tao wrote:
--- python/libvirt-override-api.xml | 1 + python/libvirt-override.c | 94 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-)
ACK, but again, we need update of virDomainSetBlkioParameters description. I'll collect all of these and push it then as one patch.
Thanks. -- Thanks, Hu Tao

--- python/libvirt-override-api.xml | 6 +- python/libvirt-override.c | 79 +++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml index 928bfb7..0a67f9d 100644 --- a/python/libvirt-override-api.xml +++ b/python/libvirt-override-api.xml @@ -221,10 +221,10 @@ <arg name='params' type='virMemoryParameterPtr' info='pointer to memory tunable objects'/> </function> <function name='virDomainGetMemoryParameters' file='python'> - <info>Get the memory parameters, the @params array will be filled with the values.</info> - <return type='int' info='-1 in case of error, 0 in case of success.'/> + <info>Get the memory parameters</info> + <return type='virSchedParameterPtr' info='None in case of error, returns a dictionary of params'/> <arg name='domain' type='virDomainPtr' info='pointer to domain object'/> - <arg name='params' type='virMemoryParameterPtr' info='pointer to memory tunable objects'/> + <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> diff --git a/python/libvirt-override.c b/python/libvirt-override.c index e53451f..7ab67e9 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -747,11 +747,84 @@ libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED, return VIR_PY_INT_FAIL; } -/* FIXME: This is a place holder for the implementation. */ static PyObject * libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED, - PyObject *args ATTRIBUTE_UNUSED) { - return VIR_PY_INT_FAIL; + PyObject *args) { + virDomainPtr domain; + PyObject *pyobj_domain, *info; + int i_retval; + int nparams = 0, i; + unsigned int flags; + virTypedParameterPtr params; + + if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetMemoryParameters", + &pyobj_domain, &flags)) + return(NULL); + domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetMemoryParameters(domain, NULL, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) + return VIR_PY_NONE; + + if ((params = malloc(sizeof(*params)*nparams)) == NULL) + return VIR_PY_NONE; + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetMemoryParameters(domain, params, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) { + free(params); + return VIR_PY_NONE; + } + + /* convert to a Python tuple of long objects */ + if ((info = PyDict_New()) == NULL) { + free(params); + return VIR_PY_NONE; + } + for (i = 0 ; i < nparams ; i++) { + PyObject *key, *val; + + 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((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_FromLongLong((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; + + default: + free(params); + Py_DECREF(info); + return VIR_PY_NONE; + } + + key = libvirt_constcharPtrWrap(params[i].field); + PyDict_SetItem(info, key, val); + } + free(params); + return(info); } static PyObject * -- 1.7.3.1

On 27.07.2011 04:13, Hu Tao wrote:
--- python/libvirt-override-api.xml | 6 +- python/libvirt-override.c | 79 +++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-)
ACK Michal

--- python/libvirt-override-api.xml | 1 + python/libvirt-override.c | 94 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml index 0a67f9d..2fa5eed 100644 --- a/python/libvirt-override-api.xml +++ b/python/libvirt-override-api.xml @@ -219,6 +219,7 @@ <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='virMemoryParameterPtr' info='pointer to memory tunable objects'/> + <arg name='flags' type='int' info='an OR'ed set of virDomainModificationImpact'/> </function> <function name='virDomainGetMemoryParameters' file='python'> <info>Get the memory parameters</info> diff --git a/python/libvirt-override.c b/python/libvirt-override.c index 7ab67e9..d4e536e 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -740,11 +740,99 @@ libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED, return(info); } -/* FIXME: This is a place holder for the implementation. */ static PyObject * libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED, - PyObject *args ATTRIBUTE_UNUSED) { - return VIR_PY_INT_FAIL; + PyObject *args) { + virDomainPtr domain; + PyObject *pyobj_domain, *info; + int i_retval; + int nparams = 0, i; + unsigned int flags; + virTypedParameterPtr params; + + if (!PyArg_ParseTuple(args, + (char *)"OOi:virDomainSetMemoryParameters", + &pyobj_domain, &info, &flags)) + return(NULL); + domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetMemoryParameters(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 VIR_PY_INT_FAIL; + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainGetMemoryParameters(domain, params, &nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) { + free(params); + return VIR_PY_INT_FAIL; + } + + /* convert to a Python tuple of long objects */ + for (i = 0; i < nparams; i++) { + PyObject *key, *val; + 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: + params[i].value.i = (int)PyInt_AS_LONG(val); + break; + + case VIR_TYPED_PARAM_UINT: + params[i].value.ui = (unsigned int)PyInt_AS_LONG(val); + break; + + case VIR_TYPED_PARAM_LLONG: + params[i].value.l = (long long)PyLong_AsLongLong(val); + break; + + case VIR_TYPED_PARAM_ULLONG: + params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val); + break; + + case VIR_TYPED_PARAM_DOUBLE: + params[i].value.d = (double)PyFloat_AsDouble(val); + break; + + case VIR_TYPED_PARAM_BOOLEAN: + { + /* Hack - Python's definition of Py_True breaks strict + * aliasing rules, so can't directly compare :-( + */ + PyObject *hacktrue = PyBool_FromLong(1); + params[i].value.b = hacktrue == val ? 1: 0; + Py_DECREF(hacktrue); + } + break; + + default: + free(params); + return VIR_PY_INT_FAIL; + } + } + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virDomainSetMemoryParameters(domain, params, nparams, flags); + LIBVIRT_END_ALLOW_THREADS; + if (i_retval < 0) { + free(params); + return VIR_PY_INT_FAIL; + } + + free(params); + return VIR_PY_INT_SUCCESS; } static PyObject * -- 1.7.3.1

On 27.07.2011 04:13, Hu Tao wrote:
--- python/libvirt-override-api.xml | 1 + python/libvirt-override.c | 94 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-)
ACK. I pushed the whole set and update of flags description for virDomainSetBlkioParameters and virDomainGetBlkioParameters. Michal

On 27.07.2011 04:13, Hu Tao wrote:
--- python/libvirt-override-api.xml | 6 +- python/libvirt-override.c | 79 +++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-)
ACK It actually exposed we need this update: diff --git a/src/libvirt.c b/src/libvirt.c index eeaf0b6..2ce391c 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -3516,7 +3516,7 @@ error: * @params: pointer to blkio parameter object * (return value, allocated by the caller) * @nparams: pointer to number of blkio parameters - * @flags: currently unused, for future extension + * @flags: an OR'ed set of virDomainModificationImpact * * Get all blkio parameters, the @params array will be filled with the values * equal to the number of parameters suggested by @nparams. But that will better be a follow up patch. Michal
participants (2)
-
Hu Tao
-
Michal Privoznik