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

[Xen-devel] [PATCH] 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@xxxxxxxxxx>
---
 tools/python/xen/lowlevel/xc/xc.c | 41 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/tools/python/xen/lowlevel/xc/xc.c 
b/tools/python/xen/lowlevel/xc/xc.c
index 737bdac..cb34446 100644
--- a/tools/python/xen/lowlevel/xc/xc.c
+++ b/tools/python/xen/lowlevel/xc/xc.c
@@ -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"
-- 
1.8.3.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.