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))
48 static data_source_t data_source[1] =
50 {"value", DS_TYPE_GAUGE, 0, 1099511627776.0}
53 static data_set_t data_set =
55 "swap", 1, data_source
60 /* No global variables */
61 /* #endif KERNEL_LINUX */
64 static unsigned long long pagesize;
66 /* #endif HAVE_LIBKSTAT */
68 #elif defined(VM_SWAPUSAGE)
69 /* No global variables */
70 /* #endif defined(VM_SWAPUSAGE) */
73 static kvm_t *kvm_obj = NULL;
75 /* #endif HAVE_LIBKVM */
77 #elif HAVE_LIBSTATGRAB
78 /* No global variables */
79 #endif /* HAVE_LIBSTATGRAB */
81 static int swap_init (void)
85 /* #endif KERNEL_LINUX */
88 /* getpagesize(3C) tells me this does not fail.. */
89 pagesize = (unsigned long long) getpagesize ();
90 if (get_kstat (&ksp, "unix", 0, "system_pages"))
92 /* #endif HAVE_LIBKSTAT */
94 #elif defined(VM_SWAPUSAGE)
96 /* #endif defined(VM_SWAPUSAGE) */
105 kvm_pagesize = getpagesize ();
107 if ((kvm_obj = kvm_open (NULL, /* execfile */
110 O_RDONLY, /* flags */
114 ERROR ("swap plugin: kvm_open failed.");
117 /* #endif HAVE_LIBKVM */
119 #elif HAVE_LIBSTATGRAB
121 #endif /* HAVE_LIBSTATGRAB */
126 static void swap_submit (const char *type_instance, double value)
129 value_list_t vl = VALUE_LIST_INIT;
131 values[0].gauge = value;
135 vl.time = time (NULL);
136 strcpy (vl.host, hostname_g);
137 strcpy (vl.plugin, "swap");
138 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
140 plugin_dispatch_values ("swap", &vl);
141 } /* void swap_submit */
143 static int swap_read (void)
152 unsigned long long swap_used = 0LL;
153 unsigned long long swap_cached = 0LL;
154 unsigned long long swap_free = 0LL;
155 unsigned long long swap_total = 0LL;
157 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
160 WARNING ("memory: fopen: %s",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
165 while (fgets (buffer, 1024, fh) != NULL)
167 unsigned long long *val = NULL;
169 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
171 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
173 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
178 numfields = strsplit (buffer, fields, 8);
183 *val = atoll (fields[1]) * 1024LL;
189 WARNING ("memory: fclose: %s",
190 sstrerror (errno, errbuf, sizeof (errbuf)));
193 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
196 swap_used = swap_total - (swap_free + swap_cached);
198 swap_submit ("used", swap_used);
199 swap_submit ("free", swap_free);
200 swap_submit ("cached", swap_cached);
201 /* #endif KERNEL_LINUX */
204 unsigned long long swap_alloc;
205 unsigned long long swap_resv;
206 unsigned long long swap_avail;
210 if (swapctl (SC_AINFO, &ai) == -1)
213 ERROR ("swap plugin: swapctl failed: %s",
214 sstrerror (errno, errbuf, sizeof (errbuf)));
220 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
222 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
223 * /usr/include/vm/anon.h
225 * In short, swap -s shows: allocated + reserved = used, available
227 * However, Solaris does not allow to allocated/reserved more than the
228 * available swap (physical memory + disk swap), so the pedant may
229 * prefer: allocated + unallocated = reserved, available
231 * We map the above to: used + resv = n/a, free
233 * Does your brain hurt yet? - Christophe Kalt
235 * Oh, and in case you wonder,
236 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
237 * can suffer from a 32bit overflow.
239 swap_alloc = ai.ani_max - ai.ani_free;
240 swap_alloc *= pagesize;
241 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
242 swap_resv *= pagesize;
243 swap_avail = ai.ani_max - ai.ani_resv;
244 swap_avail *= pagesize;
246 swap_submit ("used", swap_alloc);
247 swap_submit ("free", swap_avail);
248 swap_submit ("reserved", swap_resv - swap_alloc);
249 /* #endif HAVE_LIBKSTAT */
251 #elif defined(VM_SWAPUSAGE)
254 struct xsw_usage sw_usage;
259 mib[1] = VM_SWAPUSAGE;
261 sw_usage_len = sizeof (struct xsw_usage);
263 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
266 /* The returned values are bytes. */
267 swap_submit ("used", sw_usage.xsu_used);
268 swap_submit ("free", sw_usage.xsu_avail);
269 /* #endif VM_SWAPUSAGE */
272 struct kvm_swap data_s;
275 unsigned long long used;
276 unsigned long long free;
277 unsigned long long total;
282 /* only one structure => only get the grand total, no details */
283 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
287 total = (unsigned long long) data_s.ksw_total;
288 used = (unsigned long long) data_s.ksw_used;
290 total *= (unsigned long long) kvm_pagesize;
291 used *= (unsigned long long) kvm_pagesize;
295 swap_submit ("used", used);
296 swap_submit ("free", free);
297 /* #endif HAVE_LIBKVM */
299 #elif HAVE_LIBSTATGRAB
302 swap = sg_get_swap_stats ();
307 swap_submit ("used", swap->used);
308 swap_submit ("free", swap->free);
309 #endif /* HAVE_LIBSTATGRAB */
312 } /* int swap_read */
313 #endif /* SWAP_HAVE_READ */
315 void module_register (void)
317 plugin_register_data_set (&data_set);
320 plugin_register_init ("swap", swap_init);
321 plugin_register_read ("swap", swap_read);
322 #endif /* SWAP_HAVE_READ */