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;
78 static bool report_by_device;
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;
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;
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 = true;
113 static bool values_percentage;
114 static bool report_io = true;
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, false, DS_TYPE_GAUGE, "used", used, "free",
207 free, other_name, other_value, NULL);
208 if (values_percentage)
209 plugin_dispatch_multivalue(&vl, true, 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");
237 WARNING("swap plugin: fopen (/proc/swaps) failed: %s", STRERRNO);
241 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
250 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
254 sstrncpy(path, fields[0], sizeof(path));
255 escape_slashes(path, sizeof(path));
259 total = strtod(fields[2], &endptr);
260 if ((endptr == fields[2]) || (errno != 0))
265 used = strtod(fields[3], &endptr);
266 if ((endptr == fields[3]) || (errno != 0))
272 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
278 } /* }}} int swap_read_separate */
280 static int swap_read_combined(void) /* {{{ */
285 gauge_t swap_used = NAN;
286 gauge_t swap_cached = NAN;
287 gauge_t swap_free = NAN;
288 gauge_t swap_total = NAN;
290 fh = fopen("/proc/meminfo", "r");
292 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s", STRERRNO);
296 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
300 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
304 if (strcasecmp(fields[0], "SwapTotal:") == 0)
305 strtogauge(fields[1], &swap_total);
306 else if (strcasecmp(fields[0], "SwapFree:") == 0)
307 strtogauge(fields[1], &swap_free);
308 else if (strcasecmp(fields[0], "SwapCached:") == 0)
309 strtogauge(fields[1], &swap_cached);
314 if (isnan(swap_total) || isnan(swap_free))
317 /* Some systems, OpenVZ for example, don't provide SwapCached. */
318 if (isnan(swap_cached))
319 swap_used = swap_total - swap_free;
321 swap_used = swap_total - (swap_free + swap_cached);
322 assert(!isnan(swap_used));
327 swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
328 isnan(swap_cached) ? NULL : "cached",
329 isnan(swap_cached) ? NAN : swap_cached * 1024.0);
331 } /* }}} int swap_read_combined */
333 static int swap_read_io(void) /* {{{ */
337 uint8_t have_data = 0;
338 derive_t swap_in = 0;
339 derive_t swap_out = 0;
341 FILE *fh = fopen("/proc/vmstat", "r");
343 WARNING("swap: fopen(/proc/vmstat): %s", STRERRNO);
347 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
349 int numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
354 if (strcasecmp("pswpin", fields[0]) == 0) {
355 strtoderive(fields[1], &swap_in);
357 } else if (strcasecmp("pswpout", fields[0]) == 0) {
358 strtoderive(fields[1], &swap_out);
361 } /* while (fgets) */
365 if (have_data != 0x03)
369 swap_in = swap_in * pagesize;
370 swap_out = swap_out * pagesize;
373 swap_submit_derive("in", swap_in);
374 swap_submit_derive("out", swap_out);
377 } /* }}} int swap_read_io */
379 static int swap_read(void) /* {{{ */
381 if (report_by_device)
382 swap_read_separate();
384 swap_read_combined();
390 } /* }}} int swap_read */
391 /* #endif KERNEL_LINUX */
394 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
395 * and kstat. The former reads physical space used on a device, the latter
396 * reports the view from the virtual memory system. It was decided that the
397 * kstat-based information should be moved to the "vmem" plugin, but nobody
398 * with enough Solaris experience was available at that time to do this. The
399 * code below is still there for your reference but it won't be activated in
400 * *this* plugin again. --octo
402 #elif 0 && HAVE_LIBKSTAT
403 /* kstat-based read function */
404 static int swap_read_kstat(void) /* {{{ */
412 if (swapctl(SC_AINFO, &ai) == -1) {
413 ERROR("swap plugin: swapctl failed: %s", STRERRNO);
419 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
421 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
422 * /usr/include/vm/anon.h
424 * In short, swap -s shows: allocated + reserved = used, available
426 * However, Solaris does not allow to allocated/reserved more than the
427 * available swap (physical memory + disk swap), so the pedant may
428 * prefer: allocated + unallocated = reserved, available
430 * We map the above to: used + resv = n/a, free
432 * Does your brain hurt yet? - Christophe Kalt
434 * Oh, and in case you wonder,
435 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
436 * can suffer from a 32bit overflow.
438 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
439 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
440 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
442 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
444 } /* }}} int swap_read_kstat */
445 /* #endif 0 && HAVE_LIBKSTAT */
447 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
448 /* swapctl-based read function */
449 static int swap_read(void) /* {{{ */
459 swap_num = swapctl(SC_GETNSWP, NULL);
461 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
463 } else if (swap_num == 0)
466 /* Allocate and initialize the swaptbl_t structure */
467 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
469 ERROR("swap plugin: malloc failed.");
473 /* Memory to store the path names. We only use these paths when the
474 * separate option has been configured, but it's easier to just
475 * allocate enough memory in any case. */
476 s_paths = calloc(swap_num, PATH_MAX);
477 if (s_paths == NULL) {
478 ERROR("swap plugin: calloc failed.");
482 for (int i = 0; i < swap_num; i++)
483 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
486 status = swapctl(SC_LIST, s);
488 ERROR("swap plugin: swapctl (SC_LIST) failed: %s", STRERRNO);
492 } else if (swap_num < status) {
493 /* more elements returned than requested */
494 ERROR("swap plugin: I allocated memory for %i structure%s, "
495 "but swapctl(2) claims to have returned %i. "
496 "I'm confused and will give up.",
497 swap_num, (swap_num == 1) ? "" : "s", status);
501 } else if (swap_num > status)
502 /* less elements returned than requested */
505 for (int i = 0; i < swap_num; i++) {
510 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
513 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
514 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
516 /* Shortcut for the "combined" setting (default) */
517 if (!report_by_device) {
523 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
524 escape_slashes(path, sizeof(path));
526 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
527 } /* for (swap_num) */
531 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
538 /* If the "separate" option was specified (report_by_device == true) all
539 * values have already been dispatched from within the loop. */
540 if (!report_by_device)
541 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
546 } /* }}} int swap_read */
547 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
549 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
550 static int swap_read(void) /* {{{ */
552 struct swapent *swap_entries;
559 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
561 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
563 } else if (swap_num == 0)
566 swap_entries = calloc(swap_num, sizeof(*swap_entries));
567 if (swap_entries == NULL) {
568 ERROR("swap plugin: calloc failed.");
572 status = swapctl(SWAP_STATS, swap_entries, swap_num);
573 if (status != swap_num) {
574 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
579 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
580 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
582 #define C_SWAP_BLOCK_SIZE 512.0
585 /* TODO: Report per-device stats. The path name is available from
586 * swap_entries[i].se_path */
587 for (int i = 0; i < swap_num; i++) {
588 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
591 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
592 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
597 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
603 swap_submit_usage(NULL, used, total - used, NULL, NAN);
607 } /* }}} int swap_read */
608 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
610 #elif defined(VM_SWAPUSAGE)
611 static int swap_read(void) /* {{{ */
615 struct xsw_usage sw_usage;
620 mib[1] = VM_SWAPUSAGE;
622 sw_usage_len = sizeof(struct xsw_usage);
624 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
627 /* The returned values are bytes. */
628 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
629 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
632 } /* }}} int swap_read */
633 /* #endif VM_SWAPUSAGE */
635 #elif HAVE_LIBKVM_GETSWAPINFO
636 static int swap_read(void) /* {{{ */
638 struct kvm_swap data_s;
647 /* only one structure => only get the grand total, no details */
648 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
652 total = (gauge_t)data_s.ksw_total;
653 used = (gauge_t)data_s.ksw_used;
655 total *= (gauge_t)kvm_pagesize;
656 used *= (gauge_t)kvm_pagesize;
658 swap_submit_usage(NULL, used, total - used, NULL, NAN);
661 } /* }}} int swap_read */
662 /* #endif HAVE_LIBKVM_GETSWAPINFO */
664 #elif HAVE_LIBSTATGRAB
665 static int swap_read(void) /* {{{ */
669 swap = sg_get_swap_stats();
673 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
676 } /* }}} int swap_read */
677 /* #endif HAVE_LIBSTATGRAB */
680 static int swap_read(void) /* {{{ */
682 perfstat_memory_total_t pmemory = {0};
690 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
692 WARNING("swap plugin: perfstat_memory_total failed: %s", STRERRNO);
696 total = (gauge_t)(pmemory.pgsp_total * pagesize);
697 free = (gauge_t)(pmemory.pgsp_free * pagesize);
698 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
700 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
703 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
704 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
708 } /* }}} int swap_read */
709 #endif /* HAVE_PERFSTAT */
711 void module_register(void) {
712 plugin_register_complex_config("swap", swap_config);
713 plugin_register_init("swap", swap_init);
714 plugin_register_read("swap", swap_read);
715 } /* void module_register */