3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
6 * Copyright (C) 2009 Manuel Sanmartin
7 * Copyright (C) 2013-2014 Pierre-Yves Ritschard
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; only version 2 of the License is applicable.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Florian octo Forster <octo at collectd.org>
24 * Oleg King <king2 at kaluga.ru>
25 * Simon Kuhnle <simon at blarzwurst.de>
27 * Pierre-Yves Ritschard <pyr at spootnik.org>
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
40 #ifdef HAVE_MACH_HOST_PRIV_H
41 # include <mach/host_priv.h>
43 #if HAVE_MACH_MACH_ERROR_H
44 # include <mach/mach_error.h>
46 #ifdef HAVE_MACH_PROCESSOR_INFO_H
47 # include <mach/processor_info.h>
49 #ifdef HAVE_MACH_PROCESSOR_H
50 # include <mach/processor.h>
52 #ifdef HAVE_MACH_VM_MAP_H
53 # include <mach/vm_map.h>
57 # include <sys/sysinfo.h>
58 #endif /* HAVE_LIBKSTAT */
60 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
61 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
62 # ifdef HAVE_SYS_SYSCTL_H
63 # include <sys/sysctl.h>
66 # ifdef HAVE_SYS_DKSTAT_H
67 # include <sys/dkstat.h>
70 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
78 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
81 # if defined(CTL_HW) && defined(HW_NCPU) \
82 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
83 # define CAN_USE_SYSCTL 1
85 # define CAN_USE_SYSCTL 0
88 # define CAN_USE_SYSCTL 0
91 #define CPU_STATE_USER 0
92 #define CPU_STATE_SYSTEM 1
93 #define CPU_STATE_WAIT 2
94 #define CPU_STATE_NICE 3
95 #define CPU_STATE_SWAP 4
96 #define CPU_STATE_INTERRUPT 5
97 #define CPU_STATE_SOFTIRQ 6
98 #define CPU_STATE_STEAL 7
99 #define CPU_STATE_IDLE 8
100 #define CPU_STATE_ACTIVE 9 /* sum of (!idle) */
101 #define CPU_STATE_MAX 10 /* #states */
104 # include <statgrab.h>
107 # ifdef HAVE_PERFSTAT
108 # include <sys/protosw.h>
109 # include <libperfstat.h>
110 # endif /* HAVE_PERFSTAT */
112 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
113 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
114 # error "No applicable input method."
117 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 #define RATE_ADD(sum, val) do { \
178 else if (!isnan (val)) \
184 value_to_rate_state_t conv;
188 typedef struct cpu_state_s cpu_state_t;
190 static cpu_state_t *cpu_states = NULL;
191 static size_t cpu_states_num = 0; /* #cpu_states allocated */
193 /* Highest CPU number in the current iteration. Used by the dispatch logic to
194 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
195 static size_t global_cpu_num = 0;
197 static _Bool report_by_cpu = 1;
198 static _Bool report_by_state = 1;
199 static _Bool report_percent = 0;
201 static const char *config_keys[] =
207 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
209 static int cpu_config (char const *key, char const *value) /* {{{ */
211 if (strcasecmp (key, "ReportByCpu") == 0)
212 report_by_cpu = IS_TRUE (value) ? 1 : 0;
213 else if (strcasecmp (key, "ValuesPercentage") == 0)
214 report_percent = IS_TRUE (value) ? 1 : 0;
215 else if (strcasecmp (key, "ReportByState") == 0)
216 report_by_state = IS_TRUE (value) ? 1 : 0;
221 } /* }}} int cpu_config */
223 static int init (void)
225 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
226 kern_return_t status;
228 port_host = mach_host_self ();
230 /* FIXME: Free `cpu_list' if it's not NULL */
231 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
233 ERROR ("cpu plugin: host_processors returned %i", (int) status);
238 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
239 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
241 cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
242 /* #endif PROCESSOR_CPU_LOAD_INFO */
244 #elif defined(HAVE_LIBKSTAT)
252 /* Solaris doesn't count linear.. *sigh* */
253 for (numcpu = 0, ksp_chain = kc->kc_chain;
254 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
255 ksp_chain = ksp_chain->ks_next)
256 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
257 ksp[numcpu++] = ksp_chain;
258 /* #endif HAVE_LIBKSTAT */
262 int mib[2] = {CTL_HW, HW_NCPU};
266 numcpu_size = sizeof (numcpu);
268 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
269 &numcpu, &numcpu_size, NULL, 0);
273 WARNING ("cpu plugin: sysctl: %s",
274 sstrerror (errno, errbuf, sizeof (errbuf)));
277 /* #endif CAN_USE_SYSCTL */
279 #elif defined (HAVE_SYSCTLBYNAME)
282 numcpu_size = sizeof (numcpu);
284 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
287 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
288 sstrerror (errno, errbuf, sizeof (errbuf)));
292 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
293 numcpu_size = sizeof (maxcpu);
295 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
298 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
299 sstrerror (errno, errbuf, sizeof (errbuf)));
304 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
306 /* #endif HAVE_SYSCTLBYNAME */
308 #elif defined(HAVE_LIBSTATGRAB)
309 /* nothing to initialize */
310 /* #endif HAVE_LIBSTATGRAB */
312 #elif defined(HAVE_PERFSTAT)
313 /* nothing to initialize */
314 #endif /* HAVE_PERFSTAT */
319 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
322 value_list_t vl = VALUE_LIST_INIT;
324 memcpy(&values[0], &value, sizeof(value));
329 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
330 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
331 sstrncpy (vl.type, type, sizeof (vl.type));
332 sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
333 sizeof (vl.type_instance));
336 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
339 plugin_dispatch_values (&vl);
342 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
346 value.gauge = percent;
347 submit_value (cpu_num, cpu_state, "percent", value);
350 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
354 value.derive = derive;
355 submit_value (cpu_num, cpu_state, "cpu", value);
358 /* Takes the zero-index number of a CPU and makes sure that the module-global
359 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
360 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
368 sz = (((size_t) cpu_num) + 1) * CPU_STATE_MAX;
371 /* We already have enough space. */
372 if (cpu_states_num >= sz)
375 tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
378 ERROR ("cpu plugin: realloc failed.");
382 tmp = cpu_states + cpu_states_num;
384 memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
387 } /* }}} cpu_states_alloc */
389 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
391 size_t index = ((cpu_num * CPU_STATE_MAX) + state);
393 if (index >= cpu_states_num)
396 return (&cpu_states[index]);
397 } /* }}} cpu_state_t *get_cpu_state */
399 /* Populates the per-CPU CPU_STATE_ACTIVE rate and the global rate_by_state
401 static void aggregate (gauge_t *sum_by_state) /* {{{ */
406 for (state = 0; state < CPU_STATE_MAX; state++)
407 sum_by_state[state] = NAN;
409 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
411 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
413 this_cpu_states[CPU_STATE_ACTIVE].rate = NAN;
415 for (state = 0; state < CPU_STATE_ACTIVE; state++)
417 if (!this_cpu_states[state].has_value)
420 RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
421 if (state != CPU_STATE_IDLE)
422 RATE_ADD (this_cpu_states[CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
425 RATE_ADD (sum_by_state[CPU_STATE_ACTIVE], this_cpu_states[CPU_STATE_ACTIVE].rate);
427 } /* }}} void aggregate */
429 /* Commits (dispatches) the values for one CPU or the global aggregation.
430 * cpu_num is the index of the CPU to be committed or -1 in case of the global
431 * aggregation. rates is a pointer to CPU_STATE_MAX gauge_t values holding the
432 * current rate; each rate may be NAN. Calculates the percentage of each state
433 * and dispatches the metric. */
434 static void cpu_commit_one (int cpu_num, /* {{{ */
435 gauge_t rates[static CPU_STATE_MAX])
440 sum = rates[CPU_STATE_ACTIVE];
441 RATE_ADD (sum, rates[CPU_STATE_IDLE]);
443 if (!report_by_state)
445 gauge_t percent = 100.0 * rates[CPU_STATE_ACTIVE] / sum;
446 submit_percent (cpu_num, CPU_STATE_ACTIVE, percent);
450 for (state = 0; state < CPU_STATE_ACTIVE; state++)
452 gauge_t percent = 100.0 * rates[state] / sum;
453 submit_percent (cpu_num, state, percent);
455 } /* }}} void cpu_commit_one */
457 /* Resets the internal aggregation. This is called by the read callback after
458 * each iteration / after each call to cpu_commit(). */
459 static void cpu_reset (void) /* {{{ */
463 for (i = 0; i < cpu_states_num; i++)
464 cpu_states[i].has_value = 0;
467 } /* }}} void cpu_reset */
469 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
470 static void cpu_commit_without_aggregation (void) /* {{{ */
474 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
478 for (state = 0; state < CPU_STATE_ACTIVE; state++)
480 cpu_state_t *s = get_cpu_state (cpu_num, state);
485 submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
488 } /* }}} void cpu_commit_without_aggregation */
490 /* Aggregates the internal state and dispatches the metrics. */
491 static void cpu_commit (void) /* {{{ */
493 gauge_t global_rates[CPU_STATE_MAX] = {
494 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
498 if (report_by_cpu && report_by_state && !report_percent)
500 cpu_commit_without_aggregation ();
504 aggregate (global_rates);
508 cpu_commit_one (-1, global_rates);
512 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
514 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
515 gauge_t local_rates[CPU_STATE_MAX] = {
516 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
520 for (state = 0; state < CPU_STATE_ACTIVE; state++)
521 if (this_cpu_states[state].has_value)
522 local_rates[state] = this_cpu_states[state].rate;
524 cpu_commit_one ((int) cpu_num, local_rates);
526 } /* }}} void cpu_commit */
528 /* Adds a derive value to the internal state. This should be used by each read
529 * function for each state. At the end of the iteration, the read function
530 * should call cpu_commit(). */
531 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
537 if (state >= CPU_STATE_ACTIVE)
540 status = cpu_states_alloc (cpu_num);
544 if (global_cpu_num <= cpu_num)
545 global_cpu_num = cpu_num + 1;
547 s = get_cpu_state (cpu_num, state);
550 status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
557 } /* }}} int cpu_stage */
559 static int cpu_read (void)
561 cdtime_t now = cdtime ();
563 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE /* {{{ */
566 kern_return_t status;
568 #if PROCESSOR_CPU_LOAD_INFO
569 processor_cpu_load_info_data_t cpu_info;
570 mach_msg_type_number_t cpu_info_len;
572 #if PROCESSOR_TEMPERATURE
573 processor_info_data_t cpu_temp;
574 mach_msg_type_number_t cpu_temp_len;
579 for (cpu = 0; cpu < cpu_list_len; cpu++)
581 #if PROCESSOR_CPU_LOAD_INFO
583 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
585 if ((status = processor_info (cpu_list[cpu],
586 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
587 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
589 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
593 if (cpu_info_len < CPU_STATE_MAX)
595 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
599 cpu_stage (cpu, CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER], now);
600 cpu_stage (cpu, CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE], now);
601 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
602 cpu_stage (cpu, CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
603 #endif /* PROCESSOR_CPU_LOAD_INFO */
605 #if PROCESSOR_TEMPERATURE
607 * Not all Apple computers do have this ability. To minimize
608 * the messages sent to the syslog we do an exponential
609 * stepback if `processor_info' fails. We still try ~once a day
612 if (cpu_temp_retry_counter > 0)
614 cpu_temp_retry_counter--;
618 cpu_temp_len = PROCESSOR_INFO_MAX;
620 status = processor_info (cpu_list[cpu],
621 PROCESSOR_TEMPERATURE,
623 cpu_temp, &cpu_temp_len);
624 if (status != KERN_SUCCESS)
626 ERROR ("cpu plugin: processor_info failed: %s",
627 mach_error_string (status));
629 cpu_temp_retry_counter = cpu_temp_retry_step;
630 cpu_temp_retry_step *= 2;
631 if (cpu_temp_retry_step > cpu_temp_retry_max)
632 cpu_temp_retry_step = cpu_temp_retry_max;
637 if (cpu_temp_len != 1)
639 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
644 cpu_temp_retry_counter = 0;
645 cpu_temp_retry_step = 1;
646 #endif /* PROCESSOR_TEMPERATURE */
648 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
650 #elif defined(KERNEL_LINUX) /* {{{ */
658 if ((fh = fopen ("/proc/stat", "r")) == NULL)
661 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
662 sstrerror (errno, errbuf, sizeof (errbuf)));
666 while (fgets (buf, 1024, fh) != NULL)
668 if (strncmp (buf, "cpu", 3))
670 if ((buf[3] < '0') || (buf[3] > '9'))
673 numfields = strsplit (buf, fields, 9);
677 cpu = atoi (fields[0] + 3);
679 cpu_stage (cpu, CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
680 cpu_stage (cpu, CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
681 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
682 cpu_stage (cpu, CPU_STATE_IDLE, (derive_t) atoll(fields[4]), now);
686 cpu_stage (cpu, CPU_STATE_WAIT, (derive_t) atoll(fields[5]), now);
687 cpu_stage (cpu, CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
688 cpu_stage (cpu, CPU_STATE_SOFTIRQ, (derive_t) atoll(fields[7]), now);
691 cpu_stage (cpu, CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
695 /* }}} #endif defined(KERNEL_LINUX) */
697 #elif defined(HAVE_LIBKSTAT) /* {{{ */
699 static cpu_stat_t cs;
704 for (cpu = 0; cpu < numcpu; cpu++)
706 if (kstat_read (kc, ksp[cpu], &cs) == -1)
707 continue; /* error message? */
709 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_IDLE, (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE], now);
710 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_USER, (derive_t) cs.cpu_sysinfo.cpu[CPU_USER], now);
711 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
712 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_WAIT, (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT], now);
714 /* }}} #endif defined(HAVE_LIBKSTAT) */
716 #elif CAN_USE_SYSCTL /* {{{ */
717 uint64_t cpuinfo[numcpu][CPUSTATES];
724 ERROR ("cpu plugin: Could not determine number of "
725 "installed CPUs using sysctl(3).");
729 memset (cpuinfo, 0, sizeof (cpuinfo));
731 #if defined(KERN_CPTIME2)
733 for (i = 0; i < numcpu; i++) {
734 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
736 cpuinfo_size = sizeof (cpuinfo[0]);
738 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
739 cpuinfo[i], &cpuinfo_size, NULL, 0);
742 ERROR ("cpu plugin: sysctl failed: %s.",
743 sstrerror (errno, errbuf, sizeof (errbuf)));
749 #endif /* defined(KERN_CPTIME2) */
751 int mib[] = {CTL_KERN, KERN_CPTIME};
752 long cpuinfo_tmp[CPUSTATES];
754 cpuinfo_size = sizeof(cpuinfo_tmp);
756 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
757 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
761 ERROR ("cpu plugin: sysctl failed: %s.",
762 sstrerror (errno, errbuf, sizeof (errbuf)));
766 for(i = 0; i < CPUSTATES; i++) {
767 cpuinfo[0][i] = cpuinfo_tmp[i];
771 for (i = 0; i < numcpu; i++) {
772 cpu_state (i, CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER]);
773 cpu_state (i, CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE]);
774 cpu_state (i, CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS]);
775 cpu_state (i, CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE]);
776 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
778 /* }}} #endif CAN_USE_SYSCTL */
780 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
781 long cpuinfo[maxcpu][CPUSTATES];
785 memset (cpuinfo, 0, sizeof (cpuinfo));
787 cpuinfo_size = sizeof (cpuinfo);
788 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
791 ERROR ("cpu plugin: sysctlbyname failed: %s.",
792 sstrerror (errno, errbuf, sizeof (errbuf)));
796 for (i = 0; i < numcpu; i++) {
797 cpu_state (i, CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER]);
798 cpu_state (i, CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE]);
799 cpu_state (i, CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS]);
800 cpu_state (i, CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE]);
801 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
803 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
805 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
806 long cpuinfo[CPUSTATES];
809 cpuinfo_size = sizeof (cpuinfo);
811 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
814 ERROR ("cpu plugin: sysctlbyname failed: %s.",
815 sstrerror (errno, errbuf, sizeof (errbuf)));
819 cpu_state (0, CPU_STATE_USER, (derive_t) cpuinfo[CP_USER]);
820 cpu_state (0, CPU_STATE_NICE, (derive_t) cpuinfo[CP_NICE]);
821 cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cpuinfo[CP_SYS]);
822 cpu_state (0, CPU_STATE_IDLE, (derive_t) cpuinfo[CP_IDLE]);
823 cpu_state (0, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR]);
824 /* }}} #endif HAVE_SYSCTLBYNAME */
826 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
828 cs = sg_get_cpu_stats ();
832 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
836 cpu_state (0, CPU_STATE_IDLE, (derive_t) cs->idle);
837 cpu_state (0, CPU_STATE_NICE, (derive_t) cs->nice);
838 cpu_state (0, CPU_STATE_SWAP, (derive_t) cs->swap);
839 cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cs->kernel);
840 cpu_state (0, CPU_STATE_USER, (derive_t) cs->user);
841 cpu_state (0, CPU_STATE_WAIT, (derive_t) cs->iowait);
842 /* }}} #endif HAVE_LIBSTATGRAB */
844 #elif defined(HAVE_PERFSTAT) /* {{{ */
848 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
852 WARNING ("cpu plugin: perfstat_cpu: %s",
853 sstrerror (errno, errbuf, sizeof (errbuf)));
857 if (pnumcpu != numcpu || perfcpu == NULL)
861 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
866 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
869 WARNING ("cpu plugin: perfstat_cpu: %s",
870 sstrerror (errno, errbuf, sizeof (errbuf)));
874 for (i = 0; i < cpus; i++)
876 cpu_stage (i, CPU_STATE_IDLE, (derive_t) perfcpu[i].idle, now);
877 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys, now);
878 cpu_stage (i, CPU_STATE_USER, (derive_t) perfcpu[i].user, now);
879 cpu_stage (i, CPU_STATE_WAIT, (derive_t) perfcpu[i].wait, now);
881 #endif /* }}} HAVE_PERFSTAT */
888 void module_register (void)
890 plugin_register_init ("cpu", init);
891 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
892 plugin_register_read ("cpu", cpu_read);
893 } /* void module_register */
895 /* vim: set sw=8 sts=8 noet fdm=marker : */