diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2022-01-16 23:52:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-16 23:52:11 +0100 |
commit | f716afc81d4c9ded08b23d376612e1491d3ea5da (patch) | |
tree | 626926e37bf3eb6f5e5d7e389e1ce0d8e3409f85 | |
parent | 6173da20bc7e697167e25d672b2d20f5e4b9bc4d (diff) | |
download | psutil-f716afc81d4c9ded08b23d376612e1491d3ea5da.tar.gz |
FreeBSD files refactoring (#2059)
-rw-r--r-- | MANIFEST.in | 10 | ||||
-rw-r--r-- | docs/DEVNOTES | 1 | ||||
-rw-r--r-- | psutil/_psutil_bsd.c | 11 | ||||
-rw-r--r-- | psutil/arch/freebsd/cpu.c | 64 | ||||
-rw-r--r-- | psutil/arch/freebsd/cpu.h | 7 | ||||
-rw-r--r-- | psutil/arch/freebsd/disk.c | 86 | ||||
-rw-r--r-- | psutil/arch/freebsd/disk.h | 9 | ||||
-rw-r--r-- | psutil/arch/freebsd/mem.c | 137 | ||||
-rw-r--r-- | psutil/arch/freebsd/mem.h | 10 | ||||
-rw-r--r-- | psutil/arch/freebsd/proc.c (renamed from psutil/arch/freebsd/specific.c) | 323 | ||||
-rw-r--r-- | psutil/arch/freebsd/proc.h (renamed from psutil/arch/freebsd/specific.h) | 6 | ||||
-rw-r--r-- | psutil/arch/freebsd/sensors.c | 82 | ||||
-rw-r--r-- | psutil/arch/freebsd/sensors.h | 10 | ||||
-rwxr-xr-x | setup.py | 5 |
14 files changed, 420 insertions, 341 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index 5b4eb69a..13acb0fd 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -51,10 +51,16 @@ include psutil/arch/aix/net_connections.h include psutil/arch/aix/net_kernel_structs.h include psutil/arch/freebsd/cpu.c include psutil/arch/freebsd/cpu.h +include psutil/arch/freebsd/disk.c +include psutil/arch/freebsd/disk.h +include psutil/arch/freebsd/mem.c +include psutil/arch/freebsd/mem.h +include psutil/arch/freebsd/proc.c +include psutil/arch/freebsd/proc.h include psutil/arch/freebsd/proc_socks.c include psutil/arch/freebsd/proc_socks.h -include psutil/arch/freebsd/specific.c -include psutil/arch/freebsd/specific.h +include psutil/arch/freebsd/sensors.c +include psutil/arch/freebsd/sensors.h include psutil/arch/freebsd/sys_socks.c include psutil/arch/freebsd/sys_socks.h include psutil/arch/netbsd/socks.c diff --git a/docs/DEVNOTES b/docs/DEVNOTES index 1748bfda..fdd2bad1 100644 --- a/docs/DEVNOTES +++ b/docs/DEVNOTES @@ -124,6 +124,7 @@ INCONSISTENCIES RESOURCES ========= +- conky: https://github.com/brndnmtthws/conky/ - sigar: https://github.com/hyperic/sigar (Java) - zabbix: https://zabbix.org/wiki/Get_Zabbix - libstatgrab: http://www.i-scream.org/libstatgrab/ diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 13170838..1ffa7b00 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -64,7 +64,10 @@ #ifdef PSUTIL_FREEBSD #include "arch/freebsd/cpu.h" - #include "arch/freebsd/specific.h" + #include "arch/freebsd/mem.h" + #include "arch/freebsd/disk.h" + #include "arch/freebsd/sensors.h" + #include "arch/freebsd/proc.h" #include "arch/freebsd/sys_socks.h" #include "arch/freebsd/proc_socks.h" @@ -104,12 +107,6 @@ // convert a timeval struct to a double #define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) -#ifdef PSUTIL_FREEBSD - // convert a bintime struct to milliseconds - #define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * \ - (uint32_t) (bt.frac >> 32) ) >> 32 ) / 1000000) -#endif - #if defined(PSUTIL_OPENBSD) || defined (PSUTIL_NETBSD) #define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) #endif diff --git a/psutil/arch/freebsd/cpu.c b/psutil/arch/freebsd/cpu.c index f31e9bb0..a15d96ef 100644 --- a/psutil/arch/freebsd/cpu.c +++ b/psutil/arch/freebsd/cpu.c @@ -18,12 +18,76 @@ For reference, here's the git history with original(ish) implementations: #include <Python.h> #include <sys/sysctl.h> +#include <devstat.h> #include "../../_psutil_common.h" #include "../../_psutil_posix.h" PyObject * +psutil_per_cpu_times(PyObject *self, PyObject *args) { + static int maxcpus; + int mib[2]; + 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 maxcpus value + size = sizeof(maxcpus); + if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) { + Py_DECREF(py_retlist); + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('kern.smp.maxcpus')"); + } + long cpu_time[maxcpus][CPUSTATES]; + + // 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_SetFromOSErrnoWithSyscall("sysctl(HW_NCPU)"); + goto error; + } + + // per-cpu info + size = sizeof(cpu_time); + if (sysctlbyname("kern.cp_times", &cpu_time, &size, NULL, 0) == -1) { + PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('kern.smp.maxcpus')"); + goto error; + } + + for (i = 0; i < ncpu; i++) { + py_cputime = Py_BuildValue( + "(ddddd)", + (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)) + goto error; + Py_DECREF(py_cputime); + } + + return py_retlist; + +error: + Py_XDECREF(py_cputime); + Py_DECREF(py_retlist); + return NULL; +} + + +PyObject * psutil_cpu_topology(PyObject *self, PyObject *args) { void *topology = NULL; size_t size = 0; diff --git a/psutil/arch/freebsd/cpu.h b/psutil/arch/freebsd/cpu.h index 8decd773..b78c4391 100644 --- a/psutil/arch/freebsd/cpu.h +++ b/psutil/arch/freebsd/cpu.h @@ -6,6 +6,7 @@ #include <Python.h> -PyObject* psutil_cpu_freq(PyObject* self, PyObject* args); -PyObject* psutil_cpu_stats(PyObject* self, PyObject* args); -PyObject* psutil_cpu_topology(PyObject* self, PyObject* args); +PyObject *psutil_cpu_freq(PyObject* self, PyObject* args); +PyObject *psutil_cpu_stats(PyObject* self, PyObject* args); +PyObject *psutil_cpu_topology(PyObject* self, PyObject* args); +PyObject *psutil_per_cpu_times(PyObject *self, PyObject *args); diff --git a/psutil/arch/freebsd/disk.c b/psutil/arch/freebsd/disk.c new file mode 100644 index 00000000..320b2bc8 --- /dev/null +++ b/psutil/arch/freebsd/disk.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> +#include <sys/sysctl.h> +#include <devstat.h> + +#include "../../_psutil_common.h" +#include "../../_psutil_posix.h" + + +// convert a bintime struct to milliseconds +#define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ + (bt.frac >> 32) ) >> 32 ) / 1000000) + + +PyObject * +psutil_disk_io_counters(PyObject *self, PyObject *args) { + int i; + struct statinfo stats; + + PyObject *py_retdict = PyDict_New(); + PyObject *py_disk_info = NULL; + + if (py_retdict == NULL) + return NULL; + if (devstat_checkversion(NULL) < 0) { + PyErr_Format(PyExc_RuntimeError, + "devstat_checkversion() syscall failed"); + goto error; + } + + stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); + if (stats.dinfo == NULL) { + PyErr_NoMemory(); + goto error; + } + bzero(stats.dinfo, sizeof(struct devinfo)); + + if (devstat_getdevs(NULL, &stats) == -1) { + PyErr_Format(PyExc_RuntimeError, "devstat_getdevs() syscall failed"); + goto error; + } + + for (i = 0; i < stats.dinfo->numdevs; i++) { + py_disk_info = NULL; + struct devstat current; + char disk_name[128]; + current = stats.dinfo->devices[i]; + snprintf(disk_name, sizeof(disk_name), "%s%d", + current.device_name, + current.unit_number); + + py_disk_info = Py_BuildValue( + "(KKKKLLL)", + current.operations[DEVSTAT_READ], // no reads + current.operations[DEVSTAT_WRITE], // no writes + current.bytes[DEVSTAT_READ], // bytes read + current.bytes[DEVSTAT_WRITE], // bytes written + (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_READ]), // r time + (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_WRITE]), // w time + (long long) PSUTIL_BT2MSEC(current.busy_time) // busy time + ); // finished transactions + if (!py_disk_info) + goto error; + if (PyDict_SetItemString(py_retdict, disk_name, py_disk_info)) + goto error; + Py_DECREF(py_disk_info); + } + + if (stats.dinfo->mem_ptr) + free(stats.dinfo->mem_ptr); + free(stats.dinfo); + return py_retdict; + +error: + Py_XDECREF(py_disk_info); + Py_DECREF(py_retdict); + if (stats.dinfo != NULL) + free(stats.dinfo); + return NULL; +} + diff --git a/psutil/arch/freebsd/disk.h b/psutil/arch/freebsd/disk.h new file mode 100644 index 00000000..9e29f664 --- /dev/null +++ b/psutil/arch/freebsd/disk.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> + +PyObject *psutil_disk_io_counters(PyObject* self, PyObject* args); diff --git a/psutil/arch/freebsd/mem.c b/psutil/arch/freebsd/mem.c new file mode 100644 index 00000000..f5374ef4 --- /dev/null +++ b/psutil/arch/freebsd/mem.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#include <Python.h> +#include <sys/sysctl.h> +#include <sys/vmmeter.h> +#include <vm/vm_param.h> +#include <devstat.h> +#include <paths.h> +#include <fcntl.h> + +#include "../../_psutil_common.h" +#include "../../_psutil_posix.h" + + +#ifndef _PATH_DEVNULL + #define _PATH_DEVNULL "/dev/null" +#endif + + +PyObject * +psutil_virtual_mem(PyObject *self, PyObject *args) { + unsigned long total; + unsigned int active, inactive, wired, cached, free; + size_t size = sizeof(total); + struct vmtotal vm; + int mib[] = {CTL_VM, VM_METER}; + long pagesize = psutil_getpagesize(); +#if __FreeBSD_version > 702101 + long buffers; +#else + int buffers; +#endif + size_t buffers_size = sizeof(buffers); + + if (sysctlbyname("hw.physmem", &total, &size, NULL, 0)) { + return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('hw.physmem')"); + } + if (sysctlbyname("vm.stats.vm.v_active_count", &active, &size, NULL, 0)) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_active_count')"); + } + if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive, &size, NULL, 0)) + { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_inactive_count')"); + } + if (sysctlbyname("vm.stats.vm.v_wire_count", &wired, &size, NULL, 0)) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_wire_count')"); + } + // https://github.com/giampaolo/psutil/issues/997 + if (sysctlbyname("vm.stats.vm.v_cache_count", &cached, &size, NULL, 0)) { + cached = 0; + } + if (sysctlbyname("vm.stats.vm.v_free_count", &free, &size, NULL, 0)) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_free_count')"); + } + if (sysctlbyname("vfs.bufspace", &buffers, &buffers_size, NULL, 0)) { + return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('vfs.bufspace')"); + } + + size = sizeof(vm); + if (sysctl(mib, 2, &vm, &size, NULL, 0) != 0) { + return PyErr_SetFromOSErrnoWithSyscall("sysctl(CTL_VM | VM_METER)"); + } + + return Py_BuildValue("KKKKKKKK", + (unsigned long long) total, + (unsigned long long) free * pagesize, + (unsigned long long) active * pagesize, + (unsigned long long) inactive * pagesize, + (unsigned long long) wired * pagesize, + (unsigned long long) cached * pagesize, + (unsigned long long) buffers, + (unsigned long long) (vm.t_vmshr + vm.t_rmshr) * pagesize // shared + ); +} + + +PyObject * +psutil_swap_mem(PyObject *self, PyObject *args) { + // Return swap memory stats (see 'swapinfo' cmdline tool) + kvm_t *kd; + struct kvm_swap kvmsw[1]; + unsigned int swapin, swapout, nodein, nodeout; + size_t size = sizeof(unsigned int); + long pagesize = psutil_getpagesize(); + + kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open failed"); + if (kd == NULL) { + PyErr_SetString(PyExc_RuntimeError, "kvm_open() syscall failed"); + return NULL; + } + + if (kvm_getswapinfo(kd, kvmsw, 1, 0) < 0) { + kvm_close(kd); + PyErr_SetString(PyExc_RuntimeError, + "kvm_getswapinfo() syscall failed"); + return NULL; + } + + kvm_close(kd); + + if (sysctlbyname("vm.stats.vm.v_swapin", &swapin, &size, NULL, 0) == -1) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_swapin)'"); + } + if (sysctlbyname("vm.stats.vm.v_swapout", &swapout, &size, NULL, 0) == -1){ + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_swapout)'"); + } + if (sysctlbyname("vm.stats.vm.v_vnodein", &nodein, &size, NULL, 0) == -1) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_vnodein)'"); + } + if (sysctlbyname("vm.stats.vm.v_vnodeout", &nodeout, &size, NULL, 0) == -1) { + return PyErr_SetFromOSErrnoWithSyscall( + "sysctlbyname('vm.stats.vm.v_vnodeout)'"); + } + + return Py_BuildValue( + "(KKKII)", + (unsigned long long)kvmsw[0].ksw_total * pagesize, // total + (unsigned long long)kvmsw[0].ksw_used * pagesize, // used + (unsigned long long)kvmsw[0].ksw_total * pagesize - // free + kvmsw[0].ksw_used * pagesize, + swapin + swapout, // swap in + nodein + nodeout // swap out + ); +} + diff --git a/psutil/arch/freebsd/mem.h b/psutil/arch/freebsd/mem.h new file mode 100644 index 00000000..e7dcfc57 --- /dev/null +++ b/psutil/arch/freebsd/mem.h @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> + +PyObject *psutil_swap_mem(PyObject* self, PyObject* args); +PyObject *psutil_virtual_mem(PyObject* self, PyObject* args); diff --git a/psutil/arch/freebsd/specific.c b/psutil/arch/freebsd/proc.c index 423f0c7b..a2e130b5 100644 --- a/psutil/arch/freebsd/specific.c +++ b/psutil/arch/freebsd/proc.c @@ -2,9 +2,6 @@ * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. - * - * Helper functions specific to FreeBSD. - * Used by _psutil_bsd module methods. */ #include <Python.h> @@ -20,8 +17,7 @@ #include <sys/proc.h> #include <signal.h> #include <fcntl.h> -#include <sys/vmmeter.h> // needed for vmtotal struct -#include <devstat.h> // for swap mem +#include <devstat.h> #include <libutil.h> // process open files, shared libs (kinfo_getvmmap), cwd #include <sys/cpuset.h> @@ -30,12 +26,6 @@ #define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) -#define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ - (bt.frac >> 32) ) >> 32 ) / 1000000) -#define DECIKELVIN_2_CELCIUS(t) (t - 2731) / 10 -#ifndef _PATH_DEVNULL -#define _PATH_DEVNULL "/dev/null" -#endif // ============================================================================ @@ -363,123 +353,6 @@ error: } -/* - * Return virtual memory usage statistics. - */ -PyObject * -psutil_virtual_mem(PyObject *self, PyObject *args) { - unsigned long total; - unsigned int active, inactive, wired, cached, free; - size_t size = sizeof(total); - struct vmtotal vm; - int mib[] = {CTL_VM, VM_METER}; - long pagesize = psutil_getpagesize(); -#if __FreeBSD_version > 702101 - long buffers; -#else - int buffers; -#endif - size_t buffers_size = sizeof(buffers); - - if (sysctlbyname("hw.physmem", &total, &size, NULL, 0)) { - return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('hw.physmem')"); - } - if (sysctlbyname("vm.stats.vm.v_active_count", &active, &size, NULL, 0)) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_active_count')"); - } - if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive, &size, NULL, 0)) - { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_inactive_count')"); - } - if (sysctlbyname("vm.stats.vm.v_wire_count", &wired, &size, NULL, 0)) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_wire_count')"); - } - // https://github.com/giampaolo/psutil/issues/997 - if (sysctlbyname("vm.stats.vm.v_cache_count", &cached, &size, NULL, 0)) { - cached = 0; - } - if (sysctlbyname("vm.stats.vm.v_free_count", &free, &size, NULL, 0)) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_free_count')"); - } - if (sysctlbyname("vfs.bufspace", &buffers, &buffers_size, NULL, 0)) { - return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('vfs.bufspace')"); - } - - size = sizeof(vm); - if (sysctl(mib, 2, &vm, &size, NULL, 0) != 0) { - return PyErr_SetFromOSErrnoWithSyscall("sysctl(CTL_VM | VM_METER)"); - } - - return Py_BuildValue("KKKKKKKK", - (unsigned long long) total, - (unsigned long long) free * pagesize, - (unsigned long long) active * pagesize, - (unsigned long long) inactive * pagesize, - (unsigned long long) wired * pagesize, - (unsigned long long) cached * pagesize, - (unsigned long long) buffers, - (unsigned long long) (vm.t_vmshr + vm.t_rmshr) * pagesize // shared - ); -} - - -PyObject * -psutil_swap_mem(PyObject *self, PyObject *args) { - // Return swap memory stats (see 'swapinfo' cmdline tool) - kvm_t *kd; - struct kvm_swap kvmsw[1]; - unsigned int swapin, swapout, nodein, nodeout; - size_t size = sizeof(unsigned int); - long pagesize = psutil_getpagesize(); - - kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open failed"); - if (kd == NULL) { - PyErr_SetString(PyExc_RuntimeError, "kvm_open() syscall failed"); - return NULL; - } - - if (kvm_getswapinfo(kd, kvmsw, 1, 0) < 0) { - kvm_close(kd); - PyErr_SetString(PyExc_RuntimeError, - "kvm_getswapinfo() syscall failed"); - return NULL; - } - - kvm_close(kd); - - if (sysctlbyname("vm.stats.vm.v_swapin", &swapin, &size, NULL, 0) == -1) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_swapin)'"); - } - if (sysctlbyname("vm.stats.vm.v_swapout", &swapout, &size, NULL, 0) == -1){ - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_swapout)'"); - } - if (sysctlbyname("vm.stats.vm.v_vnodein", &nodein, &size, NULL, 0) == -1) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_vnodein)'"); - } - if (sysctlbyname("vm.stats.vm.v_vnodeout", &nodeout, &size, NULL, 0) == -1) { - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('vm.stats.vm.v_vnodeout)'"); - } - - return Py_BuildValue( - "(KKKII)", - (unsigned long long)kvmsw[0].ksw_total * pagesize, // total - (unsigned long long)kvmsw[0].ksw_used * pagesize, // used - (unsigned long long)kvmsw[0].ksw_total * pagesize - // free - kvmsw[0].ksw_used * pagesize, - swapin + swapout, // swap in - nodein + nodeout // swap out - ); -} - - #if defined(__FreeBSD_version) && __FreeBSD_version >= 701000 PyObject * psutil_proc_cwd(PyObject *self, PyObject *args) { @@ -559,137 +432,6 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) { PyObject * -psutil_per_cpu_times(PyObject *self, PyObject *args) { - static int maxcpus; - int mib[2]; - 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 maxcpus value - size = sizeof(maxcpus); - if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) { - Py_DECREF(py_retlist); - return PyErr_SetFromOSErrnoWithSyscall( - "sysctlbyname('kern.smp.maxcpus')"); - } - long cpu_time[maxcpus][CPUSTATES]; - - // 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_SetFromOSErrnoWithSyscall("sysctl(HW_NCPU)"); - goto error; - } - - // per-cpu info - size = sizeof(cpu_time); - if (sysctlbyname("kern.cp_times", &cpu_time, &size, NULL, 0) == -1) { - PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('kern.smp.maxcpus')"); - goto error; - } - - for (i = 0; i < ncpu; i++) { - py_cputime = Py_BuildValue( - "(ddddd)", - (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)) - goto error; - Py_DECREF(py_cputime); - } - - return py_retlist; - -error: - Py_XDECREF(py_cputime); - Py_DECREF(py_retlist); - return NULL; -} - - -PyObject * -psutil_disk_io_counters(PyObject *self, PyObject *args) { - int i; - struct statinfo stats; - - PyObject *py_retdict = PyDict_New(); - PyObject *py_disk_info = NULL; - - if (py_retdict == NULL) - return NULL; - if (devstat_checkversion(NULL) < 0) { - PyErr_Format(PyExc_RuntimeError, - "devstat_checkversion() syscall failed"); - goto error; - } - - stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); - if (stats.dinfo == NULL) { - PyErr_NoMemory(); - goto error; - } - bzero(stats.dinfo, sizeof(struct devinfo)); - - if (devstat_getdevs(NULL, &stats) == -1) { - PyErr_Format(PyExc_RuntimeError, "devstat_getdevs() syscall failed"); - goto error; - } - - for (i = 0; i < stats.dinfo->numdevs; i++) { - py_disk_info = NULL; - struct devstat current; - char disk_name[128]; - current = stats.dinfo->devices[i]; - snprintf(disk_name, sizeof(disk_name), "%s%d", - current.device_name, - current.unit_number); - - py_disk_info = Py_BuildValue( - "(KKKKLLL)", - current.operations[DEVSTAT_READ], // no reads - current.operations[DEVSTAT_WRITE], // no writes - current.bytes[DEVSTAT_READ], // bytes read - current.bytes[DEVSTAT_WRITE], // bytes written - (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_READ]), // r time - (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_WRITE]), // w time - (long long) PSUTIL_BT2MSEC(current.busy_time) // busy time - ); // finished transactions - if (!py_disk_info) - goto error; - if (PyDict_SetItemString(py_retdict, disk_name, py_disk_info)) - goto error; - Py_DECREF(py_disk_info); - } - - if (stats.dinfo->mem_ptr) - free(stats.dinfo->mem_ptr); - free(stats.dinfo); - return py_retdict; - -error: - Py_XDECREF(py_disk_info); - Py_DECREF(py_retdict); - if (stats.dinfo != NULL) - free(stats.dinfo); - return NULL; -} - - -PyObject * psutil_proc_memory_maps(PyObject *self, PyObject *args) { // Return a list of tuples for every process memory maps. // 'procstat' cmdline utility has been used as an example. @@ -902,69 +644,6 @@ error: /* - * Return battery information. - */ -PyObject * -psutil_sensors_battery(PyObject *self, PyObject *args) { - int percent; - int minsleft; - int power_plugged; - size_t size = sizeof(percent); - - if (sysctlbyname("hw.acpi.battery.life", &percent, &size, NULL, 0)) - goto error; - if (sysctlbyname("hw.acpi.battery.time", &minsleft, &size, NULL, 0)) - goto error; - if (sysctlbyname("hw.acpi.acline", &power_plugged, &size, NULL, 0)) - goto error; - return Py_BuildValue("iii", percent, minsleft, power_plugged); - -error: - // see: https://github.com/giampaolo/psutil/issues/1074 - if (errno == ENOENT) - PyErr_SetString(PyExc_NotImplementedError, "no battery"); - else - PyErr_SetFromErrno(PyExc_OSError); - return NULL; -} - - -/* - * Return temperature information for a given CPU core number. - */ -PyObject * -psutil_sensors_cpu_temperature(PyObject *self, PyObject *args) { - int current; - int tjmax; - int core; - char sensor[26]; - size_t size = sizeof(current); - - if (! PyArg_ParseTuple(args, "i", &core)) - return NULL; - sprintf(sensor, "dev.cpu.%d.temperature", core); - if (sysctlbyname(sensor, ¤t, &size, NULL, 0)) - goto error; - current = DECIKELVIN_2_CELCIUS(current); - - // Return -273 in case of faliure. - sprintf(sensor, "dev.cpu.%d.coretemp.tjmax", core); - if (sysctlbyname(sensor, &tjmax, &size, NULL, 0)) - tjmax = 0; - tjmax = DECIKELVIN_2_CELCIUS(tjmax); - - return Py_BuildValue("ii", current, tjmax); - -error: - if (errno == ENOENT) - PyErr_SetString(PyExc_NotImplementedError, "no temperature sensors"); - else - PyErr_SetFromErrno(PyExc_OSError); - return NULL; -} - - -/* * An emulation of Linux prlimit(). Returns a (soft, hard) tuple. */ PyObject * diff --git a/psutil/arch/freebsd/specific.h b/psutil/arch/freebsd/proc.h index 57f0a2a4..9c16f3cb 100644 --- a/psutil/arch/freebsd/specific.h +++ b/psutil/arch/freebsd/proc.h @@ -11,9 +11,7 @@ typedef struct kinfo_proc kinfo_proc; int psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount); int psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc); -PyObject* psutil_disk_io_counters(PyObject* self, PyObject* args); PyObject* psutil_get_cmdline(long pid); -PyObject* psutil_per_cpu_times(PyObject* self, PyObject* args); PyObject* psutil_proc_cpu_affinity_get(PyObject* self, PyObject* args); PyObject* psutil_proc_cpu_affinity_set(PyObject* self, PyObject* args); PyObject* psutil_proc_cwd(PyObject* self, PyObject* args); @@ -24,7 +22,3 @@ PyObject* psutil_proc_num_fds(PyObject* self, PyObject* args); PyObject* psutil_proc_num_threads(PyObject* self, PyObject* args); PyObject* psutil_proc_setrlimit(PyObject* self, PyObject* args); PyObject* psutil_proc_threads(PyObject* self, PyObject* args); -PyObject* psutil_sensors_battery(PyObject* self, PyObject* args); -PyObject* psutil_sensors_cpu_temperature(PyObject* self, PyObject* args); -PyObject* psutil_swap_mem(PyObject* self, PyObject* args); -PyObject* psutil_virtual_mem(PyObject* self, PyObject* args); diff --git a/psutil/arch/freebsd/sensors.c b/psutil/arch/freebsd/sensors.c new file mode 100644 index 00000000..ce7b2d92 --- /dev/null +++ b/psutil/arch/freebsd/sensors.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* +Original code was refactored and moved from psutil/arch/freebsd/specific.c +For reference, here's the git history with original(ish) implementations: +- sensors_battery(): 022cf0a05d34f4274269d4f8002ee95b9f3e32d2 +- sensors_cpu_temperature(): bb5d032be76980a9e110f03f1203bd35fa85a793 + (patch by Alex Manuskin) +*/ + + +#include <Python.h> +#include <sys/sysctl.h> + +#include "../../_psutil_common.h" +#include "../../_psutil_posix.h" + + +#define DECIKELVIN_2_CELCIUS(t) (t - 2731) / 10 + + +PyObject * +psutil_sensors_battery(PyObject *self, PyObject *args) { + int percent; + int minsleft; + int power_plugged; + size_t size = sizeof(percent); + + if (sysctlbyname("hw.acpi.battery.life", &percent, &size, NULL, 0)) + goto error; + if (sysctlbyname("hw.acpi.battery.time", &minsleft, &size, NULL, 0)) + goto error; + if (sysctlbyname("hw.acpi.acline", &power_plugged, &size, NULL, 0)) + goto error; + return Py_BuildValue("iii", percent, minsleft, power_plugged); + +error: + // see: https://github.com/giampaolo/psutil/issues/1074 + if (errno == ENOENT) + PyErr_SetString(PyExc_NotImplementedError, "no battery"); + else + PyErr_SetFromErrno(PyExc_OSError); + return NULL; +} + + +// Return temperature information for a given CPU core number. +PyObject * +psutil_sensors_cpu_temperature(PyObject *self, PyObject *args) { + int current; + int tjmax; + int core; + char sensor[26]; + size_t size = sizeof(current); + + if (! PyArg_ParseTuple(args, "i", &core)) + return NULL; + sprintf(sensor, "dev.cpu.%d.temperature", core); + if (sysctlbyname(sensor, ¤t, &size, NULL, 0)) + goto error; + current = DECIKELVIN_2_CELCIUS(current); + + // Return -273 in case of faliure. + sprintf(sensor, "dev.cpu.%d.coretemp.tjmax", core); + if (sysctlbyname(sensor, &tjmax, &size, NULL, 0)) + tjmax = 0; + tjmax = DECIKELVIN_2_CELCIUS(tjmax); + + return Py_BuildValue("ii", current, tjmax); + +error: + if (errno == ENOENT) + PyErr_SetString(PyExc_NotImplementedError, "no temperature sensors"); + else + PyErr_SetFromErrno(PyExc_OSError); + return NULL; +} + diff --git a/psutil/arch/freebsd/sensors.h b/psutil/arch/freebsd/sensors.h new file mode 100644 index 00000000..e5c4107b --- /dev/null +++ b/psutil/arch/freebsd/sensors.h @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> + +PyObject* psutil_sensors_battery(PyObject* self, PyObject* args); +PyObject* psutil_sensors_cpu_temperature(PyObject* self, PyObject* args); @@ -207,7 +207,10 @@ elif FREEBSD: sources=sources + [ 'psutil/_psutil_bsd.c', 'psutil/arch/freebsd/cpu.c', - 'psutil/arch/freebsd/specific.c', + 'psutil/arch/freebsd/mem.c', + 'psutil/arch/freebsd/disk.c', + 'psutil/arch/freebsd/sensors.c', + 'psutil/arch/freebsd/proc.c', 'psutil/arch/freebsd/sys_socks.c', 'psutil/arch/freebsd/proc_socks.c', ], |