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>
30 # include <sys/param.h>
33 # include <sys/sysctl.h>
40 # include <statgrab.h>
44 #define MAX(x,y) ((x) > (y) ? (x) : (y))
47 /* No global variables */
48 /* #endif KERNEL_LINUX */
51 static unsigned long long pagesize;
53 /* #endif HAVE_LIBKSTAT */
55 #elif defined(VM_SWAPUSAGE)
56 /* No global variables */
57 /* #endif defined(VM_SWAPUSAGE) */
60 static kvm_t *kvm_obj = NULL;
62 /* #endif HAVE_LIBKVM */
64 #elif HAVE_LIBSTATGRAB
65 /* No global variables */
66 /* #endif HAVE_LIBSTATGRAB */
69 # error "No applicable input method."
70 #endif /* HAVE_LIBSTATGRAB */
72 static int swap_init (void)
76 /* #endif KERNEL_LINUX */
79 /* getpagesize(3C) tells me this does not fail.. */
80 pagesize = (unsigned long long) getpagesize ();
81 if (get_kstat (&ksp, "unix", 0, "system_pages"))
83 /* #endif HAVE_LIBKSTAT */
85 #elif defined(VM_SWAPUSAGE)
87 /* #endif defined(VM_SWAPUSAGE) */
96 kvm_pagesize = getpagesize ();
98 if ((kvm_obj = kvm_open (NULL, /* execfile */
101 O_RDONLY, /* flags */
105 ERROR ("swap plugin: kvm_open failed.");
108 /* #endif HAVE_LIBKVM */
110 #elif HAVE_LIBSTATGRAB
112 #endif /* HAVE_LIBSTATGRAB */
117 static void swap_submit (const char *type_instance, double value)
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].gauge = value;
126 vl.time = time (NULL);
127 strcpy (vl.host, hostname_g);
128 strcpy (vl.plugin, "swap");
129 strcpy (vl.type, "swap");
130 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
132 plugin_dispatch_values (&vl);
133 } /* void swap_submit */
135 static int swap_read (void)
144 unsigned long long swap_used = 0LL;
145 unsigned long long swap_cached = 0LL;
146 unsigned long long swap_free = 0LL;
147 unsigned long long swap_total = 0LL;
149 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
152 WARNING ("memory: fopen: %s",
153 sstrerror (errno, errbuf, sizeof (errbuf)));
157 while (fgets (buffer, 1024, fh) != NULL)
159 unsigned long long *val = NULL;
161 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
163 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
165 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
170 numfields = strsplit (buffer, fields, 8);
175 *val = atoll (fields[1]) * 1024LL;
181 WARNING ("memory: fclose: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
185 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
188 swap_used = swap_total - (swap_free + swap_cached);
190 swap_submit ("used", swap_used);
191 swap_submit ("free", swap_free);
192 swap_submit ("cached", swap_cached);
193 /* #endif KERNEL_LINUX */
196 unsigned long long swap_alloc;
197 unsigned long long swap_resv;
198 unsigned long long swap_avail;
202 if (swapctl (SC_AINFO, &ai) == -1)
205 ERROR ("swap plugin: swapctl failed: %s",
206 sstrerror (errno, errbuf, sizeof (errbuf)));
212 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
214 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
215 * /usr/include/vm/anon.h
217 * In short, swap -s shows: allocated + reserved = used, available
219 * However, Solaris does not allow to allocated/reserved more than the
220 * available swap (physical memory + disk swap), so the pedant may
221 * prefer: allocated + unallocated = reserved, available
223 * We map the above to: used + resv = n/a, free
225 * Does your brain hurt yet? - Christophe Kalt
227 * Oh, and in case you wonder,
228 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
229 * can suffer from a 32bit overflow.
231 swap_alloc = ai.ani_max - ai.ani_free;
232 swap_alloc *= pagesize;
233 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
234 swap_resv *= pagesize;
235 swap_avail = ai.ani_max - ai.ani_resv;
236 swap_avail *= pagesize;
238 swap_submit ("used", swap_alloc);
239 swap_submit ("free", swap_avail);
240 swap_submit ("reserved", swap_resv);
241 /* #endif HAVE_LIBKSTAT */
243 #elif defined(VM_SWAPUSAGE)
246 struct xsw_usage sw_usage;
251 mib[1] = VM_SWAPUSAGE;
253 sw_usage_len = sizeof (struct xsw_usage);
255 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
258 /* The returned values are bytes. */
259 swap_submit ("used", sw_usage.xsu_used);
260 swap_submit ("free", sw_usage.xsu_avail);
261 /* #endif VM_SWAPUSAGE */
264 struct kvm_swap data_s;
267 unsigned long long used;
268 unsigned long long free;
269 unsigned long long total;
274 /* only one structure => only get the grand total, no details */
275 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
279 total = (unsigned long long) data_s.ksw_total;
280 used = (unsigned long long) data_s.ksw_used;
282 total *= (unsigned long long) kvm_pagesize;
283 used *= (unsigned long long) kvm_pagesize;
287 swap_submit ("used", used);
288 swap_submit ("free", free);
289 /* #endif HAVE_LIBKVM */
291 #elif HAVE_LIBSTATGRAB
294 swap = sg_get_swap_stats ();
299 swap_submit ("used", swap->used);
300 swap_submit ("free", swap->free);
301 #endif /* HAVE_LIBSTATGRAB */
304 } /* int swap_read */
306 void module_register (void)
308 plugin_register_init ("swap", swap_init);
309 plugin_register_read ("swap", swap_read);
310 } /* void module_register */