2 * collectd - src/memory.c
3 * Copyright (C) 2005,2006 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_debug.h"
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
38 #ifdef HAVE_MACH_MACH_HOST_H
39 # include <mach/mach_host.h>
41 #ifdef HAVE_MACH_HOST_PRIV_H
42 # include <mach/host_priv.h>
44 #ifdef HAVE_MACH_VM_STATISTICS_H
45 # include <mach/vm_statistics.h>
48 #if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
49 # define MEMORY_HAVE_READ 1
51 # define MEMORY_HAVE_READ 0
54 #define MODULE_NAME "memory"
56 static char *memory_file = "memory.rrd";
58 /* 9223372036854775807 == LLONG_MAX */
59 static char *ds_def[] =
61 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
62 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
63 "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
64 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
67 static int ds_num = 4;
69 /* vm_statistics_data_t */
70 #if defined(HOST_VM_INFO)
71 static mach_port_t port_host;
72 static vm_size_t pagesize;
73 /* #endif HOST_VM_INFO */
75 #elif HAVE_SYSCTLBYNAME
76 /* no global variables */
77 /* #endif HAVE_SYSCTLBYNAME */
80 /* no global variables */
81 /* #endif KERNEL_LINUX */
86 #endif /* HAVE_LIBKSTAT */
88 static void memory_init (void)
90 #if defined(HOST_VM_INFO)
91 port_host = mach_host_self ();
92 host_page_size (port_host, &pagesize);
93 /* #endif HOST_VM_INFO */
95 #elif HAVE_SYSCTLBYNAME
97 /* #endif HAVE_SYSCTLBYNAME */
99 #elif defined(KERNEL_LINUX)
101 /* #endif KERNEL_LINUX */
103 #elif defined(HAVE_LIBKSTAT)
104 /* getpagesize(3C) tells me this does not fail.. */
105 pagesize = getpagesize ();
106 if (get_kstat (&ksp, "unix", 0, "system_pages"))
108 #endif /* HAVE_LIBKSTAT */
113 static void memory_write (char *host, char *inst, char *val)
115 rrd_update_file (host, memory_file, val, ds_def, ds_num);
120 static void memory_submit (long long mem_used, long long mem_buffered,
121 long long mem_cached, long long mem_free)
125 if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
126 (unsigned int) curtime, mem_used, mem_free,
127 mem_buffered, mem_cached) >= BUFSIZE)
130 plugin_submit (MODULE_NAME, "-", buf);
134 static void memory_read (void)
136 #if defined(HOST_VM_INFO)
137 kern_return_t status;
138 vm_statistics_data_t vm_data;
139 mach_msg_type_number_t vm_data_len;
146 if (!port_host || !pagesize)
149 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
150 if ((status = host_statistics (port_host, HOST_VM_INFO,
151 (host_info_t) &vm_data,
152 &vm_data_len)) != KERN_SUCCESS)
154 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
159 * From <http://docs.info.apple.com/article.html?artnum=107918>:
162 * This information can't be cached to disk, so it must stay in RAM.
163 * The amount depends on what applications you are using.
166 * This information is currently in RAM and actively being used.
169 * This information is no longer being used and has been cached to
170 * disk, but it will remain in RAM until another application needs
171 * the space. Leaving this information in RAM is to your advantage if
172 * you (or a client of your computer) come back to it later.
175 * This memory is not being used.
178 wired = vm_data.wire_count * pagesize;
179 active = vm_data.active_count * pagesize;
180 inactive = vm_data.inactive_count * pagesize;
181 free = vm_data.free_count * pagesize;
183 memory_submit (wired + active, -1, inactive, free);
184 /* #endif HOST_VM_INFO */
186 #elif HAVE_SYSCTLBYNAME
188 * vm.stats.vm.v_page_size: 4096
189 * vm.stats.vm.v_page_count: 246178
190 * vm.stats.vm.v_free_count: 28760
191 * vm.stats.vm.v_wire_count: 37526
192 * vm.stats.vm.v_active_count: 55239
193 * vm.stats.vm.v_inactive_count: 113730
194 * vm.stats.vm.v_cache_count: 10809
196 char *sysctl_keys[8] =
198 "vm.stats.vm.v_page_size",
199 "vm.stats.vm.v_page_count",
200 "vm.stats.vm.v_free_count",
201 "vm.stats.vm.v_wire_count",
202 "vm.stats.vm.v_active_count",
203 "vm.stats.vm.v_inactive_count",
204 "vm.stats.vm.v_cache_count",
207 int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
213 for (i = 0; sysctl_keys[i] != NULL; i++)
216 if ((status = sysctlbyname (sysctl_keys[i],
217 (void *) &sysctl_vals[i], &len,
220 syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s",
221 sysctl_keys[i], strerror (errno));
224 DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
227 /* multiply all all page counts with the pagesize */
228 for (i = 1; sysctl_keys[i] != NULL; i++)
229 sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
231 memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
232 sysctl_vals[6], /* cache */
233 sysctl_vals[5], /* inactive */
234 sysctl_vals[2]); /* free */
235 /* #endif HAVE_SYSCTLBYNAME */
237 #elif defined(KERNEL_LINUX)
244 long long mem_used = 0;
245 long long mem_buffered = 0;
246 long long mem_cached = 0;
247 long long mem_free = 0;
249 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
251 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
255 while (fgets (buffer, 1024, fh) != NULL)
257 long long *val = NULL;
259 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
261 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
263 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
265 else if (strncasecmp (buffer, "Cached:", 7) == 0)
270 numfields = strsplit (buffer, fields, 8);
275 *val = atoll (fields[1]) * 1024LL;
279 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
281 if (mem_used >= (mem_free + mem_buffered + mem_cached))
283 mem_used -= mem_free + mem_buffered + mem_cached;
284 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
286 /* #endif defined(KERNEL_LINUX) */
288 #elif defined(HAVE_LIBKSTAT)
296 mem_used = get_kstat_value (ksp, "pagestotal");
297 mem_free = get_kstat_value (ksp, "pagesfree");
298 mem_lock = get_kstat_value (ksp, "pageslocked");
300 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
302 if (mem_used < (mem_free + mem_lock))
305 mem_used -= mem_free + mem_lock;
306 mem_used *= pagesize; /* If this overflows you have some serious */
307 mem_free *= pagesize; /* memory.. Why not call me up and give me */
308 mem_lock *= pagesize; /* some? ;) */
310 memory_submit (mem_used, mem_lock, 0LL, mem_free);
311 /* #endif defined(HAVE_LIBKSTAT) */
313 #elif defined(HAVE_LIBSTATGRAB)
316 if ((ios = sg_get_mem_stats ()) != NULL)
317 memory_submit (ios->used, 0LL, ios->cache, ios->free);
318 #endif /* HAVE_LIBSTATGRAB */
321 # define memory_read NULL
322 #endif /* MEMORY_HAVE_READ */
324 void module_register (void)
326 plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);