From 05e4b489d1fd3e2c91138138959b289feaa169ef Mon Sep 17 00:00:00 2001
From: OBATA Akio <obache@wizdas.com>
Date: Tue, 11 Jan 2022 18:51:50 +0900
Subject: kicker-applets: Add NetBSD support for KTimemon

Signed-off-by: OBATA Akio <obache@wizdas.com>
---
 kicker-applets/ktimemon/sample.cpp | 62 ++++++++++++++++++++++++++++++++++++++
 kicker-applets/ktimemon/sample.h   |  7 +++++
 2 files changed, 69 insertions(+)

diff --git a/kicker-applets/ktimemon/sample.cpp b/kicker-applets/ktimemon/sample.cpp
index a57a7c6..3ff9dec 100644
--- a/kicker-applets/ktimemon/sample.cpp
+++ b/kicker-applets/ktimemon/sample.cpp
@@ -31,6 +31,12 @@
 #include <sys/swap.h>
 #endif
 
+#ifdef __NetBSD__
+#include <sys/sysctl.h>
+#include <sys/sched.h>
+#include <uvm/uvm_extern.h>
+#endif
+
 #include <tqwidget.h>
 #include <tdelocale.h>
 #include <tdemessagebox.h>
@@ -381,6 +387,60 @@ void KSample::readSample()
         free(stbl);
     }
 
+#elif defined(__NetBSD__)
+    int mib[2];
+    int ncpu;
+    size_t len;
+
+    mib[0] = CTL_HW;
+    mib[1] = HW_NCPU;
+    len = sizeof(ncpu);
+    if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) {
+        fatal(i18n("Unable to get the number of CPUs configured.\n"
+                   "The diagnostics are '%1'.\n"
+                   "Please contact the maintainer at http://bugs.trinitydesktop.org/ who will try to sort this out.").arg(strerror(errno)));
+    } else {
+        sample.cpus = ncpu;
+    }
+
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_CP_TIME;
+    uint64_t cp_time[CPUSTATES];
+    len = sizeof(cp_time);
+    if (sysctl(mib, 2, cp_time, &len, NULL, 0) < 0) {
+        fatal(i18n("Unable to get CPUSTATES.\n"
+                   "The diagnostics are '%1'.\n"
+                   "Please contact the maintainer at http://bugs.trinitydesktop.org/ who will try to sort this out.").arg(strerror(errno)));
+    } else {
+        sample.user = cp_time[CP_USER];
+        sample.nice = cp_time[CP_NICE];
+        sample.kernel = cp_time[CP_SYS];
+        sample.iowait = cp_time[CP_INTR];
+        sample.idle = cp_time[CP_IDLE];
+    }
+
+    mib[0] = CTL_VM;
+    mib[1] = VM_UVMEXP2;
+
+    struct uvmexp_sysctl u;
+    len = sizeof(u);
+    if (sysctl(mib, 2, &u, &len, NULL, 0) < 0) {
+        fatal(i18n("Unable to get system wide vertual memory statistics.\n"
+                   "The diagnostics are '%1'.\n"
+                   "Please contact the maintainer at http://bugs.trinitydesktop.org/ who will try to sort this out.").arg(strerror(errno)));
+    } else {
+        pg_to_mb_shift = 20 - u.pageshift;
+        sample.mtotal = u.npages;
+        sample.free = u.free;
+        sample.buffers = u.filepages;
+        sample.cached = u.anonpages + u.execpages;
+        sample.mkernel = u.wired;
+        sample.stotal = u.swpages;
+        sample.sused = u.swpginuse;
+        sample.sfree = u.swpages - u.swpginuse;
+    }
+
+
 #else
 #warning This type of system is not supported
     sample.stotal = sample.sfree = 0;
@@ -406,6 +466,8 @@ inline void KSample::makeMBytes(unsigned long &v)
     v /= 1024;                  // can it be simpler ;-)
 #elif defined(USE_SOLARIS)
     v /= pagesPerMB;
+#elif defined(__NetBSD__)
+    v >>= pg_to_mb_shift;
 #endif
 }
 
diff --git a/kicker-applets/ktimemon/sample.h b/kicker-applets/ktimemon/sample.h
index 2a79ad9..eb9c6b8 100644
--- a/kicker-applets/ktimemon/sample.h
+++ b/kicker-applets/ktimemon/sample.h
@@ -47,10 +47,12 @@ public:
         void fill(unsigned scale);	// fill sample with some fake values
     };
 
+#ifdef __linux__
     struct MemStats {
         const char *name;
         unsigned long *stat;
     };
+#endif
 
     KSample(KTimeMon *timemon, bool autoScale, unsigned pageScale,
             unsigned swapScale, unsigned ctxScale);
@@ -82,11 +84,16 @@ private:
 #endif
 #if defined(USE_SOLARIS)
     unsigned long pagesPerMB;
+#endif
+#ifdef __NetBSD__
+    int pg_to_mb_shift;
 #endif
     Sample sample, oldSample;
     unsigned pageScale, swapScale, cxScale;
     bool autoscale;
+#ifdef __linux__
     struct MemStats memstats[7];
+#endif
 };
 
 #endif // SAMPLE_H
-- 
cgit v1.2.3

