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