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