1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 # undef HAVE_CONFIG_H
25 #endif
26 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
27 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
28 # undef _FILE_OFFSET_BITS
29 # undef _LARGEFILE64_SOURCE
30 #endif
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
36 #if HAVE_SYS_SWAP_H
37 # include <sys/swap.h>
38 #endif
39 #if HAVE_VM_ANON_H
40 # include <vm/anon.h>
41 #endif
42 #if HAVE_SYS_PARAM_H
43 # include <sys/param.h>
44 #endif
45 #if HAVE_SYS_SYSCTL_H
46 # include <sys/sysctl.h>
47 #endif
48 #if HAVE_SYS_DKSTAT_H
49 # include <sys/dkstat.h>
50 #endif
51 #if HAVE_KVM_H
52 # include <kvm.h>
53 #endif
55 #if HAVE_STATGRAB_H
56 # include <statgrab.h>
57 #endif
59 #undef MAX
60 #define MAX(x,y) ((x) > (y) ? (x) : (y))
62 #if KERNEL_LINUX
63 /* No global variables */
64 /* #endif KERNEL_LINUX */
66 #elif HAVE_LIBKSTAT
67 static unsigned long long pagesize;
68 static kstat_t *ksp;
69 /* #endif HAVE_LIBKSTAT */
71 #elif HAVE_SWAPCTL
72 /* No global variables */
73 /* #endif HAVE_SWAPCTL */
75 #elif defined(VM_SWAPUSAGE)
76 /* No global variables */
77 /* #endif defined(VM_SWAPUSAGE) */
79 #elif HAVE_LIBKVM_GETSWAPINFO
80 static kvm_t *kvm_obj = NULL;
81 int kvm_pagesize;
82 /* #endif HAVE_LIBKVM_GETSWAPINFO */
84 #elif HAVE_LIBSTATGRAB
85 /* No global variables */
86 /* #endif HAVE_LIBSTATGRAB */
88 #else
89 # error "No applicable input method."
90 #endif /* HAVE_LIBSTATGRAB */
92 static int swap_init (void)
93 {
94 #if KERNEL_LINUX
95 /* No init stuff */
96 /* #endif KERNEL_LINUX */
98 #elif HAVE_LIBKSTAT
99 /* getpagesize(3C) tells me this does not fail.. */
100 pagesize = (unsigned long long) getpagesize ();
101 if (get_kstat (&ksp, "unix", 0, "system_pages"))
102 ksp = NULL;
103 /* #endif HAVE_LIBKSTAT */
105 #elif HAVE_SWAPCTL
106 /* No init stuff */
107 /* #endif HAVE_SWAPCTL */
109 #elif defined(VM_SWAPUSAGE)
110 /* No init stuff */
111 /* #endif defined(VM_SWAPUSAGE) */
113 #elif HAVE_LIBKVM_GETSWAPINFO
114 if (kvm_obj != NULL)
115 {
116 kvm_close (kvm_obj);
117 kvm_obj = NULL;
118 }
120 kvm_pagesize = getpagesize ();
122 if ((kvm_obj = kvm_open (NULL, /* execfile */
123 NULL, /* corefile */
124 NULL, /* swapfile */
125 O_RDONLY, /* flags */
126 NULL)) /* errstr */
127 == NULL)
128 {
129 ERROR ("swap plugin: kvm_open failed.");
130 return (-1);
131 }
132 /* #endif HAVE_LIBKVM_GETSWAPINFO */
134 #elif HAVE_LIBSTATGRAB
135 /* No init stuff */
136 #endif /* HAVE_LIBSTATGRAB */
138 return (0);
139 }
141 static void swap_submit (const char *type_instance, double value)
142 {
143 value_t values[1];
144 value_list_t vl = VALUE_LIST_INIT;
146 values[0].gauge = value;
148 vl.values = values;
149 vl.values_len = 1;
150 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
151 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
152 sstrncpy (vl.type, "swap", sizeof (vl.type));
153 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
155 plugin_dispatch_values (&vl);
156 } /* void swap_submit */
158 static int swap_read (void)
159 {
160 #if KERNEL_LINUX
161 FILE *fh;
162 char buffer[1024];
164 char *fields[8];
165 int numfields;
167 unsigned long long swap_used = 0LL;
168 unsigned long long swap_cached = 0LL;
169 unsigned long long swap_free = 0LL;
170 unsigned long long swap_total = 0LL;
172 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
173 {
174 char errbuf[1024];
175 WARNING ("memory: fopen: %s",
176 sstrerror (errno, errbuf, sizeof (errbuf)));
177 return (-1);
178 }
180 while (fgets (buffer, 1024, fh) != NULL)
181 {
182 unsigned long long *val = NULL;
184 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
185 val = &swap_total;
186 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
187 val = &swap_free;
188 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
189 val = &swap_cached;
190 else
191 continue;
193 numfields = strsplit (buffer, fields, 8);
195 if (numfields < 2)
196 continue;
198 *val = atoll (fields[1]) * 1024LL;
199 }
201 if (fclose (fh))
202 {
203 char errbuf[1024];
204 WARNING ("memory: fclose: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
206 }
208 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
209 return (-1);
211 swap_used = swap_total - (swap_free + swap_cached);
213 swap_submit ("used", swap_used);
214 swap_submit ("free", swap_free);
215 swap_submit ("cached", swap_cached);
216 /* #endif KERNEL_LINUX */
218 #elif HAVE_LIBKSTAT
219 unsigned long long swap_alloc;
220 unsigned long long swap_resv;
221 unsigned long long swap_avail;
223 struct anoninfo ai;
225 if (swapctl (SC_AINFO, &ai) == -1)
226 {
227 char errbuf[1024];
228 ERROR ("swap plugin: swapctl failed: %s",
229 sstrerror (errno, errbuf, sizeof (errbuf)));
230 return (-1);
231 }
233 /*
234 * Calculations from:
235 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
236 * Also see:
237 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
238 * /usr/include/vm/anon.h
239 *
240 * In short, swap -s shows: allocated + reserved = used, available
241 *
242 * However, Solaris does not allow to allocated/reserved more than the
243 * available swap (physical memory + disk swap), so the pedant may
244 * prefer: allocated + unallocated = reserved, available
245 *
246 * We map the above to: used + resv = n/a, free
247 *
248 * Does your brain hurt yet? - Christophe Kalt
249 *
250 * Oh, and in case you wonder,
251 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
252 * can suffer from a 32bit overflow.
253 */
254 swap_alloc = ai.ani_max - ai.ani_free;
255 swap_alloc *= pagesize;
256 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
257 swap_resv *= pagesize;
258 swap_avail = ai.ani_max - ai.ani_resv;
259 swap_avail *= pagesize;
261 swap_submit ("used", swap_alloc);
262 swap_submit ("free", swap_avail);
263 swap_submit ("reserved", swap_resv);
264 /* #endif HAVE_LIBKSTAT */
266 #elif HAVE_SWAPCTL
267 struct swapent *swap_entries;
268 int swap_num;
269 int status;
270 int i;
272 uint64_t used = 0;
273 uint64_t total = 0;
275 /*
276 * XXX: This is the syntax for the *BSD `swapctl', which has the
277 * following prototype:
278 * swapctl (int cmd, void *arg, int misc);
279 *
280 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
281 * too, but with the following prototype:
282 * swapctl (int cmd, void *arg);
283 *
284 * Solaris is usually handled in the KSTAT case above. For other UNIXes
285 * a separate case for the other version of `swapctl' may be necessary.
286 */
287 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
288 if (swap_num < 0)
289 {
290 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
291 swap_num);
292 return (-1);
293 }
294 else if (swap_num == 0)
295 return (0);
297 swap_entries = calloc (swap_num, sizeof (*swap_entries));
298 if (swap_entries == NULL)
299 {
300 ERROR ("swap plugin: calloc failed.");
301 return (-1);
302 }
304 status = swapctl (SWAP_STATS, swap_entries, swap_num);
305 if (status != swap_num)
306 {
307 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
308 status);
309 sfree (swap_entries);
310 return (-1);
311 }
313 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
314 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
315 #else
316 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
317 #endif
319 for (i = 0; i < swap_num; i++)
320 {
321 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
322 continue;
324 used += ((uint64_t) swap_entries[i].se_inuse)
325 * C_SWAP_BLOCK_SIZE;
326 total += ((uint64_t) swap_entries[i].se_nblks)
327 * C_SWAP_BLOCK_SIZE;
328 }
330 if (total < used)
331 {
332 ERROR ("swap plugin: Total swap space (%"PRIu64") "
333 "is less than used swap space (%"PRIu64").",
334 total, used);
335 return (-1);
336 }
338 swap_submit ("used", (gauge_t) used);
339 swap_submit ("free", (gauge_t) (total - used));
341 sfree (swap_entries);
342 /* #endif HAVE_SWAPCTL */
344 #elif defined(VM_SWAPUSAGE)
345 int mib[3];
346 size_t mib_len;
347 struct xsw_usage sw_usage;
348 size_t sw_usage_len;
350 mib_len = 2;
351 mib[0] = CTL_VM;
352 mib[1] = VM_SWAPUSAGE;
354 sw_usage_len = sizeof (struct xsw_usage);
356 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
357 return (-1);
359 /* The returned values are bytes. */
360 swap_submit ("used", sw_usage.xsu_used);
361 swap_submit ("free", sw_usage.xsu_avail);
362 /* #endif VM_SWAPUSAGE */
364 #elif HAVE_LIBKVM_GETSWAPINFO
365 struct kvm_swap data_s;
366 int status;
368 unsigned long long used;
369 unsigned long long free;
370 unsigned long long total;
372 if (kvm_obj == NULL)
373 return (-1);
375 /* only one structure => only get the grand total, no details */
376 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
377 if (status == -1)
378 return (-1);
380 total = (unsigned long long) data_s.ksw_total;
381 used = (unsigned long long) data_s.ksw_used;
383 total *= (unsigned long long) kvm_pagesize;
384 used *= (unsigned long long) kvm_pagesize;
386 free = total - used;
388 swap_submit ("used", used);
389 swap_submit ("free", free);
390 /* #endif HAVE_LIBKVM_GETSWAPINFO */
392 #elif HAVE_LIBSTATGRAB
393 sg_swap_stats *swap;
395 swap = sg_get_swap_stats ();
397 if (swap == NULL)
398 return (-1);
400 swap_submit ("used", swap->used);
401 swap_submit ("free", swap->free);
402 #endif /* HAVE_LIBSTATGRAB */
404 return (0);
405 } /* int swap_read */
407 void module_register (void)
408 {
409 plugin_register_init ("swap", swap_init);
410 plugin_register_read ("swap", swap_read);
411 } /* void module_register */