Code

memory plugin: Update copyright notice and AUTHORS.
[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  **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
30 #endif
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
48 #if HAVE_STATGRAB_H
49 # include <statgrab.h>
50 #endif
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 */
58 #elif HAVE_SYSCTL
59 static int pagesize;
60 /* #endif HAVE_SYSCTL */
62 #elif HAVE_SYSCTLBYNAME
63 /* no global variables */
64 /* #endif HAVE_SYSCTLBYNAME */
66 #elif KERNEL_LINUX
67 /* no global variables */
68 /* #endif KERNEL_LINUX */
70 #elif HAVE_LIBKSTAT
71 static int pagesize;
72 static kstat_t *ksp;
73 /* #endif HAVE_LIBKSTAT */
75 #elif HAVE_LIBSTATGRAB
76 /* no global variables */
77 /* endif HAVE_LIBSTATGRAB */
79 #else
80 # error "No applicable input method."
81 #endif
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 */
90 #elif HAVE_SYSCTL
91         pagesize = getpagesize ();
92         if (pagesize <= 0)
93         {
94                 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
95                 return (-1);
96         }
97 /* #endif HAVE_SYSCTL */
99 #elif HAVE_SYSCTLBYNAME
100 /* no init stuff */
101 /* #endif HAVE_SYSCTLBYNAME */
103 #elif defined(KERNEL_LINUX)
104 /* no init stuff */
105 /* #endif KERNEL_LINUX */
107 #elif defined(HAVE_LIBKSTAT)
108         /* getpagesize(3C) tells me this does not fail.. */
109         pagesize = getpagesize ();
110         if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
111         {
112                 ksp = NULL;
113                 return (-1);
114         }
115 #endif /* HAVE_LIBKSTAT */
117         return (0);
118 } /* int memory_init */
120 static void memory_submit (const char *type_instance, gauge_t value)
122         value_t values[1];
123         value_list_t vl = VALUE_LIST_INIT;
125         values[0].gauge = value;
127         vl.values = values;
128         vl.values_len = 1;
129         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
130         sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
131         sstrncpy (vl.type, "memory", sizeof (vl.type));
132         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
134         plugin_dispatch_values (&vl);
137 static int memory_read (void)
139 #if HAVE_HOST_STATISTICS
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 (-1);
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                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
158                 return (-1);
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",    wired);
187         memory_submit ("active",   active);
188         memory_submit ("inactive", inactive);
189         memory_submit ("free",     free);
190 /* #endif HAVE_HOST_STATISTICS */
192 #elif HAVE_SYSCTL
193         int mib[] = {CTL_VM, VM_METER};
194         struct vmtotal vmtotal;
195         size_t size;
197         memset (&vmtotal, 0, sizeof (vmtotal));
198         size = sizeof (vmtotal);
200         if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
201                 char errbuf[1024];
202                 WARNING ("memory plugin: sysctl failed: %s",
203                         sstrerror (errno, errbuf, sizeof (errbuf)));
204                 return (-1);
205         }
207         assert (pagesize > 0);
208         memory_submit ("active",   vmtotal.t_arm * pagesize);
209         memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
210         memory_submit ("free",     vmtotal.t_free * pagesize);
211 /* #endif HAVE_SYSCTL */
213 #elif HAVE_SYSCTLBYNAME
214         /*
215          * vm.stats.vm.v_page_size: 4096
216          * vm.stats.vm.v_page_count: 246178
217          * vm.stats.vm.v_free_count: 28760
218          * vm.stats.vm.v_wire_count: 37526
219          * vm.stats.vm.v_active_count: 55239
220          * vm.stats.vm.v_inactive_count: 113730
221          * vm.stats.vm.v_cache_count: 10809
222          */
223         char *sysctl_keys[8] =
224         {
225                 "vm.stats.vm.v_page_size",
226                 "vm.stats.vm.v_page_count",
227                 "vm.stats.vm.v_free_count",
228                 "vm.stats.vm.v_wire_count",
229                 "vm.stats.vm.v_active_count",
230                 "vm.stats.vm.v_inactive_count",
231                 "vm.stats.vm.v_cache_count",
232                 NULL
233         };
234         double sysctl_vals[8];
236         int    i;
238         for (i = 0; sysctl_keys[i] != NULL; i++)
239         {
240                 int value;
241                 size_t value_len = sizeof (value);
243                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
244                                         NULL, 0) == 0)
245                 {
246                         sysctl_vals[i] = value;
247                         DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
248                 }
249                 else
250                 {
251                         sysctl_vals[i] = NAN;
252                 }
253         } /* for (sysctl_keys) */
255         /* multiply all all page counts with the pagesize */
256         for (i = 1; sysctl_keys[i] != NULL; i++)
257                 if (!isnan (sysctl_vals[i]))
258                         sysctl_vals[i] *= sysctl_vals[0];
260         memory_submit ("free",     sysctl_vals[2]);
261         memory_submit ("wired",    sysctl_vals[3]);
262         memory_submit ("active",   sysctl_vals[4]);
263         memory_submit ("inactive", sysctl_vals[5]);
264         memory_submit ("cache",    sysctl_vals[6]);
265 /* #endif HAVE_SYSCTLBYNAME */
267 #elif KERNEL_LINUX
268         FILE *fh;
269         char buffer[1024];
270         
271         char *fields[8];
272         int numfields;
274         long long mem_used = 0;
275         long long mem_buffered = 0;
276         long long mem_cached = 0;
277         long long mem_free = 0;
279         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
280         {
281                 char errbuf[1024];
282                 WARNING ("memory: fopen: %s",
283                                 sstrerror (errno, errbuf, sizeof (errbuf)));
284                 return (-1);
285         }
287         while (fgets (buffer, 1024, fh) != NULL)
288         {
289                 long long *val = NULL;
291                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
292                         val = &mem_used;
293                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
294                         val = &mem_free;
295                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
296                         val = &mem_buffered;
297                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
298                         val = &mem_cached;
299                 else
300                         continue;
302                 numfields = strsplit (buffer, fields, 8);
304                 if (numfields < 2)
305                         continue;
307                 *val = atoll (fields[1]) * 1024LL;
308         }
310         if (fclose (fh))
311         {
312                 char errbuf[1024];
313                 WARNING ("memory: fclose: %s",
314                                 sstrerror (errno, errbuf, sizeof (errbuf)));
315         }
317         if (mem_used >= (mem_free + mem_buffered + mem_cached))
318         {
319                 mem_used -= mem_free + mem_buffered + mem_cached;
320                 memory_submit ("used",     mem_used);
321                 memory_submit ("buffered", mem_buffered);
322                 memory_submit ("cached",   mem_cached);
323                 memory_submit ("free",     mem_free);
324         }
325 /* #endif KERNEL_LINUX */
327 #elif HAVE_LIBKSTAT
328         long long mem_used;
329         long long mem_free;
330         long long mem_lock;
332         if (ksp == NULL)
333                 return (-1);
335         mem_used = get_kstat_value (ksp, "pagestotal");
336         mem_free = get_kstat_value (ksp, "pagesfree");
337         mem_lock = get_kstat_value (ksp, "pageslocked");
339         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
340                 return (-1);
341         if (mem_used < (mem_free + mem_lock))
342                 return (-1);
344         mem_used -= mem_free + mem_lock;
345         mem_used *= pagesize; /* If this overflows you have some serious */
346         mem_free *= pagesize; /* memory.. Why not call me up and give me */
347         mem_lock *= pagesize; /* some? ;) */
349         memory_submit ("used",   mem_used);
350         memory_submit ("free",   mem_free);
351         memory_submit ("locked", mem_lock);
352 /* #endif HAVE_LIBKSTAT */
354 #elif HAVE_LIBSTATGRAB
355         sg_mem_stats *ios;
357         if ((ios = sg_get_mem_stats ()) != NULL)
358         {
359                 memory_submit ("used",   ios->used);
360                 memory_submit ("cached", ios->cache);
361                 memory_submit ("free",   ios->free);
362         }
363 #endif /* HAVE_LIBSTATGRAB */
365         return (0);
368 void module_register (void)
370         plugin_register_init ("memory", memory_init);
371         plugin_register_read ("memory", memory_read);
372 } /* void module_register */