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 */
60 /* #endif HAVE_SYSCTL */
62 #elif HAVE_SYSCTLBYNAME
63 /* no global variables */
64 /* #endif HAVE_SYSCTLBYNAME */
67 /* no global variables */
68 /* #endif KERNEL_LINUX */
73 /* #endif HAVE_LIBKSTAT */
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 */
91 pagesize = getpagesize ();
94 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
97 /* #endif HAVE_SYSCTL */
99 #elif HAVE_SYSCTLBYNAME
101 /* #endif HAVE_SYSCTLBYNAME */
103 #elif defined(KERNEL_LINUX)
105 /* #endif KERNEL_LINUX */
107 #elif defined(HAVE_LIBKSTAT)
108 /* getpagesize(3C) tells me this does not fail.. */
109 pagesize = getpagesize ();
110 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
115 #endif /* HAVE_LIBKSTAT */
118 } /* int memory_init */
120 static void memory_submit (const char *type_instance, gauge_t value)
123 value_list_t vl = VALUE_LIST_INIT;
125 values[0].gauge = value;
129 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
130 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
131 sstrncpy (vl.type, "memory", sizeof (vl.type));
132 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
134 plugin_dispatch_values (&vl);
137 static int memory_read (void)
139 #if HAVE_HOST_STATISTICS
140 kern_return_t status;
141 vm_statistics_data_t vm_data;
142 mach_msg_type_number_t vm_data_len;
149 if (!port_host || !pagesize)
152 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
153 if ((status = host_statistics (port_host, HOST_VM_INFO,
154 (host_info_t) &vm_data,
155 &vm_data_len)) != KERN_SUCCESS)
157 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
162 * From <http://docs.info.apple.com/article.html?artnum=107918>:
165 * This information can't be cached to disk, so it must stay in RAM.
166 * The amount depends on what applications you are using.
169 * This information is currently in RAM and actively being used.
172 * This information is no longer being used and has been cached to
173 * disk, but it will remain in RAM until another application needs
174 * the space. Leaving this information in RAM is to your advantage if
175 * you (or a client of your computer) come back to it later.
178 * This memory is not being used.
181 wired = vm_data.wire_count * pagesize;
182 active = vm_data.active_count * pagesize;
183 inactive = vm_data.inactive_count * pagesize;
184 free = vm_data.free_count * pagesize;
186 memory_submit ("wired", wired);
187 memory_submit ("active", active);
188 memory_submit ("inactive", inactive);
189 memory_submit ("free", free);
190 /* #endif HAVE_HOST_STATISTICS */
193 int mib[] = {CTL_VM, VM_METER};
194 struct vmtotal vmtotal;
197 memset (&vmtotal, 0, sizeof (vmtotal));
198 size = sizeof (vmtotal);
200 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
202 WARNING ("memory plugin: sysctl failed: %s",
203 sstrerror (errno, errbuf, sizeof (errbuf)));
207 assert (pagesize > 0);
208 memory_submit ("active", vmtotal.t_arm * pagesize);
209 memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
210 memory_submit ("free", vmtotal.t_free * pagesize);
211 /* #endif HAVE_SYSCTL */
213 #elif HAVE_SYSCTLBYNAME
215 * vm.stats.vm.v_page_size: 4096
216 * vm.stats.vm.v_page_count: 246178
217 * vm.stats.vm.v_free_count: 28760
218 * vm.stats.vm.v_wire_count: 37526
219 * vm.stats.vm.v_active_count: 55239
220 * vm.stats.vm.v_inactive_count: 113730
221 * vm.stats.vm.v_cache_count: 10809
223 char *sysctl_keys[8] =
225 "vm.stats.vm.v_page_size",
226 "vm.stats.vm.v_page_count",
227 "vm.stats.vm.v_free_count",
228 "vm.stats.vm.v_wire_count",
229 "vm.stats.vm.v_active_count",
230 "vm.stats.vm.v_inactive_count",
231 "vm.stats.vm.v_cache_count",
234 double sysctl_vals[8];
238 for (i = 0; sysctl_keys[i] != NULL; i++)
241 size_t value_len = sizeof (value);
243 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
246 sysctl_vals[i] = value;
247 DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
251 sysctl_vals[i] = NAN;
253 } /* for (sysctl_keys) */
255 /* multiply all all page counts with the pagesize */
256 for (i = 1; sysctl_keys[i] != NULL; i++)
257 if (!isnan (sysctl_vals[i]))
258 sysctl_vals[i] *= sysctl_vals[0];
260 memory_submit ("free", sysctl_vals[2]);
261 memory_submit ("wired", sysctl_vals[3]);
262 memory_submit ("active", sysctl_vals[4]);
263 memory_submit ("inactive", sysctl_vals[5]);
264 memory_submit ("cache", sysctl_vals[6]);
265 /* #endif HAVE_SYSCTLBYNAME */
274 long long mem_used = 0;
275 long long mem_buffered = 0;
276 long long mem_cached = 0;
277 long long mem_free = 0;
279 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
282 WARNING ("memory: fopen: %s",
283 sstrerror (errno, errbuf, sizeof (errbuf)));
287 while (fgets (buffer, 1024, fh) != NULL)
289 long long *val = NULL;
291 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
293 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
295 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
297 else if (strncasecmp (buffer, "Cached:", 7) == 0)
302 numfields = strsplit (buffer, fields, 8);
307 *val = atoll (fields[1]) * 1024LL;
313 WARNING ("memory: fclose: %s",
314 sstrerror (errno, errbuf, sizeof (errbuf)));
317 if (mem_used >= (mem_free + mem_buffered + mem_cached))
319 mem_used -= mem_free + mem_buffered + mem_cached;
320 memory_submit ("used", mem_used);
321 memory_submit ("buffered", mem_buffered);
322 memory_submit ("cached", mem_cached);
323 memory_submit ("free", mem_free);
325 /* #endif KERNEL_LINUX */
335 mem_used = get_kstat_value (ksp, "pagestotal");
336 mem_free = get_kstat_value (ksp, "pagesfree");
337 mem_lock = get_kstat_value (ksp, "pageslocked");
339 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
341 if (mem_used < (mem_free + mem_lock))
344 mem_used -= mem_free + mem_lock;
345 mem_used *= pagesize; /* If this overflows you have some serious */
346 mem_free *= pagesize; /* memory.. Why not call me up and give me */
347 mem_lock *= pagesize; /* some? ;) */
349 memory_submit ("used", mem_used);
350 memory_submit ("free", mem_free);
351 memory_submit ("locked", mem_lock);
352 /* #endif HAVE_LIBKSTAT */
354 #elif HAVE_LIBSTATGRAB
357 if ((ios = sg_get_mem_stats ()) != NULL)
359 memory_submit ("used", ios->used);
360 memory_submit ("cached", ios->cache);
361 memory_submit ("free", ios->free);
363 #endif /* HAVE_LIBSTATGRAB */
368 void module_register (void)
370 plugin_register_init ("memory", memory_init);
371 plugin_register_read ("memory", memory_read);
372 } /* void module_register */