freebsd branch: memory plugin: Quick bugfix. Works under FreeBSD now.
[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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #ifdef HAVE_SYS_SYSCTL_H
28 # include <sys/sysctl.h>
29 #endif
30
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
46
47 #if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
48 # define MEMORY_HAVE_READ 1
49 #else
50 # define MEMORY_HAVE_READ 0
51 #endif
52
53 #define MODULE_NAME "memory"
54
55 static char *memory_file = "memory.rrd";
56
57 /* 9223372036854775807 == LLONG_MAX */
58 static char *ds_def[] =
59 {
60         "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
61         "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
62         "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
63         "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
64         NULL
65 };
66 static int ds_num = 4;
67
68 /* vm_statistics_data_t */
69 #if defined(HOST_VM_INFO)
70 static mach_port_t port_host;
71 static vm_size_t pagesize;
72 /* #endif HOST_VM_INFO */
73
74 #elif HAVE_SYSCTLBYNAME
75 /* no global variables */
76 /* #endif HAVE_SYSCTLBYNAME */
77
78 #elif KERNEL_LINUX
79 /* no global variables */
80 /* #endif KERNEL_LINUX */
81
82 #elif HAVE_LIBKSTAT
83 static int pagesize;
84 static kstat_t *ksp;
85 #endif /* HAVE_LIBKSTAT */
86
87 static void memory_init (void)
88 {
89 #if defined(HOST_VM_INFO)
90         port_host = mach_host_self ();
91         host_page_size (port_host, &pagesize);
92 /* #endif HOST_VM_INFO */
93
94 #elif HAVE_SYSCTLBYNAME
95 /* no init stuff */
96 /* #endif HAVE_SYSCTLBYNAME */
97
98 #elif defined(KERNEL_LINUX)
99 /* no init stuff */
100 /* #endif KERNEL_LINUX */
101
102 #elif defined(HAVE_LIBKSTAT)
103         /* getpagesize(3C) tells me this does not fail.. */
104         pagesize = getpagesize ();
105         if (get_kstat (&ksp, "unix", 0, "system_pages"))
106                 ksp = NULL;
107 #endif /* HAVE_LIBKSTAT */
108
109         return;
110 }
111
112 static void memory_write (char *host, char *inst, char *val)
113 {
114         rrd_update_file (host, memory_file, val, ds_def, ds_num);
115 }
116
117 #if MEMORY_HAVE_READ
118 #define BUFSIZE 512
119 static void memory_submit (long long mem_used, long long mem_buffered,
120                 long long mem_cached, long long mem_free)
121 {
122         char buf[BUFSIZE];
123
124         if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
125                                 (unsigned int) curtime, mem_used, mem_free,
126                                 mem_buffered, mem_cached) >= BUFSIZE)
127                 return;
128
129         plugin_submit (MODULE_NAME, "-", buf);
130 }
131 #undef BUFSIZE
132
133 static void memory_read (void)
134 {
135 #if defined(HOST_VM_INFO)
136         kern_return_t status;
137         vm_statistics_data_t   vm_data;
138         mach_msg_type_number_t vm_data_len;
139
140         long long wired;
141         long long active;
142         long long inactive;
143         long long free;
144
145         if (!port_host || !pagesize)
146                 return;
147
148         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
149         if ((status = host_statistics (port_host, HOST_VM_INFO,
150                                         (host_info_t) &vm_data,
151                                         &vm_data_len)) != KERN_SUCCESS)
152         {
153                 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
154                 return;
155         }
156
157         /*
158          * From <http://docs.info.apple.com/article.html?artnum=107918>:
159          *
160          * Wired memory
161          *   This information can't be cached to disk, so it must stay in RAM.
162          *   The amount depends on what applications you are using.
163          *
164          * Active memory
165          *   This information is currently in RAM and actively being used.
166          *
167          * Inactive memory
168          *   This information is no longer being used and has been cached to
169          *   disk, but it will remain in RAM until another application needs
170          *   the space. Leaving this information in RAM is to your advantage if
171          *   you (or a client of your computer) come back to it later.
172          *
173          * Free memory
174          *   This memory is not being used.
175          */
176
177         wired    = vm_data.wire_count     * pagesize;
178         active   = vm_data.active_count   * pagesize;
179         inactive = vm_data.inactive_count * pagesize;
180         free     = vm_data.free_count     * pagesize;
181
182         memory_submit (wired + active, -1, inactive, free);
183 /* #endif HOST_VM_INFO */
184
185 #elif HAVE_SYSCTLBYNAME
186         /*
187          * vm.stats.vm.v_page_size: 4096
188          * vm.stats.vm.v_page_count: 246178
189          * vm.stats.vm.v_free_count: 28760
190          * vm.stats.vm.v_wire_count: 37526
191          * vm.stats.vm.v_active_count: 55239
192          * vm.stats.vm.v_inactive_count: 113730
193          * vm.stats.vm.v_cache_count: 10809
194          */
195         char *sysctl_keys[8] =
196         {
197                 "vm.stats.vm.v_page_size",
198                 "vm.stats.vm.v_page_count",
199                 "vm.stats.vm.v_free_count",
200                 "vm.stats.vm.v_wire_count",
201                 "vm.stats.vm.v_active_count",
202                 "vm.stats.vm.v_inactive_count",
203                 "vm.stats.vm.v_cache_count",
204                 NULL
205         };
206         int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
207
208         size_t len;
209         int    i;
210         int    status;
211
212         for (i = 0; sysctl_keys[i] != NULL; i++)
213         {
214                 len = sizeof (int);
215                 if ((status = sysctlbyname (sysctl_keys[i],
216                                                 (void *) &sysctl_vals[i], &len,
217                                                 NULL, 0)) < 0)
218                 {
219                         syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s",
220                                         sysctl_keys[i], strerror (status));
221                         return;
222                 }
223                 DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
224         } /* for i */
225
226         /* multiply all all page counts with the pagesize */
227         for (i = 1; sysctl_keys[i] != NULL; i++)
228                 sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
229
230         memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
231                         sysctl_vals[6],                 /* cache */
232                         sysctl_vals[5],                 /* inactive */
233                         sysctl_vals[2]);                /* free */
234 /* #endif HAVE_SYSCTLBYNAME */
235
236 #elif defined(KERNEL_LINUX)
237         FILE *fh;
238         char buffer[1024];
239         
240         char *fields[8];
241         int numfields;
242
243         long long mem_used = 0;
244         long long mem_buffered = 0;
245         long long mem_cached = 0;
246         long long mem_free = 0;
247
248         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
249         {
250                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
251                 return;
252         }
253
254         while (fgets (buffer, 1024, fh) != NULL)
255         {
256                 long long *val = NULL;
257
258                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
259                         val = &mem_used;
260                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
261                         val = &mem_free;
262                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
263                         val = &mem_buffered;
264                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
265                         val = &mem_cached;
266                 else
267                         continue;
268
269                 numfields = strsplit (buffer, fields, 8);
270
271                 if (numfields < 2)
272                         continue;
273
274                 *val = atoll (fields[1]) * 1024LL;
275         }
276
277         if (fclose (fh))
278                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
279
280         if (mem_used >= (mem_free + mem_buffered + mem_cached))
281         {
282                 mem_used -= mem_free + mem_buffered + mem_cached;
283                 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
284         }
285 /* #endif defined(KERNEL_LINUX) */
286
287 #elif defined(HAVE_LIBKSTAT)
288         long long mem_used;
289         long long mem_free;
290         long long mem_lock;
291
292         if (ksp == NULL)
293                 return;
294
295         mem_used = get_kstat_value (ksp, "pagestotal");
296         mem_free = get_kstat_value (ksp, "pagesfree");
297         mem_lock = get_kstat_value (ksp, "pageslocked");
298
299         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
300                 return;
301         if (mem_used < (mem_free + mem_lock))
302                 return;
303
304         mem_used -= mem_free + mem_lock;
305         mem_used *= pagesize; /* If this overflows you have some serious */
306         mem_free *= pagesize; /* memory.. Why not call me up and give me */
307         mem_lock *= pagesize; /* some? ;) */
308
309         memory_submit (mem_used, mem_lock, 0LL, mem_free);
310 /* #endif defined(HAVE_LIBKSTAT) */
311
312 #elif defined(HAVE_LIBSTATGRAB)
313         sg_mem_stats *ios;
314
315         if ((ios = sg_get_mem_stats ()) != NULL)
316                 memory_submit (ios->used, 0LL, ios->cache, ios->free);
317 #endif /* HAVE_LIBSTATGRAB */
318 }
319 #else
320 # define memory_read NULL
321 #endif /* MEMORY_HAVE_READ */
322
323 void module_register (void)
324 {
325         plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);
326 }
327
328 #undef MODULE_NAME