Fixed typo (vm_data vs. vm_stat), included mach/mach_host.h.
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005  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
27 #ifdef HAVE_MACH_KERN_RETURN_H
28 # include <mach/kern_return.h>
29 #endif
30 #ifdef HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
32 #endif
33 #ifdef HAVE_MACH_MACH_HOST_H
34 # include <mach/mach_host.h>
35 #endif
36 #ifdef HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
38 #endif
39 #ifdef MACH_VM_STATISTICS_H
40 # include <mach/vm_statistics.h>
41 #endif
42
43 #if defined (HOST_VM_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
44 # define MEMORY_HAVE_READ 1
45 #else
46 # define MEMORY_HAVE_READ 0
47 #endif
48
49 #define MODULE_NAME "memory"
50
51 static char *memory_file = "memory.rrd";
52
53 /* 9223372036854775807 == LLONG_MAX */
54 static char *ds_def[] =
55 {
56         "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
57         "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
58         "DS:buffers:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
59         "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
60         NULL
61 };
62 static int ds_num = 4;
63
64 /* vm_statistics_data_t */
65 #if defined(HOST_VM_INFO)
66 static mach_port_t port_host;
67 static vm_size_t pagesize;
68 /* #endif HOST_VM_INFO */
69
70 #elif defined(KERNEL_LINUX)
71 /* no global variables */
72 /* #endif KERNEL_LINUX */
73
74 #elif defined(HAVE_LIBKSTAT)
75 static int pagesize;
76 static kstat_t *ksp;
77 #endif /* HAVE_LIBKSTAT */
78
79 static void memory_init (void)
80 {
81 #if defined(HOST_VM_INFO)
82         port_host = mach_host_self ();
83         host_page_size (port_host, &pagesize);
84 /* #endif HOST_VM_INFO */
85
86 #elif defined(KERNEL_LINUX)
87 /* no init stuff */
88 /* #endif KERNEL_LINUX */
89
90 #elif defined(HAVE_LIBKSTAT)
91         /* getpagesize(3C) tells me this does not fail.. */
92         pagesize = getpagesize ();
93         if (get_kstat (&ksp, "unix", 0, "system_pages"))
94                 ksp = NULL;
95 #endif /* HAVE_LIBKSTAT */
96
97         return;
98 }
99
100 static void memory_write (char *host, char *inst, char *val)
101 {
102         rrd_update_file (host, memory_file, val, ds_def, ds_num);
103 }
104
105 #if MEMORY_HAVE_READ
106 #define BUFSIZE 512
107 static void memory_submit (long long mem_used, long long mem_buffered,
108                 long long mem_cached, long long mem_free)
109 {
110         char buf[BUFSIZE];
111
112         if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
113                                 (unsigned int) curtime, mem_used, mem_free,
114                                 mem_buffered, mem_cached) >= BUFSIZE)
115                 return;
116
117         plugin_submit (MODULE_NAME, "-", buf);
118 }
119 #undef BUFSIZE
120
121 static void memory_read (void)
122 {
123 #if defined(HOST_VM_INFO)
124         kern_return_t status;
125         vm_statistics_data_t   vm_data;
126         mach_msg_type_number_t vm_data_len;
127
128         long long wired;
129         long long active;
130         long long inactive;
131         long long free;
132
133         if (!port_host || !pagesize)
134                 return;
135
136         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
137         if ((status = host_statistics (port_host, HOST_VM_INFO,
138                                         (host_info_t) &vm_data,
139                                         &vm_data_len)) != KERN_SUCCESS)
140         {
141                 syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status);
142                 return;
143         }
144
145         /*
146          * From <http://docs.info.apple.com/article.html?artnum=107918>:
147          *
148          * Wired memory
149          *   This information can't be cached to disk, so it must stay in RAM.
150          *   The amount depends on what applications you are using.
151          *
152          * Active memory
153          *   This information is currently in RAM and actively being used.
154          *
155          * Inactive memory
156          *   This information is no longer being used and has been cached to
157          *   disk, but it will remain in RAM until another application needs
158          *   the space. Leaving this information in RAM is to your advantage if
159          *   you (or a client of your computer) come back to it later.
160          *
161          * Free memory
162          *   This memory is not being used.
163          */
164
165         wired    = vm_data.wire_count     * pagesize;
166         active   = vm_data.active_count   * pagesize;
167         inactive = vm_data.inactive_count * pagesize;
168         free     = vm_data.free_count     * pagesize;
169
170         memory_submit (wired + active, -1, inactive, free);
171 /* #endif HOST_VM_INFO */
172
173 #elif defined(KERNEL_LINUX)
174         FILE *fh;
175         char buffer[1024];
176         
177         char *fields[8];
178         int numfields;
179
180         long long mem_used = 0;
181         long long mem_buffered = 0;
182         long long mem_cached = 0;
183         long long mem_free = 0;
184
185         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
186         {
187                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
188                 return;
189         }
190
191         while (fgets (buffer, 1024, fh) != NULL)
192         {
193                 long long *val = NULL;
194
195                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
196                         val = &mem_used;
197                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
198                         val = &mem_free;
199                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
200                         val = &mem_buffered;
201                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
202                         val = &mem_cached;
203                 else
204                         continue;
205
206                 numfields = strsplit (buffer, fields, 8);
207
208                 if (numfields < 2)
209                         continue;
210
211                 *val = atoll (fields[1]) * 1024LL;
212         }
213
214         if (fclose (fh))
215                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
216
217         if (mem_used >= (mem_free + mem_buffered + mem_cached))
218         {
219                 mem_used -= mem_free + mem_buffered + mem_cached;
220                 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
221         }
222 /* #endif defined(KERNEL_LINUX) */
223
224 #elif defined(HAVE_LIBKSTAT)
225         long long mem_used;
226         long long mem_free;
227         long long mem_lock;
228
229         if (ksp == NULL)
230                 return;
231
232         mem_used = get_kstat_value (ksp, "pagestotal");
233         mem_free = get_kstat_value (ksp, "pagesfree");
234         mem_lock = get_kstat_value (ksp, "pageslocked");
235
236         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
237                 return;
238         if (mem_used < (mem_free + mem_lock))
239                 return;
240
241         mem_used -= mem_free + mem_lock;
242         mem_used *= pagesize; /* If this overflows you have some serious */
243         mem_free *= pagesize; /* memory.. Why not call me up and give me */
244         mem_lock *= pagesize; /* some? ;) */
245
246         memory_submit (mem_used, mem_lock, 0LL, mem_free);
247 /* #endif defined(HAVE_LIBKSTAT) */
248
249 #elif defined(HAVE_LIBSTATGRAB)
250         sg_mem_stats *ios;
251
252         if ((ios = sg_get_mem_stats ()) != NULL)
253                 memory_submit (ios->used, 0LL, ios->cache, ios->free);
254 #endif /* HAVE_LIBSTATGRAB */
255 }
256 #else
257 # define memory_read NULL
258 #endif /* MEMORY_HAVE_READ */
259
260 void module_register (void)
261 {
262         plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);
263 }
264
265 #undef MODULE_NAME