Skip to content

Commit

Permalink
Merge pull request #537 from ddaeschler/master
Browse files Browse the repository at this point in the history
Fix psutil_proc_cpu_affinity_get to work without CPU_ALLOC
  • Loading branch information
giampaolo committed Sep 26, 2014
2 parents 20fd80b + 05f95a8 commit fc024a3
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ psutil_linux_sysinfo(PyObject *self, PyObject *args)
}


#ifdef CPU_ALLOC

/*
* Return process CPU affinity as a Python long (the bitmask)
* Return process CPU affinity as a Python list
*/
static PyObject *
psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args)
Expand Down Expand Up @@ -317,7 +319,42 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args)
Py_XDECREF(res);
return NULL;
}
#else

/*
* Return process CPU affinity as a Python list (when the system doesnt support CPU_ALLOC)
*/
static PyObject *
psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args)
{
cpu_set_t cpuset;
unsigned int len = sizeof(cpu_set_t);
long pid;
int i;
PyObject* ret_list;

if (!PyArg_ParseTuple(args, "i", &pid)) {
return NULL;
}

CPU_ZERO(&cpuset);
if (sched_getaffinity(pid, len, &cpuset) < 0) {
return PyErr_SetFromErrno(PyExc_OSError);
}

ret_list = PyList_New(0);

for (i = 0; i < CPU_SETSIZE; ++i)
{
if (CPU_ISSET(i, &cpuset))
{
PyList_Append(ret_list, Py_BuildValue("i", i));
}
}

return ret_list;
}
#endif

/*
* Set process CPU affinity; expects a bitmask
Expand Down Expand Up @@ -374,8 +411,10 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args)
return Py_None;

error:
if (py_cpu_seq != NULL)
if (py_cpu_seq != NULL) {
Py_DECREF(py_cpu_seq);
}

return NULL;
}

Expand Down

0 comments on commit fc024a3

Please sign in to comment.