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 /* endif HAVE_PERFSTAT */
89 # error "No applicable input method."
92 static _Bool values_absolute = 1;
93 static _Bool values_percentage = 0;
95 static int memory_config (oconfig_item_t *ci) /* {{{ */
99 for (i = 0; i < ci->children_num; i++)
101 oconfig_item_t *child = ci->children + i;
102 if (strcasecmp ("ValuesAbsolute", child->key) == 0)
103 cf_util_get_boolean (child, &values_absolute);
104 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
105 cf_util_get_boolean (child, &values_percentage);
107 ERROR ("memory plugin: Invalid configuration option: "
108 "\"%s\".", child->key);
112 } /* }}} int memory_config */
114 static int memory_init (void)
116 #if HAVE_HOST_STATISTICS
117 port_host = mach_host_self ();
118 host_page_size (port_host, &pagesize);
119 /* #endif HAVE_HOST_STATISTICS */
121 #elif HAVE_SYSCTLBYNAME
123 /* #endif HAVE_SYSCTLBYNAME */
125 #elif defined(KERNEL_LINUX)
127 /* #endif KERNEL_LINUX */
129 #elif defined(HAVE_LIBKSTAT)
130 /* getpagesize(3C) tells me this does not fail.. */
131 pagesize = getpagesize ();
132 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
137 /* #endif HAVE_LIBKSTAT */
140 pagesize = getpagesize ();
143 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
146 /* #endif HAVE_SYSCTL */
148 #elif HAVE_LIBSTATGRAB
150 /* #endif HAVE_LIBSTATGRAB */
153 pagesize = getpagesize ();
154 #endif /* HAVE_PERFSTAT */
156 } /* int memory_init */
158 static void memory_submit (const char *type_instance, gauge_t value, gauge_t total)
161 value_list_t vl = VALUE_LIST_INIT;
165 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
166 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
167 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
171 values[0].gauge = value;
172 sstrncpy (vl.type, "memory", sizeof (vl.type));
173 plugin_dispatch_values (&vl);
175 if (values_percentage)
177 values[0].gauge = 100.0 * value / total;
178 sstrncpy (vl.type, "percent", sizeof (vl.type));
179 plugin_dispatch_values (&vl);
183 static int memory_read (void)
185 #if HAVE_HOST_STATISTICS
186 kern_return_t status;
187 vm_statistics_data_t vm_data;
188 mach_msg_type_number_t vm_data_len;
196 if (!port_host || !pagesize)
199 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
200 if ((status = host_statistics (port_host, HOST_VM_INFO,
201 (host_info_t) &vm_data,
202 &vm_data_len)) != KERN_SUCCESS)
204 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
209 * From <http://docs.info.apple.com/article.html?artnum=107918>:
212 * This information can't be cached to disk, so it must stay in RAM.
213 * The amount depends on what applications you are using.
216 * This information is currently in RAM and actively being used.
219 * This information is no longer being used and has been cached to
220 * disk, but it will remain in RAM until another application needs
221 * the space. Leaving this information in RAM is to your advantage if
222 * you (or a client of your computer) come back to it later.
225 * This memory is not being used.
228 wired = (gauge_t) (((uint64_t) vm_data.wire_count) * ((uint64_t) pagesize));
229 active = (gauge_t) (((uint64_t) vm_data.active_count) * ((uint64_t) pagesize));
230 inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
231 free = (gauge_t) (((uint64_t) vm_data.free_count) * ((uint64_t) pagesize));
232 total = wired + active + inactive + free;
234 memory_submit ("wired", wired, total);
235 memory_submit ("active", active, total);
236 memory_submit ("inactive", inactive, total);
237 memory_submit ("free", free, total);
238 /* #endif HAVE_HOST_STATISTICS */
240 #elif HAVE_SYSCTLBYNAME
242 * vm.stats.vm.v_page_size: 4096
243 * vm.stats.vm.v_page_count: 246178
244 * vm.stats.vm.v_free_count: 28760
245 * vm.stats.vm.v_wire_count: 37526
246 * vm.stats.vm.v_active_count: 55239
247 * vm.stats.vm.v_inactive_count: 113730
248 * vm.stats.vm.v_cache_count: 10809
250 char *sysctl_keys[8] =
252 "vm.stats.vm.v_page_size",
253 "vm.stats.vm.v_page_count",
254 "vm.stats.vm.v_free_count",
255 "vm.stats.vm.v_wire_count",
256 "vm.stats.vm.v_active_count",
257 "vm.stats.vm.v_inactive_count",
258 "vm.stats.vm.v_cache_count",
261 double sysctl_vals[8];
266 for (i = 0; sysctl_keys[i] != NULL; i++)
269 size_t value_len = sizeof (value);
271 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
274 sysctl_vals[i] = value;
275 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
279 sysctl_vals[i] = NAN;
281 } /* for (sysctl_keys) */
283 /* multiply all all page counts with the pagesize */
284 for (i = 1; sysctl_keys[i] != NULL; i++)
285 if (!isnan (sysctl_vals[i]))
286 sysctl_vals[i] *= sysctl_vals[0];
288 total = sysctl_vals[2] + sysctl_vals[3] + sysctl_vals[4] + sysctl_vals[5] + sysctl_vals[6];
289 memory_submit ("free", sysctl_vals[2], total);
290 memory_submit ("wired", sysctl_vals[3], total);
291 memory_submit ("active", sysctl_vals[4], total);
292 memory_submit ("inactive", sysctl_vals[5], total);
293 memory_submit ("cache", sysctl_vals[6], total);
294 /* #endif HAVE_SYSCTLBYNAME */
303 long long mem_total = 0;
304 long long mem_used = 0;
305 long long mem_buffered = 0;
306 long long mem_cached = 0;
307 long long mem_free = 0;
309 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
312 WARNING ("memory: fopen: %s",
313 sstrerror (errno, errbuf, sizeof (errbuf)));
317 while (fgets (buffer, 1024, fh) != NULL)
319 long long *val = NULL;
321 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
323 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
325 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
327 else if (strncasecmp (buffer, "Cached:", 7) == 0)
332 numfields = strsplit (buffer, fields, 8);
337 *val = atoll (fields[1]) * 1024LL;
343 WARNING ("memory: fclose: %s",
344 sstrerror (errno, errbuf, sizeof (errbuf)));
347 if (mem_total < (mem_free + mem_buffered + mem_cached))
350 mem_used = mem_total - (mem_free + mem_buffered + mem_cached);
351 memory_submit ("used", (gauge_t) mem_used, (gauge_t) mem_total);
352 memory_submit ("buffered", (gauge_t) mem_buffered, (gauge_t) mem_total);
353 memory_submit ("cached", (gauge_t) mem_cached, (gauge_t) mem_total);
354 memory_submit ("free", (gauge_t) mem_free, (gauge_t) mem_total);
355 /* #endif KERNEL_LINUX */
358 /* Most of the additions here were taken as-is from the k9toolkit from
359 * Brendan Gregg and are subject to change I guess */
374 mem_used = get_kstat_value (ksp, "pagestotal");
375 mem_free = get_kstat_value (ksp, "pagesfree");
376 mem_lock = get_kstat_value (ksp, "pageslocked");
380 pp_kernel = get_kstat_value (ksp, "pp_kernel");
381 physmem = get_kstat_value (ksp, "physmem");
382 availrmem = get_kstat_value (ksp, "availrmem");
384 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
386 WARNING ("memory plugin: one of used, free or locked is negative.");
390 mem_unus = physmem - mem_used;
392 if (mem_used < (mem_free + mem_lock))
394 /* source: http://wesunsolve.net/bugid/id/4909199
395 * this seems to happen when swap space is small, e.g. 2G on a 32G system
396 * we will make some assumptions here
397 * educated solaris internals help welcome here */
398 DEBUG ("memory plugin: pages total is smaller than \"free\" "
399 "+ \"locked\". This is probably due to small "
401 mem_free = availrmem;
406 mem_used -= mem_free + mem_lock;
409 /* mem_kern is accounted for in mem_lock */
410 if (pp_kernel < mem_lock)
412 mem_kern = pp_kernel;
413 mem_lock -= pp_kernel;
421 mem_used *= pagesize; /* If this overflows you have some serious */
422 mem_free *= pagesize; /* memory.. Why not call me up and give me */
423 mem_lock *= pagesize; /* some? ;) */
424 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
425 mem_unus *= pagesize;
426 mem_total = mem_used + mem_free + mem_lock + mem_kern + mem_unus;
428 memory_submit ("used", (gauge_t) mem_used, (gauge_t) mem_total);
429 memory_submit ("free", (gauge_t) mem_free, (gauge_t) mem_total);
430 memory_submit ("locked", (gauge_t) mem_lock, (gauge_t) mem_total);
431 memory_submit ("kernel", (gauge_t) mem_kern, (gauge_t) mem_total);
432 memory_submit ("unusable", (gauge_t) mem_unus, (gauge_t) mem_total);
433 /* #endif HAVE_LIBKSTAT */
436 int mib[] = {CTL_VM, VM_METER};
437 struct vmtotal vmtotal;
439 gauge_t mem_inactive;
444 memset (&vmtotal, 0, sizeof (vmtotal));
445 size = sizeof (vmtotal);
447 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
449 WARNING ("memory plugin: sysctl failed: %s",
450 sstrerror (errno, errbuf, sizeof (errbuf)));
454 assert (pagesize > 0);
455 mem_active = (gauge_t) (vmtotal.t_arm * pagesize);
456 mem_inactive = (gauge_t) ((vmtotal.t_rm - vmtotal.t_arm) * pagesize);
457 mem_free = (gauge_t) (vmtotal.t_free * pagesize);
458 mem_total = mem_active + mem_inactive + mem_free;
460 memory_submit ("active", mem_active, mem_total);
461 memory_submit ("inactive", mem_inactive, mem_total);
462 memory_submit ("free", mem_free, mem_total);
463 /* #endif HAVE_SYSCTL */
465 #elif HAVE_LIBSTATGRAB
469 ios = sg_get_mem_stats ();
473 total = (gauge_t) (ios->used + ios->cache + ios->free);
474 memory_submit ("used", (gauge_t) ios->used, total);
475 memory_submit ("cached", (gauge_t) ios->cache, total);
476 memory_submit ("free", (gauge_t) ios->free, total);
477 /* #endif HAVE_LIBSTATGRAB */
480 perfstat_memory_total_t pmemory;
482 memset (&pmemory, 0, sizeof (pmemory));
483 if (perfstat_memory_total(NULL, &pmemory, sizeof(pmemory), 1) < 0)
486 WARNING ("memory plugin: perfstat_memory_total failed: %s",
487 sstrerror (errno, errbuf, sizeof (errbuf)));
491 /* Unfortunately, the AIX documentation is not very clear on how these
492 * numbers relate to one another. The only thing is states explcitly
494 * real_total = real_process + real_free + numperm + real_system
496 * Another segmentation, which would be closer to the numbers reported
497 * by the "svmon" utility, would be:
498 * real_total = real_free + real_inuse
499 * real_inuse = "active" + real_pinned + numperm
501 memory_submit ("free", pmemory.real_free * pagesize, pmemory.real_total * pagesize);
502 memory_submit ("cached", pmemory.numperm * pagesize, pmemory.real_total * pagesize);
503 memory_submit ("system", pmemory.real_system * pagesize, pmemory.real_total * pagesize);
504 memory_submit ("user", pmemory.real_process * pagesize, pmemory.real_total * pagesize);
505 #endif /* HAVE_PERFSTAT */
510 void module_register (void)
512 plugin_register_complex_config ("memory", memory_config);
513 plugin_register_init ("memory", memory_init);
514 plugin_register_read ("memory", memory_read);
515 } /* void module_register */