Skip to content

Commit 9c114a5

Browse files
authored
[OSX] use host_statistics64 to get memory metrics (#2502)
We have lots of sporadic failures on OSX related to free virtual_memory(), whose value does not exactly match vm_stat CLI utility. With this PR we use the exact same approach of vm_stat CLI tool, whose source code is here: https://github.com/apple-open-source/macos/blob/master/system_cmds/vm_stat/vm_stat.c Hopefully this will reduce such sporadic failures.
1 parent 08d7d43 commit 9c114a5

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

HISTORY.rst

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ XXXX-XX-XX
1919

2020
- 2496_, [Linux]: Avoid segfault (a cPython bug) on ``Process.memory_maps()``
2121
for processes that use hundreds of GBs of memory.
22+
- 2502_, [macOS]: `virtual_memory()`_ now relies on ``host_statistics64``
23+
instead of ``host_statistics``. This is the same approach used by ``vm_stat``
24+
CLI tool, and should grant more accurate results.
2225

2326
**Compatibility notes**
2427

psutil/_psosx.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def virtual_memory():
112112
"""System virtual memory as a namedtuple."""
113113
total, active, inactive, wired, free, speculative = cext.virtual_mem()
114114
# This is how Zabbix calculate avail and used mem:
115-
# https://github.com/zabbix/zabbix/blob/trunk/src/libs/zbxsysinfo/
116-
# osx/memory.c
115+
# https://github.com/zabbix/zabbix/blob/master/src/libs/zbxsysinfo/osx/memory.c
117116
# Also see: https://github.com/giampaolo/psutil/issues/1277
118117
avail = inactive + free
119118
used = active + wired

psutil/_psutil_osx.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <Python.h>
10+
#include <sys/time.h> // needed for old macOS versions
1011
#include <sys/proc.h>
1112
#include <netinet/tcp_fsm.h>
1213

psutil/arch/osx/mem.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// from psutil/_psutil_osx.c in 2023. This is the GIT blame before the move:
99
// https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_osx.c
1010

11+
// See:
12+
// https://github.com/apple-open-source/macos/blob/master/system_cmds/vm_stat/vm_stat.c
13+
1114
#include <Python.h>
1215
#include <mach/host_info.h>
1316
#include <sys/sysctl.h>
@@ -17,12 +20,12 @@
1720

1821

1922
static int
20-
psutil_sys_vminfo(vm_statistics_data_t *vmstat) {
23+
psutil_sys_vminfo(vm_statistics64_t vmstat) {
2124
kern_return_t ret;
22-
mach_msg_type_number_t count = sizeof(*vmstat) / sizeof(integer_t);
25+
unsigned int count = HOST_VM_INFO64_COUNT;
2326
mach_port_t mport = mach_host_self();
2427

25-
ret = host_statistics(mport, HOST_VM_INFO, (host_info_t)vmstat, &count);
28+
ret = host_statistics64(mport, HOST_VM_INFO64, (host_info64_t)vmstat, &count);
2629
if (ret != KERN_SUCCESS) {
2730
PyErr_Format(
2831
PyExc_RuntimeError,
@@ -46,7 +49,7 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
4649
int mib[2];
4750
uint64_t total;
4851
size_t len = sizeof(total);
49-
vm_statistics_data_t vm;
52+
vm_statistics64_data_t vm;
5053
long pagesize = psutil_getpagesize();
5154
// physical mem
5255
mib[0] = CTL_HW;
@@ -86,7 +89,7 @@ psutil_swap_mem(PyObject *self, PyObject *args) {
8689
int mib[2];
8790
size_t size;
8891
struct xsw_usage totals;
89-
vm_statistics_data_t vmstat;
92+
vm_statistics64_data_t vmstat;
9093
long pagesize = psutil_getpagesize();
9194

9295
mib[0] = CTL_VM;

0 commit comments

Comments
 (0)