Skip to content

Commit

Permalink
darwin: Upgrade the relevant calculations of vm state to vm state 64
Browse files Browse the repository at this point in the history
  • Loading branch information
SuCicada committed Apr 9, 2024
1 parent b3d2dcc commit bfa5269
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 36 deletions.
16 changes: 0 additions & 16 deletions darwin/DarwinMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,13 @@ static unsigned DarwinMachine_allocateCPULoadInfo(processor_cpu_load_info_t* p)
return cpu_count;
}

static void DarwinMachine_getVMStats(vm_statistics_t p) {
mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;

if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) {
CRT_fatalError("Unable to retrieve VM statistics");
}
}

#if defined(__arm64__)
static void DarwinMachine_getVMStats64(vm_statistics64_t p) {
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;

if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t) p, &info_size) != 0) {
CRT_fatalError("Unable to retrieve VM statistics64");
}
}
#endif

void Machine_scan(Machine* super) {
DarwinMachine* host = (DarwinMachine*) super;
Expand All @@ -84,10 +74,7 @@ void Machine_scan(Machine* super) {
DarwinMachine_freeCPULoadInfo(&host->prev_load);
host->prev_load = host->curr_load;
DarwinMachine_allocateCPULoadInfo(&host->curr_load);
DarwinMachine_getVMStats(&host->vm_stats);
#if defined(__arm64__)
DarwinMachine_getVMStats64(&host->vm_stats64);
#endif
openzfs_sysctl_updateArcStats(&host->zfs);
}

Expand All @@ -104,10 +91,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
DarwinMachine_allocateCPULoadInfo(&this->curr_load);

/* Initialize the VM statistics */
DarwinMachine_getVMStats(&this->vm_stats);
#if defined(__arm64__)
DarwinMachine_getVMStats64(&this->vm_stats64);
#endif

/* Initialize the ZFS kstats, if zfs.kext loaded */
openzfs_sysctl_init(&this->zfs);
Expand Down
3 changes: 0 additions & 3 deletions darwin/DarwinMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ typedef struct DarwinMachine_ {
Machine super;

host_basic_info_data_t host_info;
vm_statistics_data_t vm_stats;
#if defined(__arm64__)
vm_statistics64_data_t vm_stats64;
#endif
processor_cpu_load_info_t prev_load;
processor_cpu_load_info_t curr_load;

Expand Down
18 changes: 1 addition & 17 deletions darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,13 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) {
return CLAMP(total, 0.0, 100.0);
}

#if defined(__arm64__)
void Platform_setMemoryValues(Meter *mtr) {
const DarwinMachine *dhost = (const DarwinMachine *) mtr->host;
const struct vm_statistics64 *vm = &dhost->vm_stats64;
double page_K = (double) vm_page_size / (double) 1024;

mtr->total = dhost->host_info.max_mem / 1024;
int used = vm->active_count + vm->inactive_count +
natural_t used = vm->active_count + vm->inactive_count +
vm->speculative_count + vm->wire_count +
vm->compressor_page_count - vm->purgeable_count - vm->external_page_count;
mtr->values[MEMORY_METER_USED] = (double) (used - vm->compressor_page_count) * page_K;
Expand All @@ -307,21 +306,6 @@ void Platform_setMemoryValues(Meter *mtr) {
mtr->values[MEMORY_METER_CACHE] = (double) vm->inactive_count * page_K;
// mtr->values[MEMORY_METER_AVAILABLE] = "available memory"
}
#else
void Platform_setMemoryValues(Meter* mtr) {
const DarwinMachine* dhost = (const DarwinMachine*) mtr->host;
const struct vm_statistics* vm = &dhost->vm_stats;
double page_K = (double)vm_page_size / (double)1024;

mtr->total = dhost->host_info.max_mem / 1024;
mtr->values[MEMORY_METER_USED] = (double)(vm->active_count + vm->wire_count) * page_K;
// mtr->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
// mtr->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
mtr->values[MEMORY_METER_BUFFERS] = (double)vm->purgeable_count * page_K;
mtr->values[MEMORY_METER_CACHE] = (double)vm->inactive_count * page_K;
// mtr->values[MEMORY_METER_AVAILABLE] = "available memory"
}
#endif

void Platform_setSwapValues(Meter* mtr) {
int mib[2] = {CTL_VM, VM_SWAPUSAGE};
Expand Down

0 comments on commit bfa5269

Please sign in to comment.