memory plugin: Use the "complex" configuration.
[collectd.git] / src / memory.c
index 799a80c..160e1ee 100644 (file)
@@ -1,7 +1,8 @@
 /**
  * collectd - src/memory.c
- * Copyright (C) 2005-2008  Florian octo Forster
+ * Copyright (C) 2005-2014  Florian octo Forster
  * Copyright (C) 2009       Simon Kuhnle
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -19,6 +20,7 @@
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
  *   Simon Kuhnle <simon at blarzwurst.de>
+ *   Manuel Sanmartin
  **/
 
 #include "collectd.h"
 # include <statgrab.h>
 #endif
 
+#if HAVE_PERFSTAT
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif /* HAVE_PERFSTAT */
+
 /* vm_statistics_data_t */
 #if HAVE_HOST_STATISTICS
 static mach_port_t port_host;
@@ -75,11 +82,36 @@ static int pagesize;
 #elif HAVE_LIBSTATGRAB
 /* no global variables */
 /* endif HAVE_LIBSTATGRAB */
-
+#elif HAVE_PERFSTAT
+static int pagesize;
+static perfstat_memory_total_t pmemory;
+/* endif HAVE_PERFSTAT */
 #else
 # error "No applicable input method."
 #endif
 
+static _Bool values_absolute = 1;
+static _Bool values_percentage = 0;
+
+static int memory_config (oconfig_item_t *ci) /* {{{ */
+{
+       int i;
+
+       for (i = 0; i < ci->children_num; i++)
+       {
+               oconfig_item_t *child = ci->children + i;
+               if (strcasecmp ("ValuesAbsolute", child->key) == 0)
+                       cf_util_get_boolean (child, &values_absolute);
+               else if (strcasecmp ("ValuesPercentage", child->key) == 0)
+                       cf_util_get_boolean (child, &values_percentage);
+               else
+                       ERROR ("memory plugin: Invalid configuration option: "
+                                       "\"%s\".", child->key);
+       }
+
+       return (0);
+} /* }}} int memory_config */
+
 static int memory_init (void)
 {
 #if HAVE_HOST_STATISTICS
@@ -116,8 +148,11 @@ static int memory_init (void)
 
 #elif HAVE_LIBSTATGRAB
 /* no init stuff */
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
 
+#elif HAVE_PERFSTAT
+       pagesize = getpagesize ();
+#endif /* HAVE_PERFSTAT */
        return (0);
 } /* int memory_init */
 
@@ -145,10 +180,11 @@ static int memory_read (void)
        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;
+       gauge_t wired;
+       gauge_t active;
+       gauge_t inactive;
+       gauge_t free;
+       gauge_t total;
 
        if (!port_host || !pagesize)
                return (-1);
@@ -182,15 +218,26 @@ static int memory_read (void)
         *   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;
+       wired    = (gauge_t) (((uint64_t) vm_data.wire_count)     * ((uint64_t) pagesize));
+       active   = (gauge_t) (((uint64_t) vm_data.active_count)   * ((uint64_t) pagesize));
+       inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
+       free     = (gauge_t) (((uint64_t) vm_data.free_count)     * ((uint64_t) pagesize));
+       total    = wired + active + inactive + free;
 
-       memory_submit ("wired",    wired);
-       memory_submit ("active",   active);
-       memory_submit ("inactive", inactive);
-       memory_submit ("free",     free);
+       if (values_absolute)
+       {
+               memory_submit ("wired",    wired);
+               memory_submit ("active",   active);
+               memory_submit ("inactive", inactive);
+               memory_submit ("free",     free);
+       }
+       if (values_percentage)
+       {
+               memory_submit ("percent_wired",    (gauge_t) ((float_t) wired) / total * 100);
+               memory_submit ("percent_active",   (gauge_t) ((float_t) active) / total * 100);
+               memory_submit ("percent_inactive", (gauge_t) ((float_t) inactive / total * 100);
+               memory_submit ("percent_free",     (gauge_t) ((float_t) free / total * 100);
+       }
 /* #endif HAVE_HOST_STATISTICS */
 
 #elif HAVE_SYSCTLBYNAME
@@ -240,20 +287,33 @@ static int memory_read (void)
                if (!isnan (sysctl_vals[i]))
                        sysctl_vals[i] *= sysctl_vals[0];
 
-       memory_submit ("free",     sysctl_vals[2]);
-       memory_submit ("wired",    sysctl_vals[3]);
-       memory_submit ("active",   sysctl_vals[4]);
-       memory_submit ("inactive", sysctl_vals[5]);
-       memory_submit ("cache",    sysctl_vals[6]);
+       if (values_absolute)
+       {
+               memory_submit ("free",     sysctl_vals[2]);
+               memory_submit ("wired",    sysctl_vals[3]);
+               memory_submit ("active",   sysctl_vals[4]);
+               memory_submit ("inactive", sysctl_vals[5]);
+               memory_submit ("cache",    sysctl_vals[6]);
+       }
+       if (values_percentage)
+       {
+               double total = sysctl_vals[2] + sysctl_vals[3] + sysctl_vals[4] + sysctl_vals[5] + sysctl_vals[6];
+               memory_submit ("percent_free",     (gauge_t) ((float_t) sysctl_vals[2]) / total * 100);
+               memory_submit ("percent_wired",    (gauge_t) ((float_t) sysctl_vals[3]) / total * 100);
+               memory_submit ("percent_active",   (gauge_t) ((float_t) sysctl_vals[4]) / total * 100);
+               memory_submit ("percent_inactive", (gauge_t) ((float_t) sysctl_vals[5]) / total * 100);
+               memory_submit ("percent_cache",    (gauge_t) ((float_t) sysctl_vals[6]) / total * 100);
+       }
 /* #endif HAVE_SYSCTLBYNAME */
 
 #elif KERNEL_LINUX
        FILE *fh;
        char buffer[1024];
-       
+
        char *fields[8];
        int numfields;
 
+       long long mem_total = 0;
        long long mem_used = 0;
        long long mem_buffered = 0;
        long long mem_cached = 0;
@@ -272,7 +332,7 @@ static int memory_read (void)
                long long *val = NULL;
 
                if (strncasecmp (buffer, "MemTotal:", 9) == 0)
-                       val = &mem_used;
+                       val = &mem_total;
                else if (strncasecmp (buffer, "MemFree:", 8) == 0)
                        val = &mem_free;
                else if (strncasecmp (buffer, "Buffers:", 8) == 0)
@@ -297,20 +357,38 @@ static int memory_read (void)
                                sstrerror (errno, errbuf, sizeof (errbuf)));
        }
 
-       if (mem_used >= (mem_free + mem_buffered + mem_cached))
+       if (mem_total >= (mem_free + mem_buffered + mem_cached))
        {
-               mem_used -= mem_free + mem_buffered + mem_cached;
-               memory_submit ("used",     mem_used);
-               memory_submit ("buffered", mem_buffered);
-               memory_submit ("cached",   mem_cached);
-               memory_submit ("free",     mem_free);
+               mem_used = mem_total - (mem_free + mem_buffered + mem_cached);
+               if (values_absolute)
+               {
+                       memory_submit ("used",     mem_used);
+                       memory_submit ("buffered", mem_buffered);
+                       memory_submit ("cached",   mem_cached);
+                       memory_submit ("free",     mem_free);
+               }
+               if (values_percentage)
+               {
+                       memory_submit ("percent_used",     (gauge_t) ((float_t) mem_used) / mem_total * 100);
+                       memory_submit ("percent_buffered", (gauge_t) ((float_t) mem_buffered) / mem_total * 100);
+                       memory_submit ("percent_cached",   (gauge_t) ((float_t) mem_cached) / mem_total * 100);
+                       memory_submit ("percent_free",     (gauge_t) ((float_t) mem_free) / mem_total * 100);
+               }
        }
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
+        /* Most of the additions here were taken as-is from the k9toolkit from
+         * Brendan Gregg and are subject to change I guess */
        long long mem_used;
        long long mem_free;
        long long mem_lock;
+       long long mem_kern;
+       long long mem_unus;
+
+       long long pp_kernel;
+       long long physmem;
+       long long availrmem;
 
        if (ksp == NULL)
                return (-1);
@@ -318,20 +396,73 @@ static int memory_read (void)
        mem_used = get_kstat_value (ksp, "pagestotal");
        mem_free = get_kstat_value (ksp, "pagesfree");
        mem_lock = get_kstat_value (ksp, "pageslocked");
+       mem_kern = 0;
+       mem_unus = 0;
+
+       pp_kernel = get_kstat_value (ksp, "pp_kernel");
+       physmem = get_kstat_value (ksp, "physmem");
+       availrmem = get_kstat_value (ksp, "availrmem");
 
        if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
+       {
+               WARNING ("memory plugin: one of used, free or locked is negative.");
                return (-1);
+       }
+
+       mem_unus = physmem - mem_used;
+
        if (mem_used < (mem_free + mem_lock))
-               return (-1);
+       {
+               /* source: http://wesunsolve.net/bugid/id/4909199
+                * this seems to happen when swap space is small, e.g. 2G on a 32G system
+                * we will make some assumptions here
+                * educated solaris internals help welcome here */
+               DEBUG ("memory plugin: pages total is smaller than \"free\" "
+                               "+ \"locked\". This is probably due to small "
+                               "swap space");
+               mem_free = availrmem;
+               mem_used = 0;
+       }
+       else
+       {
+               mem_used -= mem_free + mem_lock;
+       }
+
+       /* mem_kern is accounted for in mem_lock */
+       if ( pp_kernel < mem_lock )
+       {
+               mem_kern = pp_kernel;
+               mem_lock -= pp_kernel;
+       }
+       else
+       {
+               mem_kern = mem_lock;
+               mem_lock = 0;
+       }
 
-       mem_used -= mem_free + mem_lock;
        mem_used *= pagesize; /* If this overflows you have some serious */
        mem_free *= pagesize; /* memory.. Why not call me up and give me */
        mem_lock *= pagesize; /* some? ;) */
+       mem_kern *= pagesize; /* it's 2011 RAM is cheap */
+       mem_unus *= pagesize;
+
+       if (values_absolute)
+       {
+               memory_submit ("used",   mem_used);
+               memory_submit ("free",   mem_free);
+               memory_submit ("locked", mem_lock);
+               memory_submit ("kernel", mem_kern);
+               memory_submit ("unusable", mem_unus);
+       }
+       if (values_percentage)
+       {
+               memory_submit ("percent_used",   (gauge_t) ((float_t) mem_used) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
+               memory_submit ("percent_free",   (gauge_t) ((float_t) mem_free) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
+               memory_submit ("percent_locked", (gauge_t) ((float_t) mem_lock) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
+               memory_submit ("percent_kernel", (gauge_t) ((float_t) mem_kern) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
+               memory_submit ("percent_unusable", (gauge_t) ((float_t) mem_unus) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
 
-       memory_submit ("used",   mem_used);
-       memory_submit ("free",   mem_free);
-       memory_submit ("locked", mem_lock);
+       }
 /* #endif HAVE_LIBKSTAT */
 
 #elif HAVE_SYSCTL
@@ -350,9 +481,18 @@ static int memory_read (void)
        }
 
        assert (pagesize > 0);
-       memory_submit ("active",   vmtotal.t_arm * pagesize);
-       memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
-       memory_submit ("free",     vmtotal.t_free * pagesize);
+       if (values_absolute)
+       {
+               memory_submit ("active",   vmtotal.t_arm * pagesize);
+               memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
+               memory_submit ("free",     vmtotal.t_free * pagesize);
+       }
+       if (values_percentage)
+       {
+               memory_submit ("percent_active",   (gauge_t) ((float_t) vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
+               memory_submit ("percent_inactive", (gauge_t) ((float_t) (vmtotal.t_rm - vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
+               memory_submit ("percent_free",     (gauge_t) ((float_t) vmtotal.t_free) / (vmtotal.t_rm + vmtotal.t_free) * 100);
+       }
 /* #endif HAVE_SYSCTL */
 
 #elif HAVE_LIBSTATGRAB
@@ -360,17 +500,53 @@ static int memory_read (void)
 
        if ((ios = sg_get_mem_stats ()) != NULL)
        {
-               memory_submit ("used",   ios->used);
-               memory_submit ("cached", ios->cache);
-               memory_submit ("free",   ios->free);
+               if (values_absolute)
+               {
+                       memory_submit ("used",   ios->used);
+                       memory_submit ("cached", ios->cache);
+                       memory_submit ("free",   ios->free);
+               }
+               if (values_percentage)
+               {
+                       memory_submit ("percent_used",   (gauge_t) ((float_t) ios->used) / (ios->used + ios->cache + ios->free) * 100);
+                       memory_submit ("percent_cached", (gauge_t) ((float_t) ios->cache) / (ios->used + ios->cache + ios->free) * 100);
+                       memory_submit ("percent_free",   (gauge_t) ((float_t) ios->free) / (ios->used + ios->cache + ios->free) * 100);
+               }
+       }
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif HAVE_PERFSTAT
+       if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("memory plugin: perfstat_memory_total failed: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+       if (values_absolute)
+       {
+               memory_submit ("used",   pmemory.real_inuse * pagesize);
+               memory_submit ("free",   pmemory.real_free * pagesize);
+               memory_submit ("cached", pmemory.numperm * pagesize);
+               memory_submit ("system", pmemory.real_system * pagesize);
+               memory_submit ("user",   pmemory.real_process * pagesize);
+       }
+       if (values_percentage)
+       {
+               memory_submit ("percent_used",   (gauge_t) ((float_t) pmemory.real_inuse) / pmemory.real_total * 100);
+               memory_submit ("percent_free",   (gauge_t) ((float_t) pmemory.real_free) / pmemory.real_total * 100);
+               memory_submit ("percent_cached", (gauge_t) ((float_t) pmemory.numperm) / pmemory.real_total * 100);
+               memory_submit ("percent_system", (gauge_t) ((float_t) pmemory.real_system) / pmemory.real_total * 100);
+               memory_submit ("percent_user",   (gauge_t) ((float_t) pmemory.real_process) / pmemory.real_total * 100);
        }
-#endif /* HAVE_LIBSTATGRAB */
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 }
 
 void module_register (void)
 {
+       plugin_register_complex_config ("memory", memory_config);
        plugin_register_init ("memory", memory_init);
        plugin_register_read ("memory", memory_read);
 } /* void module_register */