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