3 * Copyright (C) 2005-2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #ifdef HAVE_MACH_KERN_RETURN_H
27 # include <mach/kern_return.h>
29 #ifdef HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
32 #ifdef HAVE_MACH_HOST_PRIV_H
33 # include <mach/host_priv.h>
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
38 #ifdef HAVE_MACH_PROCESSOR_INFO_H
39 # include <mach/processor_info.h>
41 #ifdef HAVE_MACH_PROCESSOR_H
42 # include <mach/processor.h>
44 #ifdef HAVE_MACH_VM_MAP_H
45 # include <mach/vm_map.h>
49 # include <sys/sysinfo.h>
50 #endif /* HAVE_LIBKSTAT */
52 #ifdef HAVE_SYSCTLBYNAME
53 # ifdef HAVE_SYS_SYSCTL_H
54 # include <sys/sysctl.h>
57 # ifdef HAVE_SYS_DKSTAT_H
58 # include <sys/dkstat.h>
61 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
69 #endif /* HAVE_SYSCTLBYNAME */
72 # ifdef HAVE_SYS_SYSCTL_H
73 # include <sys/sysctl.h>
76 # ifdef HAVE_SYS_DKSTAT_H
77 # include <sys/dkstat.h>
79 #endif /* __OpenBSD__ */
82 # include <statgrab.h>
85 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
86 && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !__OpenBSD__
87 # error "No applicable input method."
90 #ifdef PROCESSOR_CPU_LOAD_INFO
91 static mach_port_t port_host;
92 static processor_port_array_t cpu_list;
93 static mach_msg_type_number_t cpu_list_len;
95 #if PROCESSOR_TEMPERATURE
96 static int cpu_temp_retry_counter = 0;
97 static int cpu_temp_retry_step = 1;
98 static int cpu_temp_retry_max = 1;
99 #endif /* PROCESSOR_TEMPERATURE */
100 /* #endif PROCESSOR_CPU_LOAD_INFO */
102 #elif defined(KERNEL_LINUX)
103 /* no variables needed */
104 /* #endif KERNEL_LINUX */
106 #elif defined(HAVE_LIBKSTAT)
107 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
108 # define MAX_NUMCPU 256
109 extern kstat_ctl_t *kc;
110 static kstat_t *ksp[MAX_NUMCPU];
112 /* #endif HAVE_LIBKSTAT */
114 #elif defined(HAVE_SYSCTLBYNAME) || __OpenBSD__
116 /* #endif HAVE_SYSCTLBYNAME */
118 #elif defined(HAVE_LIBSTATGRAB)
119 /* no variables needed */
120 #endif /* HAVE_LIBSTATGRAB */
122 static int init (void)
124 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
125 kern_return_t status;
127 port_host = mach_host_self ();
129 /* FIXME: Free `cpu_list' if it's not NULL */
130 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
132 ERROR ("cpu plugin: host_processors returned %i", (int) status);
137 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
138 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
140 cpu_temp_retry_max = 86400 / interval_g;
141 /* #endif PROCESSOR_CPU_LOAD_INFO */
143 #elif defined(HAVE_LIBKSTAT)
151 /* Solaris doesn't count linear.. *sigh* */
152 for (numcpu = 0, ksp_chain = kc->kc_chain;
153 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
154 ksp_chain = ksp_chain->ks_next)
155 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
156 ksp[numcpu++] = ksp_chain;
157 /* #endif HAVE_LIBKSTAT */
159 #elif defined (HAVE_SYSCTLBYNAME)
162 numcpu_size = sizeof (numcpu);
164 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
167 WARNING ("cpu plugin: sysctlbyname: %s",
168 sstrerror (errno, errbuf, sizeof (errbuf)));
173 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
174 /* #endif HAVE_SYSCTLBYNAME */
176 #elif defined __OpenBSD__
178 int mib[2] = {CTL_HW, HW_NCPU};
180 numcpu_size = sizeof (numcpu);
182 if (sysctl (mib, 2, &numcpu, &numcpu_size, NULL, 0) < 0)
185 WARNING ("cpu plugin: sysctl: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
189 /* #endif __OpenBSD__ */
191 #elif defined(HAVE_LIBSTATGRAB)
192 /* nothing to initialize */
193 #endif /* HAVE_LIBSTATGRAB */
198 static void submit (int cpu_num, const char *type_instance, counter_t value)
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].counter = value;
207 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
208 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
209 ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
211 sstrncpy (vl.type, "cpu", sizeof (vl.type));
212 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
214 plugin_dispatch_values (&vl);
217 static int cpu_read (void)
219 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
222 kern_return_t status;
224 #if PROCESSOR_CPU_LOAD_INFO
225 processor_cpu_load_info_data_t cpu_info;
226 mach_msg_type_number_t cpu_info_len;
228 #if PROCESSOR_TEMPERATURE
229 processor_info_data_t cpu_temp;
230 mach_msg_type_number_t cpu_temp_len;
235 for (cpu = 0; cpu < cpu_list_len; cpu++)
237 #if PROCESSOR_CPU_LOAD_INFO
239 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
241 if ((status = processor_info (cpu_list[cpu],
242 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
243 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
245 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
249 if (cpu_info_len < CPU_STATE_MAX)
251 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
255 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
256 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
257 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
258 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
259 #endif /* PROCESSOR_CPU_LOAD_INFO */
260 #if PROCESSOR_TEMPERATURE
262 * Not all Apple computers do have this ability. To minimize
263 * the messages sent to the syslog we do an exponential
264 * stepback if `processor_info' fails. We still try ~once a day
267 if (cpu_temp_retry_counter > 0)
269 cpu_temp_retry_counter--;
273 cpu_temp_len = PROCESSOR_INFO_MAX;
275 status = processor_info (cpu_list[cpu],
276 PROCESSOR_TEMPERATURE,
278 cpu_temp, &cpu_temp_len);
279 if (status != KERN_SUCCESS)
281 ERROR ("cpu plugin: processor_info failed: %s",
282 mach_error_string (status));
284 cpu_temp_retry_counter = cpu_temp_retry_step;
285 cpu_temp_retry_step *= 2;
286 if (cpu_temp_retry_step > cpu_temp_retry_max)
287 cpu_temp_retry_step = cpu_temp_retry_max;
292 if (cpu_temp_len != 1)
294 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
299 cpu_temp_retry_counter = 0;
300 cpu_temp_retry_step = 1;
302 DEBUG ("cpu_temp = %i", (int) cpu_temp);
303 #endif /* PROCESSOR_TEMPERATURE */
305 /* #endif PROCESSOR_CPU_LOAD_INFO */
307 #elif defined(KERNEL_LINUX)
309 counter_t user, nice, syst, idle;
310 counter_t wait, intr, sitr; /* sitr == soft interrupt */
317 if ((fh = fopen ("/proc/stat", "r")) == NULL)
320 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
321 sstrerror (errno, errbuf, sizeof (errbuf)));
325 while (fgets (buf, 1024, fh) != NULL)
327 if (strncmp (buf, "cpu", 3))
329 if ((buf[3] < '0') || (buf[3] > '9'))
332 numfields = strsplit (buf, fields, 9);
336 cpu = atoi (fields[0] + 3);
337 user = atoll (fields[1]);
338 nice = atoll (fields[2]);
339 syst = atoll (fields[3]);
340 idle = atoll (fields[4]);
342 submit (cpu, "user", user);
343 submit (cpu, "nice", nice);
344 submit (cpu, "system", syst);
345 submit (cpu, "idle", idle);
349 wait = atoll (fields[5]);
350 intr = atoll (fields[6]);
351 sitr = atoll (fields[7]);
353 submit (cpu, "wait", wait);
354 submit (cpu, "interrupt", intr);
355 submit (cpu, "softirq", sitr);
358 submit (cpu, "steal", atoll (fields[8]));
363 /* #endif defined(KERNEL_LINUX) */
365 #elif defined(HAVE_LIBKSTAT)
367 counter_t user, syst, idle, wait;
368 static cpu_stat_t cs;
373 for (cpu = 0; cpu < numcpu; cpu++)
375 if (kstat_read (kc, ksp[cpu], &cs) == -1)
376 continue; /* error message? */
378 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
379 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
380 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
381 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
383 submit (ksp[cpu]->ks_instance, "user", user);
384 submit (ksp[cpu]->ks_instance, "system", syst);
385 submit (ksp[cpu]->ks_instance, "idle", idle);
386 submit (ksp[cpu]->ks_instance, "wait", wait);
388 /* #endif defined(HAVE_LIBKSTAT) */
390 #elif defined(HAVE_SYSCTLBYNAME)
391 long cpuinfo[CPUSTATES];
394 cpuinfo_size = sizeof (cpuinfo);
396 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
399 ERROR ("cpu plugin: sysctlbyname failed: %s.",
400 sstrerror (errno, errbuf, sizeof (errbuf)));
404 submit (0, "user", cpuinfo[CP_USER]);
405 submit (0, "nice", cpuinfo[CP_NICE]);
406 submit (0, "system", cpuinfo[CP_SYS]);
407 submit (0, "idle", cpuinfo[CP_IDLE]);
408 submit (0, "interrupt", cpuinfo[CP_INTR]);
409 /* #endif HAVE_SYSCTLBYNAME */
411 #elif defined __OpenBSD__
416 cpuinfo = (int64_t **) calloc (numcpu, sizeof(int64_t *));
417 if(cpuinfo == NULL) {
418 ERROR ("cpu plugin: calloc failed.");
421 for (i = 0; i < numcpu; i++) {
422 cpuinfo[i] = (int64_t *) calloc (CPUSTATES, sizeof(int64_t));
423 if (cpuinfo[i] == NULL) {
424 ERROR ("cpu plugin: calloc failed.");
430 cpuinfo_size = CPUSTATES * sizeof(int64_t);
432 for (i = 0; i < numcpu; i++) {
433 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
435 if (sysctl(mib, 3, cpuinfo[i], &cpuinfo_size, NULL, 0) < 0) {
437 ERROR ("cpu plugin: sysctl failed: %s.",
438 sstrerror (errno, errbuf, sizeof (errbuf)));
443 int mib[] = {CTL_KERN, KERN_CPTIME};
444 long cpuinfo_tmp[CPUSTATES];
446 cpuinfo_size = sizeof(cpuinfo_tmp);
448 if (sysctl(mib, 2, &cpuinfo_tmp, &cpuinfo_size, NULL, 0) < 0)
451 ERROR ("cpu plugin: sysctl failed: %s.",
452 sstrerror (errno, errbuf, sizeof (errbuf)));
455 for(i = 0; i < CPUSTATES; i++) {
456 cpuinfo[0][i] = cpuinfo_tmp[i];
460 for (i = 0; i < numcpu; i++) {
461 cpuinfo[i][CP_SYS] += cpuinfo[i][CP_INTR];
463 submit (i, "user", cpuinfo[i][CP_USER]);
464 submit (i, "nice", cpuinfo[i][CP_NICE]);
465 submit (i, "system", cpuinfo[i][CP_SYS]);
466 submit (i, "idle", cpuinfo[i][CP_IDLE]);
471 /* #endif __OpenBSD__ */
473 #elif defined(HAVE_LIBSTATGRAB)
475 cs = sg_get_cpu_stats ();
479 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
483 submit (0, "idle", (counter_t) cs->idle);
484 submit (0, "nice", (counter_t) cs->nice);
485 submit (0, "swap", (counter_t) cs->swap);
486 submit (0, "system", (counter_t) cs->kernel);
487 submit (0, "user", (counter_t) cs->user);
488 submit (0, "wait", (counter_t) cs->iowait);
489 #endif /* HAVE_LIBSTATGRAB */
494 void module_register (void)
496 plugin_register_init ("cpu", init);
497 plugin_register_read ("cpu", cpu_read);
498 } /* void module_register */