diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-02 09:47:25 +0000 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-02 09:47:25 +0000 |
commit | 77386f15f2c83895c6ba97e10fc35ffe92a02f56 (patch) | |
tree | 06392aac150bdad25549015e2489ec6c06c7cf4d | |
parent | 183db3a99ea89fe6de553e71eef54a076844e821 (diff) | |
download | psutil-77386f15f2c83895c6ba97e10fc35ffe92a02f56.tar.gz |
#792 / cpu_stats: sunos impl
-rw-r--r-- | docs/index.rst | 8 | ||||
-rw-r--r-- | psutil/_pssunos.py | 8 | ||||
-rw-r--r-- | psutil/_psutil_sunos.c | 46 |
3 files changed, 58 insertions, 4 deletions
diff --git a/docs/index.rst b/docs/index.rst index 35dbaad1..6fd3dfa5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 | | +-----------------+-----------------+--------------+-----------------+-----------------+-----------------+--------------+ diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index 06da5aab..33799e76 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -68,6 +68,9 @@ pmmap_grouped = namedtuple('pmmap_grouped', ['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 @@ -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() diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 9a7067d3..2b960142 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -1277,6 +1277,50 @@ error: /* + * 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. */ static PyMethodDef @@ -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} }; |