1 /**
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
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Simon Kuhnle <simon at blarzwurst.de>
23 * Manuel Sanmartin
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
32 #endif
33 #ifdef HAVE_SYS_VMMETER_H
34 # include <sys/vmmeter.h>
35 #endif
37 #ifdef HAVE_MACH_KERN_RETURN_H
38 # include <mach/kern_return.h>
39 #endif
40 #ifdef HAVE_MACH_MACH_INIT_H
41 # include <mach/mach_init.h>
42 #endif
43 #ifdef HAVE_MACH_MACH_HOST_H
44 # include <mach/mach_host.h>
45 #endif
46 #ifdef HAVE_MACH_HOST_PRIV_H
47 # include <mach/host_priv.h>
48 #endif
49 #ifdef HAVE_MACH_VM_STATISTICS_H
50 # include <mach/vm_statistics.h>
51 #endif
53 #if HAVE_STATGRAB_H
54 # include <statgrab.h>
55 #endif
57 #if HAVE_PERFSTAT
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 */
72 #elif KERNEL_LINUX
73 /* no global variables */
74 /* #endif KERNEL_LINUX */
76 #elif HAVE_LIBKSTAT
77 static int pagesize;
78 static kstat_t *ksp;
79 static kstat_t *ksz;
80 /* #endif HAVE_LIBKSTAT */
82 #elif HAVE_SYSCTL
83 static int pagesize;
84 /* #endif HAVE_SYSCTL */
86 #elif HAVE_LIBSTATGRAB
87 /* no global variables */
88 /* endif HAVE_LIBSTATGRAB */
89 #elif HAVE_PERFSTAT
90 static int pagesize;
91 /* endif HAVE_PERFSTAT */
92 #else
93 # error "No applicable input method."
94 #endif
96 static _Bool values_absolute = 1;
97 static _Bool values_percentage = 0;
99 static int memory_config (oconfig_item_t *ci) /* {{{ */
100 {
101 int i;
103 for (i = 0; i < ci->children_num; i++)
104 {
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);
110 else
111 ERROR ("memory plugin: Invalid configuration option: "
112 "\"%s\".", child->key);
113 }
115 return (0);
116 } /* }}} int memory_config */
118 static int memory_init (void)
119 {
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
126 /* no init stuff */
127 /* #endif HAVE_SYSCTLBYNAME */
129 #elif defined(KERNEL_LINUX)
130 /* no init stuff */
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)
137 {
138 ksp = NULL;
139 return (-1);
140 }
141 if (get_kstat (&ksz, "zfs", 0, "arcstats") != 0)
142 {
143 ksz = NULL;
144 return (-1);
145 }
147 /* #endif HAVE_LIBKSTAT */
149 #elif HAVE_SYSCTL
150 pagesize = getpagesize ();
151 if (pagesize <= 0)
152 {
153 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
154 return (-1);
155 }
156 /* #endif HAVE_SYSCTL */
158 #elif HAVE_LIBSTATGRAB
159 /* no init stuff */
160 /* #endif HAVE_LIBSTATGRAB */
162 #elif HAVE_PERFSTAT
163 pagesize = getpagesize ();
164 #endif /* HAVE_PERFSTAT */
165 return (0);
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); \
173 } while (0)
175 static int memory_read_internal (value_list_t *vl)
176 {
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;
182 gauge_t wired;
183 gauge_t active;
184 gauge_t inactive;
185 gauge_t free;
187 if (!port_host || !pagesize)
188 return (-1);
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)
194 {
195 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
196 return (-1);
197 }
199 /*
200 * From <http://docs.info.apple.com/article.html?artnum=107918>:
201 *
202 * Wired memory
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.
205 *
206 * Active memory
207 * This information is currently in RAM and actively being used.
208 *
209 * Inactive memory
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.
214 *
215 * Free memory
216 * This memory is not being used.
217 */
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,
225 "active", active,
226 "inactive", inactive,
227 "free", free);
228 /* #endif HAVE_HOST_STATISTICS */
230 #elif HAVE_SYSCTLBYNAME
231 /*
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
239 */
240 const char *sysctl_keys[8] =
241 {
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",
249 NULL
250 };
251 double sysctl_vals[8];
253 int i;
255 for (i = 0; sysctl_keys[i] != NULL; i++)
256 {
257 int value;
258 size_t value_len = sizeof (value);
260 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
261 NULL, 0) == 0)
262 {
263 sysctl_vals[i] = value;
264 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
265 }
266 else
267 {
268 sysctl_vals[i] = NAN;
269 }
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 */
284 #elif KERNEL_LINUX
285 FILE *fh;
286 char buffer[1024];
288 char *fields[8];
289 int numfields;
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)
303 {
304 char errbuf[1024];
305 WARNING ("memory: fopen: %s",
306 sstrerror (errno, errbuf, sizeof (errbuf)));
307 return (-1);
308 }
310 while (fgets (buffer, sizeof (buffer), fh) != NULL)
311 {
312 gauge_t *val = NULL;
314 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
315 val = &mem_total;
316 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
317 val = &mem_free;
318 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
319 val = &mem_buffered;
320 else if (strncasecmp (buffer, "Cached:", 7) == 0)
321 val = &mem_cached;
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;
327 }
328 else if (strncasecmp (buffer, "SUnreclaim:", 11) == 0) {
329 val = &mem_slab_unreclaimable;
330 detailed_slab_info = 1;
331 }
332 else
333 continue;
335 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
336 if (numfields < 2)
337 continue;
339 *val = 1024.0 * atof (fields[1]);
340 }
342 if (fclose (fh))
343 {
344 char errbuf[1024];
345 WARNING ("memory: fclose: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
347 }
349 if (mem_total < (mem_free + mem_buffered + mem_cached + mem_slab_total))
350 return (-1);
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
357 * if not. */
358 if (detailed_slab_info)
359 MEMORY_SUBMIT ("used", mem_used,
360 "buffered", mem_buffered,
361 "cached", mem_cached,
362 "free", mem_free,
363 "slab_unrecl", mem_slab_unreclaimable,
364 "slab_recl", mem_slab_reclaimable);
365 else
366 MEMORY_SUBMIT ("used", mem_used,
367 "buffered", mem_buffered,
368 "cached", mem_cached,
369 "free", mem_free,
370 "slab", mem_slab_total);
371 /* #endif KERNEL_LINUX */
373 #elif HAVE_LIBKSTAT
374 /* Most of the additions here were taken as-is from the k9toolkit from
375 * Brendan Gregg and are subject to change I guess */
376 long long mem_used;
377 long long mem_free;
378 long long mem_lock;
379 long long mem_kern;
380 long long mem_unus;
381 long long arcsize;
384 long long pp_kernel;
385 long long physmem;
386 long long availrmem;
388 if (ksp == NULL)
389 return (-1);
390 if (ksz == NULL)
391 return (-1);
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");
401 mem_kern = 0;
402 mem_unus = 0;
404 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
405 {
406 WARNING ("memory plugin: one of used, free or locked is negative.");
407 return (-1);
408 }
410 mem_unus = physmem - mem_used;
412 if (mem_used < (mem_free + mem_lock))
413 {
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 "
420 "swap space");
421 mem_free = availrmem;
422 mem_used = 0;
423 }
424 else
425 {
426 mem_used -= mem_free + mem_lock;
427 }
429 /* mem_kern is accounted for in mem_lock */
430 if (pp_kernel < mem_lock)
431 {
432 mem_kern = pp_kernel;
433 mem_lock -= pp_kernel;
434 }
435 else
436 {
437 mem_kern = mem_lock;
438 mem_lock = 0;
439 }
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;
446 mem_kern -= arcsize;
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 */
457 #elif HAVE_SYSCTL
458 int mib[] = {CTL_VM, VM_METER};
459 struct vmtotal vmtotal;
460 gauge_t mem_active;
461 gauge_t mem_inactive;
462 gauge_t mem_free;
463 size_t size;
465 memset (&vmtotal, 0, sizeof (vmtotal));
466 size = sizeof (vmtotal);
468 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
469 char errbuf[1024];
470 WARNING ("memory plugin: sysctl failed: %s",
471 sstrerror (errno, errbuf, sizeof (errbuf)));
472 return (-1);
473 }
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,
482 "free", mem_free);
483 /* #endif HAVE_SYSCTL */
485 #elif HAVE_LIBSTATGRAB
486 sg_mem_stats *ios;
488 ios = sg_get_mem_stats ();
489 if (ios == NULL)
490 return (-1);
492 MEMORY_SUBMIT ("used", (gauge_t) ios->used,
493 "cached", (gauge_t) ios->cache,
494 "free", (gauge_t) ios->free);
495 /* #endif HAVE_LIBSTATGRAB */
497 #elif HAVE_PERFSTAT
498 perfstat_memory_total_t pmemory;
500 memset (&pmemory, 0, sizeof (pmemory));
501 if (perfstat_memory_total(NULL, &pmemory, sizeof(pmemory), 1) < 0)
502 {
503 char errbuf[1024];
504 WARNING ("memory plugin: perfstat_memory_total failed: %s",
505 sstrerror (errno, errbuf, sizeof (errbuf)));
506 return (-1);
507 }
509 /* Unfortunately, the AIX documentation is not very clear on how these
510 * numbers relate to one another. The only thing is states explcitly
511 * is:
512 * real_total = real_process + real_free + numperm + real_system
513 *
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
518 */
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 */
525 return (0);
526 } /* }}} int memory_read_internal */
528 static int memory_read (void) /* {{{ */
529 {
530 value_t v[1];
531 value_list_t vl = VALUE_LIST_INIT;
533 vl.values = v;
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));
538 vl.time = cdtime ();
540 return (memory_read_internal (&vl));
541 } /* }}} int memory_read */
543 void module_register (void)
544 {
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 */