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>
27 #ifdef HAVE_SYS_SYSCTL_H
28 # include <sys/sysctl.h>
31 #ifdef HAVE_MACH_KERN_RETURN_H
32 # include <mach/kern_return.h>
34 #ifdef HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
37 #ifdef HAVE_MACH_MACH_HOST_H
38 # include <mach/mach_host.h>
40 #ifdef HAVE_MACH_HOST_PRIV_H
41 # include <mach/host_priv.h>
43 #ifdef MACH_VM_STATISTICS_H
44 # include <mach/vm_statistics.h>
47 #if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
48 # define MEMORY_HAVE_READ 1
50 # define MEMORY_HAVE_READ 0
53 #define MODULE_NAME "memory"
55 static char *memory_file = "memory.rrd";
57 /* 9223372036854775807 == LLONG_MAX */
58 static char *ds_def[] =
60 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
61 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
62 "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
63 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
66 static int ds_num = 4;
68 /* vm_statistics_data_t */
69 #if defined(HOST_VM_INFO)
70 static mach_port_t port_host;
71 static vm_size_t pagesize;
72 /* #endif HOST_VM_INFO */
74 #elif HAVE_SYSCTLBYNAME
75 /* no global variables */
76 /* #endif HAVE_SYSCTLBYNAME */
79 /* no global variables */
80 /* #endif KERNEL_LINUX */
85 #endif /* HAVE_LIBKSTAT */
87 static void memory_init (void)
89 #if defined(HOST_VM_INFO)
90 port_host = mach_host_self ();
91 host_page_size (port_host, &pagesize);
92 /* #endif HOST_VM_INFO */
94 #elif HAVE_SYSCTLBYNAME
96 /* #endif HAVE_SYSCTLBYNAME */
98 #elif defined(KERNEL_LINUX)
100 /* #endif KERNEL_LINUX */
102 #elif defined(HAVE_LIBKSTAT)
103 /* getpagesize(3C) tells me this does not fail.. */
104 pagesize = getpagesize ();
105 if (get_kstat (&ksp, "unix", 0, "system_pages"))
107 #endif /* HAVE_LIBKSTAT */
112 static void memory_write (char *host, char *inst, char *val)
114 rrd_update_file (host, memory_file, val, ds_def, ds_num);
119 static void memory_submit (long long mem_used, long long mem_buffered,
120 long long mem_cached, long long mem_free)
124 if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
125 (unsigned int) curtime, mem_used, mem_free,
126 mem_buffered, mem_cached) >= BUFSIZE)
129 plugin_submit (MODULE_NAME, "-", buf);
133 static void memory_read (void)
135 #if defined(HOST_VM_INFO)
136 kern_return_t status;
137 vm_statistics_data_t vm_data;
138 mach_msg_type_number_t vm_data_len;
145 if (!port_host || !pagesize)
148 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
149 if ((status = host_statistics (port_host, HOST_VM_INFO,
150 (host_info_t) &vm_data,
151 &vm_data_len)) != KERN_SUCCESS)
153 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
158 * From <http://docs.info.apple.com/article.html?artnum=107918>:
161 * This information can't be cached to disk, so it must stay in RAM.
162 * The amount depends on what applications you are using.
165 * This information is currently in RAM and actively being used.
168 * This information is no longer being used and has been cached to
169 * disk, but it will remain in RAM until another application needs
170 * the space. Leaving this information in RAM is to your advantage if
171 * you (or a client of your computer) come back to it later.
174 * This memory is not being used.
177 wired = vm_data.wire_count * pagesize;
178 active = vm_data.active_count * pagesize;
179 inactive = vm_data.inactive_count * pagesize;
180 free = vm_data.free_count * pagesize;
182 memory_submit (wired + active, -1, inactive, free);
183 /* #endif HOST_VM_INFO */
185 #elif HAVE_SYSCTLBYNAME
187 * vm.stats.vm.v_page_size: 4096
188 * vm.stats.vm.v_page_count: 246178
189 * vm.stats.vm.v_free_count: 28760
190 * vm.stats.vm.v_wire_count: 37526
191 * vm.stats.vm.v_active_count: 55239
192 * vm.stats.vm.v_inactive_count: 113730
193 * vm.stats.vm.v_cache_count: 10809
195 char *sysctl_keys[8] =
197 "vm.stats.vm.v_page_size",
198 "vm.stats.vm.v_page_count",
199 "vm.stats.vm.v_free_count",
200 "vm.stats.vm.v_wire_count",
201 "vm.stats.vm.v_active_count",
202 "vm.stats.vm.v_inactive_count",
203 "vm.stats.vm.v_cache_count",
206 int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
212 for (i = 0; sysctl_keys[i] != NULL; i++)
215 if ((status = sysctlbyname (sysctl_keys[i],
216 (void *) &sysctl_vals[i], &len,
219 syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s",
220 sysctl_keys[i], strerror (status));
223 DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
226 /* multiply all all page counts with the pagesize */
227 for (i = 1; sysctl_keys[i] != NULL; i++)
228 sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
230 memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
231 sysctl_vals[6], /* cache */
232 sysctl_vals[5], /* inactive */
233 sysctl_vals[2]); /* free */
234 /* #endif HAVE_SYSCTLBYNAME */
236 #elif defined(KERNEL_LINUX)
243 long long mem_used = 0;
244 long long mem_buffered = 0;
245 long long mem_cached = 0;
246 long long mem_free = 0;
248 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
250 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
254 while (fgets (buffer, 1024, fh) != NULL)
256 long long *val = NULL;
258 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
260 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
262 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
264 else if (strncasecmp (buffer, "Cached:", 7) == 0)
269 numfields = strsplit (buffer, fields, 8);
274 *val = atoll (fields[1]) * 1024LL;
278 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
280 if (mem_used >= (mem_free + mem_buffered + mem_cached))
282 mem_used -= mem_free + mem_buffered + mem_cached;
283 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
285 /* #endif defined(KERNEL_LINUX) */
287 #elif defined(HAVE_LIBKSTAT)
295 mem_used = get_kstat_value (ksp, "pagestotal");
296 mem_free = get_kstat_value (ksp, "pagesfree");
297 mem_lock = get_kstat_value (ksp, "pageslocked");
299 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
301 if (mem_used < (mem_free + mem_lock))
304 mem_used -= mem_free + mem_lock;
305 mem_used *= pagesize; /* If this overflows you have some serious */
306 mem_free *= pagesize; /* memory.. Why not call me up and give me */
307 mem_lock *= pagesize; /* some? ;) */
309 memory_submit (mem_used, mem_lock, 0LL, mem_free);
310 /* #endif defined(HAVE_LIBKSTAT) */
312 #elif defined(HAVE_LIBSTATGRAB)
315 if ((ios = sg_get_mem_stats ()) != NULL)
316 memory_submit (ios->used, 0LL, ios->cache, ios->free);
317 #endif /* HAVE_LIBSTATGRAB */
320 # define memory_read NULL
321 #endif /* MEMORY_HAVE_READ */
323 void module_register (void)
325 plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);