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 COLLECTD_CPU_STATE_USER 0
92 #define COLLECTD_CPU_STATE_SYSTEM 1
93 #define COLLECTD_CPU_STATE_WAIT 2
94 #define COLLECTD_CPU_STATE_NICE 3
95 #define COLLECTD_CPU_STATE_SWAP 4
96 #define COLLECTD_CPU_STATE_INTERRUPT 5
97 #define COLLECTD_CPU_STATE_SOFTIRQ 6
98 #define COLLECTD_CPU_STATE_STEAL 7
99 #define COLLECTD_CPU_STATE_IDLE 8
100 #define COLLECTD_CPU_STATE_ACTIVE 9 /* sum of (!idle) */
101 #define COLLECTD_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;
134 /* #endif PROCESSOR_CPU_LOAD_INFO */
136 #elif defined(KERNEL_LINUX)
137 /* no variables needed */
138 /* #endif KERNEL_LINUX */
140 #elif defined(HAVE_LIBKSTAT)
141 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
142 # define MAX_NUMCPU 256
143 extern kstat_ctl_t *kc;
144 static kstat_t *ksp[MAX_NUMCPU];
146 /* #endif HAVE_LIBKSTAT */
150 /* #endif CAN_USE_SYSCTL */
152 #elif defined(HAVE_SYSCTLBYNAME)
154 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
156 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
157 /* #endif HAVE_SYSCTLBYNAME */
159 #elif defined(HAVE_LIBSTATGRAB)
160 /* no variables needed */
161 /* #endif HAVE_LIBSTATGRAB */
163 #elif defined(HAVE_PERFSTAT)
164 static perfstat_cpu_t *perfcpu;
167 #endif /* HAVE_PERFSTAT */
169 #define RATE_ADD(sum, val) do { \
172 else if (!isnan (val)) \
178 value_to_rate_state_t conv;
182 typedef struct cpu_state_s cpu_state_t;
184 static cpu_state_t *cpu_states = NULL;
185 static size_t cpu_states_num = 0; /* #cpu_states allocated */
187 /* Highest CPU number in the current iteration. Used by the dispatch logic to
188 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
189 static size_t global_cpu_num = 0;
191 static _Bool report_by_cpu = 1;
192 static _Bool report_by_state = 1;
193 static _Bool report_percent = 0;
195 static const char *config_keys[] =
201 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
203 static int cpu_config (char const *key, char const *value) /* {{{ */
205 if (strcasecmp (key, "ReportByCpu") == 0)
206 report_by_cpu = IS_TRUE (value) ? 1 : 0;
207 else if (strcasecmp (key, "ValuesPercentage") == 0)
208 report_percent = IS_TRUE (value) ? 1 : 0;
209 else if (strcasecmp (key, "ReportByState") == 0)
210 report_by_state = IS_TRUE (value) ? 1 : 0;
215 } /* }}} int cpu_config */
217 static int init (void)
219 #if PROCESSOR_CPU_LOAD_INFO
220 kern_return_t status;
222 port_host = mach_host_self ();
224 /* FIXME: Free `cpu_list' if it's not NULL */
225 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
227 ERROR ("cpu plugin: host_processors returned %i", (int) status);
232 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
233 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
235 cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
236 /* #endif PROCESSOR_CPU_LOAD_INFO */
238 #elif defined(HAVE_LIBKSTAT)
246 /* Solaris doesn't count linear.. *sigh* */
247 for (numcpu = 0, ksp_chain = kc->kc_chain;
248 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
249 ksp_chain = ksp_chain->ks_next)
250 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
251 ksp[numcpu++] = ksp_chain;
252 /* #endif HAVE_LIBKSTAT */
256 int mib[2] = {CTL_HW, HW_NCPU};
260 numcpu_size = sizeof (numcpu);
262 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
263 &numcpu, &numcpu_size, NULL, 0);
267 WARNING ("cpu plugin: sysctl: %s",
268 sstrerror (errno, errbuf, sizeof (errbuf)));
271 /* #endif CAN_USE_SYSCTL */
273 #elif defined (HAVE_SYSCTLBYNAME)
276 numcpu_size = sizeof (numcpu);
278 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
281 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
286 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
287 numcpu_size = sizeof (maxcpu);
289 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
292 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
298 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
300 /* #endif HAVE_SYSCTLBYNAME */
302 #elif defined(HAVE_LIBSTATGRAB)
303 /* nothing to initialize */
304 /* #endif HAVE_LIBSTATGRAB */
306 #elif defined(HAVE_PERFSTAT)
307 /* nothing to initialize */
308 #endif /* HAVE_PERFSTAT */
313 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
316 value_list_t vl = VALUE_LIST_INIT;
318 memcpy(&values[0], &value, sizeof(value));
323 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
324 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
325 sstrncpy (vl.type, type, sizeof (vl.type));
326 sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
327 sizeof (vl.type_instance));
330 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
333 plugin_dispatch_values (&vl);
336 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
340 /* This function is called for all known CPU states, but each read
341 * method will only report a subset. The remaining states are left as
342 * NAN and we ignore them here. */
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) /* {{{ */
365 sz = (((size_t) cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
368 /* We already have enough space. */
369 if (cpu_states_num >= sz)
372 tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
375 ERROR ("cpu plugin: realloc failed.");
379 tmp = cpu_states + cpu_states_num;
381 memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
384 } /* }}} cpu_states_alloc */
386 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
388 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
390 if (index >= cpu_states_num)
393 return (&cpu_states[index]);
394 } /* }}} cpu_state_t *get_cpu_state */
396 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global rate_by_state
398 static void aggregate (gauge_t *sum_by_state) /* {{{ */
403 for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
404 sum_by_state[state] = NAN;
406 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
408 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
410 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
412 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
414 if (!this_cpu_states[state].has_value)
417 RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
418 if (state != COLLECTD_CPU_STATE_IDLE)
419 RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
422 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
424 } /* }}} void aggregate */
426 /* Commits (dispatches) the values for one CPU or the global aggregation.
427 * cpu_num is the index of the CPU to be committed or -1 in case of the global
428 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values holding the
429 * current rate; each rate may be NAN. Calculates the percentage of each state
430 * and dispatches the metric. */
431 static void cpu_commit_one (int cpu_num, /* {{{ */
432 gauge_t rates[static COLLECTD_CPU_STATE_MAX])
437 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
438 RATE_ADD (sum, rates[COLLECTD_CPU_STATE_IDLE]);
440 if (!report_by_state)
442 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
443 submit_percent (cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
447 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
449 gauge_t percent = 100.0 * rates[state] / sum;
450 submit_percent (cpu_num, state, percent);
452 } /* }}} void cpu_commit_one */
454 /* Resets the internal aggregation. This is called by the read callback after
455 * each iteration / after each call to cpu_commit(). */
456 static void cpu_reset (void) /* {{{ */
460 for (i = 0; i < cpu_states_num; i++)
461 cpu_states[i].has_value = 0;
464 } /* }}} void cpu_reset */
466 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
467 static void cpu_commit_without_aggregation (void) /* {{{ */
471 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
475 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
477 cpu_state_t *s = get_cpu_state (cpu_num, state);
482 submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
485 } /* }}} void cpu_commit_without_aggregation */
487 /* Aggregates the internal state and dispatches the metrics. */
488 static void cpu_commit (void) /* {{{ */
490 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
491 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
495 if (report_by_state && report_by_cpu && !report_percent)
497 cpu_commit_without_aggregation ();
501 aggregate (global_rates);
505 cpu_commit_one (-1, global_rates);
509 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
511 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
512 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
513 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
517 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
518 if (this_cpu_states[state].has_value)
519 local_rates[state] = this_cpu_states[state].rate;
521 cpu_commit_one ((int) cpu_num, local_rates);
523 } /* }}} void cpu_commit */
525 /* Adds a derive value to the internal state. This should be used by each read
526 * function for each state. At the end of the iteration, the read function
527 * should call cpu_commit(). */
528 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
534 if (state >= COLLECTD_CPU_STATE_ACTIVE)
537 status = cpu_states_alloc (cpu_num);
541 if (global_cpu_num <= cpu_num)
542 global_cpu_num = cpu_num + 1;
544 s = get_cpu_state (cpu_num, state);
547 status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
554 } /* }}} int cpu_stage */
556 static int cpu_read (void)
558 cdtime_t now = cdtime ();
560 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
563 kern_return_t status;
565 processor_cpu_load_info_data_t cpu_info;
566 mach_msg_type_number_t cpu_info_len;
570 for (cpu = 0; cpu < cpu_list_len; cpu++)
573 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
575 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
576 (processor_info_t) &cpu_info, &cpu_info_len);
577 if (status != KERN_SUCCESS)
579 ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
580 mach_error_string (status));
584 if (cpu_info_len < COLLECTD_CPU_STATE_MAX)
586 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
590 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_USER], now);
591 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_NICE], now);
592 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_SYSTEM], now);
593 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_IDLE], now);
595 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
597 #elif defined(KERNEL_LINUX) /* {{{ */
605 if ((fh = fopen ("/proc/stat", "r")) == NULL)
608 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
609 sstrerror (errno, errbuf, sizeof (errbuf)));
613 while (fgets (buf, 1024, fh) != NULL)
615 if (strncmp (buf, "cpu", 3))
617 if ((buf[3] < '0') || (buf[3] > '9'))
620 numfields = strsplit (buf, fields, 9);
624 cpu = atoi (fields[0] + 3);
626 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
627 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
628 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
629 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) atoll(fields[4]), now);
633 cpu_stage (cpu, COLLECTD_CPU_STATE_WAIT, (derive_t) atoll(fields[5]), now);
634 cpu_stage (cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
635 cpu_stage (cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t) atoll(fields[7]), now);
638 cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
642 /* }}} #endif defined(KERNEL_LINUX) */
644 #elif defined(HAVE_LIBKSTAT) /* {{{ */
646 static cpu_stat_t cs;
651 for (cpu = 0; cpu < numcpu; cpu++)
653 if (kstat_read (kc, ksp[cpu], &cs) == -1)
654 continue; /* error message? */
656 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE, (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE], now);
657 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER, (derive_t) cs.cpu_sysinfo.cpu[CPU_USER], now);
658 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
659 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT, (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT], now);
661 /* }}} #endif defined(HAVE_LIBKSTAT) */
663 #elif CAN_USE_SYSCTL /* {{{ */
664 uint64_t cpuinfo[numcpu][CPUSTATES];
671 ERROR ("cpu plugin: Could not determine number of "
672 "installed CPUs using sysctl(3).");
676 memset (cpuinfo, 0, sizeof (cpuinfo));
678 #if defined(KERN_CPTIME2)
680 for (i = 0; i < numcpu; i++) {
681 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
683 cpuinfo_size = sizeof (cpuinfo[0]);
685 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
686 cpuinfo[i], &cpuinfo_size, NULL, 0);
689 ERROR ("cpu plugin: sysctl failed: %s.",
690 sstrerror (errno, errbuf, sizeof (errbuf)));
696 #endif /* defined(KERN_CPTIME2) */
698 int mib[] = {CTL_KERN, KERN_CPTIME};
699 long cpuinfo_tmp[CPUSTATES];
701 cpuinfo_size = sizeof(cpuinfo_tmp);
703 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
704 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
708 ERROR ("cpu plugin: sysctl failed: %s.",
709 sstrerror (errno, errbuf, sizeof (errbuf)));
713 for(i = 0; i < CPUSTATES; i++) {
714 cpuinfo[0][i] = cpuinfo_tmp[i];
718 for (i = 0; i < numcpu; i++) {
719 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
720 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
721 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
722 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
723 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
725 /* }}} #endif CAN_USE_SYSCTL */
727 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
728 long cpuinfo[maxcpu][CPUSTATES];
732 memset (cpuinfo, 0, sizeof (cpuinfo));
734 cpuinfo_size = sizeof (cpuinfo);
735 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
738 ERROR ("cpu plugin: sysctlbyname failed: %s.",
739 sstrerror (errno, errbuf, sizeof (errbuf)));
743 for (i = 0; i < numcpu; i++) {
744 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
745 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
746 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
747 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
748 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
750 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
752 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
753 long cpuinfo[CPUSTATES];
756 cpuinfo_size = sizeof (cpuinfo);
758 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
761 ERROR ("cpu plugin: sysctlbyname failed: %s.",
762 sstrerror (errno, errbuf, sizeof (errbuf)));
766 cpu_stage (0, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[CP_USER], now);
767 cpu_stage (0, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[CP_NICE], now);
768 cpu_stage (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[CP_SYS], now);
769 cpu_stage (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[CP_IDLE], now);
770 cpu_stage (0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
771 /* }}} #endif HAVE_SYSCTLBYNAME */
773 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
775 cs = sg_get_cpu_stats ();
779 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
783 cpu_state (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cs->idle);
784 cpu_state (0, COLLECTD_CPU_STATE_NICE, (derive_t) cs->nice);
785 cpu_state (0, COLLECTD_CPU_STATE_SWAP, (derive_t) cs->swap);
786 cpu_state (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs->kernel);
787 cpu_state (0, COLLECTD_CPU_STATE_USER, (derive_t) cs->user);
788 cpu_state (0, COLLECTD_CPU_STATE_WAIT, (derive_t) cs->iowait);
789 /* }}} #endif HAVE_LIBSTATGRAB */
791 #elif defined(HAVE_PERFSTAT) /* {{{ */
795 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
799 WARNING ("cpu plugin: perfstat_cpu: %s",
800 sstrerror (errno, errbuf, sizeof (errbuf)));
804 if (pnumcpu != numcpu || perfcpu == NULL)
808 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
813 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
816 WARNING ("cpu plugin: perfstat_cpu: %s",
817 sstrerror (errno, errbuf, sizeof (errbuf)));
821 for (i = 0; i < cpus; i++)
823 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) perfcpu[i].idle, now);
824 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys, now);
825 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) perfcpu[i].user, now);
826 cpu_stage (i, COLLECTD_CPU_STATE_WAIT, (derive_t) perfcpu[i].wait, now);
828 #endif /* }}} HAVE_PERFSTAT */
835 void module_register (void)
837 plugin_register_init ("cpu", init);
838 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
839 plugin_register_read ("cpu", cpu_read);
840 } /* void module_register */
842 /* vim: set sw=8 sts=8 noet fdm=marker : */