Merge branch 'pull/collectd-4'
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2007  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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_debug.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_HOST_PRIV_H
34 # include <mach/host_priv.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 #  include <mach/mach_error.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 #ifdef HAVE_MACH_VM_MAP_H
46 # include <mach/vm_map.h>
47 #endif
48
49 #ifdef HAVE_LIBKSTAT
50 # include <sys/sysinfo.h>
51 #endif /* HAVE_LIBKSTAT */
52
53 #ifdef HAVE_SYSCTLBYNAME
54 # ifdef HAVE_SYS_SYSCTL_H
55 #  include <sys/sysctl.h>
56 # endif
57
58 # ifdef HAVE_SYS_DKSTAT_H
59 #  include <sys/dkstat.h>
60 # endif
61
62 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
63 #  define CP_USER   0
64 #  define CP_NICE   1
65 #  define CP_SYS    2
66 #  define CP_INTR   3
67 #  define CP_IDLE   4
68 #  define CPUSTATES 5
69 # endif
70 #endif /* HAVE_SYSCTLBYNAME */
71
72 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
73 # define CPU_HAVE_READ 1
74 #else
75 # define CPU_HAVE_READ 0
76 #endif
77
78 static data_source_t dsrc[1] =
79 {
80         {"value", DS_TYPE_COUNTER, 0, 4294967295.0}
81 };
82
83 static data_set_t ds =
84 {
85         "cpu", 1, dsrc
86 };
87
88 #if CPU_HAVE_READ
89 #ifdef PROCESSOR_CPU_LOAD_INFO
90 static mach_port_t port_host;
91 static processor_port_array_t cpu_list;
92 static mach_msg_type_number_t cpu_list_len;
93
94 #if PROCESSOR_TEMPERATURE
95 static int cpu_temp_retry_counter = 0;
96 static int cpu_temp_retry_step    = 1;
97 static int cpu_temp_retry_max     = 1;
98 #endif /* PROCESSOR_TEMPERATURE */
99 /* #endif PROCESSOR_CPU_LOAD_INFO */
100
101 #elif defined(KERNEL_LINUX)
102 /* no variables needed */
103 /* #endif KERNEL_LINUX */
104
105 #elif defined(HAVE_LIBKSTAT)
106 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
107 # define MAX_NUMCPU 256
108 extern kstat_ctl_t *kc;
109 static kstat_t *ksp[MAX_NUMCPU];
110 static int numcpu;
111 /* #endif HAVE_LIBKSTAT */
112
113 #elif defined(HAVE_SYSCTLBYNAME)
114 static int numcpu;
115 #endif /* HAVE_SYSCTLBYNAME */
116
117 static int init (void)
118 {
119 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
120         kern_return_t status;
121         int collectd_step;
122
123         port_host = mach_host_self ();
124
125         /* FIXME: Free `cpu_list' if it's not NULL */
126         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
127         {
128                 syslog (LOG_ERR, "cpu plugin: host_processors returned %i", (int) status);
129                 cpu_list_len = 0;
130                 return (-1);
131         }
132
133         DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
134         syslog (LOG_INFO, "cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
135
136         collectd_step = atoi (COLLECTD_STEP);
137         if ((collectd_step > 0) && (collectd_step <= 86400))
138                 cpu_temp_retry_max = 86400 / collectd_step;
139                 
140 /* #endif PROCESSOR_CPU_LOAD_INFO */
141
142 #elif defined(HAVE_LIBKSTAT)
143         kstat_t *ksp_chain;
144
145         numcpu = 0;
146
147         if (kc == NULL)
148                 return (-1);
149
150         /* Solaris doesn't count linear.. *sigh* */
151         for (numcpu = 0, ksp_chain = kc->kc_chain;
152                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
153                         ksp_chain = ksp_chain->ks_next)
154                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
155                         ksp[numcpu++] = ksp_chain;
156 /* #endif HAVE_LIBKSTAT */
157
158 #elif defined (HAVE_SYSCTLBYNAME)
159         size_t numcpu_size;
160
161         numcpu_size = sizeof (numcpu);
162
163         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
164         {
165                 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
166                 return (-1);
167         }
168
169         if (numcpu != 1)
170                 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
171 #endif
172
173         return (0);
174 } /* int init */
175
176 static void submit (int cpu_num, const char *type_instance, counter_t value)
177 {
178         value_t values[1];
179         value_list_t vl = VALUE_LIST_INIT;
180
181         values[0].counter = value;
182
183         vl.values = values;
184         vl.values_len = 1;
185         vl.time = time (NULL);
186         strcpy (vl.host, hostname);
187         strcpy (vl.plugin, "cpu");
188         snprintf (vl.plugin_instance, sizeof (vl.type_instance),
189                         "%i", cpu_num);
190         vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
191         strcpy (vl.type_instance, type_instance);
192
193         plugin_dispatch_values ("cpu", &vl);
194 }
195
196 static int cpu_read (void)
197 {
198 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
199         int cpu;
200
201         kern_return_t status;
202         
203 #if PROCESSOR_CPU_LOAD_INFO
204         processor_cpu_load_info_data_t cpu_info;
205         mach_msg_type_number_t         cpu_info_len;
206 #endif
207 #if PROCESSOR_TEMPERATURE
208         processor_info_data_t          cpu_temp;
209         mach_msg_type_number_t         cpu_temp_len;
210 #endif
211
212         host_t cpu_host;
213
214         for (cpu = 0; cpu < cpu_list_len; cpu++)
215         {
216 #if PROCESSOR_CPU_LOAD_INFO
217                 cpu_host = 0;
218                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
219
220                 if ((status = processor_info (cpu_list[cpu],
221                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
222                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
223                 {
224                         syslog (LOG_ERR, "cpu plugin: processor_info failed with status %i", (int) status);
225                         continue;
226                 }
227
228                 if (cpu_info_len < CPU_STATE_MAX)
229                 {
230                         syslog (LOG_ERR, "cpu plugin: processor_info returned only %i elements..", cpu_info_len);
231                         continue;
232                 }
233
234                 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
235                 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
236                 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
237                 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
238 #endif /* PROCESSOR_CPU_LOAD_INFO */
239 #if PROCESSOR_TEMPERATURE
240                 /*
241                  * Not all Apple computers do have this ability. To minimize
242                  * the messages sent to the syslog we do an exponential
243                  * stepback if `processor_info' fails. We still try ~once a day
244                  * though..
245                  */
246                 if (cpu_temp_retry_counter > 0)
247                 {
248                         cpu_temp_retry_counter--;
249                         continue;
250                 }
251
252                 cpu_temp_len = PROCESSOR_INFO_MAX;
253
254                 status = processor_info (cpu_list[cpu],
255                                 PROCESSOR_TEMPERATURE,
256                                 &cpu_host,
257                                 cpu_temp, &cpu_temp_len);
258                 if (status != KERN_SUCCESS)
259                 {
260                         syslog (LOG_ERR, "cpu plugin: processor_info failed: %s",
261                                         mach_error_string (status));
262
263                         cpu_temp_retry_counter = cpu_temp_retry_step;
264                         cpu_temp_retry_step *= 2;
265                         if (cpu_temp_retry_step > cpu_temp_retry_max)
266                                 cpu_temp_retry_step = cpu_temp_retry_max;
267
268                         continue;
269                 }
270
271                 if (cpu_temp_len != 1)
272                 {
273                         DBG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
274                                         (int) cpu_temp_len);
275                         continue;
276                 }
277
278                 cpu_temp_retry_counter = 0;
279                 cpu_temp_retry_step    = 1;
280
281                 DBG ("cpu_temp = %i", (int) cpu_temp);
282 #endif /* PROCESSOR_TEMPERATURE */
283         }
284 /* #endif PROCESSOR_CPU_LOAD_INFO */
285
286 #elif defined(KERNEL_LINUX)
287         int cpu;
288         counter_t user, nice, syst, idle;
289         counter_t wait, intr, sitr; /* sitr == soft interrupt */
290         FILE *fh;
291         char buf[1024];
292
293         char *fields[9];
294         int numfields;
295
296         static complain_t complain_obj;
297
298         if ((fh = fopen ("/proc/stat", "r")) == NULL)
299         {
300                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
301                                 "fopen (/proc/stat) failed: %s",
302                                 strerror (errno));
303                 return (-1);
304         }
305
306         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
307                         "fopen (/proc/stat) succeeded.");
308
309         while (fgets (buf, 1024, fh) != NULL)
310         {
311                 if (strncmp (buf, "cpu", 3))
312                         continue;
313                 if ((buf[3] < '0') || (buf[3] > '9'))
314                         continue;
315
316                 numfields = strsplit (buf, fields, 9);
317                 if (numfields < 5)
318                         continue;
319
320                 cpu = atoi (fields[0] + 3);
321                 user = atoll (fields[1]);
322                 nice = atoll (fields[2]);
323                 syst = atoll (fields[3]);
324                 idle = atoll (fields[4]);
325
326                 submit (cpu, "user", user);
327                 submit (cpu, "nice", nice);
328                 submit (cpu, "system", syst);
329                 submit (cpu, "idle", idle);
330
331                 if (numfields >= 8)
332                 {
333                         wait = atoll (fields[5]);
334                         intr = atoll (fields[6]);
335                         sitr = atoll (fields[7]);
336
337                         submit (cpu, "wait", wait);
338                         submit (cpu, "interrupt", intr);
339                         submit (cpu, "softirq", sitr);
340                 }
341         }
342
343         fclose (fh);
344 /* #endif defined(KERNEL_LINUX) */
345
346 #elif defined(HAVE_LIBKSTAT)
347         int cpu;
348         counter_t user, syst, idle, wait;
349         static cpu_stat_t cs;
350
351         if (kc == NULL)
352                 return;
353
354         for (cpu = 0; cpu < numcpu; cpu++)
355         {
356                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
357                         continue; /* error message? */
358
359                 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
360                 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
361                 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
362                 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
363
364                 submit (ksp[cpu]->ks_instance, "user", user);
365                 submit (ksp[cpu]->ks_instance, "system", syst);
366                 submit (ksp[cpu]->ks_instance, "idle", idle);
367                 submit (ksp[cpu]->ks_instance, "wait", wait);
368         }
369 /* #endif defined(HAVE_LIBKSTAT) */
370
371 #elif defined(HAVE_SYSCTLBYNAME)
372         long cpuinfo[CPUSTATES];
373         size_t cpuinfo_size;
374
375         static complain_t complain_obj;
376
377         cpuinfo_size = sizeof (cpuinfo);
378
379         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
380         {
381                 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
382                                 "sysctlbyname failed: %s.",
383                                 strerror (errno));
384                 return;
385         }
386
387         plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
388                         "sysctlbyname succeeded.");
389
390         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
391
392         submit (0, "user", cpuinfo[CP_USER]);
393         submit (0, "nice", cpuinfo[CP_NICE]);
394         submit (0, "system", cpuinfo[CP_SYS]);
395         submit (0, "idle", cpuinfo[CP_IDLE]);
396 #endif
397
398         return (0);
399 }
400 #endif /* CPU_HAVE_READ */
401
402 void module_register (void)
403 {
404         plugin_register_data_set (&ds);
405
406 #if CPU_HAVE_READ
407         plugin_register_init ("cpu", init);
408         plugin_register_read ("cpu", cpu_read);
409 #endif /* CPU_HAVE_READ */
410 }