Skip to content

Commit

Permalink
move disk_swaps() in freebsd specific.c mod
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 14, 2020
1 parent 1dac04c commit 31b70a3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 67 deletions.
69 changes: 2 additions & 67 deletions psutil/_psutil_bsd.c
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand Down
67 changes: 67 additions & 0 deletions psutil/arch/freebsd/specific.c
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions psutil/arch/freebsd/specific.h
Expand Up @@ -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);
Expand Down

0 comments on commit 31b70a3

Please sign in to comment.