2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
28 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
29 # undef _FILE_OFFSET_BITS
30 # undef _LARGEFILE64_SOURCE
38 # include <sys/swap.h>
44 # include <sys/param.h>
47 # include <sys/sysctl.h>
50 # include <sys/dkstat.h>
57 # include <statgrab.h>
61 #define MAX(x,y) ((x) > (y) ? (x) : (y))
64 /* No global variables */
65 /* #endif KERNEL_LINUX */
68 static derive_t pagesize;
70 /* #endif HAVE_LIBKSTAT */
73 /* No global variables */
74 /* #endif HAVE_SWAPCTL */
76 #elif defined(VM_SWAPUSAGE)
77 /* No global variables */
78 /* #endif defined(VM_SWAPUSAGE) */
80 #elif HAVE_LIBKVM_GETSWAPINFO
81 static kvm_t *kvm_obj = NULL;
83 /* #endif HAVE_LIBKVM_GETSWAPINFO */
85 #elif HAVE_LIBSTATGRAB
86 /* No global variables */
87 /* #endif HAVE_LIBSTATGRAB */
90 # error "No applicable input method."
91 #endif /* HAVE_LIBSTATGRAB */
93 static int swap_init (void)
97 /* #endif KERNEL_LINUX */
100 /* getpagesize(3C) tells me this does not fail.. */
101 pagesize = (derive_t) getpagesize ();
102 if (get_kstat (&ksp, "unix", 0, "system_pages"))
104 /* #endif HAVE_LIBKSTAT */
108 /* #endif HAVE_SWAPCTL */
110 #elif defined(VM_SWAPUSAGE)
112 /* #endif defined(VM_SWAPUSAGE) */
114 #elif HAVE_LIBKVM_GETSWAPINFO
121 kvm_pagesize = getpagesize ();
123 if ((kvm_obj = kvm_open (NULL, /* execfile */
126 O_RDONLY, /* flags */
130 ERROR ("swap plugin: kvm_open failed.");
133 /* #endif HAVE_LIBKVM_GETSWAPINFO */
135 #elif HAVE_LIBSTATGRAB
137 #endif /* HAVE_LIBSTATGRAB */
142 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
145 value_list_t vl = VALUE_LIST_INIT;
150 values[0].gauge = (gauge_t) value;
151 sstrncpy (vl.type, "swap", sizeof (vl.type));
154 values[0].derive = value;
155 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
158 ERROR ("swap plugin: swap_submit called with wrong"
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
165 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
166 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
168 plugin_dispatch_values (&vl);
169 } /* void swap_submit */
171 static int swap_read (void)
180 derive_t swap_used = 0;
181 derive_t swap_cached = 0;
182 derive_t swap_free = 0;
183 derive_t swap_total = 0;
184 derive_t swap_in = 0;
185 derive_t swap_out = 0;
187 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
190 WARNING ("memory: fopen: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
195 while (fgets (buffer, 1024, fh) != NULL)
197 derive_t *val = NULL;
199 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
201 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
203 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
208 numfields = strsplit (buffer, fields, 8);
213 *val = (derive_t) atoll (fields[1]) * 1024LL;
219 WARNING ("memory: fclose: %s",
220 sstrerror (errno, errbuf, sizeof (errbuf)));
223 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
226 swap_used = swap_total - (swap_free + swap_cached);
228 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
231 WARNING ("swap: fopen: %s",
232 sstrerror (errno, errbuf, sizeof (errbuf)));
236 while (fgets (buffer, 1024, fh) != NULL)
238 derive_t *val = NULL;
240 if (strncasecmp (buffer, "pswpin", 6) == 0)
242 else if (strncasecmp (buffer, "pswpout", 7) == 0)
247 numfields = strsplit (buffer, fields, 8);
252 *val = (derive_t) atoll (fields[1]);
258 WARNING ("swap: fclose: %s",
259 sstrerror (errno, errbuf, sizeof (errbuf)));
262 swap_submit ("used", swap_used, DS_TYPE_GAUGE);
263 swap_submit ("free", swap_free, DS_TYPE_GAUGE);
264 swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
265 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
266 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
268 /* #endif KERNEL_LINUX */
277 if (swapctl (SC_AINFO, &ai) == -1)
280 ERROR ("swap plugin: swapctl failed: %s",
281 sstrerror (errno, errbuf, sizeof (errbuf)));
287 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
289 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
290 * /usr/include/vm/anon.h
292 * In short, swap -s shows: allocated + reserved = used, available
294 * However, Solaris does not allow to allocated/reserved more than the
295 * available swap (physical memory + disk swap), so the pedant may
296 * prefer: allocated + unallocated = reserved, available
298 * We map the above to: used + resv = n/a, free
300 * Does your brain hurt yet? - Christophe Kalt
302 * Oh, and in case you wonder,
303 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
304 * can suffer from a 32bit overflow.
306 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
307 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
309 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
311 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
312 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
313 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
314 /* #endif HAVE_LIBKSTAT */
317 struct swapent *swap_entries;
326 * XXX: This is the syntax for the *BSD `swapctl', which has the
327 * following prototype:
328 * swapctl (int cmd, void *arg, int misc);
330 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
331 * too, but with the following prototype:
332 * swapctl (int cmd, void *arg);
334 * Solaris is usually handled in the KSTAT case above. For other UNIXes
335 * a separate case for the other version of `swapctl' may be necessary.
337 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
340 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
344 else if (swap_num == 0)
347 swap_entries = calloc (swap_num, sizeof (*swap_entries));
348 if (swap_entries == NULL)
350 ERROR ("swap plugin: calloc failed.");
354 status = swapctl (SWAP_STATS, swap_entries, swap_num);
355 if (status != swap_num)
357 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
359 sfree (swap_entries);
363 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
364 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
366 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
369 for (i = 0; i < swap_num; i++)
371 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
374 used += ((derive_t) swap_entries[i].se_inuse)
376 total += ((derive_t) swap_entries[i].se_nblks)
382 ERROR ("swap plugin: Total swap space (%"PRIu64") "
383 "is less than used swap space (%"PRIu64").",
388 swap_submit ("used", used, DS_TYPE_GAUGE);
389 swap_submit ("free", total - used, DS_TYPE_GAUGE);
391 sfree (swap_entries);
392 /* #endif HAVE_SWAPCTL */
394 #elif defined(VM_SWAPUSAGE)
397 struct xsw_usage sw_usage;
402 mib[1] = VM_SWAPUSAGE;
404 sw_usage_len = sizeof (struct xsw_usage);
406 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
409 /* The returned values are bytes. */
410 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
411 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
412 /* #endif VM_SWAPUSAGE */
414 #elif HAVE_LIBKVM_GETSWAPINFO
415 struct kvm_swap data_s;
425 /* only one structure => only get the grand total, no details */
426 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
430 total = (derive_t) data_s.ksw_total;
431 used = (derive_t) data_s.ksw_used;
433 total *= (derive_t) kvm_pagesize;
434 used *= (derive_t) kvm_pagesize;
438 swap_submit ("used", used, DS_TYPE_GAUGE);
439 swap_submit ("free", free, DS_TYPE_GAUGE);
440 /* #endif HAVE_LIBKVM_GETSWAPINFO */
442 #elif HAVE_LIBSTATGRAB
445 swap = sg_get_swap_stats ();
450 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
451 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
452 #endif /* HAVE_LIBSTATGRAB */
455 } /* int swap_read */
457 void module_register (void)
459 plugin_register_init ("swap", swap_init);
460 plugin_register_read ("swap", swap_read);
461 } /* void module_register */