Fixes so the cpu plugin compiles under Mac OS X
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.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 #include "utils_debug.h"
27
28 #define MODULE_NAME "cpu"
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_HOST_PRIV_H
37 # include <mach/host_priv.h>
38 #endif
39 #ifdef HAVE_MACH_PROCESSOR_INFO_H
40 # include <mach/processor_info.h>
41 #endif
42 #ifdef HAVE_MACH_PROCESSOR_H
43 # include <mach/processor.h>
44 #endif
45
46 #ifdef HAVE_LIBKSTAT
47 # include <sys/sysinfo.h>
48 #endif /* HAVE_LIBKSTAT */
49
50 #ifdef HAVE_SYSCTLBYNAME
51 # ifdef HAVE_SYS_SYSCTL_H
52 #  include <sys/sysctl.h>
53 # endif
54
55 # ifdef HAVE_SYS_DKSTAT_H
56 #  include <sys/dkstat.h>
57 # endif
58
59 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
60 #  define CP_USER   0
61 #  define CP_NICE   1
62 #  define CP_SYS    2
63 #  define CP_INTR   3
64 #  define CP_IDLE   4
65 #  define CPUSTATES 5
66 # endif
67 #endif /* HAVE_SYSCTLBYNAME */
68
69 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
70 # define CPU_HAVE_READ 1
71 #else
72 # define CPU_HAVE_READ 0
73 #endif
74
75 #ifdef PROCESSOR_CPU_LOAD_INFO
76 static mach_port_t port_host;
77 static processor_port_array_t cpu_list;
78 static mach_msg_type_number_t cpu_list_len;
79 /* #endif PROCESSOR_CPU_LOAD_INFO */
80
81 #elif defined(KERNEL_LINUX)
82 /* no variables needed */
83 /* #endif KERNEL_LINUX */
84
85 #elif defined(HAVE_LIBKSTAT)
86 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
87 # define MAX_NUMCPU 256
88 extern kstat_ctl_t *kc;
89 static kstat_t *ksp[MAX_NUMCPU];
90 static int numcpu;
91 /* #endif HAVE_LIBKSTAT */
92
93 #elif defined(HAVE_SYSCTLBYNAME)
94 static int numcpu;
95 #endif /* HAVE_SYSCTLBYNAME */
96
97 static char *cpu_filename = "cpu-%s.rrd";
98
99 static char *ds_def[] =
100 {
101         "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:U",
102         "DS:nice:COUNTER:"COLLECTD_HEARTBEAT":0:U",
103         "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:U",
104         "DS:idle:COUNTER:"COLLECTD_HEARTBEAT":0:U",
105         "DS:wait:COUNTER:"COLLECTD_HEARTBEAT":0:U",
106         NULL
107 };
108 static int ds_num = 5;
109
110 static void cpu_init (void)
111 {
112 #ifdef PROCESSOR_CPU_LOAD_INFO
113         kern_return_t status;
114
115         port_host = mach_host_self ();
116
117         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
118         {
119                 syslog (LOG_ERR, "cpu-plugin: host_processors returned %i\n", (int) status);
120                 cpu_list_len = 0;
121                 return;
122         }
123
124         DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
125         syslog (LOG_INFO, "cpu-plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
126 /* #endif PROCESSOR_CPU_LOAD_INFO */
127
128 #elif defined(HAVE_LIBKSTAT)
129         kstat_t *ksp_chain;
130
131         numcpu = 0;
132
133         if (kc == NULL)
134                 return;
135
136         /* Solaris doesn't count linear.. *sigh* */
137         for (numcpu = 0, ksp_chain = kc->kc_chain;
138                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
139                         ksp_chain = ksp_chain->ks_next)
140                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
141                         ksp[numcpu++] = ksp_chain;
142 /* #endif HAVE_LIBKSTAT */
143
144 #elif defined (HAVE_SYSCTLBYNAME)
145         size_t numcpu_size;
146
147         numcpu_size = sizeof (numcpu);
148
149         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
150         {
151                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
152                 return;
153         }
154
155         if (numcpu != 1)
156                 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
157 #endif
158
159         return;
160 }
161
162 static void cpu_write (char *host, char *inst, char *val)
163 {
164         char file[512];
165         int status;
166
167         status = snprintf (file, 512, cpu_filename, inst);
168         if (status < 1)
169                 return;
170         else if (status >= 512)
171                 return;
172
173         rrd_update_file (host, file, val, ds_def, ds_num);
174 }
175
176 #if CPU_HAVE_READ
177 #define BUFSIZE 512
178 static void cpu_submit (int cpu_num, unsigned long long user,
179                 unsigned long long nice, unsigned long long syst,
180                 unsigned long long idle, unsigned long long wait)
181 {
182         char buf[BUFSIZE];
183         char cpu[16];
184
185         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
186                                 user, nice, syst, idle, wait) >= BUFSIZE)
187                 return;
188         snprintf (cpu, 16, "%i", cpu_num);
189
190         plugin_submit (MODULE_NAME, cpu, buf);
191 }
192 #undef BUFSIZE
193
194 static void cpu_read (void)
195 {
196 #ifdef PROCESSOR_CPU_LOAD_INFO
197         int cpu;
198
199         kern_return_t status;
200         
201         processor_cpu_load_info_data_t cpu_info;
202         processor_cpu_load_info_t      cpu_info_ptr;
203         mach_msg_type_number_t         cpu_info_len;
204
205         host_t cpu_host;
206
207         for (cpu = 0; cpu < cpu_list_len; cpu++)
208         {
209                 cpu_host = 0;
210                 cpu_info_ptr = &cpu_info;
211                 cpu_info_len = sizeof (cpu_info);
212
213                 if ((status = processor_info (cpu_list[cpu],
214                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
215                                                 (processor_info_t) cpu_info_ptr, &cpu_info_len)) != KERN_SUCCESS)
216                 {
217                         syslog (LOG_ERR, "processor_info failed with status %i\n", (int) status);
218                         continue;
219                 }
220
221                 if (cpu_info_len < CPU_STATE_MAX)
222                 {
223                         syslog (LOG_ERR, "processor_info returned only %i elements..\n", cpu_info_len);
224                         continue;
225                 }
226
227                 cpu_submit (cpu, cpu_info.cpu_ticks[CPU_STATE_USER],
228                                 cpu_info.cpu_ticks[CPU_STATE_NICE],
229                                 cpu_info.cpu_ticks[CPU_STATE_SYSTEM],
230                                 cpu_info.cpu_ticks[CPU_STATE_IDLE],
231                                 0ULL);
232         }
233 /* #endif PROCESSOR_CPU_LOAD_INFO */
234
235 #elif defined(KERNEL_LINUX)
236 # define BUFSIZE 1024
237         int cpu;
238         unsigned long long user, nice, syst, idle;
239         unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
240         FILE *fh;
241         char buf[BUFSIZE];
242
243         char *fields[9];
244         int numfields;
245
246         if ((fh = fopen ("/proc/stat", "r")) == NULL)
247         {
248                 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
249                 return;
250         }
251
252         while (fgets (buf, BUFSIZE, fh) != NULL)
253         {
254                 if (strncmp (buf, "cpu", 3))
255                         continue;
256                 if ((buf[3] < '0') || (buf[3] > '9'))
257                         continue;
258
259                 numfields = strsplit (buf, fields, 9);
260                 if (numfields < 5)
261                         continue;
262
263                 cpu = atoi (fields[0] + 3);
264                 user = atoll (fields[1]);
265                 nice = atoll (fields[2]);
266                 syst = atoll (fields[3]);
267                 idle = atoll (fields[4]);
268
269                 if (numfields >= 8)
270                 {
271                         wait = atoll (fields[5]);
272                         intr = atoll (fields[6]);
273                         sitr = atoll (fields[7]);
274
275                         /* I doubt anyone cares about the time spent in
276                          * interrupt handlers.. */
277                         syst += intr + sitr;
278                 }
279                 else
280                 {
281                         wait = 0LL;
282                 }
283
284                 cpu_submit (cpu, user, nice, syst, idle, wait);
285         }
286
287         fclose (fh);
288 #undef BUFSIZE
289 /* #endif defined(KERNEL_LINUX) */
290
291 #elif defined(HAVE_LIBKSTAT)
292         int cpu;
293         unsigned long long user, syst, idle, wait;
294         static cpu_stat_t cs;
295
296         if (kc == NULL)
297                 return;
298
299         for (cpu = 0; cpu < numcpu; cpu++)
300         {
301                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
302                         continue; /* error message? */
303
304                 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
305                 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
306                 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
307                 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
308
309                 cpu_submit (ksp[cpu]->ks_instance,
310                                 user, 0LL, syst, idle, wait);
311         }
312 /* #endif defined(HAVE_LIBKSTAT) */
313
314 #elif defined(HAVE_SYSCTLBYNAME)
315         long cpuinfo[CPUSTATES];
316         size_t cpuinfo_size;
317
318         cpuinfo_size = sizeof (cpuinfo);
319
320         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
321         {
322                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
323                 return;
324         }
325
326         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
327
328         /* FIXME: Instance is always `0' */
329         cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
330 #endif
331
332         return;
333 }
334 #else
335 # define cpu_read NULL
336 #endif /* CPU_HAVE_READ */
337
338 void module_register (void)
339 {
340         plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
341 }
342
343 #undef MODULE_NAME