Merge remote-tracking branch 'github/pr/734'
[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 CPU_STATE_USER 0
92 #define CPU_STATE_SYSTEM 1
93 #define CPU_STATE_WAIT 2
94 #define CPU_STATE_NICE 3
95 #define CPU_STATE_SWAP 4
96 #define CPU_STATE_INTERRUPT 5
97 #define CPU_STATE_SOFTIRQ 6
98 #define CPU_STATE_STEAL 7
99 #define CPU_STATE_IDLE 8
100 #define CPU_STATE_ACTIVE 9 /* sum of (!idle) */
101 #define 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
135 #if PROCESSOR_TEMPERATURE
136 static int cpu_temp_retry_counter = 0;
137 static int cpu_temp_retry_step    = 1;
138 static int cpu_temp_retry_max     = 1;
139 #endif /* PROCESSOR_TEMPERATURE */
140 /* #endif PROCESSOR_CPU_LOAD_INFO */
141
142 #elif defined(KERNEL_LINUX)
143 /* no variables needed */
144 /* #endif KERNEL_LINUX */
145
146 #elif defined(HAVE_LIBKSTAT)
147 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
148 # define MAX_NUMCPU 256
149 extern kstat_ctl_t *kc;
150 static kstat_t *ksp[MAX_NUMCPU];
151 static int numcpu;
152 /* #endif HAVE_LIBKSTAT */
153
154 #elif CAN_USE_SYSCTL
155 static int numcpu;
156 /* #endif CAN_USE_SYSCTL */
157
158 #elif defined(HAVE_SYSCTLBYNAME)
159 static int numcpu;
160 #  ifdef HAVE_SYSCTL_KERN_CP_TIMES
161 static int maxcpu;
162 #  endif /* HAVE_SYSCTL_KERN_CP_TIMES */
163 /* #endif HAVE_SYSCTLBYNAME */
164
165 #elif defined(HAVE_LIBSTATGRAB)
166 /* no variables needed */
167 /* #endif  HAVE_LIBSTATGRAB */
168
169 #elif defined(HAVE_PERFSTAT)
170 static perfstat_cpu_t *perfcpu;
171 static int numcpu;
172 static int pnumcpu;
173 #endif /* HAVE_PERFSTAT */
174
175 #define RATE_ADD(sum, val) do { \
176         if (isnan (sum))        \
177         (sum) = (val);          \
178         else if (!isnan (val))  \
179         (sum) += (val);         \
180 } while (0)
181
182 struct cpu_state_s
183 {
184         value_to_rate_state_t conv;
185         gauge_t rate;
186         _Bool has_value;
187 };
188 typedef struct cpu_state_s cpu_state_t;
189
190 static cpu_state_t *cpu_states = NULL;
191 static size_t cpu_states_num = 0; /* #cpu_states allocated */
192
193 /* Highest CPU number in the current iteration. Used by the dispatch logic to
194  * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
195 static size_t global_cpu_num = 0;
196
197 static _Bool report_by_cpu = 1;
198 static _Bool report_by_state = 1;
199 static _Bool report_percent = 0;
200
201 static const char *config_keys[] =
202 {
203         "ReportByCpu",
204         "ReportByState",
205         "ValuesPercentage"
206 };
207 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
208
209 static int cpu_config (char const *key, char const *value) /* {{{ */
210 {
211         if (strcasecmp (key, "ReportByCpu") == 0)
212                 report_by_cpu = IS_TRUE (value) ? 1 : 0;
213         else if (strcasecmp (key, "ValuesPercentage") == 0)
214                 report_percent = IS_TRUE (value) ? 1 : 0;
215         else if (strcasecmp (key, "ReportByState") == 0)
216                 report_by_state = IS_TRUE (value) ? 1 : 0;
217         else
218                 return (-1);
219
220         return (0);
221 } /* }}} int cpu_config */
222
223 static int init (void)
224 {
225 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
226         kern_return_t status;
227
228         port_host = mach_host_self ();
229
230         /* FIXME: Free `cpu_list' if it's not NULL */
231         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
232         {
233                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
234                 cpu_list_len = 0;
235                 return (-1);
236         }
237
238         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
239         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
240
241         cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
242 /* #endif PROCESSOR_CPU_LOAD_INFO */
243
244 #elif defined(HAVE_LIBKSTAT)
245         kstat_t *ksp_chain;
246
247         numcpu = 0;
248
249         if (kc == NULL)
250                 return (-1);
251
252         /* Solaris doesn't count linear.. *sigh* */
253         for (numcpu = 0, ksp_chain = kc->kc_chain;
254                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
255                         ksp_chain = ksp_chain->ks_next)
256                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
257                         ksp[numcpu++] = ksp_chain;
258 /* #endif HAVE_LIBKSTAT */
259
260 #elif CAN_USE_SYSCTL
261         size_t numcpu_size;
262         int mib[2] = {CTL_HW, HW_NCPU};
263         int status;
264
265         numcpu = 0;
266         numcpu_size = sizeof (numcpu);
267
268         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
269                         &numcpu, &numcpu_size, NULL, 0);
270         if (status == -1)
271         {
272                 char errbuf[1024];
273                 WARNING ("cpu plugin: sysctl: %s",
274                                 sstrerror (errno, errbuf, sizeof (errbuf)));
275                 return (-1);
276         }
277 /* #endif CAN_USE_SYSCTL */
278
279 #elif defined (HAVE_SYSCTLBYNAME)
280         size_t numcpu_size;
281
282         numcpu_size = sizeof (numcpu);
283
284         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
285         {
286                 char errbuf[1024];
287                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
288                                 sstrerror (errno, errbuf, sizeof (errbuf)));
289                 return (-1);
290         }
291
292 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
293         numcpu_size = sizeof (maxcpu);
294
295         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
296         {
297                 char errbuf[1024];
298                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
299                                 sstrerror (errno, errbuf, sizeof (errbuf)));
300                 return (-1);
301         }
302 #else
303         if (numcpu != 1)
304                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
305 #endif
306 /* #endif HAVE_SYSCTLBYNAME */
307
308 #elif defined(HAVE_LIBSTATGRAB)
309         /* nothing to initialize */
310 /* #endif HAVE_LIBSTATGRAB */
311
312 #elif defined(HAVE_PERFSTAT)
313         /* nothing to initialize */
314 #endif /* HAVE_PERFSTAT */
315
316         return (0);
317 } /* int init */
318
319 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
320 {
321         value_t values[1];
322         value_list_t vl = VALUE_LIST_INIT;
323
324         memcpy(&values[0], &value, sizeof(value));
325
326         vl.values = values;
327         vl.values_len = 1;
328
329         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
330         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
331         sstrncpy (vl.type, type, sizeof (vl.type));
332         sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
333                         sizeof (vl.type_instance));
334
335         if (cpu_num >= 0) {
336                 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
337                                 "%i", cpu_num);
338         }
339         plugin_dispatch_values (&vl);
340 }
341
342 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
343 {
344         value_t value;
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         if (cpu_num < 0)
366                 return (EINVAL);
367
368         sz = (((size_t) cpu_num) + 1) * CPU_STATE_MAX;
369         assert (sz > 0);
370
371         /* We already have enough space. */
372         if (cpu_states_num >= sz)
373                 return 0;
374
375         tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
376         if (tmp == NULL)
377         {
378                 ERROR ("cpu plugin: realloc failed.");
379                 return (ENOMEM);
380         }
381         cpu_states = tmp;
382         tmp = cpu_states + cpu_states_num;
383
384         memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
385         cpu_states_num = sz;
386         return 0;
387 } /* }}} cpu_states_alloc */
388
389 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
390 {
391         size_t index = ((cpu_num * CPU_STATE_MAX) + state);
392
393         if (index >= cpu_states_num)
394                 return (NULL);
395
396         return (&cpu_states[index]);
397 } /* }}} cpu_state_t *get_cpu_state */
398
399 /* Populates the per-CPU CPU_STATE_ACTIVE rate and the global rate_by_state
400  * array. */
401 static void aggregate (gauge_t *sum_by_state) /* {{{ */
402 {
403         size_t cpu_num;
404         size_t state;
405
406         for (state = 0; state < CPU_STATE_MAX; state++)
407                 sum_by_state[state] = NAN;
408
409         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
410         {
411                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
412
413                 this_cpu_states[CPU_STATE_ACTIVE].rate = NAN;
414
415                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
416                 {
417                         if (!this_cpu_states[state].has_value)
418                                 continue;
419
420                         RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
421                         if (state != CPU_STATE_IDLE)
422                                 RATE_ADD (this_cpu_states[CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
423                 }
424
425                 RATE_ADD (sum_by_state[CPU_STATE_ACTIVE], this_cpu_states[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 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 CPU_STATE_MAX])
436 {
437         size_t state;
438         gauge_t sum;
439
440         sum = rates[CPU_STATE_ACTIVE];
441         RATE_ADD (sum, rates[CPU_STATE_IDLE]);
442
443         if (!report_by_state)
444         {
445                 gauge_t percent = 100.0 * rates[CPU_STATE_ACTIVE] / sum;
446                 submit_percent (cpu_num, CPU_STATE_ACTIVE, percent);
447                 return;
448         }
449
450         for (state = 0; state < 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         size_t cpu_num;
473
474         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
475         {
476                 int state;
477
478                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
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[CPU_STATE_MAX] = {
494                 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
495         };
496         size_t cpu_num;
497
498         if (report_by_cpu && report_by_state && !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[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 < CPU_STATE_ACTIVE; 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 >= 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 || PROCESSOR_TEMPERATURE /* {{{ */
564         int cpu;
565
566         kern_return_t status;
567
568 #if PROCESSOR_CPU_LOAD_INFO
569         processor_cpu_load_info_data_t cpu_info;
570         mach_msg_type_number_t         cpu_info_len;
571 #endif
572 #if PROCESSOR_TEMPERATURE
573         processor_info_data_t          cpu_temp;
574         mach_msg_type_number_t         cpu_temp_len;
575 #endif
576
577         host_t cpu_host;
578
579         for (cpu = 0; cpu < cpu_list_len; cpu++)
580         {
581 #if PROCESSOR_CPU_LOAD_INFO
582                 cpu_host = 0;
583                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
584
585                 if ((status = processor_info (cpu_list[cpu],
586                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
587                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
588                 {
589                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
590                         continue;
591                 }
592
593                 if (cpu_info_len < CPU_STATE_MAX)
594                 {
595                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
596                         continue;
597                 }
598
599                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER],   now);
600                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE],   now);
601                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
602                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE],   now);
603 #endif /* PROCESSOR_CPU_LOAD_INFO */
604
605 #if PROCESSOR_TEMPERATURE
606                 /*
607                  * Not all Apple computers do have this ability. To minimize
608                  * the messages sent to the syslog we do an exponential
609                  * stepback if `processor_info' fails. We still try ~once a day
610                  * though..
611                  */
612                 if (cpu_temp_retry_counter > 0)
613                 {
614                         cpu_temp_retry_counter--;
615                         continue;
616                 }
617
618                 cpu_temp_len = PROCESSOR_INFO_MAX;
619
620                 status = processor_info (cpu_list[cpu],
621                                 PROCESSOR_TEMPERATURE,
622                                 &cpu_host,
623                                 cpu_temp, &cpu_temp_len);
624                 if (status != KERN_SUCCESS)
625                 {
626                         ERROR ("cpu plugin: processor_info failed: %s",
627                                         mach_error_string (status));
628
629                         cpu_temp_retry_counter = cpu_temp_retry_step;
630                         cpu_temp_retry_step *= 2;
631                         if (cpu_temp_retry_step > cpu_temp_retry_max)
632                                 cpu_temp_retry_step = cpu_temp_retry_max;
633
634                         continue;
635                 }
636
637                 if (cpu_temp_len != 1)
638                 {
639                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
640                                         (int) cpu_temp_len);
641                         continue;
642                 }
643
644                 cpu_temp_retry_counter = 0;
645                 cpu_temp_retry_step    = 1;
646 #endif /* PROCESSOR_TEMPERATURE */
647         }
648 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
649
650 #elif defined(KERNEL_LINUX) /* {{{ */
651         int cpu;
652         FILE *fh;
653         char buf[1024];
654
655         char *fields[9];
656         int numfields;
657
658         if ((fh = fopen ("/proc/stat", "r")) == NULL)
659         {
660                 char errbuf[1024];
661                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
662                                 sstrerror (errno, errbuf, sizeof (errbuf)));
663                 return (-1);
664         }
665
666         while (fgets (buf, 1024, fh) != NULL)
667         {
668                 if (strncmp (buf, "cpu", 3))
669                         continue;
670                 if ((buf[3] < '0') || (buf[3] > '9'))
671                         continue;
672
673                 numfields = strsplit (buf, fields, 9);
674                 if (numfields < 5)
675                         continue;
676
677                 cpu = atoi (fields[0] + 3);
678
679                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) atoll(fields[1]), now);
680                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) atoll(fields[2]), now);
681                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
682                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) atoll(fields[4]), now);
683
684                 if (numfields >= 8)
685                 {
686                         cpu_stage (cpu, CPU_STATE_WAIT,      (derive_t) atoll(fields[5]), now);
687                         cpu_stage (cpu, CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
688                         cpu_stage (cpu, CPU_STATE_SOFTIRQ,   (derive_t) atoll(fields[7]), now);
689
690                         if (numfields >= 9)
691                                 cpu_stage (cpu, CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
692                 }
693         }
694         fclose (fh);
695 /* }}} #endif defined(KERNEL_LINUX) */
696
697 #elif defined(HAVE_LIBKSTAT) /* {{{ */
698         int cpu;
699         static cpu_stat_t cs;
700
701         if (kc == NULL)
702                 return (-1);
703
704         for (cpu = 0; cpu < numcpu; cpu++)
705         {
706                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
707                         continue; /* error message? */
708
709                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_IDLE,   (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE],   now);
710                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_USER,   (derive_t) cs.cpu_sysinfo.cpu[CPU_USER],   now);
711                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
712                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_WAIT,   (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT],   now);
713         }
714 /* }}} #endif defined(HAVE_LIBKSTAT) */
715
716 #elif CAN_USE_SYSCTL /* {{{ */
717         uint64_t cpuinfo[numcpu][CPUSTATES];
718         size_t cpuinfo_size;
719         int status;
720         int i;
721
722         if (numcpu < 1)
723         {
724                 ERROR ("cpu plugin: Could not determine number of "
725                                 "installed CPUs using sysctl(3).");
726                 return (-1);
727         }
728
729         memset (cpuinfo, 0, sizeof (cpuinfo));
730
731 #if defined(KERN_CPTIME2)
732         if (numcpu > 1) {
733                 for (i = 0; i < numcpu; i++) {
734                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
735
736                         cpuinfo_size = sizeof (cpuinfo[0]);
737
738                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
739                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
740                         if (status == -1) {
741                                 char errbuf[1024];
742                                 ERROR ("cpu plugin: sysctl failed: %s.",
743                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
744                                 return (-1);
745                         }
746                 }
747         }
748         else
749 #endif /* defined(KERN_CPTIME2) */
750         {
751                 int mib[] = {CTL_KERN, KERN_CPTIME};
752                 long cpuinfo_tmp[CPUSTATES];
753
754                 cpuinfo_size = sizeof(cpuinfo_tmp);
755
756                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
757                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
758                 if (status == -1)
759                 {
760                         char errbuf[1024];
761                         ERROR ("cpu plugin: sysctl failed: %s.",
762                                         sstrerror (errno, errbuf, sizeof (errbuf)));
763                         return (-1);
764                 }
765
766                 for(i = 0; i < CPUSTATES; i++) {
767                         cpuinfo[0][i] = cpuinfo_tmp[i];
768                 }
769         }
770
771         for (i = 0; i < numcpu; i++) {
772                 cpu_state (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER]);
773                 cpu_state (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE]);
774                 cpu_state (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS]);
775                 cpu_state (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE]);
776                 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
777         }
778 /* }}} #endif CAN_USE_SYSCTL */
779
780 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
781         long cpuinfo[maxcpu][CPUSTATES];
782         size_t cpuinfo_size;
783         int i;
784
785         memset (cpuinfo, 0, sizeof (cpuinfo));
786
787         cpuinfo_size = sizeof (cpuinfo);
788         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
789         {
790                 char errbuf[1024];
791                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
792                                 sstrerror (errno, errbuf, sizeof (errbuf)));
793                 return (-1);
794         }
795
796         for (i = 0; i < numcpu; i++) {
797                 cpu_state (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER]);
798                 cpu_state (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE]);
799                 cpu_state (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS]);
800                 cpu_state (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE]);
801                 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
802         }
803 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
804
805 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
806         long cpuinfo[CPUSTATES];
807         size_t cpuinfo_size;
808
809         cpuinfo_size = sizeof (cpuinfo);
810
811         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
812         {
813                 char errbuf[1024];
814                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
815                                 sstrerror (errno, errbuf, sizeof (errbuf)));
816                 return (-1);
817         }
818
819         cpu_state (0, CPU_STATE_USER,      (derive_t) cpuinfo[CP_USER]);
820         cpu_state (0, CPU_STATE_NICE,      (derive_t) cpuinfo[CP_NICE]);
821         cpu_state (0, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[CP_SYS]);
822         cpu_state (0, CPU_STATE_IDLE,      (derive_t) cpuinfo[CP_IDLE]);
823         cpu_state (0, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR]);
824 /* }}} #endif HAVE_SYSCTLBYNAME */
825
826 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
827         sg_cpu_stats *cs;
828         cs = sg_get_cpu_stats ();
829
830         if (cs == NULL)
831         {
832                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
833                 return (-1);
834         }
835
836         cpu_state (0, CPU_STATE_IDLE,   (derive_t) cs->idle);
837         cpu_state (0, CPU_STATE_NICE,   (derive_t) cs->nice);
838         cpu_state (0, CPU_STATE_SWAP,   (derive_t) cs->swap);
839         cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cs->kernel);
840         cpu_state (0, CPU_STATE_USER,   (derive_t) cs->user);
841         cpu_state (0, CPU_STATE_WAIT,   (derive_t) cs->iowait);
842 /* }}} #endif HAVE_LIBSTATGRAB */
843
844 #elif defined(HAVE_PERFSTAT) /* {{{ */
845         perfstat_id_t id;
846         int i, cpus;
847
848         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
849         if(numcpu == -1)
850         {
851                 char errbuf[1024];
852                 WARNING ("cpu plugin: perfstat_cpu: %s",
853                         sstrerror (errno, errbuf, sizeof (errbuf)));
854                 return (-1);
855         }
856
857         if (pnumcpu != numcpu || perfcpu == NULL)
858         {
859                 if (perfcpu != NULL)
860                         free(perfcpu);
861                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
862         }
863         pnumcpu = numcpu;
864
865         id.name[0] = '\0';
866         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
867         {
868                 char errbuf[1024];
869                 WARNING ("cpu plugin: perfstat_cpu: %s",
870                         sstrerror (errno, errbuf, sizeof (errbuf)));
871                 return (-1);
872         }
873
874         for (i = 0; i < cpus; i++)
875         {
876                 cpu_stage (i, CPU_STATE_IDLE,   (derive_t) perfcpu[i].idle, now);
877                 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys,  now);
878                 cpu_stage (i, CPU_STATE_USER,   (derive_t) perfcpu[i].user, now);
879                 cpu_stage (i, CPU_STATE_WAIT,   (derive_t) perfcpu[i].wait, now);
880         }
881 #endif /* }}} HAVE_PERFSTAT */
882
883         cpu_commit ();
884         cpu_reset ();
885         return (0);
886 }
887
888 void module_register (void)
889 {
890         plugin_register_init ("cpu", init);
891         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
892         plugin_register_read ("cpu", cpu_read);
893 } /* void module_register */
894
895 /* vim: set sw=8 sts=8 noet fdm=marker : */