Debian Codegraph @

src/memory.c

raw
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 **/
25
26 #include "collectd.h"
27
28 #include "plugin.h"
29 #include "utils/common/common.h"
30
31 #if (defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)) || \
32 defined(__OpenBSD__)
33 /* Implies BSD variant */
34 #include <sys/sysctl.h>
35 #endif
36 #ifdef HAVE_SYS_VMMETER_H
37 #include <sys/vmmeter.h>
38 #endif
39
40 #ifdef HAVE_MACH_KERN_RETURN_H
41 #include <mach/kern_return.h>
42 #endif
43 #ifdef HAVE_MACH_MACH_INIT_H
44 #include <mach/mach_init.h>
45 #endif
46 #ifdef HAVE_MACH_MACH_HOST_H
47 #include <mach/mach_host.h>
48 #endif
49 #ifdef HAVE_MACH_HOST_PRIV_H
50 #include <mach/host_priv.h>
51 #endif
52 #ifdef HAVE_MACH_VM_STATISTICS_H
53 #include <mach/vm_statistics.h>
54 #endif
55
56 #if HAVE_STATGRAB_H
57 #include <statgrab.h>
58 #endif
59
60 #if HAVE_PERFSTAT
61 #include <libperfstat.h>
62 #include <sys/protosw.h>
63 #endif /* HAVE_PERFSTAT */
64
65 /* vm_statistics_data_t */
66 #if HAVE_HOST_STATISTICS
67 static mach_port_t port_host;
68 static vm_size_t pagesize;
69 /* #endif HAVE_HOST_STATISTICS */
70
71 #elif HAVE_SYSCTLBYNAME
72 #if HAVE_SYSCTL && defined(KERNEL_NETBSD)
73 static int pagesize;
74 #include <unistd.h> /* getpagesize() */
75 #else
76 /* no global variables */
77 #endif
78 /* #endif HAVE_SYSCTLBYNAME */
79
80 #elif KERNEL_LINUX
81 /* no global variables */
82 /* #endif KERNEL_LINUX */
83
84 #elif HAVE_LIBKSTAT
85 static int pagesize;
86 static kstat_t *ksp;
87 static kstat_t *ksz;
88 /* #endif HAVE_LIBKSTAT */
89
90 #elif HAVE_SYSCTL && __OpenBSD__
91 /* OpenBSD variant does not have sysctlbyname */
92 static int pagesize;
93 /* #endif HAVE_SYSCTL && __OpenBSD__ */
94
95 #elif HAVE_LIBSTATGRAB
96 /* no global variables */
97 /* endif HAVE_LIBSTATGRAB */
98 #elif HAVE_PERFSTAT
99 static int pagesize;
100 /* endif HAVE_PERFSTAT */
101 #else
102 #error "No applicable input method."
103 #endif
104
105 #if KERNEL_NETBSD
106 #include <uvm/uvm_extern.h>
107 #endif
108
109 static bool values_absolute = true;
110 static bool values_percentage;
111
112 static int memory_config(oconfig_item_t *ci) /* {{{ */
113 {
114 for (int i = 0; i < ci->children_num; i++) {
115 oconfig_item_t *child = ci->children + i;
116 if (strcasecmp("ValuesAbsolute", child->key) == 0)
117 cf_util_get_boolean(child, &values_absolute);
118 else if (strcasecmp("ValuesPercentage", child->key) == 0)
119 cf_util_get_boolean(child, &values_percentage);
120 else
121 ERROR("memory plugin: Invalid configuration option: "
122 "\"%s\".",
123 child->key);
124 }
125
126 return 0;
127 } /* }}} int memory_config */
128
129 static int memory_init(void) {
130 #if HAVE_HOST_STATISTICS
131 port_host = mach_host_self();
132 host_page_size(port_host, &pagesize);
133 /* #endif HAVE_HOST_STATISTICS */
134
135 #elif HAVE_SYSCTLBYNAME
136 #if HAVE_SYSCTL && defined(KERNEL_NETBSD)
137 pagesize = getpagesize();
138 #else
139 /* no init stuff */
140 #endif /* HAVE_SYSCTL && defined(KERNEL_NETBSD) */
141 /* #endif HAVE_SYSCTLBYNAME */
142
143 #elif defined(KERNEL_LINUX)
144 /* no init stuff */
145 /* #endif KERNEL_LINUX */
146
147 #elif defined(HAVE_LIBKSTAT)
148 /* getpagesize(3C) tells me this does not fail.. */
149 pagesize = getpagesize();
150 if (get_kstat(&ksp, "unix", 0, "system_pages") != 0) {
151 ksp = NULL;
152 return -1;
153 }
154 if (get_kstat(&ksz, "zfs", 0, "arcstats") != 0) {
155 ksz = NULL;
156 return -1;
157 }
158
159 /* #endif HAVE_LIBKSTAT */
160
161 #elif HAVE_SYSCTL && __OpenBSD__
162 /* OpenBSD variant does not have sysctlbyname */
163 pagesize = getpagesize();
164 if (pagesize <= 0) {
165 ERROR("memory plugin: Invalid pagesize: %i", pagesize);
166 return -1;
167 }
168 /* #endif HAVE_SYSCTL && __OpenBSD__ */
169
170 #elif HAVE_LIBSTATGRAB
171 /* no init stuff */
172 /* #endif HAVE_LIBSTATGRAB */
173
174 #elif HAVE_PERFSTAT
175 pagesize = getpagesize();
176 #endif /* HAVE_PERFSTAT */
177 return 0;
178 } /* int memory_init */
179
180 #define MEMORY_SUBMIT(...) \
181 do { \
182 if (values_absolute) \
183 plugin_dispatch_multivalue(vl, false, DS_TYPE_GAUGE, __VA_ARGS__, NULL); \
184 if (values_percentage) \
185 plugin_dispatch_multivalue(vl, true, DS_TYPE_GAUGE, __VA_ARGS__, NULL); \
186 } while (0)
187
188 static int memory_read_internal(value_list_t *vl) {
189 #if HAVE_HOST_STATISTICS
190 kern_return_t status;
191 vm_statistics_data_t vm_data;
192 mach_msg_type_number_t vm_data_len;
193
194 gauge_t wired;
195 gauge_t active;
196 gauge_t inactive;
197 gauge_t free;
198
199 if (!port_host || !pagesize)
200 return -1;
201
202 vm_data_len = sizeof(vm_data) / sizeof(natural_t);
203 if ((status = host_statistics(port_host, HOST_VM_INFO, (host_info_t)&vm_data,
204 &vm_data_len)) != KERN_SUCCESS) {
205 ERROR("memory-plugin: host_statistics failed and returned the value %i",
206 (int)status);
207 return -1;
208 }
209
210 /*
211 * From <http://docs.info.apple.com/article.html?artnum=107918>:
212 *
213 * Wired memory
214 * This information can't be cached to disk, so it must stay in RAM.
215 * The amount depends on what applications you are using.
216 *
217 * Active memory
218 * This information is currently in RAM and actively being used.
219 *
220 * Inactive memory
221 * This information is no longer being used and has been cached to
222 * disk, but it will remain in RAM until another application needs
223 * the space. Leaving this information in RAM is to your advantage if
224 * you (or a client of your computer) come back to it later.
225 *
226 * Free memory
227 * This memory is not being used.
228 */
229
230 wired = (gauge_t)(((uint64_t)vm_data.wire_count) * ((uint64_t)pagesize));
231 active = (gauge_t)(((uint64_t)vm_data.active_count) * ((uint64_t)pagesize));
232 inactive =
233 (gauge_t)(((uint64_t)vm_data.inactive_count) * ((uint64_t)pagesize));
234 free = (gauge_t)(((uint64_t)vm_data.free_count) * ((uint64_t)pagesize));
235
236 MEMORY_SUBMIT("wired", wired, "active", active, "inactive", inactive, "free",
237 free);
238 /* #endif HAVE_HOST_STATISTICS */
239
240 #elif HAVE_SYSCTLBYNAME
241
242 #if HAVE_SYSCTL && defined(KERNEL_NETBSD)
243 int mib[] = {CTL_VM, VM_UVMEXP2};
244 struct uvmexp_sysctl uvmexp;
245 gauge_t mem_active;
246 gauge_t mem_inactive;
247 gauge_t mem_free;
248 gauge_t mem_wired;
249 gauge_t mem_kernel;
250 size_t size;
251
252 memset(&uvmexp, 0, sizeof(uvmexp));
253 size = sizeof(uvmexp);
254
255 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
256 char errbuf[1024];
257 WARNING("memory plugin: sysctl failed: %s",
258 sstrerror(errno, errbuf, sizeof(errbuf)));
259 return (-1);
260 }
261
262 assert(pagesize > 0);
263 mem_active = (gauge_t)(uvmexp.active * pagesize);
264 mem_inactive = (gauge_t)(uvmexp.inactive * pagesize);
265 mem_free = (gauge_t)(uvmexp.free * pagesize);
266 mem_wired = (gauge_t)(uvmexp.wired * pagesize);
267 mem_kernel = (gauge_t)((uvmexp.npages - (uvmexp.active + uvmexp.inactive +
268 uvmexp.free + uvmexp.wired)) *
269 pagesize);
270
271 MEMORY_SUBMIT("active", mem_active, "inactive", mem_inactive, "free",
272 mem_free, "wired", mem_wired, "kernel", mem_kernel);
273 /* #endif HAVE_SYSCTL && defined(KERNEL_NETBSD) */
274
275 #else /* Other HAVE_SYSCTLBYNAME providers */
276 /*
277 * vm.stats.vm.v_page_size: 4096
278 * vm.stats.vm.v_page_count: 246178
279 * vm.stats.vm.v_free_count: 28760
280 * vm.stats.vm.v_wire_count: 37526
281 * vm.stats.vm.v_active_count: 55239
282 * vm.stats.vm.v_inactive_count: 113730
283 * vm.stats.vm.v_cache_count: 10809
284 */
285 const char *sysctl_keys[8] = {
286 "vm.stats.vm.v_page_size", "vm.stats.vm.v_page_count",
287 "vm.stats.vm.v_free_count", "vm.stats.vm.v_wire_count",
288 "vm.stats.vm.v_active_count", "vm.stats.vm.v_inactive_count",
289 "vm.stats.vm.v_cache_count", NULL};
290 double sysctl_vals[8];
291
292 for (int i = 0; sysctl_keys[i] != NULL; i++) {
293 int value;
294 size_t value_len = sizeof(value);
295
296 if (sysctlbyname(sysctl_keys[i], (void *)&value, &value_len, NULL, 0) ==
297 0) {
298 sysctl_vals[i] = value;
299 DEBUG("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
300 } else {
301 sysctl_vals[i] = NAN;
302 }
303 } /* for (sysctl_keys) */
304
305 /* multiply all all page counts with the pagesize */
306 for (int i = 1; sysctl_keys[i] != NULL; i++)
307 if (!isnan(sysctl_vals[i]))
308 sysctl_vals[i] *= sysctl_vals[0];
309
310 MEMORY_SUBMIT("free", (gauge_t)sysctl_vals[2], "wired",
311 (gauge_t)sysctl_vals[3], "active", (gauge_t)sysctl_vals[4],
312 "inactive", (gauge_t)sysctl_vals[5], "cache",
313 (gauge_t)sysctl_vals[6]);
314
315 #endif /* HAVE_SYSCTL && KERNEL_NETBSD */
316 /* #endif HAVE_SYSCTLBYNAME */
317
318 #elif KERNEL_LINUX
319 FILE *fh;
320 char buffer[1024];
321
322 char *fields[8];
323 int numfields;
324
325 bool detailed_slab_info = false;
326
327 gauge_t mem_total = 0;
328 gauge_t mem_used = 0;
329 gauge_t mem_buffered = 0;
330 gauge_t mem_cached = 0;
331 gauge_t mem_free = 0;
332 gauge_t mem_slab_total = 0;
333 gauge_t mem_slab_reclaimable = 0;
334 gauge_t mem_slab_unreclaimable = 0;
335
336 if ((fh = fopen("/proc/meminfo", "r")) == NULL) {
337 WARNING("memory: fopen: %s", STRERRNO);
338 return -1;
339 }
340
341 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
342 gauge_t *val = NULL;
343
344 if (strncasecmp(buffer, "MemTotal:", 9) == 0)
345 val = &mem_total;
346 else if (strncasecmp(buffer, "MemFree:", 8) == 0)
347 val = &mem_free;
348 else if (strncasecmp(buffer, "Buffers:", 8) == 0)
349 val = &mem_buffered;
350 else if (strncasecmp(buffer, "Cached:", 7) == 0)
351 val = &mem_cached;
352 else if (strncasecmp(buffer, "Slab:", 5) == 0)
353 val = &mem_slab_total;
354 else if (strncasecmp(buffer, "SReclaimable:", 13) == 0) {
355 val = &mem_slab_reclaimable;
356 detailed_slab_info = true;
357 } else if (strncasecmp(buffer, "SUnreclaim:", 11) == 0) {
358 val = &mem_slab_unreclaimable;
359 detailed_slab_info = true;
360 } else
361 continue;
362
363 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
364 if (numfields < 2)
365 continue;
366
367 *val = 1024.0 * atof(fields[1]);
368 }
369
370 if (fclose(fh)) {
371 WARNING("memory: fclose: %s", STRERRNO);
372 }
373
374 if (mem_total < (mem_free + mem_buffered + mem_cached + mem_slab_total))
375 return -1;
376
377 mem_used =
378 mem_total - (mem_free + mem_buffered + mem_cached + mem_slab_total);
379
380 /* SReclaimable and SUnreclaim were introduced in kernel 2.6.19
381 * They sum up to the value of Slab, which is available on older & newer
382 * kernels. So SReclaimable/SUnreclaim are submitted if available, and Slab
383 * if not. */
384 if (detailed_slab_info)
385 MEMORY_SUBMIT("used", mem_used, "buffered", mem_buffered, "cached",
386 mem_cached, "free", mem_free, "slab_unrecl",
387 mem_slab_unreclaimable, "slab_recl", mem_slab_reclaimable);
388 else
389 MEMORY_SUBMIT("used", mem_used, "buffered", mem_buffered, "cached",
390 mem_cached, "free", mem_free, "slab", mem_slab_total);
391 /* #endif KERNEL_LINUX */
392
393 #elif HAVE_LIBKSTAT
394 /* Most of the additions here were taken as-is from the k9toolkit from
395 * Brendan Gregg and are subject to change I guess */
396 long long mem_used;
397 long long mem_free;
398 long long mem_lock;
399 long long mem_kern;
400 long long mem_unus;
401 long long arcsize;
402
403 long long pp_kernel;
404 long long physmem;
405 long long availrmem;
406
407 if (ksp == NULL)
408 return -1;
409 if (ksz == NULL)
410 return -1;
411
412 mem_used = get_kstat_value(ksp, "pagestotal");
413 mem_free = get_kstat_value(ksp, "pagesfree");
414 mem_lock = get_kstat_value(ksp, "pageslocked");
415 arcsize = get_kstat_value(ksz, "size");
416 pp_kernel = get_kstat_value(ksp, "pp_kernel");
417 physmem = get_kstat_value(ksp, "physmem");
418 availrmem = get_kstat_value(ksp, "availrmem");
419
420 mem_kern = 0;
421 mem_unus = 0;
422
423 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL)) {
424 WARNING("memory plugin: one of used, free or locked is negative.");
425 return -1;
426 }
427
428 mem_unus = physmem - mem_used;
429
430 if (mem_used < (mem_free + mem_lock)) {
431 /* source: http://wesunsolve.net/bugid/id/4909199
432 * this seems to happen when swap space is small, e.g. 2G on a 32G system
433 * we will make some assumptions here
434 * educated solaris internals help welcome here */
435 DEBUG("memory plugin: pages total is smaller than \"free\" "
436 "+ \"locked\". This is probably due to small "
437 "swap space");
438 mem_free = availrmem;
439 mem_used = 0;
440 } else {
441 mem_used -= mem_free + mem_lock;
442 }
443
444 /* mem_kern is accounted for in mem_lock */
445 if (pp_kernel < mem_lock) {
446 mem_kern = pp_kernel;
447 mem_lock -= pp_kernel;
448 } else {
449 mem_kern = mem_lock;
450 mem_lock = 0;
451 }
452
453 mem_used *= pagesize; /* If this overflows you have some serious */
454 mem_free *= pagesize; /* memory.. Why not call me up and give me */
455 mem_lock *= pagesize; /* some? ;) */
456 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
457 mem_unus *= pagesize;
458 mem_kern -= arcsize;
459
460 MEMORY_SUBMIT("used", (gauge_t)mem_used, "free", (gauge_t)mem_free, "locked",
461 (gauge_t)mem_lock, "kernel", (gauge_t)mem_kern, "arc",
462 (gauge_t)arcsize, "unusable", (gauge_t)mem_unus);
463 /* #endif HAVE_LIBKSTAT */
464
465 #elif HAVE_SYSCTL && __OpenBSD__
466 /* OpenBSD variant does not have HAVE_SYSCTLBYNAME */
467 int mib[] = {CTL_VM, VM_METER};
468 struct vmtotal vmtotal = {0};
469 gauge_t mem_active;
470 gauge_t mem_inactive;
471 gauge_t mem_free;
472 size_t size;
473
474 size = sizeof(vmtotal);
475
476 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
477 WARNING("memory plugin: sysctl failed: %s", STRERRNO);
478 return -1;
479 }
480
481 assert(pagesize > 0);
482 mem_active = (gauge_t)(vmtotal.t_arm * pagesize);
483 mem_inactive = (gauge_t)((vmtotal.t_rm - vmtotal.t_arm) * pagesize);
484 mem_free = (gauge_t)(vmtotal.t_free * pagesize);
485
486 MEMORY_SUBMIT("active", mem_active, "inactive", mem_inactive, "free",
487 mem_free);
488 /* #endif HAVE_SYSCTL && __OpenBSD__ */
489
490 #elif HAVE_LIBSTATGRAB
491 sg_mem_stats *ios;
492
493 ios = sg_get_mem_stats();
494 if (ios == NULL)
495 return -1;
496
497 MEMORY_SUBMIT("used", (gauge_t)ios->used, "cached", (gauge_t)ios->cache,
498 "free", (gauge_t)ios->free);
499 /* #endif HAVE_LIBSTATGRAB */
500
501 #elif HAVE_PERFSTAT
502 perfstat_memory_total_t pmemory = {0};
503
504 if (perfstat_memory_total(NULL, &pmemory, sizeof(pmemory), 1) < 0) {
505 WARNING("memory plugin: perfstat_memory_total failed: %s", STRERRNO);
506 return -1;
507 }
508
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), "cached",
520 (gauge_t)(pmemory.numperm * pagesize), "system",
521 (gauge_t)(pmemory.real_system * pagesize), "user",
522 (gauge_t)(pmemory.real_process * pagesize));
523 #endif /* HAVE_PERFSTAT */
524
525 return 0;
526 } /* }}} int memory_read_internal */
527
528 static int memory_read(void) /* {{{ */
529 {
530 value_t v[1];
531 value_list_t vl = VALUE_LIST_INIT;
532
533 vl.values = v;
534 vl.values_len = STATIC_ARRAY_SIZE(v);
535 sstrncpy(vl.plugin, "memory", sizeof(vl.plugin));
536 sstrncpy(vl.type, "memory", sizeof(vl.type));
537 vl.time = cdtime();
538
539 return memory_read_internal(&vl);
540 } /* }}} int memory_read */
541
542 void module_register(void) {
543 plugin_register_complex_config("memory", memory_config);
544 plugin_register_init("memory", memory_init);
545 plugin_register_read("memory", memory_read);
546 } /* void module_register */