mention configure script generation (idea from @hugoduncan).
[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         int state;
476
477         for (state = 0; state < CPU_STATE_ACTIVE; state++)
478         {
479                 size_t cpu_num;
480                 if (report_by_cpu) {
481                         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
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                 } else {
491                         derive_t derive_total = 0;
492                         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
493                                 {
494                                         cpu_state_t *s = get_cpu_state (cpu_num, state);
495
496                                         if (!s->has_value)
497                                                 continue;
498
499                                         derive_total += s->conv.last_value.derive;
500                         
501                                 }
502                         submit_derive (-1, (int) state, derive_total);
503                 }
504         }
505 } /* }}} void cpu_commit_without_aggregation */
506
507 /* Aggregates the internal state and dispatches the metrics. */
508 static void cpu_commit (void) /* {{{ */
509 {
510         gauge_t global_rates[CPU_STATE_MAX] = {
511                 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
512         };
513         size_t cpu_num;
514
515         if (report_by_state && !report_percent)
516         {
517                 cpu_commit_without_aggregation ();
518                 return;
519         }
520
521         aggregate (global_rates);
522
523         if (!report_by_cpu)
524         {
525                 cpu_commit_one (-1, global_rates);
526                 return;
527         }
528
529         for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
530         {
531                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
532                 gauge_t local_rates[CPU_STATE_MAX] = {
533                         NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
534                 };
535                 size_t state;
536
537                 for (state = 0; state < CPU_STATE_ACTIVE; state++)
538                         if (this_cpu_states[state].has_value)
539                                 local_rates[state] = this_cpu_states[state].rate;
540
541                 cpu_commit_one ((int) cpu_num, local_rates);
542         }
543 } /* }}} void cpu_commit */
544
545 /* Adds a derive value to the internal state. This should be used by each read
546  * function for each state. At the end of the iteration, the read function
547  * should call cpu_commit(). */
548 static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */
549 {
550         int status;
551         cpu_state_t *s;
552         value_t v;
553
554         if (state >= CPU_STATE_ACTIVE)
555                 return (EINVAL);
556
557         status = cpu_states_alloc (cpu_num);
558         if (status != 0)
559                 return (status);
560
561         if (global_cpu_num <= cpu_num)
562                 global_cpu_num = cpu_num + 1;
563
564         s = get_cpu_state (cpu_num, state);
565
566         v.gauge = NAN;
567         status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now);
568         if (status != 0)
569                 return (status);
570
571         s->rate = v.gauge;
572         s->has_value = 1;
573         return (0);
574 } /* }}} int cpu_stage */
575
576 static int cpu_read (void)
577 {
578         cdtime_t now = cdtime ();
579
580 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE /* {{{ */
581         int cpu;
582
583         kern_return_t status;
584
585 #if PROCESSOR_CPU_LOAD_INFO
586         processor_cpu_load_info_data_t cpu_info;
587         mach_msg_type_number_t         cpu_info_len;
588 #endif
589 #if PROCESSOR_TEMPERATURE
590         processor_info_data_t          cpu_temp;
591         mach_msg_type_number_t         cpu_temp_len;
592 #endif
593
594         host_t cpu_host;
595
596         for (cpu = 0; cpu < cpu_list_len; cpu++)
597         {
598 #if PROCESSOR_CPU_LOAD_INFO
599                 cpu_host = 0;
600                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
601
602                 if ((status = processor_info (cpu_list[cpu],
603                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
604                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
605                 {
606                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
607                         continue;
608                 }
609
610                 if (cpu_info_len < CPU_STATE_MAX)
611                 {
612                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
613                         continue;
614                 }
615
616                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER],   now);
617                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE],   now);
618                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
619                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE],   now);
620 #endif /* PROCESSOR_CPU_LOAD_INFO */
621
622 #if PROCESSOR_TEMPERATURE
623                 /*
624                  * Not all Apple computers do have this ability. To minimize
625                  * the messages sent to the syslog we do an exponential
626                  * stepback if `processor_info' fails. We still try ~once a day
627                  * though..
628                  */
629                 if (cpu_temp_retry_counter > 0)
630                 {
631                         cpu_temp_retry_counter--;
632                         continue;
633                 }
634
635                 cpu_temp_len = PROCESSOR_INFO_MAX;
636
637                 status = processor_info (cpu_list[cpu],
638                                 PROCESSOR_TEMPERATURE,
639                                 &cpu_host,
640                                 cpu_temp, &cpu_temp_len);
641                 if (status != KERN_SUCCESS)
642                 {
643                         ERROR ("cpu plugin: processor_info failed: %s",
644                                         mach_error_string (status));
645
646                         cpu_temp_retry_counter = cpu_temp_retry_step;
647                         cpu_temp_retry_step *= 2;
648                         if (cpu_temp_retry_step > cpu_temp_retry_max)
649                                 cpu_temp_retry_step = cpu_temp_retry_max;
650
651                         continue;
652                 }
653
654                 if (cpu_temp_len != 1)
655                 {
656                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
657                                         (int) cpu_temp_len);
658                         continue;
659                 }
660
661                 cpu_temp_retry_counter = 0;
662                 cpu_temp_retry_step    = 1;
663 #endif /* PROCESSOR_TEMPERATURE */
664         }
665 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
666
667 #elif defined(KERNEL_LINUX) /* {{{ */
668         int cpu;
669         FILE *fh;
670         char buf[1024];
671
672         char *fields[9];
673         int numfields;
674
675         if ((fh = fopen ("/proc/stat", "r")) == NULL)
676         {
677                 char errbuf[1024];
678                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
679                                 sstrerror (errno, errbuf, sizeof (errbuf)));
680                 return (-1);
681         }
682
683         while (fgets (buf, 1024, fh) != NULL)
684         {
685                 if (strncmp (buf, "cpu", 3))
686                         continue;
687                 if ((buf[3] < '0') || (buf[3] > '9'))
688                         continue;
689
690                 numfields = strsplit (buf, fields, 9);
691                 if (numfields < 5)
692                         continue;
693
694                 cpu = atoi (fields[0] + 3);
695
696                 cpu_stage (cpu, CPU_STATE_USER,   (derive_t) atoll(fields[1]), now);
697                 cpu_stage (cpu, CPU_STATE_NICE,   (derive_t) atoll(fields[2]), now);
698                 cpu_stage (cpu, CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
699                 cpu_stage (cpu, CPU_STATE_IDLE,   (derive_t) atoll(fields[4]), now);
700
701                 if (numfields >= 8)
702                 {
703                         cpu_stage (cpu, CPU_STATE_WAIT,      (derive_t) atoll(fields[5]), now);
704                         cpu_stage (cpu, CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
705                         cpu_stage (cpu, CPU_STATE_SOFTIRQ,   (derive_t) atoll(fields[7]), now);
706
707                         if (numfields >= 9)
708                                 cpu_stage (cpu, CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
709                 }
710         }
711         fclose (fh);
712 /* }}} #endif defined(KERNEL_LINUX) */
713
714 #elif defined(HAVE_LIBKSTAT) /* {{{ */
715         int cpu;
716         static cpu_stat_t cs;
717
718         if (kc == NULL)
719                 return (-1);
720
721         for (cpu = 0; cpu < numcpu; cpu++)
722         {
723                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
724                         continue; /* error message? */
725
726                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_IDLE,   (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE],   now);
727                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_USER,   (derive_t) cs.cpu_sysinfo.cpu[CPU_USER],   now);
728                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
729                 cpu_stage (ksp[cpu]->ks_instance, CPU_STATE_WAIT,   (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT],   now);
730         }
731 /* }}} #endif defined(HAVE_LIBKSTAT) */
732
733 #elif CAN_USE_SYSCTL /* {{{ */
734         uint64_t cpuinfo[numcpu][CPUSTATES];
735         size_t cpuinfo_size;
736         int status;
737         int i;
738
739         if (numcpu < 1)
740         {
741                 ERROR ("cpu plugin: Could not determine number of "
742                                 "installed CPUs using sysctl(3).");
743                 return (-1);
744         }
745
746         memset (cpuinfo, 0, sizeof (cpuinfo));
747
748 #if defined(KERN_CPTIME2)
749         if (numcpu > 1) {
750                 for (i = 0; i < numcpu; i++) {
751                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
752
753                         cpuinfo_size = sizeof (cpuinfo[0]);
754
755                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
756                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
757                         if (status == -1) {
758                                 char errbuf[1024];
759                                 ERROR ("cpu plugin: sysctl failed: %s.",
760                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
761                                 return (-1);
762                         }
763                 }
764         }
765         else
766 #endif /* defined(KERN_CPTIME2) */
767         {
768                 int mib[] = {CTL_KERN, KERN_CPTIME};
769                 long cpuinfo_tmp[CPUSTATES];
770
771                 cpuinfo_size = sizeof(cpuinfo_tmp);
772
773                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
774                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
775                 if (status == -1)
776                 {
777                         char errbuf[1024];
778                         ERROR ("cpu plugin: sysctl failed: %s.",
779                                         sstrerror (errno, errbuf, sizeof (errbuf)));
780                         return (-1);
781                 }
782
783                 for(i = 0; i < CPUSTATES; i++) {
784                         cpuinfo[0][i] = cpuinfo_tmp[i];
785                 }
786         }
787
788         for (i = 0; i < numcpu; i++) {
789                 cpu_stage (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
790                 cpu_stage (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
791                 cpu_stage (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
792                 cpu_stage (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
793                 cpu_stage (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
794         }
795 /* }}} #endif CAN_USE_SYSCTL */
796
797 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
798         long cpuinfo[maxcpu][CPUSTATES];
799         size_t cpuinfo_size;
800         int i;
801
802         memset (cpuinfo, 0, sizeof (cpuinfo));
803
804         cpuinfo_size = sizeof (cpuinfo);
805         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
806         {
807                 char errbuf[1024];
808                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
809                                 sstrerror (errno, errbuf, sizeof (errbuf)));
810                 return (-1);
811         }
812
813         for (i = 0; i < numcpu; i++) {
814                 cpu_stage (i, CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
815                 cpu_stage (i, CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
816                 cpu_stage (i, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
817                 cpu_stage (i, CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
818                 cpu_stage (i, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
819         }
820 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
821
822 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
823         long cpuinfo[CPUSTATES];
824         size_t cpuinfo_size;
825
826         cpuinfo_size = sizeof (cpuinfo);
827
828         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
829         {
830                 char errbuf[1024];
831                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
832                                 sstrerror (errno, errbuf, sizeof (errbuf)));
833                 return (-1);
834         }
835
836         cpu_stage (0, CPU_STATE_USER,      (derive_t) cpuinfo[CP_USER], now);
837         cpu_stage (0, CPU_STATE_NICE,      (derive_t) cpuinfo[CP_NICE], now);
838         cpu_stage (0, CPU_STATE_SYSTEM,    (derive_t) cpuinfo[CP_SYS], now);
839         cpu_stage (0, CPU_STATE_IDLE,      (derive_t) cpuinfo[CP_IDLE], now);
840         cpu_stage (0, CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
841 /* }}} #endif HAVE_SYSCTLBYNAME */
842
843 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
844         sg_cpu_stats *cs;
845         cs = sg_get_cpu_stats ();
846
847         if (cs == NULL)
848         {
849                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
850                 return (-1);
851         }
852
853         cpu_state (0, CPU_STATE_IDLE,   (derive_t) cs->idle);
854         cpu_state (0, CPU_STATE_NICE,   (derive_t) cs->nice);
855         cpu_state (0, CPU_STATE_SWAP,   (derive_t) cs->swap);
856         cpu_state (0, CPU_STATE_SYSTEM, (derive_t) cs->kernel);
857         cpu_state (0, CPU_STATE_USER,   (derive_t) cs->user);
858         cpu_state (0, CPU_STATE_WAIT,   (derive_t) cs->iowait);
859 /* }}} #endif HAVE_LIBSTATGRAB */
860
861 #elif defined(HAVE_PERFSTAT) /* {{{ */
862         perfstat_id_t id;
863         int i, cpus;
864
865         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
866         if(numcpu == -1)
867         {
868                 char errbuf[1024];
869                 WARNING ("cpu plugin: perfstat_cpu: %s",
870                         sstrerror (errno, errbuf, sizeof (errbuf)));
871                 return (-1);
872         }
873
874         if (pnumcpu != numcpu || perfcpu == NULL)
875         {
876                 if (perfcpu != NULL)
877                         free(perfcpu);
878                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
879         }
880         pnumcpu = numcpu;
881
882         id.name[0] = '\0';
883         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
884         {
885                 char errbuf[1024];
886                 WARNING ("cpu plugin: perfstat_cpu: %s",
887                         sstrerror (errno, errbuf, sizeof (errbuf)));
888                 return (-1);
889         }
890
891         for (i = 0; i < cpus; i++)
892         {
893                 cpu_stage (i, CPU_STATE_IDLE,   (derive_t) perfcpu[i].idle, now);
894                 cpu_stage (i, CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys,  now);
895                 cpu_stage (i, CPU_STATE_USER,   (derive_t) perfcpu[i].user, now);
896                 cpu_stage (i, CPU_STATE_WAIT,   (derive_t) perfcpu[i].wait, now);
897         }
898 #endif /* }}} HAVE_PERFSTAT */
899
900         cpu_commit ();
901         cpu_reset ();
902         return (0);
903 }
904
905 void module_register (void)
906 {
907         plugin_register_init ("cpu", init);
908         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
909         plugin_register_read ("cpu", cpu_read);
910 } /* void module_register */
911
912 /* vim: set sw=8 sts=8 noet fdm=marker : */