|
8 | 8 | const char* ffDetectMemory(FFMemoryResult* ram) |
9 | 9 | { |
10 | 10 | size_t length = sizeof(ram->bytesTotal); |
11 | | - if (sysctlbyname("hw.memsize_usable", &ram->bytesTotal, &length, NULL, 0) != 0) |
12 | | - { |
13 | | - if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0) |
14 | | - return "Failed to read hw.memsize"; |
15 | | - } |
| 11 | + if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0) |
| 12 | + return "Failed to read hw.memsize"; |
| 13 | + uint64_t usableMemory = 0; |
| 14 | + length = sizeof(usableMemory); |
| 15 | + if (sysctlbyname("hw.memsize_usable", &usableMemory, &length, NULL, 0) != 0) |
| 16 | + usableMemory = ram->bytesTotal; |
16 | 17 |
|
17 | 18 | mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; |
18 | 19 | vm_statistics64_data_t vmstat; |
19 | 20 | if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS) |
20 | 21 | return "Failed to read host_statistics64"; |
21 | 22 |
|
22 | | - // https://github.com/exelban/stats/blob/master/Modules/RAM/readers.swift#L56 |
23 | | - ram->bytesUsed = ((uint64_t) |
24 | | - + vmstat.active_count |
25 | | - + vmstat.inactive_count |
26 | | - + vmstat.speculative_count |
27 | | - + vmstat.wire_count |
28 | | - + vmstat.compressor_page_count |
29 | | - - vmstat.purgeable_count |
30 | | - - vmstat.external_page_count |
31 | | - ) * instance.state.platform.sysinfo.pageSize; |
| 23 | + uint64_t pageSize = instance.state.platform.sysinfo.pageSize; |
| 24 | + uint64_t appMemory = vmstat.internal_page_count * pageSize; |
| 25 | + uint64_t wiredMemory = vmstat.wire_count * pageSize; |
| 26 | + uint64_t compressed = vmstat.compressor_page_count * pageSize; |
| 27 | + uint64_t reserved = ram->bytesTotal - usableMemory; |
| 28 | + |
| 29 | + ram->bytesUsed = appMemory + wiredMemory + compressed + reserved; |
32 | 30 |
|
33 | 31 | return NULL; |
34 | 32 | } |
0 commit comments