Skip to content

Commit

Permalink
FreeBSD files refactoring (#2059)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 16, 2022
1 parent 6173da2 commit f716afc
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 341 deletions.
10 changes: 8 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/DEVNOTES
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
11 changes: 4 additions & 7 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions psutil/arch/freebsd/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,75 @@ 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;
Expand Down
7 changes: 4 additions & 3 deletions psutil/arch/freebsd/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
86 changes: 86 additions & 0 deletions psutil/arch/freebsd/disk.c
Original file line number Diff line number Diff line change
@@ -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;
}

9 changes: 9 additions & 0 deletions psutil/arch/freebsd/disk.h
Original file line number Diff line number Diff line change
@@ -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);
137 changes: 137 additions & 0 deletions psutil/arch/freebsd/mem.c
Original file line number Diff line number Diff line change
@@ -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
);
}

10 changes: 10 additions & 0 deletions psutil/arch/freebsd/mem.h
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit f716afc

Please sign in to comment.