summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-11-12 04:11:17 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2015-11-12 04:11:17 +0100
commitf02845d75b29ca4988038dfd93af40329e10ce61 (patch)
tree2d731755ebd2a4f8d9e2e7fc35dcdad0463672a6
parentabf77b0a225915e6f04d7f660d66f5fe47ee57b0 (diff)
downloadpsutil-f02845d75b29ca4988038dfd93af40329e10ce61.tar.gz
move per cpu times
-rw-r--r--psutil/_psutil_openbsd.c44
-rw-r--r--psutil/arch/bsd/openbsd.c61
-rw-r--r--psutil/arch/bsd/openbsd.h1
3 files changed, 84 insertions, 22 deletions
diff --git a/psutil/_psutil_openbsd.c b/psutil/_psutil_openbsd.c
index 98e19555..50c0b823 100644
--- a/psutil/_psutil_openbsd.c
+++ b/psutil/_psutil_openbsd.c
@@ -1244,14 +1244,11 @@ error:
#endif
-#ifdef __OpenBSD__
-/*
- * Return a Python list of tuple representing per-cpu times
- */
+#ifdef __FreeBSD__
static PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args) {
static int maxcpus;
- int mib[3];
+ int mib[2];
int ncpu;
size_t len;
size_t size;
@@ -1262,6 +1259,14 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) {
if (py_retlist == NULL)
return NULL;
+ // retrieve maxcpus value
+ size = sizeof(maxcpus);
+ if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
+ Py_DECREF(py_retlist);
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ long cpu_time[maxcpus][CPUSTATES];
// retrieve the number of cpus
mib[0] = CTL_HW;
@@ -1271,27 +1276,22 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
- uint64_t cpu_time[CPUSTATES];
- for (i = 0; i < ncpu; i++) {
- // per-cpu info
- mib[0] = CTL_KERN;
- mib[1] = KERN_CPTIME2;
- mib[2] = i;
- size = sizeof(cpu_time);
- if (sysctl(mib, 3, &cpu_time, &size, NULL, 0) == -1) {
- warn("failed to get kern.cptime2");
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
+ // per-cpu info
+ size = sizeof(cpu_time);
+ if (sysctlbyname("kern.cp_times", &cpu_time, &size, NULL, 0) == -1) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ goto error;
+ }
+ for (i = 0; i < ncpu; i++) {
py_cputime = Py_BuildValue(
"(ddddd)",
- (double)cpu_time[CP_USER] / CLOCKS_PER_SEC,
- (double)cpu_time[CP_NICE] / CLOCKS_PER_SEC,
- (double)cpu_time[CP_SYS] / CLOCKS_PER_SEC,
- (double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC,
- (double)cpu_time[CP_INTR] / CLOCKS_PER_SEC);
+ (double)cpu_time[i][CP_USER] / CLOCKS_PER_SEC,
+ (double)cpu_time[i][CP_NICE] / CLOCKS_PER_SEC,
+ (double)cpu_time[i][CP_SYS] / CLOCKS_PER_SEC,
+ (double)cpu_time[i][CP_IDLE] / CLOCKS_PER_SEC,
+ (double)cpu_time[i][CP_INTR] / CLOCKS_PER_SEC);
if (!py_cputime)
goto error;
if (PyList_Append(py_retlist, py_cputime))
diff --git a/psutil/arch/bsd/openbsd.c b/psutil/arch/bsd/openbsd.c
index 5bea4f09..d90796d7 100644
--- a/psutil/arch/bsd/openbsd.c
+++ b/psutil/arch/bsd/openbsd.c
@@ -25,6 +25,7 @@
// connection stuff
#include <netdb.h> // for NI_MAXHOST
#include <sys/socket.h>
+#include <sys/sched.h> // for CPUSTATES & CP_*
#define _KERNEL // for DTYPE_*
#include <sys/file.h>
#undef _KERNEL
@@ -633,3 +634,63 @@ error:
free(tcplist);
return NULL;
}
+
+
+PyObject *
+psutil_per_cpu_times(PyObject *self, PyObject *args) {
+ static int maxcpus;
+ int mib[3];
+ int ncpu;
+ size_t len;
+ size_t size;
+ int i;
+ PyObject *py_retlist = PyList_New(0);
+ PyObject *py_cputime = NULL;
+
+ if (py_retlist == NULL)
+ return NULL;
+
+
+ // retrieve the number of cpus
+ mib[0] = CTL_HW;
+ mib[1] = HW_NCPU;
+ len = sizeof(ncpu);
+ if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ goto error;
+ }
+ uint64_t cpu_time[CPUSTATES];
+
+ for (i = 0; i < ncpu; i++) {
+ // per-cpu info
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_CPTIME2;
+ mib[2] = i;
+ size = sizeof(cpu_time);
+ if (sysctl(mib, 3, &cpu_time, &size, NULL, 0) == -1) {
+ warn("failed to get kern.cptime2");
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ py_cputime = Py_BuildValue(
+ "(ddddd)",
+ (double)cpu_time[CP_USER] / CLOCKS_PER_SEC,
+ (double)cpu_time[CP_NICE] / CLOCKS_PER_SEC,
+ (double)cpu_time[CP_SYS] / CLOCKS_PER_SEC,
+ (double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC,
+ (double)cpu_time[CP_INTR] / CLOCKS_PER_SEC);
+ if (!py_cputime)
+ goto error;
+ if (PyList_Append(py_retlist, py_cputime))
+ goto error;
+ Py_DECREF(py_cputime);
+ }
+
+ return py_retlist;
+
+error:
+ Py_XDECREF(py_cputime);
+ Py_DECREF(py_retlist);
+ return NULL;
+}
diff --git a/psutil/arch/bsd/openbsd.h b/psutil/arch/bsd/openbsd.h
index dbe8b0b2..31cd8312 100644
--- a/psutil/arch/bsd/openbsd.h
+++ b/psutil/arch/bsd/openbsd.h
@@ -24,3 +24,4 @@ PyObject *psutil_swap_mem(PyObject *self, PyObject *args);
PyObject *psutil_proc_num_fds(PyObject *self, PyObject *args);
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);