* collectd - src/memory.c
* Copyright (C) 2005-2008 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
* 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;
#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
#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 */
#elif KERNEL_LINUX
FILE *fh;
char buffer[1024];
-
+
char *fields[8];
int numfields;
/* #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);
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;
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);
/* #endif HAVE_LIBKSTAT */
#elif HAVE_SYSCTL
memory_submit ("cached", ios->cache);
memory_submit ("free", ios->free);
}
-#endif /* HAVE_LIBSTATGRAB */
+/* #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);
+ }
+ 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);
+#endif /* HAVE_PERFSTAT */
return (0);
}