Code

Tree wide: Reformat with clang-format.
[collectd.git] / src / swap.c
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) /* {{{ */
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) /* {{{ */
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_t v[1];
194   value_list_t vl = VALUE_LIST_INIT;
196   vl.values = v;
197   vl.values_len = STATIC_ARRAY_SIZE(v);
198   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
199   sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
200   if (plugin_instance != NULL)
201     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
202   sstrncpy(vl.type, "swap", sizeof(vl.type));
204   if (values_absolute)
205     plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free",
206                                free, other_name, other_value, NULL);
207   if (values_percentage)
208     plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free",
209                                free, other_name, other_value, NULL);
210 } /* }}} void swap_submit_usage */
212 #if KERNEL_LINUX || HAVE_PERFSTAT
213 __attribute__((nonnull(1))) static void
214 swap_submit_derive(char const *type_instance, /* {{{ */
215                    derive_t value) {
216   value_list_t vl = VALUE_LIST_INIT;
217   value_t v[1];
219   v[0].derive = value;
221   vl.values = v;
222   vl.values_len = STATIC_ARRAY_SIZE(v);
223   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
224   sstrncpy(vl.plugin, "swap", sizeof(vl.plugin));
225   sstrncpy(vl.type, "swap_io", sizeof(vl.type));
226   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
228   plugin_dispatch_values(&vl);
229 } /* }}} void swap_submit_derive */
230 #endif
232 #if KERNEL_LINUX
233 static int swap_read_separate(void) /* {{{ */
235   FILE *fh;
236   char buffer[1024];
238   fh = fopen("/proc/swaps", "r");
239   if (fh == NULL) {
240     char errbuf[1024];
241     WARNING("swap plugin: fopen (/proc/swaps) failed: %s",
242             sstrerror(errno, errbuf, sizeof(errbuf)));
243     return (-1);
244   }
246   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
247     char *fields[8];
248     int numfields;
249     char *endptr;
251     char path[PATH_MAX];
252     gauge_t total;
253     gauge_t used;
255     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
256     if (numfields != 5)
257       continue;
259     sstrncpy(path, fields[0], sizeof(path));
260     escape_slashes(path, sizeof(path));
262     errno = 0;
263     endptr = NULL;
264     total = strtod(fields[2], &endptr);
265     if ((endptr == fields[2]) || (errno != 0))
266       continue;
268     errno = 0;
269     endptr = NULL;
270     used = strtod(fields[3], &endptr);
271     if ((endptr == fields[3]) || (errno != 0))
272       continue;
274     if (total < used)
275       continue;
277     swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
278   }
280   fclose(fh);
282   return (0);
283 } /* }}} int swap_read_separate */
285 static int swap_read_combined(void) /* {{{ */
287   FILE *fh;
288   char buffer[1024];
290   gauge_t swap_used = NAN;
291   gauge_t swap_cached = NAN;
292   gauge_t swap_free = NAN;
293   gauge_t swap_total = NAN;
295   fh = fopen("/proc/meminfo", "r");
296   if (fh == NULL) {
297     char errbuf[1024];
298     WARNING("swap plugin: fopen (/proc/meminfo) failed: %s",
299             sstrerror(errno, errbuf, sizeof(errbuf)));
300     return (-1);
301   }
303   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
304     char *fields[8];
305     int numfields;
307     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
308     if (numfields < 2)
309       continue;
311     if (strcasecmp(fields[0], "SwapTotal:") == 0)
312       strtogauge(fields[1], &swap_total);
313     else if (strcasecmp(fields[0], "SwapFree:") == 0)
314       strtogauge(fields[1], &swap_free);
315     else if (strcasecmp(fields[0], "SwapCached:") == 0)
316       strtogauge(fields[1], &swap_cached);
317   }
319   fclose(fh);
321   if (isnan(swap_total) || isnan(swap_free))
322     return (ENOENT);
324   /* Some systems, OpenVZ for example, don't provide SwapCached. */
325   if (isnan(swap_cached))
326     swap_used = swap_total - swap_free;
327   else
328     swap_used = swap_total - (swap_free + swap_cached);
329   assert(!isnan(swap_used));
331   if (swap_used < 0.0)
332     return (EINVAL);
334   swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
335                     isnan(swap_cached) ? NULL : "cached",
336                     isnan(swap_cached) ? NAN : swap_cached * 1024.0);
337   return (0);
338 } /* }}} int swap_read_combined */
340 static int swap_read_io(void) /* {{{ */
342   FILE *fh;
343   char buffer[1024];
345   _Bool old_kernel = 0;
347   uint8_t have_data = 0;
348   derive_t swap_in = 0;
349   derive_t swap_out = 0;
351   fh = fopen("/proc/vmstat", "r");
352   if (fh == NULL) {
353     /* /proc/vmstat does not exist in kernels <2.6 */
354     fh = fopen("/proc/stat", "r");
355     if (fh == NULL) {
356       char errbuf[1024];
357       WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
358       return (-1);
359     } else
360       old_kernel = 1;
361   }
363   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
364     char *fields[8];
365     int numfields;
367     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
369     if (!old_kernel) {
370       if (numfields != 2)
371         continue;
373       if (strcasecmp("pswpin", fields[0]) == 0) {
374         strtoderive(fields[1], &swap_in);
375         have_data |= 0x01;
376       } else if (strcasecmp("pswpout", fields[0]) == 0) {
377         strtoderive(fields[1], &swap_out);
378         have_data |= 0x02;
379       }
380     } else /* if (old_kernel) */
381     {
382       if (numfields != 3)
383         continue;
385       if (strcasecmp("page", fields[0]) == 0) {
386         strtoderive(fields[1], &swap_in);
387         strtoderive(fields[2], &swap_out);
388       }
389     }
390   } /* while (fgets) */
392   fclose(fh);
394   if (have_data != 0x03)
395     return (ENOENT);
397   if (report_bytes) {
398     swap_in = swap_in * pagesize;
399     swap_out = swap_out * pagesize;
400   }
402   swap_submit_derive("in", swap_in);
403   swap_submit_derive("out", swap_out);
405   return (0);
406 } /* }}} int swap_read_io */
408 static int swap_read(void) /* {{{ */
410   if (report_by_device)
411     swap_read_separate();
412   else
413     swap_read_combined();
415   swap_read_io();
417   return (0);
418 } /* }}} int swap_read */
419   /* #endif KERNEL_LINUX */
421 /*
422  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
423  * and kstat. The former reads physical space used on a device, the latter
424  * reports the view from the virtual memory system. It was decided that the
425  * kstat-based information should be moved to the "vmem" plugin, but nobody
426  * with enough Solaris experience was available at that time to do this. The
427  * code below is still there for your reference but it won't be activated in
428  * *this* plugin again. --octo
429  */
430 #elif 0 && HAVE_LIBKSTAT
431 /* kstat-based read function */
432 static int swap_read_kstat(void) /* {{{ */
434   gauge_t swap_alloc;
435   gauge_t swap_resv;
436   gauge_t swap_avail;
438   struct anoninfo ai;
440   if (swapctl(SC_AINFO, &ai) == -1) {
441     char errbuf[1024];
442     ERROR("swap plugin: swapctl failed: %s",
443           sstrerror(errno, errbuf, sizeof(errbuf)));
444     return (-1);
445   }
447   /*
448    * Calculations from:
449    * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
450    * Also see:
451    * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
452    * /usr/include/vm/anon.h
453    *
454    * In short, swap -s shows: allocated + reserved = used, available
455    *
456    * However, Solaris does not allow to allocated/reserved more than the
457    * available swap (physical memory + disk swap), so the pedant may
458    * prefer: allocated + unallocated = reserved, available
459    *
460    * We map the above to: used + resv = n/a, free
461    *
462    * Does your brain hurt yet?  - Christophe Kalt
463    *
464    * Oh, and in case you wonder,
465    * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
466    * can suffer from a 32bit overflow.
467    */
468   swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
469   swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
470   swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
472   swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
473   return (0);
474 } /* }}} int swap_read_kstat */
475   /* #endif 0 && HAVE_LIBKSTAT */
477 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
478 /* swapctl-based read function */
479 static int swap_read(void) /* {{{ */
481   swaptbl_t *s;
482   char *s_paths;
483   int swap_num;
484   int status;
486   gauge_t avail = 0;
487   gauge_t total = 0;
489   swap_num = swapctl(SC_GETNSWP, NULL);
490   if (swap_num < 0) {
491     ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
492     return (-1);
493   } else if (swap_num == 0)
494     return (0);
496   /* Allocate and initialize the swaptbl_t structure */
497   s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
498   if (s == NULL) {
499     ERROR("swap plugin: malloc failed.");
500     return (-1);
501   }
503   /* Memory to store the path names. We only use these paths when the
504    * separate option has been configured, but it's easier to just
505    * allocate enough memory in any case. */
506   s_paths = calloc(swap_num, PATH_MAX);
507   if (s_paths == NULL) {
508     ERROR("swap plugin: calloc failed.");
509     sfree(s);
510     return (-1);
511   }
512   for (int i = 0; i < swap_num; i++)
513     s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
514   s->swt_n = swap_num;
516   status = swapctl(SC_LIST, s);
517   if (status < 0) {
518     char errbuf[1024];
519     ERROR("swap plugin: swapctl (SC_LIST) failed: %s",
520           sstrerror(errno, errbuf, sizeof(errbuf)));
521     sfree(s_paths);
522     sfree(s);
523     return (-1);
524   } else if (swap_num < status) {
525     /* more elements returned than requested */
526     ERROR("swap plugin: I allocated memory for %i structure%s, "
527           "but swapctl(2) claims to have returned %i. "
528           "I'm confused and will give up.",
529           swap_num, (swap_num == 1) ? "" : "s", status);
530     sfree(s_paths);
531     sfree(s);
532     return (-1);
533   } else if (swap_num > status)
534     /* less elements returned than requested */
535     swap_num = status;
537   for (int i = 0; i < swap_num; i++) {
538     char path[PATH_MAX];
539     gauge_t this_total;
540     gauge_t this_avail;
542     if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
543       continue;
545     this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
546     this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
548     /* Shortcut for the "combined" setting (default) */
549     if (!report_by_device) {
550       avail += this_avail;
551       total += this_total;
552       continue;
553     }
555     sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
556     escape_slashes(path, sizeof(path));
558     swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
559   } /* for (swap_num) */
561   if (total < avail) {
562     ERROR(
563         "swap plugin: Total swap space (%g) is less than free swap space (%g).",
564         total, avail);
565     sfree(s_paths);
566     sfree(s);
567     return (-1);
568   }
570   /* If the "separate" option was specified (report_by_device == 1), all
571    * values have already been dispatched from within the loop. */
572   if (!report_by_device)
573     swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
575   sfree(s_paths);
576   sfree(s);
577   return (0);
578 } /* }}} int swap_read */
579   /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
581 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
582 static int swap_read(void) /* {{{ */
584   struct swapent *swap_entries;
585   int swap_num;
586   int status;
588   gauge_t used = 0;
589   gauge_t total = 0;
591   swap_num = swapctl(SWAP_NSWAP, NULL, 0);
592   if (swap_num < 0) {
593     ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
594     return (-1);
595   } else if (swap_num == 0)
596     return (0);
598   swap_entries = calloc(swap_num, sizeof(*swap_entries));
599   if (swap_entries == NULL) {
600     ERROR("swap plugin: calloc failed.");
601     return (-1);
602   }
604   status = swapctl(SWAP_STATS, swap_entries, swap_num);
605   if (status != swap_num) {
606     ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
607     sfree(swap_entries);
608     return (-1);
609   }
611 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
612 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
613 #else
614 #define C_SWAP_BLOCK_SIZE 512.0
615 #endif
617   /* TODO: Report per-device stats. The path name is available from
618    * swap_entries[i].se_path */
619   for (int i = 0; i < swap_num; i++) {
620     if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
621       continue;
623     used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
624     total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
625   }
627   if (total < used) {
628     ERROR(
629         "swap plugin: Total swap space (%g) is less than used swap space (%g).",
630         total, used);
631     sfree(swap_entries);
632     return (-1);
633   }
635   swap_submit_usage(NULL, used, total - used, NULL, NAN);
637   sfree(swap_entries);
638   return (0);
639 } /* }}} int swap_read */
640   /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
642 #elif defined(VM_SWAPUSAGE)
643 static int swap_read(void) /* {{{ */
645   int mib[3];
646   size_t mib_len;
647   struct xsw_usage sw_usage;
648   size_t sw_usage_len;
650   mib_len = 2;
651   mib[0] = CTL_VM;
652   mib[1] = VM_SWAPUSAGE;
654   sw_usage_len = sizeof(struct xsw_usage);
656   if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
657     return (-1);
659   /* The returned values are bytes. */
660   swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
661                     (gauge_t)sw_usage.xsu_avail, NULL, NAN);
663   return (0);
664 } /* }}} int swap_read */
665   /* #endif VM_SWAPUSAGE */
667 #elif HAVE_LIBKVM_GETSWAPINFO
668 static int swap_read(void) /* {{{ */
670   struct kvm_swap data_s;
671   int status;
673   gauge_t used;
674   gauge_t total;
676   if (kvm_obj == NULL)
677     return (-1);
679   /* only one structure => only get the grand total, no details */
680   status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
681   if (status == -1)
682     return (-1);
684   total = (gauge_t)data_s.ksw_total;
685   used = (gauge_t)data_s.ksw_used;
687   total *= (gauge_t)kvm_pagesize;
688   used *= (gauge_t)kvm_pagesize;
690   swap_submit_usage(NULL, used, total - used, NULL, NAN);
692   return (0);
693 } /* }}} int swap_read */
694   /* #endif HAVE_LIBKVM_GETSWAPINFO */
696 #elif HAVE_LIBSTATGRAB
697 static int swap_read(void) /* {{{ */
699   sg_swap_stats *swap;
701   swap = sg_get_swap_stats();
702   if (swap == NULL)
703     return (-1);
705   swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
707   return (0);
708 } /* }}} int swap_read */
709   /* #endif  HAVE_LIBSTATGRAB */
711 #elif HAVE_PERFSTAT
712 static int swap_read(void) /* {{{ */
714   perfstat_memory_total_t pmemory = {0};
715   int status;
717   gauge_t total;
718   gauge_t free;
719   gauge_t reserved;
721   status =
722       perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
723   if (status < 0) {
724     char errbuf[1024];
725     WARNING("swap plugin: perfstat_memory_total failed: %s",
726             sstrerror(errno, errbuf, sizeof(errbuf)));
727     return (-1);
728   }
730   total = (gauge_t)(pmemory.pgsp_total * pagesize);
731   free = (gauge_t)(pmemory.pgsp_free * pagesize);
732   reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
734   swap_submit_usage(NULL, total - free, free, "reserved", reserved);
735   swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
736   swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
738   return (0);
739 } /* }}} int swap_read */
740 #endif /* HAVE_PERFSTAT */
742 void module_register(void) {
743   plugin_register_complex_config("swap", swap_config);
744   plugin_register_init("swap", swap_init);
745   plugin_register_read("swap", swap_read);
746 } /* void module_register */
748 /* vim: set fdm=marker : */