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 # include <statgrab.h>
75 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
76 && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
77 # error "No applicable input method."
80 #ifdef PROCESSOR_CPU_LOAD_INFO
81 static mach_port_t port_host;
82 static processor_port_array_t cpu_list;
83 static mach_msg_type_number_t cpu_list_len;
85 #if PROCESSOR_TEMPERATURE
86 static int cpu_temp_retry_counter = 0;
87 static int cpu_temp_retry_step = 1;
88 static int cpu_temp_retry_max = 1;
89 #endif /* PROCESSOR_TEMPERATURE */
90 /* #endif PROCESSOR_CPU_LOAD_INFO */
92 #elif defined(KERNEL_LINUX)
93 /* no variables needed */
94 /* #endif KERNEL_LINUX */
96 #elif defined(HAVE_LIBKSTAT)
97 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
98 # define MAX_NUMCPU 256
99 extern kstat_ctl_t *kc;
100 static kstat_t *ksp[MAX_NUMCPU];
102 /* #endif HAVE_LIBKSTAT */
104 #elif defined(HAVE_SYSCTLBYNAME)
106 /* #endif HAVE_SYSCTLBYNAME */
108 #elif defined(HAVE_LIBSTATGRAB)
109 /* no variables needed */
110 #endif /* HAVE_LIBSTATGRAB */
112 static int init (void)
114 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
115 kern_return_t status;
117 port_host = mach_host_self ();
119 /* FIXME: Free `cpu_list' if it's not NULL */
120 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
122 ERROR ("cpu plugin: host_processors returned %i", (int) status);
127 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
128 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
130 cpu_temp_retry_max = 86400 / interval_g;
131 /* #endif PROCESSOR_CPU_LOAD_INFO */
133 #elif defined(HAVE_LIBKSTAT)
141 /* Solaris doesn't count linear.. *sigh* */
142 for (numcpu = 0, ksp_chain = kc->kc_chain;
143 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
144 ksp_chain = ksp_chain->ks_next)
145 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
146 ksp[numcpu++] = ksp_chain;
147 /* #endif HAVE_LIBKSTAT */
149 #elif defined (HAVE_SYSCTLBYNAME)
152 numcpu_size = sizeof (numcpu);
154 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
157 WARNING ("cpu plugin: sysctlbyname: %s",
158 sstrerror (errno, errbuf, sizeof (errbuf)));
163 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
164 /* #endif HAVE_SYSCTLBYNAME */
166 #elif defined(HAVE_LIBSTATGRAB)
167 /* nothing to initialize */
168 #endif /* HAVE_LIBSTATGRAB */
173 static void submit (int cpu_num, const char *type_instance, counter_t value)
176 value_list_t vl = VALUE_LIST_INIT;
178 values[0].counter = value;
182 vl.time = time (NULL);
183 strcpy (vl.host, hostname_g);
184 strcpy (vl.plugin, "cpu");
185 ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
187 strcpy (vl.type, "cpu");
188 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
190 plugin_dispatch_values (&vl);
193 static int cpu_read (void)
195 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
198 kern_return_t status;
200 #if PROCESSOR_CPU_LOAD_INFO
201 processor_cpu_load_info_data_t cpu_info;
202 mach_msg_type_number_t cpu_info_len;
204 #if PROCESSOR_TEMPERATURE
205 processor_info_data_t cpu_temp;
206 mach_msg_type_number_t cpu_temp_len;
211 for (cpu = 0; cpu < cpu_list_len; cpu++)
213 #if PROCESSOR_CPU_LOAD_INFO
215 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
217 if ((status = processor_info (cpu_list[cpu],
218 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
219 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
221 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
225 if (cpu_info_len < CPU_STATE_MAX)
227 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
231 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
232 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
233 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
234 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
235 #endif /* PROCESSOR_CPU_LOAD_INFO */
236 #if PROCESSOR_TEMPERATURE
238 * Not all Apple computers do have this ability. To minimize
239 * the messages sent to the syslog we do an exponential
240 * stepback if `processor_info' fails. We still try ~once a day
243 if (cpu_temp_retry_counter > 0)
245 cpu_temp_retry_counter--;
249 cpu_temp_len = PROCESSOR_INFO_MAX;
251 status = processor_info (cpu_list[cpu],
252 PROCESSOR_TEMPERATURE,
254 cpu_temp, &cpu_temp_len);
255 if (status != KERN_SUCCESS)
257 ERROR ("cpu plugin: processor_info failed: %s",
258 mach_error_string (status));
260 cpu_temp_retry_counter = cpu_temp_retry_step;
261 cpu_temp_retry_step *= 2;
262 if (cpu_temp_retry_step > cpu_temp_retry_max)
263 cpu_temp_retry_step = cpu_temp_retry_max;
268 if (cpu_temp_len != 1)
270 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
275 cpu_temp_retry_counter = 0;
276 cpu_temp_retry_step = 1;
278 DEBUG ("cpu_temp = %i", (int) cpu_temp);
279 #endif /* PROCESSOR_TEMPERATURE */
281 /* #endif PROCESSOR_CPU_LOAD_INFO */
283 #elif defined(KERNEL_LINUX)
285 counter_t user, nice, syst, idle;
286 counter_t wait, intr, sitr; /* sitr == soft interrupt */
293 if ((fh = fopen ("/proc/stat", "r")) == NULL)
296 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
297 sstrerror (errno, errbuf, sizeof (errbuf)));
301 while (fgets (buf, 1024, fh) != NULL)
303 if (strncmp (buf, "cpu", 3))
305 if ((buf[3] < '0') || (buf[3] > '9'))
308 numfields = strsplit (buf, fields, 9);
312 cpu = atoi (fields[0] + 3);
313 user = atoll (fields[1]);
314 nice = atoll (fields[2]);
315 syst = atoll (fields[3]);
316 idle = atoll (fields[4]);
318 submit (cpu, "user", user);
319 submit (cpu, "nice", nice);
320 submit (cpu, "system", syst);
321 submit (cpu, "idle", idle);
325 wait = atoll (fields[5]);
326 intr = atoll (fields[6]);
327 sitr = atoll (fields[7]);
329 submit (cpu, "wait", wait);
330 submit (cpu, "interrupt", intr);
331 submit (cpu, "softirq", sitr);
334 submit (cpu, "steal", atoll (fields[8]));
339 /* #endif defined(KERNEL_LINUX) */
341 #elif defined(HAVE_LIBKSTAT)
343 counter_t user, syst, idle, wait;
344 static cpu_stat_t cs;
349 for (cpu = 0; cpu < numcpu; cpu++)
351 if (kstat_read (kc, ksp[cpu], &cs) == -1)
352 continue; /* error message? */
354 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
355 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
356 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
357 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
359 submit (ksp[cpu]->ks_instance, "user", user);
360 submit (ksp[cpu]->ks_instance, "system", syst);
361 submit (ksp[cpu]->ks_instance, "idle", idle);
362 submit (ksp[cpu]->ks_instance, "wait", wait);
364 /* #endif defined(HAVE_LIBKSTAT) */
366 #elif defined(HAVE_SYSCTLBYNAME)
367 long cpuinfo[CPUSTATES];
370 cpuinfo_size = sizeof (cpuinfo);
372 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
375 ERROR ("cpu plugin: sysctlbyname failed: %s.",
376 sstrerror (errno, errbuf, sizeof (errbuf)));
380 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
382 submit (0, "user", cpuinfo[CP_USER]);
383 submit (0, "nice", cpuinfo[CP_NICE]);
384 submit (0, "system", cpuinfo[CP_SYS]);
385 submit (0, "idle", cpuinfo[CP_IDLE]);
386 /* #endif HAVE_SYSCTLBYNAME */
388 #elif defined(HAVE_LIBSTATGRAB)
390 cs = sg_get_cpu_stats ();
394 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
398 submit (0, "idle", (counter_t) cs->idle);
399 submit (0, "nice", (counter_t) cs->nice);
400 submit (0, "swap", (counter_t) cs->swap);
401 submit (0, "system", (counter_t) cs->kernel);
402 submit (0, "user", (counter_t) cs->user);
403 submit (0, "wait", (counter_t) cs->iowait);
404 #endif /* HAVE_LIBSTATGRAB */
409 void module_register (void)
411 plugin_register_init ("cpu", init);
412 plugin_register_read ("cpu", cpu_read);
413 } /* void module_register */