Code

Merge remote-tracking branch 'github/pr/387'
[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  * Copyright (C) 2009       Manuel Sanmartin
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at verplant.org>
22  *   Simon Kuhnle <simon at blarzwurst.de>
23  *   Manuel Sanmartin
24  **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
32 #endif
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
36 #endif
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
39 #endif
40 #ifdef HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
42 #endif
43 #ifdef HAVE_MACH_HOST_PRIV_H
44 # include <mach/host_priv.h>
45 #endif
46 #ifdef HAVE_MACH_VM_STATISTICS_H
47 # include <mach/vm_statistics.h>
48 #endif
50 #if HAVE_STATGRAB_H
51 # include <statgrab.h>
52 #endif
54 #if HAVE_PERFSTAT
55 # include <sys/protosw.h>
56 # include <libperfstat.h>
57 #endif /* HAVE_PERFSTAT */
59 /* vm_statistics_data_t */
60 #if HAVE_HOST_STATISTICS
61 static mach_port_t port_host;
62 static vm_size_t pagesize;
63 /* #endif HAVE_HOST_STATISTICS */
65 #elif HAVE_SYSCTLBYNAME
66 /* no global variables */
67 /* #endif HAVE_SYSCTLBYNAME */
69 #elif KERNEL_LINUX
70 /* no global variables */
71 /* #endif KERNEL_LINUX */
73 #elif HAVE_LIBKSTAT
74 static int pagesize;
75 static kstat_t *ksp;
76 /* #endif HAVE_LIBKSTAT */
78 #elif HAVE_SYSCTL
79 static int pagesize;
80 /* #endif HAVE_SYSCTL */
82 #elif HAVE_LIBSTATGRAB
83 /* no global variables */
84 /* endif HAVE_LIBSTATGRAB */
85 #elif HAVE_PERFSTAT
86 static int pagesize;
87 static perfstat_memory_total_t pmemory;
88 /* endif HAVE_PERFSTAT */
89 #else
90 # error "No applicable input method."
91 #endif
93 static int memory_init (void)
94 {
95 #if HAVE_HOST_STATISTICS
96         port_host = mach_host_self ();
97         host_page_size (port_host, &pagesize);
98 /* #endif HAVE_HOST_STATISTICS */
100 #elif HAVE_SYSCTLBYNAME
101 /* no init stuff */
102 /* #endif HAVE_SYSCTLBYNAME */
104 #elif defined(KERNEL_LINUX)
105 /* no init stuff */
106 /* #endif KERNEL_LINUX */
108 #elif defined(HAVE_LIBKSTAT)
109         /* getpagesize(3C) tells me this does not fail.. */
110         pagesize = getpagesize ();
111         if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
112         {
113                 ksp = NULL;
114                 return (-1);
115         }
116 /* #endif HAVE_LIBKSTAT */
118 #elif HAVE_SYSCTL
119         pagesize = getpagesize ();
120         if (pagesize <= 0)
121         {
122                 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
123                 return (-1);
124         }
125 /* #endif HAVE_SYSCTL */
127 #elif HAVE_LIBSTATGRAB
128 /* no init stuff */
129 /* #endif HAVE_LIBSTATGRAB */
131 #elif HAVE_PERFSTAT
132         pagesize = getpagesize ();
133 #endif /* HAVE_PERFSTAT */
134         return (0);
135 } /* int memory_init */
137 static void memory_submit (const char *type_instance, gauge_t value)
139         value_t values[1];
140         value_list_t vl = VALUE_LIST_INIT;
142         values[0].gauge = value;
144         vl.values = values;
145         vl.values_len = 1;
146         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
147         sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
148         sstrncpy (vl.type, "memory", sizeof (vl.type));
149         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
151         plugin_dispatch_values (&vl);
154 static int memory_read (void)
156 #if HAVE_HOST_STATISTICS
157         kern_return_t status;
158         vm_statistics_data_t   vm_data;
159         mach_msg_type_number_t vm_data_len;
161         gauge_t wired;
162         gauge_t active;
163         gauge_t inactive;
164         gauge_t free;
166         if (!port_host || !pagesize)
167                 return (-1);
169         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
170         if ((status = host_statistics (port_host, HOST_VM_INFO,
171                                         (host_info_t) &vm_data,
172                                         &vm_data_len)) != KERN_SUCCESS)
173         {
174                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
175                 return (-1);
176         }
178         /*
179          * From <http://docs.info.apple.com/article.html?artnum=107918>:
180          *
181          * Wired memory
182          *   This information can't be cached to disk, so it must stay in RAM.
183          *   The amount depends on what applications you are using.
184          *
185          * Active memory
186          *   This information is currently in RAM and actively being used.
187          *
188          * Inactive memory
189          *   This information is no longer being used and has been cached to
190          *   disk, but it will remain in RAM until another application needs
191          *   the space. Leaving this information in RAM is to your advantage if
192          *   you (or a client of your computer) come back to it later.
193          *
194          * Free memory
195          *   This memory is not being used.
196          */
198         wired    = (gauge_t) (((uint64_t) vm_data.wire_count)     * ((uint64_t) pagesize));
199         active   = (gauge_t) (((uint64_t) vm_data.active_count)   * ((uint64_t) pagesize));
200         inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
201         free     = (gauge_t) (((uint64_t) vm_data.free_count)     * ((uint64_t) pagesize));
203         memory_submit ("wired",    wired);
204         memory_submit ("active",   active);
205         memory_submit ("inactive", inactive);
206         memory_submit ("free",     free);
207 /* #endif HAVE_HOST_STATISTICS */
209 #elif HAVE_SYSCTLBYNAME
210         /*
211          * vm.stats.vm.v_page_size: 4096
212          * vm.stats.vm.v_page_count: 246178
213          * vm.stats.vm.v_free_count: 28760
214          * vm.stats.vm.v_wire_count: 37526
215          * vm.stats.vm.v_active_count: 55239
216          * vm.stats.vm.v_inactive_count: 113730
217          * vm.stats.vm.v_cache_count: 10809
218          */
219         char *sysctl_keys[8] =
220         {
221                 "vm.stats.vm.v_page_size",
222                 "vm.stats.vm.v_page_count",
223                 "vm.stats.vm.v_free_count",
224                 "vm.stats.vm.v_wire_count",
225                 "vm.stats.vm.v_active_count",
226                 "vm.stats.vm.v_inactive_count",
227                 "vm.stats.vm.v_cache_count",
228                 NULL
229         };
230         double sysctl_vals[8];
232         int    i;
234         for (i = 0; sysctl_keys[i] != NULL; i++)
235         {
236                 int value;
237                 size_t value_len = sizeof (value);
239                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
240                                         NULL, 0) == 0)
241                 {
242                         sysctl_vals[i] = value;
243                         DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
244                 }
245                 else
246                 {
247                         sysctl_vals[i] = NAN;
248                 }
249         } /* for (sysctl_keys) */
251         /* multiply all all page counts with the pagesize */
252         for (i = 1; sysctl_keys[i] != NULL; i++)
253                 if (!isnan (sysctl_vals[i]))
254                         sysctl_vals[i] *= sysctl_vals[0];
256         memory_submit ("free",     sysctl_vals[2]);
257         memory_submit ("wired",    sysctl_vals[3]);
258         memory_submit ("active",   sysctl_vals[4]);
259         memory_submit ("inactive", sysctl_vals[5]);
260         memory_submit ("cache",    sysctl_vals[6]);
261 /* #endif HAVE_SYSCTLBYNAME */
263 #elif KERNEL_LINUX
264         FILE *fh;
265         char buffer[1024];
267         char *fields[8];
268         int numfields;
270         long long mem_used = 0;
271         long long mem_buffered = 0;
272         long long mem_cached = 0;
273         long long mem_free = 0;
275         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
276         {
277                 char errbuf[1024];
278                 WARNING ("memory: fopen: %s",
279                                 sstrerror (errno, errbuf, sizeof (errbuf)));
280                 return (-1);
281         }
283         while (fgets (buffer, 1024, fh) != NULL)
284         {
285                 long long *val = NULL;
287                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
288                         val = &mem_used;
289                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
290                         val = &mem_free;
291                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
292                         val = &mem_buffered;
293                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
294                         val = &mem_cached;
295                 else
296                         continue;
298                 numfields = strsplit (buffer, fields, 8);
300                 if (numfields < 2)
301                         continue;
303                 *val = atoll (fields[1]) * 1024LL;
304         }
306         if (fclose (fh))
307         {
308                 char errbuf[1024];
309                 WARNING ("memory: fclose: %s",
310                                 sstrerror (errno, errbuf, sizeof (errbuf)));
311         }
313         if (mem_used >= (mem_free + mem_buffered + mem_cached))
314         {
315                 mem_used -= mem_free + mem_buffered + mem_cached;
316                 memory_submit ("used",     mem_used);
317                 memory_submit ("buffered", mem_buffered);
318                 memory_submit ("cached",   mem_cached);
319                 memory_submit ("free",     mem_free);
320         }
321 /* #endif KERNEL_LINUX */
323 #elif HAVE_LIBKSTAT
324         /* Most of the additions here were taken as-is from the k9toolkit from
325          * Brendan Gregg and are subject to change I guess */
326         long long mem_used;
327         long long mem_free;
328         long long mem_lock;
329         long long mem_kern;
330         long long mem_unus;
332         long long pp_kernel;
333         long long physmem;
334         long long availrmem;
336         if (ksp == NULL)
337                 return (-1);
339         mem_used = get_kstat_value (ksp, "pagestotal");
340         mem_free = get_kstat_value (ksp, "pagesfree");
341         mem_lock = get_kstat_value (ksp, "pageslocked");
342         mem_kern = 0;
343         mem_unus = 0;
345         pp_kernel = get_kstat_value (ksp, "pp_kernel");
346         physmem = get_kstat_value (ksp, "physmem");
347         availrmem = get_kstat_value (ksp, "availrmem");
349         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
350         {
351                 WARNING ("memory plugin: one of used, free or locked is negative.");
352                 return (-1);
353         }
355         mem_unus = physmem - mem_used;
357         if (mem_used < (mem_free + mem_lock))
358         {
359                 /* source: http://wesunsolve.net/bugid/id/4909199
360                  * this seems to happen when swap space is small, e.g. 2G on a 32G system
361                  * we will make some assumptions here
362                  * educated solaris internals help welcome here */
363                 DEBUG ("memory plugin: pages total is smaller than \"free\" "
364                                 "+ \"locked\". This is probably due to small "
365                                 "swap space");
366                 mem_free = availrmem;
367                 mem_used = 0;
368         }
369         else
370         {
371                 mem_used -= mem_free + mem_lock;
372         }
374         /* mem_kern is accounted for in mem_lock */
375         if ( pp_kernel < mem_lock )
376         {
377                 mem_kern = pp_kernel;
378                 mem_lock -= pp_kernel;
379         }
380         else
381         {
382                 mem_kern = mem_lock;
383                 mem_lock = 0;
384         }
386         mem_used *= pagesize; /* If this overflows you have some serious */
387         mem_free *= pagesize; /* memory.. Why not call me up and give me */
388         mem_lock *= pagesize; /* some? ;) */
389         mem_kern *= pagesize; /* it's 2011 RAM is cheap */
390         mem_unus *= pagesize;
392         memory_submit ("used",   mem_used);
393         memory_submit ("free",   mem_free);
394         memory_submit ("locked", mem_lock);
395         memory_submit ("kernel", mem_kern);
396         memory_submit ("unusable", mem_unus);
397 /* #endif HAVE_LIBKSTAT */
399 #elif HAVE_SYSCTL
400         int mib[] = {CTL_VM, VM_METER};
401         struct vmtotal vmtotal;
402         size_t size;
404         memset (&vmtotal, 0, sizeof (vmtotal));
405         size = sizeof (vmtotal);
407         if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
408                 char errbuf[1024];
409                 WARNING ("memory plugin: sysctl failed: %s",
410                         sstrerror (errno, errbuf, sizeof (errbuf)));
411                 return (-1);
412         }
414         assert (pagesize > 0);
415         memory_submit ("active",   vmtotal.t_arm * pagesize);
416         memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
417         memory_submit ("free",     vmtotal.t_free * pagesize);
418 /* #endif HAVE_SYSCTL */
420 #elif HAVE_LIBSTATGRAB
421         sg_mem_stats *ios;
423         if ((ios = sg_get_mem_stats ()) != NULL)
424         {
425                 memory_submit ("used",   ios->used);
426                 memory_submit ("cached", ios->cache);
427                 memory_submit ("free",   ios->free);
428         }
429 /* #endif HAVE_LIBSTATGRAB */
431 #elif HAVE_PERFSTAT
432         if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
433         {
434                 char errbuf[1024];
435                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
436                         sstrerror (errno, errbuf, sizeof (errbuf)));
437                 return (-1);
438         }
439         memory_submit ("used",   pmemory.real_inuse * pagesize);
440         memory_submit ("free",   pmemory.real_free * pagesize);
441         memory_submit ("cached", pmemory.numperm * pagesize);
442         memory_submit ("system", pmemory.real_system * pagesize);
443         memory_submit ("user",   pmemory.real_process * pagesize);
444 #endif /* HAVE_PERFSTAT */
446         return (0);
449 void module_register (void)
451         plugin_register_init ("memory", memory_init);
452         plugin_register_read ("memory", memory_read);
453 } /* void module_register */