1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
5 * Copyright (C) 2009 Manuel Sanmartin
6 * Copyright (C) 2010 Aurélien Reynaud
7 *
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.
11 *
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.
16 *
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
20 *
21 * Authors:
22 * Florian octo Forster <octo at collectd.org>
23 * Manuel Sanmartin
24 * Aurélien Reynaud <collectd at wattapower.net>
25 **/
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #undef HAVE_CONFIG_H
30 #endif
31 /* avoid swap.h error "Cannot use swapctl in the large files compilation
32 * environment" */
33 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
34 #undef _FILE_OFFSET_BITS
35 #undef _LARGEFILE64_SOURCE
36 #endif
38 #include "collectd.h"
40 #include "common.h"
41 #include "plugin.h"
43 #if HAVE_SYS_SWAP_H
44 #include <sys/swap.h>
45 #endif
46 #if HAVE_VM_ANON_H
47 #include <vm/anon.h>
48 #endif
49 #if HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
52 #if HAVE_SYS_SYSCTL_H
53 #include <sys/sysctl.h>
54 #endif
55 #if HAVE_SYS_DKSTAT_H
56 #include <sys/dkstat.h>
57 #endif
58 #if HAVE_KVM_H
59 #include <kvm.h>
60 #endif
62 #if HAVE_STATGRAB_H
63 #include <statgrab.h>
64 #endif
66 #if HAVE_PERFSTAT
67 #include <libperfstat.h>
68 #include <sys/protosw.h>
69 #endif
71 #undef MAX
72 #define MAX(x, y) ((x) > (y) ? (x) : (y))
74 #if KERNEL_LINUX
75 #define SWAP_HAVE_REPORT_BY_DEVICE 1
76 static derive_t pagesize;
77 static _Bool report_bytes = 0;
78 static _Bool report_by_device = 0;
79 /* #endif KERNEL_LINUX */
81 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
82 #define SWAP_HAVE_REPORT_BY_DEVICE 1
83 static derive_t pagesize;
84 static _Bool report_by_device = 0;
85 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
87 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
88 /* No global variables */
89 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
91 #elif defined(VM_SWAPUSAGE)
92 /* No global variables */
93 /* #endif defined(VM_SWAPUSAGE) */
95 #elif HAVE_LIBKVM_GETSWAPINFO
96 static kvm_t *kvm_obj = NULL;
97 int kvm_pagesize;
98 /* #endif HAVE_LIBKVM_GETSWAPINFO */
100 #elif HAVE_LIBSTATGRAB
101 /* No global variables */
102 /* #endif HAVE_LIBSTATGRAB */
104 #elif HAVE_PERFSTAT
105 static int pagesize;
106 /*# endif HAVE_PERFSTAT */
108 #else
109 #error "No applicable input method."
110 #endif /* HAVE_LIBSTATGRAB */
112 static _Bool values_absolute = 1;
113 static _Bool values_percentage = 0;
114 static _Bool report_io = 1;
116 static int swap_config(oconfig_item_t *ci) /* {{{ */
117 {
118 for (int i = 0; i < ci->children_num; i++) {
119 oconfig_item_t *child = ci->children + i;
120 if (strcasecmp("ReportBytes", child->key) == 0)
121 #if KERNEL_LINUX
122 cf_util_get_boolean(child, &report_bytes);
123 #else
124 WARNING("swap plugin: The \"ReportBytes\" option "
125 "is only valid under Linux. "
126 "The option is going to be ignored.");
127 #endif
128 else if (strcasecmp("ReportByDevice", child->key) == 0)
129 #if SWAP_HAVE_REPORT_BY_DEVICE
130 cf_util_get_boolean(child, &report_by_device);
131 #else
132 WARNING("swap plugin: The \"ReportByDevice\" option "
133 "is not supported on this platform. "
134 "The option is going to be ignored.");
135 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
136 else if (strcasecmp("ValuesAbsolute", child->key) == 0)
137 cf_util_get_boolean(child, &values_absolute);
138 else if (strcasecmp("ValuesPercentage", child->key) == 0)
139 cf_util_get_boolean(child, &values_percentage);
140 else if (strcasecmp("ReportIO", child->key) == 0)
141 cf_util_get_boolean(child, &report_io);
142 else
143 WARNING("swap plugin: Unknown config option: \"%s\"", child->key);
144 }
146 return 0;
147 } /* }}} int swap_config */
149 static int swap_init(void) /* {{{ */
150 {
151 #if KERNEL_LINUX
152 pagesize = (derive_t)sysconf(_SC_PAGESIZE);
153 /* #endif KERNEL_LINUX */
155 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
156 /* getpagesize(3C) tells me this does not fail.. */
157 pagesize = (derive_t)getpagesize();
158 /* #endif HAVE_SWAPCTL */
160 #elif defined(VM_SWAPUSAGE)
161 /* No init stuff */
162 /* #endif defined(VM_SWAPUSAGE) */
164 #elif HAVE_LIBKVM_GETSWAPINFO
165 char errbuf[_POSIX2_LINE_MAX];
167 if (kvm_obj != NULL) {
168 kvm_close(kvm_obj);
169 kvm_obj = NULL;
170 }
172 kvm_pagesize = getpagesize();
174 kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
176 if (kvm_obj == NULL) {
177 ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
178 return -1;
179 }
180 /* #endif HAVE_LIBKVM_GETSWAPINFO */
182 #elif HAVE_LIBSTATGRAB
183 /* No init stuff */
184 /* #endif HAVE_LIBSTATGRAB */
186 #elif HAVE_PERFSTAT
187 pagesize = getpagesize();
188 #endif /* HAVE_PERFSTAT */
190 return 0;
191 } /* }}} int swap_init */
193 static void swap_submit_usage(char const *plugin_instance, /* {{{ */
194 gauge_t used, gauge_t free,
195 char const *other_name, gauge_t other_value) {
196 value_list_t vl = VALUE_LIST_INIT;
198 vl.values = &(value_t){.gauge = NAN};
199 vl.values_len = 1;
200 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
201 if (plugin_instance != NULL)
202 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
203 sstrncpy(vl.type, "swap", sizeof(vl.type));
205 if (values_absolute)
206 plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free",
207 free, other_name, other_value, NULL);
208 if (values_percentage)
209 plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free",
210 free, other_name, other_value, NULL);
211 } /* }}} void swap_submit_usage */
213 #if KERNEL_LINUX || HAVE_PERFSTAT
214 __attribute__((nonnull(1))) static void
215 swap_submit_derive(char const *type_instance, /* {{{ */
216 derive_t value) {
217 value_list_t vl = VALUE_LIST_INIT;
219 vl.values = &(value_t){.derive = value};
220 vl.values_len = 1;
221 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
222 sstrncpy(vl.type, "swap_io", sizeof(vl.type));
223 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
225 plugin_dispatch_values(&vl);
226 } /* }}} void swap_submit_derive */
227 #endif
229 #if KERNEL_LINUX
230 static int swap_read_separate(void) /* {{{ */
231 {
232 FILE *fh;
233 char buffer[1024];
235 fh = fopen("/proc/swaps", "r");
236 if (fh == NULL) {
237 char errbuf[1024];
238 WARNING("swap plugin: fopen (/proc/swaps) failed: %s",
239 sstrerror(errno, errbuf, sizeof(errbuf)));
240 return -1;
241 }
243 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
244 char *fields[8];
245 int numfields;
246 char *endptr;
248 char path[PATH_MAX];
249 gauge_t total;
250 gauge_t used;
252 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
253 if (numfields != 5)
254 continue;
256 sstrncpy(path, fields[0], sizeof(path));
257 escape_slashes(path, sizeof(path));
259 errno = 0;
260 endptr = NULL;
261 total = strtod(fields[2], &endptr);
262 if ((endptr == fields[2]) || (errno != 0))
263 continue;
265 errno = 0;
266 endptr = NULL;
267 used = strtod(fields[3], &endptr);
268 if ((endptr == fields[3]) || (errno != 0))
269 continue;
271 if (total < used)
272 continue;
274 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
275 }
277 fclose(fh);
279 return 0;
280 } /* }}} int swap_read_separate */
282 static int swap_read_combined(void) /* {{{ */
283 {
284 FILE *fh;
285 char buffer[1024];
287 gauge_t swap_used = NAN;
288 gauge_t swap_cached = NAN;
289 gauge_t swap_free = NAN;
290 gauge_t swap_total = NAN;
292 fh = fopen("/proc/meminfo", "r");
293 if (fh == NULL) {
294 char errbuf[1024];
295 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s",
296 sstrerror(errno, errbuf, sizeof(errbuf)));
297 return -1;
298 }
300 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
301 char *fields[8];
302 int numfields;
304 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
305 if (numfields < 2)
306 continue;
308 if (strcasecmp(fields[0], "SwapTotal:") == 0)
309 strtogauge(fields[1], &swap_total);
310 else if (strcasecmp(fields[0], "SwapFree:") == 0)
311 strtogauge(fields[1], &swap_free);
312 else if (strcasecmp(fields[0], "SwapCached:") == 0)
313 strtogauge(fields[1], &swap_cached);
314 }
316 fclose(fh);
318 if (isnan(swap_total) || isnan(swap_free))
319 return ENOENT;
321 /* Some systems, OpenVZ for example, don't provide SwapCached. */
322 if (isnan(swap_cached))
323 swap_used = swap_total - swap_free;
324 else
325 swap_used = swap_total - (swap_free + swap_cached);
326 assert(!isnan(swap_used));
328 if (swap_used < 0.0)
329 return EINVAL;
331 swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
332 isnan(swap_cached) ? NULL : "cached",
333 isnan(swap_cached) ? NAN : swap_cached * 1024.0);
334 return 0;
335 } /* }}} int swap_read_combined */
337 static int swap_read_io(void) /* {{{ */
338 {
339 FILE *fh;
340 char buffer[1024];
342 _Bool old_kernel = 0;
344 uint8_t have_data = 0;
345 derive_t swap_in = 0;
346 derive_t swap_out = 0;
348 fh = fopen("/proc/vmstat", "r");
349 if (fh == NULL) {
350 /* /proc/vmstat does not exist in kernels <2.6 */
351 fh = fopen("/proc/stat", "r");
352 if (fh == NULL) {
353 char errbuf[1024];
354 WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
355 return -1;
356 } else
357 old_kernel = 1;
358 }
360 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
361 char *fields[8];
362 int numfields;
364 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
366 if (!old_kernel) {
367 if (numfields != 2)
368 continue;
370 if (strcasecmp("pswpin", fields[0]) == 0) {
371 strtoderive(fields[1], &swap_in);
372 have_data |= 0x01;
373 } else if (strcasecmp("pswpout", fields[0]) == 0) {
374 strtoderive(fields[1], &swap_out);
375 have_data |= 0x02;
376 }
377 } else /* if (old_kernel) */
378 {
379 if (numfields != 3)
380 continue;
382 if (strcasecmp("page", fields[0]) == 0) {
383 strtoderive(fields[1], &swap_in);
384 strtoderive(fields[2], &swap_out);
385 }
386 }
387 } /* while (fgets) */
389 fclose(fh);
391 if (have_data != 0x03)
392 return ENOENT;
394 if (report_bytes) {
395 swap_in = swap_in * pagesize;
396 swap_out = swap_out * pagesize;
397 }
399 swap_submit_derive("in", swap_in);
400 swap_submit_derive("out", swap_out);
402 return 0;
403 } /* }}} int swap_read_io */
405 static int swap_read(void) /* {{{ */
406 {
407 if (report_by_device)
408 swap_read_separate();
409 else
410 swap_read_combined();
412 if (report_io)
413 swap_read_io();
415 return 0;
416 } /* }}} int swap_read */
417 /* #endif KERNEL_LINUX */
419 /*
420 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
421 * and kstat. The former reads physical space used on a device, the latter
422 * reports the view from the virtual memory system. It was decided that the
423 * kstat-based information should be moved to the "vmem" plugin, but nobody
424 * with enough Solaris experience was available at that time to do this. The
425 * code below is still there for your reference but it won't be activated in
426 * *this* plugin again. --octo
427 */
428 #elif 0 && HAVE_LIBKSTAT
429 /* kstat-based read function */
430 static int swap_read_kstat(void) /* {{{ */
431 {
432 gauge_t swap_alloc;
433 gauge_t swap_resv;
434 gauge_t swap_avail;
436 struct anoninfo ai;
438 if (swapctl(SC_AINFO, &ai) == -1) {
439 char errbuf[1024];
440 ERROR("swap plugin: swapctl failed: %s",
441 sstrerror(errno, errbuf, sizeof(errbuf)));
442 return -1;
443 }
445 /*
446 * Calculations from:
447 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
448 * Also see:
449 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
450 * /usr/include/vm/anon.h
451 *
452 * In short, swap -s shows: allocated + reserved = used, available
453 *
454 * However, Solaris does not allow to allocated/reserved more than the
455 * available swap (physical memory + disk swap), so the pedant may
456 * prefer: allocated + unallocated = reserved, available
457 *
458 * We map the above to: used + resv = n/a, free
459 *
460 * Does your brain hurt yet? - Christophe Kalt
461 *
462 * Oh, and in case you wonder,
463 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
464 * can suffer from a 32bit overflow.
465 */
466 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
467 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
468 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
470 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
471 return 0;
472 } /* }}} int swap_read_kstat */
473 /* #endif 0 && HAVE_LIBKSTAT */
475 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
476 /* swapctl-based read function */
477 static int swap_read(void) /* {{{ */
478 {
479 swaptbl_t *s;
480 char *s_paths;
481 int swap_num;
482 int status;
484 gauge_t avail = 0;
485 gauge_t total = 0;
487 swap_num = swapctl(SC_GETNSWP, NULL);
488 if (swap_num < 0) {
489 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
490 return -1;
491 } else if (swap_num == 0)
492 return 0;
494 /* Allocate and initialize the swaptbl_t structure */
495 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
496 if (s == NULL) {
497 ERROR("swap plugin: malloc failed.");
498 return -1;
499 }
501 /* Memory to store the path names. We only use these paths when the
502 * separate option has been configured, but it's easier to just
503 * allocate enough memory in any case. */
504 s_paths = calloc(swap_num, PATH_MAX);
505 if (s_paths == NULL) {
506 ERROR("swap plugin: calloc failed.");
507 sfree(s);
508 return -1;
509 }
510 for (int i = 0; i < swap_num; i++)
511 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
512 s->swt_n = swap_num;
514 status = swapctl(SC_LIST, s);
515 if (status < 0) {
516 char errbuf[1024];
517 ERROR("swap plugin: swapctl (SC_LIST) failed: %s",
518 sstrerror(errno, errbuf, sizeof(errbuf)));
519 sfree(s_paths);
520 sfree(s);
521 return -1;
522 } else if (swap_num < status) {
523 /* more elements returned than requested */
524 ERROR("swap plugin: I allocated memory for %i structure%s, "
525 "but swapctl(2) claims to have returned %i. "
526 "I'm confused and will give up.",
527 swap_num, (swap_num == 1) ? "" : "s", status);
528 sfree(s_paths);
529 sfree(s);
530 return -1;
531 } else if (swap_num > status)
532 /* less elements returned than requested */
533 swap_num = status;
535 for (int i = 0; i < swap_num; i++) {
536 char path[PATH_MAX];
537 gauge_t this_total;
538 gauge_t this_avail;
540 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
541 continue;
543 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
544 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
546 /* Shortcut for the "combined" setting (default) */
547 if (!report_by_device) {
548 avail += this_avail;
549 total += this_total;
550 continue;
551 }
553 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
554 escape_slashes(path, sizeof(path));
556 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
557 } /* for (swap_num) */
559 if (total < avail) {
560 ERROR(
561 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
562 total, avail);
563 sfree(s_paths);
564 sfree(s);
565 return -1;
566 }
568 /* If the "separate" option was specified (report_by_device == 1), all
569 * values have already been dispatched from within the loop. */
570 if (!report_by_device)
571 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
573 sfree(s_paths);
574 sfree(s);
575 return 0;
576 } /* }}} int swap_read */
577 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
579 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
580 static int swap_read(void) /* {{{ */
581 {
582 struct swapent *swap_entries;
583 int swap_num;
584 int status;
586 gauge_t used = 0;
587 gauge_t total = 0;
589 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
590 if (swap_num < 0) {
591 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
592 return -1;
593 } else if (swap_num == 0)
594 return 0;
596 swap_entries = calloc(swap_num, sizeof(*swap_entries));
597 if (swap_entries == NULL) {
598 ERROR("swap plugin: calloc failed.");
599 return -1;
600 }
602 status = swapctl(SWAP_STATS, swap_entries, swap_num);
603 if (status != swap_num) {
604 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
605 sfree(swap_entries);
606 return -1;
607 }
609 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
610 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
611 #else
612 #define C_SWAP_BLOCK_SIZE 512.0
613 #endif
615 /* TODO: Report per-device stats. The path name is available from
616 * swap_entries[i].se_path */
617 for (int i = 0; i < swap_num; i++) {
618 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
619 continue;
621 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
622 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
623 }
625 if (total < used) {
626 ERROR(
627 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
628 total, used);
629 sfree(swap_entries);
630 return -1;
631 }
633 swap_submit_usage(NULL, used, total - used, NULL, NAN);
635 sfree(swap_entries);
636 return 0;
637 } /* }}} int swap_read */
638 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
640 #elif defined(VM_SWAPUSAGE)
641 static int swap_read(void) /* {{{ */
642 {
643 int mib[3];
644 size_t mib_len;
645 struct xsw_usage sw_usage;
646 size_t sw_usage_len;
648 mib_len = 2;
649 mib[0] = CTL_VM;
650 mib[1] = VM_SWAPUSAGE;
652 sw_usage_len = sizeof(struct xsw_usage);
654 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
655 return -1;
657 /* The returned values are bytes. */
658 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
659 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
661 return 0;
662 } /* }}} int swap_read */
663 /* #endif VM_SWAPUSAGE */
665 #elif HAVE_LIBKVM_GETSWAPINFO
666 static int swap_read(void) /* {{{ */
667 {
668 struct kvm_swap data_s;
669 int status;
671 gauge_t used;
672 gauge_t total;
674 if (kvm_obj == NULL)
675 return -1;
677 /* only one structure => only get the grand total, no details */
678 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
679 if (status == -1)
680 return -1;
682 total = (gauge_t)data_s.ksw_total;
683 used = (gauge_t)data_s.ksw_used;
685 total *= (gauge_t)kvm_pagesize;
686 used *= (gauge_t)kvm_pagesize;
688 swap_submit_usage(NULL, used, total - used, NULL, NAN);
690 return 0;
691 } /* }}} int swap_read */
692 /* #endif HAVE_LIBKVM_GETSWAPINFO */
694 #elif HAVE_LIBSTATGRAB
695 static int swap_read(void) /* {{{ */
696 {
697 sg_swap_stats *swap;
699 swap = sg_get_swap_stats();
700 if (swap == NULL)
701 return -1;
703 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
705 return 0;
706 } /* }}} int swap_read */
707 /* #endif HAVE_LIBSTATGRAB */
709 #elif HAVE_PERFSTAT
710 static int swap_read(void) /* {{{ */
711 {
712 perfstat_memory_total_t pmemory = {0};
713 int status;
715 gauge_t total;
716 gauge_t free;
717 gauge_t reserved;
719 status =
720 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
721 if (status < 0) {
722 char errbuf[1024];
723 WARNING("swap plugin: perfstat_memory_total failed: %s",
724 sstrerror(errno, errbuf, sizeof(errbuf)));
725 return -1;
726 }
728 total = (gauge_t)(pmemory.pgsp_total * pagesize);
729 free = (gauge_t)(pmemory.pgsp_free * pagesize);
730 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
732 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
734 if (report_io) {
735 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
736 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
737 }
739 return 0;
740 } /* }}} int swap_read */
741 #endif /* HAVE_PERFSTAT */
743 void module_register(void) {
744 plugin_register_complex_config("swap", swap_config);
745 plugin_register_init("swap", swap_init);
746 plugin_register_read("swap", swap_read);
747 } /* void module_register */