summaryrefslogtreecommitdiff
path: root/psutil/_psutil_sunos.c
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-02-02 09:47:25 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2016-02-02 09:47:25 +0000
commit77386f15f2c83895c6ba97e10fc35ffe92a02f56 (patch)
tree06392aac150bdad25549015e2489ec6c06c7cf4d /psutil/_psutil_sunos.c
parent183db3a99ea89fe6de553e71eef54a076844e821 (diff)
downloadpsutil-77386f15f2c83895c6ba97e10fc35ffe92a02f56.tar.gz
#792 / cpu_stats: sunos impl
Diffstat (limited to 'psutil/_psutil_sunos.c')
-rw-r--r--psutil/_psutil_sunos.c46
1 files changed, 46 insertions, 0 deletions
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}
};