| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
|
| 9 |
#include <sys/sysinfo.h> |
| 10 |
#include <kstat.h> |
| 11 |
|
| 12 |
#include "../../arch/all/init.h" |
| 13 |
|
| 14 |
|
| 15 |
// System-wide CPU times. |
| 16 |
PyObject * |
| 17 |
psutil_per_cpu_times(PyObject *self, PyObject *args) { |
| 18 |
kstat_ctl_t *kc; |
| 19 |
kstat_t *ksp; |
| 20 |
cpu_stat_t cs; |
| 21 |
PyObject *py_retlist = PyList_New(0); |
| 22 |
PyObject *py_cputime = NULL; |
| 23 |
|
| 24 |
if (py_retlist == NULL) |
| 25 |
return NULL; |
| 26 |
|
| 27 |
kc = kstat_open(); |
| 28 |
if (kc == NULL) { |
| 29 |
PyErr_SetFromErrno(PyExc_OSError); |
| 30 |
goto error; |
| 31 |
} |
| 32 |
|
| 33 |
for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) { |
| 34 |
if (strcmp(ksp->ks_module, "cpu_stat") == 0) { |
| 35 |
if (kstat_read(kc, ksp, &cs) == -1) { |
| 36 |
PyErr_SetFromErrno(PyExc_OSError); |
| 37 |
goto error; |
| 38 |
} |
| 39 |
py_cputime = Py_BuildValue("ffff", |
| 40 |
(float)cs.cpu_sysinfo.cpu[CPU_USER], |
| 41 |
(float)cs.cpu_sysinfo.cpu[CPU_KERNEL], |
| 42 |
(float)cs.cpu_sysinfo.cpu[CPU_IDLE], |
| 43 |
(float)cs.cpu_sysinfo.cpu[CPU_WAIT]); |
| 44 |
if (py_cputime == NULL) |
| 45 |
goto error; |
| 46 |
if (PyList_Append(py_retlist, py_cputime)) |
| 47 |
goto error; |
| 48 |
Py_CLEAR(py_cputime); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
kstat_close(kc); |
| 53 |
return py_retlist; |
| 54 |
|
| 55 |
error: |
| 56 |
Py_XDECREF(py_cputime); |
| 57 |
Py_DECREF(py_retlist); |
| 58 |
if (kc != NULL) |
| 59 |
kstat_close(kc); |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
// Return the number of CPU cores on the system. |
| 65 |
PyObject * |
| 66 |
psutil_cpu_count_cores(PyObject *self, PyObject *args) { |
| 67 |
kstat_ctl_t *kc; |
| 68 |
kstat_t *ksp; |
| 69 |
int ncpus = 0; |
| 70 |
|
| 71 |
kc = kstat_open(); |
| 72 |
if (kc == NULL) |
| 73 |
goto error; |
| 74 |
ksp = kstat_lookup(kc, "cpu_info", -1, NULL); |
| 75 |
if (ksp == NULL) |
| 76 |
goto error; |
| 77 |
|
| 78 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
| 79 |
if (strcmp(ksp->ks_module, "cpu_info") != 0) |
| 80 |
continue; |
| 81 |
if (kstat_read(kc, ksp, NULL) == -1) |
| 82 |
goto error; |
| 83 |
ncpus += 1; |
| 84 |
} |
| 85 |
|
| 86 |
kstat_close(kc); |
| 87 |
if (ncpus > 0) |
| 88 |
return Py_BuildValue("i", ncpus); |
| 89 |
else |
| 90 |
goto error; |
| 91 |
|
| 92 |
error: |
| 93 |
// mimic os.cpu_count() |
| 94 |
if (kc != NULL) |
| 95 |
kstat_close(kc); |
| 96 |
Py_RETURN_NONE; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
// Return CPU statistics. |
| 101 |
PyObject * |
| 102 |
psutil_cpu_stats(PyObject *self, PyObject *args) { |
| 103 |
kstat_ctl_t *kc; |
| 104 |
kstat_t *ksp; |
| 105 |
cpu_stat_t cs; |
| 106 |
unsigned int ctx_switches = 0; |
| 107 |
unsigned int interrupts = 0; |
| 108 |
unsigned int traps = 0; |
| 109 |
unsigned int syscalls = 0; |
| 110 |
|
| 111 |
kc = kstat_open(); |
| 112 |
if (kc == NULL) { |
| 113 |
PyErr_SetFromErrno(PyExc_OSError); |
| 114 |
goto error; |
| 115 |
} |
| 116 |
|
| 117 |
for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) { |
| 118 |
if (strcmp(ksp->ks_module, "cpu_stat") == 0) { |
| 119 |
if (kstat_read(kc, ksp, &cs) == -1) { |
| 120 |
PyErr_SetFromErrno(PyExc_OSError); |
| 121 |
goto error; |
| 122 |
} |
| 123 |
// voluntary + involuntary |
| 124 |
ctx_switches += cs.cpu_sysinfo.pswitch + cs.cpu_sysinfo.inv_swtch; |
| 125 |
interrupts += cs.cpu_sysinfo.intr; |
| 126 |
traps += cs.cpu_sysinfo.trap; |
| 127 |
syscalls += cs.cpu_sysinfo.syscall; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
kstat_close(kc); |
| 132 |
return Py_BuildValue( |
| 133 |
"IIII", ctx_switches, interrupts, syscalls, traps); |
| 134 |
|
| 135 |
error: |
| 136 |
if (kc != NULL) |
| 137 |
kstat_close(kc); |
| 138 |
return NULL; |
| 139 |
} |