2 * collectd - src/memory.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>
26 #ifdef HAVE_SYS_SYSCTL_H
27 # include <sys/sysctl.h>
30 #ifdef HAVE_MACH_KERN_RETURN_H
31 # include <mach/kern_return.h>
33 #ifdef HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #ifdef HAVE_MACH_MACH_HOST_H
37 # include <mach/mach_host.h>
39 #ifdef HAVE_MACH_HOST_PRIV_H
40 # include <mach/host_priv.h>
42 #ifdef HAVE_MACH_VM_STATISTICS_H
43 # include <mach/vm_statistics.h>
46 #if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
47 # define MEMORY_HAVE_READ 1
49 # define MEMORY_HAVE_READ 0
52 /* 2^48 = 281474976710656 */
53 static data_source_t dsrc[4] =
55 {"used", DS_TYPE_GAUGE, 0, 281474976710656.0},
56 {"free", DS_TYPE_GAUGE, 0, 281474976710656.0},
57 {"buffers", DS_TYPE_GAUGE, 0, 281474976710656.0},
58 {"cached", DS_TYPE_GAUGE, 0, 281474976710656.0}
61 static data_set_t ds =
66 /* vm_statistics_data_t */
67 #if defined(HOST_VM_INFO)
68 static mach_port_t port_host;
69 static vm_size_t pagesize;
70 /* #endif HOST_VM_INFO */
72 #elif HAVE_SYSCTLBYNAME
73 /* no global variables */
74 /* #endif HAVE_SYSCTLBYNAME */
77 /* no global variables */
78 /* #endif KERNEL_LINUX */
83 #endif /* HAVE_LIBKSTAT */
86 static int memory_init (void)
88 #if defined(HOST_VM_INFO)
89 port_host = mach_host_self ();
90 host_page_size (port_host, &pagesize);
91 /* #endif HOST_VM_INFO */
93 #elif HAVE_SYSCTLBYNAME
95 /* #endif HAVE_SYSCTLBYNAME */
97 #elif defined(KERNEL_LINUX)
99 /* #endif KERNEL_LINUX */
101 #elif defined(HAVE_LIBKSTAT)
102 /* getpagesize(3C) tells me this does not fail.. */
103 pagesize = getpagesize ();
104 if (get_kstat (&ksp, "unix", 0, "system_pages"))
106 #endif /* HAVE_LIBKSTAT */
109 } /* int memory_init */
111 static void memory_submit (long long mem_used, long long mem_buffered,
112 long long mem_cached, long long mem_free)
115 value_list_t vl = VALUE_LIST_INIT;
117 values[0].gauge = mem_used;
118 values[1].gauge = mem_free;
119 values[2].gauge = mem_buffered;
120 values[3].gauge = mem_cached;
124 vl.time = time (NULL);
125 strcpy (vl.host, hostname_g);
126 strcpy (vl.plugin, "memory");
128 plugin_dispatch_values ("memory", &vl);
131 static int memory_read (void)
133 #if defined(HOST_VM_INFO)
134 kern_return_t status;
135 vm_statistics_data_t vm_data;
136 mach_msg_type_number_t vm_data_len;
143 if (!port_host || !pagesize)
146 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
147 if ((status = host_statistics (port_host, HOST_VM_INFO,
148 (host_info_t) &vm_data,
149 &vm_data_len)) != KERN_SUCCESS)
151 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
156 * From <http://docs.info.apple.com/article.html?artnum=107918>:
159 * This information can't be cached to disk, so it must stay in RAM.
160 * The amount depends on what applications you are using.
163 * This information is currently in RAM and actively being used.
166 * This information is no longer being used and has been cached to
167 * disk, but it will remain in RAM until another application needs
168 * the space. Leaving this information in RAM is to your advantage if
169 * you (or a client of your computer) come back to it later.
172 * This memory is not being used.
175 wired = vm_data.wire_count * pagesize;
176 active = vm_data.active_count * pagesize;
177 inactive = vm_data.inactive_count * pagesize;
178 free = vm_data.free_count * pagesize;
180 memory_submit (wired + active, -1, inactive, free);
181 /* #endif HOST_VM_INFO */
183 #elif HAVE_SYSCTLBYNAME
185 * vm.stats.vm.v_page_size: 4096
186 * vm.stats.vm.v_page_count: 246178
187 * vm.stats.vm.v_free_count: 28760
188 * vm.stats.vm.v_wire_count: 37526
189 * vm.stats.vm.v_active_count: 55239
190 * vm.stats.vm.v_inactive_count: 113730
191 * vm.stats.vm.v_cache_count: 10809
193 char *sysctl_keys[8] =
195 "vm.stats.vm.v_page_size",
196 "vm.stats.vm.v_page_count",
197 "vm.stats.vm.v_free_count",
198 "vm.stats.vm.v_wire_count",
199 "vm.stats.vm.v_active_count",
200 "vm.stats.vm.v_inactive_count",
201 "vm.stats.vm.v_cache_count",
204 int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
210 for (i = 0; sysctl_keys[i] != NULL; i++)
213 if ((status = sysctlbyname (sysctl_keys[i],
214 (void *) &sysctl_vals[i], &len,
218 ERROR ("memory plugin: sysctlbyname (%s): %s",
220 sstrerror (errno, errbuf,
224 DEBUG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
227 /* multiply all all page counts with the pagesize */
228 for (i = 1; sysctl_keys[i] != NULL; i++)
229 sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
231 memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
232 sysctl_vals[6], /* cache */
233 sysctl_vals[5], /* inactive */
234 sysctl_vals[2]); /* free */
235 /* #endif HAVE_SYSCTLBYNAME */
237 #elif defined(KERNEL_LINUX)
244 long long mem_used = 0;
245 long long mem_buffered = 0;
246 long long mem_cached = 0;
247 long long mem_free = 0;
249 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
252 WARNING ("memory: fopen: %s",
253 sstrerror (errno, errbuf, sizeof (errbuf)));
257 while (fgets (buffer, 1024, fh) != NULL)
259 long long *val = NULL;
261 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
263 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
265 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
267 else if (strncasecmp (buffer, "Cached:", 7) == 0)
272 numfields = strsplit (buffer, fields, 8);
277 *val = atoll (fields[1]) * 1024LL;
283 WARNING ("memory: fclose: %s",
284 sstrerror (errno, errbuf, sizeof (errbuf)));
287 if (mem_used >= (mem_free + mem_buffered + mem_cached))
289 mem_used -= mem_free + mem_buffered + mem_cached;
290 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
292 /* #endif defined(KERNEL_LINUX) */
294 #elif defined(HAVE_LIBKSTAT)
302 mem_used = get_kstat_value (ksp, "pagestotal");
303 mem_free = get_kstat_value (ksp, "pagesfree");
304 mem_lock = get_kstat_value (ksp, "pageslocked");
306 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
308 if (mem_used < (mem_free + mem_lock))
311 mem_used -= mem_free + mem_lock;
312 mem_used *= pagesize; /* If this overflows you have some serious */
313 mem_free *= pagesize; /* memory.. Why not call me up and give me */
314 mem_lock *= pagesize; /* some? ;) */
316 memory_submit (mem_used, mem_lock, 0LL, mem_free);
317 /* #endif defined(HAVE_LIBKSTAT) */
319 #elif defined(HAVE_LIBSTATGRAB)
322 if ((ios = sg_get_mem_stats ()) != NULL)
323 memory_submit (ios->used, 0LL, ios->cache, ios->free);
324 #endif /* HAVE_LIBSTATGRAB */
328 #endif /* MEMORY_HAVE_READ */
330 void module_register (void)
332 plugin_register_data_set (&ds);
335 plugin_register_init ("memory", memory_init);
336 plugin_register_read ("memory", memory_read);
337 #endif /* MEMORY_HAVE_READ */