about summary refs log blame commit diff
path: root/users/multi/pkgs/htop/zfs-arc-stats/0004-ZFS-arcstats-for-Darwin-macOS-OS-X.patch
blob: 9dbc365ebeaef2cce189743719d6dcf8817d7471 (plain) (tree)



































































































































































































































































                                                                                                                       
From fc8e9a2d3e25e35c0f9903baa345b1744b12b6cb Mon Sep 17 00:00:00 2001
From: Ross Williams <ross@ross-williams.net>
Date: Sun, 7 Jul 2019 17:30:37 -0400
Subject: [PATCH 4/9] ZFS arcstats for Darwin (macOS / OS X)

---
 Makefile.am                |  6 ++-
 darwin/DarwinProcessList.c | 80 ++++++++++++++++++++++++++++++++++++++
 darwin/DarwinProcessList.h | 21 ++++++++++
 darwin/Platform.c          | 19 +++++++++
 darwin/Platform.h          |  2 +
 5 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index b850815..b6d2117 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -134,14 +134,16 @@ darwin_platform_headers = \
 	darwin/DarwinProcess.h \
 	darwin/DarwinProcessList.h \
 	darwin/DarwinCRT.h \
-	darwin/Battery.h
+	darwin/Battery.h \
+	$(zfs_platform_headers)
 
 all_platform_headers += $(darwin_platform_headers)
 
 if HTOP_DARWIN
 AM_LDFLAGS += -framework IOKit -framework CoreFoundation
 myhtopplatsources = darwin/Platform.c darwin/DarwinProcess.c \
-darwin/DarwinProcessList.c darwin/DarwinCRT.c darwin/Battery.c
+darwin/DarwinProcessList.c darwin/DarwinCRT.c darwin/Battery.c \
+$(zfs_platform_sources)
 
 myhtopplatheaders = $(darwin_platform_headers)
 endif
diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c
index 0988448..267e8e9 100644
--- a/darwin/DarwinProcessList.c
+++ b/darwin/DarwinProcessList.c
@@ -54,6 +54,7 @@ int CompareKernelVersion(short int major, short int minor, short int component)
 
 /*{
 #include "ProcessList.h"
+#include "zfs/ZfsArcStats.h"
 #include <mach/mach_host.h>
 #include <sys/sysctl.h>
 
@@ -67,10 +68,28 @@ typedef struct DarwinProcessList_ {
    uint64_t kernel_threads;
    uint64_t user_threads;
    uint64_t global_diff;
+
+   int zfsArcEnabled;
+   unsigned long long int zfsArcMax;
+   unsigned long long int zfsArcSize;
+   unsigned long long int zfsArcMFU;
+   unsigned long long int zfsArcMRU;
+   unsigned long long int zfsArcAnon;
+   unsigned long long int zfsArcHeader;
+   unsigned long long int zfsArcOther;
+
 } DarwinProcessList;
 
 }*/
 
+static int MIB_kstat_zfs_misc_arcstats_c_max[5];
+static int MIB_kstat_zfs_misc_arcstats_size[5];
+static int MIB_kstat_zfs_misc_arcstats_mfu_size[5];
+static int MIB_kstat_zfs_misc_arcstats_mru_size[5];
+static int MIB_kstat_zfs_misc_arcstats_anon_size[5];
+static int MIB_kstat_zfs_misc_arcstats_hdr_size[5];
+static int MIB_kstat_zfs_misc_arcstats_other_size[5];
+
 void ProcessList_getHostInfo(host_basic_info_data_t *p) {
    mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
 
@@ -131,8 +150,50 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
    return processes;
 }
 
+static inline void DarwinProcessList_scanZfsArcstats(DarwinProcessList* dpl) {
+   size_t len;
+
+   if (dpl->zfsArcEnabled) {
+      len = sizeof(dpl->zfsArcSize);
+      sysctl(MIB_kstat_zfs_misc_arcstats_size, 5, &(dpl->zfsArcSize), &len , NULL, 0);
+      /* TODO: adjust reported memory in use to move ARC from wired to inactive
+         Like:
+         // In bytes
+         dpl->vm_stats.wire_count -= dpl->zfsArcSize / vm_page_size;
+         dpl->vm_stats.inactive_count += dpl->zfsArcSize / vm_page_size;
+         // Would purgable_count be more true?
+         // Then convert to KB:
+      */
+      dpl->zfsArcSize /= 1024;
+
+      len = sizeof(dpl->zfsArcMax);
+      sysctl(MIB_kstat_zfs_misc_arcstats_c_max, 5, &(dpl->zfsArcMax), &len , NULL, 0);
+      dpl->zfsArcMax /= 1024;
+
+      len = sizeof(dpl->zfsArcMFU);
+      sysctl(MIB_kstat_zfs_misc_arcstats_mfu_size, 5, &(dpl->zfsArcMFU), &len , NULL, 0);
+      dpl->zfsArcMFU /= 1024;
+
+      len = sizeof(dpl->zfsArcMRU);
+      sysctl(MIB_kstat_zfs_misc_arcstats_mru_size, 5, &(dpl->zfsArcMRU), &len , NULL, 0);
+      dpl->zfsArcMRU /= 1024;
+
+      len = sizeof(dpl->zfsArcAnon);
+      sysctl(MIB_kstat_zfs_misc_arcstats_anon_size, 5, &(dpl->zfsArcAnon), &len , NULL, 0);
+      dpl->zfsArcAnon /= 1024;
+
+      len = sizeof(dpl->zfsArcHeader);
+      sysctl(MIB_kstat_zfs_misc_arcstats_hdr_size, 5, &(dpl->zfsArcHeader), &len , NULL, 0);
+      dpl->zfsArcHeader /= 1024;
+
+      len = sizeof(dpl->zfsArcOther);
+      sysctl(MIB_kstat_zfs_misc_arcstats_other_size, 5, &(dpl->zfsArcOther), &len , NULL, 0);
+      dpl->zfsArcOther /= 1024;
+   }
+}
 
 ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
+   size_t len;
    DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
 
    ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId);
@@ -145,6 +206,24 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
    /* Initialize the VM statistics */
    ProcessList_getVMStats(&this->vm_stats);
 
+   /* Initialize the ZFS kstats, if zfs.kext loaded */
+   len = sizeof(this->zfsArcSize);
+   if (sysctlbyname("kstat.zfs.misc.arcstats.size", &this->zfsArcSize, &len,
+	    NULL, 0) == 0 && this->zfsArcSize != 0) {
+		  this->zfsArcEnabled = 1;
+
+                  len = 5;
+                  sysctlnametomib("kstat.zfs.misc.arcstats.size", MIB_kstat_zfs_misc_arcstats_size, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.c_max", MIB_kstat_zfs_misc_arcstats_c_max, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.mfu_size", MIB_kstat_zfs_misc_arcstats_mfu_size, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.mru_size", MIB_kstat_zfs_misc_arcstats_mru_size, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.anon_size", MIB_kstat_zfs_misc_arcstats_anon_size, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.hdr_size", MIB_kstat_zfs_misc_arcstats_hdr_size, &len);
+                  sysctlnametomib("kstat.zfs.misc.arcstats.other_size", MIB_kstat_zfs_misc_arcstats_other_size, &len);
+   } else {
+		  this->zfsArcEnabled = 0;
+   }
+
    this->super.kernelThreads = 0;
    this->super.userlandThreads = 0;
    this->super.totalTasks = 0;
@@ -173,6 +252,7 @@ void ProcessList_goThroughEntries(ProcessList* super) {
     dpl->prev_load = dpl->curr_load;
     ProcessList_allocateCPULoadInfo(&dpl->curr_load);
     ProcessList_getVMStats(&dpl->vm_stats);
+    DarwinProcessList_scanZfsArcstats(dpl);
 
     /* Get the time difference */
     dpl->global_diff = 0;
diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h
index c216a80..6686d05 100644
--- a/darwin/DarwinProcessList.h
+++ b/darwin/DarwinProcessList.h
@@ -9,6 +9,17 @@ Released under the GNU GPL, see the COPYING file
 in the source distribution for its full text.
 */
 
+struct kern;
+
+void GetKernelVersion(struct kern *k);
+
+/* compare the given os version with the one installed returns:
+0 if equals the installed version
+positive value if less than the installed version
+negative value if more than the installed version
+*/
+int CompareKernelVersion(short int major, short int minor, short int component);
+
 #include "ProcessList.h"
 #include <mach/mach_host.h>
 #include <sys/sysctl.h>
@@ -23,6 +34,16 @@ typedef struct DarwinProcessList_ {
    uint64_t kernel_threads;
    uint64_t user_threads;
    uint64_t global_diff;
+
+   int zfsArcEnabled;
+   unsigned long long int zfsArcMax;
+   unsigned long long int zfsArcSize;
+   unsigned long long int zfsArcMFU;
+   unsigned long long int zfsArcMRU;
+   unsigned long long int zfsArcAnon;
+   unsigned long long int zfsArcHeader;
+   unsigned long long int zfsArcOther;
+
 } DarwinProcessList;
 
 
diff --git a/darwin/Platform.c b/darwin/Platform.c
index 1dce8b6..52d60a9 100644
--- a/darwin/Platform.c
+++ b/darwin/Platform.c
@@ -15,6 +15,7 @@ in the source distribution for its full text.
 #include "ClockMeter.h"
 #include "HostnameMeter.h"
 #include "UptimeMeter.h"
+#include "zfs/ZfsArcMeter.h"
 #include "DarwinProcessList.h"
 
 #include <stdlib.h>
@@ -117,6 +118,7 @@ MeterClass* Platform_meterTypes[] = {
    &RightCPUsMeter_class,
    &LeftCPUs2Meter_class,
    &RightCPUs2Meter_class,
+   &ZfsArcMeter_class,
    &BlankMeter_class,
    NULL
 };
@@ -241,6 +243,23 @@ void Platform_setSwapValues(Meter* mtr) {
   mtr->values[0] = swapused.xsu_used / 1024;
 }
 
+void Platform_setZfsArcValues(Meter* this) {
+   DarwinProcessList* dpl = (DarwinProcessList*) this->pl;
+
+   this->total = dpl->zfsArcMax;
+   this->values[0] = dpl->zfsArcMFU;
+   this->values[1] = dpl->zfsArcMRU;
+   this->values[2] = dpl->zfsArcAnon;
+   this->values[3] = dpl->zfsArcHeader;
+   this->values[4] = dpl->zfsArcOther;
+
+   // "Hide" the last value so it can
+   // only be accessed by index and is not
+   // displayed by the Bar or Graph style
+   Meter_setItems(this, 5);
+   this->values[5] = dpl->zfsArcSize;
+}
+
 char* Platform_getProcessEnv(pid_t pid) {
    char* env = NULL;
 
diff --git a/darwin/Platform.h b/darwin/Platform.h
index 1231217..4acda2c 100644
--- a/darwin/Platform.h
+++ b/darwin/Platform.h
@@ -48,6 +48,8 @@ void Platform_setMemoryValues(Meter* mtr);
 
 void Platform_setSwapValues(Meter* mtr);
 
+void Platform_setZfsArcValues(Meter* mtr);
+
 char* Platform_getProcessEnv(pid_t pid);
 
 #endif
-- 
2.20.1