memory plugin: Use the "complex" configuration.
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005-2014  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  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
32 #endif
33
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
49
50 #if HAVE_STATGRAB_H
51 # include <statgrab.h>
52 #endif
53
54 #if HAVE_PERFSTAT
55 # include <sys/protosw.h>
56 # include <libperfstat.h>
57 #endif /* HAVE_PERFSTAT */
58
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 */
64
65 #elif HAVE_SYSCTLBYNAME
66 /* no global variables */
67 /* #endif HAVE_SYSCTLBYNAME */
68
69 #elif KERNEL_LINUX
70 /* no global variables */
71 /* #endif KERNEL_LINUX */
72
73 #elif HAVE_LIBKSTAT
74 static int pagesize;
75 static kstat_t *ksp;
76 /* #endif HAVE_LIBKSTAT */
77
78 #elif HAVE_SYSCTL
79 static int pagesize;
80 /* #endif HAVE_SYSCTL */
81
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
92
93 static _Bool values_absolute = 1;
94 static _Bool values_percentage = 0;
95
96 static int memory_config (oconfig_item_t *ci) /* {{{ */
97 {
98         int i;
99
100         for (i = 0; i < ci->children_num; i++)
101         {
102                 oconfig_item_t *child = ci->children + i;
103                 if (strcasecmp ("ValuesAbsolute", child->key) == 0)
104                         cf_util_get_boolean (child, &values_absolute);
105                 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
106                         cf_util_get_boolean (child, &values_percentage);
107                 else
108                         ERROR ("memory plugin: Invalid configuration option: "
109                                         "\"%s\".", child->key);
110         }
111
112         return (0);
113 } /* }}} int memory_config */
114
115 static int memory_init (void)
116 {
117 #if HAVE_HOST_STATISTICS
118         port_host = mach_host_self ();
119         host_page_size (port_host, &pagesize);
120 /* #endif HAVE_HOST_STATISTICS */
121
122 #elif HAVE_SYSCTLBYNAME
123 /* no init stuff */
124 /* #endif HAVE_SYSCTLBYNAME */
125
126 #elif defined(KERNEL_LINUX)
127 /* no init stuff */
128 /* #endif KERNEL_LINUX */
129
130 #elif defined(HAVE_LIBKSTAT)
131         /* getpagesize(3C) tells me this does not fail.. */
132         pagesize = getpagesize ();
133         if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
134         {
135                 ksp = NULL;
136                 return (-1);
137         }
138 /* #endif HAVE_LIBKSTAT */
139
140 #elif HAVE_SYSCTL
141         pagesize = getpagesize ();
142         if (pagesize <= 0)
143         {
144                 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
145                 return (-1);
146         }
147 /* #endif HAVE_SYSCTL */
148
149 #elif HAVE_LIBSTATGRAB
150 /* no init stuff */
151 /* #endif HAVE_LIBSTATGRAB */
152
153 #elif HAVE_PERFSTAT
154         pagesize = getpagesize ();
155 #endif /* HAVE_PERFSTAT */
156         return (0);
157 } /* int memory_init */
158
159 static void memory_submit (const char *type_instance, gauge_t value)
160 {
161         value_t values[1];
162         value_list_t vl = VALUE_LIST_INIT;
163
164         values[0].gauge = value;
165
166         vl.values = values;
167         vl.values_len = 1;
168         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
169         sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
170         sstrncpy (vl.type, "memory", sizeof (vl.type));
171         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
172
173         plugin_dispatch_values (&vl);
174 }
175
176 static int memory_read (void)
177 {
178 #if HAVE_HOST_STATISTICS
179         kern_return_t status;
180         vm_statistics_data_t   vm_data;
181         mach_msg_type_number_t vm_data_len;
182
183         gauge_t wired;
184         gauge_t active;
185         gauge_t inactive;
186         gauge_t free;
187         gauge_t total;
188
189         if (!port_host || !pagesize)
190                 return (-1);
191
192         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
193         if ((status = host_statistics (port_host, HOST_VM_INFO,
194                                         (host_info_t) &vm_data,
195                                         &vm_data_len)) != KERN_SUCCESS)
196         {
197                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
198                 return (-1);
199         }
200
201         /*
202          * From <http://docs.info.apple.com/article.html?artnum=107918>:
203          *
204          * Wired memory
205          *   This information can't be cached to disk, so it must stay in RAM.
206          *   The amount depends on what applications you are using.
207          *
208          * Active memory
209          *   This information is currently in RAM and actively being used.
210          *
211          * Inactive memory
212          *   This information is no longer being used and has been cached to
213          *   disk, but it will remain in RAM until another application needs
214          *   the space. Leaving this information in RAM is to your advantage if
215          *   you (or a client of your computer) come back to it later.
216          *
217          * Free memory
218          *   This memory is not being used.
219          */
220
221         wired    = (gauge_t) (((uint64_t) vm_data.wire_count)     * ((uint64_t) pagesize));
222         active   = (gauge_t) (((uint64_t) vm_data.active_count)   * ((uint64_t) pagesize));
223         inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
224         free     = (gauge_t) (((uint64_t) vm_data.free_count)     * ((uint64_t) pagesize));
225         total    = wired + active + inactive + free;
226
227         if (values_absolute)
228         {
229                 memory_submit ("wired",    wired);
230                 memory_submit ("active",   active);
231                 memory_submit ("inactive", inactive);
232                 memory_submit ("free",     free);
233         }
234         if (values_percentage)
235         {
236                 memory_submit ("percent_wired",    (gauge_t) ((float_t) wired) / total * 100);
237                 memory_submit ("percent_active",   (gauge_t) ((float_t) active) / total * 100);
238                 memory_submit ("percent_inactive", (gauge_t) ((float_t) inactive / total * 100);
239                 memory_submit ("percent_free",     (gauge_t) ((float_t) free / total * 100);
240         }
241 /* #endif HAVE_HOST_STATISTICS */
242
243 #elif HAVE_SYSCTLBYNAME
244         /*
245          * vm.stats.vm.v_page_size: 4096
246          * vm.stats.vm.v_page_count: 246178
247          * vm.stats.vm.v_free_count: 28760
248          * vm.stats.vm.v_wire_count: 37526
249          * vm.stats.vm.v_active_count: 55239
250          * vm.stats.vm.v_inactive_count: 113730
251          * vm.stats.vm.v_cache_count: 10809
252          */
253         char *sysctl_keys[8] =
254         {
255                 "vm.stats.vm.v_page_size",
256                 "vm.stats.vm.v_page_count",
257                 "vm.stats.vm.v_free_count",
258                 "vm.stats.vm.v_wire_count",
259                 "vm.stats.vm.v_active_count",
260                 "vm.stats.vm.v_inactive_count",
261                 "vm.stats.vm.v_cache_count",
262                 NULL
263         };
264         double sysctl_vals[8];
265
266         int    i;
267
268         for (i = 0; sysctl_keys[i] != NULL; i++)
269         {
270                 int value;
271                 size_t value_len = sizeof (value);
272
273                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
274                                         NULL, 0) == 0)
275                 {
276                         sysctl_vals[i] = value;
277                         DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
278                 }
279                 else
280                 {
281                         sysctl_vals[i] = NAN;
282                 }
283         } /* for (sysctl_keys) */
284
285         /* multiply all all page counts with the pagesize */
286         for (i = 1; sysctl_keys[i] != NULL; i++)
287                 if (!isnan (sysctl_vals[i]))
288                         sysctl_vals[i] *= sysctl_vals[0];
289
290         if (values_absolute)
291         {
292                 memory_submit ("free",     sysctl_vals[2]);
293                 memory_submit ("wired",    sysctl_vals[3]);
294                 memory_submit ("active",   sysctl_vals[4]);
295                 memory_submit ("inactive", sysctl_vals[5]);
296                 memory_submit ("cache",    sysctl_vals[6]);
297         }
298         if (values_percentage)
299         {
300                 double total = sysctl_vals[2] + sysctl_vals[3] + sysctl_vals[4] + sysctl_vals[5] + sysctl_vals[6];
301                 memory_submit ("percent_free",     (gauge_t) ((float_t) sysctl_vals[2]) / total * 100);
302                 memory_submit ("percent_wired",    (gauge_t) ((float_t) sysctl_vals[3]) / total * 100);
303                 memory_submit ("percent_active",   (gauge_t) ((float_t) sysctl_vals[4]) / total * 100);
304                 memory_submit ("percent_inactive", (gauge_t) ((float_t) sysctl_vals[5]) / total * 100);
305                 memory_submit ("percent_cache",    (gauge_t) ((float_t) sysctl_vals[6]) / total * 100);
306         }
307 /* #endif HAVE_SYSCTLBYNAME */
308
309 #elif KERNEL_LINUX
310         FILE *fh;
311         char buffer[1024];
312
313         char *fields[8];
314         int numfields;
315
316         long long mem_total = 0;
317         long long mem_used = 0;
318         long long mem_buffered = 0;
319         long long mem_cached = 0;
320         long long mem_free = 0;
321
322         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
323         {
324                 char errbuf[1024];
325                 WARNING ("memory: fopen: %s",
326                                 sstrerror (errno, errbuf, sizeof (errbuf)));
327                 return (-1);
328         }
329
330         while (fgets (buffer, 1024, fh) != NULL)
331         {
332                 long long *val = NULL;
333
334                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
335                         val = &mem_total;
336                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
337                         val = &mem_free;
338                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
339                         val = &mem_buffered;
340                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
341                         val = &mem_cached;
342                 else
343                         continue;
344
345                 numfields = strsplit (buffer, fields, 8);
346
347                 if (numfields < 2)
348                         continue;
349
350                 *val = atoll (fields[1]) * 1024LL;
351         }
352
353         if (fclose (fh))
354         {
355                 char errbuf[1024];
356                 WARNING ("memory: fclose: %s",
357                                 sstrerror (errno, errbuf, sizeof (errbuf)));
358         }
359
360         if (mem_total >= (mem_free + mem_buffered + mem_cached))
361         {
362                 mem_used = mem_total - (mem_free + mem_buffered + mem_cached);
363                 if (values_absolute)
364                 {
365                         memory_submit ("used",     mem_used);
366                         memory_submit ("buffered", mem_buffered);
367                         memory_submit ("cached",   mem_cached);
368                         memory_submit ("free",     mem_free);
369                 }
370                 if (values_percentage)
371                 {
372                         memory_submit ("percent_used",     (gauge_t) ((float_t) mem_used) / mem_total * 100);
373                         memory_submit ("percent_buffered", (gauge_t) ((float_t) mem_buffered) / mem_total * 100);
374                         memory_submit ("percent_cached",   (gauge_t) ((float_t) mem_cached) / mem_total * 100);
375                         memory_submit ("percent_free",     (gauge_t) ((float_t) mem_free) / mem_total * 100);
376                 }
377         }
378 /* #endif KERNEL_LINUX */
379
380 #elif HAVE_LIBKSTAT
381         /* Most of the additions here were taken as-is from the k9toolkit from
382          * Brendan Gregg and are subject to change I guess */
383         long long mem_used;
384         long long mem_free;
385         long long mem_lock;
386         long long mem_kern;
387         long long mem_unus;
388
389         long long pp_kernel;
390         long long physmem;
391         long long availrmem;
392
393         if (ksp == NULL)
394                 return (-1);
395
396         mem_used = get_kstat_value (ksp, "pagestotal");
397         mem_free = get_kstat_value (ksp, "pagesfree");
398         mem_lock = get_kstat_value (ksp, "pageslocked");
399         mem_kern = 0;
400         mem_unus = 0;
401
402         pp_kernel = get_kstat_value (ksp, "pp_kernel");
403         physmem = get_kstat_value (ksp, "physmem");
404         availrmem = get_kstat_value (ksp, "availrmem");
405
406         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
407         {
408                 WARNING ("memory plugin: one of used, free or locked is negative.");
409                 return (-1);
410         }
411
412         mem_unus = physmem - mem_used;
413
414         if (mem_used < (mem_free + mem_lock))
415         {
416                 /* source: http://wesunsolve.net/bugid/id/4909199
417                  * this seems to happen when swap space is small, e.g. 2G on a 32G system
418                  * we will make some assumptions here
419                  * educated solaris internals help welcome here */
420                 DEBUG ("memory plugin: pages total is smaller than \"free\" "
421                                 "+ \"locked\". This is probably due to small "
422                                 "swap space");
423                 mem_free = availrmem;
424                 mem_used = 0;
425         }
426         else
427         {
428                 mem_used -= mem_free + mem_lock;
429         }
430
431         /* mem_kern is accounted for in mem_lock */
432         if ( pp_kernel < mem_lock )
433         {
434                 mem_kern = pp_kernel;
435                 mem_lock -= pp_kernel;
436         }
437         else
438         {
439                 mem_kern = mem_lock;
440                 mem_lock = 0;
441         }
442
443         mem_used *= pagesize; /* If this overflows you have some serious */
444         mem_free *= pagesize; /* memory.. Why not call me up and give me */
445         mem_lock *= pagesize; /* some? ;) */
446         mem_kern *= pagesize; /* it's 2011 RAM is cheap */
447         mem_unus *= pagesize;
448
449         if (values_absolute)
450         {
451                 memory_submit ("used",   mem_used);
452                 memory_submit ("free",   mem_free);
453                 memory_submit ("locked", mem_lock);
454                 memory_submit ("kernel", mem_kern);
455                 memory_submit ("unusable", mem_unus);
456         }
457         if (values_percentage)
458         {
459                 memory_submit ("percent_used",   (gauge_t) ((float_t) mem_used) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
460                 memory_submit ("percent_free",   (gauge_t) ((float_t) mem_free) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
461                 memory_submit ("percent_locked", (gauge_t) ((float_t) mem_lock) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
462                 memory_submit ("percent_kernel", (gauge_t) ((float_t) mem_kern) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
463                 memory_submit ("percent_unusable", (gauge_t) ((float_t) mem_unus) / (mem_used + mem_free + mem_lock + mem_kern + mem_unus) * 100);
464
465         }
466 /* #endif HAVE_LIBKSTAT */
467
468 #elif HAVE_SYSCTL
469         int mib[] = {CTL_VM, VM_METER};
470         struct vmtotal vmtotal;
471         size_t size;
472
473         memset (&vmtotal, 0, sizeof (vmtotal));
474         size = sizeof (vmtotal);
475
476         if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
477                 char errbuf[1024];
478                 WARNING ("memory plugin: sysctl failed: %s",
479                         sstrerror (errno, errbuf, sizeof (errbuf)));
480                 return (-1);
481         }
482
483         assert (pagesize > 0);
484         if (values_absolute)
485         {
486                 memory_submit ("active",   vmtotal.t_arm * pagesize);
487                 memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
488                 memory_submit ("free",     vmtotal.t_free * pagesize);
489         }
490         if (values_percentage)
491         {
492                 memory_submit ("percent_active",   (gauge_t) ((float_t) vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
493                 memory_submit ("percent_inactive", (gauge_t) ((float_t) (vmtotal.t_rm - vmtotal.t_arm) / (vmtotal.t_rm + vmtotal.t_free) * 100);
494                 memory_submit ("percent_free",     (gauge_t) ((float_t) vmtotal.t_free) / (vmtotal.t_rm + vmtotal.t_free) * 100);
495         }
496 /* #endif HAVE_SYSCTL */
497
498 #elif HAVE_LIBSTATGRAB
499         sg_mem_stats *ios;
500
501         if ((ios = sg_get_mem_stats ()) != NULL)
502         {
503                 if (values_absolute)
504                 {
505                         memory_submit ("used",   ios->used);
506                         memory_submit ("cached", ios->cache);
507                         memory_submit ("free",   ios->free);
508                 }
509                 if (values_percentage)
510                 {
511                         memory_submit ("percent_used",   (gauge_t) ((float_t) ios->used) / (ios->used + ios->cache + ios->free) * 100);
512                         memory_submit ("percent_cached", (gauge_t) ((float_t) ios->cache) / (ios->used + ios->cache + ios->free) * 100);
513                         memory_submit ("percent_free",   (gauge_t) ((float_t) ios->free) / (ios->used + ios->cache + ios->free) * 100);
514                 }
515         }
516 /* #endif HAVE_LIBSTATGRAB */
517
518 #elif HAVE_PERFSTAT
519         if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
520         {
521                 char errbuf[1024];
522                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
523                         sstrerror (errno, errbuf, sizeof (errbuf)));
524                 return (-1);
525         }
526         if (values_absolute)
527         {
528                 memory_submit ("used",   pmemory.real_inuse * pagesize);
529                 memory_submit ("free",   pmemory.real_free * pagesize);
530                 memory_submit ("cached", pmemory.numperm * pagesize);
531                 memory_submit ("system", pmemory.real_system * pagesize);
532                 memory_submit ("user",   pmemory.real_process * pagesize);
533         }
534         if (values_percentage)
535         {
536                 memory_submit ("percent_used",   (gauge_t) ((float_t) pmemory.real_inuse) / pmemory.real_total * 100);
537                 memory_submit ("percent_free",   (gauge_t) ((float_t) pmemory.real_free) / pmemory.real_total * 100);
538                 memory_submit ("percent_cached", (gauge_t) ((float_t) pmemory.numperm) / pmemory.real_total * 100);
539                 memory_submit ("percent_system", (gauge_t) ((float_t) pmemory.real_system) / pmemory.real_total * 100);
540                 memory_submit ("percent_user",   (gauge_t) ((float_t) pmemory.real_process) / pmemory.real_total * 100);
541         }
542 #endif /* HAVE_PERFSTAT */
543
544         return (0);
545 }
546
547 void module_register (void)
548 {
549         plugin_register_complex_config ("memory", memory_config);
550         plugin_register_init ("memory", memory_init);
551         plugin_register_read ("memory", memory_read);
552 } /* void module_register */