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 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
53 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
54 # ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
58 # ifdef HAVE_SYS_DKSTAT_H
59 # include <sys/dkstat.h>
62 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
70 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
73 # if defined(CTL_HW) && defined(HW_NCPU) \
74 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
75 # define CAN_USE_SYSCTL 1
77 # define CAN_USE_SYSCTL 0
80 # define CAN_USE_SYSCTL 0
84 # include <statgrab.h>
87 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
88 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
89 # error "No applicable input method."
92 #ifdef PROCESSOR_CPU_LOAD_INFO
93 static mach_port_t port_host;
94 static processor_port_array_t cpu_list;
95 static mach_msg_type_number_t cpu_list_len;
97 #if PROCESSOR_TEMPERATURE
98 static int cpu_temp_retry_counter = 0;
99 static int cpu_temp_retry_step = 1;
100 static int cpu_temp_retry_max = 1;
101 #endif /* PROCESSOR_TEMPERATURE */
102 /* #endif PROCESSOR_CPU_LOAD_INFO */
104 #elif defined(KERNEL_LINUX)
105 /* no variables needed */
106 /* #endif KERNEL_LINUX */
108 #elif defined(HAVE_LIBKSTAT)
109 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
110 # define MAX_NUMCPU 256
111 extern kstat_ctl_t *kc;
112 static kstat_t *ksp[MAX_NUMCPU];
114 /* #endif HAVE_LIBKSTAT */
118 /* #endif CAN_USE_SYSCTL */
120 #elif defined(HAVE_SYSCTLBYNAME)
122 /* #endif HAVE_SYSCTLBYNAME */
124 #elif defined(HAVE_LIBSTATGRAB)
125 /* no variables needed */
126 #endif /* HAVE_LIBSTATGRAB */
128 static int init (void)
130 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
131 kern_return_t status;
133 port_host = mach_host_self ();
135 /* FIXME: Free `cpu_list' if it's not NULL */
136 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
138 ERROR ("cpu plugin: host_processors returned %i", (int) status);
143 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
144 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
146 cpu_temp_retry_max = 86400 / interval_g;
147 /* #endif PROCESSOR_CPU_LOAD_INFO */
149 #elif defined(HAVE_LIBKSTAT)
157 /* Solaris doesn't count linear.. *sigh* */
158 for (numcpu = 0, ksp_chain = kc->kc_chain;
159 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
160 ksp_chain = ksp_chain->ks_next)
161 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
162 ksp[numcpu++] = ksp_chain;
163 /* #endif HAVE_LIBKSTAT */
167 int mib[2] = {CTL_HW, HW_NCPU};
171 numcpu_size = sizeof (numcpu);
173 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
174 &numcpu, &numcpu_size, NULL, 0);
178 WARNING ("cpu plugin: sysctl: %s",
179 sstrerror (errno, errbuf, sizeof (errbuf)));
182 /* #endif CAN_USE_SYSCTL */
184 #elif defined (HAVE_SYSCTLBYNAME)
187 numcpu_size = sizeof (numcpu);
189 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
192 WARNING ("cpu plugin: sysctlbyname: %s",
193 sstrerror (errno, errbuf, sizeof (errbuf)));
198 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
199 /* #endif HAVE_SYSCTLBYNAME */
201 #elif defined(HAVE_LIBSTATGRAB)
202 /* nothing to initialize */
203 #endif /* HAVE_LIBSTATGRAB */
208 static void submit (int cpu_num, const char *type_instance, counter_t value)
211 value_list_t vl = VALUE_LIST_INIT;
213 values[0].counter = value;
217 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
218 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
219 ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
221 sstrncpy (vl.type, "cpu", sizeof (vl.type));
222 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
224 plugin_dispatch_values (&vl);
227 static int cpu_read (void)
229 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
232 kern_return_t status;
234 #if PROCESSOR_CPU_LOAD_INFO
235 processor_cpu_load_info_data_t cpu_info;
236 mach_msg_type_number_t cpu_info_len;
238 #if PROCESSOR_TEMPERATURE
239 processor_info_data_t cpu_temp;
240 mach_msg_type_number_t cpu_temp_len;
245 for (cpu = 0; cpu < cpu_list_len; cpu++)
247 #if PROCESSOR_CPU_LOAD_INFO
249 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
251 if ((status = processor_info (cpu_list[cpu],
252 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
253 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
255 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
259 if (cpu_info_len < CPU_STATE_MAX)
261 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
265 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
266 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
267 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
268 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
269 #endif /* PROCESSOR_CPU_LOAD_INFO */
270 #if PROCESSOR_TEMPERATURE
272 * Not all Apple computers do have this ability. To minimize
273 * the messages sent to the syslog we do an exponential
274 * stepback if `processor_info' fails. We still try ~once a day
277 if (cpu_temp_retry_counter > 0)
279 cpu_temp_retry_counter--;
283 cpu_temp_len = PROCESSOR_INFO_MAX;
285 status = processor_info (cpu_list[cpu],
286 PROCESSOR_TEMPERATURE,
288 cpu_temp, &cpu_temp_len);
289 if (status != KERN_SUCCESS)
291 ERROR ("cpu plugin: processor_info failed: %s",
292 mach_error_string (status));
294 cpu_temp_retry_counter = cpu_temp_retry_step;
295 cpu_temp_retry_step *= 2;
296 if (cpu_temp_retry_step > cpu_temp_retry_max)
297 cpu_temp_retry_step = cpu_temp_retry_max;
302 if (cpu_temp_len != 1)
304 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
309 cpu_temp_retry_counter = 0;
310 cpu_temp_retry_step = 1;
312 DEBUG ("cpu_temp = %i", (int) cpu_temp);
313 #endif /* PROCESSOR_TEMPERATURE */
315 /* #endif PROCESSOR_CPU_LOAD_INFO */
317 #elif defined(KERNEL_LINUX)
319 counter_t user, nice, syst, idle;
320 counter_t wait, intr, sitr; /* sitr == soft interrupt */
327 if ((fh = fopen ("/proc/stat", "r")) == NULL)
330 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
331 sstrerror (errno, errbuf, sizeof (errbuf)));
335 while (fgets (buf, 1024, fh) != NULL)
337 if (strncmp (buf, "cpu", 3))
339 if ((buf[3] < '0') || (buf[3] > '9'))
342 numfields = strsplit (buf, fields, 9);
346 cpu = atoi (fields[0] + 3);
347 user = atoll (fields[1]);
348 nice = atoll (fields[2]);
349 syst = atoll (fields[3]);
350 idle = atoll (fields[4]);
352 submit (cpu, "user", user);
353 submit (cpu, "nice", nice);
354 submit (cpu, "system", syst);
355 submit (cpu, "idle", idle);
359 wait = atoll (fields[5]);
360 intr = atoll (fields[6]);
361 sitr = atoll (fields[7]);
363 submit (cpu, "wait", wait);
364 submit (cpu, "interrupt", intr);
365 submit (cpu, "softirq", sitr);
368 submit (cpu, "steal", atoll (fields[8]));
373 /* #endif defined(KERNEL_LINUX) */
375 #elif defined(HAVE_LIBKSTAT)
377 counter_t user, syst, idle, wait;
378 static cpu_stat_t cs;
383 for (cpu = 0; cpu < numcpu; cpu++)
385 if (kstat_read (kc, ksp[cpu], &cs) == -1)
386 continue; /* error message? */
388 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
389 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
390 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
391 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
393 submit (ksp[cpu]->ks_instance, "user", user);
394 submit (ksp[cpu]->ks_instance, "system", syst);
395 submit (ksp[cpu]->ks_instance, "idle", idle);
396 submit (ksp[cpu]->ks_instance, "wait", wait);
398 /* #endif defined(HAVE_LIBKSTAT) */
401 uint64_t cpuinfo[numcpu][CPUSTATES];
408 ERROR ("cpu plugin: Could not determine number of "
409 "installed CPUs using sysctl(3).");
413 memset (cpuinfo, 0, sizeof (cpuinfo));
415 #if defined(KERN_CPTIME2)
417 for (i = 0; i < numcpu; i++) {
418 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
420 cpuinfo_size = sizeof (cpuinfo[0]);
422 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
423 cpuinfo[i], &cpuinfo_size, NULL, 0);
426 ERROR ("cpu plugin: sysctl failed: %s.",
427 sstrerror (errno, errbuf, sizeof (errbuf)));
433 #endif /* defined(KERN_CPTIME2) */
435 int mib[] = {CTL_KERN, KERN_CPTIME};
436 long cpuinfo_tmp[CPUSTATES];
438 cpuinfo_size = sizeof(cpuinfo_tmp);
440 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
441 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
445 ERROR ("cpu plugin: sysctl failed: %s.",
446 sstrerror (errno, errbuf, sizeof (errbuf)));
450 for(i = 0; i < CPUSTATES; i++) {
451 cpuinfo[0][i] = cpuinfo_tmp[i];
455 for (i = 0; i < numcpu; i++) {
456 submit (i, "user", cpuinfo[i][CP_USER]);
457 submit (i, "nice", cpuinfo[i][CP_NICE]);
458 submit (i, "system", cpuinfo[i][CP_SYS]);
459 submit (i, "idle", cpuinfo[i][CP_IDLE]);
460 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
462 /* #endif CAN_USE_SYSCTL */
464 #elif defined(HAVE_SYSCTLBYNAME)
465 long cpuinfo[CPUSTATES];
468 cpuinfo_size = sizeof (cpuinfo);
470 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
473 ERROR ("cpu plugin: sysctlbyname failed: %s.",
474 sstrerror (errno, errbuf, sizeof (errbuf)));
478 submit (0, "user", cpuinfo[CP_USER]);
479 submit (0, "nice", cpuinfo[CP_NICE]);
480 submit (0, "system", cpuinfo[CP_SYS]);
481 submit (0, "idle", cpuinfo[CP_IDLE]);
482 submit (0, "interrupt", cpuinfo[CP_INTR]);
483 /* #endif HAVE_SYSCTLBYNAME */
485 #elif defined(HAVE_LIBSTATGRAB)
487 cs = sg_get_cpu_stats ();
491 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
495 submit (0, "idle", (counter_t) cs->idle);
496 submit (0, "nice", (counter_t) cs->nice);
497 submit (0, "swap", (counter_t) cs->swap);
498 submit (0, "system", (counter_t) cs->kernel);
499 submit (0, "user", (counter_t) cs->user);
500 submit (0, "wait", (counter_t) cs->iowait);
501 #endif /* HAVE_LIBSTATGRAB */
506 void module_register (void)
508 plugin_register_init ("cpu", init);
509 plugin_register_read ("cpu", cpu_read);
510 } /* void module_register */