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
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.
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.
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
22 * Florian octo Forster <octo at verplant.org>
23 * Oleg King <king2 at kaluga.ru>
24 * Simon Kuhnle <simon at blarzwurst.de>
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
41 #if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
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>
64 # ifdef HAVE_SYS_DKSTAT_H
65 # include <sys/dkstat.h>
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 # define CAN_USE_SYSCTL 1
83 # define CAN_USE_SYSCTL 0
86 # define CAN_USE_SYSCTL 0
89 #define CPU_SUBMIT_USER 1
90 #define CPU_SUBMIT_SYSTEM 2
91 #define CPU_SUBMIT_WAIT 3
92 #define CPU_SUBMIT_NICE 4
93 #define CPU_SUBMIT_SWAP 5
94 #define CPU_SUBMIT_INTERRUPT 6
95 #define CPU_SUBMIT_SOFTIRQ 7
96 #define CPU_SUBMIT_STEAL 8
97 #define CPU_SUBMIT_IDLE 9
98 #define CPU_SUBMIT_ACTIVE 10
99 #define CPU_SUBMIT_MAX 11
100 #define CPU_SUBMIT_FLUSH -1
103 # include <statgrab.h>
106 # ifdef HAVE_PERFSTAT
107 # include <sys/protosw.h>
108 # include <libperfstat.h>
109 # endif /* HAVE_PERFSTAT */
111 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
112 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
113 # error "No applicable input method."
116 static const char *cpu_state_names[] = {
130 #ifdef PROCESSOR_CPU_LOAD_INFO
131 static mach_port_t port_host;
132 static processor_port_array_t cpu_list;
133 static mach_msg_type_number_t cpu_list_len;
135 #if PROCESSOR_TEMPERATURE
136 static int cpu_temp_retry_counter = 0;
137 static int cpu_temp_retry_step = 1;
138 static int cpu_temp_retry_max = 1;
139 #endif /* PROCESSOR_TEMPERATURE */
140 /* #endif PROCESSOR_CPU_LOAD_INFO */
142 #elif defined(KERNEL_LINUX)
143 /* no variables needed */
144 /* #endif KERNEL_LINUX */
146 #elif defined(HAVE_LIBKSTAT)
147 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
148 # define MAX_NUMCPU 256
149 extern kstat_ctl_t *kc;
150 static kstat_t *ksp[MAX_NUMCPU];
152 /* #endif HAVE_LIBKSTAT */
156 /* #endif CAN_USE_SYSCTL */
158 #elif defined(HAVE_SYSCTLBYNAME)
160 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
162 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
163 /* #endif HAVE_SYSCTLBYNAME */
165 #elif defined(HAVE_LIBSTATGRAB)
166 /* no variables needed */
167 /* #endif HAVE_LIBSTATGRAB */
169 #elif defined(HAVE_PERFSTAT)
170 static perfstat_cpu_t *perfcpu;
173 #endif /* HAVE_PERFSTAT */
175 static gauge_t percents[CPU_SUBMIT_MAX] = {
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
180 static _Bool report_by_cpu = 1;
181 static _Bool report_percent = 0;
182 static _Bool report_active = 0;
184 static const char *config_keys[] =
190 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
192 static int cpu_config (const char *key, const char *value)
194 if (strcasecmp (key, "ReportByCpu") == 0) {
195 report_by_cpu = IS_TRUE (value) ? 1 : 0;
199 if (strcasecmp (key, "ValuesPercentage") == 0)
200 report_percent = IS_TRUE (value) ? 1 : 0;
201 if (strcasecmp (key, "ReportActive") == 0)
202 report_active = IS_TRUE (value) ? 1 : 0;
206 static int init (void)
208 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
209 kern_return_t status;
211 port_host = mach_host_self ();
213 /* FIXME: Free `cpu_list' if it's not NULL */
214 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
216 ERROR ("cpu plugin: host_processors returned %i", (int) status);
221 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
222 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
224 cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
225 /* #endif PROCESSOR_CPU_LOAD_INFO */
227 #elif defined(HAVE_LIBKSTAT)
235 /* Solaris doesn't count linear.. *sigh* */
236 for (numcpu = 0, ksp_chain = kc->kc_chain;
237 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
238 ksp_chain = ksp_chain->ks_next)
239 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
240 ksp[numcpu++] = ksp_chain;
241 /* #endif HAVE_LIBKSTAT */
245 int mib[2] = {CTL_HW, HW_NCPU};
249 numcpu_size = sizeof (numcpu);
251 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
252 &numcpu, &numcpu_size, NULL, 0);
256 WARNING ("cpu plugin: sysctl: %s",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
260 /* #endif CAN_USE_SYSCTL */
262 #elif defined (HAVE_SYSCTLBYNAME)
265 numcpu_size = sizeof (numcpu);
267 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
270 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
271 sstrerror (errno, errbuf, sizeof (errbuf)));
275 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
276 numcpu_size = sizeof (maxcpu);
278 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
281 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
287 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
289 /* #endif HAVE_SYSCTLBYNAME */
291 #elif defined(HAVE_LIBSTATGRAB)
292 /* nothing to initialize */
293 /* #endif HAVE_LIBSTATGRAB */
295 #elif defined(HAVE_PERFSTAT)
296 /* nothing to initialize */
297 #endif /* HAVE_PERFSTAT */
302 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
305 value_list_t vl = VALUE_LIST_INIT;
307 memcpy(&values[0], &value, sizeof(value));
312 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
313 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
314 sstrncpy (vl.type, type, sizeof (vl.type));
315 sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
316 sizeof (vl.type_instance));
319 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
322 plugin_dispatch_values (&vl);
325 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
329 value.gauge = percent;
330 submit_value (cpu_num, cpu_state, "percent", value);
333 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
337 value.derive = derive;
338 submit_value (cpu_num, cpu_state, "cpu", value);
341 static void submit_flush (void)
348 for (i = 1; i < CPU_SUBMIT_MAX; i++) {
349 if (percents[i] == -1)
351 submit_percent (-1, i, percents[i] / cpu_count);
354 memset(percents, 0, sizeof(percents));
357 static void submit (int cpu_num, derive_t *derives)
361 derive_t cpu_active = 0;
362 derive_t cpu_total = 0;
365 if (!report_percent && report_by_cpu) {
367 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++)
369 if (derives[i] == -1)
372 if (i != CPU_SUBMIT_IDLE)
373 cpu_active += derives[i];
375 submit_derive(cpu_num, i, derives[i]);
378 submit_derive(cpu_num, CPU_SUBMIT_ACTIVE, cpu_active);
380 else /* we are reporting percents */
382 for (i = 1; i < CPU_SUBMIT_ACTIVE; i++) {
383 if (derives[i] == -1)
385 cpu_total += derives[i];
386 if (i != CPU_SUBMIT_IDLE)
387 cpu_active += derives[i];
390 derives[CPU_SUBMIT_ACTIVE] = cpu_active;
392 derives[CPU_SUBMIT_ACTIVE] = -1;
395 for (i = 1; i < CPU_SUBMIT_MAX; i++) {
396 if (derives[i] == -1) {
400 percent = ((gauge_t)derives[i] / (gauge_t)cpu_total) * 100.0;
404 submit_percent (cpu_num, i, percent);
405 } else if (percents[i] != -1) {
406 percents[i] += percent;
412 static int cpu_read (void)
414 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
417 kern_return_t status;
419 #if PROCESSOR_CPU_LOAD_INFO
421 processor_cpu_load_info_data_t cpu_info;
422 mach_msg_type_number_t cpu_info_len;
424 #if PROCESSOR_TEMPERATURE
425 processor_info_data_t cpu_temp;
426 mach_msg_type_number_t cpu_temp_len;
431 for (cpu = 0; cpu < cpu_list_len; cpu++)
433 #if PROCESSOR_CPU_LOAD_INFO
434 derive_t derives[CPU_SUBMIT_MAX] = {
435 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
437 memset(derives, -1, sizeof(derives));
439 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
441 if ((status = processor_info (cpu_list[cpu],
442 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
443 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
445 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
449 if (cpu_info_len < CPU_STATE_MAX)
451 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
455 derives[CPU_USER] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER];
456 derives[CPU_NICE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE];
457 derives[CPU_SYSTEM] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM];
458 derives[CPU_IDLE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE];
459 submit (cpu, derives);
461 #endif /* PROCESSOR_CPU_LOAD_INFO */
462 #if PROCESSOR_TEMPERATURE
464 * Not all Apple computers do have this ability. To minimize
465 * the messages sent to the syslog we do an exponential
466 * stepback if `processor_info' fails. We still try ~once a day
469 if (cpu_temp_retry_counter > 0)
471 cpu_temp_retry_counter--;
475 cpu_temp_len = PROCESSOR_INFO_MAX;
477 status = processor_info (cpu_list[cpu],
478 PROCESSOR_TEMPERATURE,
480 cpu_temp, &cpu_temp_len);
481 if (status != KERN_SUCCESS)
483 ERROR ("cpu plugin: processor_info failed: %s",
484 mach_error_string (status));
486 cpu_temp_retry_counter = cpu_temp_retry_step;
487 cpu_temp_retry_step *= 2;
488 if (cpu_temp_retry_step > cpu_temp_retry_max)
489 cpu_temp_retry_step = cpu_temp_retry_max;
494 if (cpu_temp_len != 1)
496 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
501 cpu_temp_retry_counter = 0;
502 cpu_temp_retry_step = 1;
503 #endif /* PROCESSOR_TEMPERATURE */
506 /* #endif PROCESSOR_CPU_LOAD_INFO */
508 #elif defined(KERNEL_LINUX)
516 if ((fh = fopen ("/proc/stat", "r")) == NULL)
519 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
520 sstrerror (errno, errbuf, sizeof (errbuf)));
524 while (fgets (buf, 1024, fh) != NULL)
526 derive_t derives[CPU_SUBMIT_MAX] = {
527 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
530 if (strncmp (buf, "cpu", 3))
532 if ((buf[3] < '0') || (buf[3] > '9'))
535 numfields = strsplit (buf, fields, 9);
539 cpu = atoi (fields[0] + 3);
540 derives[CPU_SUBMIT_USER] = atoll(fields[1]);
541 derives[CPU_SUBMIT_NICE] = atoll(fields[2]);
542 derives[CPU_SUBMIT_SYSTEM] = atoll(fields[3]);
543 derives[CPU_SUBMIT_IDLE] = atoll(fields[4]);
547 derives[CPU_SUBMIT_WAIT] = atoll(fields[5]);
548 derives[CPU_SUBMIT_INTERRUPT] = atoll(fields[6]);
549 derives[CPU_SUBMIT_SOFTIRQ] = atoll(fields[6]);
552 derives[CPU_SUBMIT_STEAL] = atoll(fields[8]);
554 submit(cpu, derives);
559 /* #endif defined(KERNEL_LINUX) */
561 #elif defined(HAVE_LIBKSTAT)
563 static cpu_stat_t cs;
568 for (cpu = 0; cpu < numcpu; cpu++)
570 derive_t derives[CPU_SUBMIT_MAX] = {
571 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
574 if (kstat_read (kc, ksp[cpu], &cs) == -1)
575 continue; /* error message? */
577 memset(derives, -1, sizeof(derives));
578 derives[CPU_SUBMIT_IDLE] = cs.cpu_sysinfo.cpu[CPU_IDLE];
579 derives[CPU_SUBMIT_USER] = cs.cpu_sysinfo.cpu[CPU_USER];
580 derives[CPU_SUBMIT_SYSTEM] = cs.cpu_sysinfo.cpu[CPU_KERNEL];
581 derives[CPU_SUBMIT_WAIT] = cs.cpu_sysinfo.cpu[CPU_WAIT];
582 submit (ksp[cpu]->ks_instance, derives);
585 /* #endif defined(HAVE_LIBKSTAT) */
588 uint64_t cpuinfo[numcpu][CPUSTATES];
595 ERROR ("cpu plugin: Could not determine number of "
596 "installed CPUs using sysctl(3).");
600 memset (cpuinfo, 0, sizeof (cpuinfo));
602 #if defined(KERN_CPTIME2)
604 for (i = 0; i < numcpu; i++) {
605 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
607 cpuinfo_size = sizeof (cpuinfo[0]);
609 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
610 cpuinfo[i], &cpuinfo_size, NULL, 0);
613 ERROR ("cpu plugin: sysctl failed: %s.",
614 sstrerror (errno, errbuf, sizeof (errbuf)));
620 #endif /* defined(KERN_CPTIME2) */
622 int mib[] = {CTL_KERN, KERN_CPTIME};
623 long cpuinfo_tmp[CPUSTATES];
625 cpuinfo_size = sizeof(cpuinfo_tmp);
627 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
628 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
632 ERROR ("cpu plugin: sysctl failed: %s.",
633 sstrerror (errno, errbuf, sizeof (errbuf)));
637 for(i = 0; i < CPUSTATES; i++) {
638 cpuinfo[0][i] = cpuinfo_tmp[i];
642 for (i = 0; i < numcpu; i++) {
643 derive_t derives[CPU_SUBMIT_MAX] = {
644 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
647 derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER];
648 derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE];
649 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS];
650 derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE];
651 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR];
655 /* #endif CAN_USE_SYSCTL */
656 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
657 long cpuinfo[maxcpu][CPUSTATES];
661 memset (cpuinfo, 0, sizeof (cpuinfo));
663 cpuinfo_size = sizeof (cpuinfo);
664 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
667 ERROR ("cpu plugin: sysctlbyname failed: %s.",
668 sstrerror (errno, errbuf, sizeof (errbuf)));
672 for (i = 0; i < numcpu; i++) {
673 derive_t derives[CPU_SUBMIT_MAX] = {
674 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
677 derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER];
678 derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE];
679 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS];
680 derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE];
681 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR];
686 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
687 #elif defined(HAVE_SYSCTLBYNAME)
688 long cpuinfo[CPUSTATES];
690 derive_t derives[CPU_SUBMIT_MAX] = {
691 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
694 cpuinfo_size = sizeof (cpuinfo);
696 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
699 ERROR ("cpu plugin: sysctlbyname failed: %s.",
700 sstrerror (errno, errbuf, sizeof (errbuf)));
704 derives[CPU_SUBMIT_USER] = cpuinfo[CP_USER];
705 derives[CPU_SUBMIT_SYSTEM] = cpuinfo[CP_SYS];
706 derives[CPU_SUBMIT_NICE] = cpuinfo[CP_NICE];
707 derives[CPU_SUBMIT_IDLE] = cpuinfo[CP_IDLE];
708 derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[CP_INTR];
712 /* #endif HAVE_SYSCTLBYNAME */
714 #elif defined(HAVE_LIBSTATGRAB)
716 derive_t derives[CPU_SUBMIT_MAX] = {
717 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
719 cs = sg_get_cpu_stats ();
723 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
727 derives[CPU_SUBMIT_IDLE] = (derive_t) cs->idle;
728 derives[CPU_SUBMIT_NICE] = (derive_t) cs->nice;
729 derives[CPU_SUBMIT_SWAP] = (derive_t) cs->swap;
730 derives[CPU_SUBMIT_SYSTEM] = (derive_t) cs->kernel;
731 derives[CPU_SUBMIT_USER] = (derive_t) cs->user;
732 derives[CPU_SUBMIT_WAIT] = (derive_t) cs->iowait;
735 /* #endif HAVE_LIBSTATGRAB */
737 #elif defined(HAVE_PERFSTAT)
741 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
745 WARNING ("cpu plugin: perfstat_cpu: %s",
746 sstrerror (errno, errbuf, sizeof (errbuf)));
750 if (pnumcpu != numcpu || perfcpu == NULL)
754 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
759 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
762 WARNING ("cpu plugin: perfstat_cpu: %s",
763 sstrerror (errno, errbuf, sizeof (errbuf)));
767 for (i = 0; i < cpus; i++)
769 derive_t derives[CPU_SUBMIT_MAX] = {
770 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
772 derives[CPU_SUBMIT_IDLE] = perfcpu[i].idle;
773 derives[CPU_SUBMIT_SYSTEM] = perfcpu[i].sys;
774 derives[CPU_SUBMIT_USER] = perfcpu[i].user;
775 derives[CPU_SUBMIT_WAIT] = perfcpu[i].wait;
779 #endif /* HAVE_PERFSTAT */
784 void module_register (void)
786 plugin_register_init ("cpu", init);
787 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
788 plugin_register_read ("cpu", cpu_read);
789 } /* void module_register */