2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
5 * Copyright (C) 2009 Manuel Sanmartin
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
29 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
30 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
31 # undef _FILE_OFFSET_BITS
32 # undef _LARGEFILE64_SOURCE
40 # include <sys/swap.h>
46 # include <sys/param.h>
49 # include <sys/sysctl.h>
52 # include <sys/dkstat.h>
59 # include <statgrab.h>
63 # include <sys/protosw.h>
64 # include <libperfstat.h>
68 #define MAX(x,y) ((x) > (y) ? (x) : (y))
71 /* No global variables */
72 /* #endif KERNEL_LINUX */
75 static derive_t pagesize;
77 /* #endif HAVE_LIBKSTAT */
80 /* No global variables */
81 /* #endif HAVE_SWAPCTL */
83 #elif defined(VM_SWAPUSAGE)
84 /* No global variables */
85 /* #endif defined(VM_SWAPUSAGE) */
87 #elif HAVE_LIBKVM_GETSWAPINFO
88 static kvm_t *kvm_obj = NULL;
90 /* #endif HAVE_LIBKVM_GETSWAPINFO */
92 #elif HAVE_LIBSTATGRAB
93 /* No global variables */
94 /* #endif HAVE_LIBSTATGRAB */
98 static perfstat_memory_total_t pmemory;
99 /*# endif HAVE_PERFSTAT */
102 # error "No applicable input method."
103 #endif /* HAVE_LIBSTATGRAB */
105 static int swap_init (void)
109 /* #endif KERNEL_LINUX */
112 /* getpagesize(3C) tells me this does not fail.. */
113 pagesize = (derive_t) getpagesize ();
114 if (get_kstat (&ksp, "unix", 0, "system_pages"))
116 /* #endif HAVE_LIBKSTAT */
120 /* #endif HAVE_SWAPCTL */
122 #elif defined(VM_SWAPUSAGE)
124 /* #endif defined(VM_SWAPUSAGE) */
126 #elif HAVE_LIBKVM_GETSWAPINFO
133 kvm_pagesize = getpagesize ();
135 if ((kvm_obj = kvm_open (NULL, /* execfile */
138 O_RDONLY, /* flags */
142 ERROR ("swap plugin: kvm_open failed.");
145 /* #endif HAVE_LIBKVM_GETSWAPINFO */
147 #elif HAVE_LIBSTATGRAB
149 /* #endif HAVE_LIBSTATGRAB */
152 pagesize = getpagesize();
153 #endif /* HAVE_PERFSTAT */
158 static void swap_submit (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 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
184 plugin_dispatch_values (&vl);
185 } /* void swap_submit */
187 static int swap_read (void)
196 derive_t swap_used = 0;
197 derive_t swap_cached = 0;
198 derive_t swap_free = 0;
199 derive_t swap_total = 0;
200 derive_t swap_in = 0;
201 derive_t swap_out = 0;
203 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
206 WARNING ("memory: fopen: %s",
207 sstrerror (errno, errbuf, sizeof (errbuf)));
211 while (fgets (buffer, 1024, fh) != NULL)
213 derive_t *val = NULL;
215 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
217 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
219 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
224 numfields = strsplit (buffer, fields, 8);
229 *val = (derive_t) atoll (fields[1]) * 1024LL;
235 WARNING ("memory: fclose: %s",
236 sstrerror (errno, errbuf, sizeof (errbuf)));
239 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
242 swap_used = swap_total - (swap_free + swap_cached);
244 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
247 WARNING ("swap: fopen: %s",
248 sstrerror (errno, errbuf, sizeof (errbuf)));
252 while (fgets (buffer, 1024, fh) != NULL)
254 derive_t *val = NULL;
256 if (strncasecmp (buffer, "pswpin", 6) == 0)
258 else if (strncasecmp (buffer, "pswpout", 7) == 0)
263 numfields = strsplit (buffer, fields, 8);
268 *val = (derive_t) atoll (fields[1]);
274 WARNING ("swap: fclose: %s",
275 sstrerror (errno, errbuf, sizeof (errbuf)));
278 swap_submit ("used", swap_used, DS_TYPE_GAUGE);
279 swap_submit ("free", swap_free, DS_TYPE_GAUGE);
280 swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
281 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
282 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
284 /* #endif KERNEL_LINUX */
293 if (swapctl (SC_AINFO, &ai) == -1)
296 ERROR ("swap plugin: swapctl failed: %s",
297 sstrerror (errno, errbuf, sizeof (errbuf)));
303 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
305 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
306 * /usr/include/vm/anon.h
308 * In short, swap -s shows: allocated + reserved = used, available
310 * However, Solaris does not allow to allocated/reserved more than the
311 * available swap (physical memory + disk swap), so the pedant may
312 * prefer: allocated + unallocated = reserved, available
314 * We map the above to: used + resv = n/a, free
316 * Does your brain hurt yet? - Christophe Kalt
318 * Oh, and in case you wonder,
319 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
320 * can suffer from a 32bit overflow.
322 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
323 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
325 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
327 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
328 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
329 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
330 /* #endif HAVE_LIBKSTAT */
333 struct swapent *swap_entries;
342 * XXX: This is the syntax for the *BSD `swapctl', which has the
343 * following prototype:
344 * swapctl (int cmd, void *arg, int misc);
346 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
347 * too, but with the following prototype:
348 * swapctl (int cmd, void *arg);
350 * Solaris is usually handled in the KSTAT case above. For other UNIXes
351 * a separate case for the other version of `swapctl' may be necessary.
353 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
356 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
360 else if (swap_num == 0)
363 swap_entries = calloc (swap_num, sizeof (*swap_entries));
364 if (swap_entries == NULL)
366 ERROR ("swap plugin: calloc failed.");
370 status = swapctl (SWAP_STATS, swap_entries, swap_num);
371 if (status != swap_num)
373 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
375 sfree (swap_entries);
379 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
380 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
382 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
385 for (i = 0; i < swap_num; i++)
387 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
390 used += ((derive_t) swap_entries[i].se_inuse)
392 total += ((derive_t) swap_entries[i].se_nblks)
398 ERROR ("swap plugin: Total swap space (%"PRIu64") "
399 "is less than used swap space (%"PRIu64").",
404 swap_submit ("used", used, DS_TYPE_GAUGE);
405 swap_submit ("free", total - used, DS_TYPE_GAUGE);
407 sfree (swap_entries);
408 /* #endif HAVE_SWAPCTL */
410 #elif defined(VM_SWAPUSAGE)
413 struct xsw_usage sw_usage;
418 mib[1] = VM_SWAPUSAGE;
420 sw_usage_len = sizeof (struct xsw_usage);
422 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
425 /* The returned values are bytes. */
426 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
427 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
428 /* #endif VM_SWAPUSAGE */
430 #elif HAVE_LIBKVM_GETSWAPINFO
431 struct kvm_swap data_s;
441 /* only one structure => only get the grand total, no details */
442 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
446 total = (derive_t) data_s.ksw_total;
447 used = (derive_t) data_s.ksw_used;
449 total *= (derive_t) kvm_pagesize;
450 used *= (derive_t) kvm_pagesize;
454 swap_submit ("used", used, DS_TYPE_GAUGE);
455 swap_submit ("free", free, DS_TYPE_GAUGE);
456 /* #endif HAVE_LIBKVM_GETSWAPINFO */
458 #elif HAVE_LIBSTATGRAB
461 swap = sg_get_swap_stats ();
466 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
467 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
468 /* #endif HAVE_LIBSTATGRAB */
471 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
474 WARNING ("memory plugin: perfstat_memory_total failed: %s",
475 sstrerror (errno, errbuf, sizeof (errbuf)));
478 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
479 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
480 #endif /* HAVE_PERFSTAT */
483 } /* int swap_read */
485 void module_register (void)
487 plugin_register_init ("swap", swap_init);
488 plugin_register_read ("swap", swap_read);
489 } /* void module_register */