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_osx.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_osx.c')
-rw-r--r-- | psutil/_psutil_osx.c | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 33d46635..13d0bb68 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -441,7 +441,7 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { mach_vm_size_t size = 0; mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; kern_return_t kr; - vm_size_t page_size; + long pagesize = psutil_getpagesize(); mach_vm_address_t addr = MACH_VM_MIN_ADDRESS; mach_port_t task = MACH_PORT_NULL; vm_region_top_info_data_t info; @@ -505,11 +505,7 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { } mach_port_deallocate(mach_task_self(), task); - - if (host_page_size(mach_host_self(), &page_size) != KERN_SUCCESS) - page_size = PAGE_SIZE; - - return Py_BuildValue("K", private_pages * page_size); + return Py_BuildValue("K", private_pages * pagesize); } @@ -525,7 +521,7 @@ psutil_virtual_mem(PyObject *self, PyObject *args) { uint64_t total; size_t len = sizeof(total); vm_statistics_data_t vm; - int pagesize = getpagesize(); + long pagesize = psutil_getpagesize(); // physical mem mib[0] = CTL_HW; mib[1] = HW_MEMSIZE; @@ -565,7 +561,7 @@ psutil_swap_mem(PyObject *self, PyObject *args) { size_t size; struct xsw_usage totals; vm_statistics_data_t vmstat; - int pagesize = getpagesize(); + long pagesize = psutil_getpagesize(); mib[0] = CTL_VM; mib[1] = VM_SWAPUSAGE; |