write_tsdb: enable TCP keepalive on network socket
[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
32 #include "common.h"
33 #include "plugin.h"
34
35 #ifdef HAVE_MACH_KERN_RETURN_H
36 # include <mach/kern_return.h>
37 #endif
38 #ifdef HAVE_MACH_MACH_INIT_H
39 # include <mach/mach_init.h>
40 #endif
41 #ifdef HAVE_MACH_HOST_PRIV_H
42 # include <mach/host_priv.h>
43 #endif
44 #if HAVE_MACH_MACH_ERROR_H
45 #  include <mach/mach_error.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_INFO_H
48 # include <mach/processor_info.h>
49 #endif
50 #ifdef HAVE_MACH_PROCESSOR_H
51 # include <mach/processor.h>
52 #endif
53 #ifdef HAVE_MACH_VM_MAP_H
54 # include <mach/vm_map.h>
55 #endif
56
57 #ifdef HAVE_LIBKSTAT
58 # include <sys/sysinfo.h>
59 #endif /* HAVE_LIBKSTAT */
60
61 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
62         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
63 # ifdef HAVE_SYS_SYSCTL_H
64 #  include <sys/sysctl.h>
65 # endif
66
67 # ifdef HAVE_SYS_DKSTAT_H
68 #  include <sys/dkstat.h>
69 # endif
70
71 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
72 #  define CP_USER   0
73 #  define CP_NICE   1
74 #  define CP_SYS    2
75 #  define CP_INTR   3
76 #  define CP_IDLE   4
77 #  define CPUSTATES 5
78 # endif
79 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
80
81 #if HAVE_SYSCTL
82 # if defined(CTL_HW) && defined(HW_NCPU) \
83         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
84 #  define CAN_USE_SYSCTL 1
85 # else
86 #  define CAN_USE_SYSCTL 0
87 # endif
88 #else
89 # define CAN_USE_SYSCTL 0
90 #endif
91
92 #define COLLECTD_CPU_STATE_USER 0
93 #define COLLECTD_CPU_STATE_SYSTEM 1
94 #define COLLECTD_CPU_STATE_WAIT 2
95 #define COLLECTD_CPU_STATE_NICE 3
96 #define COLLECTD_CPU_STATE_SWAP 4
97 #define COLLECTD_CPU_STATE_INTERRUPT 5
98 #define COLLECTD_CPU_STATE_SOFTIRQ 6
99 #define COLLECTD_CPU_STATE_STEAL 7
100 #define COLLECTD_CPU_STATE_IDLE 8
101 #define COLLECTD_CPU_STATE_ACTIVE 9 /* sum of (!idle) */
102 #define COLLECTD_CPU_STATE_MAX 10 /* #states */
103
104 #if HAVE_STATGRAB_H
105 # include <statgrab.h>
106 #endif
107
108 # ifdef HAVE_PERFSTAT
109 #  include <sys/protosw.h>
110 #  include <libperfstat.h>
111 # endif /* HAVE_PERFSTAT */
112
113 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
114         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
115 # error "No applicable input method."
116 #endif
117
118 static const char *cpu_state_names[] = {
119         "user",
120         "system",
121         "wait",
122         "nice",
123         "swap",
124         "interrupt",
125         "softirq",
126         "steal",
127         "idle",
128         "active"
129 };
130
131 #ifdef PROCESSOR_CPU_LOAD_INFO
132 static mach_port_t port_host;
133 static processor_port_array_t cpu_list;
134 static mach_msg_type_number_t cpu_list_len;
135 /* #endif PROCESSOR_CPU_LOAD_INFO */
136
137 #elif defined(KERNEL_LINUX)
138 /* no variables needed */
139 /* #endif KERNEL_LINUX */
140
141 #elif defined(HAVE_LIBKSTAT)
142 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
143 # define MAX_NUMCPU 256
144 extern kstat_ctl_t *kc;
145 static kstat_t *ksp[MAX_NUMCPU];
146 static int numcpu;
147 /* #endif HAVE_LIBKSTAT */
148
149 #elif CAN_USE_SYSCTL
150 static int numcpu;
151 /* #endif CAN_USE_SYSCTL */
152
153 #elif defined(HAVE_SYSCTLBYNAME)
154 static int numcpu;
155 #  ifdef HAVE_SYSCTL_KERN_CP_TIMES
156 static int maxcpu;
157 #  endif /* HAVE_SYSCTL_KERN_CP_TIMES */
158 /* #endif HAVE_SYSCTLBYNAME */
159
160 #elif defined(HAVE_LIBSTATGRAB)
161 /* no variables needed */
162 /* #endif  HAVE_LIBSTATGRAB */
163
164 #elif defined(HAVE_PERFSTAT)
165 static perfstat_cpu_t *perfcpu;
166 static int numcpu;
167 static int pnumcpu;
168 #endif /* HAVE_PERFSTAT */
169
170 #define RATE_ADD(sum, val) do { \
171         if (isnan (sum))        \
172         (sum) = (val);          \
173         else if (!isnan (val))  \
174         (sum) += (val);         \
175 } while (0)
176
177 struct cpu_state_s
178 {
179         value_to_rate_state_t conv;
180         gauge_t rate;
181         _Bool has_value;
182 };
183 typedef struct cpu_state_s cpu_state_t;
184
185 static cpu_state_t *cpu_states = NULL;
186 static size_t cpu_states_num = 0; /* #cpu_states allocated */
187
188 /* Highest CPU number in the current iteration. Used by the dispatch logic to
189  * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
190 static size_t global_cpu_num = 0;
191
192 static _Bool report_by_cpu = 1;
193 static _Bool report_by_state = 1;
194 static _Bool report_percent = 0;
195
196 static const char *config_keys[] =
197 {
198         "ReportByCpu",
199         "ReportByState",
200         "ValuesPercentage"
201 };
202 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
203
204 static int cpu_config (char const *key, char const *value) /* {{{ */
205 {
206         if (strcasecmp (key, "ReportByCpu") == 0)
207                 report_by_cpu = IS_TRUE (value) ? 1 : 0;
208         else if (strcasecmp (key, "ValuesPercentage") == 0)
209                 report_percent = IS_TRUE (value) ? 1 : 0;
210         else if (strcasecmp (key, "ReportByState") == 0)
211                 report_by_state = IS_TRUE (value) ? 1 : 0;
212         else
213                 return (-1);
214
215         return (0);
216 } /* }}} int cpu_config */
217
218 static int init (void)
219 {
220 #if PROCESSOR_CPU_LOAD_INFO
221         kern_return_t status;
222
223         port_host = mach_host_self ();
224
225         status = host_processors (port_host, &cpu_list, &cpu_list_len);
226         if (status == KERN_INVALID_ARGUMENT)
227         {
228                 ERROR ("cpu plugin: Don't have a privileged host control port. "
229                                 "The most common cause for this problem is "
230                                 "that collectd is running without root "
231                                 "privileges, which are required to read CPU "
232                                 "load information. "
233                                 "<https://collectd.org/bugs/22>");
234                 cpu_list_len = 0;
235                 return (-1);
236         }
237         if (status != KERN_SUCCESS)
238         {
239                 ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status);
240                 cpu_list_len = 0;
241                 return (-1);
242         }
243
244         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
245 /* #endif PROCESSOR_CPU_LOAD_INFO */
246
247 #elif defined(HAVE_LIBKSTAT)
248         kstat_t *ksp_chain;
249
250         numcpu = 0;
251
252         if (kc == NULL)
253                 return (-1);
254
255         /* Solaris doesn't count linear.. *sigh* */
256         for (numcpu = 0, ksp_chain = kc->kc_chain;
257                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
258                         ksp_chain = ksp_chain->ks_next)
259                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
260                         ksp[numcpu++] = ksp_chain;
261 /* #endif HAVE_LIBKSTAT */
262
263 #elif CAN_USE_SYSCTL
264         size_t numcpu_size;
265         int mib[2] = {CTL_HW, HW_NCPU};
266         int status;
267
268         numcpu = 0;
269         numcpu_size = sizeof (numcpu);
270
271         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
272                         &numcpu, &numcpu_size, NULL, 0);
273         if (status == -1)
274         {
275                 char errbuf[1024];
276                 WARNING ("cpu plugin: sysctl: %s",
277                                 sstrerror (errno, errbuf, sizeof (errbuf)));
278                 return (-1);
279         }
280 /* #endif CAN_USE_SYSCTL */
281
282 #elif defined (HAVE_SYSCTLBYNAME)
283         size_t numcpu_size;
284
285         numcpu_size = sizeof (numcpu);
286
287         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
288         {
289                 char errbuf[1024];
290                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
291                                 sstrerror (errno, errbuf, sizeof (errbuf)));
292                 return (-1);
293         }
294
295 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
296         numcpu_size = sizeof (maxcpu);
297
298         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
299         {
300                 char errbuf[1024];
301                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
302                                 sstrerror (errno, errbuf, sizeof (errbuf)));
303                 return (-1);
304         }
305 #else
306         if (numcpu != 1)
307                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
308 #endif
309 /* #endif HAVE_SYSCTLBYNAME */
310
311 #elif defined(HAVE_LIBSTATGRAB)
312         /* nothing to initialize */
313 /* #endif HAVE_LIBSTATGRAB */
314
315 #elif defined(HAVE_PERFSTAT)
316         /* nothing to initialize */
317 #endif /* HAVE_PERFSTAT */
318
319         return (0);
320 } /* int init */
321
322 static void submit_value (int cpu_num, int cpu_state, const char *type, value_t value)
323 {
324         value_t values[1];
325         value_list_t vl = VALUE_LIST_INIT;
326
327         memcpy(&values[0], &value, sizeof(value));
328
329         vl.values = values;
330         vl.values_len = 1;
331
332         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
333         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
334         sstrncpy (vl.type, type, sizeof (vl.type));
335         sstrncpy (vl.type_instance, cpu_state_names[cpu_state],
336                         sizeof (vl.type_instance));
337
338         if (cpu_num >= 0) {
339                 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
340                                 "%i", cpu_num);
341         }
342         plugin_dispatch_values (&vl);
343 }
344
345 static void submit_percent(int cpu_num, int cpu_state, gauge_t percent)
346 {
347         value_t value;
348
349         /* This function is called for all known CPU states, but each read
350          * method will only report a subset. The remaining states are left as
351          * NAN and we ignore them here. */
352         if (isnan (percent))
353                 return;
354
355         value.gauge = percent;
356         submit_value (cpu_num, cpu_state, "percent", value);
357 }
358
359 static void submit_derive(int cpu_num, int cpu_state, derive_t derive)
360 {
361         value_t value;
362
363         value.derive = derive;
364         submit_value (cpu_num, cpu_state, "cpu", value);
365 }
366
367 /* Takes the zero-index number of a CPU and makes sure that the module-global
368  * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
369 static int cpu_states_alloc (size_t cpu_num) /* {{{ */
370 {
371         cpu_state_t *tmp;
372         size_t sz;
373
374         sz = (((size_t) cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
375         assert (sz > 0);
376
377         /* We already have enough space. */
378         if (cpu_states_num >= sz)
379                 return 0;
380
381         tmp = realloc (cpu_states, sz * sizeof (*cpu_states));
382         if (tmp == NULL)
383         {
384                 ERROR ("cpu plugin: realloc failed.");
385                 return (ENOMEM);
386         }
387         cpu_states = tmp;
388         tmp = cpu_states + cpu_states_num;
389
390         memset (tmp, 0, (sz - cpu_states_num) * sizeof (*cpu_states));
391         cpu_states_num = sz;
392         return 0;
393 } /* }}} cpu_states_alloc */
394
395 static cpu_state_t *get_cpu_state (size_t cpu_num, size_t state) /* {{{ */
396 {
397         size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
398
399         if (index >= cpu_states_num)
400                 return (NULL);
401
402         return (&cpu_states[index]);
403 } /* }}} cpu_state_t *get_cpu_state */
404
405 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global rate_by_state
406  * array. */
407 static void aggregate (gauge_t *sum_by_state) /* {{{ */
408 {
409         for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
410                 sum_by_state[state] = NAN;
411
412         for (size_t 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[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
417
418                 for (size_t state = 0; state < COLLECTD_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 != COLLECTD_CPU_STATE_IDLE)
425                                 RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate);
426                 }
427
428                 if (!isnan (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
429                         this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1;
430
431                 RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
432         }
433 } /* }}} void aggregate */
434
435 /* Commits (dispatches) the values for one CPU or the global aggregation.
436  * cpu_num is the index of the CPU to be committed or -1 in case of the global
437  * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values holding the
438  * current rate; each rate may be NAN. Calculates the percentage of each state
439  * and dispatches the metric. */
440 static void cpu_commit_one (int cpu_num, /* {{{ */
441                 gauge_t rates[static COLLECTD_CPU_STATE_MAX])
442 {
443         gauge_t sum;
444
445         sum = rates[COLLECTD_CPU_STATE_ACTIVE];
446         RATE_ADD (sum, rates[COLLECTD_CPU_STATE_IDLE]);
447
448         if (!report_by_state)
449         {
450                 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
451                 submit_percent (cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
452                 return;
453         }
454
455         for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
456         {
457                 gauge_t percent = 100.0 * rates[state] / sum;
458                 submit_percent (cpu_num, state, percent);
459         }
460 } /* }}} void cpu_commit_one */
461
462 /* Resets the internal aggregation. This is called by the read callback after
463  * each iteration / after each call to cpu_commit(). */
464 static void cpu_reset (void) /* {{{ */
465 {
466         for (size_t 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         for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++)
476         {
477                 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
478                 {
479                         cpu_state_t *s = get_cpu_state (cpu_num, state);
480
481                         if (!s->has_value)
482                                 continue;
483
484                         submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive);
485                 }
486         }
487 } /* }}} void cpu_commit_without_aggregation */
488
489 /* Aggregates the internal state and dispatches the metrics. */
490 static void cpu_commit (void) /* {{{ */
491 {
492         gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
493                 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
494         };
495
496         if (report_by_state && report_by_cpu && !report_percent)
497         {
498                 cpu_commit_without_aggregation ();
499                 return;
500         }
501
502         aggregate (global_rates);
503
504         if (!report_by_cpu)
505         {
506                 cpu_commit_one (-1, global_rates);
507                 return;
508         }
509
510         for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++)
511         {
512                 cpu_state_t *this_cpu_states = get_cpu_state (cpu_num, 0);
513                 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
514                         NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN
515                 };
516
517                 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
518                         if (this_cpu_states[state].has_value)
519                                 local_rates[state] = this_cpu_states[state].rate;
520
521                 cpu_commit_one ((int) cpu_num, local_rates);
522         }
523 } /* }}} void cpu_commit */
524
525 /* Adds a derive value to the internal state. This should be used by each read
526  * function for each state. At the end of the iteration, the read function
527  * should call cpu_commit(). */
528 static int cpu_stage (size_t cpu_num, size_t state, derive_t d, cdtime_t now) /* {{{ */
529 {
530         int status;
531         cpu_state_t *s;
532         gauge_t rate = NAN;
533         value_t val = {.derive = d};
534
535         if (state >= COLLECTD_CPU_STATE_ACTIVE)
536                 return (EINVAL);
537
538         status = cpu_states_alloc (cpu_num);
539         if (status != 0)
540                 return (status);
541
542         if (global_cpu_num <= cpu_num)
543                 global_cpu_num = cpu_num + 1;
544
545         s = get_cpu_state (cpu_num, state);
546
547         status = value_to_rate (&rate, val, DS_TYPE_DERIVE, now, &s->conv);
548         if (status != 0)
549                 return (status);
550
551         s->rate = rate;
552         s->has_value = 1;
553         return (0);
554 } /* }}} int cpu_stage */
555
556 static int cpu_read (void)
557 {
558         cdtime_t now = cdtime ();
559
560 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
561         kern_return_t status;
562
563         processor_cpu_load_info_data_t cpu_info;
564         mach_msg_type_number_t         cpu_info_len;
565
566         host_t cpu_host;
567
568         for (int cpu = 0; cpu < cpu_list_len; cpu++)
569         {
570                 cpu_host = 0;
571                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
572
573                 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
574                                 (processor_info_t) &cpu_info, &cpu_info_len);
575                 if (status != KERN_SUCCESS)
576                 {
577                         ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
578                                         mach_error_string (status));
579                         continue;
580                 }
581
582                 if (cpu_info_len < CPU_STATE_MAX)
583                 {
584                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
585                         continue;
586                 }
587
588                 cpu_stage (cpu, COLLECTD_CPU_STATE_USER,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER],   now);
589                 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE],   now);
590                 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
591                 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE,   (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE],   now);
592         }
593 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
594
595 #elif defined(KERNEL_LINUX) /* {{{ */
596         int cpu;
597         FILE *fh;
598         char buf[1024];
599
600         char *fields[9];
601         int numfields;
602
603         if ((fh = fopen ("/proc/stat", "r")) == NULL)
604         {
605                 char errbuf[1024];
606                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
607                                 sstrerror (errno, errbuf, sizeof (errbuf)));
608                 return (-1);
609         }
610
611         while (fgets (buf, 1024, fh) != NULL)
612         {
613                 if (strncmp (buf, "cpu", 3))
614                         continue;
615                 if ((buf[3] < '0') || (buf[3] > '9'))
616                         continue;
617
618                 numfields = strsplit (buf, fields, 9);
619                 if (numfields < 5)
620                         continue;
621
622                 cpu = atoi (fields[0] + 3);
623
624                 cpu_stage (cpu, COLLECTD_CPU_STATE_USER,   (derive_t) atoll(fields[1]), now);
625                 cpu_stage (cpu, COLLECTD_CPU_STATE_NICE,   (derive_t) atoll(fields[2]), now);
626                 cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) atoll(fields[3]), now);
627                 cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE,   (derive_t) atoll(fields[4]), now);
628
629                 if (numfields >= 8)
630                 {
631                         cpu_stage (cpu, COLLECTD_CPU_STATE_WAIT,      (derive_t) atoll(fields[5]), now);
632                         cpu_stage (cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) atoll(fields[6]), now);
633                         cpu_stage (cpu, COLLECTD_CPU_STATE_SOFTIRQ,   (derive_t) atoll(fields[7]), now);
634
635                         if (numfields >= 9)
636                                 cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
637                 }
638         }
639         fclose (fh);
640 /* }}} #endif defined(KERNEL_LINUX) */
641
642 #elif defined(HAVE_LIBKSTAT) /* {{{ */
643         static cpu_stat_t cs;
644
645         if (kc == NULL)
646                 return (-1);
647
648         for (int cpu = 0; cpu < numcpu; cpu++)
649         {
650                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
651                         continue; /* error message? */
652
653                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,   (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE],   now);
654                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,   (derive_t) cs.cpu_sysinfo.cpu[CPU_USER],   now);
655                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
656                 cpu_stage (ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,   (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT],   now);
657         }
658 /* }}} #endif defined(HAVE_LIBKSTAT) */
659
660 #elif CAN_USE_SYSCTL /* {{{ */
661         uint64_t cpuinfo[numcpu][CPUSTATES];
662         size_t cpuinfo_size;
663         int status;
664
665         if (numcpu < 1)
666         {
667                 ERROR ("cpu plugin: Could not determine number of "
668                                 "installed CPUs using sysctl(3).");
669                 return (-1);
670         }
671
672         memset (cpuinfo, 0, sizeof (cpuinfo));
673
674 #if defined(KERN_CPTIME2)
675         if (numcpu > 1) {
676                 for (int i = 0; i < numcpu; i++) {
677                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
678
679                         cpuinfo_size = sizeof (cpuinfo[0]);
680
681                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
682                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
683                         if (status == -1) {
684                                 char errbuf[1024];
685                                 ERROR ("cpu plugin: sysctl failed: %s.",
686                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
687                                 return (-1);
688                         }
689                 }
690         }
691         else
692 #endif /* defined(KERN_CPTIME2) */
693         {
694                 int mib[] = {CTL_KERN, KERN_CPTIME};
695                 long cpuinfo_tmp[CPUSTATES];
696
697                 cpuinfo_size = sizeof(cpuinfo_tmp);
698
699                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
700                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
701                 if (status == -1)
702                 {
703                         char errbuf[1024];
704                         ERROR ("cpu plugin: sysctl failed: %s.",
705                                         sstrerror (errno, errbuf, sizeof (errbuf)));
706                         return (-1);
707                 }
708
709                 for(int i = 0; i < CPUSTATES; i++) {
710                         cpuinfo[0][i] = cpuinfo_tmp[i];
711                 }
712         }
713
714         for (int i = 0; i < numcpu; i++) {
715                 cpu_stage (i, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
716                 cpu_stage (i, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
717                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
718                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
719                 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
720         }
721 /* }}} #endif CAN_USE_SYSCTL */
722
723 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ */
724         long cpuinfo[maxcpu][CPUSTATES];
725         size_t cpuinfo_size;
726
727         memset (cpuinfo, 0, sizeof (cpuinfo));
728
729         cpuinfo_size = sizeof (cpuinfo);
730         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
731         {
732                 char errbuf[1024];
733                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
734                                 sstrerror (errno, errbuf, sizeof (errbuf)));
735                 return (-1);
736         }
737
738         for (int i = 0; i < numcpu; i++) {
739                 cpu_stage (i, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[i][CP_USER], now);
740                 cpu_stage (i, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[i][CP_NICE], now);
741                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[i][CP_SYS], now);
742                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[i][CP_IDLE], now);
743                 cpu_stage (i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[i][CP_INTR], now);
744         }
745 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
746
747 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
748         long cpuinfo[CPUSTATES];
749         size_t cpuinfo_size;
750
751         cpuinfo_size = sizeof (cpuinfo);
752
753         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
754         {
755                 char errbuf[1024];
756                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
757                                 sstrerror (errno, errbuf, sizeof (errbuf)));
758                 return (-1);
759         }
760
761         cpu_stage (0, COLLECTD_CPU_STATE_USER,      (derive_t) cpuinfo[CP_USER], now);
762         cpu_stage (0, COLLECTD_CPU_STATE_NICE,      (derive_t) cpuinfo[CP_NICE], now);
763         cpu_stage (0, COLLECTD_CPU_STATE_SYSTEM,    (derive_t) cpuinfo[CP_SYS], now);
764         cpu_stage (0, COLLECTD_CPU_STATE_IDLE,      (derive_t) cpuinfo[CP_IDLE], now);
765         cpu_stage (0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t) cpuinfo[CP_INTR], now);
766 /* }}} #endif HAVE_SYSCTLBYNAME */
767
768 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
769         sg_cpu_stats *cs;
770         cs = sg_get_cpu_stats ();
771
772         if (cs == NULL)
773         {
774                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
775                 return (-1);
776         }
777
778         cpu_state (0, COLLECTD_CPU_STATE_IDLE,   (derive_t) cs->idle);
779         cpu_state (0, COLLECTD_CPU_STATE_NICE,   (derive_t) cs->nice);
780         cpu_state (0, COLLECTD_CPU_STATE_SWAP,   (derive_t) cs->swap);
781         cpu_state (0, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cs->kernel);
782         cpu_state (0, COLLECTD_CPU_STATE_USER,   (derive_t) cs->user);
783         cpu_state (0, COLLECTD_CPU_STATE_WAIT,   (derive_t) cs->iowait);
784 /* }}} #endif HAVE_LIBSTATGRAB */
785
786 #elif defined(HAVE_PERFSTAT) /* {{{ */
787         perfstat_id_t id;
788         int cpus;
789
790         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
791         if(numcpu == -1)
792         {
793                 char errbuf[1024];
794                 WARNING ("cpu plugin: perfstat_cpu: %s",
795                         sstrerror (errno, errbuf, sizeof (errbuf)));
796                 return (-1);
797         }
798
799         if (pnumcpu != numcpu || perfcpu == NULL)
800         {
801                 free(perfcpu);
802                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
803         }
804         pnumcpu = numcpu;
805
806         id.name[0] = '\0';
807         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
808         {
809                 char errbuf[1024];
810                 WARNING ("cpu plugin: perfstat_cpu: %s",
811                         sstrerror (errno, errbuf, sizeof (errbuf)));
812                 return (-1);
813         }
814
815         for (int i = 0; i < cpus; i++)
816         {
817                 cpu_stage (i, COLLECTD_CPU_STATE_IDLE,   (derive_t) perfcpu[i].idle, now);
818                 cpu_stage (i, COLLECTD_CPU_STATE_SYSTEM, (derive_t) perfcpu[i].sys,  now);
819                 cpu_stage (i, COLLECTD_CPU_STATE_USER,   (derive_t) perfcpu[i].user, now);
820                 cpu_stage (i, COLLECTD_CPU_STATE_WAIT,   (derive_t) perfcpu[i].wait, now);
821         }
822 #endif /* }}} HAVE_PERFSTAT */
823
824         cpu_commit ();
825         cpu_reset ();
826         return (0);
827 }
828
829 void module_register (void)
830 {
831         plugin_register_init ("cpu", init);
832         plugin_register_config ("cpu", cpu_config, config_keys, config_keys_num);
833         plugin_register_read ("cpu", cpu_read);
834 } /* void module_register */
835
836 /* vim: set sw=8 sts=8 noet fdm=marker : */