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"
31 #include "common.h"
32 #include "plugin.h"
34 #ifdef HAVE_MACH_KERN_RETURN_H
35 # include <mach/kern_return.h>
36 #endif
37 #ifdef HAVE_MACH_MACH_INIT_H
38 # include <mach/mach_init.h>
39 #endif
40 #ifdef HAVE_MACH_HOST_PRIV_H
41 # include <mach/host_priv.h>
42 #endif
43 #if HAVE_MACH_MACH_ERROR_H
44 # include <mach/mach_error.h>
45 #endif
46 #ifdef HAVE_MACH_PROCESSOR_INFO_H
47 # include <mach/processor_info.h>
48 #endif
49 #ifdef HAVE_MACH_PROCESSOR_H
50 # include <mach/processor.h>
51 #endif
52 #ifdef HAVE_MACH_VM_MAP_H
53 # include <mach/vm_map.h>
54 #endif
56 #ifdef HAVE_LIBKSTAT
57 # include <sys/sysinfo.h>
58 #endif /* HAVE_LIBKSTAT */
60 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
61 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
62 # ifdef HAVE_SYS_SYSCTL_H
63 # include <sys/sysctl.h>
64 # endif
66 # ifdef HAVE_SYS_DKSTAT_H
67 # include <sys/dkstat.h>
68 # endif
70 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
71 # define CP_USER 0
72 # define CP_NICE 1
73 # define CP_SYS 2
74 # define CP_INTR 3
75 # define CP_IDLE 4
76 # define CPUSTATES 5
77 # endif
78 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
80 #if HAVE_SYSCTL
81 # if defined(CTL_HW) && defined(HW_NCPU) \
82 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
83 # define CAN_USE_SYSCTL 1
84 # else
85 # define CAN_USE_SYSCTL 0
86 # endif
87 #else
88 # define CAN_USE_SYSCTL 0
89 #endif
91 #define COLLECTD_CPU_STATE_USER 0
92 #define COLLECTD_CPU_STATE_SYSTEM 1
93 #define COLLECTD_CPU_STATE_WAIT 2
94 #define COLLECTD_CPU_STATE_NICE 3
95 #define COLLECTD_CPU_STATE_SWAP 4
96 #define COLLECTD_CPU_STATE_INTERRUPT 5
97 #define COLLECTD_CPU_STATE_SOFTIRQ 6
98 #define COLLECTD_CPU_STATE_STEAL 7
99 #define COLLECTD_CPU_STATE_IDLE 8
100 #define COLLECTD_CPU_STATE_ACTIVE 9 /* sum of (!idle) */
101 #define COLLECTD_CPU_STATE_MAX 10 /* #states */
103 #if HAVE_STATGRAB_H
104 # include <statgrab.h>
105 #endif
107 # ifdef HAVE_PERFSTAT
108 # include <sys/protosw.h>
109 # include <libperfstat.h>
110 # endif /* HAVE_PERFSTAT */
112 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
113 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
114 # error "No applicable input method."
115 #endif
117 static const char *cpu_state_names[] = {
118 "user",
119 "system",
120 "wait",
121 "nice",
122 "swap",
123 "interrupt",
124 "softirq",
125 "steal",
126 "idle",
127 "active"
128 };
130 #ifdef PROCESSOR_CPU_LOAD_INFO
131 static mach_port_t port_host;
132 static processor_port_array_t cpu_list;
133 static mach_msg_type_number_t cpu_list_len;
134 /* #endif PROCESSOR_CPU_LOAD_INFO */
136 #elif defined(KERNEL_LINUX)
137 /* no variables needed */
138 /* #endif KERNEL_LINUX */
140 #elif defined(HAVE_LIBKSTAT)
141 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
142 # define MAX_NUMCPU 256
143 extern kstat_ctl_t *kc;
144 static kstat_t *ksp[MAX_NUMCPU];
145 static int numcpu;
146 /* #endif HAVE_LIBKSTAT */
148 #elif CAN_USE_SYSCTL
149 static int numcpu;
150 /* #endif CAN_USE_SYSCTL */
152 #elif defined(HAVE_SYSCTLBYNAME)
153 static int numcpu;
154 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
155 static int maxcpu;
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)
164 static perfstat_cpu_t *perfcpu;
165 static int numcpu;
166 static int pnumcpu;
167 #endif /* HAVE_PERFSTAT */
169 #define RATE_ADD(sum, val) do { \
170 if (isnan (sum)) \
171 (sum) = (val); \
172 else if (!isnan (val)) \
173 (sum) += (val); \
174 } while (0)
176 struct cpu_state_s
177 {
178 value_to_rate_state_t conv;
179 gauge_t rate;
180 _Bool has_value;
181 };
182 typedef struct cpu_state_s cpu_state_t;
184 static cpu_state_t *cpu_states = NULL;
185 static size_t cpu_states_num = 0; /* #cpu_states allocated */
187 /* Highest CPU number in the current iteration. Used by the dispatch logic to
188 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
189 static size_t global_cpu_num = 0;
191 static _Bool report_by_cpu = 1;
192 static _Bool report_by_state = 1;
193 static _Bool report_percent = 0;
195 static const char *config_keys[] =
196 {
197 "ReportByCpu",
198 "ReportByState",
199 "ValuesPercentage"
200 };
201 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
203 static int cpu_config (char const *key, char const *value) /* {{{ */
204 {
205 if (strcasecmp (key, "ReportByCpu") == 0)
206 report_by_cpu = IS_TRUE (value) ? 1 : 0;
207 else if (strcasecmp (key, "ValuesPercentage") == 0)
208 report_percent = IS_TRUE (value) ? 1 : 0;
209 else if (strcasecmp (key, "ReportByState") == 0)
210 report_by_state = IS_TRUE (value) ? 1 : 0;
211 else
212 return (-1);
214 return (0);
215 } /* }}} int cpu_config */
217 static int init (void)
218 {
219 #if PROCESSOR_CPU_LOAD_INFO
220 kern_return_t status;
222 port_host = mach_host_self ();
224 status = host_processors (port_host, &cpu_list, &cpu_list_len);
225 if (status == KERN_INVALID_ARGUMENT)
226 {
227 ERROR ("cpu plugin: Don't have a privileged host control port. "
228 "The most common cause for this problem is "
229 "that collectd is running without root "
230 "privileges, which are required to read CPU "
231 "load information. "
232 "<https://collectd.org/bugs/22>");
233 cpu_list_len = 0;
234 return (-1);
235 }
236 if (status != KERN_SUCCESS)
237 {
238 ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status);
239 cpu_list_len = 0;
240 return (-1);
241 }
243 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
244 /* #endif PROCESSOR_CPU_LOAD_INFO */
246 #elif defined(HAVE_LIBKSTAT)
247 kstat_t *ksp_chain;
249 numcpu = 0;
251 if (kc == NULL)
252 return (-1);
254 /* Solaris doesn't count linear.. *sigh* */
255 for (numcpu = 0, ksp_chain = kc->kc_chain;
256 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
257 ksp_chain = ksp_chain->ks_next)
258 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
259 ksp[numcpu++] = ksp_chain;
260 /* #endif HAVE_LIBKSTAT */
262 #elif CAN_USE_SYSCTL
263 size_t numcpu_size;
264 int mib[2] = {CTL_HW, HW_NCPU};
265 int status;
267 numcpu = 0;
268 numcpu_size = sizeof (numcpu);
270 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
271 &numcpu, &numcpu_size, NULL, 0);
272 if (status == -1)
273 {
274 char errbuf[1024];
275 WARNING ("cpu plugin: sysctl: %s",
276 sstrerror (errno, errbuf, sizeof (errbuf)));
277 return (-1);
278 }
279 /* #endif CAN_USE_SYSCTL */
281 #elif defined (HAVE_SYSCTLBYNAME)
282 size_t numcpu_size;
284 numcpu_size = sizeof (numcpu);
286 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
287 {
288 char errbuf[1024];
289 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
291 return (-1);
292 }
294 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
295 numcpu_size = sizeof (maxcpu);
297 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
298 {
299 char errbuf[1024];
300 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
301 sstrerror (errno, errbuf, sizeof (errbuf)));
302 return (-1);
303 }
304 #else
305 if (numcpu != 1)
306 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
307 #endif
308 /* #endif HAVE_SYSCTLBYNAME */
310 #elif defined(HAVE_LIBSTATGRAB)
311 /* nothing to initialize */
312 /* #endif HAVE_LIBSTATGRAB */
314 #elif defined(HAVE_PERFSTAT)
315 /* nothing to initialize */
316 #endif /* HAVE_PERFSTAT */
318 return (0);
319 } /* int init */
321 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
322 {
323 value_t values[1];
324 value_list_t vl = VALUE_LIST_INIT;
326 memcpy(&values[0], &value, sizeof(value));
328 vl.values = values;
329 vl.values_len = 1;
331 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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));
337 if (cpu_num >= 0) {
338 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
339 "%i", cpu_num);
340 }
341 plugin_dispatch_values (&vl);
342 }
344 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
345 {
346 value_t value;
348 /* This function is called for all known CPU states, but each read
349 * method will only report a subset. The remaining states are left as
350 * NAN and we ignore them here. */
351 if (isnan (percent))
352 return;
354 value.gauge = percent;
355 submit_value (cpu_num, cpu_state, "percent", value);
356 }
358 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
359 {
360 value_t value;
362 value.derive = derive;
363 submit_value (cpu_num, cpu_state, "cpu", value);
364 }
366 /* Takes the zero-index number of a CPU and makes sure that the module-global
367 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
368 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
369 {
370 cpu_state_t *tmp;
371 size_t sz;
373 sz = (((size_t) cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
374 assert (sz > 0);
376 /* We already have enough space. */
377 if (cpu_states_num >= sz)
378 return 0;
380 tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
381 if (tmp == NULL)
382 {
383 ERROR ("cpu plugin: realloc failed.");
384 return (ENOMEM);
385 }
386 cpu_states = tmp;
387 tmp = cpu_states + cpu_states_num;
389 memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
390 cpu_states_num = sz;
391 return 0;
392 } /* }}} cpu_states_alloc */
394 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
395 {
396 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
398 if (index >= cpu_states_num)
399 return (NULL);
401 return (&cpu_states[index]);
402 } /* }}} cpu_state_t *get_cpu_state */
404 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global rate_by_state
405 * array. */
406 static void aggregate (gauge_t *sum_by_state) /* {{{ */
407 {
408 size_t cpu_num;
409 size_t state;
411 for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
412 sum_by_state[state] = NAN;
414 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
415 {
416 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
418 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
420 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
421 {
422 if (!this_cpu_states[state].has_value)
423 continue;
425 RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
426 if (state != COLLECTD_CPU_STATE_IDLE)
427 RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
428 }
430 if (!isnan (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
431 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
433 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
434 }
435 } /* }}} void aggregate */
437 /* Commits (dispatches) the values for one CPU or the global aggregation.
438 * cpu_num is the index of the CPU to be committed or -1 in case of the global
439 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values holding the
440 * current rate; each rate may be NAN. Calculates the percentage of each state
441 * and dispatches the metric. */
442 static void cpu_commit_one (int cpu_num, /* {{{ */
443 gauge_t rates[static COLLECTD_CPU_STATE_MAX])
444 {
445 size_t state;
446 gauge_t sum;
448 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
449 RATE_ADD (sum, rates[COLLECTD_CPU_STATE_IDLE]);
451 if (!report_by_state)
452 {
453 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
454 submit_percent (cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
455 return;
456 }
458 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
459 {
460 gauge_t percent = 100.0 * rates[state] / sum;
461 submit_percent (cpu_num, state, percent);
462 }
463 } /* }}} void cpu_commit_one */
465 /* Resets the internal aggregation. This is called by the read callback after
466 * each iteration / after each call to cpu_commit(). */
467 static void cpu_reset (void) /* {{{ */
468 {
469 size_t i;
471 for (i = 0; i < cpu_states_num; i++)
472 cpu_states[i].has_value = 0;
474 global_cpu_num = 0;
475 } /* }}} void cpu_reset */
477 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
478 static void cpu_commit_without_aggregation (void) /* {{{ */
479 {
480 int state;
482 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
483 {
484 size_t cpu_num;
486 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
487 {
488 cpu_state_t *s = get_cpu_state (cpu_num, state);
490 if (!s->has_value)
491 continue;
493 submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
494 }
495 }
496 } /* }}} void cpu_commit_without_aggregation */
498 /* Aggregates the internal state and dispatches the metrics. */
499 static void cpu_commit (void) /* {{{ */
500 {
501 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
502 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
503 };
504 size_t cpu_num;
506 if (report_by_state && report_by_cpu && !report_percent)
507 {
508 cpu_commit_without_aggregation ();
509 return;
510 }
512 aggregate (global_rates);
514 if (!report_by_cpu)
515 {
516 cpu_commit_one (-1, global_rates);
517 return;
518 }
520 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
521 {
522 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
523 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
524 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
525 };
526 size_t state;
528 for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
529 if (this_cpu_states[state].has_value)
530 local_rates[state] = this_cpu_states[state].rate;
532 cpu_commit_one ((int) cpu_num, local_rates);
533 }
534 } /* }}} void cpu_commit */
536 /* Adds a derive value to the internal state. This should be used by each read
537 * function for each state. At the end of the iteration, the read function
538 * should call cpu_commit(). */
539 static int cpu_stage (size_t cpu_num, size_t state, derive_t d, cdtime_t now) /* {{{ */
540 {
541 int status;
542 cpu_state_t *s;
543 gauge_t rate = NAN;
544 value_t val = {.derive = d};
546 if (state >= COLLECTD_CPU_STATE_ACTIVE)
547 return (EINVAL);
549 status = cpu_states_alloc (cpu_num);
550 if (status != 0)
551 return (status);
553 if (global_cpu_num <= cpu_num)
554 global_cpu_num = cpu_num + 1;
556 s = get_cpu_state (cpu_num, state);
558 status = value_to_rate (&rate, val, DS_TYPE_DERIVE, now, &s->conv);
559 if (status != 0)
560 return (status);
562 s->rate = rate;
563 s->has_value = 1;
564 return (0);
565 } /* }}} int cpu_stage */
567 static int cpu_read (void)
568 {
569 cdtime_t now = cdtime ();
571 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
572 int cpu;
574 kern_return_t status;
576 processor_cpu_load_info_data_t cpu_info;
577 mach_msg_type_number_t cpu_info_len;
579 host_t cpu_host;
581 for (cpu = 0; cpu < cpu_list_len; cpu++)
582 {
583 cpu_host = 0;
584 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
586 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
587 (processor_info_t) &cpu_info, &cpu_info_len);
588 if (status != KERN_SUCCESS)
589 {
590 ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
591 mach_error_string (status));
592 continue;
593 }
595 if (cpu_info_len < CPU_STATE_MAX)
596 {
597 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
598 continue;
599 }
601 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER], now);
602 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE], now);
603 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
604 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
605 }
606 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
608 #elif defined(KERNEL_LINUX) /* {{{ */
609 int cpu;
610 FILE *fh;
611 char buf[1024];
613 char *fields[9];
614 int numfields;
616 if ((fh = fopen ("/proc/stat", "r")) == NULL)
617 {
618 char errbuf[1024];
619 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
620 sstrerror (errno, errbuf, sizeof (errbuf)));
621 return (-1);
622 }
624 while (fgets (buf, 1024, fh) != NULL)
625 {
626 if (strncmp (buf, "cpu", 3))
627 continue;
628 if ((buf[3] < '0') || (buf[3] > '9'))
629 continue;
631 numfields = strsplit (buf, fields, 9);
632 if (numfields < 5)
633 continue;
635 cpu = atoi (fields[0] + 3);
637 cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
638 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
639 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
640 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) atoll(fields[4]), now);
642 if (numfields >= 8)
643 {
644 cpu_stage (cpu, COLLECTD_CPU_STATE_WAIT, (derive_t) atoll(fields[5]), now);
645 cpu_stage (cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
646 cpu_stage (cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t) atoll(fields[7]), now);
648 if (numfields >= 9)
649 cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
650 }
651 }
652 fclose (fh);
653 /* }}} #endif defined(KERNEL_LINUX) */
655 #elif defined(HAVE_LIBKSTAT) /* {{{ */
656 int cpu;
657 static cpu_stat_t cs;
659 if (kc == NULL)
660 return (-1);
662 for (cpu = 0; cpu < numcpu; cpu++)
663 {
664 if (kstat_read (kc, ksp[cpu], &cs) == -1)
665 continue; /* error message? */
667 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE, (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE], now);
668 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER, (derive_t) cs.cpu_sysinfo.cpu[CPU_USER], now);
669 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
670 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT, (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT], now);
671 }
672 /* }}} #endif defined(HAVE_LIBKSTAT) */
674 #elif CAN_USE_SYSCTL /* {{{ */
675 uint64_t cpuinfo[numcpu][CPUSTATES];
676 size_t cpuinfo_size;
677 int status;
678 int i;
680 if (numcpu < 1)
681 {
682 ERROR ("cpu plugin: Could not determine number of "
683 "installed CPUs using sysctl(3).");
684 return (-1);
685 }
687 memset (cpuinfo, 0, sizeof (cpuinfo));
689 #if defined(KERN_CPTIME2)
690 if (numcpu > 1) {
691 for (i = 0; i < numcpu; i++) {
692 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
694 cpuinfo_size = sizeof (cpuinfo[0]);
696 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
697 cpuinfo[i], &cpuinfo_size, NULL, 0);
698 if (status == -1) {
699 char errbuf[1024];
700 ERROR ("cpu plugin: sysctl failed: %s.",
701 sstrerror (errno, errbuf, sizeof (errbuf)));
702 return (-1);
703 }
704 }
705 }
706 else
707 #endif /* defined(KERN_CPTIME2) */
708 {
709 int mib[] = {CTL_KERN, KERN_CPTIME};
710 long cpuinfo_tmp[CPUSTATES];
712 cpuinfo_size = sizeof(cpuinfo_tmp);
714 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
715 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
716 if (status == -1)
717 {
718 char errbuf[1024];
719 ERROR ("cpu plugin: sysctl failed: %s.",
720 sstrerror (errno, errbuf, sizeof (errbuf)));
721 return (-1);
722 }
724 for(i = 0; i < CPUSTATES; i++) {
725 cpuinfo[0][i] = cpuinfo_tmp[i];
726 }
727 }
729 for (i = 0; i < numcpu; i++) {
730 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
731 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
732 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
733 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
734 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
735 }
736 /* }}} #endif CAN_USE_SYSCTL */
738 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
739 long cpuinfo[maxcpu][CPUSTATES];
740 size_t cpuinfo_size;
741 int i;
743 memset (cpuinfo, 0, sizeof (cpuinfo));
745 cpuinfo_size = sizeof (cpuinfo);
746 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
747 {
748 char errbuf[1024];
749 ERROR ("cpu plugin: sysctlbyname failed: %s.",
750 sstrerror (errno, errbuf, sizeof (errbuf)));
751 return (-1);
752 }
754 for (i = 0; i < numcpu; i++) {
755 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[i][CP_USER], now);
756 cpu_stage (i, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[i][CP_NICE], now);
757 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[i][CP_SYS], now);
758 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[i][CP_IDLE], now);
759 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
760 }
761 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
763 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
764 long cpuinfo[CPUSTATES];
765 size_t cpuinfo_size;
767 cpuinfo_size = sizeof (cpuinfo);
769 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
770 {
771 char errbuf[1024];
772 ERROR ("cpu plugin: sysctlbyname failed: %s.",
773 sstrerror (errno, errbuf, sizeof (errbuf)));
774 return (-1);
775 }
777 cpu_stage (0, COLLECTD_CPU_STATE_USER, (derive_t) cpuinfo[CP_USER], now);
778 cpu_stage (0, COLLECTD_CPU_STATE_NICE, (derive_t) cpuinfo[CP_NICE], now);
779 cpu_stage (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpuinfo[CP_SYS], now);
780 cpu_stage (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cpuinfo[CP_IDLE], now);
781 cpu_stage (0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
782 /* }}} #endif HAVE_SYSCTLBYNAME */
784 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
785 sg_cpu_stats *cs;
786 cs = sg_get_cpu_stats ();
788 if (cs == NULL)
789 {
790 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
791 return (-1);
792 }
794 cpu_state (0, COLLECTD_CPU_STATE_IDLE, (derive_t) cs->idle);
795 cpu_state (0, COLLECTD_CPU_STATE_NICE, (derive_t) cs->nice);
796 cpu_state (0, COLLECTD_CPU_STATE_SWAP, (derive_t) cs->swap);
797 cpu_state (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs->kernel);
798 cpu_state (0, COLLECTD_CPU_STATE_USER, (derive_t) cs->user);
799 cpu_state (0, COLLECTD_CPU_STATE_WAIT, (derive_t) cs->iowait);
800 /* }}} #endif HAVE_LIBSTATGRAB */
802 #elif defined(HAVE_PERFSTAT) /* {{{ */
803 perfstat_id_t id;
804 int i, cpus;
806 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
807 if(numcpu == -1)
808 {
809 char errbuf[1024];
810 WARNING ("cpu plugin: perfstat_cpu: %s",
811 sstrerror (errno, errbuf, sizeof (errbuf)));
812 return (-1);
813 }
815 if (pnumcpu != numcpu || perfcpu == NULL)
816 {
817 free(perfcpu);
818 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
819 }
820 pnumcpu = numcpu;
822 id.name[0] = '\0';
823 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
824 {
825 char errbuf[1024];
826 WARNING ("cpu plugin: perfstat_cpu: %s",
827 sstrerror (errno, errbuf, sizeof (errbuf)));
828 return (-1);
829 }
831 for (i = 0; i < cpus; i++)
832 {
833 cpu_stage (i, COLLECTD_CPU_STATE_IDLE, (derive_t) perfcpu[i].idle, now);
834 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys, now);
835 cpu_stage (i, COLLECTD_CPU_STATE_USER, (derive_t) perfcpu[i].user, now);
836 cpu_stage (i, COLLECTD_CPU_STATE_WAIT, (derive_t) perfcpu[i].wait, now);
837 }
838 #endif /* }}} HAVE_PERFSTAT */
840 cpu_commit ();
841 cpu_reset ();
842 return (0);
843 }
845 void module_register (void)
846 {
847 plugin_register_init ("cpu", init);
848 plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
849 plugin_register_read ("cpu", cpu_read);
850 } /* void module_register */
852 /* vim: set sw=8 sts=8 noet fdm=marker : */