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 #define MAX(x,y) ((x) > (y) ? (x) : (y))
43 /* No global variables */
44 /* #endif KERNEL_LINUX */
47 static unsigned long long pagesize;
49 /* #endif HAVE_LIBKSTAT */
51 #elif defined(VM_SWAPUSAGE)
52 /* No global variables */
53 /* #endif defined(VM_SWAPUSAGE) */
56 static kvm_t *kvm_obj = NULL;
58 /* #endif HAVE_LIBKVM */
60 #elif HAVE_LIBSTATGRAB
61 /* No global variables */
62 /* #endif HAVE_LIBSTATGRAB */
65 # error "No applicable input method."
66 #endif /* HAVE_LIBSTATGRAB */
68 static int swap_init (void)
72 /* #endif KERNEL_LINUX */
75 /* getpagesize(3C) tells me this does not fail.. */
76 pagesize = (unsigned long long) getpagesize ();
77 if (get_kstat (&ksp, "unix", 0, "system_pages"))
79 /* #endif HAVE_LIBKSTAT */
81 #elif defined(VM_SWAPUSAGE)
83 /* #endif defined(VM_SWAPUSAGE) */
92 kvm_pagesize = getpagesize ();
94 if ((kvm_obj = kvm_open (NULL, /* execfile */
101 ERROR ("swap plugin: kvm_open failed.");
104 /* #endif HAVE_LIBKVM */
106 #elif HAVE_LIBSTATGRAB
108 #endif /* HAVE_LIBSTATGRAB */
113 static void swap_submit (const char *type_instance, double value)
116 value_list_t vl = VALUE_LIST_INIT;
118 values[0].gauge = value;
122 vl.time = time (NULL);
123 strcpy (vl.host, hostname_g);
124 strcpy (vl.plugin, "swap");
125 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
127 plugin_dispatch_values ("swap", &vl);
128 } /* void swap_submit */
130 static int swap_read (void)
139 unsigned long long swap_used = 0LL;
140 unsigned long long swap_cached = 0LL;
141 unsigned long long swap_free = 0LL;
142 unsigned long long swap_total = 0LL;
144 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
147 WARNING ("memory: fopen: %s",
148 sstrerror (errno, errbuf, sizeof (errbuf)));
152 while (fgets (buffer, 1024, fh) != NULL)
154 unsigned long long *val = NULL;
156 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
158 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
160 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
165 numfields = strsplit (buffer, fields, 8);
170 *val = atoll (fields[1]) * 1024LL;
176 WARNING ("memory: fclose: %s",
177 sstrerror (errno, errbuf, sizeof (errbuf)));
180 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
183 swap_used = swap_total - (swap_free + swap_cached);
185 swap_submit ("used", swap_used);
186 swap_submit ("free", swap_free);
187 swap_submit ("cached", swap_cached);
188 /* #endif KERNEL_LINUX */
191 unsigned long long swap_alloc;
192 unsigned long long swap_resv;
193 unsigned long long swap_avail;
197 if (swapctl (SC_AINFO, &ai) == -1)
200 ERROR ("swap plugin: swapctl failed: %s",
201 sstrerror (errno, errbuf, sizeof (errbuf)));
207 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
209 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
210 * /usr/include/vm/anon.h
212 * In short, swap -s shows: allocated + reserved = used, available
214 * However, Solaris does not allow to allocated/reserved more than the
215 * available swap (physical memory + disk swap), so the pedant may
216 * prefer: allocated + unallocated = reserved, available
218 * We map the above to: used + resv = n/a, free
220 * Does your brain hurt yet? - Christophe Kalt
222 * Oh, and in case you wonder,
223 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
224 * can suffer from a 32bit overflow.
226 swap_alloc = ai.ani_max - ai.ani_free;
227 swap_alloc *= pagesize;
228 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
229 swap_resv *= pagesize;
230 swap_avail = ai.ani_max - ai.ani_resv;
231 swap_avail *= pagesize;
233 swap_submit ("used", swap_alloc);
234 swap_submit ("free", swap_avail);
235 swap_submit ("reserved", swap_resv - swap_alloc);
236 /* #endif HAVE_LIBKSTAT */
238 #elif defined(VM_SWAPUSAGE)
241 struct xsw_usage sw_usage;
246 mib[1] = VM_SWAPUSAGE;
248 sw_usage_len = sizeof (struct xsw_usage);
250 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
253 /* The returned values are bytes. */
254 swap_submit ("used", sw_usage.xsu_used);
255 swap_submit ("free", sw_usage.xsu_avail);
256 /* #endif VM_SWAPUSAGE */
259 struct kvm_swap data_s;
262 unsigned long long used;
263 unsigned long long free;
264 unsigned long long total;
269 /* only one structure => only get the grand total, no details */
270 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
274 total = (unsigned long long) data_s.ksw_total;
275 used = (unsigned long long) data_s.ksw_used;
277 total *= (unsigned long long) kvm_pagesize;
278 used *= (unsigned long long) kvm_pagesize;
282 swap_submit ("used", used);
283 swap_submit ("free", free);
284 /* #endif HAVE_LIBKVM */
286 #elif HAVE_LIBSTATGRAB
289 swap = sg_get_swap_stats ();
294 swap_submit ("used", swap->used);
295 swap_submit ("free", swap->free);
296 #endif /* HAVE_LIBSTATGRAB */
299 } /* int swap_read */
301 void module_register (void)
303 plugin_register_init ("swap", swap_init);
304 plugin_register_read ("swap", swap_read);
305 } /* void module_register */