Skip to content

Commit

Permalink
fix #1069 / freebsd: cpu_num() may return 255; now returns -1
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 14, 2017
1 parent 06ded74 commit fd2205b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
- 1064_: [NetBSD] swap_memory() may segfault in case of error.
- 1065_: [OpenBSD] Process.cmdline() may raise SystemError.
- 1067_: [NetBSD] Process.cmdline() leaks memory if proces has terminated.
- 1069_: [FreeBSD] Process.cpu_num() may return 255 for certain kernel
processes.

**Porting notes**

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ Process class

Return what CPU this process is currently running on.
The returned number should be ``<=`` :func:`psutil.cpu_count()`.
On FreeBSD certain kernel process may return ``-1``.
It may be used in conjunction with ``psutil.cpu_percent(percpu=True)`` to
observe the system workload distributed across multiple CPUs as shown by
`cpu_distribution.py <https://github.com/giampaolo/psutil/blob/master/scripts/cpu_distribution.py>`__ example script.
Expand Down
7 changes: 4 additions & 3 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
long memtext;
long memdata;
long memstack;
unsigned char oncpu;
int oncpu;
kinfo_proc kp;
long pagesize = sysconf(_SC_PAGESIZE);
char str[1000];
Expand Down Expand Up @@ -252,6 +252,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
// what CPU we're on; top was used as an example:
// https://svnweb.freebsd.org/base/head/usr.bin/top/machine.c?
// view=markup&pathrev=273835
// XXX - note: for "intr" PID this is -1.
if (kp.ki_stat == SRUN && kp.ki_oncpu != NOCPU)
oncpu = kp.ki_oncpu;
else
Expand Down Expand Up @@ -300,7 +301,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
memdata, // (long) mem data
memstack, // (long) mem stack
// others
oncpu, // (unsigned char) the CPU we are on
oncpu, // (int) the CPU we are on
#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD)
//
(long)kp.p_ppid, // (long) ppid
Expand Down Expand Up @@ -336,7 +337,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
memdata, // (long) mem data
memstack, // (long) mem stack
// others
oncpu, // (unsigned char) the CPU we are on
oncpu, // (int) the CPU we are on
#endif
py_name // (pystr) name
);
Expand Down
4 changes: 3 additions & 1 deletion psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,12 @@ def cpu_percent(self, ret, proc):

def cpu_num(self, ret, proc):
self.assertIsInstance(ret, int)
if FREEBSD and ret == -1:
return
self.assertGreaterEqual(ret, 0)
if psutil.cpu_count() == 1:
self.assertEqual(ret, 0)
self.assertIn(ret, range(psutil.cpu_count()))
self.assertIn(ret, list(range(psutil.cpu_count())))

def memory_info(self, ret, proc):
assert is_namedtuple(ret)
Expand Down

0 comments on commit fd2205b

Please sign in to comment.