2 * collectd - src/memory.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2009 Simon Kuhnle
5 * Copyright (C) 2009 Manuel Sanmartin
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Simon Kuhnle <simon at blarzwurst.de>
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
40 #ifdef HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
43 #ifdef HAVE_MACH_HOST_PRIV_H
44 # include <mach/host_priv.h>
46 #ifdef HAVE_MACH_VM_STATISTICS_H
47 # include <mach/vm_statistics.h>
51 # include <statgrab.h>
55 # include <sys/protosw.h>
56 # include <libperfstat.h>
57 #endif /* HAVE_PERFSTAT */
59 /* vm_statistics_data_t */
60 #if HAVE_HOST_STATISTICS
61 static mach_port_t port_host;
62 static vm_size_t pagesize;
63 /* #endif HAVE_HOST_STATISTICS */
65 #elif HAVE_SYSCTLBYNAME
66 /* no global variables */
67 /* #endif HAVE_SYSCTLBYNAME */
70 /* no global variables */
71 /* #endif KERNEL_LINUX */
76 /* #endif HAVE_LIBKSTAT */
80 /* #endif HAVE_SYSCTL */
82 #elif HAVE_LIBSTATGRAB
83 /* no global variables */
84 /* endif HAVE_LIBSTATGRAB */
87 static perfstat_memory_total_t pmemory;
88 /* endif HAVE_PERFSTAT */
90 # error "No applicable input method."
93 static _Bool values_absolute = 1;
94 static _Bool values_percentage = 0;
96 static int memory_config (oconfig_item_t *ci) /* {{{ */
100 for (i = 0; i < ci->children_num; i++)
102 oconfig_item_t *child = ci->children + i;
103 if (strcasecmp ("ValuesAbsolute", child->key) == 0)
104 cf_util_get_boolean (child, &values_absolute);
105 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
106 cf_util_get_boolean (child, &values_percentage);
108 ERROR ("memory plugin: Invalid configuration option: "
109 "\"%s\".", child->key);
113 } /* }}} int memory_config */
115 static int memory_init (void)
117 #if HAVE_HOST_STATISTICS
118 port_host = mach_host_self ();
119 host_page_size (port_host, &pagesize);
120 /* #endif HAVE_HOST_STATISTICS */
122 #elif HAVE_SYSCTLBYNAME
124 /* #endif HAVE_SYSCTLBYNAME */
126 #elif defined(KERNEL_LINUX)
128 /* #endif KERNEL_LINUX */
130 #elif defined(HAVE_LIBKSTAT)
131 /* getpagesize(3C) tells me this does not fail.. */
132 pagesize = getpagesize ();
133 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
138 /* #endif HAVE_LIBKSTAT */
141 pagesize = getpagesize ();
144 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
147 /* #endif HAVE_SYSCTL */
149 #elif HAVE_LIBSTATGRAB
151 /* #endif HAVE_LIBSTATGRAB */
154 pagesize = getpagesize ();
155 #endif /* HAVE_PERFSTAT */
157 } /* int memory_init */
159 static void memory_submit (const char *type_instance, gauge_t value)
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].gauge = value;
168 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
169 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
170 sstrncpy (vl.type, "memory", sizeof (vl.type));
171 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
173 plugin_dispatch_values (&vl);
176 static int memory_read (void)
178 #if HAVE_HOST_STATISTICS
179 kern_return_t status;
180 vm_statistics_data_t vm_data;
181 mach_msg_type_number_t vm_data_len;
189 if (!port_host || !pagesize)
192 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
193 if ((status = host_statistics (port_host, HOST_VM_INFO,
194 (host_info_t) &vm_data,
195 &vm_data_len)) != KERN_SUCCESS)
197 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
202 * From <http://docs.info.apple.com/article.html?artnum=107918>:
205 * This information can't be cached to disk, so it must stay in RAM.
206 * The amount depends on what applications you are using.
209 * This information is currently in RAM and actively being used.
212 * This information is no longer being used and has been cached to
213 * disk, but it will remain in RAM until another application needs
214 * the space. Leaving this information in RAM is to your advantage if
215 * you (or a client of your computer) come back to it later.
218 * This memory is not being used.
221 wired = (gauge_t) (((uint64_t) vm_data.wire_count) * ((uint64_t) pagesize));
222 active = (gauge_t) (((uint64_t) vm_data.active_count) * ((uint64_t) pagesize));
223 inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
224 free = (gauge_t) (((uint64_t) vm_data.free_count) * ((uint64_t) pagesize));
225 total = wired + active + inactive + free;
229 memory_submit ("wired", wired);
230 memory_submit ("active", active);
231 memory_submit ("inactive", inactive);
232 memory_submit ("free", free);
234 if (values_percentage)
236 memory_submit ("percent_wired", (gauge_t) ((float_t) wired) / total * 100);
237 memory_submit ("percent_active", (gauge_t) ((float_t) active) / total * 100);
238 memory_submit ("percent_inactive", (gauge_t) ((float_t) inactive / total * 100);
239 memory_submit ("percent_free", (gauge_t) ((float_t) free / total * 100);
241 /* #endif HAVE_HOST_STATISTICS */
243 #elif HAVE_SYSCTLBYNAME
245 * vm.stats.vm.v_page_size: 4096
246 * vm.stats.vm.v_page_count: 246178
247 * vm.stats.vm.v_free_count: 28760
248 * vm.stats.vm.v_wire_count: 37526
249 * vm.stats.vm.v_active_count: 55239
250 * vm.stats.vm.v_inactive_count: 113730
251 * vm.stats.vm.v_cache_count: 10809
253 char *sysctl_keys[8] =
255 "vm.stats.vm.v_page_size",
256 "vm.stats.vm.v_page_count",
257 "vm.stats.vm.v_free_count",
258 "vm.stats.vm.v_wire_count",
259 "vm.stats.vm.v_active_count",
260 "vm.stats.vm.v_inactive_count",
261 "vm.stats.vm.v_cache_count",
264 double sysctl_vals[8];
268 for (i = 0; sysctl_keys[i] != NULL; i++)
271 size_t value_len = sizeof (value);
273 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
276 sysctl_vals[i] = value;
277 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
281 sysctl_vals[i] = NAN;
283 } /* for (sysctl_keys) */
285 /* multiply all all page counts with the pagesize */
286 for (i = 1; sysctl_keys[i] != NULL; i++)
287 if (!isnan (sysctl_vals[i]))
288 sysctl_vals[i] *= sysctl_vals[0];
292 memory_submit ("free", sysctl_vals[2]);
293 memory_submit ("wired", sysctl_vals[3]);
294 memory_submit ("active", sysctl_vals[4]);
295 memory_submit ("inactive", sysctl_vals[5]);
296 memory_submit ("cache", sysctl_vals[6]);
298 if (values_percentage)
300 double total = sysctl_vals[2] + sysctl_vals[3] + sysctl_vals[4] + sysctl_vals[5] + sysctl_vals[6];
301 memory_submit ("percent_free", (gauge_t) ((float_t) sysctl_vals[2]) / total * 100);
302 memory_submit ("percent_wired", (gauge_t) ((float_t) sysctl_vals[3]) / total * 100);
303 memory_submit ("percent_active", (gauge_t) ((float_t) sysctl_vals[4]) / total * 100);
304 memory_submit ("percent_inactive", (gauge_t) ((float_t) sysctl_vals[5]) / total * 100);
305 memory_submit ("percent_cache", (gauge_t) ((float_t) sysctl_vals[6]) / total * 100);
307 /* #endif HAVE_SYSCTLBYNAME */
316 long long mem_total = 0;
317 long long mem_used = 0;
318 long long mem_buffered = 0;
319 long long mem_cached = 0;
320 long long mem_free = 0;
322 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
325 WARNING ("memory: fopen: %s",
326 sstrerror (errno, errbuf, sizeof (errbuf)));
330 while (fgets (buffer, 1024, fh) != NULL)
332 long long *val = NULL;
334 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
336 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
338 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
340 else if (strncasecmp (buffer, "Cached:", 7) == 0)
345 numfields = strsplit (buffer, fields, 8);
350 *val = atoll (fields[1]) * 1024LL;
356 WARNING ("memory: fclose: %s",
357 sstrerror (errno, errbuf, sizeof (errbuf)));
360 if (mem_total >= (mem_free + mem_buffered + mem_cached))
362 mem_used = mem_total - (mem_free + mem_buffered + mem_cached);
365 memory_submit ("used", mem_used);
366 memory_submit ("buffered", mem_buffered);
367 memory_submit ("cached", mem_cached);
368 memory_submit ("free", mem_free);
370 if (values_percentage)
372 memory_submit ("percent_used", (gauge_t) ((float_t) mem_used) / mem_total * 100);
373 memory_submit ("percent_buffered", (gauge_t) ((float_t) mem_buffered) / mem_total * 100);
374 memory_submit ("percent_cached", (gauge_t) ((float_t) mem_cached) / mem_total * 100);
375 memory_submit ("percent_free", (gauge_t) ((float_t) mem_free) / mem_total * 100);
378 /* #endif KERNEL_LINUX */
381 /* Most of the additions here were taken as-is from the k9toolkit from
382 * Brendan Gregg and are subject to change I guess */
396 mem_used = get_kstat_value (ksp, "pagestotal");
397 mem_free = get_kstat_value (ksp, "pagesfree");
398 mem_lock = get_kstat_value (ksp, "pageslocked");
402 pp_kernel = get_kstat_value (ksp, "pp_kernel");
403 physmem = get_kstat_value (ksp, "physmem");
404 availrmem = get_kstat_value (ksp, "availrmem");
406 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
408 WARNING ("memory plugin: one of used, free or locked is negative.");
412 mem_unus = physmem - mem_used;
414 if (mem_used < (mem_free + mem_lock))
416 /* source: http://wesunsolve.net/bugid/id/4909199
417 * this seems to happen when swap space is small, e.g. 2G on a 32G system
418 * we will make some assumptions here
419 * educated solaris internals help welcome here */
420 DEBUG ("memory plugin: pages total is smaller than \"free\" "
421 "+ \"locked\". This is probably due to small "
423 mem_free = availrmem;
428 mem_used -= mem_free + mem_lock;
431 /* mem_kern is accounted for in mem_lock */
432 if ( pp_kernel < mem_lock )
434 mem_kern = pp_kernel;
435 mem_lock -= pp_kernel;
443 mem_used *= pagesize; /* If this overflows you have some serious */
444 mem_free *= pagesize; /* memory.. Why not call me up and give me */
445 mem_lock *= pagesize; /* some? ;) */
446 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
447 mem_unus *= pagesize;
451 memory_submit ("used", mem_used);
452 memory_submit ("free", mem_free);
453 memory_submit ("locked", mem_lock);
454 memory_submit ("kernel", mem_kern);
455 memory_submit ("unusable", mem_unus);
457 if (values_percentage)
459 memory_submit ("percent_used", (gauge_t) ((float_t) mem_used) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
460 memory_submit ("percent_free", (gauge_t) ((float_t) mem_free) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
461 memory_submit ("percent_locked", (gauge_t) ((float_t) mem_lock) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
462 memory_submit ("percent_kernel", (gauge_t) ((float_t) mem_kern) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
463 memory_submit ("percent_unusable", (gauge_t) ((float_t) mem_unus) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
466 /* #endif HAVE_LIBKSTAT */
469 int mib[] = {CTL_VM, VM_METER};
470 struct vmtotal vmtotal;
473 memset (&vmtotal, 0, sizeof (vmtotal));
474 size = sizeof (vmtotal);
476 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
478 WARNING ("memory plugin: sysctl failed: %s",
479 sstrerror (errno, errbuf, sizeof (errbuf)));
483 assert (pagesize > 0);
486 memory_submit ("active", vmtotal.t_arm * pagesize);
487 memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
488 memory_submit ("free", vmtotal.t_free * pagesize);
490 if (values_percentage)
492 memory_submit ("percent_active", (gauge_t) ((float_t) vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
493 memory_submit ("percent_inactive", (gauge_t) ((float_t) (vmtotal.t_rm - vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
494 memory_submit ("percent_free", (gauge_t) ((float_t) vmtotal.t_free) / (vmtotal.t_rm + vmtotal.t_free) * 100);
496 /* #endif HAVE_SYSCTL */
498 #elif HAVE_LIBSTATGRAB
501 if ((ios = sg_get_mem_stats ()) != NULL)
505 memory_submit ("used", ios->used);
506 memory_submit ("cached", ios->cache);
507 memory_submit ("free", ios->free);
509 if (values_percentage)
511 memory_submit ("percent_used", (gauge_t) ((float_t) ios->used) / (ios->used + ios->cache + ios->free) * 100);
512 memory_submit ("percent_cached", (gauge_t) ((float_t) ios->cache) / (ios->used + ios->cache + ios->free) * 100);
513 memory_submit ("percent_free", (gauge_t) ((float_t) ios->free) / (ios->used + ios->cache + ios->free) * 100);
516 /* #endif HAVE_LIBSTATGRAB */
519 if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
522 WARNING ("memory plugin: perfstat_memory_total failed: %s",
523 sstrerror (errno, errbuf, sizeof (errbuf)));
528 memory_submit ("used", pmemory.real_inuse * pagesize);
529 memory_submit ("free", pmemory.real_free * pagesize);
530 memory_submit ("cached", pmemory.numperm * pagesize);
531 memory_submit ("system", pmemory.real_system * pagesize);
532 memory_submit ("user", pmemory.real_process * pagesize);
534 if (values_percentage)
536 memory_submit ("percent_used", (gauge_t) ((float_t) pmemory.real_inuse) / pmemory.real_total * 100);
537 memory_submit ("percent_free", (gauge_t) ((float_t) pmemory.real_free) / pmemory.real_total * 100);
538 memory_submit ("percent_cached", (gauge_t) ((float_t) pmemory.numperm) / pmemory.real_total * 100);
539 memory_submit ("percent_system", (gauge_t) ((float_t) pmemory.real_system) / pmemory.real_total * 100);
540 memory_submit ("percent_user", (gauge_t) ((float_t) pmemory.real_process) / pmemory.real_total * 100);
542 #endif /* HAVE_PERFSTAT */
547 void module_register (void)
549 plugin_register_complex_config ("memory", memory_config);
550 plugin_register_init ("memory", memory_init);
551 plugin_register_read ("memory", memory_read);
552 } /* void module_register */