Code

freebsd branch: First attempt at porting the `memory' plugin to FreeBSD.
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005,2006  Florian octo Forster
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #ifdef HAVE_SYS_SYSCTL_H
28 # include <sys/sysctl.h>
29 #endif
31 #ifdef HAVE_MACH_KERN_RETURN_H
32 # include <mach/kern_return.h>
33 #endif
34 #ifdef HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
36 #endif
37 #ifdef HAVE_MACH_MACH_HOST_H
38 # include <mach/mach_host.h>
39 #endif
40 #ifdef HAVE_MACH_HOST_PRIV_H
41 # include <mach/host_priv.h>
42 #endif
43 #ifdef MACH_VM_STATISTICS_H
44 # include <mach/vm_statistics.h>
45 #endif
47 #if !defined(VM_TOTAL) && defined(VM_METER)
48 # define VM_TOTAL VM_METER
49 #endif
51 #if defined (HOST_VM_INFO) || defined(VM_TOTAL) || KERNEL_LINUX || HAVE_LIBKSTAT
52 # define MEMORY_HAVE_READ 1
53 #else
54 # define MEMORY_HAVE_READ 0
55 #endif
57 #define MODULE_NAME "memory"
59 static char *memory_file = "memory.rrd";
61 /* 9223372036854775807 == LLONG_MAX */
62 static char *ds_def[] =
63 {
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",
68         NULL
69 };
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 */
80 /* #endif VM_METER */
82 #elif KERNEL_LINUX
83 /* no global variables */
84 /* #endif KERNEL_LINUX */
86 #elif HAVE_LIBKSTAT
87 static int pagesize;
88 static kstat_t *ksp;
89 #endif /* HAVE_LIBKSTAT */
91 static void memory_init (void)
92 {
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)
99 /* no init stuff */
100 /* #endif VM_TOTAL */
102 #elif defined(KERNEL_LINUX)
103 /* no init stuff */
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"))
110                 ksp = NULL;
111 #endif /* HAVE_LIBKSTAT */
113         return;
116 static void memory_write (char *host, char *inst, char *val)
118         rrd_update_file (host, memory_file, val, ds_def, ds_num);
121 #if MEMORY_HAVE_READ
122 #define BUFSIZE 512
123 static void memory_submit (long long mem_used, long long mem_buffered,
124                 long long mem_cached, long long mem_free)
126         char buf[BUFSIZE];
128         if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
129                                 (unsigned int) curtime, mem_used, mem_free,
130                                 mem_buffered, mem_cached) >= BUFSIZE)
131                 return;
133         plugin_submit (MODULE_NAME, "-", buf);
135 #undef BUFSIZE
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;
144         long long wired;
145         long long active;
146         long long inactive;
147         long long free;
149         if (!port_host || !pagesize)
150                 return;
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)
156         {
157                 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
158                 return;
159         }
161         /*
162          * From <http://docs.info.apple.com/article.html?artnum=107918>:
163          *
164          * Wired memory
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.
167          *
168          * Active memory
169          *   This information is currently in RAM and actively being used.
170          *
171          * Inactive memory
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.
176          *
177          * Free memory
178          *   This memory is not being used.
179          */
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)
190         /*
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
198          */
199         int            page_size;
200         int            page_count;
201         int            free_count;
202         int            wire_count;
203         int            active_count;
204         int            inactive_count;
205         int            cache_count;
206         size_t         data_len;
208         int            status;
210         data_len = sizeof (page_size);
211         if ((status = sysctlbyname ("vm.stats.vm.v_page_size",
212                                         (void *) &page_size, data_len,
213                                         NULL, 0)) < 0)
214         {
215                 syslog (LOG_ERR, "memory plugin: sysctlbyname failed: %s",
216                                 strerror (status));
217                 return;
218         }
220         ...
222         status = sysctl (mib, sizeof (mib),
223                         (void *) &data, &data_len,
224                         NULL, 0); /* new value pointer */
225         if (status < 0)
226         {
227                 DBG ("sysctl failed: %s", strerror (errno));
228                 return;
229         }
231         if (data_len != sizeof (data))
232         {
233                 DBG ("data_len = %u; sizeof (data) = %u;",
234                                 (unsigned int) data_len,
235                                 (unsigned int) sizeof (data));
236                 return;
237         }
239 /* #endif VM_TOTAL */
241 #elif defined(KERNEL_LINUX)
242         FILE *fh;
243         char buffer[1024];
244         
245         char *fields[8];
246         int numfields;
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)
254         {
255                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
256                 return;
257         }
259         while (fgets (buffer, 1024, fh) != NULL)
260         {
261                 long long *val = NULL;
263                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
264                         val = &mem_used;
265                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
266                         val = &mem_free;
267                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
268                         val = &mem_buffered;
269                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
270                         val = &mem_cached;
271                 else
272                         continue;
274                 numfields = strsplit (buffer, fields, 8);
276                 if (numfields < 2)
277                         continue;
279                 *val = atoll (fields[1]) * 1024LL;
280         }
282         if (fclose (fh))
283                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
285         if (mem_used >= (mem_free + mem_buffered + mem_cached))
286         {
287                 mem_used -= mem_free + mem_buffered + mem_cached;
288                 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
289         }
290 /* #endif defined(KERNEL_LINUX) */
292 #elif defined(HAVE_LIBKSTAT)
293         long long mem_used;
294         long long mem_free;
295         long long mem_lock;
297         if (ksp == NULL)
298                 return;
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))
305                 return;
306         if (mem_used < (mem_free + mem_lock))
307                 return;
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)
318         sg_mem_stats *ios;
320         if ((ios = sg_get_mem_stats ()) != NULL)
321                 memory_submit (ios->used, 0LL, ios->cache, ios->free);
322 #endif /* HAVE_LIBSTATGRAB */
324 #else
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);
333 #undef MODULE_NAME