Skip to content

Commit

Permalink
darwin: Enhance memory metrics support for Apple Silicon (ARM64)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuCicada committed Apr 11, 2024
1 parent 72afdaa commit 09c86e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
15 changes: 15 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ if test "$my_htop_platform" = darwin; then
AC_CHECK_FUNCS([mach_timebase_info])
AC_CHECK_DECLS([IOMainPort], [], [], [[#include <IOKit/IOKitLib.h>]])
AC_CHECK_DECLS([IOMasterPort], [], [], [[#include <IOKit/IOKitLib.h>]])

AC_MSG_CHECKING([for vm_statistics64 supported])
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([[
#include <mach/mach_host.h>
#include <mach/vm_statistics.h>
]], [[
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;
vm_statistics64_data_t vm_stat;
host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &info_size);
]]
)],
AC_DEFINE([HAVE_VM_STATISTICS64], 1, [The vm_statistics64 features is supported.])
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
fi

if test "$my_htop_platform" = pcp; then
Expand Down
2 changes: 1 addition & 1 deletion darwin/DarwinMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static unsigned DarwinMachine_allocateCPULoadInfo(processor_cpu_load_info_t* p)
}

static void DarwinMachine_getVMStats(DarwinMachine* this) {
#if defined(__LP64__)
#ifdef HAVE_VM_STATISTICS64
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;

if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)&this->vm_stats, &info_size) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion darwin/DarwinMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct DarwinMachine_ {
Machine super;

host_basic_info_data_t host_info;
#if defined(__LP64__)
#ifdef HAVE_VM_STATISTICS64
vm_statistics64_data_t vm_stats;
#else
vm_statistics_data_t vm_stats;
Expand Down
6 changes: 3 additions & 3 deletions darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) {

void Platform_setMemoryValues(Meter* mtr) {
const DarwinMachine* dhost = (const DarwinMachine*) mtr->host;
#if defined(__LP64__)
#ifdef HAVE_VM_STATISTICS64
const struct vm_statistics64* vm = &dhost->vm_stats;
#else
const struct vm_statistics* vm = &dhost->vm_stats;
#endif
double page_K = (double)vm_page_size / (double)1024;

mtr->total = dhost->host_info.max_mem / 1024;
#if defined(__LP64__)
#ifdef HAVE_VM_STATISTICS64
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;
Expand All @@ -309,7 +309,7 @@ void Platform_setMemoryValues(Meter* mtr) {
mtr->values[MEMORY_METER_USED] = (double)(vm->active_count + vm->wire_count) * page_K;
#endif
// mtr->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
#if defined(__LP64__)
#ifdef HAVE_VM_STATISTICS64
mtr->values[MEMORY_METER_COMPRESSED] = (double)vm->compressor_page_count * page_K;
#else
// mtr->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
Expand Down

0 comments on commit 09c86e2

Please sign in to comment.