Skip to content

Commit

Permalink
#792 / cpu_stats: sunos impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 2, 2016
1 parent 183db3a commit 77386f1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ CPU
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
| Linux | OSX | Windows | FreeBSD | OpenBSD | NetBSD | SunOS |
+=================+=================+==============+=================+=================+=================+==============+
| ctx_switches | ctx_switches | | ctx_switches | ctx_switches | ctx_switches | |
| ctx_switches | ctx_switches | | ctx_switches | ctx_switches | ctx_switches | ctx_switches |
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
| interrupts | interrupts | | interrupts | interrupts | interrupts | |
| interrupts | interrupts | | interrupts | interrupts | interrupts | interrupts |
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
| soft_interrupts | soft_interrupts | | soft_interrupts | soft_interrupts | soft_interrupts | |
| soft_interrupts | soft_interrupts | | soft_interrupts | soft_interrupts | soft_interrupts | syscalls |
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
| procs_running | syscalls | | syscalls | syscalls | syscalls | |
| procs_running | syscalls | | syscalls | syscalls | syscalls | traps |
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
| procs_blocked | traps | | traps | traps | traps | |
+-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+
Expand Down
8 changes: 8 additions & 0 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
['path', 'rss', 'anonymous', 'locked'])
pmmap_ext = namedtuple(
'pmmap_ext', 'addr perms ' + ' '.join(pmmap_grouped._fields))
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'syscalls', 'traps'])


# set later from __init__.py
NoSuchProcess = None
Expand Down Expand Up @@ -168,6 +171,11 @@ def cpu_count_physical():
return cext.cpu_count_phys()


def cpu_stats():
ctx_switches, interrupts, syscalls, traps = cext.cpu_stats()
return scpustats(ctx_switches, interrupts, syscalls, traps)


def boot_time():
"""The system boot time expressed in seconds since the epoch."""
return cext.boot_time()
Expand Down
46 changes: 46 additions & 0 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,50 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
}


/*
* Return CPU statistics.
*/
static PyObject *
psutil_cpu_stats(PyObject *self, PyObject *args) {
kstat_ctl_t *kc;
kstat_t *ksp;
cpu_stat_t cs;
unsigned int ctx_switches = 0;
unsigned int interrupts = 0;
unsigned int traps = 0;
unsigned int syscalls = 0;

kc = kstat_open();
if (kc == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}

for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
if (kstat_read(kc, ksp, &cs) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
// voluntary + involuntary
ctx_switches += cs.cpu_sysinfo.pswitch + cs.cpu_sysinfo.inv_swtch;
interrupts += cs.cpu_sysinfo.intr;
traps += cs.cpu_sysinfo.trap;
syscalls += cs.cpu_sysinfo.syscall;
}
}

kstat_close(kc);
return Py_BuildValue(
"IIII", ctx_switches, interrupts, syscalls, traps);

error:
if (kc != NULL)
kstat_close(kc);
return NULL;
}


/*
* define the psutil C module methods and initialize the module.
*/
Expand Down Expand Up @@ -1319,6 +1363,8 @@ PsutilMethods[] = {
"Return TCP and UDP syste-wide open connections."},
{"net_if_stats", psutil_net_if_stats, METH_VARARGS,
"Return NIC stats (isup, duplex, speed, mtu)"},
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},

{NULL, NULL, 0, NULL}
};
Expand Down

0 comments on commit 77386f1

Please sign in to comment.