[libvirt] [PATCHv3] virNodeGetCPUMap: Add python binding

Added a method getCPUMap to virConnect. It can be used as follows: import libvirt import sys import os conn = libvirt.openReadOnly(None) if conn == None: print 'Failed to open connection to the hypervisor' sys.exit(1) try: (cpus, cpumap, online) = conn.getCPUMap(0) except: print 'Failed to extract the node cpu map information' sys.exit(1) print 'CPUs total %d, online %d' % (cpus, online) print 'CPU map %s' % str(cpumap) del conn print "OK" sys.exit(0) Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com> --- Rework of patch 9/9 of initial virNodeGetCPUMap series. squashed in Eric Blake's changes added missing error handling reordered return value processing Note: The code is a bit verbose, e.g. the NULL checks, but that seems to be the prevailing style here. --- python/libvirt-override-api.xml | 6 +++ python/libvirt-override.c | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 0 deletions(-) diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml index b76fb4e..e54701c 100644 --- a/python/libvirt-override-api.xml +++ b/python/libvirt-override-api.xml @@ -542,5 +542,11 @@ <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/> <arg name='flags' type='int' info='unused, always pass 0'/> </function> + <function name='virNodeGetCPUMap' file='python'> + <info>Get node CPU information</info> + <return type='str *' info='(cpunum, online, cpumap) on success, None on error'/> + <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/> + <arg name='flags' type='int' info='unused, pass 0'/> + </function> </symbols> </api> diff --git a/python/libvirt-override.c b/python/libvirt-override.c index 83dc925..0609803 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -6397,6 +6397,75 @@ cleanup: return ret; } +static PyObject * +libvirt_virNodeGetCPUMap(PyObject *self ATTRIBUTE_UNUSED, + PyObject *args) +{ + virConnectPtr conn; + PyObject *pyobj_conn; + PyObject *ret = NULL; + PyObject *pycpumap = NULL; + PyObject *pyused = NULL; + PyObject *pycpunum = NULL; + PyObject *pyonline = NULL; + int i_retval; + unsigned char *cpumap = NULL; + unsigned int online = 0; + unsigned int flags; + int i; + + if (!PyArg_ParseTuple(args, (char *)"Oi:virNodeGetCPUMap", + &pyobj_conn, &flags)) + return NULL; + conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn); + + LIBVIRT_BEGIN_ALLOW_THREADS; + i_retval = virNodeGetCPUMap(conn, &cpumap, &online, flags); + LIBVIRT_END_ALLOW_THREADS; + + if (i_retval < 0) + return VIR_PY_NONE; + + if ((ret = PyTuple_New(3)) == NULL) + goto error; + + /* 0: number of CPUs */ + if ((pycpunum = PyLong_FromLong(i_retval)) == NULL || + PyTuple_SetItem(ret, 0, pycpunum) < 0) + goto error; + + /* 1: CPU map */ + if ((pycpumap = PyList_New(i_retval)) == NULL) + goto error; + + for (i = 0; i < i_retval; i++) { + if ((pyused = PyBool_FromLong(VIR_CPU_USED(cpumap, i))) == NULL) + goto error; + if (PyList_SetItem(pycpumap, i, pyused) < 0) + goto error; + } + + if (PyTuple_SetItem(ret, 1, pycpumap) < 0) + goto error; + + /* 2: number of online CPUs */ + if((pyonline = PyLong_FromLong(online)) == NULL || + PyTuple_SetItem(ret, 2, pyonline) < 0) + goto error; + +cleanup: + VIR_FREE(cpumap); + return ret; +error: + Py_XDECREF(ret); + Py_XDECREF(pycpumap); + Py_XDECREF(pyused); + Py_XDECREF(pycpunum); + Py_XDECREF(pyonline); + ret = NULL; + goto cleanup; +} + /************************************************************************ * * @@ -6514,6 +6583,7 @@ static PyMethodDef libvirtMethods[] = { {(char *) "virDomainGetDiskErrors", libvirt_virDomainGetDiskErrors, METH_VARARGS, NULL}, {(char *) "virNodeGetMemoryParameters", libvirt_virNodeGetMemoryParameters, METH_VARARGS, NULL}, {(char *) "virNodeSetMemoryParameters", libvirt_virNodeSetMemoryParameters, METH_VARARGS, NULL}, + {(char *) "virNodeGetCPUMap", libvirt_virNodeGetCPUMap, METH_VARARGS, NULL}, {NULL, NULL, 0, NULL} }; -- 1.7.0.4

On 10/25/2012 08:59 AM, Viktor Mihajlovski wrote:
Added a method getCPUMap to virConnect. It can be used as follows:
import libvirt import sys import os
conn = libvirt.openReadOnly(None) if conn == None: print 'Failed to open connection to the hypervisor' sys.exit(1)
try: (cpus, cpumap, online) = conn.getCPUMap(0)
+ <function name='virNodeGetCPUMap' file='python'> + <info>Get node CPU information</info> + <return type='str *' info='(cpunum, online, cpumap) on success, None on error'/>
Oops, docs disagree with commit message on tuple ordering.
+ /* 0: number of CPUs */ + if ((pycpunum = PyLong_FromLong(i_retval)) == NULL || + PyTuple_SetItem(ret, 0, pycpunum) < 0) + goto error; + + /* 1: CPU map */
But code matched the commit message, so I'll fix the docs.
+ /* 2: number of online CPUs */ + if((pyonline = PyLong_FromLong(online)) == NULL ||
Style - space after 'if'.
+ PyTuple_SetItem(ret, 2, pyonline) < 0) + goto error;
Indentation (but that's caused by the earlier spacing after 'if').
+
ACK and pushed with this squashed in: diff --git i/python/libvirt-override-api.xml w/python/libvirt-override-api.xml index e54701c..a0e0496 100644 --- i/python/libvirt-override-api.xml +++ w/python/libvirt-override-api.xml @@ -544,7 +544,7 @@ </function> <function name='virNodeGetCPUMap' file='python'> <info>Get node CPU information</info> - <return type='str *' info='(cpunum, online, cpumap) on success, None on error'/> + <return type='str *' info='(cpunum, cpumap, online) on success, None on error'/> <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/> <arg name='flags' type='int' info='unused, pass 0'/> </function> diff --git i/python/libvirt-override.c w/python/libvirt-override.c index 0609803..320f26a 100644 --- i/python/libvirt-override.c +++ w/python/libvirt-override.c @@ -6449,8 +6449,8 @@ libvirt_virNodeGetCPUMap(PyObject *self ATTRIBUTE_UNUSED, goto error; /* 2: number of online CPUs */ - if((pyonline = PyLong_FromLong(online)) == NULL || - PyTuple_SetItem(ret, 2, pyonline) < 0) + if ((pyonline = PyLong_FromLong(online)) == NULL || + PyTuple_SetItem(ret, 2, pyonline) < 0) goto error; cleanup: -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 10/25/2012 07:34 PM, Eric Blake wrote:
ACK and pushed with this squashed in:
Thanks (also for guiding me through my first API). -- Mit freundlichen Grüßen/Kind Regards Viktor Mihajlovski IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Martin Jetter Geschäftsführung: Dirk Wittkopp Sitz der Gesellschaft: Böblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
participants (2)
-
Eric Blake
-
Viktor Mihajlovski