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;
114 static _Bool report_io = 1;
116 static int swap_config(oconfig_item_t *ci) /* {{{ */
118 for (int i = 0; i < ci->children_num; i++) {
119 oconfig_item_t *child = ci->children + i;
120 if (strcasecmp("ReportBytes", child->key) == 0)
122 cf_util_get_boolean(child, &report_bytes);
124 WARNING("swap plugin: The \"ReportBytes\" option "
125 "is only valid under Linux. "
126 "The option is going to be ignored.");
128 else if (strcasecmp("ReportByDevice", child->key) == 0)
129 #if SWAP_HAVE_REPORT_BY_DEVICE
130 cf_util_get_boolean(child, &report_by_device);
132 WARNING("swap plugin: The \"ReportByDevice\" option "
133 "is not supported on this platform. "
134 "The option is going to be ignored.");
135 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
136 else if (strcasecmp("ValuesAbsolute", child->key) == 0)
137 cf_util_get_boolean(child, &values_absolute);
138 else if (strcasecmp("ValuesPercentage", child->key) == 0)
139 cf_util_get_boolean(child, &values_percentage);
140 else if (strcasecmp("ReportIO", child->key) == 0)
141 cf_util_get_boolean(child, &report_io);
143 WARNING("swap plugin: Unknown config option: \"%s\"", child->key);
147 } /* }}} int swap_config */
149 static int swap_init(void) /* {{{ */
152 pagesize = (derive_t)sysconf(_SC_PAGESIZE);
153 /* #endif KERNEL_LINUX */
155 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
156 /* getpagesize(3C) tells me this does not fail.. */
157 pagesize = (derive_t)getpagesize();
158 /* #endif HAVE_SWAPCTL */
160 #elif defined(VM_SWAPUSAGE)
162 /* #endif defined(VM_SWAPUSAGE) */
164 #elif HAVE_LIBKVM_GETSWAPINFO
165 char errbuf[_POSIX2_LINE_MAX];
167 if (kvm_obj != NULL) {
172 kvm_pagesize = getpagesize();
174 kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
176 if (kvm_obj == NULL) {
177 ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
180 /* #endif HAVE_LIBKVM_GETSWAPINFO */
182 #elif HAVE_LIBSTATGRAB
184 /* #endif HAVE_LIBSTATGRAB */
187 pagesize = getpagesize();
188 #endif /* HAVE_PERFSTAT */
191 } /* }}} int swap_init */
193 static void swap_submit_usage(char const *plugin_instance, /* {{{ */
194 gauge_t used, gauge_t free,
195 char const *other_name, gauge_t other_value) {
196 value_list_t vl = VALUE_LIST_INIT;
198 vl.values = &(value_t){.gauge = NAN};
200 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
201 if (plugin_instance != NULL)
202 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
203 sstrncpy(vl.type, "swap", sizeof(vl.type));
206 plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free",
207 free, other_name, other_value, NULL);
208 if (values_percentage)
209 plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free",
210 free, other_name, other_value, NULL);
211 } /* }}} void swap_submit_usage */
213 #if KERNEL_LINUX || HAVE_PERFSTAT
214 __attribute__((nonnull(1))) static void
215 swap_submit_derive(char const *type_instance, /* {{{ */
217 value_list_t vl = VALUE_LIST_INIT;
219 vl.values = &(value_t){.derive = value};
221 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
222 sstrncpy(vl.type, "swap_io", sizeof(vl.type));
223 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
225 plugin_dispatch_values(&vl);
226 } /* }}} void swap_submit_derive */
230 static int swap_read_separate(void) /* {{{ */
235 fh = fopen("/proc/swaps", "r");
238 WARNING("swap plugin: fopen (/proc/swaps) failed: %s",
239 sstrerror(errno, errbuf, sizeof(errbuf)));
243 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
252 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
256 sstrncpy(path, fields[0], sizeof(path));
257 escape_slashes(path, sizeof(path));
261 total = strtod(fields[2], &endptr);
262 if ((endptr == fields[2]) || (errno != 0))
267 used = strtod(fields[3], &endptr);
268 if ((endptr == fields[3]) || (errno != 0))
274 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
280 } /* }}} int swap_read_separate */
282 static int swap_read_combined(void) /* {{{ */
287 gauge_t swap_used = NAN;
288 gauge_t swap_cached = NAN;
289 gauge_t swap_free = NAN;
290 gauge_t swap_total = NAN;
292 fh = fopen("/proc/meminfo", "r");
295 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s",
296 sstrerror(errno, errbuf, sizeof(errbuf)));
300 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
304 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
308 if (strcasecmp(fields[0], "SwapTotal:") == 0)
309 strtogauge(fields[1], &swap_total);
310 else if (strcasecmp(fields[0], "SwapFree:") == 0)
311 strtogauge(fields[1], &swap_free);
312 else if (strcasecmp(fields[0], "SwapCached:") == 0)
313 strtogauge(fields[1], &swap_cached);
318 if (isnan(swap_total) || isnan(swap_free))
321 /* Some systems, OpenVZ for example, don't provide SwapCached. */
322 if (isnan(swap_cached))
323 swap_used = swap_total - swap_free;
325 swap_used = swap_total - (swap_free + swap_cached);
326 assert(!isnan(swap_used));
331 swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
332 isnan(swap_cached) ? NULL : "cached",
333 isnan(swap_cached) ? NAN : swap_cached * 1024.0);
335 } /* }}} int swap_read_combined */
337 static int swap_read_io(void) /* {{{ */
342 _Bool old_kernel = 0;
344 uint8_t have_data = 0;
345 derive_t swap_in = 0;
346 derive_t swap_out = 0;
348 fh = fopen("/proc/vmstat", "r");
350 /* /proc/vmstat does not exist in kernels <2.6 */
351 fh = fopen("/proc/stat", "r");
354 WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
360 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
364 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
370 if (strcasecmp("pswpin", fields[0]) == 0) {
371 strtoderive(fields[1], &swap_in);
373 } else if (strcasecmp("pswpout", fields[0]) == 0) {
374 strtoderive(fields[1], &swap_out);
377 } else /* if (old_kernel) */
382 if (strcasecmp("page", fields[0]) == 0) {
383 strtoderive(fields[1], &swap_in);
384 strtoderive(fields[2], &swap_out);
387 } /* while (fgets) */
391 if (have_data != 0x03)
395 swap_in = swap_in * pagesize;
396 swap_out = swap_out * pagesize;
399 swap_submit_derive("in", swap_in);
400 swap_submit_derive("out", swap_out);
403 } /* }}} int swap_read_io */
405 static int swap_read(void) /* {{{ */
407 if (report_by_device)
408 swap_read_separate();
410 swap_read_combined();
416 } /* }}} int swap_read */
417 /* #endif KERNEL_LINUX */
420 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
421 * and kstat. The former reads physical space used on a device, the latter
422 * reports the view from the virtual memory system. It was decided that the
423 * kstat-based information should be moved to the "vmem" plugin, but nobody
424 * with enough Solaris experience was available at that time to do this. The
425 * code below is still there for your reference but it won't be activated in
426 * *this* plugin again. --octo
428 #elif 0 && HAVE_LIBKSTAT
429 /* kstat-based read function */
430 static int swap_read_kstat(void) /* {{{ */
438 if (swapctl(SC_AINFO, &ai) == -1) {
440 ERROR("swap plugin: swapctl failed: %s",
441 sstrerror(errno, errbuf, sizeof(errbuf)));
447 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
449 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
450 * /usr/include/vm/anon.h
452 * In short, swap -s shows: allocated + reserved = used, available
454 * However, Solaris does not allow to allocated/reserved more than the
455 * available swap (physical memory + disk swap), so the pedant may
456 * prefer: allocated + unallocated = reserved, available
458 * We map the above to: used + resv = n/a, free
460 * Does your brain hurt yet? - Christophe Kalt
462 * Oh, and in case you wonder,
463 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
464 * can suffer from a 32bit overflow.
466 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
467 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
468 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
470 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
472 } /* }}} int swap_read_kstat */
473 /* #endif 0 && HAVE_LIBKSTAT */
475 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
476 /* swapctl-based read function */
477 static int swap_read(void) /* {{{ */
487 swap_num = swapctl(SC_GETNSWP, NULL);
489 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
491 } else if (swap_num == 0)
494 /* Allocate and initialize the swaptbl_t structure */
495 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
497 ERROR("swap plugin: malloc failed.");
501 /* Memory to store the path names. We only use these paths when the
502 * separate option has been configured, but it's easier to just
503 * allocate enough memory in any case. */
504 s_paths = calloc(swap_num, PATH_MAX);
505 if (s_paths == NULL) {
506 ERROR("swap plugin: calloc failed.");
510 for (int i = 0; i < swap_num; i++)
511 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
514 status = swapctl(SC_LIST, s);
517 ERROR("swap plugin: swapctl (SC_LIST) failed: %s",
518 sstrerror(errno, errbuf, sizeof(errbuf)));
522 } else if (swap_num < status) {
523 /* more elements returned than requested */
524 ERROR("swap plugin: I allocated memory for %i structure%s, "
525 "but swapctl(2) claims to have returned %i. "
526 "I'm confused and will give up.",
527 swap_num, (swap_num == 1) ? "" : "s", status);
531 } else if (swap_num > status)
532 /* less elements returned than requested */
535 for (int i = 0; i < swap_num; i++) {
540 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
543 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
544 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
546 /* Shortcut for the "combined" setting (default) */
547 if (!report_by_device) {
553 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
554 escape_slashes(path, sizeof(path));
556 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
557 } /* for (swap_num) */
561 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
568 /* If the "separate" option was specified (report_by_device == 1), all
569 * values have already been dispatched from within the loop. */
570 if (!report_by_device)
571 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
576 } /* }}} int swap_read */
577 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
579 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
580 static int swap_read(void) /* {{{ */
582 struct swapent *swap_entries;
589 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
591 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
593 } else if (swap_num == 0)
596 swap_entries = calloc(swap_num, sizeof(*swap_entries));
597 if (swap_entries == NULL) {
598 ERROR("swap plugin: calloc failed.");
602 status = swapctl(SWAP_STATS, swap_entries, swap_num);
603 if (status != swap_num) {
604 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
609 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
610 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
612 #define C_SWAP_BLOCK_SIZE 512.0
615 /* TODO: Report per-device stats. The path name is available from
616 * swap_entries[i].se_path */
617 for (int i = 0; i < swap_num; i++) {
618 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
621 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
622 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
627 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
633 swap_submit_usage(NULL, used, total - used, NULL, NAN);
637 } /* }}} int swap_read */
638 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
640 #elif defined(VM_SWAPUSAGE)
641 static int swap_read(void) /* {{{ */
645 struct xsw_usage sw_usage;
650 mib[1] = VM_SWAPUSAGE;
652 sw_usage_len = sizeof(struct xsw_usage);
654 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
657 /* The returned values are bytes. */
658 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
659 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
662 } /* }}} int swap_read */
663 /* #endif VM_SWAPUSAGE */
665 #elif HAVE_LIBKVM_GETSWAPINFO
666 static int swap_read(void) /* {{{ */
668 struct kvm_swap data_s;
677 /* only one structure => only get the grand total, no details */
678 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
682 total = (gauge_t)data_s.ksw_total;
683 used = (gauge_t)data_s.ksw_used;
685 total *= (gauge_t)kvm_pagesize;
686 used *= (gauge_t)kvm_pagesize;
688 swap_submit_usage(NULL, used, total - used, NULL, NAN);
691 } /* }}} int swap_read */
692 /* #endif HAVE_LIBKVM_GETSWAPINFO */
694 #elif HAVE_LIBSTATGRAB
695 static int swap_read(void) /* {{{ */
699 swap = sg_get_swap_stats();
703 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
706 } /* }}} int swap_read */
707 /* #endif HAVE_LIBSTATGRAB */
710 static int swap_read(void) /* {{{ */
712 perfstat_memory_total_t pmemory = {0};
720 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
723 WARNING("swap plugin: perfstat_memory_total failed: %s",
724 sstrerror(errno, errbuf, sizeof(errbuf)));
728 total = (gauge_t)(pmemory.pgsp_total * pagesize);
729 free = (gauge_t)(pmemory.pgsp_free * pagesize);
730 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
732 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
735 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
736 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
740 } /* }}} int swap_read */
741 #endif /* HAVE_PERFSTAT */
743 void module_register(void) {
744 plugin_register_complex_config("swap", swap_config);
745 plugin_register_init("swap", swap_init);
746 plugin_register_read("swap", swap_read);
747 } /* void module_register */