X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmemory.c;h=c1564c86caf1840465ef5cecc14487d2e63a5eee;hb=92445ff3363441d0f515de4a3ab92a504cfc0366;hp=13ee950115e99b4c3ca21245fc08dfb26ae85c16;hpb=8d9b00298d5dd8c6e4f3315aa2a0ae4d3684f55d;p=collectd.git diff --git a/src/memory.c b/src/memory.c index 13ee9501..c1564c86 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,11 +1,10 @@ /** * collectd - src/memory.c - * Copyright (C) 2005 Florian octo Forster + * Copyright (C) 2005-2007 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 - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -24,68 +23,210 @@ #include "common.h" #include "plugin.h" -#if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) -# define MEMORY_HAVE_READ 1 -#else -# define MEMORY_HAVE_READ 0 +#ifdef HAVE_SYS_SYSCTL_H +# include +#endif + +#ifdef HAVE_MACH_KERN_RETURN_H +# include +#endif +#ifdef HAVE_MACH_MACH_INIT_H +# include +#endif +#ifdef HAVE_MACH_MACH_HOST_H +# include +#endif +#ifdef HAVE_MACH_HOST_PRIV_H +# include +#endif +#ifdef HAVE_MACH_VM_STATISTICS_H +# include #endif -#define MODULE_NAME "memory" +#if HAVE_STATGRAB_H +# include +#endif -static char *memory_file = "memory.rrd"; +/* vm_statistics_data_t */ +#if HAVE_HOST_STATISTICS +static mach_port_t port_host; +static vm_size_t pagesize; +/* #endif HAVE_HOST_STATISTICS */ -/* 9223372036854775807 == LLONG_MAX */ -static char *ds_def[] = -{ - "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 +#elif HAVE_SYSCTLBYNAME +/* no global variables */ +/* #endif HAVE_SYSCTLBYNAME */ + +#elif KERNEL_LINUX +/* no global variables */ +/* #endif KERNEL_LINUX */ + +#elif HAVE_LIBKSTAT static int pagesize; static kstat_t *ksp; -#endif /* HAVE_LIBKSTAT */ +/* #endif HAVE_LIBKSTAT */ + +#elif HAVE_LIBSTATGRAB +/* no global variables */ +/* endif HAVE_LIBSTATGRAB */ -static void memory_init (void) +#else +# error "No applicable input method." +#endif + +static int memory_init (void) { -#ifdef HAVE_LIBKSTAT +#if HAVE_HOST_STATISTICS + port_host = mach_host_self (); + host_page_size (port_host, &pagesize); +/* #endif HAVE_HOST_STATISTICS */ + +#elif HAVE_SYSCTLBYNAME +/* no init stuff */ +/* #endif HAVE_SYSCTLBYNAME */ + +#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")) ksp = NULL; #endif /* HAVE_LIBKSTAT */ - return; -} + return (0); +} /* int memory_init */ -static void memory_write (char *host, char *inst, char *val) +static void memory_submit (const char *type_instance, gauge_t value) { - rrd_update_file (host, memory_file, val, ds_def, ds_num); -} + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; -#if MEMORY_HAVE_READ -#define BUFSIZE 512 -static void memory_submit (long long mem_used, long long mem_buffered, - long long mem_cached, long long mem_free) -{ - char buf[BUFSIZE]; + values[0].gauge = value; - if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli", - (unsigned int) curtime, mem_used, mem_free, - mem_buffered, mem_cached) >= BUFSIZE) - return; + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + 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_submit (MODULE_NAME, "-", buf); + plugin_dispatch_values (&vl); } -#undef BUFSIZE -static void memory_read (void) +static int memory_read (void) { -#ifdef KERNEL_LINUX +#if HAVE_HOST_STATISTICS + 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 (-1); + + 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) + { + ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status); + return (-1); + } + + /* + * From : + * + * 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", wired); + memory_submit ("active", active); + memory_submit ("inactive", inactive); + memory_submit ("free", free); +/* #endif HAVE_HOST_STATISTICS */ + +#elif HAVE_SYSCTLBYNAME + /* + * 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 + */ + char *sysctl_keys[8] = + { + "vm.stats.vm.v_page_size", + "vm.stats.vm.v_page_count", + "vm.stats.vm.v_free_count", + "vm.stats.vm.v_wire_count", + "vm.stats.vm.v_active_count", + "vm.stats.vm.v_inactive_count", + "vm.stats.vm.v_cache_count", + NULL + }; + double sysctl_vals[8]; + + int i; + + for (i = 0; sysctl_keys[i] != NULL; i++) + { + int value; + size_t value_len = sizeof (value); + + if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len, + NULL, 0) == 0) + { + sysctl_vals[i] = value; + DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]); + } + else + { + sysctl_vals[i] = NAN; + } + } /* for (sysctl_keys) */ + + /* multiply all all page counts with the pagesize */ + for (i = 1; sysctl_keys[i] != NULL; i++) + 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]); +/* #endif HAVE_SYSCTLBYNAME */ + +#elif KERNEL_LINUX FILE *fh; char buffer[1024]; @@ -99,8 +240,10 @@ static void memory_read (void) if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { - syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno)); - return; + char errbuf[1024]; + WARNING ("memory: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); } while (fgets (buffer, 1024, fh) != NULL) @@ -127,54 +270,65 @@ static void memory_read (void) } if (fclose (fh)) - syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("memory: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } if (mem_used >= (mem_free + mem_buffered + mem_cached)) { mem_used -= mem_free + mem_buffered + mem_cached; - memory_submit (mem_used, mem_buffered, mem_cached, mem_free); + memory_submit ("used", mem_used); + memory_submit ("buffered", mem_buffered); + memory_submit ("cached", mem_cached); + memory_submit ("free", mem_free); } -/* #endif defined(KERNEL_LINUX) */ +/* #endif KERNEL_LINUX */ -#elif defined(HAVE_LIBKSTAT) +#elif HAVE_LIBKSTAT long long mem_used; long long mem_free; long long mem_lock; if (ksp == NULL) - return; + return (-1); mem_used = get_kstat_value (ksp, "pagestotal"); mem_free = get_kstat_value (ksp, "pagesfree"); mem_lock = get_kstat_value (ksp, "pageslocked"); if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL)) - return; + return (-1); if (mem_used < (mem_free + mem_lock)) - return; + return (-1); 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? ;) */ - memory_submit (mem_used, mem_lock, 0LL, mem_free); -/* #endif defined(HAVE_LIBKSTAT) */ + memory_submit ("used", mem_used); + memory_submit ("free", mem_free); + memory_submit ("locked", mem_lock); +/* #endif HAVE_LIBKSTAT */ -#elif defined(HAVE_LIBSTATGRAB) +#elif HAVE_LIBSTATGRAB sg_mem_stats *ios; if ((ios = sg_get_mem_stats ()) != NULL) - memory_submit (ios->used, 0LL, ios->cache, ios->free); + { + memory_submit ("used", ios->used); + memory_submit ("cached", ios->cached); + memory_submit ("free", ios->free); + } #endif /* HAVE_LIBSTATGRAB */ + + return (0); } -#else -# define memory_read NULL -#endif /* MEMORY_HAVE_READ */ void module_register (void) { - plugin_register (MODULE_NAME, memory_init, memory_read, memory_write); -} - -#undef MODULE_NAME + plugin_register_init ("memory", memory_init); + plugin_register_read ("memory", memory_read); +} /* void module_register */