1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2010 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 environment" */
32 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
33 # undef _FILE_OFFSET_BITS
34 # undef _LARGEFILE64_SOURCE
35 #endif
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
41 #if HAVE_SYS_SWAP_H
42 # include <sys/swap.h>
43 #endif
44 #if HAVE_VM_ANON_H
45 # include <vm/anon.h>
46 #endif
47 #if HAVE_SYS_PARAM_H
48 # include <sys/param.h>
49 #endif
50 #if HAVE_SYS_SYSCTL_H
51 # include <sys/sysctl.h>
52 #endif
53 #if HAVE_SYS_DKSTAT_H
54 # include <sys/dkstat.h>
55 #endif
56 #if HAVE_KVM_H
57 # include <kvm.h>
58 #endif
60 #if HAVE_STATGRAB_H
61 # include <statgrab.h>
62 #endif
64 #if HAVE_PERFSTAT
65 # include <sys/protosw.h>
66 # include <libperfstat.h>
67 #endif
69 #undef MAX
70 #define MAX(x,y) ((x) > (y) ? (x) : (y))
72 #if KERNEL_LINUX
73 /* No global variables */
74 /* #endif KERNEL_LINUX */
76 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
77 static derive_t pagesize;
79 static const char *config_keys[] =
80 {
81 "ReportByDevice"
82 };
83 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
85 static _Bool report_by_device = 0;
86 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
88 #elif defined(VM_SWAPUSAGE)
89 /* No global variables */
90 /* #endif defined(VM_SWAPUSAGE) */
92 #elif HAVE_LIBKVM_GETSWAPINFO
93 static kvm_t *kvm_obj = NULL;
94 int kvm_pagesize;
95 /* #endif HAVE_LIBKVM_GETSWAPINFO */
97 #elif HAVE_LIBSTATGRAB
98 /* No global variables */
99 /* #endif HAVE_LIBSTATGRAB */
101 #elif HAVE_PERFSTAT
102 static int pagesize;
103 static perfstat_memory_total_t pmemory;
104 /*# endif HAVE_PERFSTAT */
106 #else
107 # error "No applicable input method."
108 #endif /* HAVE_LIBSTATGRAB */
110 static int swap_init (void)
111 {
112 #if KERNEL_LINUX
113 /* No init stuff */
114 /* #endif KERNEL_LINUX */
116 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
117 /* getpagesize(3C) tells me this does not fail.. */
118 pagesize = (derive_t) getpagesize ();
119 /* #endif HAVE_SWAPCTL */
121 #elif defined(VM_SWAPUSAGE)
122 /* No init stuff */
123 /* #endif defined(VM_SWAPUSAGE) */
125 #elif HAVE_LIBKVM_GETSWAPINFO
126 if (kvm_obj != NULL)
127 {
128 kvm_close (kvm_obj);
129 kvm_obj = NULL;
130 }
132 kvm_pagesize = getpagesize ();
134 if ((kvm_obj = kvm_open (NULL, /* execfile */
135 NULL, /* corefile */
136 NULL, /* swapfile */
137 O_RDONLY, /* flags */
138 NULL)) /* errstr */
139 == NULL)
140 {
141 ERROR ("swap plugin: kvm_open failed.");
142 return (-1);
143 }
144 /* #endif HAVE_LIBKVM_GETSWAPINFO */
146 #elif HAVE_LIBSTATGRAB
147 /* No init stuff */
148 /* #endif HAVE_LIBSTATGRAB */
150 #elif HAVE_PERFSTAT
151 pagesize = getpagesize();
152 #endif /* HAVE_PERFSTAT */
154 return (0);
155 }
157 static void swap_submit_inst (const char *plugin_instance, /* {{{ */
158 const char *type_instance, derive_t value, unsigned type)
159 {
160 value_t values[1];
161 value_list_t vl = VALUE_LIST_INIT;
163 switch (type)
164 {
165 case DS_TYPE_GAUGE:
166 values[0].gauge = (gauge_t) value;
167 sstrncpy (vl.type, "swap", sizeof (vl.type));
168 break;
169 case DS_TYPE_DERIVE:
170 values[0].derive = value;
171 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
172 break;
173 default:
174 ERROR ("swap plugin: swap_submit called with wrong"
175 " type");
176 }
178 vl.values = values;
179 vl.values_len = 1;
180 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
181 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
182 if (plugin_instance != NULL)
183 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
184 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
186 plugin_dispatch_values (&vl);
187 } /* }}} void swap_submit_inst */
189 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
190 {
191 swap_submit_inst (/* plugin instance = */ NULL,
192 type_instance, value, type);
193 }
195 #if KERNEL_LINUX
196 static int swap_read (void) /* {{{ */
197 {
198 FILE *fh;
199 char buffer[1024];
201 char *fields[8];
202 int numfields;
204 _Bool old_kernel=0;
206 derive_t swap_used = 0;
207 derive_t swap_cached = 0;
208 derive_t swap_free = 0;
209 derive_t swap_total = 0;
210 derive_t swap_in = 0;
211 derive_t swap_out = 0;
213 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
214 {
215 char errbuf[1024];
216 WARNING ("memory: fopen: %s",
217 sstrerror (errno, errbuf, sizeof (errbuf)));
218 return (-1);
219 }
221 while (fgets (buffer, sizeof (buffer), fh) != NULL)
222 {
223 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
224 if (numfields < 2)
225 continue;
227 if (strcasecmp (fields[0], "SwapTotal:") == 0)
228 strtoderive (fields[1], &swap_total);
229 else if (strcasecmp (fields[0], "SwapFree:") == 0)
230 strtoderive (fields[1], &swap_free);
231 else if (strcasecmp (fields[0], "SwapCached:") == 0)
232 strtoderive (fields[1], &swap_cached);
233 }
235 if (fclose (fh))
236 {
237 char errbuf[1024];
238 WARNING ("memory: fclose: %s",
239 sstrerror (errno, errbuf, sizeof (errbuf)));
240 }
242 if ((swap_free + swap_cached) > swap_total)
243 return (-1);
245 swap_used = swap_total - (swap_free + swap_cached);
247 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
248 {
249 // /proc/vmstat does not exist in kernels <2.6
250 if ((fh = fopen ("/proc/stat", "r")) == NULL )
251 {
252 char errbuf[1024];
253 WARNING ("swap: fopen: %s",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
255 return (-1);
256 }
257 else
258 old_kernel = 1;
259 }
261 while (fgets (buffer, sizeof (buffer), fh) != NULL)
262 {
263 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
265 if (!old_kernel)
266 {
267 if (numfields != 2)
268 continue;
270 if (strcasecmp ("pswpin", fields[0]) == 0)
271 strtoderive (fields[1], &swap_in);
272 else if (strcasecmp ("pswpout", fields[0]) == 0)
273 strtoderive (fields[1], &swap_out);
274 }
275 else /* if (old_kernel) */
276 {
277 if (numfields != 3)
278 continue;
280 if (strcasecmp ("page", fields[0]) == 0)
281 {
282 strtoderive (fields[1], &swap_in);
283 strtoderive (fields[2], &swap_out);
284 }
285 }
286 } /* while (fgets) */
288 if (fclose (fh))
289 {
290 char errbuf[1024];
291 WARNING ("swap: fclose: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
293 }
295 swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE);
296 swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE);
297 swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
298 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
299 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
301 return (0);
302 } /* }}} int swap_read */
303 /* #endif KERNEL_LINUX */
305 /*
306 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
307 * and kstat. The former reads physical space used on a device, the latter
308 * reports the view from the virtual memory system. It was decided that the
309 * kstat-based information should be moved to the "vmem" plugin, but nobody
310 * with enough Solaris experience was available at that time to do this. The
311 * code below is still there for your reference but it won't be activated in
312 * *this* plugin again. --octo
313 */
314 #elif 0 && HAVE_LIBKSTAT
315 /* kstat-based read function */
316 static int swap_read_kstat (void) /* {{{ */
317 {
318 derive_t swap_alloc;
319 derive_t swap_resv;
320 derive_t swap_avail;
322 struct anoninfo ai;
324 if (swapctl (SC_AINFO, &ai) == -1)
325 {
326 char errbuf[1024];
327 ERROR ("swap plugin: swapctl failed: %s",
328 sstrerror (errno, errbuf, sizeof (errbuf)));
329 return (-1);
330 }
332 /*
333 * Calculations from:
334 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
335 * Also see:
336 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
337 * /usr/include/vm/anon.h
338 *
339 * In short, swap -s shows: allocated + reserved = used, available
340 *
341 * However, Solaris does not allow to allocated/reserved more than the
342 * available swap (physical memory + disk swap), so the pedant may
343 * prefer: allocated + unallocated = reserved, available
344 *
345 * We map the above to: used + resv = n/a, free
346 *
347 * Does your brain hurt yet? - Christophe Kalt
348 *
349 * Oh, and in case you wonder,
350 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
351 * can suffer from a 32bit overflow.
352 */
353 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
354 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
355 * pagesize);
356 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
358 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
359 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
360 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
362 return (0);
363 } /* }}} int swap_read_kstat */
364 /* #endif 0 && HAVE_LIBKSTAT */
366 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
367 /* swapctl-based read function */
368 static int swap_read (void) /* {{{ */
369 {
370 swaptbl_t *s;
371 char *s_paths;
372 int swap_num;
373 int status;
374 int i;
376 derive_t avail = 0;
377 derive_t total = 0;
379 swap_num = swapctl (SC_GETNSWP, NULL);
380 if (swap_num < 0)
381 {
382 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
383 swap_num);
384 return (-1);
385 }
386 else if (swap_num == 0)
387 return (0);
389 /* Allocate and initialize the swaptbl_t structure */
390 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
391 if (s == NULL)
392 {
393 ERROR ("swap plugin: smalloc failed.");
394 return (-1);
395 }
397 /* Memory to store the path names. We only use these paths when the
398 * separate option has been configured, but it's easier to just
399 * allocate enough memory in any case. */
400 s_paths = calloc (swap_num, PATH_MAX);
401 if (s_paths == NULL)
402 {
403 ERROR ("swap plugin: malloc failed.");
404 sfree (s);
405 return (-1);
406 }
407 for (i = 0; i < swap_num; i++)
408 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
409 s->swt_n = swap_num;
411 status = swapctl (SC_LIST, s);
412 if (status < 0)
413 {
414 char errbuf[1024];
415 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
416 sstrerror (errno, errbuf, sizeof (errbuf)));
417 sfree (s_paths);
418 sfree (s);
419 return (-1);
420 }
421 else if (swap_num < status)
422 {
423 /* more elements returned than requested */
424 ERROR ("swap plugin: I allocated memory for %i structure%s, "
425 "but swapctl(2) claims to have returned %i. "
426 "I'm confused and will give up.",
427 swap_num, (swap_num == 1) ? "" : "s",
428 status);
429 sfree (s_paths);
430 sfree (s);
431 return (-1);
432 }
433 else if (swap_num > status)
434 /* less elements returned than requested */
435 swap_num = status;
437 for (i = 0; i < swap_num; i++)
438 {
439 char path[PATH_MAX];
440 derive_t this_total;
441 derive_t this_avail;
443 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
444 continue;
446 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
447 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
449 /* Shortcut for the "combined" setting (default) */
450 if (!report_by_device)
451 {
452 avail += this_avail;
453 total += this_total;
454 continue;
455 }
457 /* Okay, using "/" as swap device would be super-weird, but
458 * we'll handle it anyway to cover all cases. */
459 if (strcmp ("/", s->swt_ent[i].ste_path) == 0)
460 sstrncpy (path, "root", sizeof (path));
461 else
462 {
463 int j;
465 s->swt_ent[i].ste_path[PATH_MAX - 1] = 0;
466 /* Don't copy the leading slash */
467 sstrncpy (path, &s->swt_ent[i].ste_path[1], sizeof (path));
468 /* Convert slashes to dashes, just like the "df" plugin. */
469 for (j = 0; path[j] != 0; j++)
470 if (path[j] == '/')
471 path[j] = '-';
472 }
474 swap_submit_inst (path, "used", this_total - this_avail, DS_TYPE_GAUGE);
475 swap_submit_inst (path, "free", this_avail, DS_TYPE_GAUGE);
476 } /* for (swap_num) */
478 if (total < avail)
479 {
480 ERROR ("swap plugin: Total swap space (%"PRIi64") "
481 "is less than free swap space (%"PRIi64").",
482 total, avail);
483 sfree (s_paths);
484 sfree (s);
485 return (-1);
486 }
488 /* If the "separate" option was specified (report_by_device == 2), all
489 * values have already been dispatched from within the loop. */
490 if (!report_by_device)
491 {
492 swap_submit ("used", total - avail, DS_TYPE_GAUGE);
493 swap_submit ("free", avail, DS_TYPE_GAUGE);
494 }
496 sfree (s_paths);
497 sfree (s);
498 return (0);
499 } /* }}} int swap_read */
501 /* Configuration: Present when swapctl or both methods are available. */
502 static int swap_config (const char *key, const char *value) /* {{{ */
503 {
504 if (strcasecmp ("ReportByDevice", key) == 0)
505 {
506 if (IS_TRUE (value))
507 report_by_device = 1;
508 else
509 report_by_device = 0;
510 }
511 else
512 {
513 return (-1);
514 }
516 return (0);
517 } /* }}} int swap_config */
518 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
520 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
521 static int swap_read (void) /* {{{ */
522 {
523 struct swapent *swap_entries;
524 int swap_num;
525 int status;
526 int i;
528 derive_t used = 0;
529 derive_t total = 0;
531 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
532 if (swap_num < 0)
533 {
534 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
535 swap_num);
536 return (-1);
537 }
538 else if (swap_num == 0)
539 return (0);
541 swap_entries = calloc (swap_num, sizeof (*swap_entries));
542 if (swap_entries == NULL)
543 {
544 ERROR ("swap plugin: calloc failed.");
545 return (-1);
546 }
548 status = swapctl (SWAP_STATS, swap_entries, swap_num);
549 if (status != swap_num)
550 {
551 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
552 status);
553 sfree (swap_entries);
554 return (-1);
555 }
557 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
558 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
559 #else
560 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
561 #endif
563 for (i = 0; i < swap_num; i++)
564 {
565 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
566 continue;
568 used += ((derive_t) swap_entries[i].se_inuse)
569 * C_SWAP_BLOCK_SIZE;
570 total += ((derive_t) swap_entries[i].se_nblks)
571 * C_SWAP_BLOCK_SIZE;
572 }
574 if (total < used)
575 {
576 ERROR ("swap plugin: Total swap space (%"PRIu64") "
577 "is less than used swap space (%"PRIu64").",
578 total, used);
579 return (-1);
580 }
582 swap_submit ("used", used, DS_TYPE_GAUGE);
583 swap_submit ("free", total - used, DS_TYPE_GAUGE);
585 sfree (swap_entries);
587 return (0);
588 } /* }}} int swap_read */
589 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
591 #elif defined(VM_SWAPUSAGE)
592 static int swap_read (void) /* {{{ */
593 {
594 int mib[3];
595 size_t mib_len;
596 struct xsw_usage sw_usage;
597 size_t sw_usage_len;
599 mib_len = 2;
600 mib[0] = CTL_VM;
601 mib[1] = VM_SWAPUSAGE;
603 sw_usage_len = sizeof (struct xsw_usage);
605 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
606 return (-1);
608 /* The returned values are bytes. */
609 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
610 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
612 return (0);
613 } /* }}} int swap_read */
614 /* #endif VM_SWAPUSAGE */
616 #elif HAVE_LIBKVM_GETSWAPINFO
617 static int swap_read (void) /* {{{ */
618 {
619 struct kvm_swap data_s;
620 int status;
622 derive_t used;
623 derive_t free;
624 derive_t total;
626 if (kvm_obj == NULL)
627 return (-1);
629 /* only one structure => only get the grand total, no details */
630 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
631 if (status == -1)
632 return (-1);
634 total = (derive_t) data_s.ksw_total;
635 used = (derive_t) data_s.ksw_used;
637 total *= (derive_t) kvm_pagesize;
638 used *= (derive_t) kvm_pagesize;
640 free = total - used;
642 swap_submit ("used", used, DS_TYPE_GAUGE);
643 swap_submit ("free", free, DS_TYPE_GAUGE);
645 return (0);
646 } /* }}} int swap_read */
647 /* #endif HAVE_LIBKVM_GETSWAPINFO */
649 #elif HAVE_LIBSTATGRAB
650 static int swap_read (void) /* {{{ */
651 {
652 sg_swap_stats *swap;
654 swap = sg_get_swap_stats ();
656 if (swap == NULL)
657 return (-1);
659 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
660 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
662 return (0);
663 } /* }}} int swap_read */
664 /* #endif HAVE_LIBSTATGRAB */
666 #elif HAVE_PERFSTAT
667 static int swap_read (void) /* {{{ */
668 {
669 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
670 {
671 char errbuf[1024];
672 WARNING ("memory plugin: perfstat_memory_total failed: %s",
673 sstrerror (errno, errbuf, sizeof (errbuf)));
674 return (-1);
675 }
676 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
677 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
679 return (0);
680 } /* }}} int swap_read */
681 #endif /* HAVE_PERFSTAT */
683 void module_register (void)
684 {
685 #if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
686 plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
687 #endif
688 plugin_register_init ("swap", swap_init);
689 plugin_register_read ("swap", swap_read);
690 } /* void module_register */
692 /* vim: set fdm=marker : */