Merge branch 'collectd-5.4'
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2010  Florian octo Forster
4  * Copyright (C) 2008       Oleg King
5  * Copyright (C) 2009       Simon Kuhnle
6  * Copyright (C) 2009       Manuel Sanmartin
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at verplant.org>
23  *   Oleg King <king2 at kaluga.ru>
24  *   Simon Kuhnle <simon at blarzwurst.de>
25  *   Manuel Sanmartin
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
34 #endif
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
37 #endif
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
40 #endif
41 #if HAVE_MACH_MACH_ERROR_H
42 #  include <mach/mach_error.h>
43 #endif
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
49 #endif
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
52 #endif
53
54 #ifdef HAVE_LIBKSTAT
55 # include <sys/sysinfo.h>
56 #endif /* HAVE_LIBKSTAT */
57
58 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
59         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
60 # ifdef HAVE_SYS_SYSCTL_H
61 #  include <sys/sysctl.h>
62 # endif
63
64 # ifdef HAVE_SYS_DKSTAT_H
65 #  include <sys/dkstat.h>
66 # endif
67
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
69 #  define CP_USER   0
70 #  define CP_NICE   1
71 #  define CP_SYS    2
72 #  define CP_INTR   3
73 #  define CP_IDLE   4
74 #  define CPUSTATES 5
75 # endif
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
77
78 #if HAVE_SYSCTL
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 #  define CAN_USE_SYSCTL 1
82 # else
83 #  define CAN_USE_SYSCTL 0
84 # endif
85 #else
86 # define CAN_USE_SYSCTL 0
87 #endif
88
89 #if HAVE_STATGRAB_H
90 # include <statgrab.h>
91 #endif
92
93 # ifdef HAVE_PERFSTAT
94 #  include <sys/protosw.h>
95 #  include <libperfstat.h>
96 # endif /* HAVE_PERFSTAT */
97
98 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
99         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
100 # error "No applicable input method."
101 #endif
102
103 #ifdef PROCESSOR_CPU_LOAD_INFO
104 static mach_port_t port_host;
105 static processor_port_array_t cpu_list;
106 static mach_msg_type_number_t cpu_list_len;
107
108 #if PROCESSOR_TEMPERATURE
109 static int cpu_temp_retry_counter = 0;
110 static int cpu_temp_retry_step    = 1;
111 static int cpu_temp_retry_max     = 1;
112 #endif /* PROCESSOR_TEMPERATURE */
113 /* #endif PROCESSOR_CPU_LOAD_INFO */
114
115 #elif defined(KERNEL_LINUX)
116 /* no variables needed */
117 /* #endif KERNEL_LINUX */
118
119 #elif defined(HAVE_LIBKSTAT)
120 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
121 # define MAX_NUMCPU 256
122 extern kstat_ctl_t *kc;
123 static kstat_t *ksp[MAX_NUMCPU];
124 static int numcpu;
125 /* #endif HAVE_LIBKSTAT */
126
127 #elif CAN_USE_SYSCTL
128 static int numcpu;
129 /* #endif CAN_USE_SYSCTL */
130
131 #elif defined(HAVE_SYSCTLBYNAME)
132 static int numcpu;
133 #  ifdef HAVE_SYSCTL_KERN_CP_TIMES
134 static int maxcpu;
135 #  endif /* HAVE_SYSCTL_KERN_CP_TIMES */
136 /* #endif HAVE_SYSCTLBYNAME */
137
138 #elif defined(HAVE_LIBSTATGRAB)
139 /* no variables needed */
140 /* #endif  HAVE_LIBSTATGRAB */
141
142 #elif defined(HAVE_PERFSTAT)
143 static perfstat_cpu_t *perfcpu;
144 static int numcpu;
145 static int pnumcpu;
146 #endif /* HAVE_PERFSTAT */
147
148 static int init (void)
149 {
150 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
151         kern_return_t status;
152
153         port_host = mach_host_self ();
154
155         /* FIXME: Free `cpu_list' if it's not NULL */
156         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
157         {
158                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
159                 cpu_list_len = 0;
160                 return (-1);
161         }
162
163         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
164         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
165
166         cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
167 /* #endif PROCESSOR_CPU_LOAD_INFO */
168
169 #elif defined(HAVE_LIBKSTAT)
170         kstat_t *ksp_chain;
171
172         numcpu = 0;
173
174         if (kc == NULL)
175                 return (-1);
176
177         /* Solaris doesn't count linear.. *sigh* */
178         for (numcpu = 0, ksp_chain = kc->kc_chain;
179                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
180                         ksp_chain = ksp_chain->ks_next)
181                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
182                         ksp[numcpu++] = ksp_chain;
183 /* #endif HAVE_LIBKSTAT */
184
185 #elif CAN_USE_SYSCTL
186         size_t numcpu_size;
187         int mib[2] = {CTL_HW, HW_NCPU};
188         int status;
189
190         numcpu = 0;
191         numcpu_size = sizeof (numcpu);
192
193         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
194                         &numcpu, &numcpu_size, NULL, 0);
195         if (status == -1)
196         {
197                 char errbuf[1024];
198                 WARNING ("cpu plugin: sysctl: %s",
199                                 sstrerror (errno, errbuf, sizeof (errbuf)));
200                 return (-1);
201         }
202 /* #endif CAN_USE_SYSCTL */
203
204 #elif defined (HAVE_SYSCTLBYNAME)
205         size_t numcpu_size;
206
207         numcpu_size = sizeof (numcpu);
208
209         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
210         {
211                 char errbuf[1024];
212                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
213                                 sstrerror (errno, errbuf, sizeof (errbuf)));
214                 return (-1);
215         }
216
217 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
218         numcpu_size = sizeof (maxcpu);
219
220         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
221         {
222                 char errbuf[1024];
223                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
224                                 sstrerror (errno, errbuf, sizeof (errbuf)));
225                 return (-1);
226         }
227 #else
228         if (numcpu != 1)
229                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
230 #endif
231 /* #endif HAVE_SYSCTLBYNAME */
232
233 #elif defined(HAVE_LIBSTATGRAB)
234         /* nothing to initialize */
235 /* #endif HAVE_LIBSTATGRAB */
236
237 #elif defined(HAVE_PERFSTAT)
238         /* nothing to initialize */
239 #endif /* HAVE_PERFSTAT */
240
241         return (0);
242 } /* int init */
243
244 static void submit (int cpu_num, const char *type_instance, derive_t value)
245 {
246         value_t values[1];
247         value_list_t vl = VALUE_LIST_INIT;
248
249         values[0].derive = value;
250
251         vl.values = values;
252         vl.values_len = 1;
253         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
254         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
255         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
256                         "%i", cpu_num);
257         sstrncpy (vl.type, "cpu", sizeof (vl.type));
258         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
259
260         plugin_dispatch_values (&vl);
261 }
262
263 static int cpu_read (void)
264 {
265 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
266         int cpu;
267
268         kern_return_t status;
269         
270 #if PROCESSOR_CPU_LOAD_INFO
271         derive_t                       cpu_active;
272         processor_cpu_load_info_data_t cpu_info;
273         mach_msg_type_number_t         cpu_info_len;
274 #endif
275 #if PROCESSOR_TEMPERATURE
276         processor_info_data_t          cpu_temp;
277         mach_msg_type_number_t         cpu_temp_len;
278 #endif
279
280         host_t cpu_host;
281
282         for (cpu = 0; cpu < cpu_list_len; cpu++)
283         {
284 #if PROCESSOR_CPU_LOAD_INFO
285                 cpu_host = 0;
286                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
287
288                 if ((status = processor_info (cpu_list[cpu],
289                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
290                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
291                 {
292                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
293                         continue;
294                 }
295
296                 if (cpu_info_len < CPU_STATE_MAX)
297                 {
298                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
299                         continue;
300                 }
301
302                 submit (cpu, "user", (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
303                 submit (cpu, "nice", (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
304                 submit (cpu, "system", (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
305                 submit (cpu, "idle", (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
306                 cpu_active = (derive_t) (cpu_info.cpu_ticks[CPU_STATE_USER] +
307                                          cpu_info.cpu_ticks[CPU_STATE_NICE] +
308                                          cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
309                 submit (cpu, "active", cpu_active);
310                                          
311 #endif /* PROCESSOR_CPU_LOAD_INFO */
312 #if PROCESSOR_TEMPERATURE
313                 /*
314                  * Not all Apple computers do have this ability. To minimize
315                  * the messages sent to the syslog we do an exponential
316                  * stepback if `processor_info' fails. We still try ~once a day
317                  * though..
318                  */
319                 if (cpu_temp_retry_counter > 0)
320                 {
321                         cpu_temp_retry_counter--;
322                         continue;
323                 }
324
325                 cpu_temp_len = PROCESSOR_INFO_MAX;
326
327                 status = processor_info (cpu_list[cpu],
328                                 PROCESSOR_TEMPERATURE,
329                                 &cpu_host,
330                                 cpu_temp, &cpu_temp_len);
331                 if (status != KERN_SUCCESS)
332                 {
333                         ERROR ("cpu plugin: processor_info failed: %s",
334                                         mach_error_string (status));
335
336                         cpu_temp_retry_counter = cpu_temp_retry_step;
337                         cpu_temp_retry_step *= 2;
338                         if (cpu_temp_retry_step > cpu_temp_retry_max)
339                                 cpu_temp_retry_step = cpu_temp_retry_max;
340
341                         continue;
342                 }
343
344                 if (cpu_temp_len != 1)
345                 {
346                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
347                                         (int) cpu_temp_len);
348                         continue;
349                 }
350
351                 cpu_temp_retry_counter = 0;
352                 cpu_temp_retry_step    = 1;
353 #endif /* PROCESSOR_TEMPERATURE */
354         }
355 /* #endif PROCESSOR_CPU_LOAD_INFO */
356
357 #elif defined(KERNEL_LINUX)
358         int cpu;
359         derive_t cpu_active;
360         derive_t user, nice, syst, idle;
361         derive_t wait, intr, sitr; /* sitr == soft interrupt */
362         FILE *fh;
363         char buf[1024];
364
365         char *fields[9];
366         int numfields;
367
368         if ((fh = fopen ("/proc/stat", "r")) == NULL)
369         {
370                 char errbuf[1024];
371                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
372                                 sstrerror (errno, errbuf, sizeof (errbuf)));
373                 return (-1);
374         }
375
376         while (fgets (buf, 1024, fh) != NULL)
377         {
378                 if (strncmp (buf, "cpu", 3))
379                         continue;
380                 if ((buf[3] < '0') || (buf[3] > '9'))
381                         continue;
382
383                 numfields = strsplit (buf, fields, 9);
384                 if (numfields < 5)
385                         continue;
386
387                 cpu = atoi (fields[0] + 3);
388                 user = atoll (fields[1]);
389                 nice = atoll (fields[2]);
390                 syst = atoll (fields[3]);
391                 idle = atoll (fields[4]);
392
393                 submit (cpu, "user", user);
394                 submit (cpu, "nice", nice);
395                 submit (cpu, "system", syst);
396                 submit (cpu, "idle", idle);
397                 cpu_active = user + nice + syst;
398
399                 if (numfields >= 8)
400                 {
401                         wait = atoll (fields[5]);
402                         intr = atoll (fields[6]);
403                         sitr = atoll (fields[7]);
404
405                         submit (cpu, "wait", wait);
406                         submit (cpu, "interrupt", intr);
407                         submit (cpu, "softirq", sitr);
408                         
409                         cpu_active += wait + intr + sitr;
410
411                         if (numfields >= 9)
412                                 cpu_active += (derive_t) atoll (fields[8]);
413                                 submit (cpu, "steal", atoll (fields[8]));
414                 }
415                 submit (cpu, "active", cpu_active);
416         }
417
418         fclose (fh);
419 /* #endif defined(KERNEL_LINUX) */
420
421 #elif defined(HAVE_LIBKSTAT)
422         int cpu;
423         derive_t user, syst, idle, wait;
424         static cpu_stat_t cs;
425
426         if (kc == NULL)
427                 return (-1);
428
429         for (cpu = 0; cpu < numcpu; cpu++)
430         {
431                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
432                         continue; /* error message? */
433
434                 idle = (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
435                 user = (derive_t) cs.cpu_sysinfo.cpu[CPU_USER];
436                 syst = (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
437                 wait = (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
438
439                 submit (ksp[cpu]->ks_instance, "user", user);
440                 submit (ksp[cpu]->ks_instance, "system", syst);
441                 submit (ksp[cpu]->ks_instance, "idle", idle);
442                 submit (ksp[cpu]->ks_instance, "wait", wait);
443                 submit (ksp[cpu]->ks_instance, "active", user + syst + wait);
444         }
445 /* #endif defined(HAVE_LIBKSTAT) */
446
447 #elif CAN_USE_SYSCTL
448         uint64_t cpuinfo[numcpu][CPUSTATES];
449         size_t cpuinfo_size;
450         int status;
451         int i;
452
453         if (numcpu < 1)
454         {
455                 ERROR ("cpu plugin: Could not determine number of "
456                                 "installed CPUs using sysctl(3).");
457                 return (-1);
458         }
459
460         memset (cpuinfo, 0, sizeof (cpuinfo));
461
462 #if defined(KERN_CPTIME2)
463         if (numcpu > 1) {
464                 for (i = 0; i < numcpu; i++) {
465                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
466
467                         cpuinfo_size = sizeof (cpuinfo[0]);
468
469                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
470                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
471                         if (status == -1) {
472                                 char errbuf[1024];
473                                 ERROR ("cpu plugin: sysctl failed: %s.",
474                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
475                                 return (-1);
476                         }
477                 }
478         }
479         else
480 #endif /* defined(KERN_CPTIME2) */
481         {
482                 int mib[] = {CTL_KERN, KERN_CPTIME};
483                 long cpuinfo_tmp[CPUSTATES];
484
485                 cpuinfo_size = sizeof(cpuinfo_tmp);
486
487                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
488                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
489                 if (status == -1)
490                 {
491                         char errbuf[1024];
492                         ERROR ("cpu plugin: sysctl failed: %s.",
493                                         sstrerror (errno, errbuf, sizeof (errbuf)));
494                         return (-1);
495                 }
496
497                 for(i = 0; i < CPUSTATES; i++) {
498                         cpuinfo[0][i] = cpuinfo_tmp[i];
499                 }
500         }
501
502         for (i = 0; i < numcpu; i++) {
503                 submit (i, "user",      cpuinfo[i][CP_USER]);
504                 submit (i, "nice",      cpuinfo[i][CP_NICE]);
505                 submit (i, "system",    cpuinfo[i][CP_SYS]);
506                 submit (i, "idle",      cpuinfo[i][CP_IDLE]);
507                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
508                 submit (i, "active",    cpuinfo[i][CP_USER] +
509                                         cpuinfo[i][CP_NICE] +
510                                         cpuinfo[i][CP_SYS] +
511                                         cpuinfo[i][CP_INTR]);
512         }
513 /* #endif CAN_USE_SYSCTL */
514 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
515         long cpuinfo[maxcpu][CPUSTATES];
516         size_t cpuinfo_size;
517         int i;
518
519         memset (cpuinfo, 0, sizeof (cpuinfo));
520
521         cpuinfo_size = sizeof (cpuinfo);
522         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
523         {
524                 char errbuf[1024];
525                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
526                                 sstrerror (errno, errbuf, sizeof (errbuf)));
527                 return (-1);
528         }
529
530         for (i = 0; i < numcpu; i++) {
531                 submit (i, "user", cpuinfo[i][CP_USER]);
532                 submit (i, "nice", cpuinfo[i][CP_NICE]);
533                 submit (i, "system", cpuinfo[i][CP_SYS]);
534                 submit (i, "idle", cpuinfo[i][CP_IDLE]);
535                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
536                 submit (i, "active", cpuinfo[i][CP_USER] +
537                         cpuinfo[i][CP_NICE] +
538                         cpuinfo[i][CP_SYS] +
539                         cpuinfo[i][CP_INTR]);
540         }
541 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
542 #elif defined(HAVE_SYSCTLBYNAME)
543         long cpuinfo[CPUSTATES];
544         size_t cpuinfo_size;
545
546         cpuinfo_size = sizeof (cpuinfo);
547
548         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
549         {
550                 char errbuf[1024];
551                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
552                                 sstrerror (errno, errbuf, sizeof (errbuf)));
553                 return (-1);
554         }
555
556         submit (0, "user", cpuinfo[CP_USER]);
557         submit (0, "nice", cpuinfo[CP_NICE]);
558         submit (0, "system", cpuinfo[CP_SYS]);
559         submit (0, "idle", cpuinfo[CP_IDLE]);
560         submit (0, "interrupt", cpuinfo[CP_INTR]);
561         submit (0, "active", cpuinfo[CP_USER] +
562                 cpuinfo[CP_NICE] +
563                 cpuinfo[CP_SYS] +
564                 cpuinfo[CP_INTR]);
565 /* #endif HAVE_SYSCTLBYNAME */
566
567 #elif defined(HAVE_LIBSTATGRAB)
568         sg_cpu_stats *cs;
569         cs = sg_get_cpu_stats ();
570
571         if (cs == NULL)
572         {
573                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
574                 return (-1);
575         }
576
577         submit (0, "idle",   (derive_t) cs->idle);
578         submit (0, "nice",   (derive_t) cs->nice);
579         submit (0, "swap",   (derive_t) cs->swap);
580         submit (0, "system", (derive_t) cs->kernel);
581         submit (0, "user",   (derive_t) cs->user);
582         submit (0, "wait",   (derive_t) cs->iowait);
583         submit (0, "active", (derive_t) cs->nice + 
584                 cs->swap +
585                 cs->kernel +
586                 cs->user +
587                 cs->iowait +
588                 cs->nice);
589 /* #endif HAVE_LIBSTATGRAB */
590
591 #elif defined(HAVE_PERFSTAT)
592         perfstat_id_t id;
593         int i, cpus;
594
595         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
596         if(numcpu == -1)
597         {
598                 char errbuf[1024];
599                 WARNING ("cpu plugin: perfstat_cpu: %s",
600                         sstrerror (errno, errbuf, sizeof (errbuf)));
601                 return (-1);
602         }
603         
604         if (pnumcpu != numcpu || perfcpu == NULL) 
605         {
606                 if (perfcpu != NULL) 
607                         free(perfcpu);
608                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
609         }
610         pnumcpu = numcpu;
611
612         id.name[0] = '\0';
613         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
614         {
615                 char errbuf[1024];
616                 WARNING ("cpu plugin: perfstat_cpu: %s",
617                         sstrerror (errno, errbuf, sizeof (errbuf)));
618                 return (-1);
619         }
620
621         for (i = 0; i < cpus; i++) 
622         {
623                 submit (i, "idle",   (derive_t) perfcpu[i].idle);
624                 submit (i, "system", (derive_t) perfcpu[i].sys);
625                 submit (i, "user",   (derive_t) perfcpu[i].user);
626                 submit (i, "wait",   (derive_t) perfcpu[i].wait);
627                 submit (i, "active", (derive_t) perfcpu[i].sys +
628                         perfcpu[i].user +
629                         perfcpu[i].wait);
630         }
631 #endif /* HAVE_PERFSTAT */
632
633         return (0);
634 }
635
636 void module_register (void)
637 {
638         plugin_register_init ("cpu", init);
639         plugin_register_read ("cpu", cpu_read);
640 } /* void module_register */