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>
35 #ifdef HAVE_MACH_KERN_RETURN_H
36 # include <mach/kern_return.h>
38 #ifdef HAVE_MACH_MACH_INIT_H
39 # include <mach/mach_init.h>
41 #ifdef HAVE_MACH_HOST_PRIV_H
42 # include <mach/host_priv.h>
44 #if HAVE_MACH_MACH_ERROR_H
45 # include <mach/mach_error.h>
47 #ifdef HAVE_MACH_PROCESSOR_INFO_H
48 # include <mach/processor_info.h>
50 #ifdef HAVE_MACH_PROCESSOR_H
51 # include <mach/processor.h>
53 #ifdef HAVE_MACH_VM_MAP_H
54 # include <mach/vm_map.h>
58 # include <sys/sysinfo.h>
59 #endif /* HAVE_LIBKSTAT */
61 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
62 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
63 # ifdef HAVE_SYS_SYSCTL_H
64 # include <sys/sysctl.h>
67 # ifdef HAVE_SYS_DKSTAT_H
68 # include <sys/dkstat.h>
71 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
79 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
82 # if defined(CTL_HW) && defined(HW_NCPU) \
83 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
84 # define CAN_USE_SYSCTL 1
86 # define CAN_USE_SYSCTL 0
89 # define CAN_USE_SYSCTL 0
92 #define COLLECTD_CPU_STATE_USER 0
93 #define COLLECTD_CPU_STATE_SYSTEM 1
94 #define COLLECTD_CPU_STATE_WAIT 2
95 #define COLLECTD_CPU_STATE_NICE 3
96 #define COLLECTD_CPU_STATE_SWAP 4
97 #define COLLECTD_CPU_STATE_INTERRUPT 5
98 #define COLLECTD_CPU_STATE_SOFTIRQ 6
99 #define COLLECTD_CPU_STATE_STEAL 7
100 #define COLLECTD_CPU_STATE_IDLE 8
101 #define COLLECTD_CPU_STATE_ACTIVE 9 /* sum of (!idle) */
102 #define COLLECTD_CPU_STATE_MAX 10 /* #states */
105 # include <statgrab.h>
108 # ifdef HAVE_PERFSTAT
109 # include <sys/protosw.h>
110 # include <libperfstat.h>
111 # endif /* HAVE_PERFSTAT */
113 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
114 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
115 # error "No applicable input method."
118 static const char *cpu_state_names[] = {
131 #ifdef PROCESSOR_CPU_LOAD_INFO
132 static mach_port_t port_host;
133 static processor_port_array_t cpu_list;
134 static mach_msg_type_number_t cpu_list_len;
135 /* #endif PROCESSOR_CPU_LOAD_INFO */
137 #elif defined(KERNEL_LINUX)
138 /* no variables needed */
139 /* #endif KERNEL_LINUX */
141 #elif defined(HAVE_LIBKSTAT)
142 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
143 # define MAX_NUMCPU 256
144 extern kstat_ctl_t *kc;
145 static kstat_t *ksp[MAX_NUMCPU];
147 /* #endif HAVE_LIBKSTAT */
151 /* #endif CAN_USE_SYSCTL */
153 #elif defined(HAVE_SYSCTLBYNAME)
155 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
157 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
158 /* #endif HAVE_SYSCTLBYNAME */
160 #elif defined(HAVE_LIBSTATGRAB)
161 /* no variables needed */
162 /* #endif HAVE_LIBSTATGRAB */
164 #elif defined(HAVE_PERFSTAT)
169 #define TOTAL_STAT_NUM 4
170 static value_to_rate_state_t total_conv[TOTAL_STAT_NUM];
171 static perfstat_cpu_t *perfcpu;
174 #endif /* HAVE_PERFSTAT */
176 #define RATE_ADD(sum, val) do { \
179 else if (!isnan (val)) \
185 value_to_rate_state_t conv;
189 typedef struct cpu_state_s cpu_state_t;
191 static cpu_state_t *cpu_states = NULL;
192 static size_t cpu_states_num = 0; /* #cpu_states allocated */
194 /* Highest CPU number in the current iteration. Used by the dispatch logic to
195 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
196 static size_t global_cpu_num = 0;
198 static _Bool report_by_cpu = 1;
199 static _Bool report_by_state = 1;
200 static _Bool report_percent = 0;
201 static _Bool report_num_cpu = 0;
203 static const char *config_keys[] =
210 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
212 static int cpu_config (char const *key, char const *value) /* {{{ */
214 if (strcasecmp (key, "ReportByCpu") == 0)
215 report_by_cpu = IS_TRUE (value) ? 1 : 0;
216 else if (strcasecmp (key, "ValuesPercentage") == 0)
217 report_percent = IS_TRUE (value) ? 1 : 0;
218 else if (strcasecmp (key, "ReportByState") == 0)
219 report_by_state = IS_TRUE (value) ? 1 : 0;
220 else if (strcasecmp (key, "ReportNumCpu") == 0)
221 report_num_cpu = IS_TRUE (value) ? 1 : 0;
226 } /* }}} int cpu_config */
228 static int init (void)
230 #if PROCESSOR_CPU_LOAD_INFO
231 kern_return_t status;
233 port_host = mach_host_self ();
235 status = host_processors (port_host, &cpu_list, &cpu_list_len);
236 if (status == KERN_INVALID_ARGUMENT)
238 ERROR ("cpu plugin: Don't have a privileged host control port. "
239 "The most common cause for this problem is "
240 "that collectd is running without root "
241 "privileges, which are required to read CPU "
243 "<https://collectd.org/bugs/22>");
247 if (status != KERN_SUCCESS)
249 ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status);
254 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
255 /* #endif PROCESSOR_CPU_LOAD_INFO */
257 #elif defined(HAVE_LIBKSTAT)
265 /* Solaris doesn't count linear.. *sigh* */
266 for (numcpu = 0, ksp_chain = kc->kc_chain;
267 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
268 ksp_chain = ksp_chain->ks_next)
269 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
270 ksp[numcpu++] = ksp_chain;
271 /* #endif HAVE_LIBKSTAT */
275 int mib[2] = {CTL_HW, HW_NCPU};
279 numcpu_size = sizeof (numcpu);
281 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
282 &numcpu, &numcpu_size, NULL, 0);
286 WARNING ("cpu plugin: sysctl: %s",
287 sstrerror (errno, errbuf, sizeof (errbuf)));
290 /* #endif CAN_USE_SYSCTL */
292 #elif defined (HAVE_SYSCTLBYNAME)
295 numcpu_size = sizeof (numcpu);
297 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
300 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
301 sstrerror (errno, errbuf, sizeof (errbuf)));
305 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
306 numcpu_size = sizeof (maxcpu);
308 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
311 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
312 sstrerror (errno, errbuf, sizeof (errbuf)));
317 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
319 /* #endif HAVE_SYSCTLBYNAME */
321 #elif defined(HAVE_LIBSTATGRAB)
322 /* nothing to initialize */
323 /* #endif HAVE_LIBSTATGRAB */
325 #elif defined(HAVE_PERFSTAT)
326 /* nothing to initialize */
327 #endif /* HAVE_PERFSTAT */
332 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
334 value_list_t vl = VALUE_LIST_INIT;
339 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
340 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
341 sstrncpy (vl.type, type, sizeof (vl.type));
342 sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
343 sizeof (vl.type_instance));
346 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
349 plugin_dispatch_values (&vl);
352 static void submit_percent (int cpu_num, int cpu_state, gauge_t value)
354 /* This function is called for all known CPU states, but each read
355 * method will only report a subset. The remaining states are left as
356 * NAN and we ignore them here. */
360 submit_value (cpu_num, cpu_state, "percent",
361 (value_t) { .gauge = value });
364 static void submit_derive (int cpu_num, int cpu_state, derive_t value)
366 submit_value (cpu_num, cpu_state, "cpu",
367 (value_t) { .derive = value });
370 /* Takes the zero-index number of a CPU and makes sure that the module-global
371 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
372 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
377 sz = (((size_t) cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
380 /* We already have enough space. */
381 if (cpu_states_num >= sz)
384 tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
387 ERROR ("cpu plugin: realloc failed.");
391 tmp = cpu_states + cpu_states_num;
393 memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
396 } /* }}} cpu_states_alloc */
398 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
400 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
402 if (index >= cpu_states_num)
405 return (&cpu_states[index]);
406 } /* }}} cpu_state_t *get_cpu_state */
408 #if defined(HAVE_PERFSTAT) /* {{{ */
409 /* populate global aggregate cpu rate */
410 static int total_rate(gauge_t *sum_by_state, size_t state, derive_t d,
411 value_to_rate_state_t* conv, cdtime_t now)
414 value_t val = { .derive = d };
415 int status = value_to_rate (&rate, val, DS_TYPE_DERIVE, now, conv);
419 sum_by_state[state] = rate;
421 if (state != COLLECTD_CPU_STATE_IDLE)
422 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], sum_by_state[state]);
425 #endif /* }}} HAVE_PERFSTAT */
427 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global rate_by_state
429 static void aggregate (gauge_t *sum_by_state) /* {{{ */
431 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
432 sum_by_state[state] = NAN;
434 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
436 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
438 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
440 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
442 if (!this_cpu_states[state].has_value)
445 RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
446 if (state != COLLECTD_CPU_STATE_IDLE)
447 RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
450 if (!isnan (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
451 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
453 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
456 #if defined(HAVE_PERFSTAT) /* {{{ */
457 cdtime_t now = cdtime ();
458 perfstat_cpu_total_t cputotal;
459 memset(&cputotal, 0, sizeof(perfstat_cpu_total_t));
461 if (!perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1)) {
463 WARNING ("cpu plugin: perfstat_cpu_total: %s",
464 sstrerror (errno, errbuf, sizeof (errbuf)));
468 /* Reset COLLECTD_CPU_STATE_ACTIVE */
469 sum_by_state[COLLECTD_CPU_STATE_ACTIVE] = NAN;
471 /* Physical Processor Utilization */
472 total_rate(sum_by_state, COLLECTD_CPU_STATE_IDLE, (derive_t) cputotal.pidle, &total_conv[TOTAL_IDLE], now);
473 total_rate(sum_by_state, COLLECTD_CPU_STATE_USER, (derive_t) cputotal.puser, &total_conv[TOTAL_USER], now);
474 total_rate(sum_by_state, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cputotal.psys , &total_conv[TOTAL_SYS], now);
475 total_rate(sum_by_state, COLLECTD_CPU_STATE_WAIT, (derive_t) cputotal.pwait, &total_conv[TOTAL_WAIT], now);
476 #endif /* }}} HAVE_PERFSTAT */
477 } /* }}} void aggregate */
479 /* Commits (dispatches) the values for one CPU or the global aggregation.
480 * cpu_num is the index of the CPU to be committed or -1 in case of the global
481 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values holding the
482 * current rate; each rate may be NAN. Calculates the percentage of each state
483 * and dispatches the metric. */
484 static void cpu_commit_one (int cpu_num, /* {{{ */
485 gauge_t rates[static COLLECTD_CPU_STATE_MAX])
489 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
490 RATE_ADD (sum, rates[COLLECTD_CPU_STATE_IDLE]);
492 if (!report_by_state)
494 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
495 submit_percent (cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
499 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
501 gauge_t percent = 100.0 * rates[state] / sum;
502 submit_percent (cpu_num, state, percent);
504 } /* }}} void cpu_commit_one */
506 /* Commits the number of cores */
507 static void cpu_commit_num_cpu (gauge_t value) /* {{{ */
509 value_list_t vl = VALUE_LIST_INIT;
511 vl.values = &(value_t) { .gauge = value };
514 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
515 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
516 sstrncpy (vl.type, "count", sizeof (vl.type));
518 plugin_dispatch_values (&vl);
519 } /* }}} void cpu_commit_num_cpu */
521 /* Resets the internal aggregation. This is called by the read callback after
522 * each iteration / after each call to cpu_commit(). */
523 static void cpu_reset (void) /* {{{ */
525 for (size_t i = 0; i < cpu_states_num; i++)
526 cpu_states[i].has_value = 0;
529 } /* }}} void cpu_reset */
531 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
532 static void cpu_commit_without_aggregation (void) /* {{{ */
534 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
536 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
538 cpu_state_t *s = get_cpu_state (cpu_num, state);
543 submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
546 } /* }}} void cpu_commit_without_aggregation */
548 /* Aggregates the internal state and dispatches the metrics. */
549 static void cpu_commit (void) /* {{{ */
551 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
552 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
556 cpu_commit_num_cpu ((gauge_t) global_cpu_num);
558 if (report_by_state && report_by_cpu && !report_percent)
560 cpu_commit_without_aggregation ();
564 aggregate (global_rates);
568 cpu_commit_one (-1, global_rates);
572 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
574 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
575 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
576 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
579 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
580 if (this_cpu_states[state].has_value)
581 local_rates[state] = this_cpu_states[state].rate;
583 cpu_commit_one ((int) cpu_num, local_rates);
585 } /* }}} void cpu_commit */
587 /* Adds a derive value to the internal state. This should be used by each read
588 * function for each state. At the end of the iteration, the read function
589 * should call cpu_commit(). */
590 static int cpu_stage (size_t cpu_num, size_t state, derive_t d, cdtime_t now) /* {{{ */
595 value_t val = { .derive = d };
597 if (state >= COLLECTD_CPU_STATE_ACTIVE)
600 status = cpu_states_alloc (cpu_num);
604 if (global_cpu_num <= cpu_num)
605 global_cpu_num = cpu_num + 1;
607 s = get_cpu_state (cpu_num, state);
609 status = value_to_rate (&rate, val, DS_TYPE_DERIVE, now, &s->conv);
616 } /* }}} int cpu_stage */
618 static int cpu_read (void)
620 cdtime_t now = cdtime ();
622 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
623 kern_return_t status;
625 processor_cpu_load_info_data_t cpu_info;
626 mach_msg_type_number_t cpu_info_len;
630 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++)
633 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
635 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
636 (processor_info_t) &cpu_info, &cpu_info_len);
637 if (status != KERN_SUCCESS)
639 ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
640 mach_error_string (status));
644 if (cpu_info_len < CPU_STATE_MAX)
646 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
650 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER], now);
651 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE], now);
652 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
653 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
655 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
657 #elif defined(KERNEL_LINUX) /* {{{ */
665 if ((fh = fopen ("/proc/stat", "r")) == NULL)
668 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
669 sstrerror (errno, errbuf, sizeof (errbuf)));
673 while (fgets (buf, 1024, fh) != NULL)
675 if (strncmp (buf, "cpu", 3))
677 if ((buf[3] < '0') || (buf[3] > '9'))
680 numfields = strsplit (buf, fields, 9);
684 cpu = atoi (fields[0] + 3);
686 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
687 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
688 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
689 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) atoll(fields[4]), now);
693 cpu_stage (cpu, COLLECTD_CPU_STATE_WAIT, (derive_t) atoll(fields[5]), now);
694 cpu_stage (cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
695 cpu_stage (cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t) atoll(fields[7]), now);
698 cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
702 /* }}} #endif defined(KERNEL_LINUX) */
704 #elif defined(HAVE_LIBKSTAT) /* {{{ */
705 static cpu_stat_t cs;
710 for (int cpu = 0; cpu < numcpu; cpu++)
712 if (kstat_read (kc, ksp[cpu], &cs) == -1)
713 continue; /* error message? */
715 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE, (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE], now);
716 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER, (derive_t) cs.cpu_sysinfo.cpu[CPU_USER], now);
717 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
718 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT, (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT], now);
720 /* }}} #endif defined(HAVE_LIBKSTAT) */
722 #elif CAN_USE_SYSCTL /* {{{ */
723 uint64_t cpuinfo[numcpu][CPUSTATES];
729 ERROR ("cpu plugin: Could not determine number of "
730 "installed CPUs using sysctl(3).");
734 memset (cpuinfo, 0, sizeof (cpuinfo));
736 #if defined(KERN_CPTIME2)
738 for (int i = 0; i < numcpu; i++) {
739 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
741 cpuinfo_size = sizeof (cpuinfo[0]);
743 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
744 cpuinfo[i], &cpuinfo_size, NULL, 0);
747 ERROR ("cpu plugin: sysctl failed: %s.",
748 sstrerror (errno, errbuf, sizeof (errbuf)));
754 #endif /* defined(KERN_CPTIME2) */
756 int mib[] = {CTL_KERN, KERN_CPTIME};
757 long cpuinfo_tmp[CPUSTATES];
759 cpuinfo_size = sizeof(cpuinfo_tmp);
761 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
762 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
766 ERROR ("cpu plugin: sysctl failed: %s.",
767 sstrerror (errno, errbuf, sizeof (errbuf)));
771 for(int i = 0; i < CPUSTATES; i++) {
772 cpuinfo[0][i] = cpuinfo_tmp[i];
776 for (int i = 0; i < numcpu; i++) {
777 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
778 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
779 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
780 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
781 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
783 /* }}} #endif CAN_USE_SYSCTL */
785 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
786 long cpuinfo[maxcpu][CPUSTATES];
789 memset (cpuinfo, 0, sizeof (cpuinfo));
791 cpuinfo_size = sizeof (cpuinfo);
792 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
795 ERROR ("cpu plugin: sysctlbyname failed: %s.",
796 sstrerror (errno, errbuf, sizeof (errbuf)));
800 for (int i = 0; i < numcpu; i++) {
801 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
802 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
803 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
804 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
805 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
807 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
809 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
810 long cpuinfo[CPUSTATES];
813 cpuinfo_size = sizeof (cpuinfo);
815 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
818 ERROR ("cpu plugin: sysctlbyname failed: %s.",
819 sstrerror (errno, errbuf, sizeof (errbuf)));
823 cpu_stage (0, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[CP_USER], now);
824 cpu_stage (0, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[CP_NICE], now);
825 cpu_stage (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[CP_SYS], now);
826 cpu_stage (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[CP_IDLE], now);
827 cpu_stage (0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
828 /* }}} #endif HAVE_SYSCTLBYNAME */
830 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
832 cs = sg_get_cpu_stats ();
836 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
840 cpu_state (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cs->idle);
841 cpu_state (0, COLLECTD_CPU_STATE_NICE, (derive_t) cs->nice);
842 cpu_state (0, COLLECTD_CPU_STATE_SWAP, (derive_t) cs->swap);
843 cpu_state (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs->kernel);
844 cpu_state (0, COLLECTD_CPU_STATE_USER, (derive_t) cs->user);
845 cpu_state (0, COLLECTD_CPU_STATE_WAIT, (derive_t) cs->iowait);
846 /* }}} #endif HAVE_LIBSTATGRAB */
848 #elif defined(HAVE_PERFSTAT) /* {{{ */
852 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
856 WARNING ("cpu plugin: perfstat_cpu: %s",
857 sstrerror (errno, errbuf, sizeof (errbuf)));
861 if (pnumcpu != numcpu || perfcpu == NULL)
864 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
869 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
872 WARNING ("cpu plugin: perfstat_cpu: %s",
873 sstrerror (errno, errbuf, sizeof (errbuf)));
877 for (int i = 0; i < cpus; i++)
879 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) perfcpu[i].idle, now);
880 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys, now);
881 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) perfcpu[i].user, now);
882 cpu_stage (i, COLLECTD_CPU_STATE_WAIT, (derive_t) perfcpu[i].wait, now);
884 #endif /* }}} HAVE_PERFSTAT */
891 void module_register (void)
893 plugin_register_init ("cpu", init);
894 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
895 plugin_register_read ("cpu", cpu_read);
896 } /* void module_register */
898 /* vim: set sw=8 sts=8 noet fdm=marker : */