2 * collectd - src/swap.c
3 * Copyright (C) 2005-2010 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_CONFIG 1
74 /* No global variables */
75 /* #endif KERNEL_LINUX */
77 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
78 # define SWAP_HAVE_CONFIG 1
79 static derive_t pagesize;
80 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
82 #elif defined(VM_SWAPUSAGE)
83 /* No global variables */
84 /* #endif defined(VM_SWAPUSAGE) */
86 #elif HAVE_LIBKVM_GETSWAPINFO
87 static kvm_t *kvm_obj = NULL;
89 /* #endif HAVE_LIBKVM_GETSWAPINFO */
91 #elif HAVE_LIBSTATGRAB
92 /* No global variables */
93 /* #endif HAVE_LIBSTATGRAB */
97 static perfstat_memory_total_t pmemory;
98 /*# endif HAVE_PERFSTAT */
101 # error "No applicable input method."
102 #endif /* HAVE_LIBSTATGRAB */
105 static const char *config_keys[] =
109 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
111 static _Bool report_by_device = 0;
113 static int swap_config (const char *key, const char *value) /* {{{ */
115 if (strcasecmp ("ReportByDevice", key) == 0)
118 report_by_device = 1;
120 report_by_device = 0;
128 } /* }}} int swap_config */
129 #endif /* SWAP_HAVE_CONFIG */
131 static int swap_init (void) /* {{{ */
135 /* #endif KERNEL_LINUX */
137 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
138 /* getpagesize(3C) tells me this does not fail.. */
139 pagesize = (derive_t) getpagesize ();
140 /* #endif HAVE_SWAPCTL */
142 #elif defined(VM_SWAPUSAGE)
144 /* #endif defined(VM_SWAPUSAGE) */
146 #elif HAVE_LIBKVM_GETSWAPINFO
153 kvm_pagesize = getpagesize ();
155 if ((kvm_obj = kvm_open (NULL, /* execfile */
158 O_RDONLY, /* flags */
162 ERROR ("swap plugin: kvm_open failed.");
165 /* #endif HAVE_LIBKVM_GETSWAPINFO */
167 #elif HAVE_LIBSTATGRAB
169 /* #endif HAVE_LIBSTATGRAB */
172 pagesize = getpagesize();
173 #endif /* HAVE_PERFSTAT */
176 } /* }}} int swap_init */
178 static void swap_submit (const char *plugin_instance, /* {{{ */
179 const char *type, const char *type_instance,
182 value_list_t vl = VALUE_LIST_INIT;
184 assert (type != NULL);
188 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
189 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
190 if (plugin_instance != NULL)
191 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
192 sstrncpy (vl.type, type, sizeof (vl.type));
193 if (type_instance != NULL)
194 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
196 plugin_dispatch_values (&vl);
197 } /* }}} void swap_submit_inst */
199 static void swap_submit_gauge (const char *plugin_instance, /* {{{ */
200 const char *type_instance, gauge_t value)
205 swap_submit (plugin_instance, "swap", type_instance, v);
206 } /* }}} void swap_submit_gauge */
209 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
210 const char *type_instance, derive_t value)
215 swap_submit (plugin_instance, "swap_io", type_instance, v);
216 } /* }}} void swap_submit_derive */
218 static int swap_read_separate (void) /* {{{ */
223 fh = fopen ("/proc/swaps", "r");
227 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
228 sstrerror (errno, errbuf, sizeof (errbuf)));
232 while (fgets (buffer, sizeof (buffer), fh) != NULL)
243 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
247 sstrncpy (path, fields[0], sizeof (path));
248 escape_slashes (path, sizeof (path));
252 size = strtod (fields[2], &endptr);
253 if ((endptr == fields[2]) || (errno != 0))
258 used = strtod (fields[3], &endptr);
259 if ((endptr == fields[3]) || (errno != 0))
267 swap_submit_gauge (path, "used", used);
268 swap_submit_gauge (path, "free", free);
274 } /* }}} int swap_read_separate */
276 static int swap_read_combined (void) /* {{{ */
281 uint8_t have_data = 0;
282 gauge_t swap_used = 0.0;
283 gauge_t swap_cached = 0.0;
284 gauge_t swap_free = 0.0;
285 gauge_t swap_total = 0.0;
287 fh = fopen ("/proc/meminfo", "r");
291 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
296 while (fgets (buffer, sizeof (buffer), fh) != NULL)
301 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
305 if (strcasecmp (fields[0], "SwapTotal:") == 0)
307 swap_total = strtod (fields[1], /* endptr = */ NULL);
310 else if (strcasecmp (fields[0], "SwapFree:") == 0)
312 swap_free = strtod (fields[1], /* endptr = */ NULL);
315 else if (strcasecmp (fields[0], "SwapCached:") == 0)
317 swap_cached = strtod (fields[1], /* endptr = */ NULL);
324 if (have_data != 0x07)
327 if (isnan (swap_total)
328 || (swap_total <= 0.0)
329 || ((swap_free + swap_cached) > swap_total))
332 swap_used = swap_total - (swap_free + swap_cached);
334 swap_submit_gauge (NULL, "used", 1024.0 * swap_used);
335 swap_submit_gauge (NULL, "free", 1024.0 * swap_free);
336 swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
339 } /* }}} int swap_read_combined */
341 static int swap_read_io (void) /* {{{ */
346 _Bool old_kernel = 0;
348 uint8_t have_data = 0;
349 derive_t swap_in = 0;
350 derive_t swap_out = 0;
352 fh = fopen ("/proc/vmstat", "r");
355 /* /proc/vmstat does not exist in kernels <2.6 */
356 fh = fopen ("/proc/stat", "r");
360 WARNING ("swap: fopen: %s",
361 sstrerror (errno, errbuf, sizeof (errbuf)));
368 while (fgets (buffer, sizeof (buffer), fh) != NULL)
373 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
380 if (strcasecmp ("pswpin", fields[0]) == 0)
382 strtoderive (fields[1], &swap_in);
385 else if (strcasecmp ("pswpout", fields[0]) == 0)
387 strtoderive (fields[1], &swap_out);
391 else /* if (old_kernel) */
396 if (strcasecmp ("page", fields[0]) == 0)
398 strtoderive (fields[1], &swap_in);
399 strtoderive (fields[2], &swap_out);
402 } /* while (fgets) */
406 if (have_data != 0x03)
409 swap_submit_derive (NULL, "in", swap_in);
410 swap_submit_derive (NULL, "out", swap_out);
413 } /* }}} int swap_read_io */
415 static int swap_read (void) /* {{{ */
417 if (report_by_device)
418 swap_read_separate ();
420 swap_read_combined ();
425 } /* }}} int swap_read */
426 /* #endif KERNEL_LINUX */
429 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
430 * and kstat. The former reads physical space used on a device, the latter
431 * reports the view from the virtual memory system. It was decided that the
432 * kstat-based information should be moved to the "vmem" plugin, but nobody
433 * with enough Solaris experience was available at that time to do this. The
434 * code below is still there for your reference but it won't be activated in
435 * *this* plugin again. --octo
437 #elif 0 && HAVE_LIBKSTAT
438 /* kstat-based read function */
439 static int swap_read_kstat (void) /* {{{ */
447 if (swapctl (SC_AINFO, &ai) == -1)
450 ERROR ("swap plugin: swapctl failed: %s",
451 sstrerror (errno, errbuf, sizeof (errbuf)));
457 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
459 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
460 * /usr/include/vm/anon.h
462 * In short, swap -s shows: allocated + reserved = used, available
464 * However, Solaris does not allow to allocated/reserved more than the
465 * available swap (physical memory + disk swap), so the pedant may
466 * prefer: allocated + unallocated = reserved, available
468 * We map the above to: used + resv = n/a, free
470 * Does your brain hurt yet? - Christophe Kalt
472 * Oh, and in case you wonder,
473 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
474 * can suffer from a 32bit overflow.
476 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
477 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
479 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
481 swap_submit_gauge (NULL, "used", swap_alloc);
482 swap_submit_gauge (NULL, "free", swap_avail);
483 swap_submit_gauge (NULL, "reserved", swap_resv);
486 } /* }}} int swap_read_kstat */
487 /* #endif 0 && HAVE_LIBKSTAT */
489 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
490 /* swapctl-based read function */
491 static int swap_read (void) /* {{{ */
502 swap_num = swapctl (SC_GETNSWP, NULL);
505 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
509 else if (swap_num == 0)
512 /* Allocate and initialize the swaptbl_t structure */
513 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
516 ERROR ("swap plugin: smalloc failed.");
520 /* Memory to store the path names. We only use these paths when the
521 * separate option has been configured, but it's easier to just
522 * allocate enough memory in any case. */
523 s_paths = calloc (swap_num, PATH_MAX);
526 ERROR ("swap plugin: malloc failed.");
530 for (i = 0; i < swap_num; i++)
531 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
534 status = swapctl (SC_LIST, s);
538 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
539 sstrerror (errno, errbuf, sizeof (errbuf)));
544 else if (swap_num < status)
546 /* more elements returned than requested */
547 ERROR ("swap plugin: I allocated memory for %i structure%s, "
548 "but swapctl(2) claims to have returned %i. "
549 "I'm confused and will give up.",
550 swap_num, (swap_num == 1) ? "" : "s",
556 else if (swap_num > status)
557 /* less elements returned than requested */
560 for (i = 0; i < swap_num; i++)
566 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
569 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
570 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
572 /* Shortcut for the "combined" setting (default) */
573 if (!report_by_device)
580 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
581 escape_slashes (path, sizeof (path));
583 swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail));
584 swap_submit_gauge (path, "free", (gauge_t) this_avail);
585 } /* for (swap_num) */
589 ERROR ("swap plugin: Total swap space (%"PRIi64") "
590 "is less than free swap space (%"PRIi64").",
597 /* If the "separate" option was specified (report_by_device == 2), all
598 * values have already been dispatched from within the loop. */
599 if (!report_by_device)
601 swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
602 swap_submit_gauge (NULL, "free", (gauge_t) avail);
608 } /* }}} int swap_read */
609 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
611 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
612 static int swap_read (void) /* {{{ */
614 struct swapent *swap_entries;
622 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
625 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
629 else if (swap_num == 0)
632 swap_entries = calloc (swap_num, sizeof (*swap_entries));
633 if (swap_entries == NULL)
635 ERROR ("swap plugin: calloc failed.");
639 status = swapctl (SWAP_STATS, swap_entries, swap_num);
640 if (status != swap_num)
642 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
644 sfree (swap_entries);
648 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
649 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
651 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
654 for (i = 0; i < swap_num; i++)
656 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
659 used += ((derive_t) swap_entries[i].se_inuse)
661 total += ((derive_t) swap_entries[i].se_nblks)
667 ERROR ("swap plugin: Total swap space (%"PRIu64") "
668 "is less than used swap space (%"PRIu64").",
673 swap_submit_gauge (NULL, "used", (gauge_t) used);
674 swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
676 sfree (swap_entries);
679 } /* }}} int swap_read */
680 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
682 #elif defined(VM_SWAPUSAGE)
683 static int swap_read (void) /* {{{ */
687 struct xsw_usage sw_usage;
692 mib[1] = VM_SWAPUSAGE;
694 sw_usage_len = sizeof (struct xsw_usage);
696 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
699 /* The returned values are bytes. */
700 swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
701 swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
704 } /* }}} int swap_read */
705 /* #endif VM_SWAPUSAGE */
707 #elif HAVE_LIBKVM_GETSWAPINFO
708 static int swap_read (void) /* {{{ */
710 struct kvm_swap data_s;
720 /* only one structure => only get the grand total, no details */
721 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
725 total = (derive_t) data_s.ksw_total;
726 used = (derive_t) data_s.ksw_used;
728 total *= (derive_t) kvm_pagesize;
729 used *= (derive_t) kvm_pagesize;
733 swap_submit_gauge (NULL, "used", (gauge_t) used);
734 swap_submit_gauge (NULL, "free", (gauge_t) free);
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 ();
750 swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
751 swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
754 } /* }}} int swap_read */
755 /* #endif HAVE_LIBSTATGRAB */
758 static int swap_read (void) /* {{{ */
760 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
763 WARNING ("memory plugin: perfstat_memory_total failed: %s",
764 sstrerror (errno, errbuf, sizeof (errbuf)));
767 swap_submit_gauge (NULL, "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
768 swap_submit_gauge (NULL, "free", (gauge_t) pmemory.pgsp_free * pagesize );
771 } /* }}} int swap_read */
772 #endif /* HAVE_PERFSTAT */
774 void module_register (void)
777 plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
779 plugin_register_init ("swap", swap_init);
780 plugin_register_read ("swap", swap_read);
781 } /* void module_register */
783 /* vim: set fdm=marker : */