diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-12-17 18:59:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 18:59:13 +0100 |
commit | 6bdde37049ba9fae31ccf83620468f77a6b9f396 (patch) | |
tree | 33875d40066b6b48b5ed5161d230288d968f6cc1 /psutil/_psutil_posix.c | |
parent | c39fef497f0c2c963a9db03ef80cdb156f64a425 (diff) | |
download | psutil-6bdde37049ba9fae31ccf83620468f77a6b9f396.tar.gz |
[macOS, UNIX] prefer _SC_PAGESIZE over (partially) deprecated getpagesize() (#1891)
Add a reusable `psutil_getpagesize()` utility common to all UNIXes.
Related to #1885 (`getpagesize()` is deprecated on recent macOS, POSIX.1-2001 and possibly other UNIXes).
The problem emerged on macOS but `getpagesize()` is also used in FreeBSD, NetBSD, OpenBSD and AIX, so it makes sense to do this in one place only, similarly to Windows which also provide a `psutil_getpagesize()` utility.
Follow cPython's `mmapmodule.c` and `resourcemodule.c` lead and rely on `sysconf(_SC_PAGESIZE)` instead, but leave `getpagesize()` in place as last resort/attempt for systems where it's not deprecated and/or they still legitimately rely on it.
Also provide a python wrapper so we can test the return value of this C function against Python's stdlib modules.
Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
Diffstat (limited to 'psutil/_psutil_posix.c')
-rw-r--r-- | psutil/_psutil_posix.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index b41c6dec..305cec76 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -15,6 +15,7 @@ #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> +#include <unistd.h> #ifdef PSUTIL_SUNOS10 #include "arch/solaris/v10/ifaddrs.h" @@ -50,6 +51,38 @@ #include "_psutil_common.h" + +// ==================================================================== +// --- Utils +// ==================================================================== + + +/* + * From "man getpagesize" on Linux, https://linux.die.net/man/2/getpagesize: + * + * > In SUSv2 the getpagesize() call is labeled LEGACY, and in POSIX.1-2001 + * > it has been dropped. + * > Portable applications should employ sysconf(_SC_PAGESIZE) instead + * > of getpagesize(). + * > Most systems allow the synonym _SC_PAGE_SIZE for _SC_PAGESIZE. + * > Whether getpagesize() is present as a Linux system call depends on the + * > architecture. + */ +long +psutil_getpagesize(void) { +#ifdef _SC_PAGESIZE + // recommended POSIX + return sysconf(_SC_PAGESIZE); +#elif _SC_PAGE_SIZE + // alias + return sysconf(_SC_PAGE_SIZE); +#else + // legacy + return (long) getpagesize(); +#endif +} + + /* * Check if PID exists. Return values: * 1: exists @@ -123,6 +156,18 @@ psutil_raise_for_pid(long pid, char *syscall) { } +// ==================================================================== +// --- Python wrappers +// ==================================================================== + + +// Exposed so we can test it against Python's stdlib. +static PyObject * +psutil_getpagesize_pywrapper(PyObject *self, PyObject *args) { + return Py_BuildValue("l", psutil_getpagesize()); +} + + /* * Given a PID return process priority as a Python integer. */ @@ -629,6 +674,8 @@ static PyMethodDef mod_methods[] = { "Retrieve NIC MTU"}, {"net_if_is_running", psutil_net_if_is_running, METH_VARARGS, "Return True if the NIC is running."}, + {"getpagesize", psutil_getpagesize_pywrapper, METH_VARARGS, + "Return memory page size."}, #if defined(PSUTIL_BSD) || defined(PSUTIL_OSX) {"net_if_duplex_speed", psutil_net_if_duplex_speed, METH_VARARGS, "Return NIC stats."}, |