From 31b70a303111577caa6b02e2342e4c121ccb65c4 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 14 Feb 2020 21:41:04 +0000 Subject: [PATCH] move disk_swaps() in freebsd specific.c mod --- psutil/_psutil_bsd.c | 69 +--------------------------------- psutil/arch/freebsd/specific.c | 67 +++++++++++++++++++++++++++++++++ psutil/arch/freebsd/specific.h | 1 + 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index d46af572e..4a1cad08f 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -691,73 +691,6 @@ psutil_disk_partitions(PyObject *self, PyObject *args) { } -#define NSWAP 16 - -/* - * Enumerate swap locations. - */ -static PyObject * -psutil_disk_swaps(PyObject *self, PyObject *args) { - struct kvm_swap ksw; - struct xswdev xsw; - size_t mibsize; - size_t size; - char path[PATH_MAX]; - int mib[NSWAP]; - int n; - int pagesize = getpagesize(); - PyObject *py_tuple = NULL; - PyObject *py_retlist = PyList_New(0); - - if (! py_retlist) - return NULL; - - mibsize = sizeof(mib) / sizeof(mib[0]); - if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) { - PyErr_SetFromOSErrnoWithSyscall("sysctlnametomib"); - goto error; - } - - for (n=0; ; ++n) { - mib[mibsize] = n; - size = sizeof(xsw); - - if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) { - if (errno == ENOENT) - break; - PyErr_SetFromOSErrnoWithSyscall("sysctl"); - goto error; - } - - if (xsw.xsw_dev == NODEV) - strlcpy(path, "[nodev]", sizeof(path)); - else if (xsw.xsw_flags & SWIF_DEV_PREFIX) - strlcpy(path, devname(xsw.xsw_dev, S_IFCHR), sizeof(path)); - else - sprintf(path, "%s%s", _PATH_DEV, devname(xsw.xsw_dev, S_IFCHR)); - - py_tuple = Py_BuildValue( - "(sii)", - path, - xsw.xsw_nblks * pagesize, // total - xsw.xsw_used * pagesize // used - ); - if (!py_tuple) - goto error; - if (PyList_Append(py_retlist, py_tuple)) - goto error; - Py_CLEAR(py_tuple); - } - - return py_retlist; - -error: - Py_XDECREF(py_tuple); - Py_DECREF(py_retlist); - return NULL; -} - - /* * Return a Python list of named tuples with overall network I/O information */ @@ -1043,8 +976,10 @@ static PyMethodDef mod_methods[] = { {"disk_partitions", psutil_disk_partitions, METH_VARARGS, "Return a list of tuples including device, mount point and " "fs type for all partitions mounted on the system."}, +#if defined(PSUTIL_FREEBSD) {"disk_swaps", psutil_disk_swaps, METH_VARARGS, "Enumerate swap partitions/files."}, +#endif {"net_io_counters", psutil_net_io_counters, METH_VARARGS, "Return dict of tuples of networks I/O information."}, {"disk_io_counters", psutil_disk_io_counters, METH_VARARGS, diff --git a/psutil/arch/freebsd/specific.c b/psutil/arch/freebsd/specific.c index 3d54b47e5..4fc75b4c5 100644 --- a/psutil/arch/freebsd/specific.c +++ b/psutil/arch/freebsd/specific.c @@ -1075,3 +1075,70 @@ psutil_cpu_freq(PyObject *self, PyObject *args) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } + + +#define NSWAP 16 + +/* + * Enumerate swap locations. + */ +static PyObject * +psutil_disk_swaps(PyObject *self, PyObject *args) { + struct kvm_swap ksw; + struct xswdev xsw; + size_t mibsize; + size_t size; + char path[PATH_MAX]; + int mib[NSWAP]; + int n; + int pagesize = getpagesize(); + PyObject *py_tuple = NULL; + PyObject *py_retlist = PyList_New(0); + + if (! py_retlist) + return NULL; + + mibsize = sizeof(mib) / sizeof(mib[0]); + if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) { + PyErr_SetFromOSErrnoWithSyscall("sysctlnametomib"); + goto error; + } + + for (n=0; ; ++n) { + mib[mibsize] = n; + size = sizeof(xsw); + + if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) { + if (errno == ENOENT) + break; + PyErr_SetFromOSErrnoWithSyscall("sysctl"); + goto error; + } + + if (xsw.xsw_dev == NODEV) + strlcpy(path, "[nodev]", sizeof(path)); + else if (xsw.xsw_flags & SWIF_DEV_PREFIX) + strlcpy(path, devname(xsw.xsw_dev, S_IFCHR), sizeof(path)); + else + sprintf(path, "%s%s", _PATH_DEV, devname(xsw.xsw_dev, S_IFCHR)); + + py_tuple = Py_BuildValue( + "(sii)", + path, + xsw.xsw_nblks * pagesize, // total + xsw.xsw_used * pagesize // used + ); + if (!py_tuple) + goto error; + if (PyList_Append(py_retlist, py_tuple)) + goto error; + Py_CLEAR(py_tuple); + } + + return py_retlist; + +error: + Py_XDECREF(py_tuple); + Py_DECREF(py_retlist); + return NULL; +} diff --git a/psutil/arch/freebsd/specific.h b/psutil/arch/freebsd/specific.h index 875c81664..f05e656ed 100644 --- a/psutil/arch/freebsd/specific.h +++ b/psutil/arch/freebsd/specific.h @@ -27,6 +27,7 @@ PyObject* psutil_proc_threads(PyObject* self, PyObject* args); PyObject* psutil_swap_mem(PyObject* self, PyObject* args); PyObject* psutil_virtual_mem(PyObject* self, PyObject* args); PyObject* psutil_cpu_stats(PyObject* self, PyObject* args); +PyObject* psutil_disk_swaps(PyObject* self, PyObject* args); #if defined(PSUTIL_FREEBSD) PyObject* psutil_sensors_battery(PyObject* self, PyObject* args); PyObject* psutil_sensors_cpu_temperature(PyObject* self, PyObject* args);