2 * collectd - src/swap.c
3 * Copyright (C) 2005-2012 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 static derive_t pagesize;
72 static _Bool report_bytes = 0;
73 /* #endif KERNEL_LINUX */
76 static derive_t pagesize;
78 /* #endif HAVE_LIBKSTAT */
81 /* No global variables */
82 /* #endif HAVE_SWAPCTL */
84 #elif defined(VM_SWAPUSAGE)
85 /* No global variables */
86 /* #endif defined(VM_SWAPUSAGE) */
88 #elif HAVE_LIBKVM_GETSWAPINFO
89 static kvm_t *kvm_obj = NULL;
91 /* #endif HAVE_LIBKVM_GETSWAPINFO */
93 #elif HAVE_LIBSTATGRAB
94 /* No global variables */
95 /* #endif HAVE_LIBSTATGRAB */
99 static perfstat_memory_total_t pmemory;
100 /*# endif HAVE_PERFSTAT */
103 # error "No applicable input method."
104 #endif /* HAVE_LIBSTATGRAB */
106 static const char *config_keys[] =
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 static int swap_init (void)
115 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
116 /* #endif KERNEL_LINUX */
119 /* getpagesize(3C) tells me this does not fail.. */
120 pagesize = (derive_t) getpagesize ();
121 if (get_kstat (&ksp, "unix", 0, "system_pages"))
123 /* #endif HAVE_LIBKSTAT */
127 /* #endif HAVE_SWAPCTL */
129 #elif defined(VM_SWAPUSAGE)
131 /* #endif defined(VM_SWAPUSAGE) */
133 #elif HAVE_LIBKVM_GETSWAPINFO
140 kvm_pagesize = getpagesize ();
142 if ((kvm_obj = kvm_open (NULL, /* execfile */
145 O_RDONLY, /* flags */
149 ERROR ("swap plugin: kvm_open failed.");
152 /* #endif HAVE_LIBKVM_GETSWAPINFO */
154 #elif HAVE_LIBSTATGRAB
156 /* #endif HAVE_LIBSTATGRAB */
159 pagesize = getpagesize();
160 #endif /* HAVE_PERFSTAT */
165 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
168 value_list_t vl = VALUE_LIST_INIT;
173 values[0].gauge = (gauge_t) value;
174 sstrncpy (vl.type, "swap", sizeof (vl.type));
177 values[0].derive = value;
178 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
181 ERROR ("swap plugin: swap_submit called with wrong"
187 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
188 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
189 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
191 plugin_dispatch_values (&vl);
192 } /* void swap_submit */
194 static int swap_read (void)
205 derive_t swap_used = 0;
206 derive_t swap_cached = 0;
207 derive_t swap_free = 0;
208 derive_t swap_total = 0;
209 derive_t swap_in = 0;
210 derive_t swap_out = 0;
212 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
215 WARNING ("memory: fopen: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
220 while (fgets (buffer, sizeof (buffer), fh) != NULL)
222 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
226 if (strcasecmp (fields[0], "SwapTotal:") == 0)
227 strtoderive (fields[1], &swap_total);
228 else if (strcasecmp (fields[0], "SwapFree:") == 0)
229 strtoderive (fields[1], &swap_free);
230 else if (strcasecmp (fields[0], "SwapCached:") == 0)
231 strtoderive (fields[1], &swap_cached);
237 WARNING ("memory: fclose: %s",
238 sstrerror (errno, errbuf, sizeof (errbuf)));
241 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
244 swap_used = swap_total - (swap_free + swap_cached);
246 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
248 // /proc/vmstat does not exist in kernels <2.6
249 if ((fh = fopen ("/proc/stat", "r")) == NULL )
252 WARNING ("swap: fopen: %s",
253 sstrerror (errno, errbuf, sizeof (errbuf)));
260 while (fgets (buffer, sizeof (buffer), fh) != NULL)
262 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
269 if (strcasecmp ("pswpin", fields[0]) == 0)
270 strtoderive (fields[1], &swap_in);
271 else if (strcasecmp ("pswpout", fields[0]) == 0)
272 strtoderive (fields[1], &swap_out);
274 else /* if (old_kernel) */
279 if (strcasecmp ("page", fields[0]) == 0)
281 strtoderive (fields[1], &swap_in);
282 strtoderive (fields[2], &swap_out);
285 } /* while (fgets) */
290 WARNING ("swap: fclose: %s",
291 sstrerror (errno, errbuf, sizeof (errbuf)));
297 swap_out *= pagesize;
300 swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE);
301 swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE);
302 swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
303 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
304 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
305 /* #endif KERNEL_LINUX */
314 if (swapctl (SC_AINFO, &ai) == -1)
317 ERROR ("swap plugin: swapctl failed: %s",
318 sstrerror (errno, errbuf, sizeof (errbuf)));
324 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
326 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
327 * /usr/include/vm/anon.h
329 * In short, swap -s shows: allocated + reserved = used, available
331 * However, Solaris does not allow to allocated/reserved more than the
332 * available swap (physical memory + disk swap), so the pedant may
333 * prefer: allocated + unallocated = reserved, available
335 * We map the above to: used + resv = n/a, free
337 * Does your brain hurt yet? - Christophe Kalt
339 * Oh, and in case you wonder,
340 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
341 * can suffer from a 32bit overflow.
343 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
344 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
346 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
348 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
349 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
350 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
351 /* #endif HAVE_LIBKSTAT */
354 struct swapent *swap_entries;
363 * XXX: This is the syntax for the *BSD `swapctl', which has the
364 * following prototype:
365 * swapctl (int cmd, void *arg, int misc);
367 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
368 * too, but with the following prototype:
369 * swapctl (int cmd, void *arg);
371 * Solaris is usually handled in the KSTAT case above. For other UNIXes
372 * a separate case for the other version of `swapctl' may be necessary.
374 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
377 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
381 else if (swap_num == 0)
384 swap_entries = calloc (swap_num, sizeof (*swap_entries));
385 if (swap_entries == NULL)
387 ERROR ("swap plugin: calloc failed.");
391 status = swapctl (SWAP_STATS, swap_entries, swap_num);
392 if (status != swap_num)
394 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
396 sfree (swap_entries);
400 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
401 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
403 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
406 for (i = 0; i < swap_num; i++)
408 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
411 used += ((derive_t) swap_entries[i].se_inuse)
413 total += ((derive_t) swap_entries[i].se_nblks)
419 ERROR ("swap plugin: Total swap space (%"PRIu64") "
420 "is less than used swap space (%"PRIu64").",
425 swap_submit ("used", used, DS_TYPE_GAUGE);
426 swap_submit ("free", total - used, DS_TYPE_GAUGE);
428 sfree (swap_entries);
429 /* #endif HAVE_SWAPCTL */
431 #elif defined(VM_SWAPUSAGE)
434 struct xsw_usage sw_usage;
439 mib[1] = VM_SWAPUSAGE;
441 sw_usage_len = sizeof (struct xsw_usage);
443 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
446 /* The returned values are bytes. */
447 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
448 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
449 /* #endif VM_SWAPUSAGE */
451 #elif HAVE_LIBKVM_GETSWAPINFO
452 struct kvm_swap data_s;
462 /* only one structure => only get the grand total, no details */
463 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
467 total = (derive_t) data_s.ksw_total;
468 used = (derive_t) data_s.ksw_used;
470 total *= (derive_t) kvm_pagesize;
471 used *= (derive_t) kvm_pagesize;
475 swap_submit ("used", used, DS_TYPE_GAUGE);
476 swap_submit ("free", free, DS_TYPE_GAUGE);
477 /* #endif HAVE_LIBKVM_GETSWAPINFO */
479 #elif HAVE_LIBSTATGRAB
482 swap = sg_get_swap_stats ();
487 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
488 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
489 /* #endif HAVE_LIBSTATGRAB */
492 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
495 WARNING ("memory plugin: perfstat_memory_total failed: %s",
496 sstrerror (errno, errbuf, sizeof (errbuf)));
499 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
500 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
501 #endif /* HAVE_PERFSTAT */
504 } /* int swap_read */
506 static int swap_config (const char *key, const char *value)
508 if (strcasecmp ("ReportBytes", key) == 0)
511 report_bytes = IS_TRUE (value) ? 1 : 0;
513 WARNING ("swap plugin: The \"ReportBytes\" option is only "
514 "valid under Linux. The option is going to "
524 } /* int swap_config */
526 void module_register (void)
528 plugin_register_config ("swap", swap_config,
529 config_keys, config_keys_num);
530 plugin_register_init ("swap", swap_init);
531 plugin_register_read ("swap", swap_read);
532 } /* void module_register */