c31b30efe29b109cf3ae7c73055a48502c5b21fb
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005-2007  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; only version 2 of the License is applicable.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #ifdef HAVE_SYS_SYSCTL_H
27 # include <sys/sysctl.h>
28 #endif
29
30 #ifdef HAVE_MACH_KERN_RETURN_H
31 # include <mach/kern_return.h>
32 #endif
33 #ifdef HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 #endif
36 #ifdef HAVE_MACH_MACH_HOST_H
37 # include <mach/mach_host.h>
38 #endif
39 #ifdef HAVE_MACH_HOST_PRIV_H
40 # include <mach/host_priv.h>
41 #endif
42 #ifdef HAVE_MACH_VM_STATISTICS_H
43 # include <mach/vm_statistics.h>
44 #endif
45
46 #if HAVE_STATGRAB_H
47 # include <statgrab.h>
48 #endif
49
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 */
55
56 #elif HAVE_SYSCTLBYNAME
57 /* no global variables */
58 /* #endif HAVE_SYSCTLBYNAME */
59
60 #elif KERNEL_LINUX
61 /* no global variables */
62 /* #endif KERNEL_LINUX */
63
64 #elif HAVE_LIBKSTAT
65 static int pagesize;
66 static kstat_t *ksp;
67 /* #endif HAVE_LIBKSTAT */
68
69 #elif HAVE_LIBSTATGRAB
70 /* no global variables */
71 /* endif HAVE_LIBSTATGRAB */
72
73 #else
74 # error "No applicable input method."
75 #endif
76
77 static int memory_init (void)
78 {
79 #if HAVE_HOST_STATISTICS
80         port_host = mach_host_self ();
81         host_page_size (port_host, &pagesize);
82 /* #endif HAVE_HOST_STATISTICS */
83
84 #elif HAVE_SYSCTLBYNAME
85 /* no init stuff */
86 /* #endif HAVE_SYSCTLBYNAME */
87
88 #elif defined(KERNEL_LINUX)
89 /* no init stuff */
90 /* #endif KERNEL_LINUX */
91
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"))
96                 ksp = NULL;
97 #endif /* HAVE_LIBKSTAT */
98
99         return (0);
100 } /* int memory_init */
101
102 static void memory_submit (const char *type_instance, gauge_t value)
103 {
104         value_t values[1];
105         value_list_t vl = VALUE_LIST_INIT;
106
107         values[0].gauge = value;
108
109         vl.values = values;
110         vl.values_len = 1;
111         vl.time = time (NULL);
112         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
113         sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
114         sstrncpy (vl.type, "memory", sizeof (vl.type));
115         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
116
117         plugin_dispatch_values (&vl);
118 }
119
120 static int memory_read (void)
121 {
122 #if HAVE_HOST_STATISTICS
123         kern_return_t status;
124         vm_statistics_data_t   vm_data;
125         mach_msg_type_number_t vm_data_len;
126
127         long long wired;
128         long long active;
129         long long inactive;
130         long long free;
131
132         if (!port_host || !pagesize)
133                 return (-1);
134
135         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
136         if ((status = host_statistics (port_host, HOST_VM_INFO,
137                                         (host_info_t) &vm_data,
138                                         &vm_data_len)) != KERN_SUCCESS)
139         {
140                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
141                 return (-1);
142         }
143
144         /*
145          * From <http://docs.info.apple.com/article.html?artnum=107918>:
146          *
147          * Wired memory
148          *   This information can't be cached to disk, so it must stay in RAM.
149          *   The amount depends on what applications you are using.
150          *
151          * Active memory
152          *   This information is currently in RAM and actively being used.
153          *
154          * Inactive memory
155          *   This information is no longer being used and has been cached to
156          *   disk, but it will remain in RAM until another application needs
157          *   the space. Leaving this information in RAM is to your advantage if
158          *   you (or a client of your computer) come back to it later.
159          *
160          * Free memory
161          *   This memory is not being used.
162          */
163
164         wired    = vm_data.wire_count     * pagesize;
165         active   = vm_data.active_count   * pagesize;
166         inactive = vm_data.inactive_count * pagesize;
167         free     = vm_data.free_count     * pagesize;
168
169         memory_submit ("wired",    wired);
170         memory_submit ("active",   active);
171         memory_submit ("inactive", inactive);
172         memory_submit ("free",     free);
173 /* #endif HAVE_HOST_STATISTICS */
174
175 #elif HAVE_SYSCTLBYNAME
176         /*
177          * vm.stats.vm.v_page_size: 4096
178          * vm.stats.vm.v_page_count: 246178
179          * vm.stats.vm.v_free_count: 28760
180          * vm.stats.vm.v_wire_count: 37526
181          * vm.stats.vm.v_active_count: 55239
182          * vm.stats.vm.v_inactive_count: 113730
183          * vm.stats.vm.v_cache_count: 10809
184          */
185         char *sysctl_keys[8] =
186         {
187                 "vm.stats.vm.v_page_size",
188                 "vm.stats.vm.v_page_count",
189                 "vm.stats.vm.v_free_count",
190                 "vm.stats.vm.v_wire_count",
191                 "vm.stats.vm.v_active_count",
192                 "vm.stats.vm.v_inactive_count",
193                 "vm.stats.vm.v_cache_count",
194                 NULL
195         };
196         double sysctl_vals[8];
197
198         int    i;
199
200         for (i = 0; sysctl_keys[i] != NULL; i++)
201         {
202                 int value;
203                 size_t value_len = sizeof (value);
204
205                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
206                                         NULL, 0) == 0)
207                 {
208                         sysctl_vals[i] = value;
209                         DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
210                 }
211                 else
212                 {
213                         sysctl_vals[i] = NAN;
214                 }
215         } /* for (sysctl_keys) */
216
217         /* multiply all all page counts with the pagesize */
218         for (i = 1; sysctl_keys[i] != NULL; i++)
219                 if (!isnan (sysctl_vals[i]))
220                         sysctl_vals[i] *= sysctl_vals[0];
221
222         memory_submit ("free",     sysctl_vals[2]);
223         memory_submit ("wired",    sysctl_vals[3]);
224         memory_submit ("active",   sysctl_vals[4]);
225         memory_submit ("inactive", sysctl_vals[5]);
226         memory_submit ("cache",    sysctl_vals[6]);
227 /* #endif HAVE_SYSCTLBYNAME */
228
229 #elif KERNEL_LINUX
230         FILE *fh;
231         char buffer[1024];
232         
233         char *fields[8];
234         int numfields;
235
236         long long mem_used = 0;
237         long long mem_buffered = 0;
238         long long mem_cached = 0;
239         long long mem_free = 0;
240
241         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
242         {
243                 char errbuf[1024];
244                 WARNING ("memory: fopen: %s",
245                                 sstrerror (errno, errbuf, sizeof (errbuf)));
246                 return (-1);
247         }
248
249         while (fgets (buffer, 1024, fh) != NULL)
250         {
251                 long long *val = NULL;
252
253                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
254                         val = &mem_used;
255                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
256                         val = &mem_free;
257                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
258                         val = &mem_buffered;
259                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
260                         val = &mem_cached;
261                 else
262                         continue;
263
264                 numfields = strsplit (buffer, fields, 8);
265
266                 if (numfields < 2)
267                         continue;
268
269                 *val = atoll (fields[1]) * 1024LL;
270         }
271
272         if (fclose (fh))
273         {
274                 char errbuf[1024];
275                 WARNING ("memory: fclose: %s",
276                                 sstrerror (errno, errbuf, sizeof (errbuf)));
277         }
278
279         if (mem_used >= (mem_free + mem_buffered + mem_cached))
280         {
281                 mem_used -= mem_free + mem_buffered + mem_cached;
282                 memory_submit ("used",     mem_used);
283                 memory_submit ("buffered", mem_buffered);
284                 memory_submit ("cached",   mem_cached);
285                 memory_submit ("free",     mem_free);
286         }
287 /* #endif KERNEL_LINUX */
288
289 #elif HAVE_LIBKSTAT
290         long long mem_used;
291         long long mem_free;
292         long long mem_lock;
293
294         if (ksp == NULL)
295                 return (-1);
296
297         mem_used = get_kstat_value (ksp, "pagestotal");
298         mem_free = get_kstat_value (ksp, "pagesfree");
299         mem_lock = get_kstat_value (ksp, "pageslocked");
300
301         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
302                 return (-1);
303         if (mem_used < (mem_free + mem_lock))
304                 return (-1);
305
306         mem_used -= mem_free + mem_lock;
307         mem_used *= pagesize; /* If this overflows you have some serious */
308         mem_free *= pagesize; /* memory.. Why not call me up and give me */
309         mem_lock *= pagesize; /* some? ;) */
310
311         memory_submit ("used",   mem_used);
312         memory_submit ("free",   mem_free);
313         memory_submit ("locked", mem_lock);
314 /* #endif HAVE_LIBKSTAT */
315
316 #elif HAVE_LIBSTATGRAB
317         sg_mem_stats *ios;
318
319         if ((ios = sg_get_mem_stats ()) != NULL)
320         {
321                 memory_submit ("used",   ios->used);
322                 memory_submit ("cached", ios->cache);
323                 memory_submit ("free",   ios->free);
324         }
325 #endif /* HAVE_LIBSTATGRAB */
326
327         return (0);
328 }
329
330 void module_register (void)
331 {
332         plugin_register_init ("memory", memory_init);
333         plugin_register_read ("memory", memory_read);
334 } /* void module_register */