From 610fc2ea36b92d6656d1d3c806cab0084dad9b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 18 Dec 2025 13:36:38 +0800 Subject: [PATCH] Memory (macOS): Refines Memory usage detection on macOS to match Activity Monitor more closely --- CHANGELOG.md | 2 +- src/detection/memory/memory_apple.c | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06283fe7b..a8fe63c72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ Features: * Improves compatibility with KDE Plasma 6.5 (#2093, Display) * Adds a `tempSensor` option to specify the sensor name used for CPU temperature detection (CPU) * Example: `{ "type": "cpu", "tempSensor": "hwmon0" /* Use /sys/class/hwmon/hwmon0 for temperature detection */ }` +* Refines Memory usage detection on macOS to match Activity Monitor more closely (Memory, macOS) * Minor optimizations -* Reports usable RAM size to match other platforms (Memory, macOS) Bugfixes: * Fixes cache line size detection (CPU, macOS) diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 812def5e8..a24dba32e 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -8,27 +8,25 @@ const char* ffDetectMemory(FFMemoryResult* ram) { size_t length = sizeof(ram->bytesTotal); - if (sysctlbyname("hw.memsize_usable", &ram->bytesTotal, &length, NULL, 0) != 0) - { - if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0) - return "Failed to read hw.memsize"; - } + if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0) + return "Failed to read hw.memsize"; + uint64_t usableMemory = 0; + length = sizeof(usableMemory); + if (sysctlbyname("hw.memsize_usable", &usableMemory, &length, NULL, 0) != 0) + usableMemory = ram->bytesTotal; mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; vm_statistics64_data_t vmstat; if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS) return "Failed to read host_statistics64"; - // https://github.com/exelban/stats/blob/master/Modules/RAM/readers.swift#L56 - ram->bytesUsed = ((uint64_t) - + vmstat.active_count - + vmstat.inactive_count - + vmstat.speculative_count - + vmstat.wire_count - + vmstat.compressor_page_count - - vmstat.purgeable_count - - vmstat.external_page_count - ) * instance.state.platform.sysinfo.pageSize; + uint64_t pageSize = instance.state.platform.sysinfo.pageSize; + uint64_t appMemory = vmstat.internal_page_count * pageSize; + uint64_t wiredMemory = vmstat.wire_count * pageSize; + uint64_t compressed = vmstat.compressor_page_count * pageSize; + uint64_t reserved = ram->bytesTotal - usableMemory; + + ram->bytesUsed = appMemory + wiredMemory + compressed + reserved; return NULL; }