Merge pull request #774 from trenkel/master
[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         /* This function is called for all known CPU states, but each read
347          * method will only report a subset. The remaining states are left as
348          * NAN and we ignore them here. */
349         if (isnan (percent))
350                 return;
351
352         value.gauge = percent;
353         submit_value (cpu_num, cpu_state, "percent", value);
354 }
355
356 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
357 {
358         value_t value;
359
360         value.derive = derive;
361         submit_value (cpu_num, cpu_state, "cpu", value);
362 }
363
364 /* Takes the zero-index number of a CPU and makes sure that the module-global
365  * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
366 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
367 {
368         cpu_state_t *tmp;
369         size_t sz;
370
371         sz = (((size_t) cpu_num) + 1) * CPU_STATE_MAX;
372         assert (sz > 0);
373
374         /* We already have enough space. */
375         if (cpu_states_num >= sz)
376                 return 0;
377
378         tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
379         if (tmp == NULL)
380         {
381                 ERROR ("cpu plugin: realloc failed.");
382                 return (ENOMEM);
383         }
384         cpu_states = tmp;
385         tmp = cpu_states + cpu_states_num;
386
387         memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
388         cpu_states_num = sz;
389         return 0;
390 } /* }}} cpu_states_alloc */
391
392 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
393 {
394         size_t index = ((cpu_num * CPU_STATE_MAX) + state);
395
396         if (index >= cpu_states_num)
397                 return (NULL);
398
399         return (&cpu_states[index]);
400 } /* }}} cpu_state_t *get_cpu_state */
401
402 /* Populates the per-CPU CPU_STATE_ACTIVE rate and the global rate_by_state
403  * array. */
404 static void aggregate (gauge_t *sum_by_state) /* {{{ */
405 {
406         size_t cpu_num;
407         size_t state;
408
409         for (state = 0; state < CPU_STATE_MAX; state++)
410                 sum_by_state[state] = NAN;
411
412         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
413         {
414                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
415
416                 this_cpu_states[CPU_STATE_ACTIVE].rate = NAN;
417
418                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
419                 {
420                         if (!this_cpu_states[state].has_value)
421                                 continue;
422
423                         RATE_ADD (sum_by_state[state], this_cpu_states[state].rate);
424                         if (state != CPU_STATE_IDLE)
425                                 RATE_ADD (this_cpu_states[CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
426                 }
427
428                 RATE_ADD (sum_by_state[CPU_STATE_ACTIVE], this_cpu_states[CPU_STATE_ACTIVE].rate);
429         }
430 } /* }}} void aggregate */
431
432 /* Commits (dispatches) the values for one CPU or the global aggregation.
433  * cpu_num is the index of the CPU to be committed or -1 in case of the global
434  * aggregation. rates is a pointer to CPU_STATE_MAX gauge_t values holding the
435  * current rate; each rate may be NAN. Calculates the percentage of each state
436  * and dispatches the metric. */
437 static void cpu_commit_one (int cpu_num, /* {{{ */
438                 gauge_t rates[static CPU_STATE_MAX])
439 {
440         size_t state;
441         gauge_t sum;
442
443         sum = rates[CPU_STATE_ACTIVE];
444         RATE_ADD (sum, rates[CPU_STATE_IDLE]);
445
446         if (!report_by_state)
447         {
448                 gauge_t percent = 100.0 * rates[CPU_STATE_ACTIVE] / sum;
449                 submit_percent (cpu_num, CPU_STATE_ACTIVE, percent);
450                 return;
451         }
452
453         for (state = 0; state < CPU_STATE_ACTIVE; state++)
454         {
455                 gauge_t percent = 100.0 * rates[state] / sum;
456                 submit_percent (cpu_num, state, percent);
457         }
458 } /* }}} void cpu_commit_one */
459
460 /* Resets the internal aggregation. This is called by the read callback after
461  * each iteration / after each call to cpu_commit(). */
462 static void cpu_reset (void) /* {{{ */
463 {
464         size_t i;
465
466         for (i = 0; i < cpu_states_num; i++)
467                 cpu_states[i].has_value = 0;
468
469         global_cpu_num = 0;
470 } /* }}} void cpu_reset */
471
472 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
473 static void cpu_commit_without_aggregation (void) /* {{{ */
474 {
475         size_t cpu_num;
476
477         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
478         {
479                 int state;
480
481                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
482                 {
483                         cpu_state_t *s = get_cpu_state (cpu_num, state);
484
485                         if (!s->has_value)
486                                 continue;
487
488                         submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
489                 }
490         }
491 } /* }}} void cpu_commit_without_aggregation */
492
493 /* Aggregates the internal state and dispatches the metrics. */
494 static void cpu_commit (void) /* {{{ */
495 {
496         gauge_t global_rates[CPU_STATE_MAX] = {
497                 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
498         };
499         size_t cpu_num;
500
501         if (report_by_cpu && report_by_state && !report_percent)
502         {
503                 cpu_commit_without_aggregation ();
504                 return;
505         }
506
507         aggregate (global_rates);
508
509         if (!report_by_cpu)
510         {
511                 cpu_commit_one (-1, global_rates);
512                 return;
513         }
514
515         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
516         {
517                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
518                 gauge_t local_rates[CPU_STATE_MAX] = {
519                         NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
520                 };
521                 size_t state;
522
523                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
524                         if (this_cpu_states[state].has_value)
525                                 local_rates[state] = this_cpu_states[state].rate;
526
527                 cpu_commit_one ((int) cpu_num, local_rates);
528         }
529 } /* }}} void cpu_commit */
530
531 /* Adds a derive value to the internal state. This should be used by each read
532  * function for each state. At the end of the iteration, the read function
533  * should call cpu_commit(). */
534 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
535 {
536         int status;
537         cpu_state_t *s;
538         value_t v;
539
540         if (state >= CPU_STATE_ACTIVE)
541                 return (EINVAL);
542
543         status = cpu_states_alloc (cpu_num);
544         if (status != 0)
545                 return (status);
546
547         if (global_cpu_num <= cpu_num)
548                 global_cpu_num = cpu_num + 1;
549
550         s = get_cpu_state (cpu_num, state);
551
552         v.gauge = NAN;
553         status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
554         if (status != 0)
555                 return (status);
556
557         s->rate = v.gauge;
558         s->has_value = 1;
559         return (0);
560 } /* }}} int cpu_stage */
561
562 static int cpu_read (void)
563 {
564         cdtime_t now = cdtime ();
565
566 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE /* {{{ */
567         int cpu;
568
569         kern_return_t status;
570
571 #if PROCESSOR_CPU_LOAD_INFO
572         processor_cpu_load_info_data_t cpu_info;
573         mach_msg_type_number_t         cpu_info_len;
574 #endif
575 #if PROCESSOR_TEMPERATURE
576         processor_info_data_t          cpu_temp;
577         mach_msg_type_number_t         cpu_temp_len;
578 #endif
579
580         host_t cpu_host;
581
582         for (cpu = 0; cpu < cpu_list_len; cpu++)
583         {
584 #if PROCESSOR_CPU_LOAD_INFO
585                 cpu_host = 0;
586                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
587
588                 if ((status = processor_info (cpu_list[cpu],
589                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
590                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
591                 {
592                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
593                         continue;
594                 }
595
596                 if (cpu_info_len < CPU_STATE_MAX)
597                 {
598                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
599                         continue;
600                 }
601
602                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER],   now);
603                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE],   now);
604                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
605                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE],   now);
606 #endif /* PROCESSOR_CPU_LOAD_INFO */
607
608 #if PROCESSOR_TEMPERATURE
609                 /*
610                  * Not all Apple computers do have this ability. To minimize
611                  * the messages sent to the syslog we do an exponential
612                  * stepback if `processor_info' fails. We still try ~once a day
613                  * though..
614                  */
615                 if (cpu_temp_retry_counter > 0)
616                 {
617                         cpu_temp_retry_counter--;
618                         continue;
619                 }
620
621                 cpu_temp_len = PROCESSOR_INFO_MAX;
622
623                 status = processor_info (cpu_list[cpu],
624                                 PROCESSOR_TEMPERATURE,
625                                 &cpu_host,
626                                 cpu_temp, &cpu_temp_len);
627                 if (status != KERN_SUCCESS)
628                 {
629                         ERROR ("cpu plugin: processor_info failed: %s",
630                                         mach_error_string (status));
631
632                         cpu_temp_retry_counter = cpu_temp_retry_step;
633                         cpu_temp_retry_step *= 2;
634                         if (cpu_temp_retry_step > cpu_temp_retry_max)
635                                 cpu_temp_retry_step = cpu_temp_retry_max;
636
637                         continue;
638                 }
639
640                 if (cpu_temp_len != 1)
641                 {
642                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
643                                         (int) cpu_temp_len);
644                         continue;
645                 }
646
647                 cpu_temp_retry_counter = 0;
648                 cpu_temp_retry_step    = 1;
649 #endif /* PROCESSOR_TEMPERATURE */
650         }
651 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
652
653 #elif defined(KERNEL_LINUX) /* {{{ */
654         int cpu;
655         FILE *fh;
656         char buf[1024];
657
658         char *fields[9];
659         int numfields;
660
661         if ((fh = fopen ("/proc/stat", "r")) == NULL)
662         {
663                 char errbuf[1024];
664                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
665                                 sstrerror (errno, errbuf, sizeof (errbuf)));
666                 return (-1);
667         }
668
669         while (fgets (buf, 1024, fh) != NULL)
670         {
671                 if (strncmp (buf, "cpu", 3))
672                         continue;
673                 if ((buf[3] < '0') || (buf[3] > '9'))
674                         continue;
675
676                 numfields = strsplit (buf, fields, 9);
677                 if (numfields < 5)
678                         continue;
679
680                 cpu = atoi (fields[0] + 3);
681
682                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) atoll(fields[1]), now);
683                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) atoll(fields[2]), now);
684                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
685                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) atoll(fields[4]), now);
686
687                 if (numfields >= 8)
688                 {
689                         cpu_stage (cpu, CPU_STATE_WAIT,      (derive_t) atoll(fields[5]), now);
690                         cpu_stage (cpu, CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
691                         cpu_stage (cpu, CPU_STATE_SOFTIRQ,   (derive_t) atoll(fields[7]), now);
692
693                         if (numfields >= 9)
694                                 cpu_stage (cpu, CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
695                 }
696         }
697         fclose (fh);
698 /* }}} #endif defined(KERNEL_LINUX) */
699
700 #elif defined(HAVE_LIBKSTAT) /* {{{ */
701         int cpu;
702         static cpu_stat_t cs;
703
704         if (kc == NULL)
705                 return (-1);
706
707         for (cpu = 0; cpu < numcpu; cpu++)
708         {
709                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
710                         continue; /* error message? */
711
712                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_IDLE,   (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE],   now);
713                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_USER,   (derive_t) cs.cpu_sysinfo.cpu[CPU_USER],   now);
714                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
715                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_WAIT,   (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT],   now);
716         }
717 /* }}} #endif defined(HAVE_LIBKSTAT) */
718
719 #elif CAN_USE_SYSCTL /* {{{ */
720         uint64_t cpuinfo[numcpu][CPUSTATES];
721         size_t cpuinfo_size;
722         int status;
723         int i;
724
725         if (numcpu < 1)
726         {
727                 ERROR ("cpu plugin: Could not determine number of "
728                                 "installed CPUs using sysctl(3).");
729                 return (-1);
730         }
731
732         memset (cpuinfo, 0, sizeof (cpuinfo));
733
734 #if defined(KERN_CPTIME2)
735         if (numcpu > 1) {
736                 for (i = 0; i < numcpu; i++) {
737                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
738
739                         cpuinfo_size = sizeof (cpuinfo[0]);
740
741                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
742                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
743                         if (status == -1) {
744                                 char errbuf[1024];
745                                 ERROR ("cpu plugin: sysctl failed: %s.",
746                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
747                                 return (-1);
748                         }
749                 }
750         }
751         else
752 #endif /* defined(KERN_CPTIME2) */
753         {
754                 int mib[] = {CTL_KERN, KERN_CPTIME};
755                 long cpuinfo_tmp[CPUSTATES];
756
757                 cpuinfo_size = sizeof(cpuinfo_tmp);
758
759                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
760                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
761                 if (status == -1)
762                 {
763                         char errbuf[1024];
764                         ERROR ("cpu plugin: sysctl failed: %s.",
765                                         sstrerror (errno, errbuf, sizeof (errbuf)));
766                         return (-1);
767                 }
768
769                 for(i = 0; i < CPUSTATES; i++) {
770                         cpuinfo[0][i] = cpuinfo_tmp[i];
771                 }
772         }
773
774         for (i = 0; i < numcpu; i++) {
775                 cpu_state (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER]);
776                 cpu_state (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE]);
777                 cpu_state (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS]);
778                 cpu_state (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE]);
779                 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
780         }
781 /* }}} #endif CAN_USE_SYSCTL */
782
783 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
784         long cpuinfo[maxcpu][CPUSTATES];
785         size_t cpuinfo_size;
786         int i;
787
788         memset (cpuinfo, 0, sizeof (cpuinfo));
789
790         cpuinfo_size = sizeof (cpuinfo);
791         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
792         {
793                 char errbuf[1024];
794                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
795                                 sstrerror (errno, errbuf, sizeof (errbuf)));
796                 return (-1);
797         }
798
799         for (i = 0; i < numcpu; i++) {
800                 cpu_state (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER]);
801                 cpu_state (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE]);
802                 cpu_state (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS]);
803                 cpu_state (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE]);
804                 cpu_state (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR]);
805         }
806 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
807
808 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
809         long cpuinfo[CPUSTATES];
810         size_t cpuinfo_size;
811
812         cpuinfo_size = sizeof (cpuinfo);
813
814         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
815         {
816                 char errbuf[1024];
817                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
818                                 sstrerror (errno, errbuf, sizeof (errbuf)));
819                 return (-1);
820         }
821
822         cpu_state (0, CPU_STATE_USER,      (derive_t) cpuinfo[CP_USER]);
823         cpu_state (0, CPU_STATE_NICE,      (derive_t) cpuinfo[CP_NICE]);
824         cpu_state (0, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[CP_SYS]);
825         cpu_state (0, CPU_STATE_IDLE,      (derive_t) cpuinfo[CP_IDLE]);
826         cpu_state (0, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR]);
827 /* }}} #endif HAVE_SYSCTLBYNAME */
828
829 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
830         sg_cpu_stats *cs;
831         cs = sg_get_cpu_stats ();
832
833         if (cs == NULL)
834         {
835                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
836                 return (-1);
837         }
838
839         cpu_state (0, CPU_STATE_IDLE,   (derive_t) cs->idle);
840         cpu_state (0, CPU_STATE_NICE,   (derive_t) cs->nice);
841         cpu_state (0, CPU_STATE_SWAP,   (derive_t) cs->swap);
842         cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cs->kernel);
843         cpu_state (0, CPU_STATE_USER,   (derive_t) cs->user);
844         cpu_state (0, CPU_STATE_WAIT,   (derive_t) cs->iowait);
845 /* }}} #endif HAVE_LIBSTATGRAB */
846
847 #elif defined(HAVE_PERFSTAT) /* {{{ */
848         perfstat_id_t id;
849         int i, cpus;
850
851         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
852         if(numcpu == -1)
853         {
854                 char errbuf[1024];
855                 WARNING ("cpu plugin: perfstat_cpu: %s",
856                         sstrerror (errno, errbuf, sizeof (errbuf)));
857                 return (-1);
858         }
859
860         if (pnumcpu != numcpu || perfcpu == NULL)
861         {
862                 if (perfcpu != NULL)
863                         free(perfcpu);
864                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
865         }
866         pnumcpu = numcpu;
867
868         id.name[0] = '\0';
869         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
870         {
871                 char errbuf[1024];
872                 WARNING ("cpu plugin: perfstat_cpu: %s",
873                         sstrerror (errno, errbuf, sizeof (errbuf)));
874                 return (-1);
875         }
876
877         for (i = 0; i < cpus; i++)
878         {
879                 cpu_stage (i, CPU_STATE_IDLE,   (derive_t) perfcpu[i].idle, now);
880                 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys,  now);
881                 cpu_stage (i, CPU_STATE_USER,   (derive_t) perfcpu[i].user, now);
882                 cpu_stage (i, CPU_STATE_WAIT,   (derive_t) perfcpu[i].wait, now);
883         }
884 #endif /* }}} HAVE_PERFSTAT */
885
886         cpu_commit ();
887         cpu_reset ();
888         return (0);
889 }
890
891 void module_register (void)
892 {
893         plugin_register_init ("cpu", init);
894         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
895         plugin_register_read ("cpu", cpu_read);
896 } /* void module_register */
897
898 /* vim: set sw=8 sts=8 noet fdm=marker : */