X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fmemory.c;h=6a50161017bf22e7d384bc597589ad48f97a1e4e;hb=743dc15efbfebc5a3feefff14dcec03236fb3cb0;hp=3fbd33fb0c1da1a90cb666e1e6df044a711a2f64;hpb=97deb523823b1f04880d584a19bc2b0e7e10d96a;p=collectd.git diff --git a/src/memory.c b/src/memory.c index 3fbd33fb..6a501610 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,6 +1,8 @@ /** * collectd - src/memory.c - * Copyright (C) 2005-2007 Florian octo Forster + * 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 @@ -17,6 +19,8 @@ * * Authors: * Florian octo Forster + * Simon Kuhnle + * Manuel Sanmartin **/ #include "collectd.h" @@ -43,6 +47,15 @@ # include #endif +#if HAVE_STATGRAB_H +# include +#endif + +#if HAVE_PERFSTAT +# include +# include +#endif /* HAVE_PERFSTAT */ + /* vm_statistics_data_t */ #if HAVE_HOST_STATISTICS static mach_port_t port_host; @@ -62,6 +75,17 @@ static int pagesize; static kstat_t *ksp; /* #endif HAVE_LIBKSTAT */ +#elif HAVE_SYSCTL +static int pagesize; +/* #endif HAVE_SYSCTL */ + +#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 @@ -84,10 +108,29 @@ static int memory_init (void) #elif defined(HAVE_LIBKSTAT) /* getpagesize(3C) tells me this does not fail.. */ pagesize = getpagesize (); - if (get_kstat (&ksp, "unix", 0, "system_pages")) + if (get_kstat (&ksp, "unix", 0, "system_pages") != 0) + { ksp = NULL; -#endif /* HAVE_LIBKSTAT */ + return (-1); + } +/* #endif HAVE_LIBKSTAT */ +#elif HAVE_SYSCTL + pagesize = getpagesize (); + if (pagesize <= 0) + { + ERROR ("memory plugin: Invalid pagesize: %i", pagesize); + return (-1); + } +/* #endif HAVE_SYSCTL */ + +#elif HAVE_LIBSTATGRAB +/* no init stuff */ +/* #endif HAVE_LIBSTATGRAB */ + +#elif HAVE_PERFSTAT + pagesize = getpagesize (); +#endif /* HAVE_PERFSTAT */ return (0); } /* int memory_init */ @@ -100,13 +143,12 @@ static void memory_submit (const char *type_instance, gauge_t value) vl.values = values; vl.values_len = 1; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "memory"); - strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - vl.type_instance[sizeof (vl.type_instance) - 1] = '\0'; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "memory", sizeof (vl.plugin)); + sstrncpy (vl.type, "memory", sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - plugin_dispatch_values ("memory", &vl); + plugin_dispatch_values (&vl); } static int memory_read (void) @@ -116,10 +158,10 @@ 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; if (!port_host || !pagesize) return (-1); @@ -153,10 +195,10 @@ 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)); memory_submit ("wired", wired); memory_submit ("active", active); @@ -198,7 +240,7 @@ static int memory_read (void) NULL, 0) == 0) { sysctl_vals[i] = value; - DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]); + DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]); } else { @@ -218,10 +260,10 @@ static int memory_read (void) memory_submit ("cache", sysctl_vals[6]); /* #endif HAVE_SYSCTLBYNAME */ -#elif defined(KERNEL_LINUX) +#elif KERNEL_LINUX FILE *fh; char buffer[1024]; - + char *fields[8]; int numfields; @@ -276,12 +318,20 @@ static int memory_read (void) memory_submit ("cached", mem_cached); memory_submit ("free", mem_free); } -/* #endif defined(KERNEL_LINUX) */ +/* #endif KERNEL_LINUX */ -#elif defined(HAVE_LIBKSTAT) +#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); @@ -289,32 +339,109 @@ 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; memory_submit ("used", mem_used); memory_submit ("free", mem_free); memory_submit ("locked", mem_lock); -/* #endif defined(HAVE_LIBKSTAT) */ + memory_submit ("kernel", mem_kern); + memory_submit ("unusable", mem_unus); +/* #endif HAVE_LIBKSTAT */ + +#elif HAVE_SYSCTL + int mib[] = {CTL_VM, VM_METER}; + struct vmtotal vmtotal; + size_t size; + + memset (&vmtotal, 0, sizeof (vmtotal)); + size = sizeof (vmtotal); + + if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) { + char errbuf[1024]; + WARNING ("memory plugin: sysctl failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } -#elif defined(HAVE_LIBSTATGRAB) + 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); +/* #endif HAVE_SYSCTL */ + +#elif HAVE_LIBSTATGRAB sg_mem_stats *ios; if ((ios = sg_get_mem_stats ()) != NULL) { memory_submit ("used", ios->used); - memory_submit ("cached", ios->cached); + 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); }