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