2 * collectd - src/memory.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2009 Simon Kuhnle
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
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>
21 * Simon Kuhnle <simon at blarzwurst.de>
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>
49 # include <statgrab.h>
52 /* vm_statistics_data_t */
53 #if HAVE_HOST_STATISTICS
54 static mach_port_t port_host;
55 static vm_size_t pagesize;
56 /* #endif HAVE_HOST_STATISTICS */
58 #elif HAVE_SYSCTLBYNAME
59 /* no global variables */
60 /* #endif HAVE_SYSCTLBYNAME */
63 /* no global variables */
64 /* #endif KERNEL_LINUX */
69 /* #endif HAVE_LIBKSTAT */
73 /* #endif HAVE_SYSCTL */
75 #elif HAVE_LIBSTATGRAB
76 /* no global variables */
77 /* endif HAVE_LIBSTATGRAB */
80 # error "No applicable input method."
83 static int memory_init (void)
85 #if HAVE_HOST_STATISTICS
86 port_host = mach_host_self ();
87 host_page_size (port_host, &pagesize);
88 /* #endif HAVE_HOST_STATISTICS */
90 #elif HAVE_SYSCTLBYNAME
92 /* #endif HAVE_SYSCTLBYNAME */
94 #elif defined(KERNEL_LINUX)
96 /* #endif KERNEL_LINUX */
98 #elif defined(HAVE_LIBKSTAT)
99 /* getpagesize(3C) tells me this does not fail.. */
100 pagesize = getpagesize ();
101 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
106 /* #endif HAVE_LIBKSTAT */
109 pagesize = getpagesize ();
112 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
115 /* #endif HAVE_SYSCTL */
117 #elif HAVE_LIBSTATGRAB
119 #endif /* HAVE_LIBSTATGRAB */
122 } /* int memory_init */
124 static void memory_submit (const char *type_instance, gauge_t value)
127 value_list_t vl = VALUE_LIST_INIT;
129 values[0].gauge = value;
133 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
134 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
135 sstrncpy (vl.type, "memory", sizeof (vl.type));
136 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
138 plugin_dispatch_values (&vl);
141 static int memory_read (void)
143 #if HAVE_HOST_STATISTICS
144 kern_return_t status;
145 vm_statistics_data_t vm_data;
146 mach_msg_type_number_t vm_data_len;
153 if (!port_host || !pagesize)
156 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
157 if ((status = host_statistics (port_host, HOST_VM_INFO,
158 (host_info_t) &vm_data,
159 &vm_data_len)) != KERN_SUCCESS)
161 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
166 * From <http://docs.info.apple.com/article.html?artnum=107918>:
169 * This information can't be cached to disk, so it must stay in RAM.
170 * The amount depends on what applications you are using.
173 * This information is currently in RAM and actively being used.
176 * This information is no longer being used and has been cached to
177 * disk, but it will remain in RAM until another application needs
178 * the space. Leaving this information in RAM is to your advantage if
179 * you (or a client of your computer) come back to it later.
182 * This memory is not being used.
185 wired = vm_data.wire_count * pagesize;
186 active = vm_data.active_count * pagesize;
187 inactive = vm_data.inactive_count * pagesize;
188 free = vm_data.free_count * pagesize;
190 memory_submit ("wired", wired);
191 memory_submit ("active", active);
192 memory_submit ("inactive", inactive);
193 memory_submit ("free", free);
194 /* #endif HAVE_HOST_STATISTICS */
196 #elif HAVE_SYSCTLBYNAME
198 * vm.stats.vm.v_page_size: 4096
199 * vm.stats.vm.v_page_count: 246178
200 * vm.stats.vm.v_free_count: 28760
201 * vm.stats.vm.v_wire_count: 37526
202 * vm.stats.vm.v_active_count: 55239
203 * vm.stats.vm.v_inactive_count: 113730
204 * vm.stats.vm.v_cache_count: 10809
206 char *sysctl_keys[8] =
208 "vm.stats.vm.v_page_size",
209 "vm.stats.vm.v_page_count",
210 "vm.stats.vm.v_free_count",
211 "vm.stats.vm.v_wire_count",
212 "vm.stats.vm.v_active_count",
213 "vm.stats.vm.v_inactive_count",
214 "vm.stats.vm.v_cache_count",
217 double sysctl_vals[8];
221 for (i = 0; sysctl_keys[i] != NULL; i++)
224 size_t value_len = sizeof (value);
226 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
229 sysctl_vals[i] = value;
230 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
234 sysctl_vals[i] = NAN;
236 } /* for (sysctl_keys) */
238 /* multiply all all page counts with the pagesize */
239 for (i = 1; sysctl_keys[i] != NULL; i++)
240 if (!isnan (sysctl_vals[i]))
241 sysctl_vals[i] *= sysctl_vals[0];
243 memory_submit ("free", sysctl_vals[2]);
244 memory_submit ("wired", sysctl_vals[3]);
245 memory_submit ("active", sysctl_vals[4]);
246 memory_submit ("inactive", sysctl_vals[5]);
247 memory_submit ("cache", sysctl_vals[6]);
248 /* #endif HAVE_SYSCTLBYNAME */
257 long long mem_used = 0;
258 long long mem_buffered = 0;
259 long long mem_cached = 0;
260 long long mem_free = 0;
262 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
265 WARNING ("memory: fopen: %s",
266 sstrerror (errno, errbuf, sizeof (errbuf)));
270 while (fgets (buffer, 1024, fh) != NULL)
272 long long *val = NULL;
274 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
276 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
278 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
280 else if (strncasecmp (buffer, "Cached:", 7) == 0)
285 numfields = strsplit (buffer, fields, 8);
290 *val = atoll (fields[1]) * 1024LL;
296 WARNING ("memory: fclose: %s",
297 sstrerror (errno, errbuf, sizeof (errbuf)));
300 if (mem_used >= (mem_free + mem_buffered + mem_cached))
302 mem_used -= mem_free + mem_buffered + mem_cached;
303 memory_submit ("used", mem_used);
304 memory_submit ("buffered", mem_buffered);
305 memory_submit ("cached", mem_cached);
306 memory_submit ("free", mem_free);
308 /* #endif KERNEL_LINUX */
318 mem_used = get_kstat_value (ksp, "pagestotal");
319 mem_free = get_kstat_value (ksp, "pagesfree");
320 mem_lock = get_kstat_value (ksp, "pageslocked");
322 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
324 if (mem_used < (mem_free + mem_lock))
327 mem_used -= mem_free + mem_lock;
328 mem_used *= pagesize; /* If this overflows you have some serious */
329 mem_free *= pagesize; /* memory.. Why not call me up and give me */
330 mem_lock *= pagesize; /* some? ;) */
332 memory_submit ("used", mem_used);
333 memory_submit ("free", mem_free);
334 memory_submit ("locked", mem_lock);
335 /* #endif HAVE_LIBKSTAT */
338 int mib[] = {CTL_VM, VM_METER};
339 struct vmtotal vmtotal;
342 memset (&vmtotal, 0, sizeof (vmtotal));
343 size = sizeof (vmtotal);
345 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
347 WARNING ("memory plugin: sysctl failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
352 assert (pagesize > 0);
353 memory_submit ("active", vmtotal.t_arm * pagesize);
354 memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
355 memory_submit ("free", vmtotal.t_free * pagesize);
356 /* #endif HAVE_SYSCTL */
358 #elif HAVE_LIBSTATGRAB
361 if ((ios = sg_get_mem_stats ()) != NULL)
363 memory_submit ("used", ios->used);
364 memory_submit ("cached", ios->cache);
365 memory_submit ("free", ios->free);
367 #endif /* HAVE_LIBSTATGRAB */
372 void module_register (void)
374 plugin_register_init ("memory", memory_init);
375 plugin_register_read ("memory", memory_read);
376 } /* void module_register */