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 /* This function is called for all known CPU states, but each read
347 * method will only report a subset. The remaining states are left as
348 * NAN and we ignore them here. */
352 value.gauge = percent;
353 submit_value (cpu_num, cpu_state, "percent", value);
356 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
360 value.derive = derive;
361 submit_value (cpu_num, cpu_state, "cpu", value);
364 /* Takes the zero-index number of a CPU and makes sure that the module-global
365 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
366 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
371 sz = (((size_t) cpu_num) + 1) * CPU_STATE_MAX;
374 /* We already have enough space. */
375 if (cpu_states_num >= sz)
378 tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
381 ERROR ("cpu plugin: realloc failed.");
385 tmp = cpu_states + cpu_states_num;
387 memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
390 } /* }}} cpu_states_alloc */
392 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
394 size_t index = ((cpu_num * CPU_STATE_MAX) + state);
396 if (index >= cpu_states_num)
399 return (&cpu_states[index]);
400 } /* }}} cpu_state_t *get_cpu_state */
402 /* Populates the per-CPU CPU_STATE_ACTIVE rate and the global rate_by_state
404 static void aggregate (gauge_t *sum_by_state) /* {{{ */
409 for (state = 0; state < CPU_STATE_MAX; state++)
410 sum_by_state[state] = NAN;
412 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
414 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
416 this_cpu_states[CPU_STATE_ACTIVE].rate = NAN;
418 for (state = 0; state < CPU_STATE_ACTIVE; state++)
420 if (!this_cpu_states[state].has_value)
423 RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
424 if (state != CPU_STATE_IDLE)
425 RATE_ADD (this_cpu_states[CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
428 RATE_ADD (sum_by_state[CPU_STATE_ACTIVE], this_cpu_states[CPU_STATE_ACTIVE].rate);
430 } /* }}} void aggregate */
432 /* Commits (dispatches) the values for one CPU or the global aggregation.
433 * cpu_num is the index of the CPU to be committed or -1 in case of the global
434 * aggregation. rates is a pointer to CPU_STATE_MAX gauge_t values holding the
435 * current rate; each rate may be NAN. Calculates the percentage of each state
436 * and dispatches the metric. */
437 static void cpu_commit_one (int cpu_num, /* {{{ */
438 gauge_t rates[static CPU_STATE_MAX])
443 sum = rates[CPU_STATE_ACTIVE];
444 RATE_ADD (sum, rates[CPU_STATE_IDLE]);
446 if (!report_by_state)
448 gauge_t percent = 100.0 * rates[CPU_STATE_ACTIVE] / sum;
449 submit_percent (cpu_num, CPU_STATE_ACTIVE, percent);
453 for (state = 0; state < CPU_STATE_ACTIVE; state++)
455 gauge_t percent = 100.0 * rates[state] / sum;
456 submit_percent (cpu_num, state, percent);
458 } /* }}} void cpu_commit_one */
460 /* Resets the internal aggregation. This is called by the read callback after
461 * each iteration / after each call to cpu_commit(). */
462 static void cpu_reset (void) /* {{{ */
466 for (i = 0; i < cpu_states_num; i++)
467 cpu_states[i].has_value = 0;
470 } /* }}} void cpu_reset */
472 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
473 static void cpu_commit_without_aggregation (void) /* {{{ */
477 for (state = 0; state < CPU_STATE_ACTIVE; state++)
481 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
483 cpu_state_t *s = get_cpu_state (cpu_num, state);
488 submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
491 derive_t derive_total = 0;
492 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
494 cpu_state_t *s = get_cpu_state (cpu_num, state);
499 derive_total += s->conv.last_value.derive;
502 submit_derive (-1, (int) state, derive_total);
505 } /* }}} void cpu_commit_without_aggregation */
507 /* Aggregates the internal state and dispatches the metrics. */
508 static void cpu_commit (void) /* {{{ */
510 gauge_t global_rates[CPU_STATE_MAX] = {
511 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
515 if (report_by_state && !report_percent)
517 cpu_commit_without_aggregation ();
521 aggregate (global_rates);
525 cpu_commit_one (-1, global_rates);
529 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
531 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
532 gauge_t local_rates[CPU_STATE_MAX] = {
533 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
537 for (state = 0; state < CPU_STATE_ACTIVE; state++)
538 if (this_cpu_states[state].has_value)
539 local_rates[state] = this_cpu_states[state].rate;
541 cpu_commit_one ((int) cpu_num, local_rates);
543 } /* }}} void cpu_commit */
545 /* Adds a derive value to the internal state. This should be used by each read
546 * function for each state. At the end of the iteration, the read function
547 * should call cpu_commit(). */
548 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
554 if (state >= CPU_STATE_ACTIVE)
557 status = cpu_states_alloc (cpu_num);
561 if (global_cpu_num <= cpu_num)
562 global_cpu_num = cpu_num + 1;
564 s = get_cpu_state (cpu_num, state);
567 status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
574 } /* }}} int cpu_stage */
576 static int cpu_read (void)
578 cdtime_t now = cdtime ();
580 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE /* {{{ */
583 kern_return_t status;
585 #if PROCESSOR_CPU_LOAD_INFO
586 processor_cpu_load_info_data_t cpu_info;
587 mach_msg_type_number_t cpu_info_len;
589 #if PROCESSOR_TEMPERATURE
590 processor_info_data_t cpu_temp;
591 mach_msg_type_number_t cpu_temp_len;
596 for (cpu = 0; cpu < cpu_list_len; cpu++)
598 #if PROCESSOR_CPU_LOAD_INFO
600 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
602 if ((status = processor_info (cpu_list[cpu],
603 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
604 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
606 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
610 if (cpu_info_len < CPU_STATE_MAX)
612 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
616 cpu_stage (cpu, CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER], now);
617 cpu_stage (cpu, CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE], now);
618 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
619 cpu_stage (cpu, CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
620 #endif /* PROCESSOR_CPU_LOAD_INFO */
622 #if PROCESSOR_TEMPERATURE
624 * Not all Apple computers do have this ability. To minimize
625 * the messages sent to the syslog we do an exponential
626 * stepback if `processor_info' fails. We still try ~once a day
629 if (cpu_temp_retry_counter > 0)
631 cpu_temp_retry_counter--;
635 cpu_temp_len = PROCESSOR_INFO_MAX;
637 status = processor_info (cpu_list[cpu],
638 PROCESSOR_TEMPERATURE,
640 cpu_temp, &cpu_temp_len);
641 if (status != KERN_SUCCESS)
643 ERROR ("cpu plugin: processor_info failed: %s",
644 mach_error_string (status));
646 cpu_temp_retry_counter = cpu_temp_retry_step;
647 cpu_temp_retry_step *= 2;
648 if (cpu_temp_retry_step > cpu_temp_retry_max)
649 cpu_temp_retry_step = cpu_temp_retry_max;
654 if (cpu_temp_len != 1)
656 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
661 cpu_temp_retry_counter = 0;
662 cpu_temp_retry_step = 1;
663 #endif /* PROCESSOR_TEMPERATURE */
665 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
667 #elif defined(KERNEL_LINUX) /* {{{ */
675 if ((fh = fopen ("/proc/stat", "r")) == NULL)
678 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
679 sstrerror (errno, errbuf, sizeof (errbuf)));
683 while (fgets (buf, 1024, fh) != NULL)
685 if (strncmp (buf, "cpu", 3))
687 if ((buf[3] < '0') || (buf[3] > '9'))
690 numfields = strsplit (buf, fields, 9);
694 cpu = atoi (fields[0] + 3);
696 cpu_stage (cpu, CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
697 cpu_stage (cpu, CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
698 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
699 cpu_stage (cpu, CPU_STATE_IDLE, (derive_t) atoll(fields[4]), now);
703 cpu_stage (cpu, CPU_STATE_WAIT, (derive_t) atoll(fields[5]), now);
704 cpu_stage (cpu, CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
705 cpu_stage (cpu, CPU_STATE_SOFTIRQ, (derive_t) atoll(fields[7]), now);
708 cpu_stage (cpu, CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
712 /* }}} #endif defined(KERNEL_LINUX) */
714 #elif defined(HAVE_LIBKSTAT) /* {{{ */
716 static cpu_stat_t cs;
721 for (cpu = 0; cpu < numcpu; cpu++)
723 if (kstat_read (kc, ksp[cpu], &cs) == -1)
724 continue; /* error message? */
726 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_IDLE, (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE], now);
727 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_USER, (derive_t) cs.cpu_sysinfo.cpu[CPU_USER], now);
728 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
729 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_WAIT, (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT], now);
731 /* }}} #endif defined(HAVE_LIBKSTAT) */
733 #elif CAN_USE_SYSCTL /* {{{ */
734 uint64_t cpuinfo[numcpu][CPUSTATES];
741 ERROR ("cpu plugin: Could not determine number of "
742 "installed CPUs using sysctl(3).");
746 memset (cpuinfo, 0, sizeof (cpuinfo));
748 #if defined(KERN_CPTIME2)
750 for (i = 0; i < numcpu; i++) {
751 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
753 cpuinfo_size = sizeof (cpuinfo[0]);
755 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
756 cpuinfo[i], &cpuinfo_size, NULL, 0);
759 ERROR ("cpu plugin: sysctl failed: %s.",
760 sstrerror (errno, errbuf, sizeof (errbuf)));
766 #endif /* defined(KERN_CPTIME2) */
768 int mib[] = {CTL_KERN, KERN_CPTIME};
769 long cpuinfo_tmp[CPUSTATES];
771 cpuinfo_size = sizeof(cpuinfo_tmp);
773 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
774 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
778 ERROR ("cpu plugin: sysctl failed: %s.",
779 sstrerror (errno, errbuf, sizeof (errbuf)));
783 for(i = 0; i < CPUSTATES; i++) {
784 cpuinfo[0][i] = cpuinfo_tmp[i];
788 for (i = 0; i < numcpu; i++) {
789 cpu_stage (i, CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
790 cpu_stage (i, CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
791 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
792 cpu_stage (i, CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
793 cpu_stage (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
795 /* }}} #endif CAN_USE_SYSCTL */
797 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
798 long cpuinfo[maxcpu][CPUSTATES];
802 memset (cpuinfo, 0, sizeof (cpuinfo));
804 cpuinfo_size = sizeof (cpuinfo);
805 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
808 ERROR ("cpu plugin: sysctlbyname failed: %s.",
809 sstrerror (errno, errbuf, sizeof (errbuf)));
813 for (i = 0; i < numcpu; i++) {
814 cpu_stage (i, CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
815 cpu_stage (i, CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
816 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
817 cpu_stage (i, CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
818 cpu_stage (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
820 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
822 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
823 long cpuinfo[CPUSTATES];
826 cpuinfo_size = sizeof (cpuinfo);
828 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
831 ERROR ("cpu plugin: sysctlbyname failed: %s.",
832 sstrerror (errno, errbuf, sizeof (errbuf)));
836 cpu_stage (0, CPU_STATE_USER, (derive_t) cpuinfo[CP_USER], now);
837 cpu_stage (0, CPU_STATE_NICE, (derive_t) cpuinfo[CP_NICE], now);
838 cpu_stage (0, CPU_STATE_SYSTEM, (derive_t) cpuinfo[CP_SYS], now);
839 cpu_stage (0, CPU_STATE_IDLE, (derive_t) cpuinfo[CP_IDLE], now);
840 cpu_stage (0, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
841 /* }}} #endif HAVE_SYSCTLBYNAME */
843 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
845 cs = sg_get_cpu_stats ();
849 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
853 cpu_state (0, CPU_STATE_IDLE, (derive_t) cs->idle);
854 cpu_state (0, CPU_STATE_NICE, (derive_t) cs->nice);
855 cpu_state (0, CPU_STATE_SWAP, (derive_t) cs->swap);
856 cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cs->kernel);
857 cpu_state (0, CPU_STATE_USER, (derive_t) cs->user);
858 cpu_state (0, CPU_STATE_WAIT, (derive_t) cs->iowait);
859 /* }}} #endif HAVE_LIBSTATGRAB */
861 #elif defined(HAVE_PERFSTAT) /* {{{ */
865 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
869 WARNING ("cpu plugin: perfstat_cpu: %s",
870 sstrerror (errno, errbuf, sizeof (errbuf)));
874 if (pnumcpu != numcpu || perfcpu == NULL)
878 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
883 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
886 WARNING ("cpu plugin: perfstat_cpu: %s",
887 sstrerror (errno, errbuf, sizeof (errbuf)));
891 for (i = 0; i < cpus; i++)
893 cpu_stage (i, CPU_STATE_IDLE, (derive_t) perfcpu[i].idle, now);
894 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys, now);
895 cpu_stage (i, CPU_STATE_USER, (derive_t) perfcpu[i].user, now);
896 cpu_stage (i, CPU_STATE_WAIT, (derive_t) perfcpu[i].wait, now);
898 #endif /* }}} HAVE_PERFSTAT */
905 void module_register (void)
907 plugin_register_init ("cpu", init);
908 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
909 plugin_register_read ("cpu", cpu_read);
910 } /* void module_register */
912 /* vim: set sw=8 sts=8 noet fdm=marker : */