]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Implement memory plugin for NetBSD
authorEdgar Fuß <ef@math.uni-bonn.de>
Wed, 8 Jul 2020 17:20:45 +0000 (19:20 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2020 17:20:45 +0000 (19:20 +0200)
Add a port of the memory plugin for NetBSD using VM_UVMEXP2, and preferring sysctl over sysctlbyname.
Written by Håvard Eidnes <he@NetBSD.org>

src/memory.c

index 107e867f9c92e8524e4332f961ba05b3dcbad652..60546a0cb93decf6e91c83a007659a987620a840 100644 (file)
@@ -68,6 +68,10 @@ static mach_port_t port_host;
 static vm_size_t pagesize;
 /* #endif HAVE_HOST_STATISTICS */
 
+#elif HAVE_SYSCTL
+static int pagesize;
+/* #endif HAVE_SYSCTL */
+
 #elif HAVE_SYSCTLBYNAME
 /* no global variables */
 /* #endif HAVE_SYSCTLBYNAME */
@@ -82,11 +86,6 @@ static kstat_t *ksp;
 static kstat_t *ksz;
 /* #endif HAVE_LIBKSTAT */
 
-#elif HAVE_SYSCTL && __OpenBSD__
-/* OpenBSD variant does not have sysctlbyname */
-static int pagesize;
-/* #endif HAVE_SYSCTL && __OpenBSD__ */
-
 #elif HAVE_LIBSTATGRAB
 /* no global variables */
 /* endif HAVE_LIBSTATGRAB */
@@ -97,6 +96,10 @@ static int pagesize;
 #error "No applicable input method."
 #endif
 
+#if KERNEL_NETBSD
+# include <uvm/uvm_extern.h>
+#endif
+
 static bool values_absolute = true;
 static bool values_percentage;
 
@@ -225,6 +228,45 @@ static int memory_read_internal(value_list_t *vl) {
   /* #endif HAVE_HOST_STATISTICS */
 
 #elif HAVE_SYSCTLBYNAME
+
+#if HAVE_SYSCTL && defined(KERNEL_NETBSD)
+  int mib[] = {CTL_VM, VM_UVMEXP2};
+  struct uvmexp_sysctl uvmexp;
+  gauge_t mem_active;
+  gauge_t mem_inactive;
+  gauge_t mem_free;
+  gauge_t mem_wired;
+  gauge_t mem_kernel;
+  size_t size;
+
+  memset (&uvmexp, 0, sizeof (uvmexp));
+  size = sizeof (uvmexp);
+
+  if (sysctl (mib, 2, &uvmexp, &size, NULL, 0) < 0) {
+    char errbuf[1024];
+    WARNING ("memory plugin: sysctl failed: %s",
+      sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (-1);
+  }
+
+  assert (pagesize > 0);
+  mem_active   = (gauge_t) (uvmexp.active * pagesize);
+  mem_inactive = (gauge_t) (uvmexp.inactive * pagesize);
+  mem_free     = (gauge_t) (uvmexp.free * pagesize);
+  mem_wired    = (gauge_t) (uvmexp.wired * pagesize);
+  mem_kernel   = (gauge_t) ((uvmexp.npages - (
+    uvmexp.active + uvmexp.inactive +
+    uvmexp.free + uvmexp.wired
+    )) * pagesize);
+
+  MEMORY_SUBMIT ("active",   mem_active,
+                 "inactive", mem_inactive,
+                 "free",     mem_free,
+                 "wired",    mem_wired,
+                 "kernel",   mem_kernel);
+/* #endif HAVE_SYSCTL && defined(KERNEL_NETBSD) */
+
+#else /* Other HAVE_SYSCTLBYNAME providers */
   /*
    * vm.stats.vm.v_page_size: 4096
    * vm.stats.vm.v_page_count: 246178
@@ -263,6 +305,8 @@ static int memory_read_internal(value_list_t *vl) {
                 (gauge_t)sysctl_vals[3], "active", (gauge_t)sysctl_vals[4],
                 "inactive", (gauge_t)sysctl_vals[5], "cache",
                 (gauge_t)sysctl_vals[6]);
+
+#endif /* HAVE_SYSCTL && KERNEL_NETBSD */
   /* #endif HAVE_SYSCTLBYNAME */
 
 #elif KERNEL_LINUX