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>
39 #if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
40 # define SWAP_HAVE_READ 1
42 # define SWAP_HAVE_READ 0
46 #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) */
63 static kvm_t *kvm_obj = NULL;
65 /* #endif HAVE_LIBKVM */
67 #elif HAVE_LIBSTATGRAB
68 /* No global variables */
69 #endif /* HAVE_LIBSTATGRAB */
71 static int swap_init (void)
75 /* #endif KERNEL_LINUX */
78 /* getpagesize(3C) tells me this does not fail.. */
79 pagesize = (unsigned long long) getpagesize ();
80 if (get_kstat (&ksp, "unix", 0, "system_pages"))
82 /* #endif HAVE_LIBKSTAT */
84 #elif defined(VM_SWAPUSAGE)
86 /* #endif defined(VM_SWAPUSAGE) */
95 kvm_pagesize = getpagesize ();
97 if ((kvm_obj = kvm_open (NULL, /* execfile */
100 O_RDONLY, /* flags */
104 ERROR ("swap plugin: kvm_open failed.");
107 /* #endif HAVE_LIBKVM */
109 #elif HAVE_LIBSTATGRAB
111 #endif /* HAVE_LIBSTATGRAB */
116 static void swap_submit (const char *type_instance, double value)
119 value_list_t vl = VALUE_LIST_INIT;
121 values[0].gauge = value;
125 vl.time = time (NULL);
126 strcpy (vl.host, hostname_g);
127 strcpy (vl.plugin, "swap");
128 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
130 plugin_dispatch_values ("swap", &vl);
131 } /* void swap_submit */
133 static int swap_read (void)
142 unsigned long long swap_used = 0LL;
143 unsigned long long swap_cached = 0LL;
144 unsigned long long swap_free = 0LL;
145 unsigned long long swap_total = 0LL;
147 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
150 WARNING ("memory: fopen: %s",
151 sstrerror (errno, errbuf, sizeof (errbuf)));
155 while (fgets (buffer, 1024, fh) != NULL)
157 unsigned long long *val = NULL;
159 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
161 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
163 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
168 numfields = strsplit (buffer, fields, 8);
173 *val = atoll (fields[1]) * 1024LL;
179 WARNING ("memory: fclose: %s",
180 sstrerror (errno, errbuf, sizeof (errbuf)));
183 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
186 swap_used = swap_total - (swap_free + swap_cached);
188 swap_submit ("used", swap_used);
189 swap_submit ("free", swap_free);
190 swap_submit ("cached", swap_cached);
191 /* #endif KERNEL_LINUX */
194 unsigned long long swap_alloc;
195 unsigned long long swap_resv;
196 unsigned long long swap_avail;
200 if (swapctl (SC_AINFO, &ai) == -1)
203 ERROR ("swap plugin: swapctl failed: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
210 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
212 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
213 * /usr/include/vm/anon.h
215 * In short, swap -s shows: allocated + reserved = used, available
217 * However, Solaris does not allow to allocated/reserved more than the
218 * available swap (physical memory + disk swap), so the pedant may
219 * prefer: allocated + unallocated = reserved, available
221 * We map the above to: used + resv = n/a, free
223 * Does your brain hurt yet? - Christophe Kalt
225 * Oh, and in case you wonder,
226 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
227 * can suffer from a 32bit overflow.
229 swap_alloc = ai.ani_max - ai.ani_free;
230 swap_alloc *= pagesize;
231 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
232 swap_resv *= pagesize;
233 swap_avail = ai.ani_max - ai.ani_resv;
234 swap_avail *= pagesize;
236 swap_submit ("used", swap_alloc);
237 swap_submit ("free", swap_avail);
238 swap_submit ("reserved", swap_resv - swap_alloc);
239 /* #endif HAVE_LIBKSTAT */
241 #elif defined(VM_SWAPUSAGE)
244 struct xsw_usage sw_usage;
249 mib[1] = VM_SWAPUSAGE;
251 sw_usage_len = sizeof (struct xsw_usage);
253 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
256 /* The returned values are bytes. */
257 swap_submit ("used", sw_usage.xsu_used);
258 swap_submit ("free", sw_usage.xsu_avail);
259 /* #endif VM_SWAPUSAGE */
262 struct kvm_swap data_s;
265 unsigned long long used;
266 unsigned long long free;
267 unsigned long long total;
272 /* only one structure => only get the grand total, no details */
273 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
277 total = (unsigned long long) data_s.ksw_total;
278 used = (unsigned long long) data_s.ksw_used;
280 total *= (unsigned long long) kvm_pagesize;
281 used *= (unsigned long long) kvm_pagesize;
285 swap_submit ("used", used);
286 swap_submit ("free", free);
287 /* #endif HAVE_LIBKVM */
289 #elif HAVE_LIBSTATGRAB
292 swap = sg_get_swap_stats ();
297 swap_submit ("used", swap->used);
298 swap_submit ("free", swap->free);
299 #endif /* HAVE_LIBSTATGRAB */
302 } /* int swap_read */
303 #endif /* SWAP_HAVE_READ */
305 void module_register (void)
308 plugin_register_init ("swap", swap_init);
309 plugin_register_read ("swap", swap_read);
310 #endif /* SWAP_HAVE_READ */
311 } /* void module_register */