2 * collectd - src/memory.c
3 * Copyright (C) 2005-2007 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; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_debug.h"
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 HAVE_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 /* 2^48 = 281474976710656 */
54 static data_source_t dsrc[4] =
56 {"used", DS_TYPE_GAUGE, 0, 281474976710656.0},
57 {"free", DS_TYPE_GAUGE, 0, 281474976710656.0},
58 {"buffers", DS_TYPE_GAUGE, 0, 281474976710656.0},
59 {"cached", DS_TYPE_GAUGE, 0, 281474976710656.0}
62 static data_set_t ds =
67 /* vm_statistics_data_t */
68 #if defined(HOST_VM_INFO)
69 static mach_port_t port_host;
70 static vm_size_t pagesize;
71 /* #endif HOST_VM_INFO */
73 #elif HAVE_SYSCTLBYNAME
74 /* no global variables */
75 /* #endif HAVE_SYSCTLBYNAME */
78 /* no global variables */
79 /* #endif KERNEL_LINUX */
84 #endif /* HAVE_LIBKSTAT */
87 static int 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 */
110 } /* int memory_init */
112 static void memory_submit (long long mem_used, long long mem_buffered,
113 long long mem_cached, long long mem_free)
116 value_list_t vl = VALUE_LIST_INIT;
118 values[0].gauge = mem_used;
119 values[1].gauge = mem_free;
120 values[2].gauge = mem_buffered;
121 values[3].gauge = mem_cached;
125 vl.time = time (NULL);
126 strcpy (vl.host, hostname);
127 strcpy (vl.plugin, "memory");
129 plugin_dispatch_values ("memory", &vl);
132 static int memory_read (void)
134 #if defined(HOST_VM_INFO)
135 kern_return_t status;
136 vm_statistics_data_t vm_data;
137 mach_msg_type_number_t vm_data_len;
144 if (!port_host || !pagesize)
147 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
148 if ((status = host_statistics (port_host, HOST_VM_INFO,
149 (host_info_t) &vm_data,
150 &vm_data_len)) != KERN_SUCCESS)
152 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
157 * From <http://docs.info.apple.com/article.html?artnum=107918>:
160 * This information can't be cached to disk, so it must stay in RAM.
161 * The amount depends on what applications you are using.
164 * This information is currently in RAM and actively being used.
167 * This information is no longer being used and has been cached to
168 * disk, but it will remain in RAM until another application needs
169 * the space. Leaving this information in RAM is to your advantage if
170 * you (or a client of your computer) come back to it later.
173 * This memory is not being used.
176 wired = vm_data.wire_count * pagesize;
177 active = vm_data.active_count * pagesize;
178 inactive = vm_data.inactive_count * pagesize;
179 free = vm_data.free_count * pagesize;
181 memory_submit (wired + active, -1, inactive, free);
182 /* #endif HOST_VM_INFO */
184 #elif HAVE_SYSCTLBYNAME
186 * vm.stats.vm.v_page_size: 4096
187 * vm.stats.vm.v_page_count: 246178
188 * vm.stats.vm.v_free_count: 28760
189 * vm.stats.vm.v_wire_count: 37526
190 * vm.stats.vm.v_active_count: 55239
191 * vm.stats.vm.v_inactive_count: 113730
192 * vm.stats.vm.v_cache_count: 10809
194 char *sysctl_keys[8] =
196 "vm.stats.vm.v_page_size",
197 "vm.stats.vm.v_page_count",
198 "vm.stats.vm.v_free_count",
199 "vm.stats.vm.v_wire_count",
200 "vm.stats.vm.v_active_count",
201 "vm.stats.vm.v_inactive_count",
202 "vm.stats.vm.v_cache_count",
205 int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
211 for (i = 0; sysctl_keys[i] != NULL; i++)
214 if ((status = sysctlbyname (sysctl_keys[i],
215 (void *) &sysctl_vals[i], &len,
218 syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s",
219 sysctl_keys[i], strerror (errno));
222 DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
225 /* multiply all all page counts with the pagesize */
226 for (i = 1; sysctl_keys[i] != NULL; i++)
227 sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
229 memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
230 sysctl_vals[6], /* cache */
231 sysctl_vals[5], /* inactive */
232 sysctl_vals[2]); /* free */
233 /* #endif HAVE_SYSCTLBYNAME */
235 #elif defined(KERNEL_LINUX)
242 long long mem_used = 0;
243 long long mem_buffered = 0;
244 long long mem_cached = 0;
245 long long mem_free = 0;
247 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
249 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
253 while (fgets (buffer, 1024, fh) != NULL)
255 long long *val = NULL;
257 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
259 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
261 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
263 else if (strncasecmp (buffer, "Cached:", 7) == 0)
268 numfields = strsplit (buffer, fields, 8);
273 *val = atoll (fields[1]) * 1024LL;
277 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
279 if (mem_used >= (mem_free + mem_buffered + mem_cached))
281 mem_used -= mem_free + mem_buffered + mem_cached;
282 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
284 /* #endif defined(KERNEL_LINUX) */
286 #elif defined(HAVE_LIBKSTAT)
294 mem_used = get_kstat_value (ksp, "pagestotal");
295 mem_free = get_kstat_value (ksp, "pagesfree");
296 mem_lock = get_kstat_value (ksp, "pageslocked");
298 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
300 if (mem_used < (mem_free + mem_lock))
303 mem_used -= mem_free + mem_lock;
304 mem_used *= pagesize; /* If this overflows you have some serious */
305 mem_free *= pagesize; /* memory.. Why not call me up and give me */
306 mem_lock *= pagesize; /* some? ;) */
308 memory_submit (mem_used, mem_lock, 0LL, mem_free);
309 /* #endif defined(HAVE_LIBKSTAT) */
311 #elif defined(HAVE_LIBSTATGRAB)
314 if ((ios = sg_get_mem_stats ()) != NULL)
315 memory_submit (ios->used, 0LL, ios->cache, ios->free);
316 #endif /* HAVE_LIBSTATGRAB */
320 #endif /* MEMORY_HAVE_READ */
322 void module_register (void)
324 plugin_register_data_set (&ds);
327 plugin_register_init ("memory", memory_init);
328 plugin_register_read ("memory", memory_read);
329 #endif /* MEMORY_HAVE_READ */