Merge branch 'collectd-4.8' into collectd-4.9
[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  **/
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 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 */
99
100 #elif HAVE_SYSCTLBYNAME
101 /* no init stuff */
102 /* #endif HAVE_SYSCTLBYNAME */
103
104 #elif defined(KERNEL_LINUX)
105 /* no init stuff */
106 /* #endif KERNEL_LINUX */
107
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 */
117
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 */
126
127 #elif HAVE_LIBSTATGRAB
128 /* no init stuff */
129 /* #endif HAVE_LIBSTATGRAB */
130
131 #elif HAVE_PERFSTAT
132         pagesize = getpagesize ();
133 #endif /* HAVE_PERFSTAT */
134         return (0);
135 } /* int memory_init */
136
137 static void memory_submit (const char *type_instance, gauge_t value)
138 {
139         value_t values[1];
140         value_list_t vl = VALUE_LIST_INIT;
141
142         values[0].gauge = value;
143
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));
150
151         plugin_dispatch_values (&vl);
152 }
153
154 static int memory_read (void)
155 {
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;
160
161         gauge_t wired;
162         gauge_t active;
163         gauge_t inactive;
164         gauge_t free;
165
166         if (!port_host || !pagesize)
167                 return (-1);
168
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         }
177
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          */
197
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));
202
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 */
208
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];
231
232         int    i;
233
234         for (i = 0; sysctl_keys[i] != NULL; i++)
235         {
236                 int value;
237                 size_t value_len = sizeof (value);
238
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) */
250
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];
255
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 */
262
263 #elif KERNEL_LINUX
264         FILE *fh;
265         char buffer[1024];
266
267         char *fields[8];
268         int numfields;
269
270         long long mem_used = 0;
271         long long mem_buffered = 0;
272         long long mem_cached = 0;
273         long long mem_free = 0;
274
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         }
282
283         while (fgets (buffer, 1024, fh) != NULL)
284         {
285                 long long *val = NULL;
286
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;
297
298                 numfields = strsplit (buffer, fields, 8);
299
300                 if (numfields < 2)
301                         continue;
302
303                 *val = atoll (fields[1]) * 1024LL;
304         }
305
306         if (fclose (fh))
307         {
308                 char errbuf[1024];
309                 WARNING ("memory: fclose: %s",
310                                 sstrerror (errno, errbuf, sizeof (errbuf)));
311         }
312
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 */
322
323 #elif HAVE_LIBKSTAT
324         long long mem_used;
325         long long mem_free;
326         long long mem_lock;
327
328         if (ksp == NULL)
329                 return (-1);
330
331         mem_used = get_kstat_value (ksp, "pagestotal");
332         mem_free = get_kstat_value (ksp, "pagesfree");
333         mem_lock = get_kstat_value (ksp, "pageslocked");
334
335         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
336                 return (-1);
337         if (mem_used < (mem_free + mem_lock))
338                 return (-1);
339
340         mem_used -= mem_free + mem_lock;
341         mem_used *= pagesize; /* If this overflows you have some serious */
342         mem_free *= pagesize; /* memory.. Why not call me up and give me */
343         mem_lock *= pagesize; /* some? ;) */
344
345         memory_submit ("used",   mem_used);
346         memory_submit ("free",   mem_free);
347         memory_submit ("locked", mem_lock);
348 /* #endif HAVE_LIBKSTAT */
349
350 #elif HAVE_SYSCTL
351         int mib[] = {CTL_VM, VM_METER};
352         struct vmtotal vmtotal;
353         size_t size;
354
355         memset (&vmtotal, 0, sizeof (vmtotal));
356         size = sizeof (vmtotal);
357
358         if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
359                 char errbuf[1024];
360                 WARNING ("memory plugin: sysctl failed: %s",
361                         sstrerror (errno, errbuf, sizeof (errbuf)));
362                 return (-1);
363         }
364
365         assert (pagesize > 0);
366         memory_submit ("active",   vmtotal.t_arm * pagesize);
367         memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
368         memory_submit ("free",     vmtotal.t_free * pagesize);
369 /* #endif HAVE_SYSCTL */
370
371 #elif HAVE_LIBSTATGRAB
372         sg_mem_stats *ios;
373
374         if ((ios = sg_get_mem_stats ()) != NULL)
375         {
376                 memory_submit ("used",   ios->used);
377                 memory_submit ("cached", ios->cache);
378                 memory_submit ("free",   ios->free);
379         }
380 /* #endif HAVE_LIBSTATGRAB */
381
382 #elif HAVE_PERFSTAT
383         if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
384         {
385                 char errbuf[1024];
386                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
387                         sstrerror (errno, errbuf, sizeof (errbuf)));
388                 return (-1);
389         }
390         memory_submit ("used",   pmemory.real_inuse * pagesize);
391         memory_submit ("free",   pmemory.real_free * pagesize);
392         memory_submit ("cached", pmemory.numperm * pagesize);
393         memory_submit ("system", pmemory.real_system * pagesize);
394         memory_submit ("user",   pmemory.real_process * pagesize);
395 #endif /* HAVE_PERFSTAT */
396
397         return (0);
398 }
399
400 void module_register (void)
401 {
402         plugin_register_init ("memory", memory_init);
403         plugin_register_read ("memory", memory_read);
404 } /* void module_register */