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>
33 # include <sys/param.h>
36 # include <sys/sysctl.h>
39 # include <sys/dkstat.h>
46 # include <statgrab.h>
50 #define MAX(x,y) ((x) > (y) ? (x) : (y))
53 /* No global variables */
54 /* #endif KERNEL_LINUX */
57 static unsigned long long pagesize;
59 /* #endif HAVE_LIBKSTAT */
62 /* No global variables */
63 /* #endif HAVE_SWAPCTL */
65 #elif defined(VM_SWAPUSAGE)
66 /* No global variables */
67 /* #endif defined(VM_SWAPUSAGE) */
69 #elif HAVE_LIBKVM_GETSWAPINFO
70 static kvm_t *kvm_obj = NULL;
72 /* #endif HAVE_LIBKVM_GETSWAPINFO */
74 #elif HAVE_LIBSTATGRAB
75 /* No global variables */
76 /* #endif HAVE_LIBSTATGRAB */
79 # error "No applicable input method."
80 #endif /* HAVE_LIBSTATGRAB */
82 static int swap_init (void)
86 /* #endif KERNEL_LINUX */
89 /* getpagesize(3C) tells me this does not fail.. */
90 pagesize = (unsigned long long) getpagesize ();
91 if (get_kstat (&ksp, "unix", 0, "system_pages"))
93 /* #endif HAVE_LIBKSTAT */
97 /* #endif HAVE_SWAPCTL */
99 #elif defined(VM_SWAPUSAGE)
101 /* #endif defined(VM_SWAPUSAGE) */
103 #elif HAVE_LIBKVM_GETSWAPINFO
110 kvm_pagesize = getpagesize ();
112 if ((kvm_obj = kvm_open (NULL, /* execfile */
115 O_RDONLY, /* flags */
119 ERROR ("swap plugin: kvm_open failed.");
122 /* #endif HAVE_LIBKVM_GETSWAPINFO */
124 #elif HAVE_LIBSTATGRAB
126 #endif /* HAVE_LIBSTATGRAB */
131 static void swap_submit (const char *type_instance, double value)
134 value_list_t vl = VALUE_LIST_INIT;
136 values[0].gauge = value;
140 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
141 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
142 sstrncpy (vl.type, "swap", sizeof (vl.type));
143 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
145 plugin_dispatch_values (&vl);
146 } /* void swap_submit */
148 static int swap_read (void)
157 unsigned long long swap_used = 0LL;
158 unsigned long long swap_cached = 0LL;
159 unsigned long long swap_free = 0LL;
160 unsigned long long swap_total = 0LL;
162 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
165 WARNING ("memory: fopen: %s",
166 sstrerror (errno, errbuf, sizeof (errbuf)));
170 while (fgets (buffer, 1024, fh) != NULL)
172 unsigned long long *val = NULL;
174 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
176 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
178 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
183 numfields = strsplit (buffer, fields, 8);
188 *val = atoll (fields[1]) * 1024LL;
194 WARNING ("memory: fclose: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
198 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
201 swap_used = swap_total - (swap_free + swap_cached);
203 swap_submit ("used", swap_used);
204 swap_submit ("free", swap_free);
205 swap_submit ("cached", swap_cached);
206 /* #endif KERNEL_LINUX */
209 unsigned long long swap_alloc;
210 unsigned long long swap_resv;
211 unsigned long long swap_avail;
215 if (swapctl (SC_AINFO, &ai) == -1)
218 ERROR ("swap plugin: swapctl failed: %s",
219 sstrerror (errno, errbuf, sizeof (errbuf)));
225 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
227 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
228 * /usr/include/vm/anon.h
230 * In short, swap -s shows: allocated + reserved = used, available
232 * However, Solaris does not allow to allocated/reserved more than the
233 * available swap (physical memory + disk swap), so the pedant may
234 * prefer: allocated + unallocated = reserved, available
236 * We map the above to: used + resv = n/a, free
238 * Does your brain hurt yet? - Christophe Kalt
240 * Oh, and in case you wonder,
241 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
242 * can suffer from a 32bit overflow.
244 swap_alloc = ai.ani_max - ai.ani_free;
245 swap_alloc *= pagesize;
246 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
247 swap_resv *= pagesize;
248 swap_avail = ai.ani_max - ai.ani_resv;
249 swap_avail *= pagesize;
251 swap_submit ("used", swap_alloc);
252 swap_submit ("free", swap_avail);
253 swap_submit ("reserved", swap_resv);
254 /* #endif HAVE_LIBKSTAT */
257 struct swapent *swap_entries;
266 * XXX: This is the syntax for the *BSD `swapctl', which has the
267 * following prototype:
268 * swapctl (int cmd, void *arg, int misc);
270 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
271 * too, but with the following prototype:
272 * swapctl (int cmd, void *arg);
274 * Solaris is usually handled in the KSTAT case above. For other UNIXes
275 * a separate case for the other version of `swapctl' may be necessary.
277 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
280 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
284 else if (swap_num == 0)
287 swap_entries = calloc (swap_num, sizeof (*swap_entries));
288 if (swap_entries == NULL)
290 ERROR ("swap plugin: calloc failed.");
294 status = swapctl (SWAP_STATS, swap_entries, swap_num);
295 if (status != swap_num)
297 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
299 sfree (swap_entries);
303 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
304 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
306 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
309 for (i = 0; i < swap_num; i++)
311 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
314 used += ((uint64_t) swap_entries[i].se_inuse)
316 total += ((uint64_t) swap_entries[i].se_nblks)
322 ERROR ("swap plugin: Total swap space (%"PRIu64") "
323 "is less than used swap space (%"PRIu64").",
328 swap_submit ("used", (gauge_t) used);
329 swap_submit ("free", (gauge_t) (total - used));
331 sfree (swap_entries);
332 /* #endif HAVE_SWAPCTL */
334 #elif defined(VM_SWAPUSAGE)
337 struct xsw_usage sw_usage;
342 mib[1] = VM_SWAPUSAGE;
344 sw_usage_len = sizeof (struct xsw_usage);
346 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
349 /* The returned values are bytes. */
350 swap_submit ("used", sw_usage.xsu_used);
351 swap_submit ("free", sw_usage.xsu_avail);
352 /* #endif VM_SWAPUSAGE */
354 #elif HAVE_LIBKVM_GETSWAPINFO
355 struct kvm_swap data_s;
358 unsigned long long used;
359 unsigned long long free;
360 unsigned long long total;
365 /* only one structure => only get the grand total, no details */
366 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
370 total = (unsigned long long) data_s.ksw_total;
371 used = (unsigned long long) data_s.ksw_used;
373 total *= (unsigned long long) kvm_pagesize;
374 used *= (unsigned long long) kvm_pagesize;
378 swap_submit ("used", used);
379 swap_submit ("free", free);
380 /* #endif HAVE_LIBKVM_GETSWAPINFO */
382 #elif HAVE_LIBSTATGRAB
385 swap = sg_get_swap_stats ();
390 swap_submit ("used", swap->used);
391 swap_submit ("free", swap->free);
392 #endif /* HAVE_LIBSTATGRAB */
395 } /* int swap_read */
397 void module_register (void)
399 plugin_register_init ("swap", swap_init);
400 plugin_register_read ("swap", swap_read);
401 } /* void module_register */