2 * collectd - src/swap.c
3 * Copyright (C) 2005,2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
28 # include <sys/swap.h>
31 # include <sys/param.h>
34 # include <sys/sysctl.h>
40 #define MODULE_NAME "swap"
42 #if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
43 # define SWAP_HAVE_READ 1
45 # define SWAP_HAVE_READ 0
49 #define MAX(x,y) ((x) > (y) ? (x) : (y))
51 static char *swap_file = "swap.rrd";
53 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
54 static char *ds_def[] =
56 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
57 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
58 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
59 "DS:resv:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
62 static int ds_num = 4;
65 /* No global variables */
66 /* #endif KERNEL_LINUX */
71 /* #endif HAVE_LIBKSTAT */
73 #elif defined(VM_SWAPUSAGE)
74 /* No global variables */
75 /* #endif defined(VM_SWAPUSAGE) */
78 static kvm_t *kvm_obj = NULL;
80 /* #endif HAVE_LIBKVM */
82 #elif HAVE_LIBSTATGRAB
83 /* No global variables */
84 #endif /* HAVE_LIBSTATGRAB */
86 static void swap_init (void)
90 /* #endif KERNEL_LINUX */
93 /* getpagesize(3C) tells me this does not fail.. */
94 pagesize = getpagesize ();
95 if (get_kstat (&ksp, "unix", 0, "system_pages"))
97 /* #endif HAVE_LIBKSTAT */
99 #elif defined(VM_SWAPUSAGE)
101 /* #endif defined(VM_SWAPUSAGE) */
110 kvm_pagesize = getpagesize ();
112 if ((kvm_obj = kvm_open (NULL, /* execfile */
115 O_RDONLY, /* flags */
119 syslog (LOG_ERR, "swap plugin: kvm_open failed.");
122 /* #endif HAVE_LIBKVM */
124 #elif HAVE_LIBSTATGRAB
126 #endif /* HAVE_LIBSTATGRAB */
131 static void swap_write (char *host, char *inst, char *val)
133 rrd_update_file (host, swap_file, val, ds_def, ds_num);
137 static void swap_submit (unsigned long long swap_used,
138 unsigned long long swap_free,
139 unsigned long long swap_cached,
140 unsigned long long swap_resv)
144 if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
145 swap_used, swap_free, swap_cached, swap_resv) >= 512)
148 plugin_submit (MODULE_NAME, "-", buffer);
151 static void swap_read (void)
160 unsigned long long swap_used = 0LL;
161 unsigned long long swap_cached = 0LL;
162 unsigned long long swap_free = 0LL;
163 unsigned long long swap_total = 0LL;
165 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
167 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
171 while (fgets (buffer, 1024, fh) != NULL)
173 unsigned long long *val = NULL;
175 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
177 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
179 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
184 numfields = strsplit (buffer, fields, 8);
189 *val = atoll (fields[1]) * 1024LL;
193 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
195 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
198 swap_used = swap_total - (swap_free + swap_cached);
200 swap_submit (swap_used, swap_free, swap_cached, -1LL);
201 /* #endif KERNEL_LINUX */
204 unsigned long long swap_alloc;
205 unsigned long long swap_resv;
206 unsigned long long swap_avail;
207 /* unsigned long long swap_free; */
210 long long swapfs_minfree;
214 if (swapctl (SC_AINFO, &ai) == -1)
217 availrmem = get_kstat_value (ksp, "availrmem");
218 swapfs_minfree = get_kstat_value (ksp, "minfree");
220 if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
224 * Calculations learned by reading
225 * http://www.itworld.com/Comp/2377/UIR980701perf/
227 * swap_resv += ani_resv
228 * swap_alloc += MAX(ani_resv, ani_max) - ani_free
229 * swap_avail += MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
230 * swap_free += ani_free + (availrmem - swapfs_minfree)
232 * To clear up the terminology a bit:
233 * resv = reserved (but not neccessarily used)
234 * alloc = used (neccessarily reserved)
235 * avail = not reserved (neccessarily free)
236 * free = not allocates (possibly reserved)
238 swap_resv = pagesize * ai.ani_resv;
239 swap_alloc = pagesize * (MAX(ai.ani_resv, ai.ani_max) - ai.ani_free);
240 swap_avail = pagesize * (MAX(ai.ani_max - ai.ani_resv, 0) + (availrmem - swapfs_minfree));
241 /* swap_free = pagesize * (ai.ani_free + (availrmem - swapfs_minfree)); */
243 swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
244 /* #endif HAVE_LIBKSTAT */
246 #elif defined(VM_SWAPUSAGE)
249 struct xsw_usage sw_usage;
255 mib[1] = VM_SWAPUSAGE;
257 sw_usage_len = sizeof (struct xsw_usage);
259 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
262 /* The returned values are bytes. */
263 swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
264 /* #endif VM_SWAPUSAGE */
267 struct kvm_swap data_s;
270 unsigned long long used;
271 unsigned long long free;
272 unsigned long long total;
277 /* only one structure => only get the grand total, no details */
278 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
282 total = (unsigned long long) data_s.ksw_total;
283 used = (unsigned long long) data_s.ksw_used;
285 total *= (unsigned long long) kvm_pagesize;
286 used *= (unsigned long long) kvm_pagesize;
290 swap_submit (used, free, -1LL, -1LL);
291 /* #endif HAVE_LIBKVM */
293 #elif HAVE_LIBSTATGRAB
296 if ((swap = sg_get_swap_stats ()) != NULL)
297 swap_submit (swap->used, swap->free, -1LL, -1LL);
298 #endif /* HAVE_LIBSTATGRAB */
301 # define swap_read NULL
302 #endif /* SWAP_HAVE_READ */
304 void module_register (void)
306 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);