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;
115 static int swap_config(oconfig_item_t *ci) /* {{{ */
116 {
117 for (int i = 0; i < ci->children_num; i++) {
118 oconfig_item_t *child = ci->children + i;
119 if (strcasecmp("ReportBytes", child->key) == 0)
120 #if KERNEL_LINUX
121 cf_util_get_boolean(child, &report_bytes);
122 #else
123 WARNING("swap plugin: The \"ReportBytes\" option "
124 "is only valid under Linux. "
125 "The option is going to be ignored.");
126 #endif
127 else if (strcasecmp("ReportByDevice", child->key) == 0)
128 #if SWAP_HAVE_REPORT_BY_DEVICE
129 cf_util_get_boolean(child, &report_by_device);
130 #else
131 WARNING("swap plugin: The \"ReportByDevice\" option "
132 "is not supported on this platform. "
133 "The option is going to be ignored.");
134 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
135 else if (strcasecmp("ValuesAbsolute", child->key) == 0)
136 cf_util_get_boolean(child, &values_absolute);
137 else if (strcasecmp("ValuesPercentage", child->key) == 0)
138 cf_util_get_boolean(child, &values_percentage);
139 else
140 WARNING("swap plugin: Unknown config option: \"%s\"", child->key);
141 }
143 return (0);
144 } /* }}} int swap_config */
146 static int swap_init(void) /* {{{ */
147 {
148 #if KERNEL_LINUX
149 pagesize = (derive_t)sysconf(_SC_PAGESIZE);
150 /* #endif KERNEL_LINUX */
152 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
153 /* getpagesize(3C) tells me this does not fail.. */
154 pagesize = (derive_t)getpagesize();
155 /* #endif HAVE_SWAPCTL */
157 #elif defined(VM_SWAPUSAGE)
158 /* No init stuff */
159 /* #endif defined(VM_SWAPUSAGE) */
161 #elif HAVE_LIBKVM_GETSWAPINFO
162 char errbuf[_POSIX2_LINE_MAX];
164 if (kvm_obj != NULL) {
165 kvm_close(kvm_obj);
166 kvm_obj = NULL;
167 }
169 kvm_pagesize = getpagesize();
171 kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
173 if (kvm_obj == NULL) {
174 ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
175 return (-1);
176 }
177 /* #endif HAVE_LIBKVM_GETSWAPINFO */
179 #elif HAVE_LIBSTATGRAB
180 /* No init stuff */
181 /* #endif HAVE_LIBSTATGRAB */
183 #elif HAVE_PERFSTAT
184 pagesize = getpagesize();
185 #endif /* HAVE_PERFSTAT */
187 return (0);
188 } /* }}} int swap_init */
190 static void swap_submit_usage(char const *plugin_instance, /* {{{ */
191 gauge_t used, gauge_t free,
192 char const *other_name, gauge_t other_value) {
193 value_list_t vl = VALUE_LIST_INIT;
195 vl.values = &(value_t){.gauge = NAN};
196 vl.values_len = 1;
197 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
198 if (plugin_instance != NULL)
199 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
200 sstrncpy(vl.type, "swap", sizeof(vl.type));
202 if (values_absolute)
203 plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free",
204 free, other_name, other_value, NULL);
205 if (values_percentage)
206 plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free",
207 free, other_name, other_value, NULL);
208 } /* }}} void swap_submit_usage */
210 #if KERNEL_LINUX || HAVE_PERFSTAT
211 __attribute__((nonnull(1))) static void
212 swap_submit_derive(char const *type_instance, /* {{{ */
213 derive_t value) {
214 value_list_t vl = VALUE_LIST_INIT;
216 vl.values = &(value_t){.derive = value};
217 vl.values_len = 1;
218 sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
219 sstrncpy(vl.type, "swap_io", sizeof(vl.type));
220 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
222 plugin_dispatch_values(&vl);
223 } /* }}} void swap_submit_derive */
224 #endif
226 #if KERNEL_LINUX
227 static int swap_read_separate(void) /* {{{ */
228 {
229 FILE *fh;
230 char buffer[1024];
232 fh = fopen("/proc/swaps", "r");
233 if (fh == NULL) {
234 char errbuf[1024];
235 WARNING("swap plugin: fopen (/proc/swaps) failed: %s",
236 sstrerror(errno, errbuf, sizeof(errbuf)));
237 return (-1);
238 }
240 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
241 char *fields[8];
242 int numfields;
243 char *endptr;
245 char path[PATH_MAX];
246 gauge_t total;
247 gauge_t used;
249 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
250 if (numfields != 5)
251 continue;
253 sstrncpy(path, fields[0], sizeof(path));
254 escape_slashes(path, sizeof(path));
256 errno = 0;
257 endptr = NULL;
258 total = strtod(fields[2], &endptr);
259 if ((endptr == fields[2]) || (errno != 0))
260 continue;
262 errno = 0;
263 endptr = NULL;
264 used = strtod(fields[3], &endptr);
265 if ((endptr == fields[3]) || (errno != 0))
266 continue;
268 if (total < used)
269 continue;
271 swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
272 }
274 fclose(fh);
276 return (0);
277 } /* }}} int swap_read_separate */
279 static int swap_read_combined(void) /* {{{ */
280 {
281 FILE *fh;
282 char buffer[1024];
284 gauge_t swap_used = NAN;
285 gauge_t swap_cached = NAN;
286 gauge_t swap_free = NAN;
287 gauge_t swap_total = NAN;
289 fh = fopen("/proc/meminfo", "r");
290 if (fh == NULL) {
291 char errbuf[1024];
292 WARNING("swap plugin: fopen (/proc/meminfo) failed: %s",
293 sstrerror(errno, errbuf, sizeof(errbuf)));
294 return (-1);
295 }
297 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
298 char *fields[8];
299 int numfields;
301 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
302 if (numfields < 2)
303 continue;
305 if (strcasecmp(fields[0], "SwapTotal:") == 0)
306 strtogauge(fields[1], &swap_total);
307 else if (strcasecmp(fields[0], "SwapFree:") == 0)
308 strtogauge(fields[1], &swap_free);
309 else if (strcasecmp(fields[0], "SwapCached:") == 0)
310 strtogauge(fields[1], &swap_cached);
311 }
313 fclose(fh);
315 if (isnan(swap_total) || isnan(swap_free))
316 return (ENOENT);
318 /* Some systems, OpenVZ for example, don't provide SwapCached. */
319 if (isnan(swap_cached))
320 swap_used = swap_total - swap_free;
321 else
322 swap_used = swap_total - (swap_free + swap_cached);
323 assert(!isnan(swap_used));
325 if (swap_used < 0.0)
326 return (EINVAL);
328 swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
329 isnan(swap_cached) ? NULL : "cached",
330 isnan(swap_cached) ? NAN : swap_cached * 1024.0);
331 return (0);
332 } /* }}} int swap_read_combined */
334 static int swap_read_io(void) /* {{{ */
335 {
336 FILE *fh;
337 char buffer[1024];
339 _Bool old_kernel = 0;
341 uint8_t have_data = 0;
342 derive_t swap_in = 0;
343 derive_t swap_out = 0;
345 fh = fopen("/proc/vmstat", "r");
346 if (fh == NULL) {
347 /* /proc/vmstat does not exist in kernels <2.6 */
348 fh = fopen("/proc/stat", "r");
349 if (fh == NULL) {
350 char errbuf[1024];
351 WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
352 return (-1);
353 } else
354 old_kernel = 1;
355 }
357 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
358 char *fields[8];
359 int numfields;
361 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
363 if (!old_kernel) {
364 if (numfields != 2)
365 continue;
367 if (strcasecmp("pswpin", fields[0]) == 0) {
368 strtoderive(fields[1], &swap_in);
369 have_data |= 0x01;
370 } else if (strcasecmp("pswpout", fields[0]) == 0) {
371 strtoderive(fields[1], &swap_out);
372 have_data |= 0x02;
373 }
374 } else /* if (old_kernel) */
375 {
376 if (numfields != 3)
377 continue;
379 if (strcasecmp("page", fields[0]) == 0) {
380 strtoderive(fields[1], &swap_in);
381 strtoderive(fields[2], &swap_out);
382 }
383 }
384 } /* while (fgets) */
386 fclose(fh);
388 if (have_data != 0x03)
389 return (ENOENT);
391 if (report_bytes) {
392 swap_in = swap_in * pagesize;
393 swap_out = swap_out * pagesize;
394 }
396 swap_submit_derive("in", swap_in);
397 swap_submit_derive("out", swap_out);
399 return (0);
400 } /* }}} int swap_read_io */
402 static int swap_read(void) /* {{{ */
403 {
404 if (report_by_device)
405 swap_read_separate();
406 else
407 swap_read_combined();
409 swap_read_io();
411 return (0);
412 } /* }}} int swap_read */
413 /* #endif KERNEL_LINUX */
415 /*
416 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
417 * and kstat. The former reads physical space used on a device, the latter
418 * reports the view from the virtual memory system. It was decided that the
419 * kstat-based information should be moved to the "vmem" plugin, but nobody
420 * with enough Solaris experience was available at that time to do this. The
421 * code below is still there for your reference but it won't be activated in
422 * *this* plugin again. --octo
423 */
424 #elif 0 && HAVE_LIBKSTAT
425 /* kstat-based read function */
426 static int swap_read_kstat(void) /* {{{ */
427 {
428 gauge_t swap_alloc;
429 gauge_t swap_resv;
430 gauge_t swap_avail;
432 struct anoninfo ai;
434 if (swapctl(SC_AINFO, &ai) == -1) {
435 char errbuf[1024];
436 ERROR("swap plugin: swapctl failed: %s",
437 sstrerror(errno, errbuf, sizeof(errbuf)));
438 return (-1);
439 }
441 /*
442 * Calculations from:
443 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
444 * Also see:
445 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
446 * /usr/include/vm/anon.h
447 *
448 * In short, swap -s shows: allocated + reserved = used, available
449 *
450 * However, Solaris does not allow to allocated/reserved more than the
451 * available swap (physical memory + disk swap), so the pedant may
452 * prefer: allocated + unallocated = reserved, available
453 *
454 * We map the above to: used + resv = n/a, free
455 *
456 * Does your brain hurt yet? - Christophe Kalt
457 *
458 * Oh, and in case you wonder,
459 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
460 * can suffer from a 32bit overflow.
461 */
462 swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
463 swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
464 swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
466 swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
467 return (0);
468 } /* }}} int swap_read_kstat */
469 /* #endif 0 && HAVE_LIBKSTAT */
471 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
472 /* swapctl-based read function */
473 static int swap_read(void) /* {{{ */
474 {
475 swaptbl_t *s;
476 char *s_paths;
477 int swap_num;
478 int status;
480 gauge_t avail = 0;
481 gauge_t total = 0;
483 swap_num = swapctl(SC_GETNSWP, NULL);
484 if (swap_num < 0) {
485 ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
486 return (-1);
487 } else if (swap_num == 0)
488 return (0);
490 /* Allocate and initialize the swaptbl_t structure */
491 s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
492 if (s == NULL) {
493 ERROR("swap plugin: malloc failed.");
494 return (-1);
495 }
497 /* Memory to store the path names. We only use these paths when the
498 * separate option has been configured, but it's easier to just
499 * allocate enough memory in any case. */
500 s_paths = calloc(swap_num, PATH_MAX);
501 if (s_paths == NULL) {
502 ERROR("swap plugin: calloc failed.");
503 sfree(s);
504 return (-1);
505 }
506 for (int i = 0; i < swap_num; i++)
507 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
508 s->swt_n = swap_num;
510 status = swapctl(SC_LIST, s);
511 if (status < 0) {
512 char errbuf[1024];
513 ERROR("swap plugin: swapctl (SC_LIST) failed: %s",
514 sstrerror(errno, errbuf, sizeof(errbuf)));
515 sfree(s_paths);
516 sfree(s);
517 return (-1);
518 } else if (swap_num < status) {
519 /* more elements returned than requested */
520 ERROR("swap plugin: I allocated memory for %i structure%s, "
521 "but swapctl(2) claims to have returned %i. "
522 "I'm confused and will give up.",
523 swap_num, (swap_num == 1) ? "" : "s", status);
524 sfree(s_paths);
525 sfree(s);
526 return (-1);
527 } else if (swap_num > status)
528 /* less elements returned than requested */
529 swap_num = status;
531 for (int i = 0; i < swap_num; i++) {
532 char path[PATH_MAX];
533 gauge_t this_total;
534 gauge_t this_avail;
536 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
537 continue;
539 this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
540 this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
542 /* Shortcut for the "combined" setting (default) */
543 if (!report_by_device) {
544 avail += this_avail;
545 total += this_total;
546 continue;
547 }
549 sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
550 escape_slashes(path, sizeof(path));
552 swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
553 } /* for (swap_num) */
555 if (total < avail) {
556 ERROR(
557 "swap plugin: Total swap space (%g) is less than free swap space (%g).",
558 total, avail);
559 sfree(s_paths);
560 sfree(s);
561 return (-1);
562 }
564 /* If the "separate" option was specified (report_by_device == 1), all
565 * values have already been dispatched from within the loop. */
566 if (!report_by_device)
567 swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
569 sfree(s_paths);
570 sfree(s);
571 return (0);
572 } /* }}} int swap_read */
573 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
575 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
576 static int swap_read(void) /* {{{ */
577 {
578 struct swapent *swap_entries;
579 int swap_num;
580 int status;
582 gauge_t used = 0;
583 gauge_t total = 0;
585 swap_num = swapctl(SWAP_NSWAP, NULL, 0);
586 if (swap_num < 0) {
587 ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
588 return (-1);
589 } else if (swap_num == 0)
590 return (0);
592 swap_entries = calloc(swap_num, sizeof(*swap_entries));
593 if (swap_entries == NULL) {
594 ERROR("swap plugin: calloc failed.");
595 return (-1);
596 }
598 status = swapctl(SWAP_STATS, swap_entries, swap_num);
599 if (status != swap_num) {
600 ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
601 sfree(swap_entries);
602 return (-1);
603 }
605 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
606 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
607 #else
608 #define C_SWAP_BLOCK_SIZE 512.0
609 #endif
611 /* TODO: Report per-device stats. The path name is available from
612 * swap_entries[i].se_path */
613 for (int i = 0; i < swap_num; i++) {
614 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
615 continue;
617 used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
618 total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
619 }
621 if (total < used) {
622 ERROR(
623 "swap plugin: Total swap space (%g) is less than used swap space (%g).",
624 total, used);
625 sfree(swap_entries);
626 return (-1);
627 }
629 swap_submit_usage(NULL, used, total - used, NULL, NAN);
631 sfree(swap_entries);
632 return (0);
633 } /* }}} int swap_read */
634 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
636 #elif defined(VM_SWAPUSAGE)
637 static int swap_read(void) /* {{{ */
638 {
639 int mib[3];
640 size_t mib_len;
641 struct xsw_usage sw_usage;
642 size_t sw_usage_len;
644 mib_len = 2;
645 mib[0] = CTL_VM;
646 mib[1] = VM_SWAPUSAGE;
648 sw_usage_len = sizeof(struct xsw_usage);
650 if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
651 return (-1);
653 /* The returned values are bytes. */
654 swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
655 (gauge_t)sw_usage.xsu_avail, NULL, NAN);
657 return (0);
658 } /* }}} int swap_read */
659 /* #endif VM_SWAPUSAGE */
661 #elif HAVE_LIBKVM_GETSWAPINFO
662 static int swap_read(void) /* {{{ */
663 {
664 struct kvm_swap data_s;
665 int status;
667 gauge_t used;
668 gauge_t total;
670 if (kvm_obj == NULL)
671 return (-1);
673 /* only one structure => only get the grand total, no details */
674 status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
675 if (status == -1)
676 return (-1);
678 total = (gauge_t)data_s.ksw_total;
679 used = (gauge_t)data_s.ksw_used;
681 total *= (gauge_t)kvm_pagesize;
682 used *= (gauge_t)kvm_pagesize;
684 swap_submit_usage(NULL, used, total - used, NULL, NAN);
686 return (0);
687 } /* }}} int swap_read */
688 /* #endif HAVE_LIBKVM_GETSWAPINFO */
690 #elif HAVE_LIBSTATGRAB
691 static int swap_read(void) /* {{{ */
692 {
693 sg_swap_stats *swap;
695 swap = sg_get_swap_stats();
696 if (swap == NULL)
697 return (-1);
699 swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
701 return (0);
702 } /* }}} int swap_read */
703 /* #endif HAVE_LIBSTATGRAB */
705 #elif HAVE_PERFSTAT
706 static int swap_read(void) /* {{{ */
707 {
708 perfstat_memory_total_t pmemory = {0};
709 int status;
711 gauge_t total;
712 gauge_t free;
713 gauge_t reserved;
715 status =
716 perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
717 if (status < 0) {
718 char errbuf[1024];
719 WARNING("swap plugin: perfstat_memory_total failed: %s",
720 sstrerror(errno, errbuf, sizeof(errbuf)));
721 return (-1);
722 }
724 total = (gauge_t)(pmemory.pgsp_total * pagesize);
725 free = (gauge_t)(pmemory.pgsp_free * pagesize);
726 reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
728 swap_submit_usage(NULL, total - free, free, "reserved", reserved);
729 swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
730 swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
732 return (0);
733 } /* }}} int swap_read */
734 #endif /* HAVE_PERFSTAT */
736 void module_register(void) {
737 plugin_register_complex_config("swap", swap_config);
738 plugin_register_init("swap", swap_init);
739 plugin_register_read("swap", swap_read);
740 } /* void module_register */