freebsd branch: First attempt at porting the `memory' plugin to FreeBSD.
[collectd.git] / src / memory.c
index 3ab19e6..db7662f 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/memory.c
- * Copyright (C) 2005  Florian octo Forster
+ * Copyright (C) 2005,2006  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
 #include "common.h"
 #include "plugin.h"
 
-#if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
+#ifdef HAVE_SYS_SYSCTL_H
+# include <sys/sysctl.h>
+#endif
+
+#ifdef HAVE_MACH_KERN_RETURN_H
+# include <mach/kern_return.h>
+#endif
+#ifdef HAVE_MACH_MACH_INIT_H
+# include <mach/mach_init.h>
+#endif
+#ifdef HAVE_MACH_MACH_HOST_H
+# include <mach/mach_host.h>
+#endif
+#ifdef HAVE_MACH_HOST_PRIV_H
+# include <mach/host_priv.h>
+#endif
+#ifdef MACH_VM_STATISTICS_H
+# include <mach/vm_statistics.h>
+#endif
+
+#if !defined(VM_TOTAL) && defined(VM_METER)
+# define VM_TOTAL VM_METER
+#endif
+
+#if defined (HOST_VM_INFO) || defined(VM_TOTAL) || KERNEL_LINUX || HAVE_LIBKSTAT
 # define MEMORY_HAVE_READ 1
 #else
 # define MEMORY_HAVE_READ 0
@@ -37,22 +61,49 @@ static char *memory_file = "memory.rrd";
 /* 9223372036854775807 == LLONG_MAX */
 static char *ds_def[] =
 {
-       "DS:used:GAUGE:25:0:9223372036854775807",
-       "DS:free:GAUGE:25:0:9223372036854775807",
-       "DS:buffers:GAUGE:25:0:9223372036854775807",
-       "DS:cached:GAUGE:25:0:9223372036854775807",
+       "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
+       "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
+       "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
+       "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
        NULL
 };
 static int ds_num = 4;
 
-#ifdef HAVE_LIBKSTAT
+/* vm_statistics_data_t */
+#if defined(HOST_VM_INFO)
+static mach_port_t port_host;
+static vm_size_t pagesize;
+/* #endif HOST_VM_INFO */
+
+#elif defined(VM_TOTAL)
+/* no global variables */
+/* #endif VM_METER */
+
+#elif KERNEL_LINUX
+/* no global variables */
+/* #endif KERNEL_LINUX */
+
+#elif HAVE_LIBKSTAT
 static int pagesize;
 static kstat_t *ksp;
 #endif /* HAVE_LIBKSTAT */
 
 static void memory_init (void)
 {
-#ifdef HAVE_LIBKSTAT
+#if defined(HOST_VM_INFO)
+       port_host = mach_host_self ();
+       host_page_size (port_host, &pagesize);
+/* #endif HOST_VM_INFO */
+
+#elif defined(VM_TOTAL)
+/* no init stuff */
+/* #endif VM_TOTAL */
+
+#elif defined(KERNEL_LINUX)
+/* no init stuff */
+/* #endif KERNEL_LINUX */
+
+#elif defined(HAVE_LIBKSTAT)
        /* getpagesize(3C) tells me this does not fail.. */
        pagesize = getpagesize ();
        if (get_kstat (&ksp, "unix", 0, "system_pages"))
@@ -85,7 +136,109 @@ static void memory_submit (long long mem_used, long long mem_buffered,
 
 static void memory_read (void)
 {
-#ifdef KERNEL_LINUX
+#if defined(HOST_VM_INFO)
+       kern_return_t status;
+       vm_statistics_data_t   vm_data;
+       mach_msg_type_number_t vm_data_len;
+
+       long long wired;
+       long long active;
+       long long inactive;
+       long long free;
+
+       if (!port_host || !pagesize)
+               return;
+
+       vm_data_len = sizeof (vm_data) / sizeof (natural_t);
+       if ((status = host_statistics (port_host, HOST_VM_INFO,
+                                       (host_info_t) &vm_data,
+                                       &vm_data_len)) != KERN_SUCCESS)
+       {
+               syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
+               return;
+       }
+
+       /*
+        * From <http://docs.info.apple.com/article.html?artnum=107918>:
+        *
+        * Wired memory
+        *   This information can't be cached to disk, so it must stay in RAM.
+        *   The amount depends on what applications you are using.
+        *
+        * Active memory
+        *   This information is currently in RAM and actively being used.
+        *
+        * Inactive memory
+        *   This information is no longer being used and has been cached to
+        *   disk, but it will remain in RAM until another application needs
+        *   the space. Leaving this information in RAM is to your advantage if
+        *   you (or a client of your computer) come back to it later.
+        *
+        * Free memory
+        *   This memory is not being used.
+        */
+
+       wired    = vm_data.wire_count     * pagesize;
+       active   = vm_data.active_count   * pagesize;
+       inactive = vm_data.inactive_count * pagesize;
+       free     = vm_data.free_count     * pagesize;
+
+       memory_submit (wired + active, -1, inactive, free);
+/* #endif HOST_VM_INFO */
+
+#elif defined(VM_TOTAL)
+       /*
+        * vm.stats.vm.v_page_size: 4096
+        * vm.stats.vm.v_page_count: 246178
+        * vm.stats.vm.v_free_count: 28760
+        * vm.stats.vm.v_wire_count: 37526
+        * vm.stats.vm.v_active_count: 55239
+        * vm.stats.vm.v_inactive_count: 113730
+        * vm.stats.vm.v_cache_count: 10809
+        */
+       int            page_size;
+       int            page_count;
+       int            free_count;
+       int            wire_count;
+       int            active_count;
+       int            inactive_count;
+       int            cache_count;
+       size_t         data_len;
+
+       int            status;
+
+       data_len = sizeof (page_size);
+       if ((status = sysctlbyname ("vm.stats.vm.v_page_size",
+                                       (void *) &page_size, data_len,
+                                       NULL, 0)) < 0)
+       {
+               syslog (LOG_ERR, "memory plugin: sysctlbyname failed: %s",
+                               strerror (status));
+               return;
+       }
+
+       ...
+
+       status = sysctl (mib, sizeof (mib),
+                       (void *) &data, &data_len,
+                       NULL, 0); /* new value pointer */
+       if (status < 0)
+       {
+               DBG ("sysctl failed: %s", strerror (errno));
+               return;
+       }
+
+       if (data_len != sizeof (data))
+       {
+               DBG ("data_len = %u; sizeof (data) = %u;",
+                               (unsigned int) data_len,
+                               (unsigned int) sizeof (data));
+               return;
+       }
+
+/* #endif VM_TOTAL */
+
+#elif defined(KERNEL_LINUX)
        FILE *fh;
        char buffer[1024];