Code

octo@casella:~/collectd $ svn merge -r753:807 trunk branches/processes
[collectd.git] / src / memory.c
index 3291bc870c4cfd6bb977f58a8f0f86921296a272..00fd1bf9504c5430bfbc3ab352819c25bde81a47 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
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-#include "memory.h"
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
 
-#if COLLECT_MEMORY
-#define MODULE_NAME "memory"
+#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 HAVE_MACH_VM_STATISTICS_H
+# include <mach/vm_statistics.h>
+#endif
+
+#if defined (HOST_VM_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
+# define MEMORY_HAVE_READ 1
+#else
+# define MEMORY_HAVE_READ 0
+#endif
 
-#include "plugin.h"
-#include "common.h"
+#define MODULE_NAME "memory"
 
 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(KERNEL_LINUX)
+/* no global variables */
+/* #endif KERNEL_LINUX */
+
+#elif defined(HAVE_LIBKSTAT)
 static int pagesize;
 static kstat_t *ksp;
 #endif /* HAVE_LIBKSTAT */
 
-extern time_t curtime;
-
-void memory_init (void)
+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(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"))
@@ -60,13 +97,14 @@ void memory_init (void)
        return;
 }
 
-void memory_write (char *host, char *inst, char *val)
+static void memory_write (char *host, char *inst, char *val)
 {
        rrd_update_file (host, memory_file, val, ds_def, ds_num);
 }
 
+#if MEMORY_HAVE_READ
 #define BUFSIZE 512
-void memory_submit (long long mem_used, long long mem_buffered,
+static void memory_submit (long long mem_used, long long mem_buffered,
                long long mem_cached, long long mem_free)
 {
        char buf[BUFSIZE];
@@ -80,9 +118,59 @@ void memory_submit (long long mem_used, long long mem_buffered,
 }
 #undef BUFSIZE
 
-void memory_read (void)
+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(KERNEL_LINUX)
        FILE *fh;
        char buffer[1024];
        
@@ -165,6 +253,9 @@ void memory_read (void)
                memory_submit (ios->used, 0LL, ios->cache, ios->free);
 #endif /* HAVE_LIBSTATGRAB */
 }
+#else
+# define memory_read NULL
+#endif /* MEMORY_HAVE_READ */
 
 void module_register (void)
 {
@@ -172,4 +263,3 @@ void module_register (void)
 }
 
 #undef MODULE_NAME
-#endif /* COLLECT_MEMORY */