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 environment" */
32 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
33 # undef _FILE_OFFSET_BITS
34 # undef _LARGEFILE64_SOURCE
43 # include <sys/swap.h>
49 # include <sys/param.h>
52 # include <sys/sysctl.h>
55 # include <sys/dkstat.h>
62 # include <statgrab.h>
66 # include <sys/protosw.h>
67 # include <libperfstat.h>
71 #define MAX(x,y) ((x) > (y) ? (x) : (y))
74 # define SWAP_HAVE_REPORT_BY_DEVICE 1
75 static derive_t pagesize;
76 static _Bool report_bytes = 0;
77 static _Bool report_by_device = 0;
78 /* #endif KERNEL_LINUX */
80 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
81 # define SWAP_HAVE_REPORT_BY_DEVICE 1
82 static derive_t pagesize;
83 static _Bool report_by_device = 0;
84 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
86 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
87 /* No global variables */
88 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
90 #elif defined(VM_SWAPUSAGE)
91 /* No global variables */
92 /* #endif defined(VM_SWAPUSAGE) */
94 #elif HAVE_LIBKVM_GETSWAPINFO
95 static kvm_t *kvm_obj = NULL;
97 /* #endif HAVE_LIBKVM_GETSWAPINFO */
99 #elif HAVE_LIBSTATGRAB
100 /* No global variables */
101 /* #endif HAVE_LIBSTATGRAB */
105 /*# endif HAVE_PERFSTAT */
108 # error "No applicable input method."
109 #endif /* HAVE_LIBSTATGRAB */
111 static _Bool values_absolute = 1;
112 static _Bool values_percentage = 0;
114 static int swap_config (oconfig_item_t *ci) /* {{{ */
116 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\"",
145 } /* }}} int swap_config */
147 static int swap_init (void) /* {{{ */
150 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
151 /* #endif KERNEL_LINUX */
153 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
154 /* getpagesize(3C) tells me this does not fail.. */
155 pagesize = (derive_t) getpagesize ();
156 /* #endif HAVE_SWAPCTL */
158 #elif defined(VM_SWAPUSAGE)
160 /* #endif defined(VM_SWAPUSAGE) */
162 #elif HAVE_LIBKVM_GETSWAPINFO
163 char errbuf[_POSIX2_LINE_MAX];
171 kvm_pagesize = getpagesize ();
173 kvm_obj = kvm_openfiles (NULL, "/dev/null", NULL, O_RDONLY, errbuf);
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)
197 value_list_t vl = VALUE_LIST_INIT;
199 vl.values = &(value_t) { .gauge = NAN };
201 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
202 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
203 if (plugin_instance != NULL)
204 sstrncpy (vl.plugin_instance, plugin_instance,
205 sizeof (vl.plugin_instance));
206 sstrncpy (vl.type, "swap", sizeof (vl.type));
209 plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE,
210 "used", used, "free", free,
211 other_name, other_value, NULL);
212 if (values_percentage)
213 plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
214 "used", used, "free", free,
215 other_name, other_value, NULL);
216 } /* }}} void swap_submit_usage */
218 #if KERNEL_LINUX || HAVE_PERFSTAT
219 __attribute__((nonnull(1)))
220 static void swap_submit_derive (char const *type_instance, /* {{{ */
223 value_list_t vl = VALUE_LIST_INIT;
225 vl.values = &(value_t) { .derive = value };
227 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
228 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
229 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
230 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
232 plugin_dispatch_values (&vl);
233 } /* }}} void swap_submit_derive */
237 static int swap_read_separate (void) /* {{{ */
242 fh = fopen ("/proc/swaps", "r");
246 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
247 sstrerror (errno, errbuf, sizeof (errbuf)));
251 while (fgets (buffer, sizeof (buffer), fh) != NULL)
261 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
265 sstrncpy (path, fields[0], sizeof (path));
266 escape_slashes (path, sizeof (path));
270 total = strtod (fields[2], &endptr);
271 if ((endptr == fields[2]) || (errno != 0))
276 used = strtod (fields[3], &endptr);
277 if ((endptr == fields[3]) || (errno != 0))
283 swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0,
290 } /* }}} int swap_read_separate */
292 static int swap_read_combined (void) /* {{{ */
297 gauge_t swap_used = NAN;
298 gauge_t swap_cached = NAN;
299 gauge_t swap_free = NAN;
300 gauge_t swap_total = NAN;
302 fh = fopen ("/proc/meminfo", "r");
306 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
307 sstrerror (errno, errbuf, sizeof (errbuf)));
311 while (fgets (buffer, sizeof (buffer), fh) != NULL)
316 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
320 if (strcasecmp (fields[0], "SwapTotal:") == 0)
321 strtogauge (fields[1], &swap_total);
322 else if (strcasecmp (fields[0], "SwapFree:") == 0)
323 strtogauge (fields[1], &swap_free);
324 else if (strcasecmp (fields[0], "SwapCached:") == 0)
325 strtogauge (fields[1], &swap_cached);
330 if (isnan (swap_total) || isnan (swap_free))
333 /* Some systems, OpenVZ for example, don't provide SwapCached. */
334 if (isnan (swap_cached))
335 swap_used = swap_total - swap_free;
337 swap_used = swap_total - (swap_free + swap_cached);
338 assert (!isnan (swap_used));
343 swap_submit_usage (NULL, swap_used * 1024.0, swap_free * 1024.0,
344 isnan (swap_cached) ? NULL : "cached",
345 isnan (swap_cached) ? NAN : swap_cached * 1024.0);
347 } /* }}} int swap_read_combined */
349 static int swap_read_io (void) /* {{{ */
354 _Bool old_kernel = 0;
356 uint8_t have_data = 0;
357 derive_t swap_in = 0;
358 derive_t swap_out = 0;
360 fh = fopen ("/proc/vmstat", "r");
363 /* /proc/vmstat does not exist in kernels <2.6 */
364 fh = fopen ("/proc/stat", "r");
368 WARNING ("swap: fopen: %s",
369 sstrerror (errno, errbuf, sizeof (errbuf)));
376 while (fgets (buffer, sizeof (buffer), fh) != NULL)
381 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
388 if (strcasecmp ("pswpin", fields[0]) == 0)
390 strtoderive (fields[1], &swap_in);
393 else if (strcasecmp ("pswpout", fields[0]) == 0)
395 strtoderive (fields[1], &swap_out);
399 else /* if (old_kernel) */
404 if (strcasecmp ("page", fields[0]) == 0)
406 strtoderive (fields[1], &swap_in);
407 strtoderive (fields[2], &swap_out);
410 } /* while (fgets) */
414 if (have_data != 0x03)
419 swap_in = swap_in * pagesize;
420 swap_out = swap_out * pagesize;
423 swap_submit_derive ("in", swap_in);
424 swap_submit_derive ("out", swap_out);
427 } /* }}} int swap_read_io */
429 static int swap_read (void) /* {{{ */
431 if (report_by_device)
432 swap_read_separate ();
434 swap_read_combined ();
439 } /* }}} int swap_read */
440 /* #endif KERNEL_LINUX */
443 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
444 * and kstat. The former reads physical space used on a device, the latter
445 * reports the view from the virtual memory system. It was decided that the
446 * kstat-based information should be moved to the "vmem" plugin, but nobody
447 * with enough Solaris experience was available at that time to do this. The
448 * code below is still there for your reference but it won't be activated in
449 * *this* plugin again. --octo
451 #elif 0 && HAVE_LIBKSTAT
452 /* kstat-based read function */
453 static int swap_read_kstat (void) /* {{{ */
461 if (swapctl (SC_AINFO, &ai) == -1)
464 ERROR ("swap plugin: swapctl failed: %s",
465 sstrerror (errno, errbuf, sizeof (errbuf)));
471 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
473 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
474 * /usr/include/vm/anon.h
476 * In short, swap -s shows: allocated + reserved = used, available
478 * However, Solaris does not allow to allocated/reserved more than the
479 * available swap (physical memory + disk swap), so the pedant may
480 * prefer: allocated + unallocated = reserved, available
482 * We map the above to: used + resv = n/a, free
484 * Does your brain hurt yet? - Christophe Kalt
486 * Oh, and in case you wonder,
487 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
488 * can suffer from a 32bit overflow.
490 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
491 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
492 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
494 swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
496 } /* }}} int swap_read_kstat */
497 /* #endif 0 && HAVE_LIBKSTAT */
499 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
500 /* swapctl-based read function */
501 static int swap_read (void) /* {{{ */
511 swap_num = swapctl (SC_GETNSWP, NULL);
514 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
518 else if (swap_num == 0)
521 /* Allocate and initialize the swaptbl_t structure */
522 s = malloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
525 ERROR ("swap plugin: malloc failed.");
529 /* Memory to store the path names. We only use these paths when the
530 * separate option has been configured, but it's easier to just
531 * allocate enough memory in any case. */
532 s_paths = calloc (swap_num, PATH_MAX);
535 ERROR ("swap plugin: calloc failed.");
539 for (int i = 0; i < swap_num; i++)
540 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
543 status = swapctl (SC_LIST, s);
547 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
548 sstrerror (errno, errbuf, sizeof (errbuf)));
553 else if (swap_num < status)
555 /* more elements returned than requested */
556 ERROR ("swap plugin: I allocated memory for %i structure%s, "
557 "but swapctl(2) claims to have returned %i. "
558 "I'm confused and will give up.",
559 swap_num, (swap_num == 1) ? "" : "s",
565 else if (swap_num > status)
566 /* less elements returned than requested */
569 for (int i = 0; i < swap_num; i++)
575 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
578 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
579 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
581 /* Shortcut for the "combined" setting (default) */
582 if (!report_by_device)
589 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
590 escape_slashes (path, sizeof (path));
592 swap_submit_usage (path, this_total - this_avail, this_avail,
594 } /* for (swap_num) */
598 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
605 /* If the "separate" option was specified (report_by_device == 1), all
606 * values have already been dispatched from within the loop. */
607 if (!report_by_device)
608 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
613 } /* }}} int swap_read */
614 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
616 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
617 static int swap_read (void) /* {{{ */
619 struct swapent *swap_entries;
626 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
629 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
633 else if (swap_num == 0)
636 swap_entries = calloc (swap_num, sizeof (*swap_entries));
637 if (swap_entries == NULL)
639 ERROR ("swap plugin: calloc failed.");
643 status = swapctl (SWAP_STATS, swap_entries, swap_num);
644 if (status != swap_num)
646 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
648 sfree (swap_entries);
652 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
653 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
655 # define C_SWAP_BLOCK_SIZE 512.0
658 /* TODO: Report per-device stats. The path name is available from
659 * swap_entries[i].se_path */
660 for (int i = 0; i < swap_num; i++)
662 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
665 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
666 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
671 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
673 sfree (swap_entries);
677 swap_submit_usage (NULL, used, total - used, NULL, NAN);
679 sfree (swap_entries);
681 } /* }}} int swap_read */
682 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
684 #elif defined(VM_SWAPUSAGE)
685 static int swap_read (void) /* {{{ */
689 struct xsw_usage sw_usage;
694 mib[1] = VM_SWAPUSAGE;
696 sw_usage_len = sizeof (struct xsw_usage);
698 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
701 /* The returned values are bytes. */
702 swap_submit_usage (NULL,
703 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
707 } /* }}} int swap_read */
708 /* #endif VM_SWAPUSAGE */
710 #elif HAVE_LIBKVM_GETSWAPINFO
711 static int swap_read (void) /* {{{ */
713 struct kvm_swap data_s;
722 /* only one structure => only get the grand total, no details */
723 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
727 total = (gauge_t) data_s.ksw_total;
728 used = (gauge_t) data_s.ksw_used;
730 total *= (gauge_t) kvm_pagesize;
731 used *= (gauge_t) kvm_pagesize;
733 swap_submit_usage (NULL, used, total - used, NULL, NAN);
736 } /* }}} int swap_read */
737 /* #endif HAVE_LIBKVM_GETSWAPINFO */
739 #elif HAVE_LIBSTATGRAB
740 static int swap_read (void) /* {{{ */
744 swap = sg_get_swap_stats ();
748 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
752 } /* }}} int swap_read */
753 /* #endif HAVE_LIBSTATGRAB */
756 static int swap_read (void) /* {{{ */
758 perfstat_memory_total_t pmemory = { 0 };
765 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
769 WARNING ("swap plugin: perfstat_memory_total failed: %s",
770 sstrerror (errno, errbuf, sizeof (errbuf)));
774 total = (gauge_t) (pmemory.pgsp_total * pagesize);
775 free = (gauge_t) (pmemory.pgsp_free * pagesize);
776 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
778 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
779 swap_submit_derive ("in", (derive_t) pmemory.pgspins * pagesize);
780 swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
783 } /* }}} int swap_read */
784 #endif /* HAVE_PERFSTAT */
786 void module_register (void)
788 plugin_register_complex_config ("swap", swap_config);
789 plugin_register_init ("swap", swap_init);
790 plugin_register_read ("swap", swap_read);
791 } /* void module_register */
793 /* vim: set fdm=marker : */