1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Oleg King <king2 at kaluga.ru>
23 * Simon Kuhnle <simon at blarzwurst.de>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
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
52 #ifdef HAVE_LIBKSTAT
53 # include <sys/sysinfo.h>
54 #endif /* HAVE_LIBKSTAT */
56 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
57 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
58 # ifdef HAVE_SYS_SYSCTL_H
59 # include <sys/sysctl.h>
60 # endif
62 # ifdef HAVE_SYS_DKSTAT_H
63 # include <sys/dkstat.h>
64 # endif
66 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
67 # define CP_USER 0
68 # define CP_NICE 1
69 # define CP_SYS 2
70 # define CP_INTR 3
71 # define CP_IDLE 4
72 # define CPUSTATES 5
73 # endif
74 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
76 #if HAVE_SYSCTL
77 # if defined(CTL_HW) && defined(HW_NCPU) \
78 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
79 # define CAN_USE_SYSCTL 1
80 # else
81 # define CAN_USE_SYSCTL 0
82 # endif
83 #else
84 # define CAN_USE_SYSCTL 0
85 #endif
87 #if HAVE_STATGRAB_H
88 # include <statgrab.h>
89 #endif
91 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
92 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
93 # error "No applicable input method."
94 #endif
96 #ifdef PROCESSOR_CPU_LOAD_INFO
97 static mach_port_t port_host;
98 static processor_port_array_t cpu_list;
99 static mach_msg_type_number_t cpu_list_len;
101 #if PROCESSOR_TEMPERATURE
102 static int cpu_temp_retry_counter = 0;
103 static int cpu_temp_retry_step = 1;
104 static int cpu_temp_retry_max = 1;
105 #endif /* PROCESSOR_TEMPERATURE */
106 /* #endif PROCESSOR_CPU_LOAD_INFO */
108 #elif defined(KERNEL_LINUX)
109 /* no variables needed */
110 /* #endif KERNEL_LINUX */
112 #elif defined(HAVE_LIBKSTAT)
113 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
114 # define MAX_NUMCPU 256
115 extern kstat_ctl_t *kc;
116 static kstat_t *ksp[MAX_NUMCPU];
117 static int numcpu;
118 /* #endif HAVE_LIBKSTAT */
120 #elif CAN_USE_SYSCTL
121 static int numcpu;
122 /* #endif CAN_USE_SYSCTL */
124 #elif defined(HAVE_SYSCTLBYNAME)
125 static int numcpu;
126 /* #endif HAVE_SYSCTLBYNAME */
128 #elif defined(HAVE_LIBSTATGRAB)
129 /* no variables needed */
130 #endif /* HAVE_LIBSTATGRAB */
132 static int init (void)
133 {
134 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
135 kern_return_t status;
137 port_host = mach_host_self ();
139 /* FIXME: Free `cpu_list' if it's not NULL */
140 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
141 {
142 ERROR ("cpu plugin: host_processors returned %i", (int) status);
143 cpu_list_len = 0;
144 return (-1);
145 }
147 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
148 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
150 cpu_temp_retry_max = 86400 / interval_g;
151 /* #endif PROCESSOR_CPU_LOAD_INFO */
153 #elif defined(HAVE_LIBKSTAT)
154 kstat_t *ksp_chain;
156 numcpu = 0;
158 if (kc == NULL)
159 return (-1);
161 /* Solaris doesn't count linear.. *sigh* */
162 for (numcpu = 0, ksp_chain = kc->kc_chain;
163 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
164 ksp_chain = ksp_chain->ks_next)
165 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
166 ksp[numcpu++] = ksp_chain;
167 /* #endif HAVE_LIBKSTAT */
169 #elif CAN_USE_SYSCTL
170 size_t numcpu_size;
171 int mib[2] = {CTL_HW, HW_NCPU};
172 int status;
174 numcpu = 0;
175 numcpu_size = sizeof (numcpu);
177 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
178 &numcpu, &numcpu_size, NULL, 0);
179 if (status == -1)
180 {
181 char errbuf[1024];
182 WARNING ("cpu plugin: sysctl: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
184 return (-1);
185 }
186 /* #endif CAN_USE_SYSCTL */
188 #elif defined (HAVE_SYSCTLBYNAME)
189 size_t numcpu_size;
191 numcpu_size = sizeof (numcpu);
193 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
194 {
195 char errbuf[1024];
196 WARNING ("cpu plugin: sysctlbyname: %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
198 return (-1);
199 }
201 #ifndef HAVE_SYSCTL_KERN_CP_TIMES
202 if (numcpu != 1)
203 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
204 #endif
205 /* #endif HAVE_SYSCTLBYNAME */
207 #elif defined(HAVE_LIBSTATGRAB)
208 /* nothing to initialize */
209 #endif /* HAVE_LIBSTATGRAB */
211 return (0);
212 } /* int init */
214 static void submit (int cpu_num, const char *type_instance, counter_t value)
215 {
216 value_t values[1];
217 value_list_t vl = VALUE_LIST_INIT;
219 values[0].counter = value;
221 vl.values = values;
222 vl.values_len = 1;
223 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
224 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
225 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
226 "%i", cpu_num);
227 sstrncpy (vl.type, "cpu", sizeof (vl.type));
228 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
230 plugin_dispatch_values (&vl);
231 }
233 static int cpu_read (void)
234 {
235 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
236 int cpu;
238 kern_return_t status;
240 #if PROCESSOR_CPU_LOAD_INFO
241 processor_cpu_load_info_data_t cpu_info;
242 mach_msg_type_number_t cpu_info_len;
243 #endif
244 #if PROCESSOR_TEMPERATURE
245 processor_info_data_t cpu_temp;
246 mach_msg_type_number_t cpu_temp_len;
247 #endif
249 host_t cpu_host;
251 for (cpu = 0; cpu < cpu_list_len; cpu++)
252 {
253 #if PROCESSOR_CPU_LOAD_INFO
254 cpu_host = 0;
255 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
257 if ((status = processor_info (cpu_list[cpu],
258 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
259 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
260 {
261 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
262 continue;
263 }
265 if (cpu_info_len < CPU_STATE_MAX)
266 {
267 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
268 continue;
269 }
271 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
272 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
273 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
274 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
275 #endif /* PROCESSOR_CPU_LOAD_INFO */
276 #if PROCESSOR_TEMPERATURE
277 /*
278 * Not all Apple computers do have this ability. To minimize
279 * the messages sent to the syslog we do an exponential
280 * stepback if `processor_info' fails. We still try ~once a day
281 * though..
282 */
283 if (cpu_temp_retry_counter > 0)
284 {
285 cpu_temp_retry_counter--;
286 continue;
287 }
289 cpu_temp_len = PROCESSOR_INFO_MAX;
291 status = processor_info (cpu_list[cpu],
292 PROCESSOR_TEMPERATURE,
293 &cpu_host,
294 cpu_temp, &cpu_temp_len);
295 if (status != KERN_SUCCESS)
296 {
297 ERROR ("cpu plugin: processor_info failed: %s",
298 mach_error_string (status));
300 cpu_temp_retry_counter = cpu_temp_retry_step;
301 cpu_temp_retry_step *= 2;
302 if (cpu_temp_retry_step > cpu_temp_retry_max)
303 cpu_temp_retry_step = cpu_temp_retry_max;
305 continue;
306 }
308 if (cpu_temp_len != 1)
309 {
310 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
311 (int) cpu_temp_len);
312 continue;
313 }
315 cpu_temp_retry_counter = 0;
316 cpu_temp_retry_step = 1;
318 DEBUG ("cpu_temp = %i", (int) cpu_temp);
319 #endif /* PROCESSOR_TEMPERATURE */
320 }
321 /* #endif PROCESSOR_CPU_LOAD_INFO */
323 #elif defined(KERNEL_LINUX)
324 int cpu;
325 counter_t user, nice, syst, idle;
326 counter_t wait, intr, sitr; /* sitr == soft interrupt */
327 FILE *fh;
328 char buf[1024];
330 char *fields[9];
331 int numfields;
333 if ((fh = fopen ("/proc/stat", "r")) == NULL)
334 {
335 char errbuf[1024];
336 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
337 sstrerror (errno, errbuf, sizeof (errbuf)));
338 return (-1);
339 }
341 while (fgets (buf, 1024, fh) != NULL)
342 {
343 if (strncmp (buf, "cpu", 3))
344 continue;
345 if ((buf[3] < '0') || (buf[3] > '9'))
346 continue;
348 numfields = strsplit (buf, fields, 9);
349 if (numfields < 5)
350 continue;
352 cpu = atoi (fields[0] + 3);
353 user = atoll (fields[1]);
354 nice = atoll (fields[2]);
355 syst = atoll (fields[3]);
356 idle = atoll (fields[4]);
358 submit (cpu, "user", user);
359 submit (cpu, "nice", nice);
360 submit (cpu, "system", syst);
361 submit (cpu, "idle", idle);
363 if (numfields >= 8)
364 {
365 wait = atoll (fields[5]);
366 intr = atoll (fields[6]);
367 sitr = atoll (fields[7]);
369 submit (cpu, "wait", wait);
370 submit (cpu, "interrupt", intr);
371 submit (cpu, "softirq", sitr);
373 if (numfields >= 9)
374 submit (cpu, "steal", atoll (fields[8]));
375 }
376 }
378 fclose (fh);
379 /* #endif defined(KERNEL_LINUX) */
381 #elif defined(HAVE_LIBKSTAT)
382 int cpu;
383 counter_t user, syst, idle, wait;
384 static cpu_stat_t cs;
386 if (kc == NULL)
387 return (-1);
389 for (cpu = 0; cpu < numcpu; cpu++)
390 {
391 if (kstat_read (kc, ksp[cpu], &cs) == -1)
392 continue; /* error message? */
394 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
395 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
396 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
397 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
399 submit (ksp[cpu]->ks_instance, "user", user);
400 submit (ksp[cpu]->ks_instance, "system", syst);
401 submit (ksp[cpu]->ks_instance, "idle", idle);
402 submit (ksp[cpu]->ks_instance, "wait", wait);
403 }
404 /* #endif defined(HAVE_LIBKSTAT) */
406 #elif CAN_USE_SYSCTL
407 uint64_t cpuinfo[numcpu][CPUSTATES];
408 size_t cpuinfo_size;
409 int status;
410 int i;
412 if (numcpu < 1)
413 {
414 ERROR ("cpu plugin: Could not determine number of "
415 "installed CPUs using sysctl(3).");
416 return (-1);
417 }
419 memset (cpuinfo, 0, sizeof (cpuinfo));
421 #if defined(KERN_CPTIME2)
422 if (numcpu > 1) {
423 for (i = 0; i < numcpu; i++) {
424 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
426 cpuinfo_size = sizeof (cpuinfo[0]);
428 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
429 cpuinfo[i], &cpuinfo_size, NULL, 0);
430 if (status == -1) {
431 char errbuf[1024];
432 ERROR ("cpu plugin: sysctl failed: %s.",
433 sstrerror (errno, errbuf, sizeof (errbuf)));
434 return (-1);
435 }
436 }
437 }
438 else
439 #endif /* defined(KERN_CPTIME2) */
440 {
441 int mib[] = {CTL_KERN, KERN_CPTIME};
442 long cpuinfo_tmp[CPUSTATES];
444 cpuinfo_size = sizeof(cpuinfo_tmp);
446 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
447 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
448 if (status == -1)
449 {
450 char errbuf[1024];
451 ERROR ("cpu plugin: sysctl failed: %s.",
452 sstrerror (errno, errbuf, sizeof (errbuf)));
453 return (-1);
454 }
456 for(i = 0; i < CPUSTATES; i++) {
457 cpuinfo[0][i] = cpuinfo_tmp[i];
458 }
459 }
461 for (i = 0; i < numcpu; i++) {
462 submit (i, "user", cpuinfo[i][CP_USER]);
463 submit (i, "nice", cpuinfo[i][CP_NICE]);
464 submit (i, "system", cpuinfo[i][CP_SYS]);
465 submit (i, "idle", cpuinfo[i][CP_IDLE]);
466 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
467 }
468 /* #endif CAN_USE_SYSCTL */
469 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
470 long cpuinfo[numcpu][CPUSTATES];
471 size_t cpuinfo_size;
472 int i;
474 memset (cpuinfo, 0, sizeof (cpuinfo));
476 cpuinfo_size = sizeof (cpuinfo);
477 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
478 {
479 char errbuf[1024];
480 ERROR ("cpu plugin: sysctlbyname failed: %s.",
481 sstrerror (errno, errbuf, sizeof (errbuf)));
482 return (-1);
483 }
485 for (i = 0; i < numcpu; i++) {
486 submit (i, "user", cpuinfo[i][CP_USER]);
487 submit (i, "nice", cpuinfo[i][CP_NICE]);
488 submit (i, "system", cpuinfo[i][CP_SYS]);
489 submit (i, "idle", cpuinfo[i][CP_IDLE]);
490 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
491 }
492 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
493 #elif defined(HAVE_SYSCTLBYNAME)
494 long cpuinfo[CPUSTATES];
495 size_t cpuinfo_size;
497 cpuinfo_size = sizeof (cpuinfo);
499 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
500 {
501 char errbuf[1024];
502 ERROR ("cpu plugin: sysctlbyname failed: %s.",
503 sstrerror (errno, errbuf, sizeof (errbuf)));
504 return (-1);
505 }
507 submit (0, "user", cpuinfo[CP_USER]);
508 submit (0, "nice", cpuinfo[CP_NICE]);
509 submit (0, "system", cpuinfo[CP_SYS]);
510 submit (0, "idle", cpuinfo[CP_IDLE]);
511 submit (0, "interrupt", cpuinfo[CP_INTR]);
512 /* #endif HAVE_SYSCTLBYNAME */
514 #elif defined(HAVE_LIBSTATGRAB)
515 sg_cpu_stats *cs;
516 cs = sg_get_cpu_stats ();
518 if (cs == NULL)
519 {
520 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
521 return (-1);
522 }
524 submit (0, "idle", (counter_t) cs->idle);
525 submit (0, "nice", (counter_t) cs->nice);
526 submit (0, "swap", (counter_t) cs->swap);
527 submit (0, "system", (counter_t) cs->kernel);
528 submit (0, "user", (counter_t) cs->user);
529 submit (0, "wait", (counter_t) cs->iowait);
530 #endif /* HAVE_LIBSTATGRAB */
532 return (0);
533 }
535 void module_register (void)
536 {
537 plugin_register_init ("cpu", init);
538 plugin_register_read ("cpu", cpu_read);
539 } /* void module_register */