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) || \
72 !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
80 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
83 #if defined(CTL_HW) && defined(HW_NCPU) && defined(CTL_KERN) && \
84 defined(KERN_CPTIME) && defined(CPUSTATES)
85 #define CAN_USE_SYSCTL 1
87 #define CAN_USE_SYSCTL 0
90 #define CAN_USE_SYSCTL 0
93 #define COLLECTD_CPU_STATE_USER 0
94 #define COLLECTD_CPU_STATE_SYSTEM 1
95 #define COLLECTD_CPU_STATE_WAIT 2
96 #define COLLECTD_CPU_STATE_NICE 3
97 #define COLLECTD_CPU_STATE_SWAP 4
98 #define COLLECTD_CPU_STATE_INTERRUPT 5
99 #define COLLECTD_CPU_STATE_SOFTIRQ 6
100 #define COLLECTD_CPU_STATE_STEAL 7
101 #define COLLECTD_CPU_STATE_GUEST 8
102 #define COLLECTD_CPU_STATE_GUEST_NICE 9
103 #define COLLECTD_CPU_STATE_IDLE 10
104 #define COLLECTD_CPU_STATE_ACTIVE 11 /* sum of (!idle) */
105 #define COLLECTD_CPU_STATE_MAX 12 /* #states */
108 #include <statgrab.h>
112 #include <libperfstat.h>
113 #include <sys/protosw.h>
114 #endif /* HAVE_PERFSTAT */
116 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
117 !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && \
119 #error "No applicable input method."
122 static const char *cpu_state_names[] = {
123 "user", "system", "wait", "nice", "swap", "interrupt",
124 "softirq", "steal", "guest", "guest_nice", "idle", "active"};
126 #ifdef PROCESSOR_CPU_LOAD_INFO
127 static mach_port_t port_host;
128 static processor_port_array_t cpu_list;
129 static mach_msg_type_number_t cpu_list_len;
130 /* #endif PROCESSOR_CPU_LOAD_INFO */
132 #elif defined(KERNEL_LINUX)
133 /* no variables needed */
134 /* #endif KERNEL_LINUX */
136 #elif defined(HAVE_LIBKSTAT)
140 /* colleague tells me that Sun doesn't sell systems with more than 100 or so
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)
168 #define TOTAL_STAT_NUM 4
169 static value_to_rate_state_t total_conv[TOTAL_STAT_NUM];
170 static perfstat_cpu_t *perfcpu;
173 #endif /* HAVE_PERFSTAT */
175 #define RATE_ADD(sum, val) \
179 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;
191 static size_t cpu_states_num; /* #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;
197 static bool report_by_cpu = true;
198 static bool report_by_state = true;
199 static bool report_percent;
200 static bool report_num_cpu;
201 static bool report_guest;
202 static bool subtract_guest = true;
204 static const char *config_keys[] = {"ReportByCpu", "ReportByState",
205 "ReportNumCpu", "ValuesPercentage",
206 "ReportGuestState", "SubtractGuestState"};
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);
213 else if (strcasecmp(key, "ValuesPercentage") == 0)
214 report_percent = IS_TRUE(value);
215 else if (strcasecmp(key, "ReportByState") == 0)
216 report_by_state = IS_TRUE(value);
217 else if (strcasecmp(key, "ReportNumCpu") == 0)
218 report_num_cpu = IS_TRUE(value);
219 else if (strcasecmp(key, "ReportGuestState") == 0)
220 report_guest = IS_TRUE(value);
221 else if (strcasecmp(key, "SubtractGuestState") == 0)
222 subtract_guest = IS_TRUE(value);
227 } /* }}} int cpu_config */
229 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) {
237 ERROR("cpu plugin: Don't have a privileged host control port. "
238 "The most common cause for this problem is "
239 "that collectd is running without root "
240 "privileges, which are required to read CPU "
242 "<https://collectd.org/bugs/22>");
246 if (status != KERN_SUCCESS) {
247 ERROR("cpu plugin: host_processors() failed with status %d.", (int)status);
252 INFO("cpu plugin: Found %i processor%s.", (int)cpu_list_len,
253 cpu_list_len == 1 ? "" : "s");
254 /* #endif PROCESSOR_CPU_LOAD_INFO */
256 #elif defined(HAVE_LIBKSTAT)
264 /* Solaris doesn't count linear.. *sigh* */
265 for (numcpu = 0, ksp_chain = kc->kc_chain;
266 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
267 ksp_chain = ksp_chain->ks_next)
268 if (strncmp(ksp_chain->ks_module, "cpu_stat", 8) == 0)
269 ksp[numcpu++] = ksp_chain;
270 /* #endif HAVE_LIBKSTAT */
274 int mib[2] = {CTL_HW, HW_NCPU};
278 numcpu_size = sizeof(numcpu);
280 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &numcpu, &numcpu_size, NULL, 0);
282 WARNING("cpu plugin: sysctl: %s", STRERRNO);
285 /* #endif CAN_USE_SYSCTL */
287 #elif defined(HAVE_SYSCTLBYNAME)
290 numcpu_size = sizeof(numcpu);
292 if (sysctlbyname("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) {
293 WARNING("cpu plugin: sysctlbyname(hw.ncpu): %s", STRERRNO);
297 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
298 numcpu_size = sizeof(maxcpu);
300 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) {
301 WARNING("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s", STRERRNO);
306 NOTICE("cpu: Only one processor supported when using `sysctlbyname' (found "
310 /* #endif HAVE_SYSCTLBYNAME */
312 #elif defined(HAVE_LIBSTATGRAB)
313 /* nothing to initialize */
314 /* #endif HAVE_LIBSTATGRAB */
316 #elif defined(HAVE_PERFSTAT)
317 /* nothing to initialize */
318 #endif /* HAVE_PERFSTAT */
323 static void submit_value(int cpu_num, int cpu_state, const char *type,
325 value_list_t vl = VALUE_LIST_INIT;
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 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
338 plugin_dispatch_values(&vl);
341 static void submit_percent(int cpu_num, int cpu_state, gauge_t value) {
342 /* This function is called for all known CPU states, but each read
343 * method will only report a subset. The remaining states are left as
344 * NAN and we ignore them here. */
348 submit_value(cpu_num, cpu_state, "percent", (value_t){.gauge = value});
351 static void submit_derive(int cpu_num, int cpu_state, derive_t value) {
352 submit_value(cpu_num, cpu_state, "cpu", (value_t){.derive = value});
355 /* Takes the zero-index number of a CPU and makes sure that the module-global
356 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
357 static int cpu_states_alloc(size_t cpu_num) /* {{{ */
362 sz = (((size_t)cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
365 /* We already have enough space. */
366 if (cpu_states_num >= sz)
369 tmp = realloc(cpu_states, sz * sizeof(*cpu_states));
371 ERROR("cpu plugin: realloc failed.");
375 tmp = cpu_states + cpu_states_num;
377 memset(tmp, 0, (sz - cpu_states_num) * sizeof(*cpu_states));
380 } /* }}} cpu_states_alloc */
382 static cpu_state_t *get_cpu_state(size_t cpu_num, size_t state) /* {{{ */
384 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
386 if (index >= cpu_states_num)
389 return &cpu_states[index];
390 } /* }}} cpu_state_t *get_cpu_state */
392 #if defined(HAVE_PERFSTAT) /* {{{ */
393 /* populate global aggregate cpu rate */
394 static int total_rate(gauge_t *sum_by_state, size_t state, derive_t d,
395 value_to_rate_state_t *conv, cdtime_t now) {
398 value_to_rate(&rate, (value_t){.derive = d}, DS_TYPE_DERIVE, now, conv);
402 sum_by_state[state] = rate;
404 if (state != COLLECTD_CPU_STATE_IDLE)
405 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE], sum_by_state[state]);
408 #endif /* }}} HAVE_PERFSTAT */
410 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global
413 static void aggregate(gauge_t *sum_by_state) /* {{{ */
415 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
416 sum_by_state[state] = NAN;
418 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
419 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
421 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
423 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
424 if (!this_cpu_states[state].has_value)
427 RATE_ADD(sum_by_state[state], this_cpu_states[state].rate);
428 if (state != COLLECTD_CPU_STATE_IDLE)
429 RATE_ADD(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate,
430 this_cpu_states[state].rate);
433 if (!isnan(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
434 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = true;
436 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE],
437 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
440 #if defined(HAVE_PERFSTAT) /* {{{ */
441 cdtime_t now = cdtime();
442 perfstat_cpu_total_t cputotal = {0};
444 if (!perfstat_cpu_total(NULL, &cputotal, sizeof(cputotal), 1)) {
445 WARNING("cpu plugin: perfstat_cpu_total: %s", STRERRNO);
449 /* Reset COLLECTD_CPU_STATE_ACTIVE */
450 sum_by_state[COLLECTD_CPU_STATE_ACTIVE] = NAN;
452 /* Physical Processor Utilization */
453 total_rate(sum_by_state, COLLECTD_CPU_STATE_IDLE, (derive_t)cputotal.pidle,
454 &total_conv[TOTAL_IDLE], now);
455 total_rate(sum_by_state, COLLECTD_CPU_STATE_USER, (derive_t)cputotal.puser,
456 &total_conv[TOTAL_USER], now);
457 total_rate(sum_by_state, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cputotal.psys,
458 &total_conv[TOTAL_SYS], now);
459 total_rate(sum_by_state, COLLECTD_CPU_STATE_WAIT, (derive_t)cputotal.pwait,
460 &total_conv[TOTAL_WAIT], now);
461 #endif /* }}} HAVE_PERFSTAT */
462 } /* }}} void aggregate */
464 /* Commits (dispatches) the values for one CPU or the global aggregation.
465 * cpu_num is the index of the CPU to be committed or -1 in case of the global
466 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values
468 * current rate; each rate may be NAN. Calculates the percentage of each state
469 * and dispatches the metric. */
470 static void cpu_commit_one(int cpu_num, /* {{{ */
471 gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
474 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
475 RATE_ADD(sum, rates[COLLECTD_CPU_STATE_IDLE]);
477 if (!report_by_state) {
478 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
479 submit_percent(cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
483 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
484 gauge_t percent = 100.0 * rates[state] / sum;
485 submit_percent(cpu_num, state, percent);
487 } /* }}} void cpu_commit_one */
489 /* Commits the number of cores */
490 static void cpu_commit_num_cpu(gauge_t value) /* {{{ */
492 value_list_t vl = VALUE_LIST_INIT;
494 vl.values = &(value_t){.gauge = value};
497 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
498 sstrncpy(vl.type, "count", sizeof(vl.type));
500 plugin_dispatch_values(&vl);
501 } /* }}} void cpu_commit_num_cpu */
503 /* Resets the internal aggregation. This is called by the read callback after
504 * each iteration / after each call to cpu_commit(). */
505 static void cpu_reset(void) /* {{{ */
507 for (size_t i = 0; i < cpu_states_num; i++)
508 cpu_states[i].has_value = false;
511 } /* }}} void cpu_reset */
513 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
514 static void cpu_commit_without_aggregation(void) /* {{{ */
516 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
517 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
518 cpu_state_t *s = get_cpu_state(cpu_num, state);
523 submit_derive((int)cpu_num, (int)state, s->conv.last_value.derive);
526 } /* }}} void cpu_commit_without_aggregation */
528 /* Aggregates the internal state and dispatches the metrics. */
529 static void cpu_commit(void) /* {{{ */
531 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
532 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
536 cpu_commit_num_cpu((gauge_t)global_cpu_num);
538 if (report_by_state && report_by_cpu && !report_percent) {
539 cpu_commit_without_aggregation();
543 aggregate(global_rates);
545 if (!report_by_cpu) {
546 cpu_commit_one(-1, global_rates);
550 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
551 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
552 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
553 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN};
555 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
556 if (this_cpu_states[state].has_value)
557 local_rates[state] = this_cpu_states[state].rate;
559 cpu_commit_one((int)cpu_num, local_rates);
561 } /* }}} void cpu_commit */
563 /* Adds a derive value to the internal state. This should be used by each read
564 * function for each state. At the end of the iteration, the read function
565 * should call cpu_commit(). */
566 static int cpu_stage(size_t cpu_num, size_t state, derive_t d,
567 cdtime_t now) /* {{{ */
572 value_t val = {.derive = d};
574 if (state >= COLLECTD_CPU_STATE_ACTIVE)
577 status = cpu_states_alloc(cpu_num);
581 if (global_cpu_num <= cpu_num)
582 global_cpu_num = cpu_num + 1;
584 s = get_cpu_state(cpu_num, state);
586 status = value_to_rate(&rate, val, DS_TYPE_DERIVE, now, &s->conv);
593 } /* }}} int cpu_stage */
595 static int cpu_read(void) {
596 cdtime_t now = cdtime();
598 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
599 kern_return_t status;
601 processor_cpu_load_info_data_t cpu_info;
602 mach_msg_type_number_t cpu_info_len;
606 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++) {
608 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
610 status = processor_info(cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
611 (processor_info_t)&cpu_info, &cpu_info_len);
612 if (status != KERN_SUCCESS) {
613 ERROR("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
614 mach_error_string(status));
618 if (cpu_info_len < CPU_STATE_MAX) {
619 ERROR("cpu plugin: processor_info returned only %i elements..",
624 cpu_stage(cpu, COLLECTD_CPU_STATE_USER,
625 (derive_t)cpu_info.cpu_ticks[CPU_STATE_USER], now);
626 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE,
627 (derive_t)cpu_info.cpu_ticks[CPU_STATE_NICE], now);
628 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM,
629 (derive_t)cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
630 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE,
631 (derive_t)cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
633 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
635 #elif defined(KERNEL_LINUX) /* {{{ */
643 if ((fh = fopen("/proc/stat", "r")) == NULL) {
644 ERROR("cpu plugin: fopen (/proc/stat) failed: %s", STRERRNO);
648 while (fgets(buf, 1024, fh) != NULL) {
649 if (strncmp(buf, "cpu", 3))
651 if ((buf[3] < '0') || (buf[3] > '9'))
654 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
658 cpu = atoi(fields[0] + 3);
660 /* Do not stage User and Nice immediately: we may need to alter them later:
662 long long user_value = atoll(fields[1]);
663 long long nice_value = atoll(fields[2]);
664 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
665 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
667 if (numfields >= 8) {
668 cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
669 cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
671 cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
675 if (numfields >= 9) { /* Steal (since Linux 2.6.11) */
676 cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]), now);
679 if (numfields >= 10) { /* Guest (since Linux 2.6.24) */
681 long long value = atoll(fields[9]);
682 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST, (derive_t)value, now);
683 /* Guest is included in User; optionally subtract Guest from User: */
684 if (subtract_guest) {
692 if (numfields >= 11) { /* Guest_nice (since Linux 2.6.33) */
694 long long value = atoll(fields[10]);
695 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST_NICE, (derive_t)value, now);
696 /* Guest_nice is included in Nice; optionally subtract Guest_nice from
698 if (subtract_guest) {
706 /* Eventually stage User and Nice: */
707 cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)user_value, now);
708 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)nice_value, now);
711 /* }}} #endif defined(KERNEL_LINUX) */
713 #elif defined(HAVE_LIBKSTAT) /* {{{ */
714 static cpu_stat_t cs;
719 for (int cpu = 0; cpu < numcpu; cpu++) {
720 if (kstat_read(kc, ksp[cpu], &cs) == -1)
721 continue; /* error message? */
723 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,
724 (derive_t)cs.cpu_sysinfo.cpu[CPU_IDLE], now);
725 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,
726 (derive_t)cs.cpu_sysinfo.cpu[CPU_USER], now);
727 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM,
728 (derive_t)cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
729 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,
730 (derive_t)cs.cpu_sysinfo.cpu[CPU_WAIT], now);
732 /* }}} #endif defined(HAVE_LIBKSTAT) */
734 #elif CAN_USE_SYSCTL /* {{{ */
735 uint64_t cpuinfo[numcpu][CPUSTATES];
740 ERROR("cpu plugin: Could not determine number of "
741 "installed CPUs using sysctl(3).");
745 memset(cpuinfo, 0, sizeof(cpuinfo));
747 #if defined(KERN_CPTIME2)
749 for (int i = 0; i < numcpu; i++) {
750 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
752 cpuinfo_size = sizeof(cpuinfo[0]);
754 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), cpuinfo[i], &cpuinfo_size,
757 ERROR("cpu plugin: sysctl failed: %s.", STRERRNO);
762 #endif /* defined(KERN_CPTIME2) */
764 int mib[] = {CTL_KERN, KERN_CPTIME};
765 long cpuinfo_tmp[CPUSTATES];
767 cpuinfo_size = sizeof(cpuinfo_tmp);
769 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &cpuinfo_tmp, &cpuinfo_size,
772 ERROR("cpu plugin: sysctl failed: %s.", STRERRNO);
776 for (int i = 0; i < CPUSTATES; i++) {
777 cpuinfo[0][i] = cpuinfo_tmp[i];
781 for (int i = 0; i < numcpu; i++) {
782 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
783 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
784 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
785 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
786 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
789 /* }}} #endif CAN_USE_SYSCTL */
791 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \
793 long cpuinfo[maxcpu][CPUSTATES];
796 memset(cpuinfo, 0, sizeof(cpuinfo));
798 cpuinfo_size = sizeof(cpuinfo);
799 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
800 ERROR("cpu plugin: sysctlbyname failed: %s.", STRERRNO);
804 for (int i = 0; i < numcpu; i++) {
805 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
806 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
807 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
808 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
809 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
812 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
814 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
815 long cpuinfo[CPUSTATES];
818 cpuinfo_size = sizeof(cpuinfo);
820 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
821 ERROR("cpu plugin: sysctlbyname failed: %s.", STRERRNO);
825 cpu_stage(0, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[CP_USER], now);
826 cpu_stage(0, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[CP_NICE], now);
827 cpu_stage(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[CP_SYS], now);
828 cpu_stage(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[CP_IDLE], now);
829 cpu_stage(0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[CP_INTR], now);
830 /* }}} #endif HAVE_SYSCTLBYNAME */
832 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
834 cs = sg_get_cpu_stats();
837 ERROR("cpu plugin: sg_get_cpu_stats failed.");
841 cpu_state(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cs->idle);
842 cpu_state(0, COLLECTD_CPU_STATE_NICE, (derive_t)cs->nice);
843 cpu_state(0, COLLECTD_CPU_STATE_SWAP, (derive_t)cs->swap);
844 cpu_state(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cs->kernel);
845 cpu_state(0, COLLECTD_CPU_STATE_USER, (derive_t)cs->user);
846 cpu_state(0, COLLECTD_CPU_STATE_WAIT, (derive_t)cs->iowait);
847 /* }}} #endif HAVE_LIBSTATGRAB */
849 #elif defined(HAVE_PERFSTAT) /* {{{ */
853 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
855 WARNING("cpu plugin: perfstat_cpu: %s", STRERRNO);
859 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) {
867 WARNING("cpu plugin: perfstat_cpu: %s", STRERRNO);
871 for (int i = 0; i < cpus; i++) {
872 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)perfcpu[i].idle, now);
873 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)perfcpu[i].sys, now);
874 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)perfcpu[i].user, now);
875 cpu_stage(i, COLLECTD_CPU_STATE_WAIT, (derive_t)perfcpu[i].wait, now);
877 #endif /* }}} HAVE_PERFSTAT */
884 void module_register(void) {
885 plugin_register_init("cpu", init);
886 plugin_register_config("cpu", cpu_config, config_keys, config_keys_num);
887 plugin_register_read("cpu", cpu_read);
888 } /* void module_register */