3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
6 * Copyright (C) 2009 Manuel Sanmartin
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.
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.
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
22 * Florian octo Forster <octo at verplant.org>
23 * Oleg King <king2 at kaluga.ru>
24 * Simon Kuhnle <simon at blarzwurst.de>
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
41 #if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
55 # include <sys/sysinfo.h>
56 #endif /* HAVE_LIBKSTAT */
58 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
59 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
60 # ifdef HAVE_SYS_SYSCTL_H
61 # include <sys/sysctl.h>
64 # ifdef HAVE_SYS_DKSTAT_H
65 # include <sys/dkstat.h>
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 # define CAN_USE_SYSCTL 1
83 # define CAN_USE_SYSCTL 0
86 # define CAN_USE_SYSCTL 0
90 # include <statgrab.h>
94 # include <sys/protosw.h>
95 # include <libperfstat.h>
96 # endif /* HAVE_PERFSTAT */
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."
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;
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 */
115 #elif defined(KERNEL_LINUX)
116 /* no variables needed */
117 /* #endif KERNEL_LINUX */
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];
125 /* #endif HAVE_LIBKSTAT */
129 /* #endif CAN_USE_SYSCTL */
131 #elif defined(HAVE_SYSCTLBYNAME)
133 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
135 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
136 /* #endif HAVE_SYSCTLBYNAME */
138 #elif defined(HAVE_LIBSTATGRAB)
139 /* no variables needed */
140 /* #endif HAVE_LIBSTATGRAB */
142 #elif defined(HAVE_PERFSTAT)
143 static perfstat_cpu_t *perfcpu;
146 #endif /* HAVE_PERFSTAT */
148 static int init (void)
150 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
151 kern_return_t status;
153 port_host = mach_host_self ();
155 /* FIXME: Free `cpu_list' if it's not NULL */
156 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
158 ERROR ("cpu plugin: host_processors returned %i", (int) status);
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");
166 cpu_temp_retry_max = 86400 / interval_g;
167 /* #endif PROCESSOR_CPU_LOAD_INFO */
169 #elif defined(HAVE_LIBKSTAT)
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 */
187 int mib[2] = {CTL_HW, HW_NCPU};
191 numcpu_size = sizeof (numcpu);
193 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
194 &numcpu, &numcpu_size, NULL, 0);
198 WARNING ("cpu plugin: sysctl: %s",
199 sstrerror (errno, errbuf, sizeof (errbuf)));
202 /* #endif CAN_USE_SYSCTL */
204 #elif defined (HAVE_SYSCTLBYNAME)
207 numcpu_size = sizeof (numcpu);
209 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
212 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
217 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
218 numcpu_size = sizeof (maxcpu);
220 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
223 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
224 sstrerror (errno, errbuf, sizeof (errbuf)));
229 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
231 /* #endif HAVE_SYSCTLBYNAME */
233 #elif defined(HAVE_LIBSTATGRAB)
234 /* nothing to initialize */
235 /* #endif HAVE_LIBSTATGRAB */
237 #elif defined(HAVE_PERFSTAT)
238 /* nothing to initialize */
239 #endif /* HAVE_PERFSTAT */
244 static void submit (int cpu_num, const char *type_instance, counter_t value)
247 value_list_t vl = VALUE_LIST_INIT;
249 values[0].counter = value;
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),
257 sstrncpy (vl.type, "cpu", sizeof (vl.type));
258 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
260 plugin_dispatch_values (&vl);
263 static int cpu_read (void)
265 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
268 kern_return_t status;
270 #if PROCESSOR_CPU_LOAD_INFO
271 processor_cpu_load_info_data_t cpu_info;
272 mach_msg_type_number_t cpu_info_len;
274 #if PROCESSOR_TEMPERATURE
275 processor_info_data_t cpu_temp;
276 mach_msg_type_number_t cpu_temp_len;
281 for (cpu = 0; cpu < cpu_list_len; cpu++)
283 #if PROCESSOR_CPU_LOAD_INFO
285 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
287 if ((status = processor_info (cpu_list[cpu],
288 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
289 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
291 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
295 if (cpu_info_len < CPU_STATE_MAX)
297 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
301 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
302 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
303 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
304 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
305 #endif /* PROCESSOR_CPU_LOAD_INFO */
306 #if PROCESSOR_TEMPERATURE
308 * Not all Apple computers do have this ability. To minimize
309 * the messages sent to the syslog we do an exponential
310 * stepback if `processor_info' fails. We still try ~once a day
313 if (cpu_temp_retry_counter > 0)
315 cpu_temp_retry_counter--;
319 cpu_temp_len = PROCESSOR_INFO_MAX;
321 status = processor_info (cpu_list[cpu],
322 PROCESSOR_TEMPERATURE,
324 cpu_temp, &cpu_temp_len);
325 if (status != KERN_SUCCESS)
327 ERROR ("cpu plugin: processor_info failed: %s",
328 mach_error_string (status));
330 cpu_temp_retry_counter = cpu_temp_retry_step;
331 cpu_temp_retry_step *= 2;
332 if (cpu_temp_retry_step > cpu_temp_retry_max)
333 cpu_temp_retry_step = cpu_temp_retry_max;
338 if (cpu_temp_len != 1)
340 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
345 cpu_temp_retry_counter = 0;
346 cpu_temp_retry_step = 1;
348 DEBUG ("cpu_temp = %i", (int) cpu_temp);
349 #endif /* PROCESSOR_TEMPERATURE */
351 /* #endif PROCESSOR_CPU_LOAD_INFO */
353 #elif defined(KERNEL_LINUX)
355 counter_t user, nice, syst, idle;
356 counter_t wait, intr, sitr; /* sitr == soft interrupt */
363 if ((fh = fopen ("/proc/stat", "r")) == NULL)
366 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
367 sstrerror (errno, errbuf, sizeof (errbuf)));
371 while (fgets (buf, 1024, fh) != NULL)
373 if (strncmp (buf, "cpu", 3))
375 if ((buf[3] < '0') || (buf[3] > '9'))
378 numfields = strsplit (buf, fields, 9);
382 cpu = atoi (fields[0] + 3);
383 user = atoll (fields[1]);
384 nice = atoll (fields[2]);
385 syst = atoll (fields[3]);
386 idle = atoll (fields[4]);
388 submit (cpu, "user", user);
389 submit (cpu, "nice", nice);
390 submit (cpu, "system", syst);
391 submit (cpu, "idle", idle);
395 wait = atoll (fields[5]);
396 intr = atoll (fields[6]);
397 sitr = atoll (fields[7]);
399 submit (cpu, "wait", wait);
400 submit (cpu, "interrupt", intr);
401 submit (cpu, "softirq", sitr);
404 submit (cpu, "steal", atoll (fields[8]));
409 /* #endif defined(KERNEL_LINUX) */
411 #elif defined(HAVE_LIBKSTAT)
413 counter_t user, syst, idle, wait;
414 static cpu_stat_t cs;
419 for (cpu = 0; cpu < numcpu; cpu++)
421 if (kstat_read (kc, ksp[cpu], &cs) == -1)
422 continue; /* error message? */
424 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
425 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
426 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
427 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
429 submit (ksp[cpu]->ks_instance, "user", user);
430 submit (ksp[cpu]->ks_instance, "system", syst);
431 submit (ksp[cpu]->ks_instance, "idle", idle);
432 submit (ksp[cpu]->ks_instance, "wait", wait);
434 /* #endif defined(HAVE_LIBKSTAT) */
437 uint64_t cpuinfo[numcpu][CPUSTATES];
444 ERROR ("cpu plugin: Could not determine number of "
445 "installed CPUs using sysctl(3).");
449 memset (cpuinfo, 0, sizeof (cpuinfo));
451 #if defined(KERN_CPTIME2)
453 for (i = 0; i < numcpu; i++) {
454 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
456 cpuinfo_size = sizeof (cpuinfo[0]);
458 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
459 cpuinfo[i], &cpuinfo_size, NULL, 0);
462 ERROR ("cpu plugin: sysctl failed: %s.",
463 sstrerror (errno, errbuf, sizeof (errbuf)));
469 #endif /* defined(KERN_CPTIME2) */
471 int mib[] = {CTL_KERN, KERN_CPTIME};
472 long cpuinfo_tmp[CPUSTATES];
474 cpuinfo_size = sizeof(cpuinfo_tmp);
476 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
477 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
481 ERROR ("cpu plugin: sysctl failed: %s.",
482 sstrerror (errno, errbuf, sizeof (errbuf)));
486 for(i = 0; i < CPUSTATES; i++) {
487 cpuinfo[0][i] = cpuinfo_tmp[i];
491 for (i = 0; i < numcpu; i++) {
492 submit (i, "user", cpuinfo[i][CP_USER]);
493 submit (i, "nice", cpuinfo[i][CP_NICE]);
494 submit (i, "system", cpuinfo[i][CP_SYS]);
495 submit (i, "idle", cpuinfo[i][CP_IDLE]);
496 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
498 /* #endif CAN_USE_SYSCTL */
499 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
500 long cpuinfo[maxcpu][CPUSTATES];
504 memset (cpuinfo, 0, sizeof (cpuinfo));
506 cpuinfo_size = sizeof (cpuinfo);
507 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
510 ERROR ("cpu plugin: sysctlbyname failed: %s.",
511 sstrerror (errno, errbuf, sizeof (errbuf)));
515 for (i = 0; i < numcpu; i++) {
516 submit (i, "user", cpuinfo[i][CP_USER]);
517 submit (i, "nice", cpuinfo[i][CP_NICE]);
518 submit (i, "system", cpuinfo[i][CP_SYS]);
519 submit (i, "idle", cpuinfo[i][CP_IDLE]);
520 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
522 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
523 #elif defined(HAVE_SYSCTLBYNAME)
524 long cpuinfo[CPUSTATES];
527 cpuinfo_size = sizeof (cpuinfo);
529 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
532 ERROR ("cpu plugin: sysctlbyname failed: %s.",
533 sstrerror (errno, errbuf, sizeof (errbuf)));
537 submit (0, "user", cpuinfo[CP_USER]);
538 submit (0, "nice", cpuinfo[CP_NICE]);
539 submit (0, "system", cpuinfo[CP_SYS]);
540 submit (0, "idle", cpuinfo[CP_IDLE]);
541 submit (0, "interrupt", cpuinfo[CP_INTR]);
542 /* #endif HAVE_SYSCTLBYNAME */
544 #elif defined(HAVE_LIBSTATGRAB)
546 cs = sg_get_cpu_stats ();
550 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
554 submit (0, "idle", (counter_t) cs->idle);
555 submit (0, "nice", (counter_t) cs->nice);
556 submit (0, "swap", (counter_t) cs->swap);
557 submit (0, "system", (counter_t) cs->kernel);
558 submit (0, "user", (counter_t) cs->user);
559 submit (0, "wait", (counter_t) cs->iowait);
560 /* #endif HAVE_LIBSTATGRAB */
562 #elif defined(HAVE_PERFSTAT)
566 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
570 WARNING ("cpu plugin: perfstat_cpu: %s",
571 sstrerror (errno, errbuf, sizeof (errbuf)));
575 if (pnumcpu != numcpu || perfcpu == NULL)
579 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
584 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
587 WARNING ("cpu plugin: perfstat_cpu: %s",
588 sstrerror (errno, errbuf, sizeof (errbuf)));
592 for (i = 0; i < cpus; i++)
594 submit (i, "idle", (counter_t) perfcpu[i].idle);
595 submit (i, "system", (counter_t) perfcpu[i].sys);
596 submit (i, "user", (counter_t) perfcpu[i].user);
597 submit (i, "wait", (counter_t) perfcpu[i].wait);
599 #endif /* HAVE_PERFSTAT */
604 void module_register (void)
606 plugin_register_init ("cpu", init);
607 plugin_register_read ("cpu", cpu_read);
608 } /* void module_register */