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 static perfstat_cpu_t *perfcpu;
160 static int numcpu;
161 static int pnumcpu;
162 #endif /* HAVE_PERFSTAT */
164 #define RATE_ADD(sum, val) \
165 do { \
166 if (isnan(sum)) \
167 (sum) = (val); \
168 else if (!isnan(val)) \
169 (sum) += (val); \
170 } while (0)
172 struct cpu_state_s {
173 value_to_rate_state_t conv;
174 gauge_t rate;
175 _Bool has_value;
176 };
177 typedef struct cpu_state_s cpu_state_t;
179 static cpu_state_t *cpu_states = NULL;
180 static size_t cpu_states_num = 0; /* #cpu_states allocated */
182 /* Highest CPU number in the current iteration. Used by the dispatch logic to
183 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
184 static size_t global_cpu_num = 0;
186 static _Bool report_by_cpu = 1;
187 static _Bool report_by_state = 1;
188 static _Bool report_percent = 0;
189 static _Bool report_num_cpu = 0;
191 static const char *config_keys[] = {"ReportByCpu", "ReportByState",
192 "ReportNumCpu", "ValuesPercentage"};
193 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
195 static int cpu_config(char const *key, char const *value) /* {{{ */
196 {
197 if (strcasecmp(key, "ReportByCpu") == 0)
198 report_by_cpu = IS_TRUE(value) ? 1 : 0;
199 else if (strcasecmp(key, "ValuesPercentage") == 0)
200 report_percent = IS_TRUE(value) ? 1 : 0;
201 else if (strcasecmp(key, "ReportByState") == 0)
202 report_by_state = IS_TRUE(value) ? 1 : 0;
203 else if (strcasecmp(key, "ReportNumCpu") == 0)
204 report_num_cpu = IS_TRUE(value) ? 1 : 0;
205 else
206 return (-1);
208 return (0);
209 } /* }}} int cpu_config */
211 static int init(void) {
212 #if PROCESSOR_CPU_LOAD_INFO
213 kern_return_t status;
215 port_host = mach_host_self();
217 status = host_processors(port_host, &cpu_list, &cpu_list_len);
218 if (status == KERN_INVALID_ARGUMENT) {
219 ERROR("cpu plugin: Don't have a privileged host control port. "
220 "The most common cause for this problem is "
221 "that collectd is running without root "
222 "privileges, which are required to read CPU "
223 "load information. "
224 "<https://collectd.org/bugs/22>");
225 cpu_list_len = 0;
226 return (-1);
227 }
228 if (status != KERN_SUCCESS) {
229 ERROR("cpu plugin: host_processors() failed with status %d.", (int)status);
230 cpu_list_len = 0;
231 return (-1);
232 }
234 INFO("cpu plugin: Found %i processor%s.", (int)cpu_list_len,
235 cpu_list_len == 1 ? "" : "s");
236 /* #endif PROCESSOR_CPU_LOAD_INFO */
238 #elif defined(HAVE_LIBKSTAT)
239 kstat_t *ksp_chain;
241 numcpu = 0;
243 if (kc == NULL)
244 return (-1);
246 /* Solaris doesn't count linear.. *sigh* */
247 for (numcpu = 0, ksp_chain = kc->kc_chain;
248 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
249 ksp_chain = ksp_chain->ks_next)
250 if (strncmp(ksp_chain->ks_module, "cpu_stat", 8) == 0)
251 ksp[numcpu++] = ksp_chain;
252 /* #endif HAVE_LIBKSTAT */
254 #elif CAN_USE_SYSCTL
255 size_t numcpu_size;
256 int mib[2] = {CTL_HW, HW_NCPU};
257 int status;
259 numcpu = 0;
260 numcpu_size = sizeof(numcpu);
262 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &numcpu, &numcpu_size, NULL, 0);
263 if (status == -1) {
264 char errbuf[1024];
265 WARNING("cpu plugin: sysctl: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
266 return (-1);
267 }
268 /* #endif CAN_USE_SYSCTL */
270 #elif defined(HAVE_SYSCTLBYNAME)
271 size_t numcpu_size;
273 numcpu_size = sizeof(numcpu);
275 if (sysctlbyname("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) {
276 char errbuf[1024];
277 WARNING("cpu plugin: sysctlbyname(hw.ncpu): %s",
278 sstrerror(errno, errbuf, sizeof(errbuf)));
279 return (-1);
280 }
282 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
283 numcpu_size = sizeof(maxcpu);
285 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) {
286 char errbuf[1024];
287 WARNING("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
288 sstrerror(errno, errbuf, sizeof(errbuf)));
289 return (-1);
290 }
291 #else
292 if (numcpu != 1)
293 NOTICE("cpu: Only one processor supported when using `sysctlbyname' (found "
294 "%i)",
295 numcpu);
296 #endif
297 /* #endif HAVE_SYSCTLBYNAME */
299 #elif defined(HAVE_LIBSTATGRAB)
300 /* nothing to initialize */
301 /* #endif HAVE_LIBSTATGRAB */
303 #elif defined(HAVE_PERFSTAT)
304 /* nothing to initialize */
305 #endif /* HAVE_PERFSTAT */
307 return (0);
308 } /* int init */
310 static void submit_value(int cpu_num, int cpu_state, const char *type,
311 value_t value) {
312 value_t values[1];
313 value_list_t vl = VALUE_LIST_INIT;
315 memcpy(&values[0], &value, sizeof(value));
317 vl.values = values;
318 vl.values_len = 1;
320 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
321 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
322 sstrncpy(vl.type, type, sizeof(vl.type));
323 sstrncpy(vl.type_instance, cpu_state_names[cpu_state],
324 sizeof(vl.type_instance));
326 if (cpu_num >= 0) {
327 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
328 }
329 plugin_dispatch_values(&vl);
330 }
332 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent) {
333 value_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(percent))
339 return;
341 value.gauge = percent;
342 submit_value(cpu_num, cpu_state, "percent", value);
343 }
345 static void submit_derive(int cpu_num, int cpu_state, derive_t derive) {
346 value_t value;
348 value.derive = derive;
349 submit_value(cpu_num, cpu_state, "cpu", value);
350 }
352 /* Takes the zero-index number of a CPU and makes sure that the module-global
353 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
354 static int cpu_states_alloc(size_t cpu_num) /* {{{ */
355 {
356 cpu_state_t *tmp;
357 size_t sz;
359 sz = (((size_t)cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
360 assert(sz > 0);
362 /* We already have enough space. */
363 if (cpu_states_num >= sz)
364 return 0;
366 tmp = realloc(cpu_states, sz * sizeof(*cpu_states));
367 if (tmp == NULL) {
368 ERROR("cpu plugin: realloc failed.");
369 return (ENOMEM);
370 }
371 cpu_states = tmp;
372 tmp = cpu_states + cpu_states_num;
374 memset(tmp, 0, (sz - cpu_states_num) * sizeof(*cpu_states));
375 cpu_states_num = sz;
376 return 0;
377 } /* }}} cpu_states_alloc */
379 static cpu_state_t *get_cpu_state(size_t cpu_num, size_t state) /* {{{ */
380 {
381 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
383 if (index >= cpu_states_num)
384 return (NULL);
386 return (&cpu_states[index]);
387 } /* }}} cpu_state_t *get_cpu_state */
389 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global
390 * rate_by_state
391 * array. */
392 static void aggregate(gauge_t *sum_by_state) /* {{{ */
393 {
394 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
395 sum_by_state[state] = NAN;
397 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
398 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
400 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
402 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
403 if (!this_cpu_states[state].has_value)
404 continue;
406 RATE_ADD(sum_by_state[state], this_cpu_states[state].rate);
407 if (state != COLLECTD_CPU_STATE_IDLE)
408 RATE_ADD(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate,
409 this_cpu_states[state].rate);
410 }
412 if (!isnan(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
413 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
415 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE],
416 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
417 }
418 } /* }}} void aggregate */
420 /* Commits (dispatches) the values for one CPU or the global aggregation.
421 * cpu_num is the index of the CPU to be committed or -1 in case of the global
422 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values
423 * holding the
424 * current rate; each rate may be NAN. Calculates the percentage of each state
425 * and dispatches the metric. */
426 static void cpu_commit_one(int cpu_num, /* {{{ */
427 gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
428 gauge_t sum;
430 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
431 RATE_ADD(sum, rates[COLLECTD_CPU_STATE_IDLE]);
433 if (!report_by_state) {
434 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
435 submit_percent(cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
436 return;
437 }
439 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
440 gauge_t percent = 100.0 * rates[state] / sum;
441 submit_percent(cpu_num, state, percent);
442 }
443 } /* }}} void cpu_commit_one */
445 /* Commits the number of cores */
446 static void cpu_commit_num_cpu(gauge_t num_cpu) /* {{{ */
447 {
448 value_t values[1];
449 value_list_t vl = VALUE_LIST_INIT;
451 values[0].gauge = num_cpu;
453 vl.values = values;
454 vl.values_len = 1;
456 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
457 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
458 sstrncpy(vl.type, "count", sizeof(vl.type));
460 plugin_dispatch_values(&vl);
461 } /* }}} void cpu_commit_num_cpu */
463 /* Resets the internal aggregation. This is called by the read callback after
464 * each iteration / after each call to cpu_commit(). */
465 static void cpu_reset(void) /* {{{ */
466 {
467 for (size_t i = 0; i < cpu_states_num; i++)
468 cpu_states[i].has_value = 0;
470 global_cpu_num = 0;
471 } /* }}} void cpu_reset */
473 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
474 static void cpu_commit_without_aggregation(void) /* {{{ */
475 {
476 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
477 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
478 cpu_state_t *s = get_cpu_state(cpu_num, state);
480 if (!s->has_value)
481 continue;
483 submit_derive((int)cpu_num, (int)state, s->conv.last_value.derive);
484 }
485 }
486 } /* }}} void cpu_commit_without_aggregation */
488 /* Aggregates the internal state and dispatches the metrics. */
489 static void cpu_commit(void) /* {{{ */
490 {
491 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
492 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
493 };
495 if (report_num_cpu)
496 cpu_commit_num_cpu((gauge_t)global_cpu_num);
498 if (report_by_state && report_by_cpu && !report_percent) {
499 cpu_commit_without_aggregation();
500 return;
501 }
503 aggregate(global_rates);
505 if (!report_by_cpu) {
506 cpu_commit_one(-1, global_rates);
507 return;
508 }
510 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
511 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
512 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {NAN, NAN, NAN, NAN, NAN,
513 NAN, NAN, NAN, NAN, NAN};
515 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
516 if (this_cpu_states[state].has_value)
517 local_rates[state] = this_cpu_states[state].rate;
519 cpu_commit_one((int)cpu_num, local_rates);
520 }
521 } /* }}} void cpu_commit */
523 /* Adds a derive value to the internal state. This should be used by each read
524 * function for each state. At the end of the iteration, the read function
525 * should call cpu_commit(). */
526 static int cpu_stage(size_t cpu_num, size_t state, derive_t d,
527 cdtime_t now) /* {{{ */
528 {
529 int status;
530 cpu_state_t *s;
531 gauge_t rate = NAN;
532 value_t val = {.derive = d};
534 if (state >= COLLECTD_CPU_STATE_ACTIVE)
535 return (EINVAL);
537 status = cpu_states_alloc(cpu_num);
538 if (status != 0)
539 return (status);
541 if (global_cpu_num <= cpu_num)
542 global_cpu_num = cpu_num + 1;
544 s = get_cpu_state(cpu_num, state);
546 status = value_to_rate(&rate, val, DS_TYPE_DERIVE, now, &s->conv);
547 if (status != 0)
548 return (status);
550 s->rate = rate;
551 s->has_value = 1;
552 return (0);
553 } /* }}} int cpu_stage */
555 static int cpu_read(void) {
556 cdtime_t now = cdtime();
558 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
559 kern_return_t status;
561 processor_cpu_load_info_data_t cpu_info;
562 mach_msg_type_number_t cpu_info_len;
564 host_t cpu_host;
566 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++) {
567 cpu_host = 0;
568 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
570 status = processor_info(cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
571 (processor_info_t)&cpu_info, &cpu_info_len);
572 if (status != KERN_SUCCESS) {
573 ERROR("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
574 mach_error_string(status));
575 continue;
576 }
578 if (cpu_info_len < CPU_STATE_MAX) {
579 ERROR("cpu plugin: processor_info returned only %i elements..",
580 cpu_info_len);
581 continue;
582 }
584 cpu_stage(cpu, COLLECTD_CPU_STATE_USER,
585 (derive_t)cpu_info.cpu_ticks[CPU_STATE_USER], now);
586 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE,
587 (derive_t)cpu_info.cpu_ticks[CPU_STATE_NICE], now);
588 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM,
589 (derive_t)cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
590 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE,
591 (derive_t)cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
592 }
593 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
595 #elif defined(KERNEL_LINUX) /* {{{ */
596 int cpu;
597 FILE *fh;
598 char buf[1024];
600 char *fields[9];
601 int numfields;
603 if ((fh = fopen("/proc/stat", "r")) == NULL) {
604 char errbuf[1024];
605 ERROR("cpu plugin: fopen (/proc/stat) failed: %s",
606 sstrerror(errno, errbuf, sizeof(errbuf)));
607 return (-1);
608 }
610 while (fgets(buf, 1024, fh) != NULL) {
611 if (strncmp(buf, "cpu", 3))
612 continue;
613 if ((buf[3] < '0') || (buf[3] > '9'))
614 continue;
616 numfields = strsplit(buf, fields, 9);
617 if (numfields < 5)
618 continue;
620 cpu = atoi(fields[0] + 3);
622 cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)atoll(fields[1]), now);
623 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)atoll(fields[2]), now);
624 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
625 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
627 if (numfields >= 8) {
628 cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
629 cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
630 now);
631 cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
632 now);
634 if (numfields >= 9)
635 cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]),
636 now);
637 }
638 }
639 fclose(fh);
640 /* }}} #endif defined(KERNEL_LINUX) */
642 #elif defined(HAVE_LIBKSTAT) /* {{{ */
643 static cpu_stat_t cs;
645 if (kc == NULL)
646 return (-1);
648 for (int cpu = 0; cpu < numcpu; cpu++) {
649 if (kstat_read(kc, ksp[cpu], &cs) == -1)
650 continue; /* error message? */
652 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,
653 (derive_t)cs.cpu_sysinfo.cpu[CPU_IDLE], now);
654 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,
655 (derive_t)cs.cpu_sysinfo.cpu[CPU_USER], now);
656 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM,
657 (derive_t)cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
658 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,
659 (derive_t)cs.cpu_sysinfo.cpu[CPU_WAIT], now);
660 }
661 /* }}} #endif defined(HAVE_LIBKSTAT) */
663 #elif CAN_USE_SYSCTL /* {{{ */
664 uint64_t cpuinfo[numcpu][CPUSTATES];
665 size_t cpuinfo_size;
666 int status;
668 if (numcpu < 1) {
669 ERROR("cpu plugin: Could not determine number of "
670 "installed CPUs using sysctl(3).");
671 return (-1);
672 }
674 memset(cpuinfo, 0, sizeof(cpuinfo));
676 #if defined(KERN_CPTIME2)
677 if (numcpu > 1) {
678 for (int i = 0; i < numcpu; i++) {
679 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
681 cpuinfo_size = sizeof(cpuinfo[0]);
683 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), cpuinfo[i], &cpuinfo_size,
684 NULL, 0);
685 if (status == -1) {
686 char errbuf[1024];
687 ERROR("cpu plugin: sysctl failed: %s.",
688 sstrerror(errno, errbuf, sizeof(errbuf)));
689 return (-1);
690 }
691 }
692 } else
693 #endif /* defined(KERN_CPTIME2) */
694 {
695 int mib[] = {CTL_KERN, KERN_CPTIME};
696 long cpuinfo_tmp[CPUSTATES];
698 cpuinfo_size = sizeof(cpuinfo_tmp);
700 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &cpuinfo_tmp, &cpuinfo_size,
701 NULL, 0);
702 if (status == -1) {
703 char errbuf[1024];
704 ERROR("cpu plugin: sysctl failed: %s.",
705 sstrerror(errno, errbuf, sizeof(errbuf)));
706 return (-1);
707 }
709 for (int i = 0; i < CPUSTATES; i++) {
710 cpuinfo[0][i] = cpuinfo_tmp[i];
711 }
712 }
714 for (int i = 0; i < numcpu; i++) {
715 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
716 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
717 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
718 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
719 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
720 now);
721 }
722 /* }}} #endif CAN_USE_SYSCTL */
724 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \
725 */
726 long cpuinfo[maxcpu][CPUSTATES];
727 size_t cpuinfo_size;
729 memset(cpuinfo, 0, sizeof(cpuinfo));
731 cpuinfo_size = sizeof(cpuinfo);
732 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
733 char errbuf[1024];
734 ERROR("cpu plugin: sysctlbyname failed: %s.",
735 sstrerror(errno, errbuf, sizeof(errbuf)));
736 return (-1);
737 }
739 for (int i = 0; i < numcpu; i++) {
740 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
741 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
742 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
743 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
744 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
745 now);
746 }
747 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
749 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
750 long cpuinfo[CPUSTATES];
751 size_t cpuinfo_size;
753 cpuinfo_size = sizeof(cpuinfo);
755 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
756 char errbuf[1024];
757 ERROR("cpu plugin: sysctlbyname failed: %s.",
758 sstrerror(errno, errbuf, sizeof(errbuf)));
759 return (-1);
760 }
762 cpu_stage(0, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[CP_USER], now);
763 cpu_stage(0, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[CP_NICE], now);
764 cpu_stage(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[CP_SYS], now);
765 cpu_stage(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[CP_IDLE], now);
766 cpu_stage(0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[CP_INTR], now);
767 /* }}} #endif HAVE_SYSCTLBYNAME */
769 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
770 sg_cpu_stats *cs;
771 cs = sg_get_cpu_stats();
773 if (cs == NULL) {
774 ERROR("cpu plugin: sg_get_cpu_stats failed.");
775 return (-1);
776 }
778 cpu_state(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cs->idle);
779 cpu_state(0, COLLECTD_CPU_STATE_NICE, (derive_t)cs->nice);
780 cpu_state(0, COLLECTD_CPU_STATE_SWAP, (derive_t)cs->swap);
781 cpu_state(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cs->kernel);
782 cpu_state(0, COLLECTD_CPU_STATE_USER, (derive_t)cs->user);
783 cpu_state(0, COLLECTD_CPU_STATE_WAIT, (derive_t)cs->iowait);
784 /* }}} #endif HAVE_LIBSTATGRAB */
786 #elif defined(HAVE_PERFSTAT) /* {{{ */
787 perfstat_id_t id;
788 int cpus;
790 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
791 if (numcpu == -1) {
792 char errbuf[1024];
793 WARNING("cpu plugin: perfstat_cpu: %s",
794 sstrerror(errno, errbuf, sizeof(errbuf)));
795 return (-1);
796 }
798 if (pnumcpu != numcpu || perfcpu == NULL) {
799 free(perfcpu);
800 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
801 }
802 pnumcpu = numcpu;
804 id.name[0] = '\0';
805 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0) {
806 char errbuf[1024];
807 WARNING("cpu plugin: perfstat_cpu: %s",
808 sstrerror(errno, errbuf, sizeof(errbuf)));
809 return (-1);
810 }
812 for (int i = 0; i < cpus; i++) {
813 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)perfcpu[i].idle, now);
814 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)perfcpu[i].sys, now);
815 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)perfcpu[i].user, now);
816 cpu_stage(i, COLLECTD_CPU_STATE_WAIT, (derive_t)perfcpu[i].wait, now);
817 }
818 #endif /* }}} HAVE_PERFSTAT */
820 cpu_commit();
821 cpu_reset();
822 return (0);
823 }
825 void module_register(void) {
826 plugin_register_init("cpu", init);
827 plugin_register_config("cpu", cpu_config, config_keys, config_keys_num);
828 plugin_register_read("cpu", cpu_read);
829 } /* void module_register */
831 /* vim: set sw=8 sts=8 noet fdm=marker : */