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