2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
27 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
28 # undef _FILE_OFFSET_BITS
29 # undef _LARGEFILE64_SOURCE
37 # include <sys/swap.h>
43 # include <sys/param.h>
46 # include <sys/sysctl.h>
49 # include <sys/dkstat.h>
56 # include <statgrab.h>
60 #define MAX(x,y) ((x) > (y) ? (x) : (y))
63 /* No global variables */
64 /* #endif KERNEL_LINUX */
67 static unsigned long long pagesize;
69 /* #endif HAVE_LIBKSTAT */
72 /* No global variables */
73 /* #endif HAVE_SWAPCTL */
75 #elif defined(VM_SWAPUSAGE)
76 /* No global variables */
77 /* #endif defined(VM_SWAPUSAGE) */
79 #elif HAVE_LIBKVM_GETSWAPINFO
80 static kvm_t *kvm_obj = NULL;
82 /* #endif HAVE_LIBKVM_GETSWAPINFO */
84 #elif HAVE_LIBSTATGRAB
85 /* No global variables */
86 /* #endif HAVE_LIBSTATGRAB */
89 # error "No applicable input method."
90 #endif /* HAVE_LIBSTATGRAB */
92 static int swap_init (void)
96 /* #endif KERNEL_LINUX */
99 /* getpagesize(3C) tells me this does not fail.. */
100 pagesize = (unsigned long long) getpagesize ();
101 if (get_kstat (&ksp, "unix", 0, "system_pages"))
103 /* #endif HAVE_LIBKSTAT */
107 /* #endif HAVE_SWAPCTL */
109 #elif defined(VM_SWAPUSAGE)
111 /* #endif defined(VM_SWAPUSAGE) */
113 #elif HAVE_LIBKVM_GETSWAPINFO
120 kvm_pagesize = getpagesize ();
122 if ((kvm_obj = kvm_open (NULL, /* execfile */
125 O_RDONLY, /* flags */
129 ERROR ("swap plugin: kvm_open failed.");
132 /* #endif HAVE_LIBKVM_GETSWAPINFO */
134 #elif HAVE_LIBSTATGRAB
136 #endif /* HAVE_LIBSTATGRAB */
141 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
144 value_list_t vl = VALUE_LIST_INIT;
149 values[0].gauge = value;
150 sstrncpy (vl.type, "swap", sizeof (vl.type));
153 values[0].derive = value;
154 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
157 ERROR ("swap plugin: swap_submit called with wrong"
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
165 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
167 plugin_dispatch_values (&vl);
168 } /* void swap_submit */
170 static int swap_read (void)
179 unsigned long long swap_used = 0LL;
180 unsigned long long swap_cached = 0LL;
181 unsigned long long swap_free = 0LL;
182 unsigned long long swap_total = 0LL;
183 unsigned long long swap_in = 0LL;
184 unsigned long long swap_out = 0LL;
186 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
189 WARNING ("memory: fopen: %s",
190 sstrerror (errno, errbuf, sizeof (errbuf)));
194 while (fgets (buffer, 1024, fh) != NULL)
196 unsigned long long *val = NULL;
198 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
200 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
202 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
207 numfields = strsplit (buffer, fields, 8);
212 *val = atoll (fields[1]) * 1024LL;
218 WARNING ("memory: fclose: %s",
219 sstrerror (errno, errbuf, sizeof (errbuf)));
222 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
225 swap_used = swap_total - (swap_free + swap_cached);
227 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
230 WARNING ("swap: fopen: %s",
231 sstrerror (errno, errbuf, sizeof (errbuf)));
235 while (fgets (buffer, 1024, fh) != NULL)
237 unsigned long long *val = NULL;
239 if (strncasecmp (buffer, "pswpin", 6) == 0)
241 else if (strncasecmp (buffer, "pswpout", 7) == 0)
246 numfields = strsplit (buffer, fields, 8);
251 *val = atoll (fields[1]);
257 WARNING ("swap: fclose: %s",
258 sstrerror (errno, errbuf, sizeof (errbuf)));
261 swap_submit ("used", swap_used, DS_TYPE_GAUGE);
262 swap_submit ("free", swap_free, DS_TYPE_GAUGE);
263 swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
264 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
265 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
267 /* #endif KERNEL_LINUX */
270 unsigned long long swap_alloc;
271 unsigned long long swap_resv;
272 unsigned long long swap_avail;
276 if (swapctl (SC_AINFO, &ai) == -1)
279 ERROR ("swap plugin: swapctl failed: %s",
280 sstrerror (errno, errbuf, sizeof (errbuf)));
286 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
288 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
289 * /usr/include/vm/anon.h
291 * In short, swap -s shows: allocated + reserved = used, available
293 * However, Solaris does not allow to allocated/reserved more than the
294 * available swap (physical memory + disk swap), so the pedant may
295 * prefer: allocated + unallocated = reserved, available
297 * We map the above to: used + resv = n/a, free
299 * Does your brain hurt yet? - Christophe Kalt
301 * Oh, and in case you wonder,
302 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
303 * can suffer from a 32bit overflow.
305 swap_alloc = ai.ani_max - ai.ani_free;
306 swap_alloc *= pagesize;
307 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
308 swap_resv *= pagesize;
309 swap_avail = ai.ani_max - ai.ani_resv;
310 swap_avail *= pagesize;
312 swap_submit ("used", swap_alloc);
313 swap_submit ("free", swap_avail);
314 swap_submit ("reserved", swap_resv);
315 /* #endif HAVE_LIBKSTAT */
318 struct swapent *swap_entries;
327 * XXX: This is the syntax for the *BSD `swapctl', which has the
328 * following prototype:
329 * swapctl (int cmd, void *arg, int misc);
331 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
332 * too, but with the following prototype:
333 * swapctl (int cmd, void *arg);
335 * Solaris is usually handled in the KSTAT case above. For other UNIXes
336 * a separate case for the other version of `swapctl' may be necessary.
338 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
341 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
345 else if (swap_num == 0)
348 swap_entries = calloc (swap_num, sizeof (*swap_entries));
349 if (swap_entries == NULL)
351 ERROR ("swap plugin: calloc failed.");
355 status = swapctl (SWAP_STATS, swap_entries, swap_num);
356 if (status != swap_num)
358 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
360 sfree (swap_entries);
364 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
365 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
367 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
370 for (i = 0; i < swap_num; i++)
372 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
375 used += ((uint64_t) swap_entries[i].se_inuse)
377 total += ((uint64_t) swap_entries[i].se_nblks)
383 ERROR ("swap plugin: Total swap space (%"PRIu64") "
384 "is less than used swap space (%"PRIu64").",
389 swap_submit ("used", (gauge_t) used);
390 swap_submit ("free", (gauge_t) (total - used));
392 sfree (swap_entries);
393 /* #endif HAVE_SWAPCTL */
395 #elif defined(VM_SWAPUSAGE)
398 struct xsw_usage sw_usage;
403 mib[1] = VM_SWAPUSAGE;
405 sw_usage_len = sizeof (struct xsw_usage);
407 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
410 /* The returned values are bytes. */
411 swap_submit ("used", sw_usage.xsu_used);
412 swap_submit ("free", sw_usage.xsu_avail);
413 /* #endif VM_SWAPUSAGE */
415 #elif HAVE_LIBKVM_GETSWAPINFO
416 struct kvm_swap data_s;
419 unsigned long long used;
420 unsigned long long free;
421 unsigned long long total;
426 /* only one structure => only get the grand total, no details */
427 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
431 total = (unsigned long long) data_s.ksw_total;
432 used = (unsigned long long) data_s.ksw_used;
434 total *= (unsigned long long) kvm_pagesize;
435 used *= (unsigned long long) kvm_pagesize;
439 swap_submit ("used", used);
440 swap_submit ("free", free);
441 /* #endif HAVE_LIBKVM_GETSWAPINFO */
443 #elif HAVE_LIBSTATGRAB
446 swap = sg_get_swap_stats ();
451 swap_submit ("used", swap->used);
452 swap_submit ("free", swap->free);
453 #endif /* HAVE_LIBSTATGRAB */
456 } /* int swap_read */
458 void module_register (void)
460 plugin_register_init ("swap", swap_init);
461 plugin_register_read ("swap", swap_read);
462 } /* void module_register */