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
50 #include <sys/param.h>
53 #include <sys/sysctl.h>
56 #include <sys/dkstat.h>
67 #include <libperfstat.h>
68 #include <sys/protosw.h>
72 #define MAX(x, y) ((x) > (y) ? (x) : (y))
75 #define SWAP_HAVE_REPORT_BY_DEVICE 1
76 static derive_t pagesize;
77 static _Bool report_bytes = 0;
78 static _Bool report_by_device = 0;
79 /* #endif KERNEL_LINUX */
81 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
82 #define SWAP_HAVE_REPORT_BY_DEVICE 1
83 static derive_t pagesize;
84 static _Bool report_by_device = 0;
85 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
87 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
88 /* No global variables */
89 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
91 #elif defined(VM_SWAPUSAGE)
92 /* No global variables */
93 /* #endif defined(VM_SWAPUSAGE) */
95 #elif HAVE_LIBKVM_GETSWAPINFO
96 static kvm_t *kvm_obj = NULL;
98 /* #endif HAVE_LIBKVM_GETSWAPINFO */
100 #elif HAVE_LIBSTATGRAB
101 /* No global variables */
102 /* #endif HAVE_LIBSTATGRAB */
106 /*# endif HAVE_PERFSTAT */
109 #error "No applicable input method."
110 #endif /* HAVE_LIBSTATGRAB */
112 static _Bool values_absolute = 1;
113 static _Bool values_percentage = 0;
115 static int swap_config(oconfig_item_t *ci) /* {{{ */
117 for (int i = 0; i < ci->children_num; i++) {
118 oconfig_item_t *child = ci->children + i;
119 if (strcasecmp("ReportBytes", child->key) == 0)
121 cf_util_get_boolean(child, &report_bytes);
123 WARNING("swap plugin: The \"ReportBytes\" option "
124 "is only valid under Linux. "
125 "The option is going to be ignored.");
127 else if (strcasecmp("ReportByDevice", child->key) == 0)
128 #if SWAP_HAVE_REPORT_BY_DEVICE
129 cf_util_get_boolean(child, &report_by_device);
131 WARNING("swap plugin: The \"ReportByDevice\" option "
132 "is not supported on this platform. "
133 "The option is going to be ignored.");
134 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
135 else if (strcasecmp("ValuesAbsolute", child->key) == 0)
136 cf_util_get_boolean(child, &values_absolute);
137 else if (strcasecmp("ValuesPercentage", child->key) == 0)
138 cf_util_get_boolean(child, &values_percentage);
140 WARNING("swap plugin: Unknown config option: \"%s\"", child->key);
144 } /* }}} int swap_config */
146 static int swap_init(void) /* {{{ */
149 pagesize = (derive_t)sysconf(_SC_PAGESIZE);
150 /* #endif KERNEL_LINUX */
152 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
153 /* getpagesize(3C) tells me this does not fail.. */
154 pagesize = (derive_t)getpagesize();
155 /* #endif HAVE_SWAPCTL */
157 #elif defined(VM_SWAPUSAGE)
159 /* #endif defined(VM_SWAPUSAGE) */
161 #elif HAVE_LIBKVM_GETSWAPINFO
162 char errbuf[_POSIX2_LINE_MAX];
164 if (kvm_obj != NULL) {
169 kvm_pagesize = getpagesize();
171 kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
173 if (kvm_obj == NULL) {
174 ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
177 /* #endif HAVE_LIBKVM_GETSWAPINFO */
179 #elif HAVE_LIBSTATGRAB
181 /* #endif HAVE_LIBSTATGRAB */
184 pagesize = getpagesize();
185 #endif /* HAVE_PERFSTAT */
188 } /* }}} int swap_init */
190 static void swap_submit_usage(char const *plugin_instance, /* {{{ */
191 gauge_t used, gauge_t free,
192 char const *other_name, gauge_t other_value) {
193 value_list_t vl = VALUE_LIST_INIT;
195 vl.values = &(value_t){.gauge = NAN};
197 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
198 if (plugin_instance != NULL)
199 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
200 sstrncpy(vl.type, "swap", sizeof(vl.type));
203 plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free",
204 free, other_name, other_value, NULL);
205 if (values_percentage)
206 plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free",
207 free, other_name, other_value, NULL);
208 } /* }}} void swap_submit_usage */
210 #if KERNEL_LINUX || HAVE_PERFSTAT
211 __attribute__((nonnull(1))) static void
212 swap_submit_derive(char const *type_instance, /* {{{ */
214 value_list_t vl = VALUE_LIST_INIT;
216 vl.values = &(value_t){.derive = value};
218 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
219 sstrncpy(vl.type, "swap_io", sizeof(vl.type));
220 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
222 plugin_dispatch_values(&vl);
223 } /* }}} void swap_submit_derive */
227 static int swap_read_separate(void) /* {{{ */
232 fh = fopen("/proc/swaps", "r");
235 WARNING("swap plugin: fopen (/proc/swaps) failed: %s",
236 sstrerror(errno, errbuf, sizeof(errbuf)));
240 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
249 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
253 sstrncpy(path, fields[0], sizeof(path));
254 escape_slashes(path, sizeof(path));
258 total = strtod(fields[2], &endptr);
259 if ((endptr == fields[2]) || (errno != 0))
264 used = strtod(fields[3], &endptr);
265 if ((endptr == fields[3]) || (errno != 0))
271 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
277 } /* }}} int swap_read_separate */
279 static int swap_read_combined(void) /* {{{ */
284 gauge_t swap_used = NAN;
285 gauge_t swap_cached = NAN;
286 gauge_t swap_free = NAN;
287 gauge_t swap_total = NAN;
289 fh = fopen("/proc/meminfo", "r");
292 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s",
293 sstrerror(errno, errbuf, sizeof(errbuf)));
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) /* {{{ */
339 _Bool old_kernel = 0;
341 uint8_t have_data = 0;
342 derive_t swap_in = 0;
343 derive_t swap_out = 0;
345 fh = fopen("/proc/vmstat", "r");
347 /* /proc/vmstat does not exist in kernels <2.6 */
348 fh = fopen("/proc/stat", "r");
351 WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
357 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
361 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
367 if (strcasecmp("pswpin", fields[0]) == 0) {
368 strtoderive(fields[1], &swap_in);
370 } else if (strcasecmp("pswpout", fields[0]) == 0) {
371 strtoderive(fields[1], &swap_out);
374 } else /* if (old_kernel) */
379 if (strcasecmp("page", fields[0]) == 0) {
380 strtoderive(fields[1], &swap_in);
381 strtoderive(fields[2], &swap_out);
384 } /* while (fgets) */
388 if (have_data != 0x03)
392 swap_in = swap_in * pagesize;
393 swap_out = swap_out * pagesize;
396 swap_submit_derive("in", swap_in);
397 swap_submit_derive("out", swap_out);
400 } /* }}} int swap_read_io */
402 static int swap_read(void) /* {{{ */
404 if (report_by_device)
405 swap_read_separate();
407 swap_read_combined();
412 } /* }}} int swap_read */
413 /* #endif KERNEL_LINUX */
416 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
417 * and kstat. The former reads physical space used on a device, the latter
418 * reports the view from the virtual memory system. It was decided that the
419 * kstat-based information should be moved to the "vmem" plugin, but nobody
420 * with enough Solaris experience was available at that time to do this. The
421 * code below is still there for your reference but it won't be activated in
422 * *this* plugin again. --octo
424 #elif 0 && HAVE_LIBKSTAT
425 /* kstat-based read function */
426 static int swap_read_kstat(void) /* {{{ */
434 if (swapctl(SC_AINFO, &ai) == -1) {
436 ERROR("swap plugin: swapctl failed: %s",
437 sstrerror(errno, errbuf, sizeof(errbuf)));
443 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
445 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
446 * /usr/include/vm/anon.h
448 * In short, swap -s shows: allocated + reserved = used, available
450 * However, Solaris does not allow to allocated/reserved more than the
451 * available swap (physical memory + disk swap), so the pedant may
452 * prefer: allocated + unallocated = reserved, available
454 * We map the above to: used + resv = n/a, free
456 * Does your brain hurt yet? - Christophe Kalt
458 * Oh, and in case you wonder,
459 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
460 * can suffer from a 32bit overflow.
462 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
463 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
464 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
466 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
468 } /* }}} int swap_read_kstat */
469 /* #endif 0 && HAVE_LIBKSTAT */
471 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
472 /* swapctl-based read function */
473 static int swap_read(void) /* {{{ */
483 swap_num = swapctl(SC_GETNSWP, NULL);
485 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
487 } else if (swap_num == 0)
490 /* Allocate and initialize the swaptbl_t structure */
491 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
493 ERROR("swap plugin: malloc failed.");
497 /* Memory to store the path names. We only use these paths when the
498 * separate option has been configured, but it's easier to just
499 * allocate enough memory in any case. */
500 s_paths = calloc(swap_num, PATH_MAX);
501 if (s_paths == NULL) {
502 ERROR("swap plugin: calloc failed.");
506 for (int i = 0; i < swap_num; i++)
507 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
510 status = swapctl(SC_LIST, s);
513 ERROR("swap plugin: swapctl (SC_LIST) failed: %s",
514 sstrerror(errno, errbuf, sizeof(errbuf)));
518 } else if (swap_num < status) {
519 /* more elements returned than requested */
520 ERROR("swap plugin: I allocated memory for %i structure%s, "
521 "but swapctl(2) claims to have returned %i. "
522 "I'm confused and will give up.",
523 swap_num, (swap_num == 1) ? "" : "s", status);
527 } else if (swap_num > status)
528 /* less elements returned than requested */
531 for (int i = 0; i < swap_num; i++) {
536 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
539 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
540 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
542 /* Shortcut for the "combined" setting (default) */
543 if (!report_by_device) {
549 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
550 escape_slashes(path, sizeof(path));
552 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
553 } /* for (swap_num) */
557 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
564 /* If the "separate" option was specified (report_by_device == 1), all
565 * values have already been dispatched from within the loop. */
566 if (!report_by_device)
567 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
572 } /* }}} int swap_read */
573 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
575 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
576 static int swap_read(void) /* {{{ */
578 struct swapent *swap_entries;
585 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
587 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
589 } else if (swap_num == 0)
592 swap_entries = calloc(swap_num, sizeof(*swap_entries));
593 if (swap_entries == NULL) {
594 ERROR("swap plugin: calloc failed.");
598 status = swapctl(SWAP_STATS, swap_entries, swap_num);
599 if (status != swap_num) {
600 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
605 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
606 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
608 #define C_SWAP_BLOCK_SIZE 512.0
611 /* TODO: Report per-device stats. The path name is available from
612 * swap_entries[i].se_path */
613 for (int i = 0; i < swap_num; i++) {
614 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
617 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
618 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
623 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
629 swap_submit_usage(NULL, used, total - used, NULL, NAN);
633 } /* }}} int swap_read */
634 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
636 #elif defined(VM_SWAPUSAGE)
637 static int swap_read(void) /* {{{ */
641 struct xsw_usage sw_usage;
646 mib[1] = VM_SWAPUSAGE;
648 sw_usage_len = sizeof(struct xsw_usage);
650 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
653 /* The returned values are bytes. */
654 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
655 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
658 } /* }}} int swap_read */
659 /* #endif VM_SWAPUSAGE */
661 #elif HAVE_LIBKVM_GETSWAPINFO
662 static int swap_read(void) /* {{{ */
664 struct kvm_swap data_s;
673 /* only one structure => only get the grand total, no details */
674 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
678 total = (gauge_t)data_s.ksw_total;
679 used = (gauge_t)data_s.ksw_used;
681 total *= (gauge_t)kvm_pagesize;
682 used *= (gauge_t)kvm_pagesize;
684 swap_submit_usage(NULL, used, total - used, NULL, NAN);
687 } /* }}} int swap_read */
688 /* #endif HAVE_LIBKVM_GETSWAPINFO */
690 #elif HAVE_LIBSTATGRAB
691 static int swap_read(void) /* {{{ */
695 swap = sg_get_swap_stats();
699 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
702 } /* }}} int swap_read */
703 /* #endif HAVE_LIBSTATGRAB */
706 static int swap_read(void) /* {{{ */
708 perfstat_memory_total_t pmemory = {0};
716 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
719 WARNING("swap plugin: perfstat_memory_total failed: %s",
720 sstrerror(errno, errbuf, sizeof(errbuf)));
724 total = (gauge_t)(pmemory.pgsp_total * pagesize);
725 free = (gauge_t)(pmemory.pgsp_free * pagesize);
726 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
728 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
729 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
730 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
733 } /* }}} int swap_read */
734 #endif /* HAVE_PERFSTAT */
736 void module_register(void) {
737 plugin_register_complex_config("swap", swap_config);
738 plugin_register_init("swap", swap_init);
739 plugin_register_read("swap", swap_read);
740 } /* void module_register */