tools/python: expose xc_getcpuinfo()

This API can be used to get per physical CPU utilization.

Testing:

      # python
      >>> import xen.lowlevel.xc
      >>> xc = xen.lowlevel.xc.xc()
      >>> xc.getcpuinfo()
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: Required argument 'max_cpus' (pos 1) not found
      >>> xc.getcpuinfo(4)
      [{'idletime': 109322086128854}, {'idletime': 109336447648802},
      {'idletime': 109069270544960}, {'idletime': 109065612611363}]
      >>> xc.getcpuinfo(100)
      [{'idletime': 109639015806078}, {'idletime': 109654551195681},
      {'idletime': 109382107891193}, {'idletime': 109382057541119}]
      >>> xc.getcpuinfo(1)
      [{'idletime': 109682068418798}]
      >>> xc.getcpuinfo(2)
      [{'idletime': 109711311201330}, {'idletime': 109728458214729}]
      >>> xc.getcpuinfo(max_cpus=4)
      [{'idletime': 109747116214638}, {'idletime': 109764982453261},
      {'idletime': 109491373228931}, {'idletime': 109489858724432}]

Signed-off-by: Zhigang Wang <zhigang.x.wang@oracle.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
This commit is contained in:
Zhigang Wang authored and Ian Campbell committed 2014-05-15 16:12:52 +01:00
1 parent 9aa2cef51f
commit a9958947e4
1 file changed
+41
+41
View File
@@ -1183,6 +1183,40 @@ static PyObject *pyxc_physinfo(XcObject *self)
"virt_caps", virt_caps);
}
static PyObject *pyxc_getcpuinfo(XcObject *self, PyObject *args, PyObject *kwds)
{
xc_cpuinfo_t *cpuinfo, *cpuinfo_ptr;
PyObject *cpuinfo_list_obj, *cpuinfo_obj;
int max_cpus, nr_cpus, ret, i;
static char *kwd_list[] = { "max_cpus", NULL };
static char kwd_type[] = "i";
if(!PyArg_ParseTupleAndKeywords(args, kwds, kwd_type, kwd_list, &max_cpus))
return NULL;
cpuinfo = malloc(sizeof(xc_cpuinfo_t) * max_cpus);
if (!cpuinfo)
return NULL;
ret = xc_getcpuinfo(self->xc_handle, max_cpus, cpuinfo, &nr_cpus);
if (ret != 0) {
free(cpuinfo);
return pyxc_error_to_exception(self->xc_handle);
}
cpuinfo_list_obj = PyList_New(0);
cpuinfo_ptr = cpuinfo;
for (i = 0; i < nr_cpus; i++) {
cpuinfo_obj = Py_BuildValue("{s:k}", "idletime", cpuinfo_ptr->idletime);
PyList_Append(cpuinfo_list_obj, cpuinfo_obj);
cpuinfo_ptr++;
}
free(cpuinfo);
return cpuinfo_list_obj;
}
static PyObject *pyxc_topologyinfo(XcObject *self)
{
#define MAX_CPU_INDEX 255
@@ -2611,6 +2645,13 @@ static PyMethodDef pyxc_methods[] = {
"Returns [dict]: information about the hardware"
" [None]: on failure.\n" },
{ "getcpuinfo",
(PyCFunction)pyxc_getcpuinfo,
METH_VARARGS | METH_KEYWORDS, "\n"
"Get information about physical CPUs\n"
"Returns [list]: information about physical CPUs"
" [None]: on failure.\n" },
{ "topologyinfo",
(PyCFunction)pyxc_topologyinfo,
METH_NOARGS, "\n"
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.