2 * collectd - src/swap.c
3 * Copyright (C) 2005-2012 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 static perfstat_memory_total_t pmemory;
101 /*# endif HAVE_PERFSTAT */
104 # error "No applicable input method."
105 #endif /* HAVE_LIBSTATGRAB */
107 static _Bool values_absolute = 1;
108 static _Bool values_percentage = 0;
110 static const char *config_keys[] =
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 static int swap_config (const char *key, const char *value) /* {{{ */
121 if (strcasecmp ("ReportBytes", key) == 0)
124 report_bytes = IS_TRUE (value) ? 1 : 0;
126 WARNING ("swap plugin: The \"ReportBytes\" option is only "
127 "valid under Linux. "
128 "The option is going to be ignored.");
131 else if (strcasecmp ("ReportByDevice", key) == 0)
133 #if SWAP_HAVE_REPORT_BY_DEVICE
135 report_by_device = 1;
137 report_by_device = 0;
139 WARNING ("swap plugin: The \"ReportByDevice\" option is not "
140 "supported on this platform. "
141 "The option is going to be ignored.");
142 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
144 else if (strcasecmp (key, "ValuesAbsolute") == 0)
153 else if (strcasecmp (key, "ValuesPercentage") == 0)
156 values_percentage = 1;
158 values_percentage = 0;
168 } /* }}} int swap_config */
170 static int swap_init (void) /* {{{ */
173 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
174 /* #endif KERNEL_LINUX */
176 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
177 /* getpagesize(3C) tells me this does not fail.. */
178 pagesize = (derive_t) getpagesize ();
179 /* #endif HAVE_SWAPCTL */
181 #elif defined(VM_SWAPUSAGE)
183 /* #endif defined(VM_SWAPUSAGE) */
185 #elif HAVE_LIBKVM_GETSWAPINFO
192 kvm_pagesize = getpagesize ();
194 if ((kvm_obj = kvm_open (NULL, /* execfile */
197 O_RDONLY, /* flags */
201 ERROR ("swap plugin: kvm_open failed.");
204 /* #endif HAVE_LIBKVM_GETSWAPINFO */
206 #elif HAVE_LIBSTATGRAB
208 /* #endif HAVE_LIBSTATGRAB */
211 pagesize = getpagesize();
212 #endif /* HAVE_PERFSTAT */
215 } /* }}} int swap_init */
217 static void swap_submit (const char *plugin_instance, /* {{{ */
218 const char *type, const char *type_instance,
221 value_list_t vl = VALUE_LIST_INIT;
223 assert (type != NULL);
227 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
228 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
229 if (plugin_instance != NULL)
230 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
231 sstrncpy (vl.type, type, sizeof (vl.type));
232 if (type_instance != NULL)
233 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
235 plugin_dispatch_values (&vl);
236 } /* }}} void swap_submit_inst */
238 static void swap_submit_gauge (const char *plugin_instance, /* {{{ */
239 const char *type_instance, gauge_t value)
244 swap_submit (plugin_instance, "swap", type_instance, v);
245 } /* }}} void swap_submit_gauge */
247 #if KERNEL_LINUX || HAVE_PERFSTAT
248 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
249 const char *type_instance, derive_t value)
254 swap_submit (plugin_instance, "swap_io", type_instance, v);
255 } /* }}} void swap_submit_derive */
259 static int swap_read_separate (void) /* {{{ */
264 fh = fopen ("/proc/swaps", "r");
268 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
269 sstrerror (errno, errbuf, sizeof (errbuf)));
273 while (fgets (buffer, sizeof (buffer), fh) != NULL)
284 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
288 sstrncpy (path, fields[0], sizeof (path));
289 escape_slashes (path, sizeof (path));
293 size = strtod (fields[2], &endptr);
294 if ((endptr == fields[2]) || (errno != 0))
299 used = strtod (fields[3], &endptr);
300 if ((endptr == fields[3]) || (errno != 0))
310 swap_submit_gauge (path, "used", used);
311 swap_submit_gauge (path, "free", free);
313 if (values_percentage)
315 swap_submit_gauge (path, "percent_used", (gauge_t) ((float_t) used) / (used + free) * 100);
316 swap_submit_gauge (path, "percent_free", (gauge_t) ((float_t) free) / (used + free) * 100);
323 } /* }}} int swap_read_separate */
325 static int swap_read_combined (void) /* {{{ */
330 uint8_t have_data = 0;
331 gauge_t swap_used = 0.0;
332 gauge_t swap_cached = 0.0;
333 gauge_t swap_free = 0.0;
334 gauge_t swap_total = 0.0;
336 fh = fopen ("/proc/meminfo", "r");
340 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
341 sstrerror (errno, errbuf, sizeof (errbuf)));
345 while (fgets (buffer, sizeof (buffer), fh) != NULL)
350 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
354 if (strcasecmp (fields[0], "SwapTotal:") == 0)
356 swap_total = strtod (fields[1], /* endptr = */ NULL);
359 else if (strcasecmp (fields[0], "SwapFree:") == 0)
361 swap_free = strtod (fields[1], /* endptr = */ NULL);
364 else if (strcasecmp (fields[0], "SwapCached:") == 0)
366 swap_cached = strtod (fields[1], /* endptr = */ NULL);
373 if (have_data != 0x07)
376 if (isnan (swap_total)
377 || (swap_total <= 0.0)
378 || ((swap_free + swap_cached) > swap_total))
381 swap_used = swap_total - (swap_free + swap_cached);
385 swap_submit_gauge (NULL, "used", 1024.0 * swap_used);
386 swap_submit_gauge (NULL, "free", 1024.0 * swap_free);
387 swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
389 if (values_percentage)
391 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap_used) / (swap_used + swap_free + swap_cached) * 100);
392 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap_free) / (swap_used + swap_free + swap_cached) * 100);
393 swap_submit_gauge (NULL, "percent_cached", (gauge_t) ((float_t) swap_cached) / (swap_used + swap_free + swap_cached) * 100);
397 } /* }}} int swap_read_combined */
399 static int swap_read_io (void) /* {{{ */
404 _Bool old_kernel = 0;
406 uint8_t have_data = 0;
407 derive_t swap_in = 0;
408 derive_t swap_out = 0;
410 fh = fopen ("/proc/vmstat", "r");
413 /* /proc/vmstat does not exist in kernels <2.6 */
414 fh = fopen ("/proc/stat", "r");
418 WARNING ("swap: fopen: %s",
419 sstrerror (errno, errbuf, sizeof (errbuf)));
426 while (fgets (buffer, sizeof (buffer), fh) != NULL)
431 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
438 if (strcasecmp ("pswpin", fields[0]) == 0)
440 strtoderive (fields[1], &swap_in);
443 else if (strcasecmp ("pswpout", fields[0]) == 0)
445 strtoderive (fields[1], &swap_out);
449 else /* if (old_kernel) */
454 if (strcasecmp ("page", fields[0]) == 0)
456 strtoderive (fields[1], &swap_in);
457 strtoderive (fields[2], &swap_out);
460 } /* while (fgets) */
464 if (have_data != 0x03)
469 swap_in = swap_in * pagesize;
470 swap_out = swap_out * pagesize;
473 swap_submit_derive (NULL, "in", swap_in);
474 swap_submit_derive (NULL, "out", swap_out);
477 } /* }}} int swap_read_io */
479 static int swap_read (void) /* {{{ */
481 if (report_by_device)
482 swap_read_separate ();
484 swap_read_combined ();
489 } /* }}} int swap_read */
490 /* #endif KERNEL_LINUX */
493 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
494 * and kstat. The former reads physical space used on a device, the latter
495 * reports the view from the virtual memory system. It was decided that the
496 * kstat-based information should be moved to the "vmem" plugin, but nobody
497 * with enough Solaris experience was available at that time to do this. The
498 * code below is still there for your reference but it won't be activated in
499 * *this* plugin again. --octo
501 #elif 0 && HAVE_LIBKSTAT
502 /* kstat-based read function */
503 static int swap_read_kstat (void) /* {{{ */
511 if (swapctl (SC_AINFO, &ai) == -1)
514 ERROR ("swap plugin: swapctl failed: %s",
515 sstrerror (errno, errbuf, sizeof (errbuf)));
521 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
523 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
524 * /usr/include/vm/anon.h
526 * In short, swap -s shows: allocated + reserved = used, available
528 * However, Solaris does not allow to allocated/reserved more than the
529 * available swap (physical memory + disk swap), so the pedant may
530 * prefer: allocated + unallocated = reserved, available
532 * We map the above to: used + resv = n/a, free
534 * Does your brain hurt yet? - Christophe Kalt
536 * Oh, and in case you wonder,
537 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
538 * can suffer from a 32bit overflow.
540 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
541 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
543 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
547 swap_submit_gauge (NULL, "used", swap_alloc);
548 swap_submit_gauge (NULL, "free", swap_avail);
549 swap_submit_gauge (NULL, "reserved", swap_resv);
551 if (values_percentage)
553 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap_alloc) / (swap_alloc + swap_avail + swap_resv) * 100);
554 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap_avail) / (swap_alloc + swap_avail + swap_resv) * 100);
555 swap_submit_gauge (NULL, "percent_reserved", (gauge_t) ((float_t) swap_resv) / (swap_alloc + swap_avail + swap_resv) * 100);
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 = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
643 this_avail = ((derive_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, "used", (gauge_t) (this_total - this_avail));
659 swap_submit_gauge (path, "free", (gauge_t) this_avail);
661 if (values_percentage)
663 swap_submit_gauge (path, "percent_used", (gauge_t) ((float_t) (this_total - this_avail)) / this_total * 100);
664 swap_submit_gauge (path, "percent_free", (gauge_t) ((float_t) this_avail) / this_total * 100);
667 } /* for (swap_num) */
671 ERROR ("swap plugin: Total swap space (%"PRIi64") "
672 "is less than free swap space (%"PRIi64").",
679 /* If the "separate" option was specified (report_by_device == 2), all
680 * values have already been dispatched from within the loop. */
681 if (!report_by_device)
685 swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
686 swap_submit_gauge (NULL, "free", (gauge_t) avail);
688 if (values_percentage)
690 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) (total - avail)) / total * 100);
691 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) avail) / total * 100);
698 } /* }}} int swap_read */
699 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
701 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
702 static int swap_read (void) /* {{{ */
704 struct swapent *swap_entries;
712 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
715 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
719 else if (swap_num == 0)
722 swap_entries = calloc (swap_num, sizeof (*swap_entries));
723 if (swap_entries == NULL)
725 ERROR ("swap plugin: calloc failed.");
729 status = swapctl (SWAP_STATS, swap_entries, swap_num);
730 if (status != swap_num)
732 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
734 sfree (swap_entries);
738 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
739 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
741 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
744 for (i = 0; i < swap_num; i++)
746 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
749 used += ((derive_t) swap_entries[i].se_inuse)
751 total += ((derive_t) swap_entries[i].se_nblks)
757 ERROR ("swap plugin: Total swap space (%"PRIu64") "
758 "is less than used swap space (%"PRIu64").",
765 swap_submit_gauge (NULL, "used", (gauge_t) used);
766 swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
768 if (values_percentage)
770 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) used) / total * 100);
771 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) (total - used)) / total * 100);
774 sfree (swap_entries);
777 } /* }}} int swap_read */
778 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
780 #elif defined(VM_SWAPUSAGE)
781 static int swap_read (void) /* {{{ */
785 struct xsw_usage sw_usage;
790 mib[1] = VM_SWAPUSAGE;
792 sw_usage_len = sizeof (struct xsw_usage);
794 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
797 /* The returned values are bytes. */
800 swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
801 swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
803 if (values_percentage)
805 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) sw_usage.xsu_used) / (sw_usage.xsu_used + sw_usage.xsu_avail) * 100);
806 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) sw_usage.xsu_avail) / (sw_usage.xsu_used + sw_usage.xsu_avail) * 100);
810 } /* }}} int swap_read */
811 /* #endif VM_SWAPUSAGE */
813 #elif HAVE_LIBKVM_GETSWAPINFO
814 static int swap_read (void) /* {{{ */
816 struct kvm_swap data_s;
826 /* only one structure => only get the grand total, no details */
827 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
831 total = (derive_t) data_s.ksw_total;
832 used = (derive_t) data_s.ksw_used;
834 total *= (derive_t) kvm_pagesize;
835 used *= (derive_t) kvm_pagesize;
841 swap_submit_gauge (NULL, "used", (gauge_t) used);
842 swap_submit_gauge (NULL, "free", (gauge_t) free);
844 if (values_percentage)
846 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) used) / total * 100);
847 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) free) / total * 100);
851 } /* }}} int swap_read */
852 /* #endif HAVE_LIBKVM_GETSWAPINFO */
854 #elif HAVE_LIBSTATGRAB
855 static int swap_read (void) /* {{{ */
859 swap = sg_get_swap_stats ();
866 swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
867 swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
869 if (values_percentage)
871 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) swap->used) / (swap->used + swap->free) * 100);
872 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) swap->free) / (swap_used + swap->free) * 100);
876 } /* }}} int swap_read */
877 /* #endif HAVE_LIBSTATGRAB */
880 static int swap_read (void) /* {{{ */
882 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
885 WARNING ("memory plugin: perfstat_memory_total failed: %s",
886 sstrerror (errno, errbuf, sizeof (errbuf)));
892 swap_submit_gauge (NULL, "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
893 swap_submit_gauge (NULL, "free", (gauge_t) pmemory.pgsp_free * pagesize );
894 swap_submit_gauge (NULL, "reserved", (gauge_t) pmemory.pgsp_rsvd * pagesize);
896 if (values_percentage)
898 swap_submit_gauge (NULL, "percent_used", (gauge_t) ((float_t) (pmemory.pgsp_total - pmemory.pgsp_free)) / pmemory.pgsp_total * 100);
899 swap_submit_gauge (NULL, "percent_free", (gauge_t) ((float_t) pmemory.pgsp_free) / pmemory.pgsp_total * 100);
900 swap_submit_gauge (NULL, "percent_reserved", (gauge_t) ((float_t) pmemory.pgsp_rsvd) / pmemory.pgsp_total * 100);
902 swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
903 swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
906 } /* }}} int swap_read */
907 #endif /* HAVE_PERFSTAT */
909 void module_register (void)
911 plugin_register_config ("swap", swap_config,
912 config_keys, config_keys_num);
913 plugin_register_init ("swap", swap_init);
914 plugin_register_read ("swap", swap_read);
915 } /* void module_register */
917 /* vim: set fdm=marker : */