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 /* No global variables */
74 /* #endif KERNEL_LINUX */
76 #elif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS)
77 static derive_t pagesize;
80 # endif /* HAVE_LIBKSTAT */
82 # if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
83 static const char *config_keys[] =
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
93 static _Bool report_virtual = 0;
95 static int report_physical = 1;
96 # endif /* HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
97 /* #endif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS) */
99 #elif defined(VM_SWAPUSAGE)
100 /* No global variables */
101 /* #endif defined(VM_SWAPUSAGE) */
103 #elif HAVE_LIBKVM_GETSWAPINFO
104 static kvm_t *kvm_obj = NULL;
106 /* #endif HAVE_LIBKVM_GETSWAPINFO */
108 #elif HAVE_LIBSTATGRAB
109 /* No global variables */
110 /* #endif HAVE_LIBSTATGRAB */
114 static perfstat_memory_total_t pmemory;
115 /*# endif HAVE_PERFSTAT */
118 # error "No applicable input method."
119 #endif /* HAVE_LIBSTATGRAB */
121 static int swap_init (void)
125 /* #endif KERNEL_LINUX */
128 /* getpagesize(3C) tells me this does not fail.. */
129 pagesize = (derive_t) getpagesize ();
130 if (get_kstat (&ksp, "unix", 0, "system_pages"))
132 /* #endif HAVE_LIBKSTAT */
134 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
135 /* getpagesize(3C) tells me this does not fail.. */
136 pagesize = (derive_t) getpagesize ();
137 /* #endif HAVE_SWAPCTL */
139 #elif defined(VM_SWAPUSAGE)
141 /* #endif defined(VM_SWAPUSAGE) */
143 #elif HAVE_LIBKVM_GETSWAPINFO
150 kvm_pagesize = getpagesize ();
152 if ((kvm_obj = kvm_open (NULL, /* execfile */
155 O_RDONLY, /* flags */
159 ERROR ("swap plugin: kvm_open failed.");
162 /* #endif HAVE_LIBKVM_GETSWAPINFO */
164 #elif HAVE_LIBSTATGRAB
166 /* #endif HAVE_LIBSTATGRAB */
169 pagesize = getpagesize();
170 #endif /* HAVE_PERFSTAT */
175 static void swap_submit_inst (const char *plugin_instance, /* {{{ */
176 const char *type_instance, derive_t value, unsigned type)
179 value_list_t vl = VALUE_LIST_INIT;
184 values[0].gauge = (gauge_t) value;
185 sstrncpy (vl.type, "swap", sizeof (vl.type));
188 values[0].derive = value;
189 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
192 ERROR ("swap plugin: swap_submit called with wrong"
198 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
199 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
200 if (plugin_instance != NULL)
201 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
202 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
204 plugin_dispatch_values (&vl);
205 } /* }}} void swap_submit_inst */
207 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
209 swap_submit_inst (/* plugin instance = */ NULL,
210 type_instance, value, type);
214 static int swap_read (void) /* {{{ */
224 derive_t swap_used = 0;
225 derive_t swap_cached = 0;
226 derive_t swap_free = 0;
227 derive_t swap_total = 0;
228 derive_t swap_in = 0;
229 derive_t swap_out = 0;
231 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
234 WARNING ("memory: fopen: %s",
235 sstrerror (errno, errbuf, sizeof (errbuf)));
239 while (fgets (buffer, sizeof (buffer), fh) != NULL)
241 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
245 if (strcasecmp (fields[0], "SwapTotal:") == 0)
246 strtoderive (fields[1], &swap_total);
247 else if (strcasecmp (fields[0], "SwapFree:") == 0)
248 strtoderive (fields[1], &swap_free);
249 else if (strcasecmp (fields[0], "SwapCached:") == 0)
250 strtoderive (fields[1], &swap_cached);
256 WARNING ("memory: fclose: %s",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
260 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
263 swap_used = swap_total - (swap_free + swap_cached);
265 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
267 // /proc/vmstat does not exist in kernels <2.6
268 if ((fh = fopen ("/proc/stat", "r")) == NULL )
271 WARNING ("swap: fopen: %s",
272 sstrerror (errno, errbuf, sizeof (errbuf)));
279 while (fgets (buffer, sizeof (buffer), fh) != NULL)
281 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
288 if (strcasecmp ("pswpin", fields[0]) == 0)
289 strtoderive (fields[1], &swap_in);
290 else if (strcasecmp ("pswpout", fields[0]) == 0)
291 strtoderive (fields[1], &swap_out);
293 else /* if (old_kernel) */
298 if (strcasecmp ("page", fields[0]) == 0)
300 strtoderive (fields[1], &swap_in);
301 strtoderive (fields[2], &swap_out);
304 } /* while (fgets) */
309 WARNING ("swap: fclose: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
313 swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE);
314 swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE);
315 swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
316 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
317 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
320 } /* }}} int swap_read */
321 /* #endif KERNEL_LINUX */
323 /* Sorry for the amount of preprocessor magic that follows. Under Solaris, two
324 * mechanisms can be used to read swap statistics, swapctl and kstat. The
325 * former reads physical space used on a device, the latter reports the view
326 * from the virtual memory system. The following magic tries to be as flexible
327 * as possible by allowing either mechanism to be missing and act correctly in
328 * the event that both are available, i.e. let the user decide what to do. */
329 #elif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS)
331 /* kstat-based read function */
332 static int swap_read_kstat (void) /* {{{ */
340 if (swapctl (SC_AINFO, &ai) == -1)
343 ERROR ("swap plugin: swapctl failed: %s",
344 sstrerror (errno, errbuf, sizeof (errbuf)));
350 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
352 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
353 * /usr/include/vm/anon.h
355 * In short, swap -s shows: allocated + reserved = used, available
357 * However, Solaris does not allow to allocated/reserved more than the
358 * available swap (physical memory + disk swap), so the pedant may
359 * prefer: allocated + unallocated = reserved, available
361 * We map the above to: used + resv = n/a, free
363 * Does your brain hurt yet? - Christophe Kalt
365 * Oh, and in case you wonder,
366 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
367 * can suffer from a 32bit overflow.
369 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
370 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
372 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
374 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
375 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
376 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
379 } /* }}} int swap_read_kstat */
380 # endif /* HAVE_LIBKSTAT */
382 # if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
383 /* swapctl-based read function */
384 static int swap_read_swapctl2 (void) /* {{{ */
395 swap_num = swapctl (SC_GETNSWP, NULL);
398 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
402 else if (swap_num == 0)
405 /* Allocate and initialize the swaptbl_t structure */
406 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
409 ERROR ("swap plugin: smalloc failed.");
413 /* Memory to store the path names. We only use these paths when the
414 * separate option has been configured, but it's easier to just
415 * allocate enough memory in any case. */
416 s_paths = calloc (swap_num, PATH_MAX);
419 ERROR ("swap plugin: malloc failed.");
423 for (i = 0; i < swap_num; i++)
424 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
427 status = swapctl (SC_LIST, s);
431 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
432 sstrerror (errno, errbuf, sizeof (errbuf)));
437 else if (swap_num < status)
439 /* more elements returned than requested */
440 ERROR ("swap plugin: I allocated memory for %i structure%s, "
441 "but swapctl(2) claims to have returned %i. "
442 "I'm confused and will give up.",
443 swap_num, (swap_num == 1) ? "" : "s",
449 else if (swap_num > status)
450 /* less elements returned than requested */
453 for (i = 0; i < swap_num; i++)
459 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
462 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
463 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
465 /* Shortcut for the "combined" setting (default) */
466 if (report_physical != 2)
473 /* Okay, using "/" as swap device would be super-weird, but
474 * we'll handle it anyway to cover all cases. */
475 if (strcmp ("/", s->swt_ent[i].ste_path) == 0)
476 sstrncpy (path, "root", sizeof (path));
481 s->swt_ent[i].ste_path[PATH_MAX - 1] = 0;
482 /* Don't copy the leading slash */
483 sstrncpy (path, &s->swt_ent[i].ste_path[1], sizeof (path));
484 /* Convert slashes to dashes, just like the "df" plugin. */
485 for (j = 0; path[j] != 0; j++)
490 swap_submit_inst (path, "used", this_total - this_avail, DS_TYPE_GAUGE);
491 swap_submit_inst (path, "free", this_avail, DS_TYPE_GAUGE);
492 } /* for (swap_num) */
496 ERROR ("swap plugin: Total swap space (%"PRIi64") "
497 "is less than free swap space (%"PRIi64").",
504 /* If the "separate" option was specified (report_physical == 2), all
505 * values have already been dispatched from within the loop. */
506 if (report_physical != 2)
508 swap_submit ("used", total - avail, DS_TYPE_GAUGE);
509 swap_submit ("free", avail, DS_TYPE_GAUGE);
515 } /* }}} int swap_read_swapctl2 */
517 /* Configuration: Present when swapctl or both methods are available. */
518 static int swap_config (const char *key, const char *value) /* {{{ */
520 if (strcasecmp ("ReportPhysical", key) == 0)
522 if (strcasecmp ("combined", value) == 0)
524 else if (strcasecmp ("separate", value) == 0)
527 else if (IS_TRUE (value))
529 else if (IS_FALSE (value))
533 WARNING ("swap plugin: The value \"%s\" is not "
534 "recognized by the ReportPhysical option.",
538 else if (strcasecmp ("ReportVirtual", key) == 0)
542 else if (IS_FALSE (value))
545 WARNING ("swap plugin: The value \"%s\" is not "
546 "recognized by the ReportVirtual option.",
549 # endif /* HAVE_LIBKSTAT */
556 } /* }}} int swap_config */
557 # endif /* HAVE_SWAPCTL_TWO_ARGS */
559 /* If both methods are available, check the config variables to decide which
560 * function to call. Otherwise, add aliases for the functions so we can
561 * "swap_read" in "module_register". */
562 # if HAVE_LIBKSTAT && HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
563 static int swap_read (void) /* {{{ */
567 if (!report_physical && !report_virtual)
569 WARNING ("swap plugin: Neither the \"ReportPhysical\" nor the \"ReportVirtual\" option "
570 "has been activated. This plugin will not collect any data.");
576 status = swap_read_swapctl2 ();
583 status = swap_read_kstat ();
589 } /* }}} int swap_read */
591 # define swap_read swap_read_kstat
592 # else /* if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
593 # define swap_read swap_read_swapctl2
595 /* #endif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS) */
597 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
598 static int swap_read (void) /* {{{ */
600 struct swapent *swap_entries;
608 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
611 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
615 else if (swap_num == 0)
618 swap_entries = calloc (swap_num, sizeof (*swap_entries));
619 if (swap_entries == NULL)
621 ERROR ("swap plugin: calloc failed.");
625 status = swapctl (SWAP_STATS, swap_entries, swap_num);
626 if (status != swap_num)
628 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
630 sfree (swap_entries);
634 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
635 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
637 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
640 for (i = 0; i < swap_num; i++)
642 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
645 used += ((derive_t) swap_entries[i].se_inuse)
647 total += ((derive_t) swap_entries[i].se_nblks)
653 ERROR ("swap plugin: Total swap space (%"PRIu64") "
654 "is less than used swap space (%"PRIu64").",
659 swap_submit ("used", used, DS_TYPE_GAUGE);
660 swap_submit ("free", total - used, DS_TYPE_GAUGE);
662 sfree (swap_entries);
665 } /* }}} int swap_read */
666 /* #endif HAVE_SWAPCTL */
668 #elif defined(VM_SWAPUSAGE)
669 static int swap_read (void) /* {{{ */
673 struct xsw_usage sw_usage;
678 mib[1] = VM_SWAPUSAGE;
680 sw_usage_len = sizeof (struct xsw_usage);
682 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
685 /* The returned values are bytes. */
686 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
687 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
690 } /* }}} int swap_read */
691 /* #endif VM_SWAPUSAGE */
693 #elif HAVE_LIBKVM_GETSWAPINFO
694 static int swap_read (void) /* {{{ */
696 struct kvm_swap data_s;
706 /* only one structure => only get the grand total, no details */
707 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
711 total = (derive_t) data_s.ksw_total;
712 used = (derive_t) data_s.ksw_used;
714 total *= (derive_t) kvm_pagesize;
715 used *= (derive_t) kvm_pagesize;
719 swap_submit ("used", used, DS_TYPE_GAUGE);
720 swap_submit ("free", free, DS_TYPE_GAUGE);
723 } /* }}} int swap_read */
724 /* #endif HAVE_LIBKVM_GETSWAPINFO */
726 #elif HAVE_LIBSTATGRAB
727 static int swap_read (void) /* {{{ */
731 swap = sg_get_swap_stats ();
736 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
737 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
740 } /* }}} int swap_read */
741 /* #endif HAVE_LIBSTATGRAB */
744 static int swap_read (void) /* {{{ */
746 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
749 WARNING ("memory plugin: perfstat_memory_total failed: %s",
750 sstrerror (errno, errbuf, sizeof (errbuf)));
753 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
754 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
757 } /* }}} int swap_read */
758 #endif /* HAVE_PERFSTAT */
760 void module_register (void)
762 #if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
763 plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
765 plugin_register_init ("swap", swap_init);
766 plugin_register_read ("swap", swap_read);
767 } /* void module_register */
769 /* vim: set fdm=marker : */