Skip to content

Commit

Permalink
#792 / cpu_stats: openbsd impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 14, 2016
1 parent fbda723 commit 7b260a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
30 changes: 15 additions & 15 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
'pmmap_grouped', 'path rss, private, ref_count, shadow_count')
pmmap_ext = namedtuple(
'pmmap_ext', 'addr, perms path rss, private, ref_count, shadow_count')
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts', 'syscalls',
'traps'])
if FREEBSD:
sdiskio = namedtuple('sdiskio', ['read_count', 'write_count',
'read_bytes', 'write_bytes',
Expand All @@ -114,10 +117,6 @@
else:
sdiskio = namedtuple('sdiskio', ['read_count', 'write_count',
'read_bytes', 'write_bytes'])
if FREEBSD or NETBSD:
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts',
'syscalls', 'traps'])


# set later from __init__.py
Expand Down Expand Up @@ -229,28 +228,29 @@ def cpu_count_physical():

def cpu_stats():
if FREEBSD:
ctx_switches, interrupts, soft_interrupts, syscalls, traps = \
cext.cpu_stats()
return scpustats(
ctx_switches, interrupts, soft_interrupts, syscalls, traps)
ctxsw, intrs, soft_intrs, syscalls, traps = cext.cpu_stats()
elif NETBSD:
# XXX
# Note about interrupts: the C extension returns 0. interrupts
# Note about intrs: the C extension returns 0. intrs
# can be determined via /proc/stat; it has the same value as
# soft_interrupts thought so the kernel is faking it (?).
# soft_intrs thought so the kernel is faking it (?).
#
# Note about syscalls: the C extension always sets it to 0 (?).
#
# Note: the C ext is returning two metrics we are not returning:
# faults and forks.
(ctx_switches, interrupts, soft_interrupts, syscalls, traps, faults,
forks) = cext.cpu_stats()
ctxsw, intrs, soft_intrs, syscalls, traps, faults, forks = \
cext.cpu_stats()
with open('/proc/stat', 'rb') as f:
for line in f:
if line.startswith(b'intr'):
interrupts = int(line.split()[1])
return scpustats(
ctx_switches, interrupts, soft_interrupts, syscalls, traps)
intrs = int(line.split()[1])
elif OPENBSD:
# Note: the C ext is returning two metrics we are not returning:
# faults and forks.
ctxsw, intrs, soft_intrs, syscalls, traps, faults, forks = \
cext.cpu_stats()
return scpustats(ctxsw, intrs, soft_intrs, syscalls, traps)


def boot_time():
Expand Down
24 changes: 24 additions & 0 deletions psutil/arch/bsd/openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,27 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) {
return NULL;
}


PyObject *
psutil_cpu_stats(PyObject *self, PyObject *args) {
size_t size;
struct uvmexp uv;
int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};

size = sizeof(uv);
if (sysctl(uvmexp_mib, 2, &uv, &size, NULL, 0) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

return Py_BuildValue(
"IIIIIII",
uv.swtch, // ctx switches
uv.intrs, // interrupts - XXX always 0, will be determined via /proc
uv.softs, // soft interrupts
uv.syscalls, // syscalls - XXX always 0
uv.traps, // traps
uv.faults, // faults
uv.forks // forks
);
}
1 change: 1 addition & 0 deletions psutil/arch/bsd/openbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ PyObject *psutil_proc_cwd(PyObject *self, PyObject *args);
PyObject *psutil_proc_connections(PyObject *self, PyObject *args);
PyObject *psutil_per_cpu_times(PyObject *self, PyObject *args);
PyObject* psutil_disk_io_counters(PyObject* self, PyObject* args);
PyObject* psutil_cpu_stats(PyObject* self, PyObject* args);

0 comments on commit 7b260a4

Please sign in to comment.