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 verplant.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
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
36 #endif
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
39 #endif
40 #ifdef HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
42 #endif
43 #ifdef HAVE_MACH_HOST_PRIV_H
44 # include <mach/host_priv.h>
45 #endif
46 #ifdef HAVE_MACH_VM_STATISTICS_H
47 # include <mach/vm_statistics.h>
48 #endif
50 #if HAVE_STATGRAB_H
51 # include <statgrab.h>
52 #endif
54 #if HAVE_PERFSTAT
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 */
69 #elif KERNEL_LINUX
70 /* no global variables */
71 /* #endif KERNEL_LINUX */
73 #elif HAVE_LIBKSTAT
74 static int pagesize;
75 static kstat_t *ksp;
76 /* #endif HAVE_LIBKSTAT */
78 #elif HAVE_SYSCTL
79 static int pagesize;
80 /* #endif HAVE_SYSCTL */
82 #elif HAVE_LIBSTATGRAB
83 /* no global variables */
84 /* endif HAVE_LIBSTATGRAB */
85 #elif HAVE_PERFSTAT
86 static int pagesize;
87 /* endif HAVE_PERFSTAT */
88 #else
89 # error "No applicable input method."
90 #endif
92 static _Bool values_absolute = 1;
93 static _Bool values_percentage = 0;
95 static int memory_config (oconfig_item_t *ci) /* {{{ */
96 {
97 int i;
99 for (i = 0; i < ci->children_num; i++)
100 {
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);
106 else
107 ERROR ("memory plugin: Invalid configuration option: "
108 "\"%s\".", child->key);
109 }
111 return (0);
112 } /* }}} int memory_config */
114 static int memory_init (void)
115 {
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
122 /* no init stuff */
123 /* #endif HAVE_SYSCTLBYNAME */
125 #elif defined(KERNEL_LINUX)
126 /* no init stuff */
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)
133 {
134 ksp = NULL;
135 return (-1);
136 }
137 /* #endif HAVE_LIBKSTAT */
139 #elif HAVE_SYSCTL
140 pagesize = getpagesize ();
141 if (pagesize <= 0)
142 {
143 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
144 return (-1);
145 }
146 /* #endif HAVE_SYSCTL */
148 #elif HAVE_LIBSTATGRAB
149 /* no init stuff */
150 /* #endif HAVE_LIBSTATGRAB */
152 #elif HAVE_PERFSTAT
153 pagesize = getpagesize ();
154 #endif /* HAVE_PERFSTAT */
155 return (0);
156 } /* int memory_init */
158 #define MEMORY_SUBMIT(...) do { \
159 if (values_absolute) \
160 plugin_dispatch_multivalue (vl, 0, __VA_ARGS__, NULL); \
161 if (values_percentage) \
162 plugin_dispatch_multivalue (vl, 1, __VA_ARGS__, NULL); \
163 } while (0)
165 static int memory_read_internal (value_list_t *vl)
166 {
167 #if HAVE_HOST_STATISTICS
168 kern_return_t status;
169 vm_statistics_data_t vm_data;
170 mach_msg_type_number_t vm_data_len;
172 gauge_t wired;
173 gauge_t active;
174 gauge_t inactive;
175 gauge_t free;
177 if (!port_host || !pagesize)
178 return (-1);
180 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
181 if ((status = host_statistics (port_host, HOST_VM_INFO,
182 (host_info_t) &vm_data,
183 &vm_data_len)) != KERN_SUCCESS)
184 {
185 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
186 return (-1);
187 }
189 /*
190 * From <http://docs.info.apple.com/article.html?artnum=107918>:
191 *
192 * Wired memory
193 * This information can't be cached to disk, so it must stay in RAM.
194 * The amount depends on what applications you are using.
195 *
196 * Active memory
197 * This information is currently in RAM and actively being used.
198 *
199 * Inactive memory
200 * This information is no longer being used and has been cached to
201 * disk, but it will remain in RAM until another application needs
202 * the space. Leaving this information in RAM is to your advantage if
203 * you (or a client of your computer) come back to it later.
204 *
205 * Free memory
206 * This memory is not being used.
207 */
209 wired = (gauge_t) (((uint64_t) vm_data.wire_count) * ((uint64_t) pagesize));
210 active = (gauge_t) (((uint64_t) vm_data.active_count) * ((uint64_t) pagesize));
211 inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
212 free = (gauge_t) (((uint64_t) vm_data.free_count) * ((uint64_t) pagesize));
214 MEMORY_SUBMIT ("wired", wired,
215 "active", active,
216 "inactive", inactive,
217 "free", free);
218 /* #endif HAVE_HOST_STATISTICS */
220 #elif HAVE_SYSCTLBYNAME
221 /*
222 * vm.stats.vm.v_page_size: 4096
223 * vm.stats.vm.v_page_count: 246178
224 * vm.stats.vm.v_free_count: 28760
225 * vm.stats.vm.v_wire_count: 37526
226 * vm.stats.vm.v_active_count: 55239
227 * vm.stats.vm.v_inactive_count: 113730
228 * vm.stats.vm.v_cache_count: 10809
229 */
230 char *sysctl_keys[8] =
231 {
232 "vm.stats.vm.v_page_size",
233 "vm.stats.vm.v_page_count",
234 "vm.stats.vm.v_free_count",
235 "vm.stats.vm.v_wire_count",
236 "vm.stats.vm.v_active_count",
237 "vm.stats.vm.v_inactive_count",
238 "vm.stats.vm.v_cache_count",
239 NULL
240 };
241 double sysctl_vals[8];
243 int i;
245 for (i = 0; sysctl_keys[i] != NULL; i++)
246 {
247 int value;
248 size_t value_len = sizeof (value);
250 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
251 NULL, 0) == 0)
252 {
253 sysctl_vals[i] = value;
254 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
255 }
256 else
257 {
258 sysctl_vals[i] = NAN;
259 }
260 } /* for (sysctl_keys) */
262 /* multiply all all page counts with the pagesize */
263 for (i = 1; sysctl_keys[i] != NULL; i++)
264 if (!isnan (sysctl_vals[i]))
265 sysctl_vals[i] *= sysctl_vals[0];
267 MEMORY_SUBMIT ("free", (gauge_t) sysctl_vals[2],
268 "wired", (gauge_t) sysctl_vals[3],
269 "active", (gauge_t) sysctl_vals[4],
270 "inactive", (gauge_t) sysctl_vals[5],
271 "cache", (gauge_t) sysctl_vals[6]);
272 /* #endif HAVE_SYSCTLBYNAME */
274 #elif KERNEL_LINUX
275 FILE *fh;
276 char buffer[1024];
278 char *fields[8];
279 int numfields;
281 gauge_t mem_total = 0;
282 gauge_t mem_used = 0;
283 gauge_t mem_buffered = 0;
284 gauge_t mem_cached = 0;
285 gauge_t mem_free = 0;
287 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
288 {
289 char errbuf[1024];
290 WARNING ("memory: fopen: %s",
291 sstrerror (errno, errbuf, sizeof (errbuf)));
292 return (-1);
293 }
295 while (fgets (buffer, sizeof (buffer), fh) != NULL)
296 {
297 gauge_t *val = NULL;
299 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
300 val = &mem_total;
301 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
302 val = &mem_free;
303 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
304 val = &mem_buffered;
305 else if (strncasecmp (buffer, "Cached:", 7) == 0)
306 val = &mem_cached;
307 else
308 continue;
310 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
311 if (numfields < 2)
312 continue;
314 *val = 1024.0 * atof (fields[1]);
315 }
317 if (fclose (fh))
318 {
319 char errbuf[1024];
320 WARNING ("memory: fclose: %s",
321 sstrerror (errno, errbuf, sizeof (errbuf)));
322 }
324 if (mem_total < (mem_free + mem_buffered + mem_cached))
325 return (-1);
327 mem_used = mem_total - (mem_free + mem_buffered + mem_cached);
328 MEMORY_SUBMIT ("used", mem_used,
329 "buffered", mem_buffered,
330 "cached", mem_cached,
331 "free", mem_free);
332 /* #endif KERNEL_LINUX */
334 #elif HAVE_LIBKSTAT
335 /* Most of the additions here were taken as-is from the k9toolkit from
336 * Brendan Gregg and are subject to change I guess */
337 long long mem_used;
338 long long mem_free;
339 long long mem_lock;
340 long long mem_kern;
341 long long mem_unus;
343 long long pp_kernel;
344 long long physmem;
345 long long availrmem;
347 if (ksp == NULL)
348 return (-1);
350 mem_used = get_kstat_value (ksp, "pagestotal");
351 mem_free = get_kstat_value (ksp, "pagesfree");
352 mem_lock = get_kstat_value (ksp, "pageslocked");
353 mem_kern = 0;
354 mem_unus = 0;
356 pp_kernel = get_kstat_value (ksp, "pp_kernel");
357 physmem = get_kstat_value (ksp, "physmem");
358 availrmem = get_kstat_value (ksp, "availrmem");
360 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
361 {
362 WARNING ("memory plugin: one of used, free or locked is negative.");
363 return (-1);
364 }
366 mem_unus = physmem - mem_used;
368 if (mem_used < (mem_free + mem_lock))
369 {
370 /* source: http://wesunsolve.net/bugid/id/4909199
371 * this seems to happen when swap space is small, e.g. 2G on a 32G system
372 * we will make some assumptions here
373 * educated solaris internals help welcome here */
374 DEBUG ("memory plugin: pages total is smaller than \"free\" "
375 "+ \"locked\". This is probably due to small "
376 "swap space");
377 mem_free = availrmem;
378 mem_used = 0;
379 }
380 else
381 {
382 mem_used -= mem_free + mem_lock;
383 }
385 /* mem_kern is accounted for in mem_lock */
386 if (pp_kernel < mem_lock)
387 {
388 mem_kern = pp_kernel;
389 mem_lock -= pp_kernel;
390 }
391 else
392 {
393 mem_kern = mem_lock;
394 mem_lock = 0;
395 }
397 mem_used *= pagesize; /* If this overflows you have some serious */
398 mem_free *= pagesize; /* memory.. Why not call me up and give me */
399 mem_lock *= pagesize; /* some? ;) */
400 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
401 mem_unus *= pagesize;
403 MEMORY_SUBMIT ("used", (gauge_t) mem_used,
404 "free", (gauge_t) mem_free,
405 "locked", (gauge_t) mem_lock,
406 "kernel", (gauge_t) mem_kern,
407 "unusable", (gauge_t) mem_unus);
408 /* #endif HAVE_LIBKSTAT */
410 #elif HAVE_SYSCTL
411 int mib[] = {CTL_VM, VM_METER};
412 struct vmtotal vmtotal;
413 gauge_t mem_active;
414 gauge_t mem_inactive;
415 gauge_t mem_free;
416 size_t size;
418 memset (&vmtotal, 0, sizeof (vmtotal));
419 size = sizeof (vmtotal);
421 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
422 char errbuf[1024];
423 WARNING ("memory plugin: sysctl failed: %s",
424 sstrerror (errno, errbuf, sizeof (errbuf)));
425 return (-1);
426 }
428 assert (pagesize > 0);
429 mem_active = (gauge_t) (vmtotal.t_arm * pagesize);
430 mem_inactive = (gauge_t) ((vmtotal.t_rm - vmtotal.t_arm) * pagesize);
431 mem_free = (gauge_t) (vmtotal.t_free * pagesize);
433 MEMORY_SUBMIT ("active", mem_active,
434 "inactive", mem_inactive,
435 "free", mem_free);
436 /* #endif HAVE_SYSCTL */
438 #elif HAVE_LIBSTATGRAB
439 sg_mem_stats *ios;
441 ios = sg_get_mem_stats ();
442 if (ios == NULL)
443 return (-1);
445 MEMORY_SUBMIT ("used", (gauge_t) ios->used,
446 "cached", (gauge_t) ios->cache,
447 "free", (gauge_t) ios->free);
448 /* #endif HAVE_LIBSTATGRAB */
450 #elif HAVE_PERFSTAT
451 perfstat_memory_total_t pmemory;
453 memset (&pmemory, 0, sizeof (pmemory));
454 if (perfstat_memory_total(NULL, &pmemory, sizeof(pmemory), 1) < 0)
455 {
456 char errbuf[1024];
457 WARNING ("memory plugin: perfstat_memory_total failed: %s",
458 sstrerror (errno, errbuf, sizeof (errbuf)));
459 return (-1);
460 }
462 /* Unfortunately, the AIX documentation is not very clear on how these
463 * numbers relate to one another. The only thing is states explcitly
464 * is:
465 * real_total = real_process + real_free + numperm + real_system
466 *
467 * Another segmentation, which would be closer to the numbers reported
468 * by the "svmon" utility, would be:
469 * real_total = real_free + real_inuse
470 * real_inuse = "active" + real_pinned + numperm
471 */
472 MEMORY_SUBMIT ("free", (gauge_t) (pmemory.real_free * pagesize),
473 "cached", (gauge_t) (pmemory.numperm * pagesize),
474 "system", (gauge_t) (pmemory.real_system * pagesize),
475 "user", (gauge_t) (pmemory.real_process * pagesize));
476 #endif /* HAVE_PERFSTAT */
478 return (0);
479 } /* }}} int memory_read_internal */
481 static int memory_read (void) /* {{{ */
482 {
483 value_t v[1];
484 value_list_t vl = VALUE_LIST_INIT;
486 vl.values = v;
487 vl.values_len = STATIC_ARRAY_SIZE (v);
488 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
489 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
490 sstrncpy (vl.type, "memory", sizeof (vl.type));
491 vl.time = cdtime ();
493 return (memory_read_internal (&vl));
494 } /* }}} int memory_read */
496 void module_register (void)
497 {
498 plugin_register_complex_config ("memory", memory_config);
499 plugin_register_init ("memory", memory_init);
500 plugin_register_read ("memory", memory_read);
501 } /* void module_register */