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[] = {"user", "system", "wait", "nice",
123 "swap", "interrupt", "softirq", "steal",
124 "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)
137 /* colleague tells me that Sun doesn't sell systems with more than 100 or so
139 #define MAX_NUMCPU 256
140 extern kstat_ctl_t *kc;
141 static kstat_t *ksp[MAX_NUMCPU];
143 /* #endif HAVE_LIBKSTAT */
147 /* #endif CAN_USE_SYSCTL */
149 #elif defined(HAVE_SYSCTLBYNAME)
151 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
153 #endif /* HAVE_SYSCTL_KERN_CP_TIMES */
154 /* #endif HAVE_SYSCTLBYNAME */
156 #elif defined(HAVE_LIBSTATGRAB)
157 /* no variables needed */
158 /* #endif HAVE_LIBSTATGRAB */
160 #elif defined(HAVE_PERFSTAT)
165 #define TOTAL_STAT_NUM 4
166 static value_to_rate_state_t total_conv[TOTAL_STAT_NUM];
167 static perfstat_cpu_t *perfcpu;
170 #endif /* HAVE_PERFSTAT */
172 #define RATE_ADD(sum, val) \
176 else if (!isnan(val)) \
181 value_to_rate_state_t conv;
185 typedef struct cpu_state_s cpu_state_t;
187 static cpu_state_t *cpu_states = NULL;
188 static size_t cpu_states_num = 0; /* #cpu_states allocated */
190 /* Highest CPU number in the current iteration. Used by the dispatch logic to
191 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
192 static size_t global_cpu_num = 0;
194 static _Bool report_by_cpu = 1;
195 static _Bool report_by_state = 1;
196 static _Bool report_percent = 0;
197 static _Bool report_num_cpu = 0;
198 static _Bool report_guest = 0;
199 static _Bool subtract_guest = 1;
201 static const char *config_keys[] = {"ReportByCpu", "ReportByState",
202 "ReportNumCpu", "ValuesPercentage",
203 "ReportGuestState", "SubtractGuestState"};
204 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
206 static int cpu_config(char const *key, char const *value) /* {{{ */
208 if (strcasecmp(key, "ReportByCpu") == 0)
209 report_by_cpu = IS_TRUE(value) ? 1 : 0;
210 else if (strcasecmp(key, "ValuesPercentage") == 0)
211 report_percent = IS_TRUE(value) ? 1 : 0;
212 else if (strcasecmp(key, "ReportByState") == 0)
213 report_by_state = IS_TRUE(value) ? 1 : 0;
214 else if (strcasecmp(key, "ReportNumCpu") == 0)
215 report_num_cpu = IS_TRUE(value) ? 1 : 0;
216 else if (strcasecmp(key, "ReportGuestState") == 0)
217 report_guest = IS_TRUE(value) ? 1 : 0;
218 else if (strcasecmp(key, "SubtractGuestState") == 0)
219 subtract_guest = IS_TRUE(value) ? 1 : 0;
224 } /* }}} int cpu_config */
226 static int init(void) {
227 #if PROCESSOR_CPU_LOAD_INFO
228 kern_return_t status;
230 port_host = mach_host_self();
232 status = host_processors(port_host, &cpu_list, &cpu_list_len);
233 if (status == KERN_INVALID_ARGUMENT) {
234 ERROR("cpu plugin: Don't have a privileged host control port. "
235 "The most common cause for this problem is "
236 "that collectd is running without root "
237 "privileges, which are required to read CPU "
239 "<https://collectd.org/bugs/22>");
243 if (status != KERN_SUCCESS) {
244 ERROR("cpu plugin: host_processors() failed with status %d.", (int)status);
249 INFO("cpu plugin: Found %i processor%s.", (int)cpu_list_len,
250 cpu_list_len == 1 ? "" : "s");
251 /* #endif PROCESSOR_CPU_LOAD_INFO */
253 #elif defined(HAVE_LIBKSTAT)
261 /* Solaris doesn't count linear.. *sigh* */
262 for (numcpu = 0, ksp_chain = kc->kc_chain;
263 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
264 ksp_chain = ksp_chain->ks_next)
265 if (strncmp(ksp_chain->ks_module, "cpu_stat", 8) == 0)
266 ksp[numcpu++] = ksp_chain;
267 /* #endif HAVE_LIBKSTAT */
271 int mib[2] = {CTL_HW, HW_NCPU};
275 numcpu_size = sizeof(numcpu);
277 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &numcpu, &numcpu_size, NULL, 0);
280 WARNING("cpu plugin: sysctl: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
283 /* #endif CAN_USE_SYSCTL */
285 #elif defined(HAVE_SYSCTLBYNAME)
288 numcpu_size = sizeof(numcpu);
290 if (sysctlbyname("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) {
292 WARNING("cpu plugin: sysctlbyname(hw.ncpu): %s",
293 sstrerror(errno, errbuf, sizeof(errbuf)));
297 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
298 numcpu_size = sizeof(maxcpu);
300 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) {
302 WARNING("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
303 sstrerror(errno, errbuf, sizeof(errbuf)));
308 NOTICE("cpu: Only one processor supported when using `sysctlbyname' (found "
312 /* #endif HAVE_SYSCTLBYNAME */
314 #elif defined(HAVE_LIBSTATGRAB)
315 /* nothing to initialize */
316 /* #endif HAVE_LIBSTATGRAB */
318 #elif defined(HAVE_PERFSTAT)
319 /* nothing to initialize */
320 #endif /* HAVE_PERFSTAT */
325 static void submit_value(int cpu_num, int cpu_state, const char *type,
327 value_list_t vl = VALUE_LIST_INIT;
332 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
333 sstrncpy(vl.type, type, sizeof(vl.type));
334 sstrncpy(vl.type_instance, cpu_state_names[cpu_state],
335 sizeof(vl.type_instance));
338 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
340 plugin_dispatch_values(&vl);
343 static void submit_percent(int cpu_num, int cpu_state, gauge_t value) {
344 /* This function is called for all known CPU states, but each read
345 * method will only report a subset. The remaining states are left as
346 * NAN and we ignore them here. */
350 submit_value(cpu_num, cpu_state, "percent", (value_t){.gauge = value});
353 static void submit_derive(int cpu_num, int cpu_state, derive_t value) {
354 submit_value(cpu_num, cpu_state, "cpu", (value_t){.derive = value});
357 /* Takes the zero-index number of a CPU and makes sure that the module-global
358 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
359 static int cpu_states_alloc(size_t cpu_num) /* {{{ */
364 sz = (((size_t)cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
367 /* We already have enough space. */
368 if (cpu_states_num >= sz)
371 tmp = realloc(cpu_states, sz * sizeof(*cpu_states));
373 ERROR("cpu plugin: realloc failed.");
377 tmp = cpu_states + cpu_states_num;
379 memset(tmp, 0, (sz - cpu_states_num) * sizeof(*cpu_states));
382 } /* }}} cpu_states_alloc */
384 static cpu_state_t *get_cpu_state(size_t cpu_num, size_t state) /* {{{ */
386 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
388 if (index >= cpu_states_num)
391 return &cpu_states[index];
392 } /* }}} cpu_state_t *get_cpu_state */
394 #if defined(HAVE_PERFSTAT) /* {{{ */
395 /* populate global aggregate cpu rate */
396 static int total_rate(gauge_t *sum_by_state, size_t state, derive_t d,
397 value_to_rate_state_t *conv, cdtime_t now) {
400 value_to_rate(&rate, (value_t){.derive = d}, DS_TYPE_DERIVE, now, conv);
404 sum_by_state[state] = rate;
406 if (state != COLLECTD_CPU_STATE_IDLE)
407 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE], sum_by_state[state]);
410 #endif /* }}} HAVE_PERFSTAT */
412 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global
415 static void aggregate(gauge_t *sum_by_state) /* {{{ */
417 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
418 sum_by_state[state] = NAN;
420 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
421 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
423 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
425 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
426 if (!this_cpu_states[state].has_value)
429 RATE_ADD(sum_by_state[state], this_cpu_states[state].rate);
430 if (state != COLLECTD_CPU_STATE_IDLE)
431 RATE_ADD(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate,
432 this_cpu_states[state].rate);
435 if (!isnan(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
436 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
438 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE],
439 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
442 #if defined(HAVE_PERFSTAT) /* {{{ */
443 cdtime_t now = cdtime();
444 perfstat_cpu_total_t cputotal = {0};
446 if (!perfstat_cpu_total(NULL, &cputotal, sizeof(cputotal), 1)) {
448 WARNING("cpu plugin: perfstat_cpu_total: %s",
449 sstrerror(errno, errbuf, sizeof(errbuf)));
453 /* Reset COLLECTD_CPU_STATE_ACTIVE */
454 sum_by_state[COLLECTD_CPU_STATE_ACTIVE] = NAN;
456 /* Physical Processor Utilization */
457 total_rate(sum_by_state, COLLECTD_CPU_STATE_IDLE, (derive_t)cputotal.pidle,
458 &total_conv[TOTAL_IDLE], now);
459 total_rate(sum_by_state, COLLECTD_CPU_STATE_USER, (derive_t)cputotal.puser,
460 &total_conv[TOTAL_USER], now);
461 total_rate(sum_by_state, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cputotal.psys,
462 &total_conv[TOTAL_SYS], now);
463 total_rate(sum_by_state, COLLECTD_CPU_STATE_WAIT, (derive_t)cputotal.pwait,
464 &total_conv[TOTAL_WAIT], now);
465 #endif /* }}} HAVE_PERFSTAT */
466 } /* }}} void aggregate */
468 /* Commits (dispatches) the values for one CPU or the global aggregation.
469 * cpu_num is the index of the CPU to be committed or -1 in case of the global
470 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values
472 * current rate; each rate may be NAN. Calculates the percentage of each state
473 * and dispatches the metric. */
474 static void cpu_commit_one(int cpu_num, /* {{{ */
475 gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
478 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
479 RATE_ADD(sum, rates[COLLECTD_CPU_STATE_IDLE]);
481 if (!report_by_state) {
482 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
483 submit_percent(cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
487 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
488 gauge_t percent = 100.0 * rates[state] / sum;
489 submit_percent(cpu_num, state, percent);
491 } /* }}} void cpu_commit_one */
493 /* Commits the number of cores */
494 static void cpu_commit_num_cpu(gauge_t value) /* {{{ */
496 value_list_t vl = VALUE_LIST_INIT;
498 vl.values = &(value_t){.gauge = value};
501 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
502 sstrncpy(vl.type, "count", sizeof(vl.type));
504 plugin_dispatch_values(&vl);
505 } /* }}} void cpu_commit_num_cpu */
507 /* Resets the internal aggregation. This is called by the read callback after
508 * each iteration / after each call to cpu_commit(). */
509 static void cpu_reset(void) /* {{{ */
511 for (size_t i = 0; i < cpu_states_num; i++)
512 cpu_states[i].has_value = 0;
515 } /* }}} void cpu_reset */
517 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
518 static void cpu_commit_without_aggregation(void) /* {{{ */
520 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
521 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
522 cpu_state_t *s = get_cpu_state(cpu_num, state);
527 submit_derive((int)cpu_num, (int)state, s->conv.last_value.derive);
530 } /* }}} void cpu_commit_without_aggregation */
532 /* Aggregates the internal state and dispatches the metrics. */
533 static void cpu_commit(void) /* {{{ */
535 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
536 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
540 cpu_commit_num_cpu((gauge_t)global_cpu_num);
542 if (report_by_state && report_by_cpu && !report_percent) {
543 cpu_commit_without_aggregation();
547 aggregate(global_rates);
549 if (!report_by_cpu) {
550 cpu_commit_one(-1, global_rates);
554 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
555 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
556 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {NAN, NAN, NAN, NAN, NAN,
557 NAN, NAN, NAN, NAN, NAN,
560 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
561 if (this_cpu_states[state].has_value)
562 local_rates[state] = this_cpu_states[state].rate;
564 cpu_commit_one((int)cpu_num, local_rates);
566 } /* }}} void cpu_commit */
568 /* Adds a derive value to the internal state. This should be used by each read
569 * function for each state. At the end of the iteration, the read function
570 * should call cpu_commit(). */
571 static int cpu_stage(size_t cpu_num, size_t state, derive_t d,
572 cdtime_t now) /* {{{ */
577 value_t val = {.derive = d};
579 if (state >= COLLECTD_CPU_STATE_ACTIVE)
582 status = cpu_states_alloc(cpu_num);
586 if (global_cpu_num <= cpu_num)
587 global_cpu_num = cpu_num + 1;
589 s = get_cpu_state(cpu_num, state);
591 status = value_to_rate(&rate, val, DS_TYPE_DERIVE, now, &s->conv);
598 } /* }}} int cpu_stage */
600 static int cpu_read(void) {
601 cdtime_t now = cdtime();
603 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
604 kern_return_t status;
606 processor_cpu_load_info_data_t cpu_info;
607 mach_msg_type_number_t cpu_info_len;
611 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++) {
613 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
615 status = processor_info(cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
616 (processor_info_t)&cpu_info, &cpu_info_len);
617 if (status != KERN_SUCCESS) {
618 ERROR("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
619 mach_error_string(status));
623 if (cpu_info_len < CPU_STATE_MAX) {
624 ERROR("cpu plugin: processor_info returned only %i elements..",
629 cpu_stage(cpu, COLLECTD_CPU_STATE_USER,
630 (derive_t)cpu_info.cpu_ticks[CPU_STATE_USER], now);
631 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE,
632 (derive_t)cpu_info.cpu_ticks[CPU_STATE_NICE], now);
633 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM,
634 (derive_t)cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
635 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE,
636 (derive_t)cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
638 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
640 #elif defined(KERNEL_LINUX) /* {{{ */
648 if ((fh = fopen("/proc/stat", "r")) == NULL) {
650 ERROR("cpu plugin: fopen (/proc/stat) failed: %s",
651 sstrerror(errno, errbuf, sizeof(errbuf)));
655 while (fgets(buf, 1024, fh) != NULL) {
656 if (strncmp(buf, "cpu", 3))
658 if ((buf[3] < '0') || (buf[3] > '9'))
661 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
665 cpu = atoi(fields[0] + 3);
667 /* Do not stage User and Nice immediately: we may need to alter them later: */
668 long long user_value = atoll(fields[1]);
669 long long nice_value = atoll(fields[2]);
670 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
671 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
673 if (numfields >= 8) {
674 cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
675 cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
677 cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
681 if (numfields >= 9) { /* Steal (since Linux 2.6.11) */
682 cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]), now);
685 if (numfields >= 10) { /* Guest (since Linux 2.6.24) */
687 long long value = atoll(fields[9]);
688 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST, (derive_t)value, now);
689 /* Guest is included in User; optionally subtract Guest from User: */
690 if (subtract_guest) {
692 if (user_value < 0) user_value = 0;
697 if (numfields >= 11) { /* Guest_nice (since Linux 2.6.33) */
699 long long value = atoll(fields[10]);
700 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST_NICE, (derive_t)value, now);
701 /* Guest_nice is included in Nice; optionally subtract Guest_nice from
703 if (subtract_guest) {
705 if (nice_value < 0) nice_value = 0;
710 /* Eventually stage User and Nice: */
711 cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)user_value, now);
712 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)nice_value, now);
715 /* }}} #endif defined(KERNEL_LINUX) */
717 #elif defined(HAVE_LIBKSTAT) /* {{{ */
718 static cpu_stat_t cs;
723 for (int cpu = 0; cpu < numcpu; cpu++) {
724 if (kstat_read(kc, ksp[cpu], &cs) == -1)
725 continue; /* error message? */
727 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,
728 (derive_t)cs.cpu_sysinfo.cpu[CPU_IDLE], now);
729 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,
730 (derive_t)cs.cpu_sysinfo.cpu[CPU_USER], now);
731 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM,
732 (derive_t)cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
733 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,
734 (derive_t)cs.cpu_sysinfo.cpu[CPU_WAIT], now);
736 /* }}} #endif defined(HAVE_LIBKSTAT) */
738 #elif CAN_USE_SYSCTL /* {{{ */
739 uint64_t cpuinfo[numcpu][CPUSTATES];
744 ERROR("cpu plugin: Could not determine number of "
745 "installed CPUs using sysctl(3).");
749 memset(cpuinfo, 0, sizeof(cpuinfo));
751 #if defined(KERN_CPTIME2)
753 for (int i = 0; i < numcpu; i++) {
754 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
756 cpuinfo_size = sizeof(cpuinfo[0]);
758 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), cpuinfo[i], &cpuinfo_size,
762 ERROR("cpu plugin: sysctl failed: %s.",
763 sstrerror(errno, errbuf, sizeof(errbuf)));
768 #endif /* defined(KERN_CPTIME2) */
770 int mib[] = {CTL_KERN, KERN_CPTIME};
771 long cpuinfo_tmp[CPUSTATES];
773 cpuinfo_size = sizeof(cpuinfo_tmp);
775 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &cpuinfo_tmp, &cpuinfo_size,
779 ERROR("cpu plugin: sysctl failed: %s.",
780 sstrerror(errno, errbuf, sizeof(errbuf)));
784 for (int i = 0; i < CPUSTATES; i++) {
785 cpuinfo[0][i] = cpuinfo_tmp[i];
789 for (int i = 0; i < numcpu; i++) {
790 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
791 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
792 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
793 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
794 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
797 /* }}} #endif CAN_USE_SYSCTL */
799 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \
801 long cpuinfo[maxcpu][CPUSTATES];
804 memset(cpuinfo, 0, sizeof(cpuinfo));
806 cpuinfo_size = sizeof(cpuinfo);
807 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
809 ERROR("cpu plugin: sysctlbyname failed: %s.",
810 sstrerror(errno, errbuf, sizeof(errbuf)));
814 for (int i = 0; i < numcpu; i++) {
815 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
816 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
817 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
818 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
819 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
822 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
824 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
825 long cpuinfo[CPUSTATES];
828 cpuinfo_size = sizeof(cpuinfo);
830 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
832 ERROR("cpu plugin: sysctlbyname failed: %s.",
833 sstrerror(errno, errbuf, sizeof(errbuf)));
837 cpu_stage(0, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[CP_USER], now);
838 cpu_stage(0, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[CP_NICE], now);
839 cpu_stage(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[CP_SYS], now);
840 cpu_stage(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[CP_IDLE], now);
841 cpu_stage(0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[CP_INTR], now);
842 /* }}} #endif HAVE_SYSCTLBYNAME */
844 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
846 cs = sg_get_cpu_stats();
849 ERROR("cpu plugin: sg_get_cpu_stats failed.");
853 cpu_state(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cs->idle);
854 cpu_state(0, COLLECTD_CPU_STATE_NICE, (derive_t)cs->nice);
855 cpu_state(0, COLLECTD_CPU_STATE_SWAP, (derive_t)cs->swap);
856 cpu_state(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cs->kernel);
857 cpu_state(0, COLLECTD_CPU_STATE_USER, (derive_t)cs->user);
858 cpu_state(0, COLLECTD_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);
868 WARNING("cpu plugin: perfstat_cpu: %s",
869 sstrerror(errno, errbuf, sizeof(errbuf)));
873 if (pnumcpu != numcpu || perfcpu == NULL) {
875 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
880 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0) {
882 WARNING("cpu plugin: perfstat_cpu: %s",
883 sstrerror(errno, errbuf, sizeof(errbuf)));
887 for (int i = 0; i < cpus; i++) {
888 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)perfcpu[i].idle, now);
889 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)perfcpu[i].sys, now);
890 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)perfcpu[i].user, now);
891 cpu_stage(i, COLLECTD_CPU_STATE_WAIT, (derive_t)perfcpu[i].wait, now);
893 #endif /* }}} HAVE_PERFSTAT */
900 void module_register(void) {
901 plugin_register_init("cpu", init);
902 plugin_register_config("cpu", cpu_config, config_keys, config_keys_num);
903 plugin_register_read("cpu", cpu_read);
904 } /* void module_register */