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_usage (char const *plugin_instance, /* {{{ */
238 gauge_t used, gauge_t free,
239 char const *other_name, gauge_t other_value)
242 value_list_t vl = VALUE_LIST_INIT;
245 vl.values_len = STATIC_ARRAY_SIZE (v);
246 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
247 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
248 if (plugin_instance != NULL)
249 sstrncpy (vl.plugin_instance, plugin_instance,
250 sizeof (vl.plugin_instance));
251 sstrncpy (vl.type, "swap", sizeof (vl.type));
254 plugin_dispatch_multivalue (&vl, 0,
255 "used", used, "free", free,
256 other_name, other_value, NULL);
257 if (values_percentage)
258 plugin_dispatch_multivalue (&vl, 1,
259 "used", used, "free", free,
260 other_name, other_value, NULL);
261 } /* }}} void swap_submit_usage */
263 #if KERNEL_LINUX || HAVE_PERFSTAT
264 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
265 const char *type_instance, derive_t value)
270 swap_submit (plugin_instance, "swap_io", type_instance, v);
271 } /* }}} void swap_submit_derive */
275 static int swap_read_separate (void) /* {{{ */
280 fh = fopen ("/proc/swaps", "r");
284 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
285 sstrerror (errno, errbuf, sizeof (errbuf)));
289 while (fgets (buffer, sizeof (buffer), fh) != NULL)
299 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
303 sstrncpy (path, fields[0], sizeof (path));
304 escape_slashes (path, sizeof (path));
308 total = strtod (fields[2], &endptr);
309 if ((endptr == fields[2]) || (errno != 0))
314 used = strtod (fields[3], &endptr);
315 if ((endptr == fields[3]) || (errno != 0))
321 swap_submit_usage (path, used, total - used, NULL, NAN);
327 } /* }}} int swap_read_separate */
329 static int swap_read_combined (void) /* {{{ */
334 uint8_t have_data = 0;
335 gauge_t swap_used = 0.0;
336 gauge_t swap_cached = 0.0;
337 gauge_t swap_free = 0.0;
338 gauge_t swap_total = 0.0;
340 fh = fopen ("/proc/meminfo", "r");
344 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
345 sstrerror (errno, errbuf, sizeof (errbuf)));
349 while (fgets (buffer, sizeof (buffer), fh) != NULL)
354 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
358 if (strcasecmp (fields[0], "SwapTotal:") == 0)
360 swap_total = strtod (fields[1], /* endptr = */ NULL);
363 else if (strcasecmp (fields[0], "SwapFree:") == 0)
365 swap_free = strtod (fields[1], /* endptr = */ NULL);
368 else if (strcasecmp (fields[0], "SwapCached:") == 0)
370 swap_cached = strtod (fields[1], /* endptr = */ NULL);
377 if (have_data != 0x07)
380 if (isnan (swap_total)
381 || (swap_total <= 0.0)
382 || ((swap_free + swap_cached) > swap_total))
385 swap_used = swap_total - (swap_free + swap_cached);
387 INFO ("swap plugin: used = %g, free = %g, cached = %g", swap_used, swap_free, swap_cached);
388 swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
390 } /* }}} int swap_read_combined */
392 static int swap_read_io (void) /* {{{ */
397 _Bool old_kernel = 0;
399 uint8_t have_data = 0;
400 derive_t swap_in = 0;
401 derive_t swap_out = 0;
403 fh = fopen ("/proc/vmstat", "r");
406 /* /proc/vmstat does not exist in kernels <2.6 */
407 fh = fopen ("/proc/stat", "r");
411 WARNING ("swap: fopen: %s",
412 sstrerror (errno, errbuf, sizeof (errbuf)));
419 while (fgets (buffer, sizeof (buffer), fh) != NULL)
424 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
431 if (strcasecmp ("pswpin", fields[0]) == 0)
433 strtoderive (fields[1], &swap_in);
436 else if (strcasecmp ("pswpout", fields[0]) == 0)
438 strtoderive (fields[1], &swap_out);
442 else /* if (old_kernel) */
447 if (strcasecmp ("page", fields[0]) == 0)
449 strtoderive (fields[1], &swap_in);
450 strtoderive (fields[2], &swap_out);
453 } /* while (fgets) */
457 if (have_data != 0x03)
462 swap_in = swap_in * pagesize;
463 swap_out = swap_out * pagesize;
466 swap_submit_derive (NULL, "in", swap_in);
467 swap_submit_derive (NULL, "out", swap_out);
470 } /* }}} int swap_read_io */
472 static int swap_read (void) /* {{{ */
474 if (report_by_device)
475 swap_read_separate ();
477 swap_read_combined ();
482 } /* }}} int swap_read */
483 /* #endif KERNEL_LINUX */
486 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
487 * and kstat. The former reads physical space used on a device, the latter
488 * reports the view from the virtual memory system. It was decided that the
489 * kstat-based information should be moved to the "vmem" plugin, but nobody
490 * with enough Solaris experience was available at that time to do this. The
491 * code below is still there for your reference but it won't be activated in
492 * *this* plugin again. --octo
494 #elif 0 && HAVE_LIBKSTAT
495 /* kstat-based read function */
496 static int swap_read_kstat (void) /* {{{ */
504 if (swapctl (SC_AINFO, &ai) == -1)
507 ERROR ("swap plugin: swapctl failed: %s",
508 sstrerror (errno, errbuf, sizeof (errbuf)));
514 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
516 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
517 * /usr/include/vm/anon.h
519 * In short, swap -s shows: allocated + reserved = used, available
521 * However, Solaris does not allow to allocated/reserved more than the
522 * available swap (physical memory + disk swap), so the pedant may
523 * prefer: allocated + unallocated = reserved, available
525 * We map the above to: used + resv = n/a, free
527 * Does your brain hurt yet? - Christophe Kalt
529 * Oh, and in case you wonder,
530 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
531 * can suffer from a 32bit overflow.
533 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
534 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
535 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
537 swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
539 } /* }}} int swap_read_kstat */
540 /* #endif 0 && HAVE_LIBKSTAT */
542 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
543 /* swapctl-based read function */
544 static int swap_read (void) /* {{{ */
555 swap_num = swapctl (SC_GETNSWP, NULL);
558 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
562 else if (swap_num == 0)
565 /* Allocate and initialize the swaptbl_t structure */
566 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
569 ERROR ("swap plugin: smalloc failed.");
573 /* Memory to store the path names. We only use these paths when the
574 * separate option has been configured, but it's easier to just
575 * allocate enough memory in any case. */
576 s_paths = calloc (swap_num, PATH_MAX);
579 ERROR ("swap plugin: malloc failed.");
583 for (i = 0; i < swap_num; i++)
584 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
587 status = swapctl (SC_LIST, s);
591 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
592 sstrerror (errno, errbuf, sizeof (errbuf)));
597 else if (swap_num < status)
599 /* more elements returned than requested */
600 ERROR ("swap plugin: I allocated memory for %i structure%s, "
601 "but swapctl(2) claims to have returned %i. "
602 "I'm confused and will give up.",
603 swap_num, (swap_num == 1) ? "" : "s",
609 else if (swap_num > status)
610 /* less elements returned than requested */
613 for (i = 0; i < swap_num; i++)
619 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
622 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
623 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
625 /* Shortcut for the "combined" setting (default) */
626 if (!report_by_device)
633 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
634 escape_slashes (path, sizeof (path));
636 swap_submit_usage (path, this_total - this_avail, this_avail,
638 } /* for (swap_num) */
642 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
649 /* If the "separate" option was specified (report_by_device == 1), all
650 * values have already been dispatched from within the loop. */
651 if (!report_by_device)
652 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
657 } /* }}} int swap_read */
658 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
660 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
661 static int swap_read (void) /* {{{ */
663 struct swapent *swap_entries;
671 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
674 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
678 else if (swap_num == 0)
681 swap_entries = calloc (swap_num, sizeof (*swap_entries));
682 if (swap_entries == NULL)
684 ERROR ("swap plugin: calloc failed.");
688 status = swapctl (SWAP_STATS, swap_entries, swap_num);
689 if (status != swap_num)
691 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
693 sfree (swap_entries);
697 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
698 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
700 # define C_SWAP_BLOCK_SIZE 512.0
703 /* TODO: Report per-device stats. The path name is available from
704 * swap_entries[i].se_path */
705 for (i = 0; i < swap_num; i++)
707 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
710 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
711 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
716 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
721 swap_submit_usage (NULL, used, total - used, NULL, NAN);
723 sfree (swap_entries);
725 } /* }}} int swap_read */
726 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
728 #elif defined(VM_SWAPUSAGE)
729 static int swap_read (void) /* {{{ */
733 struct xsw_usage sw_usage;
738 mib[1] = VM_SWAPUSAGE;
740 sw_usage_len = sizeof (struct xsw_usage);
742 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
745 /* The returned values are bytes. */
746 swap_submit_usage (NULL,
747 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
751 } /* }}} int swap_read */
752 /* #endif VM_SWAPUSAGE */
754 #elif HAVE_LIBKVM_GETSWAPINFO
755 static int swap_read (void) /* {{{ */
757 struct kvm_swap data_s;
766 /* only one structure => only get the grand total, no details */
767 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
771 total = (gauge_t) data_s.ksw_total;
772 used = (gauge_t) data_s.ksw_used;
774 total *= (gauge_t) kvm_pagesize;
775 used *= (gauge_t) kvm_pagesize;
777 swap_submit_usage (NULL, used, total - used, NULL, NAN);
780 } /* }}} int swap_read */
781 /* #endif HAVE_LIBKVM_GETSWAPINFO */
783 #elif HAVE_LIBSTATGRAB
784 static int swap_read (void) /* {{{ */
788 swap = sg_get_swap_stats ();
792 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
796 } /* }}} int swap_read */
797 /* #endif HAVE_LIBSTATGRAB */
800 static int swap_read (void) /* {{{ */
802 perfstat_memory_total_t pmemory;
809 memset (&pmemory, 0, sizeof (pmemory));
810 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
814 WARNING ("swap plugin: perfstat_memory_total failed: %s",
815 sstrerror (errno, errbuf, sizeof (errbuf)));
819 total = (gauge_t) (pmemory.pgsp_total * pagesize);
820 free = (gauge_t) (pmemory.pgsp_free * pagesize);
821 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
823 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
824 swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
825 swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
828 } /* }}} int swap_read */
829 #endif /* HAVE_PERFSTAT */
831 void module_register (void)
833 plugin_register_config ("swap", swap_config,
834 config_keys, config_keys_num);
835 plugin_register_init ("swap", swap_init);
836 plugin_register_read ("swap", swap_read);
837 } /* void module_register */
839 /* vim: set fdm=marker : */