2 * collectd - src/swap.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
5 * Copyright (C) 2009 Manuel Sanmartin
6 * Copyright (C) 2010 Aurélien Reynaud
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Florian octo Forster <octo at collectd.org>
24 * Aurélien Reynaud <collectd at wattapower.net>
31 /* avoid swap.h error "Cannot use swapctl in the large files compilation
33 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
34 #undef _FILE_OFFSET_BITS
35 #undef _LARGEFILE64_SOURCE
41 #include "utils/common/common.h"
50 #include <sys/param.h>
52 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
53 /* implies BSD variant */
54 #include <sys/sysctl.h>
57 #include <sys/dkstat.h>
68 #include <libperfstat.h>
69 #include <sys/protosw.h>
73 #define MAX(x, y) ((x) > (y) ? (x) : (y))
76 #define SWAP_HAVE_REPORT_BY_DEVICE 1
77 static derive_t pagesize;
78 static bool report_bytes;
79 static bool report_by_device;
80 /* #endif KERNEL_LINUX */
82 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
83 #define SWAP_HAVE_REPORT_BY_DEVICE 1
84 static derive_t pagesize;
85 static bool report_by_device;
86 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
88 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
89 /* No global variables */
90 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
92 #elif defined(VM_SWAPUSAGE)
93 /* No global variables */
94 /* #endif defined(VM_SWAPUSAGE) */
96 #elif HAVE_LIBKVM_GETSWAPINFO
97 static kvm_t *kvm_obj;
99 /* #endif HAVE_LIBKVM_GETSWAPINFO */
101 #elif HAVE_LIBSTATGRAB
102 /* No global variables */
103 /* #endif HAVE_LIBSTATGRAB */
107 /*# endif HAVE_PERFSTAT */
110 #error "No applicable input method."
111 #endif /* HAVE_LIBSTATGRAB */
113 static bool values_absolute = true;
114 static bool values_percentage;
115 static bool report_io = true;
117 static int swap_config(oconfig_item_t *ci) /* {{{ */
119 for (int i = 0; i < ci->children_num; i++) {
120 oconfig_item_t *child = ci->children + i;
121 if (strcasecmp("ReportBytes", child->key) == 0)
123 cf_util_get_boolean(child, &report_bytes);
125 WARNING("swap plugin: The \"ReportBytes\" option "
126 "is only valid under Linux. "
127 "The option is going to be ignored.");
129 else if (strcasecmp("ReportByDevice", child->key) == 0)
130 #if SWAP_HAVE_REPORT_BY_DEVICE
131 cf_util_get_boolean(child, &report_by_device);
133 WARNING("swap plugin: The \"ReportByDevice\" option "
134 "is not supported on this platform. "
135 "The option is going to be ignored.");
136 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
137 else if (strcasecmp("ValuesAbsolute", child->key) == 0)
138 cf_util_get_boolean(child, &values_absolute);
139 else if (strcasecmp("ValuesPercentage", child->key) == 0)
140 cf_util_get_boolean(child, &values_percentage);
141 else if (strcasecmp("ReportIO", child->key) == 0)
142 cf_util_get_boolean(child, &report_io);
144 WARNING("swap plugin: Unknown config option: \"%s\"", child->key);
148 } /* }}} int swap_config */
150 static int swap_init(void) /* {{{ */
153 pagesize = (derive_t)sysconf(_SC_PAGESIZE);
154 /* #endif KERNEL_LINUX */
156 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
157 /* getpagesize(3C) tells me this does not fail.. */
158 pagesize = (derive_t)getpagesize();
159 /* #endif HAVE_SWAPCTL */
161 #elif defined(VM_SWAPUSAGE)
163 /* #endif defined(VM_SWAPUSAGE) */
165 #elif HAVE_LIBKVM_GETSWAPINFO
166 char errbuf[_POSIX2_LINE_MAX];
168 if (kvm_obj != NULL) {
173 kvm_pagesize = getpagesize();
175 kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
177 if (kvm_obj == NULL) {
178 ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
181 /* #endif HAVE_LIBKVM_GETSWAPINFO */
183 #elif HAVE_LIBSTATGRAB
185 /* #endif HAVE_LIBSTATGRAB */
188 pagesize = getpagesize();
189 #endif /* HAVE_PERFSTAT */
192 } /* }}} int swap_init */
194 static void swap_submit_usage(char const *plugin_instance, /* {{{ */
195 gauge_t used, gauge_t free,
196 char const *other_name, gauge_t other_value) {
197 value_list_t vl = VALUE_LIST_INIT;
199 vl.values = &(value_t){.gauge = NAN};
201 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
202 if (plugin_instance != NULL)
203 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
204 sstrncpy(vl.type, "swap", sizeof(vl.type));
207 plugin_dispatch_multivalue(&vl, false, DS_TYPE_GAUGE, "used", used, "free",
208 free, other_name, other_value, NULL);
209 if (values_percentage)
210 plugin_dispatch_multivalue(&vl, true, DS_TYPE_GAUGE, "used", used, "free",
211 free, other_name, other_value, NULL);
212 } /* }}} void swap_submit_usage */
214 #if KERNEL_LINUX || HAVE_PERFSTAT
215 __attribute__((nonnull(1))) static void
216 swap_submit_derive(char const *type_instance, /* {{{ */
218 value_list_t vl = VALUE_LIST_INIT;
220 vl.values = &(value_t){.derive = value};
222 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
223 sstrncpy(vl.type, "swap_io", sizeof(vl.type));
224 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
226 plugin_dispatch_values(&vl);
227 } /* }}} void swap_submit_derive */
231 static int swap_read_separate(void) /* {{{ */
236 fh = fopen("/proc/swaps", "r");
238 WARNING("swap plugin: fopen (/proc/swaps) failed: %s", STRERRNO);
242 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
251 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
255 sstrncpy(path, fields[0], sizeof(path));
256 escape_slashes(path, sizeof(path));
260 total = strtod(fields[2], &endptr);
261 if ((endptr == fields[2]) || (errno != 0))
266 used = strtod(fields[3], &endptr);
267 if ((endptr == fields[3]) || (errno != 0))
273 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
279 } /* }}} int swap_read_separate */
281 static int swap_read_combined(void) /* {{{ */
286 gauge_t swap_used = NAN;
287 gauge_t swap_cached = NAN;
288 gauge_t swap_free = NAN;
289 gauge_t swap_total = NAN;
291 fh = fopen("/proc/meminfo", "r");
293 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s", STRERRNO);
297 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
301 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
305 if (strcasecmp(fields[0], "SwapTotal:") == 0)
306 strtogauge(fields[1], &swap_total);
307 else if (strcasecmp(fields[0], "SwapFree:") == 0)
308 strtogauge(fields[1], &swap_free);
309 else if (strcasecmp(fields[0], "SwapCached:") == 0)
310 strtogauge(fields[1], &swap_cached);
315 if (isnan(swap_total) || isnan(swap_free))
318 /* Some systems, OpenVZ for example, don't provide SwapCached. */
319 if (isnan(swap_cached))
320 swap_used = swap_total - swap_free;
322 swap_used = swap_total - (swap_free + swap_cached);
323 assert(!isnan(swap_used));
328 swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
329 isnan(swap_cached) ? NULL : "cached",
330 isnan(swap_cached) ? NAN : swap_cached * 1024.0);
332 } /* }}} int swap_read_combined */
334 static int swap_read_io(void) /* {{{ */
338 uint8_t have_data = 0;
339 derive_t swap_in = 0;
340 derive_t swap_out = 0;
342 FILE *fh = fopen("/proc/vmstat", "r");
344 WARNING("swap: fopen(/proc/vmstat): %s", STRERRNO);
348 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
350 int numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
355 if (strcasecmp("pswpin", fields[0]) == 0) {
356 strtoderive(fields[1], &swap_in);
358 } else if (strcasecmp("pswpout", fields[0]) == 0) {
359 strtoderive(fields[1], &swap_out);
362 } /* while (fgets) */
366 if (have_data != 0x03)
370 swap_in = swap_in * pagesize;
371 swap_out = swap_out * pagesize;
374 swap_submit_derive("in", swap_in);
375 swap_submit_derive("out", swap_out);
378 } /* }}} int swap_read_io */
380 static int swap_read(void) /* {{{ */
382 if (report_by_device)
383 swap_read_separate();
385 swap_read_combined();
391 } /* }}} int swap_read */
392 /* #endif KERNEL_LINUX */
395 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
396 * and kstat. The former reads physical space used on a device, the latter
397 * reports the view from the virtual memory system. It was decided that the
398 * kstat-based information should be moved to the "vmem" plugin, but nobody
399 * with enough Solaris experience was available at that time to do this. The
400 * code below is still there for your reference but it won't be activated in
401 * *this* plugin again. --octo
403 #elif 0 && HAVE_LIBKSTAT
404 /* kstat-based read function */
405 static int swap_read_kstat(void) /* {{{ */
413 if (swapctl(SC_AINFO, &ai) == -1) {
414 ERROR("swap plugin: swapctl failed: %s", STRERRNO);
420 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
422 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
423 * /usr/include/vm/anon.h
425 * In short, swap -s shows: allocated + reserved = used, available
427 * However, Solaris does not allow to allocated/reserved more than the
428 * available swap (physical memory + disk swap), so the pedant may
429 * prefer: allocated + unallocated = reserved, available
431 * We map the above to: used + resv = n/a, free
433 * Does your brain hurt yet? - Christophe Kalt
435 * Oh, and in case you wonder,
436 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
437 * can suffer from a 32bit overflow.
439 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
440 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
441 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
443 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
445 } /* }}} int swap_read_kstat */
446 /* #endif 0 && HAVE_LIBKSTAT */
448 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
449 /* swapctl-based read function */
450 static int swap_read(void) /* {{{ */
460 swap_num = swapctl(SC_GETNSWP, NULL);
462 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
464 } else if (swap_num == 0)
467 /* Allocate and initialize the swaptbl_t structure */
468 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
470 ERROR("swap plugin: malloc failed.");
474 /* Memory to store the path names. We only use these paths when the
475 * separate option has been configured, but it's easier to just
476 * allocate enough memory in any case. */
477 s_paths = calloc(swap_num, PATH_MAX);
478 if (s_paths == NULL) {
479 ERROR("swap plugin: calloc failed.");
483 for (int i = 0; i < swap_num; i++)
484 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
487 status = swapctl(SC_LIST, s);
489 ERROR("swap plugin: swapctl (SC_LIST) failed: %s", STRERRNO);
493 } else if (swap_num < status) {
494 /* more elements returned than requested */
495 ERROR("swap plugin: I allocated memory for %i structure%s, "
496 "but swapctl(2) claims to have returned %i. "
497 "I'm confused and will give up.",
498 swap_num, (swap_num == 1) ? "" : "s", status);
502 } else if (swap_num > status)
503 /* less elements returned than requested */
506 for (int i = 0; i < swap_num; i++) {
511 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
514 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
515 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
517 /* Shortcut for the "combined" setting (default) */
518 if (!report_by_device) {
524 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
525 escape_slashes(path, sizeof(path));
527 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
528 } /* for (swap_num) */
532 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
539 /* If the "separate" option was specified (report_by_device == true) all
540 * values have already been dispatched from within the loop. */
541 if (!report_by_device)
542 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
547 } /* }}} int swap_read */
548 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
550 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
551 static int swap_read(void) /* {{{ */
553 struct swapent *swap_entries;
560 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
562 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
564 } else if (swap_num == 0)
567 swap_entries = calloc(swap_num, sizeof(*swap_entries));
568 if (swap_entries == NULL) {
569 ERROR("swap plugin: calloc failed.");
573 status = swapctl(SWAP_STATS, swap_entries, swap_num);
574 if (status != swap_num) {
575 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
580 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
581 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
583 #define C_SWAP_BLOCK_SIZE 512.0
586 /* TODO: Report per-device stats. The path name is available from
587 * swap_entries[i].se_path */
588 for (int i = 0; i < swap_num; i++) {
589 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
592 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
593 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
598 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
604 swap_submit_usage(NULL, used, total - used, NULL, NAN);
608 } /* }}} int swap_read */
609 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
611 #elif defined(VM_SWAPUSAGE)
612 static int swap_read(void) /* {{{ */
616 struct xsw_usage sw_usage;
621 mib[1] = VM_SWAPUSAGE;
623 sw_usage_len = sizeof(struct xsw_usage);
625 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
628 /* The returned values are bytes. */
629 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
630 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
633 } /* }}} int swap_read */
634 /* #endif VM_SWAPUSAGE */
636 #elif HAVE_LIBKVM_GETSWAPINFO
637 static int swap_read(void) /* {{{ */
639 struct kvm_swap data_s;
648 /* only one structure => only get the grand total, no details */
649 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
653 total = (gauge_t)data_s.ksw_total;
654 used = (gauge_t)data_s.ksw_used;
656 total *= (gauge_t)kvm_pagesize;
657 used *= (gauge_t)kvm_pagesize;
659 swap_submit_usage(NULL, used, total - used, NULL, NAN);
662 } /* }}} int swap_read */
663 /* #endif HAVE_LIBKVM_GETSWAPINFO */
665 #elif HAVE_LIBSTATGRAB
666 static int swap_read(void) /* {{{ */
670 swap = sg_get_swap_stats();
674 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
677 } /* }}} int swap_read */
678 /* #endif HAVE_LIBSTATGRAB */
681 static int swap_read(void) /* {{{ */
683 perfstat_memory_total_t pmemory = {0};
691 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
693 WARNING("swap plugin: perfstat_memory_total failed: %s", STRERRNO);
697 total = (gauge_t)(pmemory.pgsp_total * pagesize);
698 free = (gauge_t)(pmemory.pgsp_free * pagesize);
699 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
701 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
704 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
705 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
709 } /* }}} int swap_read */
710 #endif /* HAVE_PERFSTAT */
712 void module_register(void) {
713 plugin_register_complex_config("swap", swap_config);
714 plugin_register_init("swap", swap_init);
715 plugin_register_read("swap", swap_read);
716 } /* void module_register */