memory plugin: Check `pagesize' when initializing, not when reading.
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005-2008  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 HAVE_STATGRAB_H
47 # include <statgrab.h>
48 #endif
49
50 /* vm_statistics_data_t */
51 #if HAVE_HOST_STATISTICS
52 static mach_port_t port_host;
53 static vm_size_t pagesize;
54 /* #endif HAVE_HOST_STATISTICS */
55
56 #elif HAVE_SYSCTL
57 static int pagesize;
58 /* #endif HAVE_SYSCTL */
59
60 #elif HAVE_SYSCTLBYNAME
61 /* no global variables */
62 /* #endif HAVE_SYSCTLBYNAME */
63
64 #elif KERNEL_LINUX
65 /* no global variables */
66 /* #endif KERNEL_LINUX */
67
68 #elif HAVE_LIBKSTAT
69 static int pagesize;
70 static kstat_t *ksp;
71 /* #endif HAVE_LIBKSTAT */
72
73 #elif HAVE_LIBSTATGRAB
74 /* no global variables */
75 /* endif HAVE_LIBSTATGRAB */
76
77 #else
78 # error "No applicable input method."
79 #endif
80
81 static int memory_init (void)
82 {
83 #if HAVE_HOST_STATISTICS
84         port_host = mach_host_self ();
85         host_page_size (port_host, &pagesize);
86 /* #endif HAVE_HOST_STATISTICS */
87
88 #elif HAVE_SYSCTL
89         pagesize = getpagesize ();
90         if (pagesize <= 0)
91         {
92                 ERROR ("memory plugin: Invalid pagesize: %i", pagesize);
93                 return (-1);
94         }
95 /* #endif HAVE_SYSCTL */
96
97 #elif HAVE_SYSCTLBYNAME
98 /* no init stuff */
99 /* #endif HAVE_SYSCTLBYNAME */
100
101 #elif defined(KERNEL_LINUX)
102 /* no init stuff */
103 /* #endif KERNEL_LINUX */
104
105 #elif defined(HAVE_LIBKSTAT)
106         /* getpagesize(3C) tells me this does not fail.. */
107         pagesize = getpagesize ();
108         if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
109         {
110                 ksp = NULL;
111                 return (-1);
112         }
113 #endif /* HAVE_LIBKSTAT */
114
115         return (0);
116 } /* int memory_init */
117
118 static void memory_submit (const char *type_instance, gauge_t value)
119 {
120         value_t values[1];
121         value_list_t vl = VALUE_LIST_INIT;
122
123         values[0].gauge = value;
124
125         vl.values = values;
126         vl.values_len = 1;
127         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
128         sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
129         sstrncpy (vl.type, "memory", sizeof (vl.type));
130         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
131
132         plugin_dispatch_values (&vl);
133 }
134
135 static int memory_read (void)
136 {
137 #if HAVE_HOST_STATISTICS
138         kern_return_t status;
139         vm_statistics_data_t   vm_data;
140         mach_msg_type_number_t vm_data_len;
141
142         long long wired;
143         long long active;
144         long long inactive;
145         long long free;
146
147         if (!port_host || !pagesize)
148                 return (-1);
149
150         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
151         if ((status = host_statistics (port_host, HOST_VM_INFO,
152                                         (host_info_t) &vm_data,
153                                         &vm_data_len)) != KERN_SUCCESS)
154         {
155                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
156                 return (-1);
157         }
158
159         /*
160          * From <http://docs.info.apple.com/article.html?artnum=107918>:
161          *
162          * Wired memory
163          *   This information can't be cached to disk, so it must stay in RAM.
164          *   The amount depends on what applications you are using.
165          *
166          * Active memory
167          *   This information is currently in RAM and actively being used.
168          *
169          * Inactive memory
170          *   This information is no longer being used and has been cached to
171          *   disk, but it will remain in RAM until another application needs
172          *   the space. Leaving this information in RAM is to your advantage if
173          *   you (or a client of your computer) come back to it later.
174          *
175          * Free memory
176          *   This memory is not being used.
177          */
178
179         wired    = vm_data.wire_count     * pagesize;
180         active   = vm_data.active_count   * pagesize;
181         inactive = vm_data.inactive_count * pagesize;
182         free     = vm_data.free_count     * pagesize;
183
184         memory_submit ("wired",    wired);
185         memory_submit ("active",   active);
186         memory_submit ("inactive", inactive);
187         memory_submit ("free",     free);
188 /* #endif HAVE_HOST_STATISTICS */
189
190 #elif HAVE_SYSCTL
191         int mib[] = {CTL_VM, VM_METER};
192         struct vmtotal vmtotal;
193         size_t size;
194
195         memset (&vmtotal, 0, sizeof (vmtotal));
196         size = sizeof (vmtotal);
197
198         if (sysctl (mib, 2, &vmtotal, &size, NULL, 0) < 0) {
199                 char errbuf[1024];
200                 WARNING ("memory plugin: sysctl failed: %s",
201                         sstrerror (errno, errbuf, sizeof (errbuf)));
202                 return (-1);
203         }
204
205         assert (pagesize > 0);
206         memory_submit ("active",   vmtotal.t_arm * pagesize);
207         memory_submit ("inactive", (vmtotal.t_rm - vmtotal.t_arm) * pagesize);
208         memory_submit ("free",     vmtotal.t_free * pagesize);
209 /* #endif HAVE_SYSCTL */
210
211 #elif HAVE_SYSCTLBYNAME
212         /*
213          * vm.stats.vm.v_page_size: 4096
214          * vm.stats.vm.v_page_count: 246178
215          * vm.stats.vm.v_free_count: 28760
216          * vm.stats.vm.v_wire_count: 37526
217          * vm.stats.vm.v_active_count: 55239
218          * vm.stats.vm.v_inactive_count: 113730
219          * vm.stats.vm.v_cache_count: 10809
220          */
221         char *sysctl_keys[8] =
222         {
223                 "vm.stats.vm.v_page_size",
224                 "vm.stats.vm.v_page_count",
225                 "vm.stats.vm.v_free_count",
226                 "vm.stats.vm.v_wire_count",
227                 "vm.stats.vm.v_active_count",
228                 "vm.stats.vm.v_inactive_count",
229                 "vm.stats.vm.v_cache_count",
230                 NULL
231         };
232         double sysctl_vals[8];
233
234         int    i;
235
236         for (i = 0; sysctl_keys[i] != NULL; i++)
237         {
238                 int value;
239                 size_t value_len = sizeof (value);
240
241                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
242                                         NULL, 0) == 0)
243                 {
244                         sysctl_vals[i] = value;
245                         DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
246                 }
247                 else
248                 {
249                         sysctl_vals[i] = NAN;
250                 }
251         } /* for (sysctl_keys) */
252
253         /* multiply all all page counts with the pagesize */
254         for (i = 1; sysctl_keys[i] != NULL; i++)
255                 if (!isnan (sysctl_vals[i]))
256                         sysctl_vals[i] *= sysctl_vals[0];
257
258         memory_submit ("free",     sysctl_vals[2]);
259         memory_submit ("wired",    sysctl_vals[3]);
260         memory_submit ("active",   sysctl_vals[4]);
261         memory_submit ("inactive", sysctl_vals[5]);
262         memory_submit ("cache",    sysctl_vals[6]);
263 /* #endif HAVE_SYSCTLBYNAME */
264
265 #elif KERNEL_LINUX
266         FILE *fh;
267         char buffer[1024];
268         
269         char *fields[8];
270         int numfields;
271
272         long long mem_used = 0;
273         long long mem_buffered = 0;
274         long long mem_cached = 0;
275         long long mem_free = 0;
276
277         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
278         {
279                 char errbuf[1024];
280                 WARNING ("memory: fopen: %s",
281                                 sstrerror (errno, errbuf, sizeof (errbuf)));
282                 return (-1);
283         }
284
285         while (fgets (buffer, 1024, fh) != NULL)
286         {
287                 long long *val = NULL;
288
289                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
290                         val = &mem_used;
291                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
292                         val = &mem_free;
293                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
294                         val = &mem_buffered;
295                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
296                         val = &mem_cached;
297                 else
298                         continue;
299
300                 numfields = strsplit (buffer, fields, 8);
301
302                 if (numfields < 2)
303                         continue;
304
305                 *val = atoll (fields[1]) * 1024LL;
306         }
307
308         if (fclose (fh))
309         {
310                 char errbuf[1024];
311                 WARNING ("memory: fclose: %s",
312                                 sstrerror (errno, errbuf, sizeof (errbuf)));
313         }
314
315         if (mem_used >= (mem_free + mem_buffered + mem_cached))
316         {
317                 mem_used -= mem_free + mem_buffered + mem_cached;
318                 memory_submit ("used",     mem_used);
319                 memory_submit ("buffered", mem_buffered);
320                 memory_submit ("cached",   mem_cached);
321                 memory_submit ("free",     mem_free);
322         }
323 /* #endif KERNEL_LINUX */
324
325 #elif HAVE_LIBKSTAT
326         long long mem_used;
327         long long mem_free;
328         long long mem_lock;
329
330         if (ksp == NULL)
331                 return (-1);
332
333         mem_used = get_kstat_value (ksp, "pagestotal");
334         mem_free = get_kstat_value (ksp, "pagesfree");
335         mem_lock = get_kstat_value (ksp, "pageslocked");
336
337         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
338                 return (-1);
339         if (mem_used < (mem_free + mem_lock))
340                 return (-1);
341
342         mem_used -= mem_free + mem_lock;
343         mem_used *= pagesize; /* If this overflows you have some serious */
344         mem_free *= pagesize; /* memory.. Why not call me up and give me */
345         mem_lock *= pagesize; /* some? ;) */
346
347         memory_submit ("used",   mem_used);
348         memory_submit ("free",   mem_free);
349         memory_submit ("locked", mem_lock);
350 /* #endif HAVE_LIBKSTAT */
351
352 #elif HAVE_LIBSTATGRAB
353         sg_mem_stats *ios;
354
355         if ((ios = sg_get_mem_stats ()) != NULL)
356         {
357                 memory_submit ("used",   ios->used);
358                 memory_submit ("cached", ios->cache);
359                 memory_submit ("free",   ios->free);
360         }
361 #endif /* HAVE_LIBSTATGRAB */
362
363         return (0);
364 }
365
366 void module_register (void)
367 {
368         plugin_register_init ("memory", memory_init);
369         plugin_register_read ("memory", memory_read);
370 } /* void module_register */