2 * collectd - src/memory.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>
27 #ifdef HAVE_SYS_SYSCTL_H
28 # include <sys/sysctl.h>
31 #ifdef HAVE_MACH_KERN_RETURN_H
32 # include <mach/kern_return.h>
34 #ifdef HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
37 #ifdef HAVE_MACH_MACH_HOST_H
38 # include <mach/mach_host.h>
40 #ifdef HAVE_MACH_HOST_PRIV_H
41 # include <mach/host_priv.h>
43 #ifdef MACH_VM_STATISTICS_H
44 # include <mach/vm_statistics.h>
47 #if !defined(VM_TOTAL) && defined(VM_METER)
48 # define VM_TOTAL VM_METER
51 #if defined (HOST_VM_INFO) || defined(VM_TOTAL) || KERNEL_LINUX || HAVE_LIBKSTAT
52 # define MEMORY_HAVE_READ 1
54 # define MEMORY_HAVE_READ 0
57 #define MODULE_NAME "memory"
59 static char *memory_file = "memory.rrd";
61 /* 9223372036854775807 == LLONG_MAX */
62 static char *ds_def[] =
64 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
65 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
66 "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
67 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
70 static int ds_num = 4;
72 /* vm_statistics_data_t */
73 #if defined(HOST_VM_INFO)
74 static mach_port_t port_host;
75 static vm_size_t pagesize;
76 /* #endif HOST_VM_INFO */
78 #elif defined(VM_TOTAL)
79 /* no global variables */
83 /* no global variables */
84 /* #endif KERNEL_LINUX */
89 #endif /* HAVE_LIBKSTAT */
91 static void memory_init (void)
93 #if defined(HOST_VM_INFO)
94 port_host = mach_host_self ();
95 host_page_size (port_host, &pagesize);
96 /* #endif HOST_VM_INFO */
98 #elif defined(VM_TOTAL)
100 /* #endif VM_TOTAL */
102 #elif defined(KERNEL_LINUX)
104 /* #endif KERNEL_LINUX */
106 #elif defined(HAVE_LIBKSTAT)
107 /* getpagesize(3C) tells me this does not fail.. */
108 pagesize = getpagesize ();
109 if (get_kstat (&ksp, "unix", 0, "system_pages"))
111 #endif /* HAVE_LIBKSTAT */
116 static void memory_write (char *host, char *inst, char *val)
118 rrd_update_file (host, memory_file, val, ds_def, ds_num);
123 static void memory_submit (long long mem_used, long long mem_buffered,
124 long long mem_cached, long long mem_free)
128 if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
129 (unsigned int) curtime, mem_used, mem_free,
130 mem_buffered, mem_cached) >= BUFSIZE)
133 plugin_submit (MODULE_NAME, "-", buf);
137 static void memory_read (void)
139 #if defined(HOST_VM_INFO)
140 kern_return_t status;
141 vm_statistics_data_t vm_data;
142 mach_msg_type_number_t vm_data_len;
149 if (!port_host || !pagesize)
152 vm_data_len = sizeof (vm_data) / sizeof (natural_t);
153 if ((status = host_statistics (port_host, HOST_VM_INFO,
154 (host_info_t) &vm_data,
155 &vm_data_len)) != KERN_SUCCESS)
157 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
162 * From <http://docs.info.apple.com/article.html?artnum=107918>:
165 * This information can't be cached to disk, so it must stay in RAM.
166 * The amount depends on what applications you are using.
169 * This information is currently in RAM and actively being used.
172 * This information is no longer being used and has been cached to
173 * disk, but it will remain in RAM until another application needs
174 * the space. Leaving this information in RAM is to your advantage if
175 * you (or a client of your computer) come back to it later.
178 * This memory is not being used.
181 wired = vm_data.wire_count * pagesize;
182 active = vm_data.active_count * pagesize;
183 inactive = vm_data.inactive_count * pagesize;
184 free = vm_data.free_count * pagesize;
186 memory_submit (wired + active, -1, inactive, free);
187 /* #endif HOST_VM_INFO */
189 #elif defined(VM_TOTAL)
191 * vm.stats.vm.v_page_size: 4096
192 * vm.stats.vm.v_page_count: 246178
193 * vm.stats.vm.v_free_count: 28760
194 * vm.stats.vm.v_wire_count: 37526
195 * vm.stats.vm.v_active_count: 55239
196 * vm.stats.vm.v_inactive_count: 113730
197 * vm.stats.vm.v_cache_count: 10809
210 data_len = sizeof (page_size);
211 if ((status = sysctlbyname ("vm.stats.vm.v_page_size",
212 (void *) &page_size, data_len,
215 syslog (LOG_ERR, "memory plugin: sysctlbyname failed: %s",
222 status = sysctl (mib, sizeof (mib),
223 (void *) &data, &data_len,
224 NULL, 0); /* new value pointer */
227 DBG ("sysctl failed: %s", strerror (errno));
231 if (data_len != sizeof (data))
233 DBG ("data_len = %u; sizeof (data) = %u;",
234 (unsigned int) data_len,
235 (unsigned int) sizeof (data));
239 /* #endif VM_TOTAL */
241 #elif defined(KERNEL_LINUX)
248 long long mem_used = 0;
249 long long mem_buffered = 0;
250 long long mem_cached = 0;
251 long long mem_free = 0;
253 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
255 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
259 while (fgets (buffer, 1024, fh) != NULL)
261 long long *val = NULL;
263 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
265 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
267 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
269 else if (strncasecmp (buffer, "Cached:", 7) == 0)
274 numfields = strsplit (buffer, fields, 8);
279 *val = atoll (fields[1]) * 1024LL;
283 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
285 if (mem_used >= (mem_free + mem_buffered + mem_cached))
287 mem_used -= mem_free + mem_buffered + mem_cached;
288 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
290 /* #endif defined(KERNEL_LINUX) */
292 #elif defined(HAVE_LIBKSTAT)
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 (mem_used, mem_lock, 0LL, mem_free);
315 /* #endif defined(HAVE_LIBKSTAT) */
317 #elif defined(HAVE_LIBSTATGRAB)
320 if ((ios = sg_get_mem_stats ()) != NULL)
321 memory_submit (ios->used, 0LL, ios->cache, ios->free);
322 #endif /* HAVE_LIBSTATGRAB */
325 # define memory_read NULL
326 #endif /* MEMORY_HAVE_READ */
328 void module_register (void)
330 plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);