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