Merge branch 'collectd-5.4'
[collectd.git] / src / cpu.c
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  **/
29
30 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
33
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
55
56 #ifdef HAVE_LIBKSTAT
57 # include <sys/sysinfo.h>
58 #endif /* HAVE_LIBKSTAT */
59
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
65
66 # ifdef HAVE_SYS_DKSTAT_H
67 #  include <sys/dkstat.h>
68 # endif
69
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 */
79
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
90
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 */
102
103 #if HAVE_STATGRAB_H
104 # include <statgrab.h>
105 #endif
106
107 # ifdef HAVE_PERFSTAT
108 #  include <sys/protosw.h>
109 #  include <libperfstat.h>
110 # endif /* HAVE_PERFSTAT */
111
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
116
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 };
129
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 */
135
136 #elif defined(KERNEL_LINUX)
137 /* no variables needed */
138 /* #endif KERNEL_LINUX */
139
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 */
147
148 #elif CAN_USE_SYSCTL
149 static int numcpu;
150 /* #endif CAN_USE_SYSCTL */
151
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 */
158
159 #elif defined(HAVE_LIBSTATGRAB)
160 /* no variables needed */
161 /* #endif  HAVE_LIBSTATGRAB */
162
163 #elif defined(HAVE_PERFSTAT)
164 static perfstat_cpu_t *perfcpu;
165 static int numcpu;
166 static int pnumcpu;
167 #endif /* HAVE_PERFSTAT */
168
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)
175
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;
183
184 static cpu_state_t *cpu_states = NULL;
185 static size_t cpu_states_num = 0; /* #cpu_states allocated */
186
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;
190
191 static _Bool report_by_cpu = 1;
192 static _Bool report_by_state = 1;
193 static _Bool report_percent = 0;
194
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);
202
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);
213
214         return (0);
215 } /* }}} int cpu_config */
216
217 static int init (void)
218 {
219 #if PROCESSOR_CPU_LOAD_INFO
220         kern_return_t status;
221
222         port_host = mach_host_self ();
223
224         /* FIXME: Free `cpu_list' if it's not NULL */
225         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
226         {
227                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
228                 cpu_list_len = 0;
229                 return (-1);
230         }
231
232         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
233         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
234 /* #endif PROCESSOR_CPU_LOAD_INFO */
235
236 #elif defined(HAVE_LIBKSTAT)
237         kstat_t *ksp_chain;
238
239         numcpu = 0;
240
241         if (kc == NULL)
242                 return (-1);
243
244         /* Solaris doesn't count linear.. *sigh* */
245         for (numcpu = 0, ksp_chain = kc->kc_chain;
246                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
247                         ksp_chain = ksp_chain->ks_next)
248                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
249                         ksp[numcpu++] = ksp_chain;
250 /* #endif HAVE_LIBKSTAT */
251
252 #elif CAN_USE_SYSCTL
253         size_t numcpu_size;
254         int mib[2] = {CTL_HW, HW_NCPU};
255         int status;
256
257         numcpu = 0;
258         numcpu_size = sizeof (numcpu);
259
260         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
261                         &numcpu, &numcpu_size, NULL, 0);
262         if (status == -1)
263         {
264                 char errbuf[1024];
265                 WARNING ("cpu plugin: sysctl: %s",
266                                 sstrerror (errno, errbuf, sizeof (errbuf)));
267                 return (-1);
268         }
269 /* #endif CAN_USE_SYSCTL */
270
271 #elif defined (HAVE_SYSCTLBYNAME)
272         size_t numcpu_size;
273
274         numcpu_size = sizeof (numcpu);
275
276         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
277         {
278                 char errbuf[1024];
279                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
280                                 sstrerror (errno, errbuf, sizeof (errbuf)));
281                 return (-1);
282         }
283
284 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
285         numcpu_size = sizeof (maxcpu);
286
287         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
288         {
289                 char errbuf[1024];
290                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
291                                 sstrerror (errno, errbuf, sizeof (errbuf)));
292                 return (-1);
293         }
294 #else
295         if (numcpu != 1)
296                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
297 #endif
298 /* #endif HAVE_SYSCTLBYNAME */
299
300 #elif defined(HAVE_LIBSTATGRAB)
301         /* nothing to initialize */
302 /* #endif HAVE_LIBSTATGRAB */
303
304 #elif defined(HAVE_PERFSTAT)
305         /* nothing to initialize */
306 #endif /* HAVE_PERFSTAT */
307
308         return (0);
309 } /* int init */
310
311 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
312 {
313         value_t values[1];
314         value_list_t vl = VALUE_LIST_INIT;
315
316         memcpy(&values[0], &value, sizeof(value));
317
318         vl.values = values;
319         vl.values_len = 1;
320
321         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
322         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
323         sstrncpy (vl.type, type, sizeof (vl.type));
324         sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
325                         sizeof (vl.type_instance));
326
327         if (cpu_num >= 0) {
328                 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
329                                 "%i", cpu_num);
330         }
331         plugin_dispatch_values (&vl);
332 }
333
334 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
335 {
336         value_t value;
337
338         /* This function is called for all known CPU states, but each read
339          * method will only report a subset. The remaining states are left as
340          * NAN and we ignore them here. */
341         if (isnan (percent))
342                 return;
343
344         value.gauge = percent;
345         submit_value (cpu_num, cpu_state, "percent", value);
346 }
347
348 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
349 {
350         value_t value;
351
352         value.derive = derive;
353         submit_value (cpu_num, cpu_state, "cpu", value);
354 }
355
356 /* Takes the zero-index number of a CPU and makes sure that the module-global
357  * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
358 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
359 {
360         cpu_state_t *tmp;
361         size_t sz;
362
363         sz = (((size_t) cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
364         assert (sz > 0);
365
366         /* We already have enough space. */
367         if (cpu_states_num >= sz)
368                 return 0;
369
370         tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
371         if (tmp == NULL)
372         {
373                 ERROR ("cpu plugin: realloc failed.");
374                 return (ENOMEM);
375         }
376         cpu_states = tmp;
377         tmp = cpu_states + cpu_states_num;
378
379         memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
380         cpu_states_num = sz;
381         return 0;
382 } /* }}} cpu_states_alloc */
383
384 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
385 {
386         size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
387
388         if (index >= cpu_states_num)
389                 return (NULL);
390
391         return (&cpu_states[index]);
392 } /* }}} cpu_state_t *get_cpu_state */
393
394 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global rate_by_state
395  * array. */
396 static void aggregate (gauge_t *sum_by_state) /* {{{ */
397 {
398         size_t cpu_num;
399         size_t state;
400
401         for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
402                 sum_by_state[state] = NAN;
403
404         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
405         {
406                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
407
408                 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
409
410                 for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
411                 {
412                         if (!this_cpu_states[state].has_value)
413                                 continue;
414
415                         RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
416                         if (state != COLLECTD_CPU_STATE_IDLE)
417                                 RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
418                 }
419
420                 if (!isnan (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
421                         this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
422
423                 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
424         }
425 } /* }}} void aggregate */
426
427 /* Commits (dispatches) the values for one CPU or the global aggregation.
428  * cpu_num is the index of the CPU to be committed or -1 in case of the global
429  * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values holding the
430  * current rate; each rate may be NAN. Calculates the percentage of each state
431  * and dispatches the metric. */
432 static void cpu_commit_one (int cpu_num, /* {{{ */
433                 gauge_t rates[static COLLECTD_CPU_STATE_MAX])
434 {
435         size_t state;
436         gauge_t sum;
437
438         sum = rates[COLLECTD_CPU_STATE_ACTIVE];
439         RATE_ADD (sum, rates[COLLECTD_CPU_STATE_IDLE]);
440
441         if (!report_by_state)
442         {
443                 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
444                 submit_percent (cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
445                 return;
446         }
447
448         for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
449         {
450                 gauge_t percent = 100.0 * rates[state] / sum;
451                 submit_percent (cpu_num, state, percent);
452         }
453 } /* }}} void cpu_commit_one */
454
455 /* Resets the internal aggregation. This is called by the read callback after
456  * each iteration / after each call to cpu_commit(). */
457 static void cpu_reset (void) /* {{{ */
458 {
459         size_t i;
460
461         for (i = 0; i < cpu_states_num; i++)
462                 cpu_states[i].has_value = 0;
463
464         global_cpu_num = 0;
465 } /* }}} void cpu_reset */
466
467 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
468 static void cpu_commit_without_aggregation (void) /* {{{ */
469 {
470         int state;
471
472         for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
473         {
474                 size_t cpu_num;
475
476                 for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
477                 {
478                         cpu_state_t *s = get_cpu_state (cpu_num, state);
479
480                         if (!s->has_value)
481                                 continue;
482
483                         submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
484                 }
485         }
486 } /* }}} void cpu_commit_without_aggregation */
487
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         };
494         size_t cpu_num;
495
496         if (report_by_state && report_by_cpu && !report_percent)
497         {
498                 cpu_commit_without_aggregation ();
499                 return;
500         }
501
502         aggregate (global_rates);
503
504         if (!report_by_cpu)
505         {
506                 cpu_commit_one (-1, global_rates);
507                 return;
508         }
509
510         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
511         {
512                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
513                 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
514                         NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
515                 };
516                 size_t state;
517
518                 for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
519                         if (this_cpu_states[state].has_value)
520                                 local_rates[state] = this_cpu_states[state].rate;
521
522                 cpu_commit_one ((int) cpu_num, local_rates);
523         }
524 } /* }}} void cpu_commit */
525
526 /* Adds a derive value to the internal state. This should be used by each read
527  * function for each state. At the end of the iteration, the read function
528  * should call cpu_commit(). */
529 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
530 {
531         int status;
532         cpu_state_t *s;
533         value_t v;
534
535         if (state >= COLLECTD_CPU_STATE_ACTIVE)
536                 return (EINVAL);
537
538         status = cpu_states_alloc (cpu_num);
539         if (status != 0)
540                 return (status);
541
542         if (global_cpu_num <= cpu_num)
543                 global_cpu_num = cpu_num + 1;
544
545         s = get_cpu_state (cpu_num, state);
546
547         v.gauge = NAN;
548         status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
549         if (status != 0)
550                 return (status);
551
552         s->rate = v.gauge;
553         s->has_value = 1;
554         return (0);
555 } /* }}} int cpu_stage */
556
557 static int cpu_read (void)
558 {
559         cdtime_t now = cdtime ();
560
561 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
562         int cpu;
563
564         kern_return_t status;
565
566         processor_cpu_load_info_data_t cpu_info;
567         mach_msg_type_number_t         cpu_info_len;
568
569         host_t cpu_host;
570
571         for (cpu = 0; cpu < cpu_list_len; cpu++)
572         {
573                 cpu_host = 0;
574                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
575
576                 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
577                                 (processor_info_t) &cpu_info, &cpu_info_len);
578                 if (status != KERN_SUCCESS)
579                 {
580                         ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
581                                         mach_error_string (status));
582                         continue;
583                 }
584
585                 if (cpu_info_len < COLLECTD_CPU_STATE_MAX)
586                 {
587                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
588                         continue;
589                 }
590
591                 cpu_stage (cpu, COLLECTD_CPU_STATE_USER,   (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_USER],   now);
592                 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE,   (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_NICE],   now);
593                 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_SYSTEM], now);
594                 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE,   (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_IDLE],   now);
595         }
596 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
597
598 #elif defined(KERNEL_LINUX) /* {{{ */
599         int cpu;
600         FILE *fh;
601         char buf[1024];
602
603         char *fields[9];
604         int numfields;
605
606         if ((fh = fopen ("/proc/stat", "r")) == NULL)
607         {
608                 char errbuf[1024];
609                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
610                                 sstrerror (errno, errbuf, sizeof (errbuf)));
611                 return (-1);
612         }
613
614         while (fgets (buf, 1024, fh) != NULL)
615         {
616                 if (strncmp (buf, "cpu", 3))
617                         continue;
618                 if ((buf[3] < '0') || (buf[3] > '9'))
619                         continue;
620
621                 numfields = strsplit (buf, fields, 9);
622                 if (numfields < 5)
623                         continue;
624
625                 cpu = atoi (fields[0] + 3);
626
627                 cpu_stage (cpu, COLLECTD_CPU_STATE_USER,   (derive_t) atoll(fields[1]), now);
628                 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE,   (derive_t) atoll(fields[2]), now);
629                 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
630                 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE,   (derive_t) atoll(fields[4]), now);
631
632                 if (numfields >= 8)
633                 {
634                         cpu_stage (cpu, COLLECTD_CPU_STATE_WAIT,      (derive_t) atoll(fields[5]), now);
635                         cpu_stage (cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
636                         cpu_stage (cpu, COLLECTD_CPU_STATE_SOFTIRQ,   (derive_t) atoll(fields[7]), now);
637
638                         if (numfields >= 9)
639                                 cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
640                 }
641         }
642         fclose (fh);
643 /* }}} #endif defined(KERNEL_LINUX) */
644
645 #elif defined(HAVE_LIBKSTAT) /* {{{ */
646         int cpu;
647         static cpu_stat_t cs;
648
649         if (kc == NULL)
650                 return (-1);
651
652         for (cpu = 0; cpu < numcpu; cpu++)
653         {
654                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
655                         continue; /* error message? */
656
657                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,   (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE],   now);
658                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,   (derive_t) cs.cpu_sysinfo.cpu[CPU_USER],   now);
659                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
660                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,   (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT],   now);
661         }
662 /* }}} #endif defined(HAVE_LIBKSTAT) */
663
664 #elif CAN_USE_SYSCTL /* {{{ */
665         uint64_t cpuinfo[numcpu][CPUSTATES];
666         size_t cpuinfo_size;
667         int status;
668         int i;
669
670         if (numcpu < 1)
671         {
672                 ERROR ("cpu plugin: Could not determine number of "
673                                 "installed CPUs using sysctl(3).");
674                 return (-1);
675         }
676
677         memset (cpuinfo, 0, sizeof (cpuinfo));
678
679 #if defined(KERN_CPTIME2)
680         if (numcpu > 1) {
681                 for (i = 0; i < numcpu; i++) {
682                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
683
684                         cpuinfo_size = sizeof (cpuinfo[0]);
685
686                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
687                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
688                         if (status == -1) {
689                                 char errbuf[1024];
690                                 ERROR ("cpu plugin: sysctl failed: %s.",
691                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
692                                 return (-1);
693                         }
694                 }
695         }
696         else
697 #endif /* defined(KERN_CPTIME2) */
698         {
699                 int mib[] = {CTL_KERN, KERN_CPTIME};
700                 long cpuinfo_tmp[CPUSTATES];
701
702                 cpuinfo_size = sizeof(cpuinfo_tmp);
703
704                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
705                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
706                 if (status == -1)
707                 {
708                         char errbuf[1024];
709                         ERROR ("cpu plugin: sysctl failed: %s.",
710                                         sstrerror (errno, errbuf, sizeof (errbuf)));
711                         return (-1);
712                 }
713
714                 for(i = 0; i < CPUSTATES; i++) {
715                         cpuinfo[0][i] = cpuinfo_tmp[i];
716                 }
717         }
718
719         for (i = 0; i < numcpu; i++) {
720                 cpu_stage (i, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
721                 cpu_stage (i, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
722                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
723                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
724                 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
725         }
726 /* }}} #endif CAN_USE_SYSCTL */
727
728 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
729         long cpuinfo[maxcpu][CPUSTATES];
730         size_t cpuinfo_size;
731         int i;
732
733         memset (cpuinfo, 0, sizeof (cpuinfo));
734
735         cpuinfo_size = sizeof (cpuinfo);
736         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
737         {
738                 char errbuf[1024];
739                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
740                                 sstrerror (errno, errbuf, sizeof (errbuf)));
741                 return (-1);
742         }
743
744         for (i = 0; i < numcpu; i++) {
745                 cpu_stage (i, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
746                 cpu_stage (i, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
747                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
748                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
749                 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
750         }
751 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
752
753 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
754         long cpuinfo[CPUSTATES];
755         size_t cpuinfo_size;
756
757         cpuinfo_size = sizeof (cpuinfo);
758
759         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
760         {
761                 char errbuf[1024];
762                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
763                                 sstrerror (errno, errbuf, sizeof (errbuf)));
764                 return (-1);
765         }
766
767         cpu_stage (0, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[CP_USER], now);
768         cpu_stage (0, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[CP_NICE], now);
769         cpu_stage (0, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[CP_SYS], now);
770         cpu_stage (0, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[CP_IDLE], now);
771         cpu_stage (0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
772 /* }}} #endif HAVE_SYSCTLBYNAME */
773
774 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
775         sg_cpu_stats *cs;
776         cs = sg_get_cpu_stats ();
777
778         if (cs == NULL)
779         {
780                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
781                 return (-1);
782         }
783
784         cpu_state (0, COLLECTD_CPU_STATE_IDLE,   (derive_t) cs->idle);
785         cpu_state (0, COLLECTD_CPU_STATE_NICE,   (derive_t) cs->nice);
786         cpu_state (0, COLLECTD_CPU_STATE_SWAP,   (derive_t) cs->swap);
787         cpu_state (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs->kernel);
788         cpu_state (0, COLLECTD_CPU_STATE_USER,   (derive_t) cs->user);
789         cpu_state (0, COLLECTD_CPU_STATE_WAIT,   (derive_t) cs->iowait);
790 /* }}} #endif HAVE_LIBSTATGRAB */
791
792 #elif defined(HAVE_PERFSTAT) /* {{{ */
793         perfstat_id_t id;
794         int i, cpus;
795
796         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
797         if(numcpu == -1)
798         {
799                 char errbuf[1024];
800                 WARNING ("cpu plugin: perfstat_cpu: %s",
801                         sstrerror (errno, errbuf, sizeof (errbuf)));
802                 return (-1);
803         }
804
805         if (pnumcpu != numcpu || perfcpu == NULL)
806         {
807                 if (perfcpu != NULL)
808                         free(perfcpu);
809                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
810         }
811         pnumcpu = numcpu;
812
813         id.name[0] = '\0';
814         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
815         {
816                 char errbuf[1024];
817                 WARNING ("cpu plugin: perfstat_cpu: %s",
818                         sstrerror (errno, errbuf, sizeof (errbuf)));
819                 return (-1);
820         }
821
822         for (i = 0; i < cpus; i++)
823         {
824                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,   (derive_t) perfcpu[i].idle, now);
825                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys,  now);
826                 cpu_stage (i, COLLECTD_CPU_STATE_USER,   (derive_t) perfcpu[i].user, now);
827                 cpu_stage (i, COLLECTD_CPU_STATE_WAIT,   (derive_t) perfcpu[i].wait, now);
828         }
829 #endif /* }}} HAVE_PERFSTAT */
830
831         cpu_commit ();
832         cpu_reset ();
833         return (0);
834 }
835
836 void module_register (void)
837 {
838         plugin_register_init ("cpu", init);
839         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
840         plugin_register_read ("cpu", cpu_read);
841 } /* void module_register */
842
843 /* vim: set sw=8 sts=8 noet fdm=marker : */