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 collectd.org>
22 * Simon Kuhnle <simon at blarzwurst.de>
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
33 #ifdef HAVE_SYS_VMMETER_H
34 # include <sys/vmmeter.h>
37 #ifdef HAVE_MACH_KERN_RETURN_H
38 # include <mach/kern_return.h>
40 #ifdef HAVE_MACH_MACH_INIT_H
41 # include <mach/mach_init.h>
43 #ifdef HAVE_MACH_MACH_HOST_H
44 # include <mach/mach_host.h>
46 #ifdef HAVE_MACH_HOST_PRIV_H
47 # include <mach/host_priv.h>
49 #ifdef HAVE_MACH_VM_STATISTICS_H
50 # include <mach/vm_statistics.h>
54 # include <statgrab.h>
58 # include <sys/protosw.h>
59 # include <libperfstat.h>
60 #endif /* HAVE_PERFSTAT */
62 /* vm_statistics_data_t */
63 #if HAVE_HOST_STATISTICS
64 static mach_port_t port_host;
65 static vm_size_t pagesize;
66 /* #endif HAVE_HOST_STATISTICS */
68 #elif HAVE_SYSCTLBYNAME
69 /* no global variables */
70 /* #endif HAVE_SYSCTLBYNAME */
73 /* no global variables */
74 /* #endif KERNEL_LINUX */
80 /* #endif HAVE_LIBKSTAT */
84 /* #endif HAVE_SYSCTL */
86 #elif HAVE_LIBSTATGRAB
87 /* no global variables */
88 /* endif HAVE_LIBSTATGRAB */
91 /* endif HAVE_PERFSTAT */
93 # error "No applicable input method."
96 static _Bool values_absolute = 1;
97 static _Bool values_percentage = 0;
99 static int memory_config (oconfig_item_t *ci) /* {{{ */
103 for (i = 0; i < ci->children_num; i++)
105 oconfig_item_t *child = ci->children + i;
106 if (strcasecmp ("ValuesAbsolute", child->key) == 0)
107 cf_util_get_boolean (child, &values_absolute);
108 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
109 cf_util_get_boolean (child, &values_percentage);
111 ERROR ("memory plugin: Invalid configuration option: "
112 "\"%s\".", child->key);
116 } /* }}} int memory_config */
118 static int memory_init (void)
120 #if HAVE_HOST_STATISTICS
121 port_host = mach_host_self ();
122 host_page_size (port_host, &pagesize);
123 /* #endif HAVE_HOST_STATISTICS */
125 #elif HAVE_SYSCTLBYNAME
127 /* #endif HAVE_SYSCTLBYNAME */
129 #elif defined(KERNEL_LINUX)
131 /* #endif KERNEL_LINUX */
133 #elif defined(HAVE_LIBKSTAT)
134 /* getpagesize(3C) tells me this does not fail.. */
135 pagesize = getpagesize ();
136 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
141 if (get_kstat (&ksz, "zfs", 0, "arcstats") != 0)
147 /* #endif HAVE_LIBKSTAT */
150 pagesize = getpagesize ();
153 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
156 /* #endif HAVE_SYSCTL */
158 #elif HAVE_LIBSTATGRAB
160 /* #endif HAVE_LIBSTATGRAB */
163 pagesize = getpagesize ();
164 #endif /* HAVE_PERFSTAT */
166 } /* int memory_init */
168 #define MEMORY_SUBMIT(...) do { \
169 if (values_absolute) \
170 plugin_dispatch_multivalue (vl, 0, DS_TYPE_GAUGE, __VA_ARGS__, NULL); \
171 if (values_percentage) \
172 plugin_dispatch_multivalue (vl, 1, DS_TYPE_GAUGE, __VA_ARGS__, NULL); \
175 static int memory_read_internal (value_list_t *vl)
177 #if HAVE_HOST_STATISTICS
178 kern_return_t status;
179 vm_statistics_data_t vm_data;
180 mach_msg_type_number_t vm_data_len;
187 if (!port_host || !pagesize)
190 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
191 if ((status = host_statistics (port_host, HOST_VM_INFO,
192 (host_info_t) &vm_data,
193 &vm_data_len)) != KERN_SUCCESS)
195 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
200 * From <http://docs.info.apple.com/article.html?artnum=107918>:
203 * This information can't be cached to disk, so it must stay in RAM.
204 * The amount depends on what applications you are using.
207 * This information is currently in RAM and actively being used.
210 * This information is no longer being used and has been cached to
211 * disk, but it will remain in RAM until another application needs
212 * the space. Leaving this information in RAM is to your advantage if
213 * you (or a client of your computer) come back to it later.
216 * This memory is not being used.
219 wired = (gauge_t) (((uint64_t) vm_data.wire_count) * ((uint64_t) pagesize));
220 active = (gauge_t) (((uint64_t) vm_data.active_count) * ((uint64_t) pagesize));
221 inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
222 free = (gauge_t) (((uint64_t) vm_data.free_count) * ((uint64_t) pagesize));
224 MEMORY_SUBMIT ("wired", wired,
226 "inactive", inactive,
228 /* #endif HAVE_HOST_STATISTICS */
230 #elif HAVE_SYSCTLBYNAME
232 * vm.stats.vm.v_page_size: 4096
233 * vm.stats.vm.v_page_count: 246178
234 * vm.stats.vm.v_free_count: 28760
235 * vm.stats.vm.v_wire_count: 37526
236 * vm.stats.vm.v_active_count: 55239
237 * vm.stats.vm.v_inactive_count: 113730
238 * vm.stats.vm.v_cache_count: 10809
240 const char *sysctl_keys[8] =
242 "vm.stats.vm.v_page_size",
243 "vm.stats.vm.v_page_count",
244 "vm.stats.vm.v_free_count",
245 "vm.stats.vm.v_wire_count",
246 "vm.stats.vm.v_active_count",
247 "vm.stats.vm.v_inactive_count",
248 "vm.stats.vm.v_cache_count",
251 double sysctl_vals[8];
255 for (i = 0; sysctl_keys[i] != NULL; i++)
258 size_t value_len = sizeof (value);
260 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
263 sysctl_vals[i] = value;
264 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
268 sysctl_vals[i] = NAN;
270 } /* for (sysctl_keys) */
272 /* multiply all all page counts with the pagesize */
273 for (i = 1; sysctl_keys[i] != NULL; i++)
274 if (!isnan (sysctl_vals[i]))
275 sysctl_vals[i] *= sysctl_vals[0];
277 MEMORY_SUBMIT ("free", (gauge_t) sysctl_vals[2],
278 "wired", (gauge_t) sysctl_vals[3],
279 "active", (gauge_t) sysctl_vals[4],
280 "inactive", (gauge_t) sysctl_vals[5],
281 "cache", (gauge_t) sysctl_vals[6]);
282 /* #endif HAVE_SYSCTLBYNAME */
291 _Bool detailed_slab_info = 0;
293 gauge_t mem_total = 0;
294 gauge_t mem_used = 0;
295 gauge_t mem_buffered = 0;
296 gauge_t mem_cached = 0;
297 gauge_t mem_free = 0;
298 gauge_t mem_slab_total = 0;
299 gauge_t mem_slab_reclaimable = 0;
300 gauge_t mem_slab_unreclaimable = 0;
302 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
305 WARNING ("memory: fopen: %s",
306 sstrerror (errno, errbuf, sizeof (errbuf)));
310 while (fgets (buffer, sizeof (buffer), fh) != NULL)
314 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
316 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
318 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
320 else if (strncasecmp (buffer, "Cached:", 7) == 0)
322 else if (strncasecmp (buffer, "Slab:", 5) == 0)
323 val = &mem_slab_total;
324 else if (strncasecmp (buffer, "SReclaimable:", 13) == 0) {
325 val = &mem_slab_reclaimable;
326 detailed_slab_info = 1;
328 else if (strncasecmp (buffer, "SUnreclaim:", 11) == 0) {
329 val = &mem_slab_unreclaimable;
330 detailed_slab_info = 1;
335 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
339 *val = 1024.0 * atof (fields[1]);
345 WARNING ("memory: fclose: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
349 if (mem_total < (mem_free + mem_buffered + mem_cached + mem_slab_total))
352 mem_used = mem_total - (mem_free + mem_buffered + mem_cached + mem_slab_total);
354 /* SReclaimable and SUnreclaim were introduced in kernel 2.6.19
355 * They sum up to the value of Slab, which is available on older & newer
356 * kernels. So SReclaimable/SUnreclaim are submitted if available, and Slab
358 if (detailed_slab_info)
359 MEMORY_SUBMIT ("used", mem_used,
360 "buffered", mem_buffered,
361 "cached", mem_cached,
363 "slab_unrecl", mem_slab_unreclaimable,
364 "slab_recl", mem_slab_reclaimable);
366 MEMORY_SUBMIT ("used", mem_used,
367 "buffered", mem_buffered,
368 "cached", mem_cached,
370 "slab", mem_slab_total);
371 /* #endif KERNEL_LINUX */
374 /* Most of the additions here were taken as-is from the k9toolkit from
375 * Brendan Gregg and are subject to change I guess */
393 mem_used = get_kstat_value (ksp, "pagestotal");
394 mem_free = get_kstat_value (ksp, "pagesfree");
395 mem_lock = get_kstat_value (ksp, "pageslocked");
396 arcsize = get_kstat_value (ksz, "size");
397 pp_kernel = get_kstat_value (ksp, "pp_kernel");
398 physmem = get_kstat_value (ksp, "physmem");
399 availrmem = get_kstat_value (ksp, "availrmem");
404 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
406 WARNING ("memory plugin: one of used, free or locked is negative.");
410 mem_unus = physmem - mem_used;
412 if (mem_used < (mem_free + mem_lock))
414 /* source: http://wesunsolve.net/bugid/id/4909199
415 * this seems to happen when swap space is small, e.g. 2G on a 32G system
416 * we will make some assumptions here
417 * educated solaris internals help welcome here */
418 DEBUG ("memory plugin: pages total is smaller than \"free\" "
419 "+ \"locked\". This is probably due to small "
421 mem_free = availrmem;
426 mem_used -= mem_free + mem_lock;
429 /* mem_kern is accounted for in mem_lock */
430 if (pp_kernel < mem_lock)
432 mem_kern = pp_kernel;
433 mem_lock -= pp_kernel;
441 mem_used *= pagesize; /* If this overflows you have some serious */
442 mem_free *= pagesize; /* memory.. Why not call me up and give me */
443 mem_lock *= pagesize; /* some? ;) */
444 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
445 mem_unus *= pagesize;
449 MEMORY_SUBMIT ("used", (gauge_t) mem_used,
450 "free", (gauge_t) mem_free,
451 "locked", (gauge_t) mem_lock,
452 "kernel", (gauge_t) mem_kern,
453 "arc", (gauge_t) arcsize,
454 "unusable", (gauge_t) mem_unus);
455 /* #endif HAVE_LIBKSTAT */
458 int mib[] = {CTL_VM, VM_METER};
459 struct vmtotal vmtotal;
461 gauge_t mem_inactive;
465 memset (&vmtotal, 0, sizeof (vmtotal));
466 size = sizeof (vmtotal);
468 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
470 WARNING ("memory plugin: sysctl failed: %s",
471 sstrerror (errno, errbuf, sizeof (errbuf)));
475 assert (pagesize > 0);
476 mem_active = (gauge_t) (vmtotal.t_arm * pagesize);
477 mem_inactive = (gauge_t) ((vmtotal.t_rm - vmtotal.t_arm) * pagesize);
478 mem_free = (gauge_t) (vmtotal.t_free * pagesize);
480 MEMORY_SUBMIT ("active", mem_active,
481 "inactive", mem_inactive,
483 /* #endif HAVE_SYSCTL */
485 #elif HAVE_LIBSTATGRAB
488 ios = sg_get_mem_stats ();
492 MEMORY_SUBMIT ("used", (gauge_t) ios->used,
493 "cached", (gauge_t) ios->cache,
494 "free", (gauge_t) ios->free);
495 /* #endif HAVE_LIBSTATGRAB */
498 perfstat_memory_total_t pmemory;
500 memset (&pmemory, 0, sizeof (pmemory));
501 if (perfstat_memory_total(NULL, &pmemory, sizeof(pmemory), 1) < 0)
504 WARNING ("memory plugin: perfstat_memory_total failed: %s",
505 sstrerror (errno, errbuf, sizeof (errbuf)));
509 /* Unfortunately, the AIX documentation is not very clear on how these
510 * numbers relate to one another. The only thing is states explcitly
512 * real_total = real_process + real_free + numperm + real_system
514 * Another segmentation, which would be closer to the numbers reported
515 * by the "svmon" utility, would be:
516 * real_total = real_free + real_inuse
517 * real_inuse = "active" + real_pinned + numperm
519 MEMORY_SUBMIT ("free", (gauge_t) (pmemory.real_free * pagesize),
520 "cached", (gauge_t) (pmemory.numperm * pagesize),
521 "system", (gauge_t) (pmemory.real_system * pagesize),
522 "user", (gauge_t) (pmemory.real_process * pagesize));
523 #endif /* HAVE_PERFSTAT */
526 } /* }}} int memory_read_internal */
528 static int memory_read (void) /* {{{ */
531 value_list_t vl = VALUE_LIST_INIT;
534 vl.values_len = STATIC_ARRAY_SIZE (v);
535 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
536 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
537 sstrncpy (vl.type, "memory", sizeof (vl.type));
540 return (memory_read_internal (&vl));
541 } /* }}} int memory_read */
543 void module_register (void)
545 plugin_register_complex_config ("memory", memory_config);
546 plugin_register_init ("memory", memory_init);
547 plugin_register_read ("memory", memory_read);
548 } /* void module_register */