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