Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ AC_CHECK_HEADERS([execinfo.h])
if test "$my_htop_platform" = darwin; then
AC_CHECK_HEADERS([mach/mach_time.h])
AC_CHECK_TYPES([thread_extended_info_data_t], [], [], [[#include <mach/thread_info.h>]])
AC_CHECK_MEMBERS([struct vm_statistics64.compressor_page_count], [], [], [[#include <mach/mach_host.h>]])
AC_CHECK_MEMBERS([struct vm_statistics64.external_page_count], [], [], [[#include <mach/mach_host.h>]])

AC_CHECK_DECLS([kIOMainPortDefault], [], [], [[#include <IOKit/IOKitLib.h>]])
fi

# ----------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions darwin/DarwinMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ in the source distribution for its full text.
#include <unistd.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#include <Availability.h>

#include "CRT.h"
#include "Machine.h"
Expand All @@ -24,6 +25,10 @@ in the source distribution for its full text.
#include "generic/openzfs_sysctl.h"
#include "zfs/ZfsArcStats.h"

// Compatibility for older SDKs.
#if defined(HAVE_DECL_KIOMAINPORTDEFAULT) && !HAVE_DECL_KIOMAINPORTDEFAULT
#define kIOMainPortDefault kIOMasterPortDefault
#endif

static void DarwinMachine_getHostInfo(host_basic_info_data_t* p) {
mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
Expand Down
17 changes: 14 additions & 3 deletions darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ in the source distribution for its full text.
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <sys/socket.h>
#include <mach/port.h>
#include <net/if.h> // After `sys/socket.h` for struct `sockaddr` (for iOS6 SDK)

#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFDictionary.h>
Expand Down Expand Up @@ -404,15 +404,26 @@ void Platform_setMemoryValues(Meter* mtr) {
mtr->total = dhost->host_info.max_mem / 1024;
#ifdef HAVE_STRUCT_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;
vm->speculative_count + vm->wire_count - vm->purgeable_count;
#ifdef HAVE_STRUCT_VM_STATISTICS64_EXTERNAL_PAGE_COUNT
used -= vm->external_page_count;
#endif
#ifdef HAVE_STRUCT_VM_STATISTICS64_COMPRESSOR_PAGE_COUNT
used += vm->compressor_page_count;
mtr->values[MEMORY_METER_USED] = (double)(used - vm->compressor_page_count) * page_K;
#else
mtr->values[MEMORY_METER_USED] = (double)used * page_K;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate mtr->values[MEMORY_METER_USED] assignment line doesn't look right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, there are already two branches. To avoid assigning again, I'll have to introduce a new variable:

@@ -401,6 +401,7 @@
    const struct vm_statistics* vm = &dhost->vm_stats;
 #endif
    double page_K = (double)vm_page_size / (double)1024;
+   double usedPages = 0.0;
 
    mtr->total = dhost->host_info.max_mem / 1024;
 #ifdef HAVE_STRUCT_VM_STATISTICS64
@@ -411,13 +412,14 @@
 #endif
 #ifdef HAVE_STRUCT_VM_STATISTICS64_COMPRESSOR_PAGE_COUNT
    used += vm->compressor_page_count;
-   mtr->values[MEMORY_METER_USED] = (double)(used - vm->compressor_page_count) * page_K;
+   usedPages = (double)(used - vm->compressor_page_count);
 #else
-   mtr->values[MEMORY_METER_USED] = (double)used * page_K;
+   usedPages = (double)used;
 #endif
 #else
-   mtr->values[MEMORY_METER_USED] = (double)(vm->active_count + vm->wire_count) * page_K;
+   usedPages = (double)(vm->active_count + vm->wire_count);
 #endif
+   mtr->values[MEMORY_METER_USED] = usedPages * page_K;
    // mtr->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
 #ifdef HAVE_STRUCT_VM_STATISTICS64
 #ifdef HAVE_STRUCT_VM_STATISTICS64_COMPRESSOR_PAGE_COUNT

which doesn't look too elegant to me. What do you say?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it might be good to examine why there were two branches in the first place. Ideally the formula for counting "used" memory shouldn't differ between 32-bits and 64-bits.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Apple Silicon support (731812d), perhaps something to do with Unified Memory if I'm reading it right.

That said, it's not yet clear to me how to unify all three branches together.

#endif
#else
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"
#ifdef HAVE_STRUCT_VM_STATISTICS64
#ifdef HAVE_STRUCT_VM_STATISTICS64_COMPRESSOR_PAGE_COUNT
mtr->values[MEMORY_METER_COMPRESSED] = (double)vm->compressor_page_count * page_K;
#else
// mtr->values[MEMORY_METER_COMPRESSED] = "compressed memory not available"
#endif
#else
// mtr->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
#endif
Expand Down