Merge branch 'collectd-5.8'
[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  **/
26
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
37
38 #include "collectd.h"
39
40 #include "common.h"
41 #include "plugin.h"
42
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
61
62 #if HAVE_STATGRAB_H
63 #include <statgrab.h>
64 #endif
65
66 #if HAVE_PERFSTAT
67 #include <libperfstat.h>
68 #include <sys/protosw.h>
69 #endif
70
71 #undef MAX
72 #define MAX(x, y) ((x) > (y) ? (x) : (y))
73
74 #if KERNEL_LINUX
75 #define SWAP_HAVE_REPORT_BY_DEVICE 1
76 static derive_t pagesize;
77 static bool report_bytes;
78 static bool report_by_device;
79 /* #endif KERNEL_LINUX */
80
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;
85 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
86
87 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
88 /* No global variables */
89 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
90
91 #elif defined(VM_SWAPUSAGE)
92 /* No global variables */
93 /* #endif defined(VM_SWAPUSAGE) */
94
95 #elif HAVE_LIBKVM_GETSWAPINFO
96 static kvm_t *kvm_obj;
97 int kvm_pagesize;
98 /* #endif HAVE_LIBKVM_GETSWAPINFO */
99
100 #elif HAVE_LIBSTATGRAB
101 /* No global variables */
102 /* #endif HAVE_LIBSTATGRAB */
103
104 #elif HAVE_PERFSTAT
105 static int pagesize;
106 /*# endif HAVE_PERFSTAT */
107
108 #else
109 #error "No applicable input method."
110 #endif /* HAVE_LIBSTATGRAB */
111
112 static bool values_absolute = true;
113 static bool values_percentage;
114 static bool report_io = true;
115
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   }
145
146   return 0;
147 } /* }}} int swap_config */
148
149 static int swap_init(void) /* {{{ */
150 {
151 #if KERNEL_LINUX
152   pagesize = (derive_t)sysconf(_SC_PAGESIZE);
153 /* #endif KERNEL_LINUX */
154
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 */
159
160 #elif defined(VM_SWAPUSAGE)
161 /* No init stuff */
162 /* #endif defined(VM_SWAPUSAGE) */
163
164 #elif HAVE_LIBKVM_GETSWAPINFO
165   char errbuf[_POSIX2_LINE_MAX];
166
167   if (kvm_obj != NULL) {
168     kvm_close(kvm_obj);
169     kvm_obj = NULL;
170   }
171
172   kvm_pagesize = getpagesize();
173
174   kvm_obj = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, errbuf);
175
176   if (kvm_obj == NULL) {
177     ERROR("swap plugin: kvm_openfiles failed, %s", errbuf);
178     return -1;
179   }
180 /* #endif HAVE_LIBKVM_GETSWAPINFO */
181
182 #elif HAVE_LIBSTATGRAB
183 /* No init stuff */
184 /* #endif HAVE_LIBSTATGRAB */
185
186 #elif HAVE_PERFSTAT
187   pagesize = getpagesize();
188 #endif /* HAVE_PERFSTAT */
189
190   return 0;
191 } /* }}} int swap_init */
192
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;
197
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));
204
205   if (values_absolute)
206     plugin_dispatch_multivalue(&vl, false, DS_TYPE_GAUGE, "used", used, "free",
207                                free, other_name, other_value, NULL);
208   if (values_percentage)
209     plugin_dispatch_multivalue(&vl, true, DS_TYPE_GAUGE, "used", used, "free",
210                                free, other_name, other_value, NULL);
211 } /* }}} void swap_submit_usage */
212
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;
218
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));
224
225   plugin_dispatch_values(&vl);
226 } /* }}} void swap_submit_derive */
227 #endif
228
229 #if KERNEL_LINUX
230 static int swap_read_separate(void) /* {{{ */
231 {
232   FILE *fh;
233   char buffer[1024];
234
235   fh = fopen("/proc/swaps", "r");
236   if (fh == NULL) {
237     WARNING("swap plugin: fopen (/proc/swaps) failed: %s", STRERRNO);
238     return -1;
239   }
240
241   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
242     char *fields[8];
243     int numfields;
244     char *endptr;
245
246     char path[PATH_MAX];
247     gauge_t total;
248     gauge_t used;
249
250     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
251     if (numfields != 5)
252       continue;
253
254     sstrncpy(path, fields[0], sizeof(path));
255     escape_slashes(path, sizeof(path));
256
257     errno = 0;
258     endptr = NULL;
259     total = strtod(fields[2], &endptr);
260     if ((endptr == fields[2]) || (errno != 0))
261       continue;
262
263     errno = 0;
264     endptr = NULL;
265     used = strtod(fields[3], &endptr);
266     if ((endptr == fields[3]) || (errno != 0))
267       continue;
268
269     if (total < used)
270       continue;
271
272     swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN);
273   }
274
275   fclose(fh);
276
277   return 0;
278 } /* }}} int swap_read_separate */
279
280 static int swap_read_combined(void) /* {{{ */
281 {
282   FILE *fh;
283   char buffer[1024];
284
285   gauge_t swap_used = NAN;
286   gauge_t swap_cached = NAN;
287   gauge_t swap_free = NAN;
288   gauge_t swap_total = NAN;
289
290   fh = fopen("/proc/meminfo", "r");
291   if (fh == NULL) {
292     WARNING("swap plugin: fopen (/proc/meminfo) failed: %s", STRERRNO);
293     return -1;
294   }
295
296   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
297     char *fields[8];
298     int numfields;
299
300     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
301     if (numfields < 2)
302       continue;
303
304     if (strcasecmp(fields[0], "SwapTotal:") == 0)
305       strtogauge(fields[1], &swap_total);
306     else if (strcasecmp(fields[0], "SwapFree:") == 0)
307       strtogauge(fields[1], &swap_free);
308     else if (strcasecmp(fields[0], "SwapCached:") == 0)
309       strtogauge(fields[1], &swap_cached);
310   }
311
312   fclose(fh);
313
314   if (isnan(swap_total) || isnan(swap_free))
315     return ENOENT;
316
317   /* Some systems, OpenVZ for example, don't provide SwapCached. */
318   if (isnan(swap_cached))
319     swap_used = swap_total - swap_free;
320   else
321     swap_used = swap_total - (swap_free + swap_cached);
322   assert(!isnan(swap_used));
323
324   if (swap_used < 0.0)
325     return EINVAL;
326
327   swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0,
328                     isnan(swap_cached) ? NULL : "cached",
329                     isnan(swap_cached) ? NAN : swap_cached * 1024.0);
330   return 0;
331 } /* }}} int swap_read_combined */
332
333 static int swap_read_io(void) /* {{{ */
334 {
335   FILE *fh;
336   char buffer[1024];
337
338   bool old_kernel = false;
339
340   uint8_t have_data = 0;
341   derive_t swap_in = 0;
342   derive_t swap_out = 0;
343
344   fh = fopen("/proc/vmstat", "r");
345   if (fh == NULL) {
346     /* /proc/vmstat does not exist in kernels <2.6 */
347     fh = fopen("/proc/stat", "r");
348     if (fh == NULL) {
349       WARNING("swap: fopen: %s", STRERRNO);
350       return -1;
351     } else
352       old_kernel = true;
353   }
354
355   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
356     char *fields[8];
357     int numfields;
358
359     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
360
361     if (!old_kernel) {
362       if (numfields != 2)
363         continue;
364
365       if (strcasecmp("pswpin", fields[0]) == 0) {
366         strtoderive(fields[1], &swap_in);
367         have_data |= 0x01;
368       } else if (strcasecmp("pswpout", fields[0]) == 0) {
369         strtoderive(fields[1], &swap_out);
370         have_data |= 0x02;
371       }
372     } else /* if (old_kernel) */
373     {
374       if (numfields != 3)
375         continue;
376
377       if (strcasecmp("page", fields[0]) == 0) {
378         strtoderive(fields[1], &swap_in);
379         strtoderive(fields[2], &swap_out);
380       }
381     }
382   } /* while (fgets) */
383
384   fclose(fh);
385
386   if (have_data != 0x03)
387     return ENOENT;
388
389   if (report_bytes) {
390     swap_in = swap_in * pagesize;
391     swap_out = swap_out * pagesize;
392   }
393
394   swap_submit_derive("in", swap_in);
395   swap_submit_derive("out", swap_out);
396
397   return 0;
398 } /* }}} int swap_read_io */
399
400 static int swap_read(void) /* {{{ */
401 {
402   if (report_by_device)
403     swap_read_separate();
404   else
405     swap_read_combined();
406
407   if (report_io)
408     swap_read_io();
409
410   return 0;
411 } /* }}} int swap_read */
412 /* #endif KERNEL_LINUX */
413
414 /*
415  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
416  * and kstat. The former reads physical space used on a device, the latter
417  * reports the view from the virtual memory system. It was decided that the
418  * kstat-based information should be moved to the "vmem" plugin, but nobody
419  * with enough Solaris experience was available at that time to do this. The
420  * code below is still there for your reference but it won't be activated in
421  * *this* plugin again. --octo
422  */
423 #elif 0 && HAVE_LIBKSTAT
424 /* kstat-based read function */
425 static int swap_read_kstat(void) /* {{{ */
426 {
427   gauge_t swap_alloc;
428   gauge_t swap_resv;
429   gauge_t swap_avail;
430
431   struct anoninfo ai;
432
433   if (swapctl(SC_AINFO, &ai) == -1) {
434     ERROR("swap plugin: swapctl failed: %s", STRERRNO);
435     return -1;
436   }
437
438   /*
439    * Calculations from:
440    * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
441    * Also see:
442    * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
443    * /usr/include/vm/anon.h
444    *
445    * In short, swap -s shows: allocated + reserved = used, available
446    *
447    * However, Solaris does not allow to allocated/reserved more than the
448    * available swap (physical memory + disk swap), so the pedant may
449    * prefer: allocated + unallocated = reserved, available
450    *
451    * We map the above to: used + resv = n/a, free
452    *
453    * Does your brain hurt yet?  - Christophe Kalt
454    *
455    * Oh, and in case you wonder,
456    * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
457    * can suffer from a 32bit overflow.
458    */
459   swap_alloc = (gauge_t)((ai.ani_max - ai.ani_free) * pagesize);
460   swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
461   swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize);
462
463   swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv);
464   return 0;
465 } /* }}} int swap_read_kstat */
466   /* #endif 0 && HAVE_LIBKSTAT */
467
468 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
469 /* swapctl-based read function */
470 static int swap_read(void) /* {{{ */
471 {
472   swaptbl_t *s;
473   char *s_paths;
474   int swap_num;
475   int status;
476
477   gauge_t avail = 0;
478   gauge_t total = 0;
479
480   swap_num = swapctl(SC_GETNSWP, NULL);
481   if (swap_num < 0) {
482     ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num);
483     return -1;
484   } else if (swap_num == 0)
485     return 0;
486
487   /* Allocate and initialize the swaptbl_t structure */
488   s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable));
489   if (s == NULL) {
490     ERROR("swap plugin: malloc failed.");
491     return -1;
492   }
493
494   /* Memory to store the path names. We only use these paths when the
495    * separate option has been configured, but it's easier to just
496    * allocate enough memory in any case. */
497   s_paths = calloc(swap_num, PATH_MAX);
498   if (s_paths == NULL) {
499     ERROR("swap plugin: calloc failed.");
500     sfree(s);
501     return -1;
502   }
503   for (int i = 0; i < swap_num; i++)
504     s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
505   s->swt_n = swap_num;
506
507   status = swapctl(SC_LIST, s);
508   if (status < 0) {
509     ERROR("swap plugin: swapctl (SC_LIST) failed: %s", STRERRNO);
510     sfree(s_paths);
511     sfree(s);
512     return -1;
513   } else if (swap_num < status) {
514     /* more elements returned than requested */
515     ERROR("swap plugin: I allocated memory for %i structure%s, "
516           "but swapctl(2) claims to have returned %i. "
517           "I'm confused and will give up.",
518           swap_num, (swap_num == 1) ? "" : "s", status);
519     sfree(s_paths);
520     sfree(s);
521     return -1;
522   } else if (swap_num > status)
523     /* less elements returned than requested */
524     swap_num = status;
525
526   for (int i = 0; i < swap_num; i++) {
527     char path[PATH_MAX];
528     gauge_t this_total;
529     gauge_t this_avail;
530
531     if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
532       continue;
533
534     this_total = (gauge_t)(s->swt_ent[i].ste_pages * pagesize);
535     this_avail = (gauge_t)(s->swt_ent[i].ste_free * pagesize);
536
537     /* Shortcut for the "combined" setting (default) */
538     if (!report_by_device) {
539       avail += this_avail;
540       total += this_total;
541       continue;
542     }
543
544     sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path));
545     escape_slashes(path, sizeof(path));
546
547     swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN);
548   } /* for (swap_num) */
549
550   if (total < avail) {
551     ERROR(
552         "swap plugin: Total swap space (%g) is less than free swap space (%g).",
553         total, avail);
554     sfree(s_paths);
555     sfree(s);
556     return -1;
557   }
558
559   /* If the "separate" option was specified (report_by_device == true) all
560    * values have already been dispatched from within the loop. */
561   if (!report_by_device)
562     swap_submit_usage(NULL, total - avail, avail, NULL, NAN);
563
564   sfree(s_paths);
565   sfree(s);
566   return 0;
567 } /* }}} int swap_read */
568   /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
569
570 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
571 static int swap_read(void) /* {{{ */
572 {
573   struct swapent *swap_entries;
574   int swap_num;
575   int status;
576
577   gauge_t used = 0;
578   gauge_t total = 0;
579
580   swap_num = swapctl(SWAP_NSWAP, NULL, 0);
581   if (swap_num < 0) {
582     ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num);
583     return -1;
584   } else if (swap_num == 0)
585     return 0;
586
587   swap_entries = calloc(swap_num, sizeof(*swap_entries));
588   if (swap_entries == NULL) {
589     ERROR("swap plugin: calloc failed.");
590     return -1;
591   }
592
593   status = swapctl(SWAP_STATS, swap_entries, swap_num);
594   if (status != swap_num) {
595     ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status);
596     sfree(swap_entries);
597     return -1;
598   }
599
600 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
601 #define C_SWAP_BLOCK_SIZE ((gauge_t)DEV_BSIZE)
602 #else
603 #define C_SWAP_BLOCK_SIZE 512.0
604 #endif
605
606   /* TODO: Report per-device stats. The path name is available from
607    * swap_entries[i].se_path */
608   for (int i = 0; i < swap_num; i++) {
609     if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
610       continue;
611
612     used += ((gauge_t)swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
613     total += ((gauge_t)swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
614   }
615
616   if (total < used) {
617     ERROR(
618         "swap plugin: Total swap space (%g) is less than used swap space (%g).",
619         total, used);
620     sfree(swap_entries);
621     return -1;
622   }
623
624   swap_submit_usage(NULL, used, total - used, NULL, NAN);
625
626   sfree(swap_entries);
627   return 0;
628 } /* }}} int swap_read */
629   /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
630
631 #elif defined(VM_SWAPUSAGE)
632 static int swap_read(void) /* {{{ */
633 {
634   int mib[3];
635   size_t mib_len;
636   struct xsw_usage sw_usage;
637   size_t sw_usage_len;
638
639   mib_len = 2;
640   mib[0] = CTL_VM;
641   mib[1] = VM_SWAPUSAGE;
642
643   sw_usage_len = sizeof(struct xsw_usage);
644
645   if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
646     return -1;
647
648   /* The returned values are bytes. */
649   swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used,
650                     (gauge_t)sw_usage.xsu_avail, NULL, NAN);
651
652   return 0;
653 } /* }}} int swap_read */
654   /* #endif VM_SWAPUSAGE */
655
656 #elif HAVE_LIBKVM_GETSWAPINFO
657 static int swap_read(void) /* {{{ */
658 {
659   struct kvm_swap data_s;
660   int status;
661
662   gauge_t used;
663   gauge_t total;
664
665   if (kvm_obj == NULL)
666     return -1;
667
668   /* only one structure => only get the grand total, no details */
669   status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0);
670   if (status == -1)
671     return -1;
672
673   total = (gauge_t)data_s.ksw_total;
674   used = (gauge_t)data_s.ksw_used;
675
676   total *= (gauge_t)kvm_pagesize;
677   used *= (gauge_t)kvm_pagesize;
678
679   swap_submit_usage(NULL, used, total - used, NULL, NAN);
680
681   return 0;
682 } /* }}} int swap_read */
683   /* #endif HAVE_LIBKVM_GETSWAPINFO */
684
685 #elif HAVE_LIBSTATGRAB
686 static int swap_read(void) /* {{{ */
687 {
688   sg_swap_stats *swap;
689
690   swap = sg_get_swap_stats();
691   if (swap == NULL)
692     return -1;
693
694   swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN);
695
696   return 0;
697 } /* }}} int swap_read */
698   /* #endif  HAVE_LIBSTATGRAB */
699
700 #elif HAVE_PERFSTAT
701 static int swap_read(void) /* {{{ */
702 {
703   perfstat_memory_total_t pmemory = {0};
704   int status;
705
706   gauge_t total;
707   gauge_t free;
708   gauge_t reserved;
709
710   status =
711       perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
712   if (status < 0) {
713     WARNING("swap plugin: perfstat_memory_total failed: %s", STRERRNO);
714     return -1;
715   }
716
717   total = (gauge_t)(pmemory.pgsp_total * pagesize);
718   free = (gauge_t)(pmemory.pgsp_free * pagesize);
719   reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize);
720
721   swap_submit_usage(NULL, total - free, free, "reserved", reserved);
722
723   if (report_io) {
724     swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize);
725     swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize);
726   }
727
728   return 0;
729 } /* }}} int swap_read */
730 #endif /* HAVE_PERFSTAT */
731
732 void module_register(void) {
733   plugin_register_complex_config("swap", swap_config);
734   plugin_register_init("swap", swap_init);
735   plugin_register_read("swap", swap_read);
736 } /* void module_register */