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>
47 # include <statgrab.h>
50 /* vm_statistics_data_t */
51 #if HAVE_HOST_STATISTICS
52 static mach_port_t port_host;
53 static vm_size_t pagesize;
54 /* #endif HAVE_HOST_STATISTICS */
56 #elif HAVE_SYSCTLBYNAME
57 /* no global variables */
58 /* #endif HAVE_SYSCTLBYNAME */
61 /* no global variables */
62 /* #endif KERNEL_LINUX */
67 /* #endif HAVE_LIBKSTAT */
69 #elif HAVE_LIBSTATGRAB
70 /* no global variables */
71 /* endif HAVE_LIBSTATGRAB */
74 # error "No applicable input method."
77 static int memory_init (void)
79 #if HAVE_HOST_STATISTICS
80 port_host = mach_host_self ();
81 host_page_size (port_host, &pagesize);
82 /* #endif HAVE_HOST_STATISTICS */
84 #elif HAVE_SYSCTLBYNAME
86 /* #endif HAVE_SYSCTLBYNAME */
88 #elif defined(KERNEL_LINUX)
90 /* #endif KERNEL_LINUX */
92 #elif defined(HAVE_LIBKSTAT)
93 /* getpagesize(3C) tells me this does not fail.. */
94 pagesize = getpagesize ();
95 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
100 #endif /* HAVE_LIBKSTAT */
103 } /* int memory_init */
105 static void memory_submit (const char *type_instance, gauge_t value)
108 value_list_t vl = VALUE_LIST_INIT;
110 values[0].gauge = value;
114 vl.time = time (NULL);
115 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
116 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
117 sstrncpy (vl.type, "memory", sizeof (vl.type));
118 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
120 plugin_dispatch_values (&vl);
123 static int memory_read (void)
125 #if HAVE_HOST_STATISTICS
126 kern_return_t status;
127 vm_statistics_data_t vm_data;
128 mach_msg_type_number_t vm_data_len;
135 if (!port_host || !pagesize)
138 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
139 if ((status = host_statistics (port_host, HOST_VM_INFO,
140 (host_info_t) &vm_data,
141 &vm_data_len)) != KERN_SUCCESS)
143 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
148 * From <http://docs.info.apple.com/article.html?artnum=107918>:
151 * This information can't be cached to disk, so it must stay in RAM.
152 * The amount depends on what applications you are using.
155 * This information is currently in RAM and actively being used.
158 * This information is no longer being used and has been cached to
159 * disk, but it will remain in RAM until another application needs
160 * the space. Leaving this information in RAM is to your advantage if
161 * you (or a client of your computer) come back to it later.
164 * This memory is not being used.
167 wired = vm_data.wire_count * pagesize;
168 active = vm_data.active_count * pagesize;
169 inactive = vm_data.inactive_count * pagesize;
170 free = vm_data.free_count * pagesize;
172 memory_submit ("wired", wired);
173 memory_submit ("active", active);
174 memory_submit ("inactive", inactive);
175 memory_submit ("free", free);
176 /* #endif HAVE_HOST_STATISTICS */
178 #elif HAVE_SYSCTLBYNAME
180 * vm.stats.vm.v_page_size: 4096
181 * vm.stats.vm.v_page_count: 246178
182 * vm.stats.vm.v_free_count: 28760
183 * vm.stats.vm.v_wire_count: 37526
184 * vm.stats.vm.v_active_count: 55239
185 * vm.stats.vm.v_inactive_count: 113730
186 * vm.stats.vm.v_cache_count: 10809
188 char *sysctl_keys[8] =
190 "vm.stats.vm.v_page_size",
191 "vm.stats.vm.v_page_count",
192 "vm.stats.vm.v_free_count",
193 "vm.stats.vm.v_wire_count",
194 "vm.stats.vm.v_active_count",
195 "vm.stats.vm.v_inactive_count",
196 "vm.stats.vm.v_cache_count",
199 double sysctl_vals[8];
203 for (i = 0; sysctl_keys[i] != NULL; i++)
206 size_t value_len = sizeof (value);
208 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
211 sysctl_vals[i] = value;
212 DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
216 sysctl_vals[i] = NAN;
218 } /* for (sysctl_keys) */
220 /* multiply all all page counts with the pagesize */
221 for (i = 1; sysctl_keys[i] != NULL; i++)
222 if (!isnan (sysctl_vals[i]))
223 sysctl_vals[i] *= sysctl_vals[0];
225 memory_submit ("free", sysctl_vals[2]);
226 memory_submit ("wired", sysctl_vals[3]);
227 memory_submit ("active", sysctl_vals[4]);
228 memory_submit ("inactive", sysctl_vals[5]);
229 memory_submit ("cache", sysctl_vals[6]);
230 /* #endif HAVE_SYSCTLBYNAME */
239 long long mem_used = 0;
240 long long mem_buffered = 0;
241 long long mem_cached = 0;
242 long long mem_free = 0;
244 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
247 WARNING ("memory: fopen: %s",
248 sstrerror (errno, errbuf, sizeof (errbuf)));
252 while (fgets (buffer, 1024, fh) != NULL)
254 long long *val = NULL;
256 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
258 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
260 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
262 else if (strncasecmp (buffer, "Cached:", 7) == 0)
267 numfields = strsplit (buffer, fields, 8);
272 *val = atoll (fields[1]) * 1024LL;
278 WARNING ("memory: fclose: %s",
279 sstrerror (errno, errbuf, sizeof (errbuf)));
282 if (mem_used >= (mem_free + mem_buffered + mem_cached))
284 mem_used -= mem_free + mem_buffered + mem_cached;
285 memory_submit ("used", mem_used);
286 memory_submit ("buffered", mem_buffered);
287 memory_submit ("cached", mem_cached);
288 memory_submit ("free", mem_free);
290 /* #endif KERNEL_LINUX */
300 mem_used = get_kstat_value (ksp, "pagestotal");
301 mem_free = get_kstat_value (ksp, "pagesfree");
302 mem_lock = get_kstat_value (ksp, "pageslocked");
304 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
306 if (mem_used < (mem_free + mem_lock))
309 mem_used -= mem_free + mem_lock;
310 mem_used *= pagesize; /* If this overflows you have some serious */
311 mem_free *= pagesize; /* memory.. Why not call me up and give me */
312 mem_lock *= pagesize; /* some? ;) */
314 memory_submit ("used", mem_used);
315 memory_submit ("free", mem_free);
316 memory_submit ("locked", mem_lock);
317 /* #endif HAVE_LIBKSTAT */
319 #elif HAVE_LIBSTATGRAB
322 if ((ios = sg_get_mem_stats ()) != NULL)
324 memory_submit ("used", ios->used);
325 memory_submit ("cached", ios->cache);
326 memory_submit ("free", ios->free);
328 #endif /* HAVE_LIBSTATGRAB */
333 void module_register (void)
335 plugin_register_init ("memory", memory_init);
336 plugin_register_read ("memory", memory_read);
337 } /* void module_register */