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