1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005-2010 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
6 * Copyright (C) 2009 Manuel Sanmartin
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Florian octo Forster <octo at verplant.org>
23 * Oleg King <king2 at kaluga.ru>
24 * Simon Kuhnle <simon at blarzwurst.de>
25 * Manuel Sanmartin
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
34 #endif
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
37 #endif
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
40 #endif
41 #if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
43 #endif
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
49 #endif
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
52 #endif
54 #ifdef HAVE_LIBKSTAT
55 # include <sys/sysinfo.h>
56 #endif /* HAVE_LIBKSTAT */
58 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
59 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
60 # ifdef HAVE_SYS_SYSCTL_H
61 # include <sys/sysctl.h>
62 # endif
64 # ifdef HAVE_SYS_DKSTAT_H
65 # include <sys/dkstat.h>
66 # endif
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
69 # define CP_USER 0
70 # define CP_NICE 1
71 # define CP_SYS 2
72 # define CP_INTR 3
73 # define CP_IDLE 4
74 # define CPUSTATES 5
75 # endif
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
78 #if HAVE_SYSCTL
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 # define CAN_USE_SYSCTL 1
82 # else
83 # define CAN_USE_SYSCTL 0
84 # endif
85 #else
86 # define CAN_USE_SYSCTL 0
87 #endif
89 #if HAVE_STATGRAB_H
90 # include <statgrab.h>
91 #endif
93 # ifdef HAVE_PERFSTAT
94 # include <sys/protosw.h>
95 # include <libperfstat.h>
96 # endif /* HAVE_PERFSTAT */
98 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
99 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
100 # error "No applicable input method."
101 #endif
103 #ifdef PROCESSOR_CPU_LOAD_INFO
104 static mach_port_t port_host;
105 static processor_port_array_t cpu_list;
106 static mach_msg_type_number_t cpu_list_len;
107 /* #endif PROCESSOR_CPU_LOAD_INFO */
109 #elif defined(KERNEL_LINUX)
110 /* no variables needed */
111 /* #endif KERNEL_LINUX */
113 #elif defined(HAVE_LIBKSTAT)
114 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
115 # define MAX_NUMCPU 256
116 extern kstat_ctl_t *kc;
117 static kstat_t *ksp[MAX_NUMCPU];
118 static int numcpu;
119 /* #endif HAVE_LIBKSTAT */
121 #elif CAN_USE_SYSCTL
122 static int numcpu;
123 /* #endif CAN_USE_SYSCTL */
125 #elif defined(HAVE_SYSCTLBYNAME)
126 static int numcpu;
127 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
128 static int maxcpu;
129 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
130 /* #endif HAVE_SYSCTLBYNAME */
132 #elif defined(HAVE_LIBSTATGRAB)
133 /* no variables needed */
134 /* #endif HAVE_LIBSTATGRAB */
136 #elif defined(HAVE_PERFSTAT)
137 static perfstat_cpu_t *perfcpu;
138 static int numcpu;
139 static int pnumcpu;
140 #endif /* HAVE_PERFSTAT */
142 static int init (void)
143 {
144 #if PROCESSOR_CPU_LOAD_INFO
145 kern_return_t status;
147 port_host = mach_host_self ();
149 /* FIXME: Free `cpu_list' if it's not NULL */
150 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
151 {
152 ERROR ("cpu plugin: host_processors returned %i", (int) status);
153 cpu_list_len = 0;
154 return (-1);
155 }
157 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
158 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
159 /* #endif PROCESSOR_CPU_LOAD_INFO */
161 #elif defined(HAVE_LIBKSTAT)
162 kstat_t *ksp_chain;
164 numcpu = 0;
166 if (kc == NULL)
167 return (-1);
169 /* Solaris doesn't count linear.. *sigh* */
170 for (numcpu = 0, ksp_chain = kc->kc_chain;
171 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
172 ksp_chain = ksp_chain->ks_next)
173 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
174 ksp[numcpu++] = ksp_chain;
175 /* #endif HAVE_LIBKSTAT */
177 #elif CAN_USE_SYSCTL
178 size_t numcpu_size;
179 int mib[2] = {CTL_HW, HW_NCPU};
180 int status;
182 numcpu = 0;
183 numcpu_size = sizeof (numcpu);
185 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
186 &numcpu, &numcpu_size, NULL, 0);
187 if (status == -1)
188 {
189 char errbuf[1024];
190 WARNING ("cpu plugin: sysctl: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
192 return (-1);
193 }
194 /* #endif CAN_USE_SYSCTL */
196 #elif defined (HAVE_SYSCTLBYNAME)
197 size_t numcpu_size;
199 numcpu_size = sizeof (numcpu);
201 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
202 {
203 char errbuf[1024];
204 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
206 return (-1);
207 }
209 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
210 numcpu_size = sizeof (maxcpu);
212 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
213 {
214 char errbuf[1024];
215 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
217 return (-1);
218 }
219 #else
220 if (numcpu != 1)
221 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
222 #endif
223 /* #endif HAVE_SYSCTLBYNAME */
225 #elif defined(HAVE_LIBSTATGRAB)
226 /* nothing to initialize */
227 /* #endif HAVE_LIBSTATGRAB */
229 #elif defined(HAVE_PERFSTAT)
230 /* nothing to initialize */
231 #endif /* HAVE_PERFSTAT */
233 return (0);
234 } /* int init */
236 static void submit (int cpu_num, const char *type_instance, derive_t value)
237 {
238 value_t values[1];
239 value_list_t vl = VALUE_LIST_INIT;
241 values[0].derive = value;
243 vl.values = values;
244 vl.values_len = 1;
245 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
246 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
247 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
248 "%i", cpu_num);
249 sstrncpy (vl.type, "cpu", sizeof (vl.type));
250 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
252 plugin_dispatch_values (&vl);
253 }
255 static int cpu_read (void)
256 {
257 #if PROCESSOR_CPU_LOAD_INFO
258 int cpu;
260 kern_return_t status;
262 processor_cpu_load_info_data_t cpu_info;
263 mach_msg_type_number_t cpu_info_len;
265 host_t cpu_host;
267 for (cpu = 0; cpu < cpu_list_len; cpu++)
268 {
269 cpu_host = 0;
270 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
272 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
273 (processor_info_t) &cpu_info, &cpu_info_len);
274 if (status != KERN_SUCCESS)
275 {
276 ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
277 mach_error_string (status));
278 continue;
279 }
281 if (cpu_info_len < CPU_STATE_MAX)
282 {
283 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
284 continue;
285 }
287 submit (cpu, "user", (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
288 submit (cpu, "nice", (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
289 submit (cpu, "system", (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
290 submit (cpu, "idle", (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
291 }
292 /* #endif PROCESSOR_CPU_LOAD_INFO */
294 #elif defined(KERNEL_LINUX)
295 int cpu;
296 derive_t user, nice, syst, idle;
297 derive_t wait, intr, sitr; /* sitr == soft interrupt */
298 FILE *fh;
299 char buf[1024];
301 char *fields[9];
302 int numfields;
304 if ((fh = fopen ("/proc/stat", "r")) == NULL)
305 {
306 char errbuf[1024];
307 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
308 sstrerror (errno, errbuf, sizeof (errbuf)));
309 return (-1);
310 }
312 while (fgets (buf, 1024, fh) != NULL)
313 {
314 if (strncmp (buf, "cpu", 3))
315 continue;
316 if ((buf[3] < '0') || (buf[3] > '9'))
317 continue;
319 numfields = strsplit (buf, fields, 9);
320 if (numfields < 5)
321 continue;
323 cpu = atoi (fields[0] + 3);
324 user = atoll (fields[1]);
325 nice = atoll (fields[2]);
326 syst = atoll (fields[3]);
327 idle = atoll (fields[4]);
329 submit (cpu, "user", user);
330 submit (cpu, "nice", nice);
331 submit (cpu, "system", syst);
332 submit (cpu, "idle", idle);
334 if (numfields >= 8)
335 {
336 wait = atoll (fields[5]);
337 intr = atoll (fields[6]);
338 sitr = atoll (fields[7]);
340 submit (cpu, "wait", wait);
341 submit (cpu, "interrupt", intr);
342 submit (cpu, "softirq", sitr);
344 if (numfields >= 9)
345 submit (cpu, "steal", atoll (fields[8]));
346 }
347 }
349 fclose (fh);
350 /* #endif defined(KERNEL_LINUX) */
352 #elif defined(HAVE_LIBKSTAT)
353 int cpu;
354 derive_t user, syst, idle, wait;
355 static cpu_stat_t cs;
357 if (kc == NULL)
358 return (-1);
360 for (cpu = 0; cpu < numcpu; cpu++)
361 {
362 if (kstat_read (kc, ksp[cpu], &cs) == -1)
363 continue; /* error message? */
365 idle = (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
366 user = (derive_t) cs.cpu_sysinfo.cpu[CPU_USER];
367 syst = (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
368 wait = (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
370 submit (ksp[cpu]->ks_instance, "user", user);
371 submit (ksp[cpu]->ks_instance, "system", syst);
372 submit (ksp[cpu]->ks_instance, "idle", idle);
373 submit (ksp[cpu]->ks_instance, "wait", wait);
374 }
375 /* #endif defined(HAVE_LIBKSTAT) */
377 #elif CAN_USE_SYSCTL
378 uint64_t cpuinfo[numcpu][CPUSTATES];
379 size_t cpuinfo_size;
380 int status;
381 int i;
383 if (numcpu < 1)
384 {
385 ERROR ("cpu plugin: Could not determine number of "
386 "installed CPUs using sysctl(3).");
387 return (-1);
388 }
390 memset (cpuinfo, 0, sizeof (cpuinfo));
392 #if defined(KERN_CPTIME2)
393 if (numcpu > 1) {
394 for (i = 0; i < numcpu; i++) {
395 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
397 cpuinfo_size = sizeof (cpuinfo[0]);
399 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
400 cpuinfo[i], &cpuinfo_size, NULL, 0);
401 if (status == -1) {
402 char errbuf[1024];
403 ERROR ("cpu plugin: sysctl failed: %s.",
404 sstrerror (errno, errbuf, sizeof (errbuf)));
405 return (-1);
406 }
407 }
408 }
409 else
410 #endif /* defined(KERN_CPTIME2) */
411 {
412 int mib[] = {CTL_KERN, KERN_CPTIME};
413 long cpuinfo_tmp[CPUSTATES];
415 cpuinfo_size = sizeof(cpuinfo_tmp);
417 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
418 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
419 if (status == -1)
420 {
421 char errbuf[1024];
422 ERROR ("cpu plugin: sysctl failed: %s.",
423 sstrerror (errno, errbuf, sizeof (errbuf)));
424 return (-1);
425 }
427 for(i = 0; i < CPUSTATES; i++) {
428 cpuinfo[0][i] = cpuinfo_tmp[i];
429 }
430 }
432 for (i = 0; i < numcpu; i++) {
433 submit (i, "user", cpuinfo[i][CP_USER]);
434 submit (i, "nice", cpuinfo[i][CP_NICE]);
435 submit (i, "system", cpuinfo[i][CP_SYS]);
436 submit (i, "idle", cpuinfo[i][CP_IDLE]);
437 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
438 }
439 /* #endif CAN_USE_SYSCTL */
440 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
441 long cpuinfo[maxcpu][CPUSTATES];
442 size_t cpuinfo_size;
443 int i;
445 memset (cpuinfo, 0, sizeof (cpuinfo));
447 cpuinfo_size = sizeof (cpuinfo);
448 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
449 {
450 char errbuf[1024];
451 ERROR ("cpu plugin: sysctlbyname failed: %s.",
452 sstrerror (errno, errbuf, sizeof (errbuf)));
453 return (-1);
454 }
456 for (i = 0; i < numcpu; i++) {
457 submit (i, "user", cpuinfo[i][CP_USER]);
458 submit (i, "nice", cpuinfo[i][CP_NICE]);
459 submit (i, "system", cpuinfo[i][CP_SYS]);
460 submit (i, "idle", cpuinfo[i][CP_IDLE]);
461 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
462 }
463 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
464 #elif defined(HAVE_SYSCTLBYNAME)
465 long cpuinfo[CPUSTATES];
466 size_t cpuinfo_size;
468 cpuinfo_size = sizeof (cpuinfo);
470 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
471 {
472 char errbuf[1024];
473 ERROR ("cpu plugin: sysctlbyname failed: %s.",
474 sstrerror (errno, errbuf, sizeof (errbuf)));
475 return (-1);
476 }
478 submit (0, "user", cpuinfo[CP_USER]);
479 submit (0, "nice", cpuinfo[CP_NICE]);
480 submit (0, "system", cpuinfo[CP_SYS]);
481 submit (0, "idle", cpuinfo[CP_IDLE]);
482 submit (0, "interrupt", cpuinfo[CP_INTR]);
483 /* #endif HAVE_SYSCTLBYNAME */
485 #elif defined(HAVE_LIBSTATGRAB)
486 sg_cpu_stats *cs;
487 cs = sg_get_cpu_stats ();
489 if (cs == NULL)
490 {
491 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
492 return (-1);
493 }
495 submit (0, "idle", (derive_t) cs->idle);
496 submit (0, "nice", (derive_t) cs->nice);
497 submit (0, "swap", (derive_t) cs->swap);
498 submit (0, "system", (derive_t) cs->kernel);
499 submit (0, "user", (derive_t) cs->user);
500 submit (0, "wait", (derive_t) cs->iowait);
501 /* #endif HAVE_LIBSTATGRAB */
503 #elif defined(HAVE_PERFSTAT)
504 perfstat_id_t id;
505 int i, cpus;
507 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
508 if(numcpu == -1)
509 {
510 char errbuf[1024];
511 WARNING ("cpu plugin: perfstat_cpu: %s",
512 sstrerror (errno, errbuf, sizeof (errbuf)));
513 return (-1);
514 }
516 if (pnumcpu != numcpu || perfcpu == NULL)
517 {
518 if (perfcpu != NULL)
519 free(perfcpu);
520 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
521 }
522 pnumcpu = numcpu;
524 id.name[0] = '\0';
525 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
526 {
527 char errbuf[1024];
528 WARNING ("cpu plugin: perfstat_cpu: %s",
529 sstrerror (errno, errbuf, sizeof (errbuf)));
530 return (-1);
531 }
533 for (i = 0; i < cpus; i++)
534 {
535 submit (i, "idle", (derive_t) perfcpu[i].idle);
536 submit (i, "system", (derive_t) perfcpu[i].sys);
537 submit (i, "user", (derive_t) perfcpu[i].user);
538 submit (i, "wait", (derive_t) perfcpu[i].wait);
539 }
540 #endif /* HAVE_PERFSTAT */
542 return (0);
543 }
545 void module_register (void)
546 {
547 plugin_register_init ("cpu", init);
548 plugin_register_read ("cpu", cpu_read);
549 } /* void module_register */