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 int swap_config (oconfig_item_t *ci) /* {{{ */
113 for (i = 0; i < ci->children_num; i++)
115 oconfig_item_t *child = ci->children + i;
116 if (strcasecmp ("ReportBytes", child->key) == 0)
118 cf_util_get_boolean (child, &report_bytes);
120 WARNING ("swap plugin: The \"ReportBytes\" option "
121 "is only valid under Linux. "
122 "The option is going to be ignored.");
124 else if (strcasecmp ("ReportByDevice", child->key) == 0)
125 #if SWAP_HAVE_REPORT_BY_DEVICE
126 cf_util_get_boolean (child, &report_by_device);
128 WARNING ("swap plugin: The \"ReportByDevice\" option "
129 "is not supported on this platform. "
130 "The option is going to be ignored.");
131 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
132 else if (strcasecmp ("ValuesAbsolute", child->key) == 0)
133 cf_util_get_boolean (child, &values_absolute);
134 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
135 cf_util_get_boolean (child, &values_percentage);
137 WARNING ("swap plugin: Unknown config option: \"%s\"",
142 } /* }}} int swap_config */
144 static int swap_init (void) /* {{{ */
147 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
148 /* #endif KERNEL_LINUX */
150 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
151 /* getpagesize(3C) tells me this does not fail.. */
152 pagesize = (derive_t) getpagesize ();
153 /* #endif HAVE_SWAPCTL */
155 #elif defined(VM_SWAPUSAGE)
157 /* #endif defined(VM_SWAPUSAGE) */
159 #elif HAVE_LIBKVM_GETSWAPINFO
166 kvm_pagesize = getpagesize ();
168 if ((kvm_obj = kvm_open (NULL, /* execfile */
171 O_RDONLY, /* flags */
175 ERROR ("swap plugin: kvm_open failed.");
178 /* #endif HAVE_LIBKVM_GETSWAPINFO */
180 #elif HAVE_LIBSTATGRAB
182 /* #endif HAVE_LIBSTATGRAB */
185 pagesize = getpagesize();
186 #endif /* HAVE_PERFSTAT */
189 } /* }}} int swap_init */
191 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
192 gauge_t used, gauge_t free,
193 char const *other_name, gauge_t other_value)
196 value_list_t vl = VALUE_LIST_INIT;
199 vl.values_len = STATIC_ARRAY_SIZE (v);
200 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
202 if (plugin_instance != NULL)
203 sstrncpy (vl.plugin_instance, plugin_instance,
204 sizeof (vl.plugin_instance));
205 sstrncpy (vl.type, "swap", sizeof (vl.type));
208 plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE,
209 "used", used, "free", free,
210 other_name, other_value, NULL);
211 if (values_percentage)
212 plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
213 "used", used, "free", free,
214 other_name, other_value, NULL);
215 } /* }}} void swap_submit_usage */
217 #if KERNEL_LINUX || HAVE_PERFSTAT
218 __attribute__((nonnull(1)))
219 static void swap_submit_derive (char const *type_instance, /* {{{ */
222 value_list_t vl = VALUE_LIST_INIT;
228 vl.values_len = STATIC_ARRAY_SIZE (v);
229 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
230 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
231 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
232 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* }}} void swap_submit_derive */
239 static int swap_read_separate (void) /* {{{ */
244 fh = fopen ("/proc/swaps", "r");
248 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
249 sstrerror (errno, errbuf, sizeof (errbuf)));
253 while (fgets (buffer, sizeof (buffer), fh) != NULL)
263 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
267 sstrncpy (path, fields[0], sizeof (path));
268 escape_slashes (path, sizeof (path));
272 total = strtod (fields[2], &endptr);
273 if ((endptr == fields[2]) || (errno != 0))
278 used = strtod (fields[3], &endptr);
279 if ((endptr == fields[3]) || (errno != 0))
285 swap_submit_usage (path, used, total - used, NULL, NAN);
291 } /* }}} int swap_read_separate */
293 static int swap_read_combined (void) /* {{{ */
298 gauge_t swap_used = NAN;
299 gauge_t swap_cached = NAN;
300 gauge_t swap_free = NAN;
301 gauge_t swap_total = NAN;
303 fh = fopen ("/proc/meminfo", "r");
307 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
308 sstrerror (errno, errbuf, sizeof (errbuf)));
312 while (fgets (buffer, sizeof (buffer), fh) != NULL)
317 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
321 if (strcasecmp (fields[0], "SwapTotal:") == 0)
322 strtogauge (fields[1], &swap_total);
323 else if (strcasecmp (fields[0], "SwapFree:") == 0)
324 strtogauge (fields[1], &swap_free);
325 else if (strcasecmp (fields[0], "SwapCached:") == 0)
326 strtogauge (fields[1], &swap_cached);
331 if (isnan (swap_total) || isnan (swap_free))
334 /* Some systems, OpenVZ for example, don't provide SwapCached. */
335 if (isnan (swap_cached))
336 swap_used = swap_total - swap_free;
338 swap_used = swap_total - (swap_free + swap_cached);
339 assert (!isnan (swap_used));
344 swap_submit_usage (NULL, swap_used, swap_free,
345 isnan (swap_cached) ? NULL : "cached", swap_cached);
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) /* {{{ */
512 swap_num = swapctl (SC_GETNSWP, NULL);
515 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
519 else if (swap_num == 0)
522 /* Allocate and initialize the swaptbl_t structure */
523 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
526 ERROR ("swap plugin: smalloc failed.");
530 /* Memory to store the path names. We only use these paths when the
531 * separate option has been configured, but it's easier to just
532 * allocate enough memory in any case. */
533 s_paths = calloc (swap_num, PATH_MAX);
536 ERROR ("swap plugin: malloc failed.");
540 for (i = 0; i < swap_num; i++)
541 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
544 status = swapctl (SC_LIST, s);
548 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
549 sstrerror (errno, errbuf, sizeof (errbuf)));
554 else if (swap_num < status)
556 /* more elements returned than requested */
557 ERROR ("swap plugin: I allocated memory for %i structure%s, "
558 "but swapctl(2) claims to have returned %i. "
559 "I'm confused and will give up.",
560 swap_num, (swap_num == 1) ? "" : "s",
566 else if (swap_num > status)
567 /* less elements returned than requested */
570 for (i = 0; i < swap_num; i++)
576 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
579 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
580 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
582 /* Shortcut for the "combined" setting (default) */
583 if (!report_by_device)
590 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
591 escape_slashes (path, sizeof (path));
593 swap_submit_usage (path, this_total - this_avail, this_avail,
595 } /* for (swap_num) */
599 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
606 /* If the "separate" option was specified (report_by_device == 1), all
607 * values have already been dispatched from within the loop. */
608 if (!report_by_device)
609 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
614 } /* }}} int swap_read */
615 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
617 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
618 static int swap_read (void) /* {{{ */
620 struct swapent *swap_entries;
628 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
631 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
635 else if (swap_num == 0)
638 swap_entries = calloc (swap_num, sizeof (*swap_entries));
639 if (swap_entries == NULL)
641 ERROR ("swap plugin: calloc failed.");
645 status = swapctl (SWAP_STATS, swap_entries, swap_num);
646 if (status != swap_num)
648 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
650 sfree (swap_entries);
654 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
655 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
657 # define C_SWAP_BLOCK_SIZE 512.0
660 /* TODO: Report per-device stats. The path name is available from
661 * swap_entries[i].se_path */
662 for (i = 0; i < swap_num; i++)
664 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
667 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
668 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
673 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
678 swap_submit_usage (NULL, used, total - used, NULL, NAN);
680 sfree (swap_entries);
682 } /* }}} int swap_read */
683 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
685 #elif defined(VM_SWAPUSAGE)
686 static int swap_read (void) /* {{{ */
690 struct xsw_usage sw_usage;
695 mib[1] = VM_SWAPUSAGE;
697 sw_usage_len = sizeof (struct xsw_usage);
699 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
702 /* The returned values are bytes. */
703 swap_submit_usage (NULL,
704 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
708 } /* }}} int swap_read */
709 /* #endif VM_SWAPUSAGE */
711 #elif HAVE_LIBKVM_GETSWAPINFO
712 static int swap_read (void) /* {{{ */
714 struct kvm_swap data_s;
723 /* only one structure => only get the grand total, no details */
724 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
728 total = (gauge_t) data_s.ksw_total;
729 used = (gauge_t) data_s.ksw_used;
731 total *= (gauge_t) kvm_pagesize;
732 used *= (gauge_t) kvm_pagesize;
734 swap_submit_usage (NULL, used, total - used, NULL, NAN);
737 } /* }}} int swap_read */
738 /* #endif HAVE_LIBKVM_GETSWAPINFO */
740 #elif HAVE_LIBSTATGRAB
741 static int swap_read (void) /* {{{ */
745 swap = sg_get_swap_stats ();
749 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
753 } /* }}} int swap_read */
754 /* #endif HAVE_LIBSTATGRAB */
757 static int swap_read (void) /* {{{ */
759 perfstat_memory_total_t pmemory;
766 memset (&pmemory, 0, sizeof (pmemory));
767 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
771 WARNING ("swap plugin: perfstat_memory_total failed: %s",
772 sstrerror (errno, errbuf, sizeof (errbuf)));
776 total = (gauge_t) (pmemory.pgsp_total * pagesize);
777 free = (gauge_t) (pmemory.pgsp_free * pagesize);
778 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
780 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
781 swap_submit_derive ("in", (derive_t) pmemory.pgspins * pagesize);
782 swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
785 } /* }}} int swap_read */
786 #endif /* HAVE_PERFSTAT */
788 void module_register (void)
790 plugin_register_complex_config ("swap", swap_config);
791 plugin_register_init ("swap", swap_init);
792 plugin_register_read ("swap", swap_read);
793 } /* void module_register */
795 /* vim: set fdm=marker : */