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