2 * collectd - src/swap.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
27 # include <sys/swap.h>
33 # include <sys/param.h>
36 # include <sys/sysctl.h>
43 # include <statgrab.h>
47 #define MAX(x,y) ((x) > (y) ? (x) : (y))
50 /* No global variables */
51 /* #endif KERNEL_LINUX */
54 static unsigned long long pagesize;
56 /* #endif HAVE_LIBKSTAT */
58 #elif defined(VM_SWAPUSAGE)
59 /* No global variables */
60 /* #endif defined(VM_SWAPUSAGE) */
62 #elif HAVE_LIBKVM_GETSWAPINFO
63 static kvm_t *kvm_obj = NULL;
65 /* #endif HAVE_LIBKVM_GETSWAPINFO */
67 #elif HAVE_LIBSTATGRAB
68 /* No global variables */
69 /* #endif HAVE_LIBSTATGRAB */
72 # error "No applicable input method."
73 #endif /* HAVE_LIBSTATGRAB */
75 static int swap_init (void)
79 /* #endif KERNEL_LINUX */
82 /* getpagesize(3C) tells me this does not fail.. */
83 pagesize = (unsigned long long) getpagesize ();
84 if (get_kstat (&ksp, "unix", 0, "system_pages"))
86 /* #endif HAVE_LIBKSTAT */
88 #elif defined(VM_SWAPUSAGE)
90 /* #endif defined(VM_SWAPUSAGE) */
92 #elif HAVE_LIBKVM_GETSWAPINFO
99 kvm_pagesize = getpagesize ();
101 if ((kvm_obj = kvm_open (NULL, /* execfile */
104 O_RDONLY, /* flags */
108 ERROR ("swap plugin: kvm_open failed.");
111 /* #endif HAVE_LIBKVM_GETSWAPINFO */
113 #elif HAVE_LIBSTATGRAB
115 #endif /* HAVE_LIBSTATGRAB */
120 static void swap_submit (const char *type_instance, double value)
123 value_list_t vl = VALUE_LIST_INIT;
125 values[0].gauge = value;
129 vl.time = time (NULL);
130 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
131 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
132 sstrncpy (vl.type, "swap", sizeof (vl.type));
133 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
135 plugin_dispatch_values (&vl);
136 } /* void swap_submit */
138 static int swap_read (void)
147 unsigned long long swap_used = 0LL;
148 unsigned long long swap_cached = 0LL;
149 unsigned long long swap_free = 0LL;
150 unsigned long long swap_total = 0LL;
152 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
155 WARNING ("memory: fopen: %s",
156 sstrerror (errno, errbuf, sizeof (errbuf)));
160 while (fgets (buffer, 1024, fh) != NULL)
162 unsigned long long *val = NULL;
164 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
166 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
168 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
173 numfields = strsplit (buffer, fields, 8);
178 *val = atoll (fields[1]) * 1024LL;
184 WARNING ("memory: fclose: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
188 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
191 swap_used = swap_total - (swap_free + swap_cached);
193 swap_submit ("used", swap_used);
194 swap_submit ("free", swap_free);
195 swap_submit ("cached", swap_cached);
196 /* #endif KERNEL_LINUX */
199 unsigned long long swap_alloc;
200 unsigned long long swap_resv;
201 unsigned long long swap_avail;
205 if (swapctl (SC_AINFO, &ai) == -1)
208 ERROR ("swap plugin: swapctl failed: %s",
209 sstrerror (errno, errbuf, sizeof (errbuf)));
215 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
217 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
218 * /usr/include/vm/anon.h
220 * In short, swap -s shows: allocated + reserved = used, available
222 * However, Solaris does not allow to allocated/reserved more than the
223 * available swap (physical memory + disk swap), so the pedant may
224 * prefer: allocated + unallocated = reserved, available
226 * We map the above to: used + resv = n/a, free
228 * Does your brain hurt yet? - Christophe Kalt
230 * Oh, and in case you wonder,
231 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
232 * can suffer from a 32bit overflow.
234 swap_alloc = ai.ani_max - ai.ani_free;
235 swap_alloc *= pagesize;
236 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
237 swap_resv *= pagesize;
238 swap_avail = ai.ani_max - ai.ani_resv;
239 swap_avail *= pagesize;
241 swap_submit ("used", swap_alloc);
242 swap_submit ("free", swap_avail);
243 swap_submit ("reserved", swap_resv);
244 /* #endif HAVE_LIBKSTAT */
246 #elif defined(VM_SWAPUSAGE)
249 struct xsw_usage sw_usage;
254 mib[1] = VM_SWAPUSAGE;
256 sw_usage_len = sizeof (struct xsw_usage);
258 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
261 /* The returned values are bytes. */
262 swap_submit ("used", sw_usage.xsu_used);
263 swap_submit ("free", sw_usage.xsu_avail);
264 /* #endif VM_SWAPUSAGE */
266 #elif HAVE_LIBKVM_GETSWAPINFO
267 struct kvm_swap data_s;
270 unsigned long long used;
271 unsigned long long free;
272 unsigned long long total;
277 /* only one structure => only get the grand total, no details */
278 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
282 total = (unsigned long long) data_s.ksw_total;
283 used = (unsigned long long) data_s.ksw_used;
285 total *= (unsigned long long) kvm_pagesize;
286 used *= (unsigned long long) kvm_pagesize;
290 swap_submit ("used", used);
291 swap_submit ("free", free);
292 /* #endif HAVE_LIBKVM_GETSWAPINFO */
294 #elif HAVE_LIBSTATGRAB
297 swap = sg_get_swap_stats ();
302 swap_submit ("used", swap->used);
303 swap_submit ("free", swap->free);
304 #endif /* HAVE_LIBSTATGRAB */
307 } /* int swap_read */
309 void module_register (void)
311 plugin_register_init ("swap", swap_init);
312 plugin_register_read ("swap", swap_read);
313 } /* void module_register */