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
42 # include <sys/swap.h>
48 # include <sys/param.h>
51 # include <sys/sysctl.h>
54 # include <sys/dkstat.h>
61 # include <statgrab.h>
65 # include <sys/protosw.h>
66 # include <libperfstat.h>
70 #define MAX(x,y) ((x) > (y) ? (x) : (y))
73 # define SWAP_HAVE_REPORT_BY_DEVICE 1
74 static derive_t pagesize;
75 static _Bool report_bytes = 0;
76 static _Bool report_by_device = 0;
77 /* #endif KERNEL_LINUX */
79 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
80 # define SWAP_HAVE_REPORT_BY_DEVICE 1
81 static derive_t pagesize;
82 static _Bool report_by_device = 0;
83 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
85 #elif defined(VM_SWAPUSAGE)
86 /* No global variables */
87 /* #endif defined(VM_SWAPUSAGE) */
89 #elif HAVE_LIBKVM_GETSWAPINFO
90 static kvm_t *kvm_obj = NULL;
92 /* #endif HAVE_LIBKVM_GETSWAPINFO */
94 #elif HAVE_LIBSTATGRAB
95 /* No global variables */
96 /* #endif HAVE_LIBSTATGRAB */
100 /*# endif HAVE_PERFSTAT */
103 # error "No applicable input method."
104 #endif /* HAVE_LIBSTATGRAB */
106 static _Bool values_absolute = 1;
107 static _Bool values_percentage = 0;
109 static const char *config_keys[] =
116 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
118 static int swap_config (const char *key, const char *value) /* {{{ */
120 if (strcasecmp ("ReportBytes", key) == 0)
123 report_bytes = IS_TRUE (value) ? 1 : 0;
125 WARNING ("swap plugin: The \"ReportBytes\" option is only "
126 "valid under Linux. "
127 "The option is going to be ignored.");
130 else if (strcasecmp ("ReportByDevice", key) == 0)
132 #if SWAP_HAVE_REPORT_BY_DEVICE
134 report_by_device = 1;
136 report_by_device = 0;
138 WARNING ("swap plugin: The \"ReportByDevice\" option is not "
139 "supported on this platform. "
140 "The option is going to be ignored.");
141 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
143 else if (strcasecmp (key, "ValuesAbsolute") == 0)
152 else if (strcasecmp (key, "ValuesPercentage") == 0)
155 values_percentage = 1;
157 values_percentage = 0;
167 } /* }}} int swap_config */
169 static int swap_init (void) /* {{{ */
172 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
173 /* #endif KERNEL_LINUX */
175 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
176 /* getpagesize(3C) tells me this does not fail.. */
177 pagesize = (derive_t) getpagesize ();
178 /* #endif HAVE_SWAPCTL */
180 #elif defined(VM_SWAPUSAGE)
182 /* #endif defined(VM_SWAPUSAGE) */
184 #elif HAVE_LIBKVM_GETSWAPINFO
191 kvm_pagesize = getpagesize ();
193 if ((kvm_obj = kvm_open (NULL, /* execfile */
196 O_RDONLY, /* flags */
200 ERROR ("swap plugin: kvm_open failed.");
203 /* #endif HAVE_LIBKVM_GETSWAPINFO */
205 #elif HAVE_LIBSTATGRAB
207 /* #endif HAVE_LIBSTATGRAB */
210 pagesize = getpagesize();
211 #endif /* HAVE_PERFSTAT */
214 } /* }}} int swap_init */
216 static void swap_submit (const char *plugin_instance, /* {{{ */
217 const char *type, const char *type_instance,
220 value_list_t vl = VALUE_LIST_INIT;
222 assert (type != NULL);
226 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
227 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
228 if (plugin_instance != NULL)
229 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
230 sstrncpy (vl.type, type, sizeof (vl.type));
231 if (type_instance != NULL)
232 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* }}} void swap_submit_inst */
237 static void swap_submit_gauge (char const *plugin_instance, char const *type, /* {{{ */
238 char const *type_instance, gauge_t value)
243 swap_submit (plugin_instance, type, type_instance, v);
244 } /* }}} void swap_submit_gauge */
246 #if KERNEL_LINUX || HAVE_PERFSTAT
247 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
248 const char *type_instance, derive_t value)
253 swap_submit (plugin_instance, "swap_io", type_instance, v);
254 } /* }}} void swap_submit_derive */
258 static int swap_read_separate (void) /* {{{ */
263 fh = fopen ("/proc/swaps", "r");
267 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
268 sstrerror (errno, errbuf, sizeof (errbuf)));
272 while (fgets (buffer, sizeof (buffer), fh) != NULL)
283 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
287 sstrncpy (path, fields[0], sizeof (path));
288 escape_slashes (path, sizeof (path));
292 size = strtod (fields[2], &endptr);
293 if ((endptr == fields[2]) || (errno != 0))
298 used = strtod (fields[3], &endptr);
299 if ((endptr == fields[3]) || (errno != 0))
309 swap_submit_gauge (path, "swap", "used", used);
310 swap_submit_gauge (path, "swap", "free", free);
312 if (values_percentage)
314 swap_submit_gauge (path, "percent", "used", 100.0 * used / (used + free));
315 swap_submit_gauge (path, "percent", "free", 100.0 * free / (used + free));
322 } /* }}} int swap_read_separate */
324 static int swap_read_combined (void) /* {{{ */
329 uint8_t have_data = 0;
330 gauge_t swap_used = 0.0;
331 gauge_t swap_cached = 0.0;
332 gauge_t swap_free = 0.0;
333 gauge_t swap_total = 0.0;
335 fh = fopen ("/proc/meminfo", "r");
339 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
340 sstrerror (errno, errbuf, sizeof (errbuf)));
344 while (fgets (buffer, sizeof (buffer), fh) != NULL)
349 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
353 if (strcasecmp (fields[0], "SwapTotal:") == 0)
355 swap_total = strtod (fields[1], /* endptr = */ NULL);
358 else if (strcasecmp (fields[0], "SwapFree:") == 0)
360 swap_free = strtod (fields[1], /* endptr = */ NULL);
363 else if (strcasecmp (fields[0], "SwapCached:") == 0)
365 swap_cached = strtod (fields[1], /* endptr = */ NULL);
372 if (have_data != 0x07)
375 if (isnan (swap_total)
376 || (swap_total <= 0.0)
377 || ((swap_free + swap_cached) > swap_total))
380 swap_used = swap_total - (swap_free + swap_cached);
384 swap_submit_gauge (NULL, "swap", "used", 1024.0 * swap_used);
385 swap_submit_gauge (NULL, "swap", "free", 1024.0 * swap_free);
386 swap_submit_gauge (NULL, "swap", "cached", 1024.0 * swap_cached);
388 if (values_percentage)
390 swap_submit_gauge (NULL, "percent", "used", 100.0 * swap_used / swap_total);
391 swap_submit_gauge (NULL, "percent", "free", 100.0 * swap_free / swap_total);
392 swap_submit_gauge (NULL, "percent", "cached", 100.0 * swap_cached / swap_total);
396 } /* }}} int swap_read_combined */
398 static int swap_read_io (void) /* {{{ */
403 _Bool old_kernel = 0;
405 uint8_t have_data = 0;
406 derive_t swap_in = 0;
407 derive_t swap_out = 0;
409 fh = fopen ("/proc/vmstat", "r");
412 /* /proc/vmstat does not exist in kernels <2.6 */
413 fh = fopen ("/proc/stat", "r");
417 WARNING ("swap: fopen: %s",
418 sstrerror (errno, errbuf, sizeof (errbuf)));
425 while (fgets (buffer, sizeof (buffer), fh) != NULL)
430 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
437 if (strcasecmp ("pswpin", fields[0]) == 0)
439 strtoderive (fields[1], &swap_in);
442 else if (strcasecmp ("pswpout", fields[0]) == 0)
444 strtoderive (fields[1], &swap_out);
448 else /* if (old_kernel) */
453 if (strcasecmp ("page", fields[0]) == 0)
455 strtoderive (fields[1], &swap_in);
456 strtoderive (fields[2], &swap_out);
459 } /* while (fgets) */
463 if (have_data != 0x03)
468 swap_in = swap_in * pagesize;
469 swap_out = swap_out * pagesize;
472 swap_submit_derive (NULL, "in", swap_in);
473 swap_submit_derive (NULL, "out", swap_out);
476 } /* }}} int swap_read_io */
478 static int swap_read (void) /* {{{ */
480 if (report_by_device)
481 swap_read_separate ();
483 swap_read_combined ();
488 } /* }}} int swap_read */
489 /* #endif KERNEL_LINUX */
492 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
493 * and kstat. The former reads physical space used on a device, the latter
494 * reports the view from the virtual memory system. It was decided that the
495 * kstat-based information should be moved to the "vmem" plugin, but nobody
496 * with enough Solaris experience was available at that time to do this. The
497 * code below is still there for your reference but it won't be activated in
498 * *this* plugin again. --octo
500 #elif 0 && HAVE_LIBKSTAT
501 /* kstat-based read function */
502 static int swap_read_kstat (void) /* {{{ */
510 if (swapctl (SC_AINFO, &ai) == -1)
513 ERROR ("swap plugin: swapctl failed: %s",
514 sstrerror (errno, errbuf, sizeof (errbuf)));
520 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
522 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
523 * /usr/include/vm/anon.h
525 * In short, swap -s shows: allocated + reserved = used, available
527 * However, Solaris does not allow to allocated/reserved more than the
528 * available swap (physical memory + disk swap), so the pedant may
529 * prefer: allocated + unallocated = reserved, available
531 * We map the above to: used + resv = n/a, free
533 * Does your brain hurt yet? - Christophe Kalt
535 * Oh, and in case you wonder,
536 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
537 * can suffer from a 32bit overflow.
539 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
540 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
541 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
545 swap_submit_gauge (NULL, "swap", "used", swap_alloc);
546 swap_submit_gauge (NULL, "swap", "free", swap_avail);
547 swap_submit_gauge (NULL, "swap", "reserved", swap_resv);
549 if (values_percentage)
551 gauge_t swap_total = swap_alloc + swap_avail + swap_resv;
553 swap_submit_gauge (NULL, "percent", "used", 100.0 * swap_alloc / swap_total);
554 swap_submit_gauge (NULL, "percent", "free", 100.0 * swap_avail / swap_total);
555 swap_submit_gauge (NULL, "percent", "cached", 100.0 * swap_resv / swap_total);
559 } /* }}} int swap_read_kstat */
560 /* #endif 0 && HAVE_LIBKSTAT */
562 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
563 /* swapctl-based read function */
564 static int swap_read (void) /* {{{ */
575 swap_num = swapctl (SC_GETNSWP, NULL);
578 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
582 else if (swap_num == 0)
585 /* Allocate and initialize the swaptbl_t structure */
586 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
589 ERROR ("swap plugin: smalloc failed.");
593 /* Memory to store the path names. We only use these paths when the
594 * separate option has been configured, but it's easier to just
595 * allocate enough memory in any case. */
596 s_paths = calloc (swap_num, PATH_MAX);
599 ERROR ("swap plugin: malloc failed.");
603 for (i = 0; i < swap_num; i++)
604 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
607 status = swapctl (SC_LIST, s);
611 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
612 sstrerror (errno, errbuf, sizeof (errbuf)));
617 else if (swap_num < status)
619 /* more elements returned than requested */
620 ERROR ("swap plugin: I allocated memory for %i structure%s, "
621 "but swapctl(2) claims to have returned %i. "
622 "I'm confused and will give up.",
623 swap_num, (swap_num == 1) ? "" : "s",
629 else if (swap_num > status)
630 /* less elements returned than requested */
633 for (i = 0; i < swap_num; i++)
639 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
642 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
643 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
645 /* Shortcut for the "combined" setting (default) */
646 if (!report_by_device)
653 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
654 escape_slashes (path, sizeof (path));
658 swap_submit_gauge (path, "swap", "used", this_total - this_avail);
659 swap_submit_gauge (path, "swap", "free", this_avail);
661 if (values_percentage)
663 swap_submit_gauge (path, "percent", "used", 100.0 * (this_total - this_avail) / this_total);
664 swap_submit_gauge (path, "percent", "free", 100.0 * this_avail / this_total);
667 } /* for (swap_num) */
671 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
678 /* If the "separate" option was specified (report_by_device == 2), all
679 * values have already been dispatched from within the loop. */
680 if (!report_by_device)
684 swap_submit_gauge (NULL, "swap", "used", (total - avail));
685 swap_submit_gauge (NULL, "swap", "free", avail);
687 if (values_percentage)
689 swap_submit_gauge (NULL, "percent", "used", 100.0 * (total - avail) / total);
690 swap_submit_gauge (NULL, "percent", "free", 100.0 * avail / total);
697 } /* }}} int swap_read */
698 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
700 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
701 static int swap_read (void) /* {{{ */
703 struct swapent *swap_entries;
711 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
714 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
718 else if (swap_num == 0)
721 swap_entries = calloc (swap_num, sizeof (*swap_entries));
722 if (swap_entries == NULL)
724 ERROR ("swap plugin: calloc failed.");
728 status = swapctl (SWAP_STATS, swap_entries, swap_num);
729 if (status != swap_num)
731 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
733 sfree (swap_entries);
737 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
738 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
740 # define C_SWAP_BLOCK_SIZE 512.0
743 for (i = 0; i < swap_num; i++)
745 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
748 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
749 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
754 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
761 swap_submit_gauge (NULL, "swap", "used", used);
762 swap_submit_gauge (NULL, "swap", "free", (total - used));
764 if (values_percentage)
766 swap_submit_gauge (NULL, "percent", "used", 100.0 * used / total);
767 swap_submit_gauge (NULL, "percent", "free", 100.0 * (total - used) / total);
770 sfree (swap_entries);
773 } /* }}} int swap_read */
774 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
776 #elif defined(VM_SWAPUSAGE)
777 static int swap_read (void) /* {{{ */
781 struct xsw_usage sw_usage;
786 mib[1] = VM_SWAPUSAGE;
788 sw_usage_len = sizeof (struct xsw_usage);
790 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
793 /* The returned values are bytes. */
796 swap_submit_gauge (NULL, "swap", "used", (gauge_t) sw_usage.xsu_used);
797 swap_submit_gauge (NULL, "swap", "free", (gauge_t) sw_usage.xsu_avail);
799 if (values_percentage)
801 gauge_t swap_total = (gauge_t) (sw_usage.xsu_used + sw_usage.xsu_avail);
802 swap_submit_gauge (NULL, "percent", "used", 100.0 * ((gauge_t) sw_usage.xsu_used) / swap_total);
803 swap_submit_gauge (NULL, "percent", "free", 100.0 * ((gauge_t) sw_usage.xsu_avail) / swap_total);
807 } /* }}} int swap_read */
808 /* #endif VM_SWAPUSAGE */
810 #elif HAVE_LIBKVM_GETSWAPINFO
811 static int swap_read (void) /* {{{ */
813 struct kvm_swap data_s;
823 /* only one structure => only get the grand total, no details */
824 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
828 total = (gauge_t) data_s.ksw_total;
829 used = (gauge_t) data_s.ksw_used;
831 total *= (gauge_t) kvm_pagesize;
832 used *= (gauge_t) kvm_pagesize;
838 swap_submit_gauge (NULL, "swap", "used", used);
839 swap_submit_gauge (NULL, "swap", "free", free);
841 if (values_percentage)
843 swap_submit_gauge (NULL, "percent", "used", 100.0 * used / total);
844 swap_submit_gauge (NULL, "percent", "free", 100.0 * free / total);
848 } /* }}} int swap_read */
849 /* #endif HAVE_LIBKVM_GETSWAPINFO */
851 #elif HAVE_LIBSTATGRAB
852 static int swap_read (void) /* {{{ */
856 swap = sg_get_swap_stats ();
863 swap_submit_gauge (NULL, "swap", "used", (gauge_t) swap->used);
864 swap_submit_gauge (NULL, "swap", "free", (gauge_t) swap->free);
866 if (values_percentage)
868 gauge_t swap_total = (gauge_t) (swap->used + swap->free);
869 swap_submit_gauge (NULL, "percent", "used", 100.0 * ((gauge_t) swap->used) / swap_total);
870 swap_submit_gauge (NULL, "percent", "free", 100.0 * ((gauge_t) swap->free) / swap_total);
874 } /* }}} int swap_read */
875 /* #endif HAVE_LIBSTATGRAB */
878 static int swap_read (void) /* {{{ */
880 perfstat_memory_total_t pmemory;
883 memset (&pmemory, 0, sizeof (pmemory));
884 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
888 WARNING ("swap plugin: perfstat_memory_total failed: %s",
889 sstrerror (errno, errbuf, sizeof (errbuf)));
895 swap_submit_gauge (NULL, "swap", "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
896 swap_submit_gauge (NULL, "swap", "free", (gauge_t) pmemory.pgsp_free * pagesize);
897 swap_submit_gauge (NULL, "swap", "reserved", (gauge_t) pmemory.pgsp_rsvd * pagesize);
899 if (values_percentage)
901 gauge_t swap_total = (gauge_t) pmemory.pgsp_total;
902 swap_submit_gauge (NULL, "percent", "used", 100.0 * ((gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free)) / swap_total);
903 swap_submit_gauge (NULL, "percent", "free", 100.0 * ((gauge_t) pmemory.pgsp_free) / swap_total);
904 swap_submit_gauge (NULL, "percent", "reserved", 100.0 * ((gauge_t) pmemory.pgsp_rsvd) / swap_total);
906 swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
907 swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
910 } /* }}} int swap_read */
911 #endif /* HAVE_PERFSTAT */
913 void module_register (void)
915 plugin_register_config ("swap", swap_config,
916 config_keys, config_keys_num);
917 plugin_register_init ("swap", swap_init);
918 plugin_register_read ("swap", swap_read);
919 } /* void module_register */
921 /* vim: set fdm=marker : */