Skip to content

Commit

Permalink
#792 / cpu_stats: freebsd impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 15, 2016
1 parent d4817f2 commit fb0154e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 19 deletions.
40 changes: 21 additions & 19 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,27 @@ CPU
Return various CPU statistics as a namedtuple whose fields change depending
on the platform.

+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD |
+=================+==============+==============+==============+============+=============+==============+
| ctx_switches | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| interrupts | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| soft_interrupts | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| procs_running | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| procs_blocked | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+------------+-------------+--------------+
ctx_switches, interrupts, soft_interrupts, syscalls, traps

+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD |
+=================+==============+==============+==============+=================+=============+==============+
| ctx_switches | | | | ctx_switches | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| interrupts | | | | interrupts | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| soft_interrupts | | | | soft_interrupts | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| procs_running | | | | syscalls | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| procs_blocked | | | | traps | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+
| | | | | | | |
+-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+

Example (Linux):

Expand Down
13 changes: 13 additions & 0 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
else:
sdiskio = namedtuple('sdiskio', ['read_count', 'write_count',
'read_bytes', 'write_bytes'])
if FREEBSD:
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts',
'syscalls', 'traps'])


# set later from __init__.py
NoSuchProcess = None
Expand Down Expand Up @@ -222,6 +227,14 @@ def cpu_count_physical():
return ret


def cpu_stats():
if FREEBSD:
ctx_switches, interrupts, soft_interrupts, syscalls, traps = \
cext.cpu_stats()
return scpustats(
ctx_switches, interrupts, soft_interrupts, syscalls, traps)


def boot_time():
"""The system boot time expressed in seconds since the epoch."""
return cext.boot_time()
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ PsutilMethods[] = {
"Return a Python dict of tuples for disk I/O information"},
{"users", psutil_users, METH_VARARGS,
"Return currently connected users as a list of tuples"},
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},
#if defined(__FreeBSD__) || defined(__NetBSD__)
{"net_connections", psutil_net_connections, METH_VARARGS,
"Return system-wide open connections."},
Expand Down
35 changes: 35 additions & 0 deletions psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,38 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
Py_DECREF(py_cpu_seq);
return NULL;
}


PyObject *
psutil_cpu_stats(PyObject *self, PyObject *args) {
unsigned int v_soft;
unsigned int v_intr;
unsigned int v_syscall;
unsigned int v_trap;
unsigned int v_swtch;
size_t size = sizeof(v_soft);

if (sysctlbyname("vm.stats.sys.v_soft", &v_soft, &size, NULL, 0))
goto error;
if (sysctlbyname("vm.stats.sys.v_intr", &v_intr, &size, NULL, 0))
goto error;
if (sysctlbyname("vm.stats.sys.v_syscall", &v_syscall, &size, NULL, 0))
goto error;
if (sysctlbyname("vm.stats.sys.v_trap", &v_trap, &size, NULL, 0))
goto error;
if (sysctlbyname("vm.stats.sys.v_swtch", &v_swtch, &size, NULL, 0))
goto error;

return Py_BuildValue(
"IIIII",
v_swtch, // ctx switches
v_intr, // interrupts
v_soft, // software interrupts
v_syscall, // syscalls
v_trap // traps
);

error:
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
1 change: 1 addition & 0 deletions psutil/arch/bsd/freebsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ PyObject* psutil_proc_num_threads(PyObject* self, PyObject* args);
PyObject* psutil_proc_threads(PyObject* self, PyObject* args);
PyObject* psutil_swap_mem(PyObject* self, PyObject* args);
PyObject* psutil_virtual_mem(PyObject* self, PyObject* args);
PyObject* psutil_cpu_stats(PyObject* self, PyObject* args);
20 changes: 20 additions & 0 deletions psutil/tests/test_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,26 @@ def test_muse_vmem_buffers(self):
self.assertAlmostEqual(psutil.virtual_memory().buffers, num,
delta=MEMORY_TOLERANCE)

def test_cpu_stats_ctx_switches(self):
self.assertAlmostEqual(psutil.cpu_stats().ctx_switches,
sysctl('vm.stats.sys.v_swtch'), delta=1000)

def test_cpu_stats_interrupts(self):
self.assertAlmostEqual(psutil.cpu_stats().interrupts,
sysctl('vm.stats.sys.v_intr'), delta=1000)

def test_cpu_stats_soft_interrupts(self):
self.assertAlmostEqual(psutil.cpu_stats().soft_interrupts,
sysctl('vm.stats.sys.v_soft'), delta=1000)

def test_cpu_stats_syscalls(self):
self.assertAlmostEqual(psutil.cpu_stats().syscalls,
sysctl('vm.stats.sys.v_syscall'), delta=1000)

def test_cpu_stats_traps(self):
self.assertAlmostEqual(psutil.cpu_stats().traps,
sysctl('vm.stats.sys.v_trap'), delta=1000)


# =====================================================================
# --- OpenBSD
Expand Down

0 comments on commit fb0154e

Please sign in to comment.