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_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
77 static derive_t pagesize;
79 static const char *config_keys[] =
83 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
85 static _Bool report_by_device = 0;
86 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
88 #elif defined(VM_SWAPUSAGE)
89 /* No global variables */
90 /* #endif defined(VM_SWAPUSAGE) */
92 #elif HAVE_LIBKVM_GETSWAPINFO
93 static kvm_t *kvm_obj = NULL;
95 /* #endif HAVE_LIBKVM_GETSWAPINFO */
97 #elif HAVE_LIBSTATGRAB
98 /* No global variables */
99 /* #endif HAVE_LIBSTATGRAB */
103 static perfstat_memory_total_t pmemory;
104 /*# endif HAVE_PERFSTAT */
107 # error "No applicable input method."
108 #endif /* HAVE_LIBSTATGRAB */
110 static int swap_init (void)
114 /* #endif KERNEL_LINUX */
116 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
117 /* getpagesize(3C) tells me this does not fail.. */
118 pagesize = (derive_t) getpagesize ();
119 /* #endif HAVE_SWAPCTL */
121 #elif defined(VM_SWAPUSAGE)
123 /* #endif defined(VM_SWAPUSAGE) */
125 #elif HAVE_LIBKVM_GETSWAPINFO
132 kvm_pagesize = getpagesize ();
134 if ((kvm_obj = kvm_open (NULL, /* execfile */
137 O_RDONLY, /* flags */
141 ERROR ("swap plugin: kvm_open failed.");
144 /* #endif HAVE_LIBKVM_GETSWAPINFO */
146 #elif HAVE_LIBSTATGRAB
148 /* #endif HAVE_LIBSTATGRAB */
151 pagesize = getpagesize();
152 #endif /* HAVE_PERFSTAT */
157 static void swap_submit_inst (const char *plugin_instance, /* {{{ */
158 const char *type_instance, derive_t value, unsigned type)
161 value_list_t vl = VALUE_LIST_INIT;
166 values[0].gauge = (gauge_t) value;
167 sstrncpy (vl.type, "swap", sizeof (vl.type));
170 values[0].derive = value;
171 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
174 ERROR ("swap plugin: swap_submit called with wrong"
180 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
181 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
182 if (plugin_instance != NULL)
183 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
184 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
186 plugin_dispatch_values (&vl);
187 } /* }}} void swap_submit_inst */
189 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
191 swap_submit_inst (/* plugin instance = */ NULL,
192 type_instance, value, type);
196 static int swap_read (void) /* {{{ */
206 derive_t swap_used = 0;
207 derive_t swap_cached = 0;
208 derive_t swap_free = 0;
209 derive_t swap_total = 0;
210 derive_t swap_in = 0;
211 derive_t swap_out = 0;
213 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
216 WARNING ("memory: fopen: %s",
217 sstrerror (errno, errbuf, sizeof (errbuf)));
221 while (fgets (buffer, sizeof (buffer), fh) != NULL)
223 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
227 if (strcasecmp (fields[0], "SwapTotal:") == 0)
228 strtoderive (fields[1], &swap_total);
229 else if (strcasecmp (fields[0], "SwapFree:") == 0)
230 strtoderive (fields[1], &swap_free);
231 else if (strcasecmp (fields[0], "SwapCached:") == 0)
232 strtoderive (fields[1], &swap_cached);
238 WARNING ("memory: fclose: %s",
239 sstrerror (errno, errbuf, sizeof (errbuf)));
242 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
245 swap_used = swap_total - (swap_free + swap_cached);
247 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
249 // /proc/vmstat does not exist in kernels <2.6
250 if ((fh = fopen ("/proc/stat", "r")) == NULL )
253 WARNING ("swap: fopen: %s",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
261 while (fgets (buffer, sizeof (buffer), fh) != NULL)
263 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
270 if (strcasecmp ("pswpin", fields[0]) == 0)
271 strtoderive (fields[1], &swap_in);
272 else if (strcasecmp ("pswpout", fields[0]) == 0)
273 strtoderive (fields[1], &swap_out);
275 else /* if (old_kernel) */
280 if (strcasecmp ("page", fields[0]) == 0)
282 strtoderive (fields[1], &swap_in);
283 strtoderive (fields[2], &swap_out);
286 } /* while (fgets) */
291 WARNING ("swap: fclose: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
295 swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE);
296 swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE);
297 swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
298 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
299 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
302 } /* }}} int swap_read */
303 /* #endif KERNEL_LINUX */
306 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
307 * and kstat. The former reads physical space used on a device, the latter
308 * reports the view from the virtual memory system. It was decided that the
309 * kstat-based information should be moved to the "vmem" plugin, but nobody
310 * with enough Solaris experience was available at that time to do this. The
311 * code below is still there for your reference but it won't be activated in
312 * *this* plugin again. --octo
314 #elif 0 && HAVE_LIBKSTAT
315 /* kstat-based read function */
316 static int swap_read_kstat (void) /* {{{ */
324 if (swapctl (SC_AINFO, &ai) == -1)
327 ERROR ("swap plugin: swapctl failed: %s",
328 sstrerror (errno, errbuf, sizeof (errbuf)));
334 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
336 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
337 * /usr/include/vm/anon.h
339 * In short, swap -s shows: allocated + reserved = used, available
341 * However, Solaris does not allow to allocated/reserved more than the
342 * available swap (physical memory + disk swap), so the pedant may
343 * prefer: allocated + unallocated = reserved, available
345 * We map the above to: used + resv = n/a, free
347 * Does your brain hurt yet? - Christophe Kalt
349 * Oh, and in case you wonder,
350 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
351 * can suffer from a 32bit overflow.
353 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
354 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
356 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
358 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
359 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
360 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
363 } /* }}} int swap_read_kstat */
364 /* #endif 0 && HAVE_LIBKSTAT */
366 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
367 /* swapctl-based read function */
368 static int swap_read (void) /* {{{ */
379 swap_num = swapctl (SC_GETNSWP, NULL);
382 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
386 else if (swap_num == 0)
389 /* Allocate and initialize the swaptbl_t structure */
390 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
393 ERROR ("swap plugin: smalloc failed.");
397 /* Memory to store the path names. We only use these paths when the
398 * separate option has been configured, but it's easier to just
399 * allocate enough memory in any case. */
400 s_paths = calloc (swap_num, PATH_MAX);
403 ERROR ("swap plugin: malloc failed.");
407 for (i = 0; i < swap_num; i++)
408 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
411 status = swapctl (SC_LIST, s);
415 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
416 sstrerror (errno, errbuf, sizeof (errbuf)));
421 else if (swap_num < status)
423 /* more elements returned than requested */
424 ERROR ("swap plugin: I allocated memory for %i structure%s, "
425 "but swapctl(2) claims to have returned %i. "
426 "I'm confused and will give up.",
427 swap_num, (swap_num == 1) ? "" : "s",
433 else if (swap_num > status)
434 /* less elements returned than requested */
437 for (i = 0; i < swap_num; i++)
443 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
446 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
447 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
449 /* Shortcut for the "combined" setting (default) */
450 if (!report_by_device)
457 /* Okay, using "/" as swap device would be super-weird, but
458 * we'll handle it anyway to cover all cases. */
459 if (strcmp ("/", s->swt_ent[i].ste_path) == 0)
460 sstrncpy (path, "root", sizeof (path));
465 s->swt_ent[i].ste_path[PATH_MAX - 1] = 0;
466 /* Don't copy the leading slash */
467 sstrncpy (path, &s->swt_ent[i].ste_path[1], sizeof (path));
468 /* Convert slashes to dashes, just like the "df" plugin. */
469 for (j = 0; path[j] != 0; j++)
474 swap_submit_inst (path, "used", this_total - this_avail, DS_TYPE_GAUGE);
475 swap_submit_inst (path, "free", this_avail, DS_TYPE_GAUGE);
476 } /* for (swap_num) */
480 ERROR ("swap plugin: Total swap space (%"PRIi64") "
481 "is less than free swap space (%"PRIi64").",
488 /* If the "separate" option was specified (report_by_device == 2), all
489 * values have already been dispatched from within the loop. */
490 if (!report_by_device)
492 swap_submit ("used", total - avail, DS_TYPE_GAUGE);
493 swap_submit ("free", avail, DS_TYPE_GAUGE);
499 } /* }}} int swap_read_swapctl2 */
501 /* Configuration: Present when swapctl or both methods are available. */
502 static int swap_config (const char *key, const char *value) /* {{{ */
504 if (strcasecmp ("ReportByDevice", key) == 0)
507 report_by_device = 1;
509 report_by_device = 0;
517 } /* }}} int swap_config */
518 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
520 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
521 static int swap_read (void) /* {{{ */
523 struct swapent *swap_entries;
531 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
534 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
538 else if (swap_num == 0)
541 swap_entries = calloc (swap_num, sizeof (*swap_entries));
542 if (swap_entries == NULL)
544 ERROR ("swap plugin: calloc failed.");
548 status = swapctl (SWAP_STATS, swap_entries, swap_num);
549 if (status != swap_num)
551 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
553 sfree (swap_entries);
557 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
558 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
560 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
563 for (i = 0; i < swap_num; i++)
565 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
568 used += ((derive_t) swap_entries[i].se_inuse)
570 total += ((derive_t) swap_entries[i].se_nblks)
576 ERROR ("swap plugin: Total swap space (%"PRIu64") "
577 "is less than used swap space (%"PRIu64").",
582 swap_submit ("used", used, DS_TYPE_GAUGE);
583 swap_submit ("free", total - used, DS_TYPE_GAUGE);
585 sfree (swap_entries);
588 } /* }}} int swap_read */
589 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
591 #elif defined(VM_SWAPUSAGE)
592 static int swap_read (void) /* {{{ */
596 struct xsw_usage sw_usage;
601 mib[1] = VM_SWAPUSAGE;
603 sw_usage_len = sizeof (struct xsw_usage);
605 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
608 /* The returned values are bytes. */
609 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
610 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
613 } /* }}} int swap_read */
614 /* #endif VM_SWAPUSAGE */
616 #elif HAVE_LIBKVM_GETSWAPINFO
617 static int swap_read (void) /* {{{ */
619 struct kvm_swap data_s;
629 /* only one structure => only get the grand total, no details */
630 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
634 total = (derive_t) data_s.ksw_total;
635 used = (derive_t) data_s.ksw_used;
637 total *= (derive_t) kvm_pagesize;
638 used *= (derive_t) kvm_pagesize;
642 swap_submit ("used", used, DS_TYPE_GAUGE);
643 swap_submit ("free", free, DS_TYPE_GAUGE);
646 } /* }}} int swap_read */
647 /* #endif HAVE_LIBKVM_GETSWAPINFO */
649 #elif HAVE_LIBSTATGRAB
650 static int swap_read (void) /* {{{ */
654 swap = sg_get_swap_stats ();
659 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
660 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
663 } /* }}} int swap_read */
664 /* #endif HAVE_LIBSTATGRAB */
667 static int swap_read (void) /* {{{ */
669 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
672 WARNING ("memory plugin: perfstat_memory_total failed: %s",
673 sstrerror (errno, errbuf, sizeof (errbuf)));
676 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
677 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
680 } /* }}} int swap_read */
681 #endif /* HAVE_PERFSTAT */
683 void module_register (void)
685 #if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
686 plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
688 plugin_register_init ("swap", swap_init);
689 plugin_register_read ("swap", swap_read);
690 } /* void module_register */
692 /* vim: set fdm=marker : */