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