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>
37 #define MODULE_NAME "swap"
39 #if KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_SYS_SYSCTL_H || 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 char *swap_file = "swap.rrd";
50 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
51 static char *ds_def[] =
53 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
54 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
55 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
56 "DS:resv:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
59 static int ds_num = 4;
62 /* No global variables */
63 /* #endif KERNEL_LINUX */
68 /* #endif HAVE_LIBKSTAT */
70 #elif HAVE_SYS_SYSCTL_H
71 /* No global variables */
72 /* #endif HAVE_SYS_SYSCTL_H */
74 #elif HAVE_LIBSTATGRAB
75 /* No global variables */
76 #endif /* HAVE_LIBSTATGRAB */
78 static void swap_init (void)
82 /* #endif KERNEL_LINUX */
85 /* getpagesize(3C) tells me this does not fail.. */
86 pagesize = getpagesize ();
87 if (get_kstat (&ksp, "unix", 0, "system_pages"))
89 /* #endif HAVE_LIBKSTAT */
91 #elif HAVE_SYS_SYSCTL_H
93 /* #endif HAVE_SYS_SYSCTL_H */
95 #elif HAVE_LIBSTATGRAB
97 #endif /* HAVE_LIBSTATGRAB */
102 static void swap_write (char *host, char *inst, char *val)
104 rrd_update_file (host, swap_file, val, ds_def, ds_num);
108 static void swap_submit (unsigned long long swap_used,
109 unsigned long long swap_free,
110 unsigned long long swap_cached,
111 unsigned long long swap_resv)
115 if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
116 swap_used, swap_free, swap_cached, swap_resv) >= 512)
119 plugin_submit (MODULE_NAME, "-", buffer);
122 static void swap_read (void)
131 unsigned long long swap_used = 0LL;
132 unsigned long long swap_cached = 0LL;
133 unsigned long long swap_free = 0LL;
134 unsigned long long swap_total = 0LL;
136 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
138 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
142 while (fgets (buffer, 1024, fh) != NULL)
144 unsigned long long *val = NULL;
146 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
148 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
150 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
155 numfields = strsplit (buffer, fields, 8);
160 *val = atoll (fields[1]) * 1024LL;
164 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
166 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
169 swap_used = swap_total - (swap_free + swap_cached);
171 swap_submit (swap_used, swap_free, swap_cached, -1LL);
172 /* #endif KERNEL_LINUX */
175 unsigned long long swap_alloc;
176 unsigned long long swap_resv;
177 unsigned long long swap_avail;
178 /* unsigned long long swap_free; */
181 long long swapfs_minfree;
185 if (swapctl (SC_AINFO, &ai) == -1)
188 availrmem = get_kstat_value (ksp, "availrmem");
189 swapfs_minfree = get_kstat_value (ksp, "minfree");
191 if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
195 * Calculations learned by reading
196 * http://www.itworld.com/Comp/2377/UIR980701perf/
198 * swap_resv += ani_resv
199 * swap_alloc += MAX(ani_resv, ani_max) - ani_free
200 * swap_avail += MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
201 * swap_free += ani_free + (availrmem - swapfs_minfree)
203 * To clear up the terminology a bit:
204 * resv = reserved (but not neccessarily used)
205 * alloc = used (neccessarily reserved)
206 * avail = not reserved (neccessarily free)
207 * free = not allocates (possibly reserved)
209 swap_resv = pagesize * ai.ani_resv;
210 swap_alloc = pagesize * (MAX(ai.ani_resv, ai.ani_max) - ai.ani_free);
211 swap_avail = pagesize * (MAX(ai.ani_max - ai.ani_resv, 0) + (availrmem - swapfs_minfree));
212 /* swap_free = pagesize * (ai.ani_free + (availrmem - swapfs_minfree)); */
214 swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
215 /* #endif HAVE_LIBKSTAT */
217 #elif HAVE_SYS_SYSCTL_H
219 struct xsw_usage sw_usage;
223 mib[1] = VM_SWAPUSAGE;
225 sw_usage_len = sizeof (struct xsw_usage);
227 if (sysctl (mib, 2, &sw_usage, &sw_usage_len, NULL, 0) != 0)
230 /* The returned values are bytes. */
231 swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
232 /* #endif HAVE_SYS_SYSCTL_H */
234 #elif HAVE_LIBSTATGRAB
237 if ((swap = sg_get_swap_stats ()) != NULL)
238 swap_submit (swap->used, swap->free, -1LL, -1LL);
239 #endif /* HAVE_LIBSTATGRAB */
242 # define swap_read NULL
243 #endif /* SWAP_HAVE_READ */
245 void module_register (void)
247 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);