2 * collectd - src/memory.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2009 Simon Kuhnle
5 * Copyright (C) 2009 Manuel Sanmartin
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Simon Kuhnle <simon at blarzwurst.de>
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
40 #ifdef HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
43 #ifdef HAVE_MACH_HOST_PRIV_H
44 # include <mach/host_priv.h>
46 #ifdef HAVE_MACH_VM_STATISTICS_H
47 # include <mach/vm_statistics.h>
51 # include <statgrab.h>
55 # include <sys/protosw.h>
56 # include <libperfstat.h>
57 #endif /* HAVE_PERFSTAT */
59 /* vm_statistics_data_t */
60 #if HAVE_HOST_STATISTICS
61 static mach_port_t port_host;
62 static vm_size_t pagesize;
63 /* #endif HAVE_HOST_STATISTICS */
65 #elif HAVE_SYSCTLBYNAME
66 /* no global variables */
67 /* #endif HAVE_SYSCTLBYNAME */
70 /* no global variables */
71 /* #endif KERNEL_LINUX */
76 /* #endif HAVE_LIBKSTAT */
80 /* #endif HAVE_SYSCTL */
82 #elif HAVE_LIBSTATGRAB
83 /* no global variables */
84 /* endif HAVE_LIBSTATGRAB */
87 static perfstat_memory_total_t pmemory;
88 /* endif HAVE_PERFSTAT */
90 # error "No applicable input method."
93 static int memory_init (void)
95 #if HAVE_HOST_STATISTICS
96 port_host = mach_host_self ();
97 host_page_size (port_host, &pagesize);
98 /* #endif HAVE_HOST_STATISTICS */
100 #elif HAVE_SYSCTLBYNAME
102 /* #endif HAVE_SYSCTLBYNAME */
104 #elif defined(KERNEL_LINUX)
106 /* #endif KERNEL_LINUX */
108 #elif defined(HAVE_LIBKSTAT)
109 /* getpagesize(3C) tells me this does not fail.. */
110 pagesize = getpagesize ();
111 if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
116 /* #endif HAVE_LIBKSTAT */
119 pagesize = getpagesize ();
122 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
125 /* #endif HAVE_SYSCTL */
127 #elif HAVE_LIBSTATGRAB
129 /* #endif HAVE_LIBSTATGRAB */
132 pagesize = getpagesize ();
133 #endif /* HAVE_PERFSTAT */
135 } /* int memory_init */
137 static void memory_submit (const char *type_instance, gauge_t value)
140 value_list_t vl = VALUE_LIST_INIT;
142 values[0].gauge = value;
146 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
147 sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
148 sstrncpy (vl.type, "memory", sizeof (vl.type));
149 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
151 plugin_dispatch_values (&vl);
154 static int memory_read (void)
156 #if HAVE_HOST_STATISTICS
157 kern_return_t status;
158 vm_statistics_data_t vm_data;
159 mach_msg_type_number_t vm_data_len;
166 if (!port_host || !pagesize)
169 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
170 if ((status = host_statistics (port_host, HOST_VM_INFO,
171 (host_info_t) &vm_data,
172 &vm_data_len)) != KERN_SUCCESS)
174 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
179 * From <http://docs.info.apple.com/article.html?artnum=107918>:
182 * This information can't be cached to disk, so it must stay in RAM.
183 * The amount depends on what applications you are using.
186 * This information is currently in RAM and actively being used.
189 * This information is no longer being used and has been cached to
190 * disk, but it will remain in RAM until another application needs
191 * the space. Leaving this information in RAM is to your advantage if
192 * you (or a client of your computer) come back to it later.
195 * This memory is not being used.
198 wired = (gauge_t) (((uint64_t) vm_data.wire_count) * ((uint64_t) pagesize));
199 active = (gauge_t) (((uint64_t) vm_data.active_count) * ((uint64_t) pagesize));
200 inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
201 free = (gauge_t) (((uint64_t) vm_data.free_count) * ((uint64_t) pagesize));
203 memory_submit ("wired", wired);
204 memory_submit ("active", active);
205 memory_submit ("inactive", inactive);
206 memory_submit ("free", free);
207 /* #endif HAVE_HOST_STATISTICS */
209 #elif HAVE_SYSCTLBYNAME
211 * vm.stats.vm.v_page_size: 4096
212 * vm.stats.vm.v_page_count: 246178
213 * vm.stats.vm.v_free_count: 28760
214 * vm.stats.vm.v_wire_count: 37526
215 * vm.stats.vm.v_active_count: 55239
216 * vm.stats.vm.v_inactive_count: 113730
217 * vm.stats.vm.v_cache_count: 10809
219 char *sysctl_keys[8] =
221 "vm.stats.vm.v_page_size",
222 "vm.stats.vm.v_page_count",
223 "vm.stats.vm.v_free_count",
224 "vm.stats.vm.v_wire_count",
225 "vm.stats.vm.v_active_count",
226 "vm.stats.vm.v_inactive_count",
227 "vm.stats.vm.v_cache_count",
230 double sysctl_vals[8];
234 for (i = 0; sysctl_keys[i] != NULL; i++)
237 size_t value_len = sizeof (value);
239 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
242 sysctl_vals[i] = value;
243 DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
247 sysctl_vals[i] = NAN;
249 } /* for (sysctl_keys) */
251 /* multiply all all page counts with the pagesize */
252 for (i = 1; sysctl_keys[i] != NULL; i++)
253 if (!isnan (sysctl_vals[i]))
254 sysctl_vals[i] *= sysctl_vals[0];
256 memory_submit ("free", sysctl_vals[2]);
257 memory_submit ("wired", sysctl_vals[3]);
258 memory_submit ("active", sysctl_vals[4]);
259 memory_submit ("inactive", sysctl_vals[5]);
260 memory_submit ("cache", sysctl_vals[6]);
261 /* #endif HAVE_SYSCTLBYNAME */
270 long long mem_used = 0;
271 long long mem_buffered = 0;
272 long long mem_cached = 0;
273 long long mem_free = 0;
275 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
278 WARNING ("memory: fopen: %s",
279 sstrerror (errno, errbuf, sizeof (errbuf)));
283 while (fgets (buffer, 1024, fh) != NULL)
285 long long *val = NULL;
287 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
289 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
291 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
293 else if (strncasecmp (buffer, "Cached:", 7) == 0)
298 numfields = strsplit (buffer, fields, 8);
303 *val = atoll (fields[1]) * 1024LL;
309 WARNING ("memory: fclose: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
313 if (mem_used >= (mem_free + mem_buffered + mem_cached))
315 mem_used -= mem_free + mem_buffered + mem_cached;
316 memory_submit ("used", mem_used);
317 memory_submit ("buffered", mem_buffered);
318 memory_submit ("cached", mem_cached);
319 memory_submit ("free", mem_free);
321 /* #endif KERNEL_LINUX */
324 /* Most of the additions here were taken as-is from the k9toolkit from
325 * Brendan Gregg and are subject to change I guess */
339 mem_used = get_kstat_value (ksp, "pagestotal");
340 mem_free = get_kstat_value (ksp, "pagesfree");
341 mem_lock = get_kstat_value (ksp, "pageslocked");
345 pp_kernel = get_kstat_value (ksp, "pp_kernel");
346 physmem = get_kstat_value (ksp, "physmem");
347 availrmem = get_kstat_value (ksp, "availrmem");
349 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
351 WARNING ("memory plugin: one of used, free or locked is negative.");
355 mem_unus = physmem - mem_used;
357 if (mem_used < (mem_free + mem_lock))
359 /* source: http://wesunsolve.net/bugid/id/4909199
360 * this seems to happen when swap space is small, e.g. 2G on a 32G system
361 * we will make some assumptions here
362 * educated solaris internals help welcome here */
363 DEBUG ("memory plugin: pages total is smaller than \"free\" "
364 "+ \"locked\". This is probably due to small "
366 mem_free = availrmem;
371 mem_used -= mem_free + mem_lock;
374 /* mem_kern is accounted for in mem_lock */
375 if ( pp_kernel < mem_lock )
377 mem_kern = pp_kernel;
378 mem_lock -= pp_kernel;
386 mem_used *= pagesize; /* If this overflows you have some serious */
387 mem_free *= pagesize; /* memory.. Why not call me up and give me */
388 mem_lock *= pagesize; /* some? ;) */
389 mem_kern *= pagesize; /* it's 2011 RAM is cheap */
390 mem_unus *= pagesize;
392 memory_submit ("used", mem_used);
393 memory_submit ("free", mem_free);
394 memory_submit ("locked", mem_lock);
395 memory_submit ("kernel", mem_kern);
396 memory_submit ("unusable", mem_unus);
397 /* #endif HAVE_LIBKSTAT */
400 int mib[] = {CTL_VM, VM_METER};
401 struct vmtotal vmtotal;
404 memset (&vmtotal, 0, sizeof (vmtotal));
405 size = sizeof (vmtotal);
407 if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
409 WARNING ("memory plugin: sysctl failed: %s",
410 sstrerror (errno, errbuf, sizeof (errbuf)));
414 assert (pagesize > 0);
415 memory_submit ("active", vmtotal.t_arm * pagesize);
416 memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
417 memory_submit ("free", vmtotal.t_free * pagesize);
418 /* #endif HAVE_SYSCTL */
420 #elif HAVE_LIBSTATGRAB
423 if ((ios = sg_get_mem_stats ()) != NULL)
425 memory_submit ("used", ios->used);
426 memory_submit ("cached", ios->cache);
427 memory_submit ("free", ios->free);
429 /* #endif HAVE_LIBSTATGRAB */
432 if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
435 WARNING ("memory plugin: perfstat_memory_total failed: %s",
436 sstrerror (errno, errbuf, sizeof (errbuf)));
439 memory_submit ("used", pmemory.real_inuse * pagesize);
440 memory_submit ("free", pmemory.real_free * pagesize);
441 memory_submit ("cached", pmemory.numperm * pagesize);
442 memory_submit ("system", pmemory.real_system * pagesize);
443 memory_submit ("user", pmemory.real_process * pagesize);
444 #endif /* HAVE_PERFSTAT */
449 void module_register (void)
451 plugin_register_init ("memory", memory_init);
452 plugin_register_read ("memory", memory_read);
453 } /* void module_register */