1 /**
2 * collectd - src/cpu.c
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
8 *
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.
12 *
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.
17 *
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
21 *
22 * Authors:
23 * Florian octo Forster <octo at collectd.org>
24 * Oleg King <king2 at kaluga.ru>
25 * Simon Kuhnle <simon at blarzwurst.de>
26 * Manuel Sanmartin
27 * Pierre-Yves Ritschard <pyr at spootnik.org>
28 **/
30 #include "collectd.h"
32 #include "common.h"
33 #include "plugin.h"
35 #ifdef HAVE_MACH_KERN_RETURN_H
36 #include <mach/kern_return.h>
37 #endif
38 #ifdef HAVE_MACH_MACH_INIT_H
39 #include <mach/mach_init.h>
40 #endif
41 #ifdef HAVE_MACH_HOST_PRIV_H
42 #include <mach/host_priv.h>
43 #endif
44 #if HAVE_MACH_MACH_ERROR_H
45 #include <mach/mach_error.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_INFO_H
48 #include <mach/processor_info.h>
49 #endif
50 #ifdef HAVE_MACH_PROCESSOR_H
51 #include <mach/processor.h>
52 #endif
53 #ifdef HAVE_MACH_VM_MAP_H
54 #include <mach/vm_map.h>
55 #endif
57 #ifdef HAVE_LIBKSTAT
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>
65 #endif
67 #ifdef HAVE_SYS_DKSTAT_H
68 #include <sys/dkstat.h>
69 #endif
71 #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || \
72 !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
73 #define CP_USER 0
74 #define CP_NICE 1
75 #define CP_SYS 2
76 #define CP_INTR 3
77 #define CP_IDLE 4
78 #define CPUSTATES 5
79 #endif
80 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
82 #if HAVE_SYSCTL
83 #if defined(CTL_HW) && defined(HW_NCPU) && defined(CTL_KERN) && \
84 defined(KERN_CPTIME) && defined(CPUSTATES)
85 #define CAN_USE_SYSCTL 1
86 #else
87 #define CAN_USE_SYSCTL 0
88 #endif
89 #else
90 #define CAN_USE_SYSCTL 0
91 #endif
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_IDLE 8
102 #define COLLECTD_CPU_STATE_ACTIVE 9 /* sum of (!idle) */
103 #define COLLECTD_CPU_STATE_MAX 10 /* #states */
105 #if HAVE_STATGRAB_H
106 #include <statgrab.h>
107 #endif
109 #ifdef HAVE_PERFSTAT
110 #include <libperfstat.h>
111 #include <sys/protosw.h>
112 #endif /* HAVE_PERFSTAT */
114 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
115 !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && \
116 !HAVE_PERFSTAT
117 #error "No applicable input method."
118 #endif
120 static const char *cpu_state_names[] = {"user", "system", "wait", "nice",
121 "swap", "interrupt", "softirq", "steal",
122 "idle", "active"};
124 #ifdef PROCESSOR_CPU_LOAD_INFO
125 static mach_port_t port_host;
126 static processor_port_array_t cpu_list;
127 static mach_msg_type_number_t cpu_list_len;
128 /* #endif PROCESSOR_CPU_LOAD_INFO */
130 #elif defined(KERNEL_LINUX)
131 /* no variables needed */
132 /* #endif KERNEL_LINUX */
134 #elif defined(HAVE_LIBKSTAT)
135 /* colleague tells me that Sun doesn't sell systems with more than 100 or so
136 * CPUs.. */
137 #define MAX_NUMCPU 256
138 extern kstat_ctl_t *kc;
139 static kstat_t *ksp[MAX_NUMCPU];
140 static int numcpu;
141 /* #endif HAVE_LIBKSTAT */
143 #elif CAN_USE_SYSCTL
144 static int numcpu;
145 /* #endif CAN_USE_SYSCTL */
147 #elif defined(HAVE_SYSCTLBYNAME)
148 static int numcpu;
149 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
150 static int maxcpu;
151 #endif /* HAVE_SYSCTL_KERN_CP_TIMES */
152 /* #endif HAVE_SYSCTLBYNAME */
154 #elif defined(HAVE_LIBSTATGRAB)
155 /* no variables needed */
156 /* #endif HAVE_LIBSTATGRAB */
158 #elif defined(HAVE_PERFSTAT)
159 #define TOTAL_IDLE 0
160 #define TOTAL_USER 1
161 #define TOTAL_SYS 2
162 #define TOTAL_WAIT 3
163 #define TOTAL_STAT_NUM 4
164 static value_to_rate_state_t total_conv[TOTAL_STAT_NUM];
165 static perfstat_cpu_t *perfcpu;
166 static int numcpu;
167 static int pnumcpu;
168 #endif /* HAVE_PERFSTAT */
170 #define RATE_ADD(sum, val) \
171 do { \
172 if (isnan(sum)) \
173 (sum) = (val); \
174 else if (!isnan(val)) \
175 (sum) += (val); \
176 } while (0)
178 struct cpu_state_s {
179 value_to_rate_state_t conv;
180 gauge_t rate;
181 _Bool has_value;
182 };
183 typedef struct cpu_state_s cpu_state_t;
185 static cpu_state_t *cpu_states = NULL;
186 static size_t cpu_states_num = 0; /* #cpu_states allocated */
188 /* Highest CPU number in the current iteration. Used by the dispatch logic to
189 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
190 static size_t global_cpu_num = 0;
192 static _Bool report_by_cpu = 1;
193 static _Bool report_by_state = 1;
194 static _Bool report_percent = 0;
195 static _Bool report_num_cpu = 0;
197 static const char *config_keys[] = {"ReportByCpu", "ReportByState",
198 "ReportNumCpu", "ValuesPercentage"};
199 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
201 static int cpu_config(char const *key, char const *value) /* {{{ */
202 {
203 if (strcasecmp(key, "ReportByCpu") == 0)
204 report_by_cpu = IS_TRUE(value) ? 1 : 0;
205 else if (strcasecmp(key, "ValuesPercentage") == 0)
206 report_percent = IS_TRUE(value) ? 1 : 0;
207 else if (strcasecmp(key, "ReportByState") == 0)
208 report_by_state = IS_TRUE(value) ? 1 : 0;
209 else if (strcasecmp(key, "ReportNumCpu") == 0)
210 report_num_cpu = IS_TRUE(value) ? 1 : 0;
211 else
212 return (-1);
214 return (0);
215 } /* }}} int cpu_config */
217 static int init(void) {
218 #if PROCESSOR_CPU_LOAD_INFO
219 kern_return_t status;
221 port_host = mach_host_self();
223 status = host_processors(port_host, &cpu_list, &cpu_list_len);
224 if (status == KERN_INVALID_ARGUMENT) {
225 ERROR("cpu plugin: Don't have a privileged host control port. "
226 "The most common cause for this problem is "
227 "that collectd is running without root "
228 "privileges, which are required to read CPU "
229 "load information. "
230 "<https://collectd.org/bugs/22>");
231 cpu_list_len = 0;
232 return (-1);
233 }
234 if (status != KERN_SUCCESS) {
235 ERROR("cpu plugin: host_processors() failed with status %d.", (int)status);
236 cpu_list_len = 0;
237 return (-1);
238 }
240 INFO("cpu plugin: Found %i processor%s.", (int)cpu_list_len,
241 cpu_list_len == 1 ? "" : "s");
242 /* #endif PROCESSOR_CPU_LOAD_INFO */
244 #elif defined(HAVE_LIBKSTAT)
245 kstat_t *ksp_chain;
247 numcpu = 0;
249 if (kc == NULL)
250 return (-1);
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 */
260 #elif CAN_USE_SYSCTL
261 size_t numcpu_size;
262 int mib[2] = {CTL_HW, HW_NCPU};
263 int status;
265 numcpu = 0;
266 numcpu_size = sizeof(numcpu);
268 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &numcpu, &numcpu_size, NULL, 0);
269 if (status == -1) {
270 char errbuf[1024];
271 WARNING("cpu plugin: sysctl: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
272 return (-1);
273 }
274 /* #endif CAN_USE_SYSCTL */
276 #elif defined(HAVE_SYSCTLBYNAME)
277 size_t numcpu_size;
279 numcpu_size = sizeof(numcpu);
281 if (sysctlbyname("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) {
282 char errbuf[1024];
283 WARNING("cpu plugin: sysctlbyname(hw.ncpu): %s",
284 sstrerror(errno, errbuf, sizeof(errbuf)));
285 return (-1);
286 }
288 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
289 numcpu_size = sizeof(maxcpu);
291 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) {
292 char errbuf[1024];
293 WARNING("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
294 sstrerror(errno, errbuf, sizeof(errbuf)));
295 return (-1);
296 }
297 #else
298 if (numcpu != 1)
299 NOTICE("cpu: Only one processor supported when using `sysctlbyname' (found "
300 "%i)",
301 numcpu);
302 #endif
303 /* #endif HAVE_SYSCTLBYNAME */
305 #elif defined(HAVE_LIBSTATGRAB)
306 /* nothing to initialize */
307 /* #endif HAVE_LIBSTATGRAB */
309 #elif defined(HAVE_PERFSTAT)
310 /* nothing to initialize */
311 #endif /* HAVE_PERFSTAT */
313 return (0);
314 } /* int init */
316 static void submit_value(int cpu_num, int cpu_state, const char *type,
317 value_t value) {
318 value_list_t vl = VALUE_LIST_INIT;
320 vl.values = &value;
321 vl.values_len = 1;
323 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
324 sstrncpy(vl.type, type, sizeof(vl.type));
325 sstrncpy(vl.type_instance, cpu_state_names[cpu_state],
326 sizeof(vl.type_instance));
328 if (cpu_num >= 0) {
329 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
330 }
331 plugin_dispatch_values(&vl);
332 }
334 static void submit_percent(int cpu_num, int cpu_state, gauge_t value) {
335 /* This function is called for all known CPU states, but each read
336 * method will only report a subset. The remaining states are left as
337 * NAN and we ignore them here. */
338 if (isnan(value))
339 return;
341 submit_value(cpu_num, cpu_state, "percent", (value_t){.gauge = value});
342 }
344 static void submit_derive(int cpu_num, int cpu_state, derive_t value) {
345 submit_value(cpu_num, cpu_state, "cpu", (value_t){.derive = value});
346 }
348 /* Takes the zero-index number of a CPU and makes sure that the module-global
349 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
350 static int cpu_states_alloc(size_t cpu_num) /* {{{ */
351 {
352 cpu_state_t *tmp;
353 size_t sz;
355 sz = (((size_t)cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
356 assert(sz > 0);
358 /* We already have enough space. */
359 if (cpu_states_num >= sz)
360 return 0;
362 tmp = realloc(cpu_states, sz * sizeof(*cpu_states));
363 if (tmp == NULL) {
364 ERROR("cpu plugin: realloc failed.");
365 return (ENOMEM);
366 }
367 cpu_states = tmp;
368 tmp = cpu_states + cpu_states_num;
370 memset(tmp, 0, (sz - cpu_states_num) * sizeof(*cpu_states));
371 cpu_states_num = sz;
372 return 0;
373 } /* }}} cpu_states_alloc */
375 static cpu_state_t *get_cpu_state(size_t cpu_num, size_t state) /* {{{ */
376 {
377 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
379 if (index >= cpu_states_num)
380 return (NULL);
382 return (&cpu_states[index]);
383 } /* }}} cpu_state_t *get_cpu_state */
385 #if defined(HAVE_PERFSTAT) /* {{{ */
386 /* populate global aggregate cpu rate */
387 static int total_rate(gauge_t *sum_by_state, size_t state, derive_t d,
388 value_to_rate_state_t *conv, cdtime_t now) {
389 gauge_t rate = NAN;
390 int status =
391 value_to_rate(&rate, (value_t){.derive = d}, DS_TYPE_DERIVE, now, conv);
392 if (status != 0)
393 return (status);
395 sum_by_state[state] = rate;
397 if (state != COLLECTD_CPU_STATE_IDLE)
398 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE], sum_by_state[state]);
399 return (0);
400 }
401 #endif /* }}} HAVE_PERFSTAT */
403 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global
404 * rate_by_state
405 * array. */
406 static void aggregate(gauge_t *sum_by_state) /* {{{ */
407 {
408 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
409 sum_by_state[state] = NAN;
411 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
412 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
414 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
416 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
417 if (!this_cpu_states[state].has_value)
418 continue;
420 RATE_ADD(sum_by_state[state], this_cpu_states[state].rate);
421 if (state != COLLECTD_CPU_STATE_IDLE)
422 RATE_ADD(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate,
423 this_cpu_states[state].rate);
424 }
426 if (!isnan(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
427 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
429 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE],
430 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
431 }
433 #if defined(HAVE_PERFSTAT) /* {{{ */
434 cdtime_t now = cdtime();
435 perfstat_cpu_total_t cputotal = {0};
437 if (!perfstat_cpu_total(NULL, &cputotal, sizeof(cputotal), 1)) {
438 char errbuf[1024];
439 WARNING("cpu plugin: perfstat_cpu_total: %s",
440 sstrerror(errno, errbuf, sizeof(errbuf)));
441 return;
442 }
444 /* Reset COLLECTD_CPU_STATE_ACTIVE */
445 sum_by_state[COLLECTD_CPU_STATE_ACTIVE] = NAN;
447 /* Physical Processor Utilization */
448 total_rate(sum_by_state, COLLECTD_CPU_STATE_IDLE, (derive_t)cputotal.pidle,
449 &total_conv[TOTAL_IDLE], now);
450 total_rate(sum_by_state, COLLECTD_CPU_STATE_USER, (derive_t)cputotal.puser,
451 &total_conv[TOTAL_USER], now);
452 total_rate(sum_by_state, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cputotal.psys,
453 &total_conv[TOTAL_SYS], now);
454 total_rate(sum_by_state, COLLECTD_CPU_STATE_WAIT, (derive_t)cputotal.pwait,
455 &total_conv[TOTAL_WAIT], now);
456 #endif /* }}} HAVE_PERFSTAT */
457 } /* }}} void aggregate */
459 /* Commits (dispatches) the values for one CPU or the global aggregation.
460 * cpu_num is the index of the CPU to be committed or -1 in case of the global
461 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values
462 * holding the
463 * current rate; each rate may be NAN. Calculates the percentage of each state
464 * and dispatches the metric. */
465 static void cpu_commit_one(int cpu_num, /* {{{ */
466 gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
467 gauge_t sum;
469 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
470 RATE_ADD(sum, rates[COLLECTD_CPU_STATE_IDLE]);
472 if (!report_by_state) {
473 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
474 submit_percent(cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
475 return;
476 }
478 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
479 gauge_t percent = 100.0 * rates[state] / sum;
480 submit_percent(cpu_num, state, percent);
481 }
482 } /* }}} void cpu_commit_one */
484 /* Commits the number of cores */
485 static void cpu_commit_num_cpu(gauge_t value) /* {{{ */
486 {
487 value_list_t vl = VALUE_LIST_INIT;
489 vl.values = &(value_t){.gauge = value};
490 vl.values_len = 1;
492 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
493 sstrncpy(vl.type, "count", sizeof(vl.type));
495 plugin_dispatch_values(&vl);
496 } /* }}} void cpu_commit_num_cpu */
498 /* Resets the internal aggregation. This is called by the read callback after
499 * each iteration / after each call to cpu_commit(). */
500 static void cpu_reset(void) /* {{{ */
501 {
502 for (size_t i = 0; i < cpu_states_num; i++)
503 cpu_states[i].has_value = 0;
505 global_cpu_num = 0;
506 } /* }}} void cpu_reset */
508 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
509 static void cpu_commit_without_aggregation(void) /* {{{ */
510 {
511 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
512 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
513 cpu_state_t *s = get_cpu_state(cpu_num, state);
515 if (!s->has_value)
516 continue;
518 submit_derive((int)cpu_num, (int)state, s->conv.last_value.derive);
519 }
520 }
521 } /* }}} void cpu_commit_without_aggregation */
523 /* Aggregates the internal state and dispatches the metrics. */
524 static void cpu_commit(void) /* {{{ */
525 {
526 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
527 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
528 };
530 if (report_num_cpu)
531 cpu_commit_num_cpu((gauge_t)global_cpu_num);
533 if (report_by_state && report_by_cpu && !report_percent) {
534 cpu_commit_without_aggregation();
535 return;
536 }
538 aggregate(global_rates);
540 if (!report_by_cpu) {
541 cpu_commit_one(-1, global_rates);
542 return;
543 }
545 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
546 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
547 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {NAN, NAN, NAN, NAN, NAN,
548 NAN, NAN, NAN, NAN, NAN};
550 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
551 if (this_cpu_states[state].has_value)
552 local_rates[state] = this_cpu_states[state].rate;
554 cpu_commit_one((int)cpu_num, local_rates);
555 }
556 } /* }}} void cpu_commit */
558 /* Adds a derive value to the internal state. This should be used by each read
559 * function for each state. At the end of the iteration, the read function
560 * should call cpu_commit(). */
561 static int cpu_stage(size_t cpu_num, size_t state, derive_t d,
562 cdtime_t now) /* {{{ */
563 {
564 int status;
565 cpu_state_t *s;
566 gauge_t rate = NAN;
567 value_t val = {.derive = d};
569 if (state >= COLLECTD_CPU_STATE_ACTIVE)
570 return (EINVAL);
572 status = cpu_states_alloc(cpu_num);
573 if (status != 0)
574 return (status);
576 if (global_cpu_num <= cpu_num)
577 global_cpu_num = cpu_num + 1;
579 s = get_cpu_state(cpu_num, state);
581 status = value_to_rate(&rate, val, DS_TYPE_DERIVE, now, &s->conv);
582 if (status != 0)
583 return (status);
585 s->rate = rate;
586 s->has_value = 1;
587 return (0);
588 } /* }}} int cpu_stage */
590 static int cpu_read(void) {
591 cdtime_t now = cdtime();
593 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
594 kern_return_t status;
596 processor_cpu_load_info_data_t cpu_info;
597 mach_msg_type_number_t cpu_info_len;
599 host_t cpu_host;
601 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++) {
602 cpu_host = 0;
603 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
605 status = processor_info(cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
606 (processor_info_t)&cpu_info, &cpu_info_len);
607 if (status != KERN_SUCCESS) {
608 ERROR("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
609 mach_error_string(status));
610 continue;
611 }
613 if (cpu_info_len < CPU_STATE_MAX) {
614 ERROR("cpu plugin: processor_info returned only %i elements..",
615 cpu_info_len);
616 continue;
617 }
619 cpu_stage(cpu, COLLECTD_CPU_STATE_USER,
620 (derive_t)cpu_info.cpu_ticks[CPU_STATE_USER], now);
621 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE,
622 (derive_t)cpu_info.cpu_ticks[CPU_STATE_NICE], now);
623 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM,
624 (derive_t)cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
625 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE,
626 (derive_t)cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
627 }
628 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
630 #elif defined(KERNEL_LINUX) /* {{{ */
631 int cpu;
632 FILE *fh;
633 char buf[1024];
635 char *fields[9];
636 int numfields;
638 if ((fh = fopen("/proc/stat", "r")) == NULL) {
639 char errbuf[1024];
640 ERROR("cpu plugin: fopen (/proc/stat) failed: %s",
641 sstrerror(errno, errbuf, sizeof(errbuf)));
642 return (-1);
643 }
645 while (fgets(buf, 1024, fh) != NULL) {
646 if (strncmp(buf, "cpu", 3))
647 continue;
648 if ((buf[3] < '0') || (buf[3] > '9'))
649 continue;
651 numfields = strsplit(buf, fields, 9);
652 if (numfields < 5)
653 continue;
655 cpu = atoi(fields[0] + 3);
657 cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)atoll(fields[1]), now);
658 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)atoll(fields[2]), now);
659 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
660 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
662 if (numfields >= 8) {
663 cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
664 cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
665 now);
666 cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
667 now);
669 if (numfields >= 9)
670 cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]),
671 now);
672 }
673 }
674 fclose(fh);
675 /* }}} #endif defined(KERNEL_LINUX) */
677 #elif defined(HAVE_LIBKSTAT) /* {{{ */
678 static cpu_stat_t cs;
680 if (kc == NULL)
681 return (-1);
683 for (int cpu = 0; cpu < numcpu; cpu++) {
684 if (kstat_read(kc, ksp[cpu], &cs) == -1)
685 continue; /* error message? */
687 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,
688 (derive_t)cs.cpu_sysinfo.cpu[CPU_IDLE], now);
689 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,
690 (derive_t)cs.cpu_sysinfo.cpu[CPU_USER], now);
691 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM,
692 (derive_t)cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
693 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,
694 (derive_t)cs.cpu_sysinfo.cpu[CPU_WAIT], now);
695 }
696 /* }}} #endif defined(HAVE_LIBKSTAT) */
698 #elif CAN_USE_SYSCTL /* {{{ */
699 uint64_t cpuinfo[numcpu][CPUSTATES];
700 size_t cpuinfo_size;
701 int status;
703 if (numcpu < 1) {
704 ERROR("cpu plugin: Could not determine number of "
705 "installed CPUs using sysctl(3).");
706 return (-1);
707 }
709 memset(cpuinfo, 0, sizeof(cpuinfo));
711 #if defined(KERN_CPTIME2)
712 if (numcpu > 1) {
713 for (int i = 0; i < numcpu; i++) {
714 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
716 cpuinfo_size = sizeof(cpuinfo[0]);
718 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), cpuinfo[i], &cpuinfo_size,
719 NULL, 0);
720 if (status == -1) {
721 char errbuf[1024];
722 ERROR("cpu plugin: sysctl failed: %s.",
723 sstrerror(errno, errbuf, sizeof(errbuf)));
724 return (-1);
725 }
726 }
727 } else
728 #endif /* defined(KERN_CPTIME2) */
729 {
730 int mib[] = {CTL_KERN, KERN_CPTIME};
731 long cpuinfo_tmp[CPUSTATES];
733 cpuinfo_size = sizeof(cpuinfo_tmp);
735 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &cpuinfo_tmp, &cpuinfo_size,
736 NULL, 0);
737 if (status == -1) {
738 char errbuf[1024];
739 ERROR("cpu plugin: sysctl failed: %s.",
740 sstrerror(errno, errbuf, sizeof(errbuf)));
741 return (-1);
742 }
744 for (int i = 0; i < CPUSTATES; i++) {
745 cpuinfo[0][i] = cpuinfo_tmp[i];
746 }
747 }
749 for (int i = 0; i < numcpu; i++) {
750 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
751 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
752 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
753 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
754 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
755 now);
756 }
757 /* }}} #endif CAN_USE_SYSCTL */
759 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \
760 */
761 long cpuinfo[maxcpu][CPUSTATES];
762 size_t cpuinfo_size;
764 memset(cpuinfo, 0, sizeof(cpuinfo));
766 cpuinfo_size = sizeof(cpuinfo);
767 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
768 char errbuf[1024];
769 ERROR("cpu plugin: sysctlbyname failed: %s.",
770 sstrerror(errno, errbuf, sizeof(errbuf)));
771 return (-1);
772 }
774 for (int i = 0; i < numcpu; i++) {
775 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
776 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
777 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
778 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
779 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
780 now);
781 }
782 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
784 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
785 long cpuinfo[CPUSTATES];
786 size_t cpuinfo_size;
788 cpuinfo_size = sizeof(cpuinfo);
790 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
791 char errbuf[1024];
792 ERROR("cpu plugin: sysctlbyname failed: %s.",
793 sstrerror(errno, errbuf, sizeof(errbuf)));
794 return (-1);
795 }
797 cpu_stage(0, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[CP_USER], now);
798 cpu_stage(0, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[CP_NICE], now);
799 cpu_stage(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[CP_SYS], now);
800 cpu_stage(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[CP_IDLE], now);
801 cpu_stage(0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[CP_INTR], now);
802 /* }}} #endif HAVE_SYSCTLBYNAME */
804 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
805 sg_cpu_stats *cs;
806 cs = sg_get_cpu_stats();
808 if (cs == NULL) {
809 ERROR("cpu plugin: sg_get_cpu_stats failed.");
810 return (-1);
811 }
813 cpu_state(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cs->idle);
814 cpu_state(0, COLLECTD_CPU_STATE_NICE, (derive_t)cs->nice);
815 cpu_state(0, COLLECTD_CPU_STATE_SWAP, (derive_t)cs->swap);
816 cpu_state(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cs->kernel);
817 cpu_state(0, COLLECTD_CPU_STATE_USER, (derive_t)cs->user);
818 cpu_state(0, COLLECTD_CPU_STATE_WAIT, (derive_t)cs->iowait);
819 /* }}} #endif HAVE_LIBSTATGRAB */
821 #elif defined(HAVE_PERFSTAT) /* {{{ */
822 perfstat_id_t id;
823 int cpus;
825 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
826 if (numcpu == -1) {
827 char errbuf[1024];
828 WARNING("cpu plugin: perfstat_cpu: %s",
829 sstrerror(errno, errbuf, sizeof(errbuf)));
830 return (-1);
831 }
833 if (pnumcpu != numcpu || perfcpu == NULL) {
834 free(perfcpu);
835 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
836 }
837 pnumcpu = numcpu;
839 id.name[0] = '\0';
840 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0) {
841 char errbuf[1024];
842 WARNING("cpu plugin: perfstat_cpu: %s",
843 sstrerror(errno, errbuf, sizeof(errbuf)));
844 return (-1);
845 }
847 for (int i = 0; i < cpus; i++) {
848 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)perfcpu[i].idle, now);
849 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)perfcpu[i].sys, now);
850 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)perfcpu[i].user, now);
851 cpu_stage(i, COLLECTD_CPU_STATE_WAIT, (derive_t)perfcpu[i].wait, now);
852 }
853 #endif /* }}} HAVE_PERFSTAT */
855 cpu_commit();
856 cpu_reset();
857 return (0);
858 }
860 void module_register(void) {
861 plugin_register_init("cpu", init);
862 plugin_register_config("cpu", cpu_config, config_keys, config_keys_num);
863 plugin_register_read("cpu", cpu_read);
864 } /* void module_register */
866 /* vim: set sw=8 sts=8 noet fdm=marker : */