2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 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>
30 # include <sys/param.h>
33 # include <sys/sysctl.h>
36 # include <sys/dkstat.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 */
59 /* No global variables */
60 /* #endif HAVE_SWAPCTL */
62 #elif defined(VM_SWAPUSAGE)
63 /* No global variables */
64 /* #endif defined(VM_SWAPUSAGE) */
66 #elif HAVE_LIBKVM_GETSWAPINFO
67 static kvm_t *kvm_obj = NULL;
69 /* #endif HAVE_LIBKVM_GETSWAPINFO */
71 #elif HAVE_LIBSTATGRAB
72 /* No global variables */
73 /* #endif HAVE_LIBSTATGRAB */
76 # error "No applicable input method."
77 #endif /* HAVE_LIBSTATGRAB */
79 static int swap_init (void)
83 /* #endif KERNEL_LINUX */
86 /* getpagesize(3C) tells me this does not fail.. */
87 pagesize = (unsigned long long) getpagesize ();
88 if (get_kstat (&ksp, "unix", 0, "system_pages"))
90 /* #endif HAVE_LIBKSTAT */
94 /* #endif HAVE_SWAPCTL */
96 #elif defined(VM_SWAPUSAGE)
98 /* #endif defined(VM_SWAPUSAGE) */
100 #elif HAVE_LIBKVM_GETSWAPINFO
107 kvm_pagesize = getpagesize ();
109 if ((kvm_obj = kvm_open (NULL, /* execfile */
112 O_RDONLY, /* flags */
116 ERROR ("swap plugin: kvm_open failed.");
119 /* #endif HAVE_LIBKVM_GETSWAPINFO */
121 #elif HAVE_LIBSTATGRAB
123 #endif /* HAVE_LIBSTATGRAB */
128 static void swap_submit (const char *type_instance, double value)
131 value_list_t vl = VALUE_LIST_INIT;
133 values[0].gauge = value;
137 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
138 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
139 sstrncpy (vl.type, "swap", sizeof (vl.type));
140 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
143 } /* void swap_submit */
145 static int swap_read (void)
154 unsigned long long swap_used = 0LL;
155 unsigned long long swap_cached = 0LL;
156 unsigned long long swap_free = 0LL;
157 unsigned long long swap_total = 0LL;
159 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
162 WARNING ("memory: fopen: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
167 while (fgets (buffer, 1024, fh) != NULL)
169 unsigned long long *val = NULL;
171 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
173 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
175 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
180 numfields = strsplit (buffer, fields, 8);
185 *val = atoll (fields[1]) * 1024LL;
191 WARNING ("memory: fclose: %s",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
195 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
198 swap_used = swap_total - (swap_free + swap_cached);
200 swap_submit ("used", swap_used);
201 swap_submit ("free", swap_free);
202 swap_submit ("cached", swap_cached);
203 /* #endif KERNEL_LINUX */
206 unsigned long long swap_alloc;
207 unsigned long long swap_resv;
208 unsigned long long swap_avail;
212 if (swapctl (SC_AINFO, &ai) == -1)
215 ERROR ("swap plugin: swapctl failed: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
222 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
224 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
225 * /usr/include/vm/anon.h
227 * In short, swap -s shows: allocated + reserved = used, available
229 * However, Solaris does not allow to allocated/reserved more than the
230 * available swap (physical memory + disk swap), so the pedant may
231 * prefer: allocated + unallocated = reserved, available
233 * We map the above to: used + resv = n/a, free
235 * Does your brain hurt yet? - Christophe Kalt
237 * Oh, and in case you wonder,
238 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
239 * can suffer from a 32bit overflow.
241 swap_alloc = ai.ani_max - ai.ani_free;
242 swap_alloc *= pagesize;
243 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
244 swap_resv *= pagesize;
245 swap_avail = ai.ani_max - ai.ani_resv;
246 swap_avail *= pagesize;
248 swap_submit ("used", swap_alloc);
249 swap_submit ("free", swap_avail);
250 swap_submit ("reserved", swap_resv);
251 /* #endif HAVE_LIBKSTAT */
254 struct swapent *swap_entries;
263 * XXX: This is the syntax for the *BSD `swapctl', which has the
264 * following prototype:
265 * swapctl (int cmd, void *arg, int misc);
267 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
268 * too, but with the following prototype:
269 * swapctl (int cmd, void *arg);
271 * Solaris is usually handled in the KSTAT case above. For other UNIXes
272 * a separate case for the other version of `swapctl' may be necessary.
274 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
277 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
281 else if (swap_num == 0)
284 swap_entries = calloc (swap_num, sizeof (*swap_entries));
285 if (swap_entries == NULL)
287 ERROR ("swap plugin: calloc failed.");
291 status = swapctl (SWAP_STATS, swap_entries, swap_num);
292 if (status != swap_num)
294 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
296 sfree (swap_entries);
300 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
301 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
303 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
306 for (i = 0; i < swap_num; i++)
308 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
311 used += ((uint64_t) swap_entries[i].se_inuse)
313 total += ((uint64_t) swap_entries[i].se_nblks)
319 ERROR ("swap plugin: Total swap space (%"PRIu64") "
320 "is less than used swap space (%"PRIu64").",
325 swap_submit ("used", (gauge_t) used);
326 swap_submit ("free", (gauge_t) (total - used));
328 sfree (swap_entries);
329 /* #endif HAVE_SWAPCTL */
331 #elif defined(VM_SWAPUSAGE)
334 struct xsw_usage sw_usage;
339 mib[1] = VM_SWAPUSAGE;
341 sw_usage_len = sizeof (struct xsw_usage);
343 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
346 /* The returned values are bytes. */
347 swap_submit ("used", sw_usage.xsu_used);
348 swap_submit ("free", sw_usage.xsu_avail);
349 /* #endif VM_SWAPUSAGE */
351 #elif HAVE_LIBKVM_GETSWAPINFO
352 struct kvm_swap data_s;
355 unsigned long long used;
356 unsigned long long free;
357 unsigned long long total;
362 /* only one structure => only get the grand total, no details */
363 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
367 total = (unsigned long long) data_s.ksw_total;
368 used = (unsigned long long) data_s.ksw_used;
370 total *= (unsigned long long) kvm_pagesize;
371 used *= (unsigned long long) kvm_pagesize;
375 swap_submit ("used", used);
376 swap_submit ("free", free);
377 /* #endif HAVE_LIBKVM_GETSWAPINFO */
379 #elif HAVE_LIBSTATGRAB
382 swap = sg_get_swap_stats ();
387 swap_submit ("used", swap->used);
388 swap_submit ("free", swap->free);
389 #endif /* HAVE_LIBSTATGRAB */
392 } /* int swap_read */
394 void module_register (void)
396 plugin_register_init ("swap", swap_init);
397 plugin_register_read ("swap", swap_read);
398 } /* void module_register */