Files
2026-02-07 16:34:13 +11:00

45 lines
1.7 KiB
Diff

Add a fallback option for outdated Darwin kernels without compressed memory support.
task_vm_info_data_t has been implemented since macOS 10.9, but task_vm_info_data_t.phys_footprint can only be used since macOS 10.11.
--- Python/gc_free_threading.c
+++ Python/gc_free_threading.c
@@ -25,8 +25,9 @@
#include <unistd.h> // For sysconf, getpid
#elif defined(__APPLE__)
#include <mach/mach.h>
- #include <mach/task.h> // Required for TASK_VM_INFO
+ #include <mach/task.h> // Required for TASK_VM_INFO/TASK_BASIC_INFO
#include <unistd.h> // For sysconf, getpid
+ #include <AvailabilityMacros.h> // For macOS version checks
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
@@ -1984,6 +1985,9 @@ get_process_mem_usage(void)
return -1;
#elif defined(__APPLE__)
+#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) \
+ && (defined(MAC_OS_X_VERSION_10_11) && \
+ MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11)
// --- MacOS (Darwin) ---
// Returns phys_footprint (RAM + compressed memory)
task_vm_info_data_t vm_info;
@@ -1996,6 +2000,18 @@ get_process_mem_usage(void)
}
// phys_footprint is in bytes. Convert to KB.
return (Py_ssize_t)(vm_info.phys_footprint / 1024);
+ #else
+ task_basic_info_data_t info;
+ mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
+ kern_return_t kerr;
+
+ kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
+ if (kerr != KERN_SUCCESS) {
+ return -1;
+ }
+ // resident_size is in bytes. Convert to KB.
+ return (Py_ssize_t)(info.resident_size / 1024);
+#endif
#elif defined(__FreeBSD__)
// NOTE: Returns RSS only. Per-process swap usage isn't readily available