2022-09-05 10:57:09 +02:00
|
|
|
#include "memory.h"
|
2022-09-19 21:45:39 +02:00
|
|
|
#include "common/sysctl.h"
|
2022-09-05 02:46:53 -07:00
|
|
|
|
2022-09-08 14:51:31 +02:00
|
|
|
#include <string.h>
|
2022-09-05 02:46:53 -07:00
|
|
|
#include <mach/mach.h>
|
2022-09-05 10:57:09 +02:00
|
|
|
|
2022-09-20 15:12:24 +02:00
|
|
|
static void detectRam(FFMemoryStorage* ram)
|
2022-09-05 10:57:09 +02:00
|
|
|
{
|
2022-09-19 21:31:50 +02:00
|
|
|
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0);
|
2022-09-20 15:12:24 +02:00
|
|
|
if(ram->bytesTotal == 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&ram->error, "Failed to read hw.memsize");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-05 02:46:53 -07:00
|
|
|
|
2022-09-19 21:31:50 +02:00
|
|
|
uint32_t pagesize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
|
2022-09-05 02:46:53 -07:00
|
|
|
if(pagesize == 0)
|
2022-09-20 15:12:24 +02:00
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
|
2022-09-05 02:46:53 -07:00
|
|
|
return;
|
2022-09-20 15:12:24 +02:00
|
|
|
}
|
2022-09-05 02:46:53 -07:00
|
|
|
|
|
|
|
|
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
|
|
|
|
|
vm_statistics_data_t vmstat;
|
|
|
|
|
if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) (&vmstat), &count) != KERN_SUCCESS)
|
2022-09-20 15:12:24 +02:00
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&ram->error, "Failed to read vm statistics");
|
2022-09-05 02:46:53 -07:00
|
|
|
return;
|
2022-09-20 15:12:24 +02:00
|
|
|
}
|
2022-09-05 02:46:53 -07:00
|
|
|
|
2022-09-08 14:51:31 +02:00
|
|
|
memory->ram.bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
|
2022-09-20 15:12:24 +02:00
|
|
|
}
|
2022-09-17 11:21:05 +08:00
|
|
|
|
2022-09-20 15:12:24 +02:00
|
|
|
static void detectSwap(FFMemoryStorage* swap)
|
|
|
|
|
{
|
2022-09-17 11:21:05 +08:00
|
|
|
struct xsw_usage swap;
|
|
|
|
|
size_t size = sizeof(swap);
|
2022-09-20 15:12:24 +02:00
|
|
|
if(sysctlbyname("vm.swapusage", &swap, &size, 0, 0) != 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage");
|
2022-09-17 11:21:05 +08:00
|
|
|
return;
|
2022-09-20 15:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-17 11:21:05 +08:00
|
|
|
memory->swap.bytesTotal = swap.xsu_total;
|
|
|
|
|
memory->swap.bytesUsed = swap.xsu_used;
|
2022-09-05 10:57:09 +02:00
|
|
|
}
|
2022-09-20 15:12:24 +02:00
|
|
|
|
|
|
|
|
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
|
|
|
|
{
|
|
|
|
|
detectRam(&memory->ram);
|
|
|
|
|
detectSwap(&memory->swap);
|
|
|
|
|
}
|