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 */
71 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_SYSCTLBYNAME
72 # error "No applicable input method."
75 #ifdef PROCESSOR_CPU_LOAD_INFO
76 static mach_port_t port_host;
77 static processor_port_array_t cpu_list;
78 static mach_msg_type_number_t cpu_list_len;
80 #if PROCESSOR_TEMPERATURE
81 static int cpu_temp_retry_counter = 0;
82 static int cpu_temp_retry_step = 1;
83 static int cpu_temp_retry_max = 1;
84 #endif /* PROCESSOR_TEMPERATURE */
85 /* #endif PROCESSOR_CPU_LOAD_INFO */
87 #elif defined(KERNEL_LINUX)
88 /* no variables needed */
89 /* #endif KERNEL_LINUX */
91 #elif defined(HAVE_LIBKSTAT)
92 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
93 # define MAX_NUMCPU 256
94 extern kstat_ctl_t *kc;
95 static kstat_t *ksp[MAX_NUMCPU];
97 /* #endif HAVE_LIBKSTAT */
99 #elif defined(HAVE_SYSCTLBYNAME)
101 #endif /* HAVE_SYSCTLBYNAME */
103 static int init (void)
105 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
106 kern_return_t status;
108 port_host = mach_host_self ();
110 /* FIXME: Free `cpu_list' if it's not NULL */
111 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
113 ERROR ("cpu plugin: host_processors returned %i", (int) status);
118 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
119 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
121 cpu_temp_retry_max = 86400 / interval_g;
122 /* #endif PROCESSOR_CPU_LOAD_INFO */
124 #elif defined(HAVE_LIBKSTAT)
132 /* Solaris doesn't count linear.. *sigh* */
133 for (numcpu = 0, ksp_chain = kc->kc_chain;
134 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
135 ksp_chain = ksp_chain->ks_next)
136 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
137 ksp[numcpu++] = ksp_chain;
138 /* #endif HAVE_LIBKSTAT */
140 #elif defined (HAVE_SYSCTLBYNAME)
143 numcpu_size = sizeof (numcpu);
145 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
148 WARNING ("cpu plugin: sysctlbyname: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
154 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
160 static void submit (int cpu_num, const char *type_instance, counter_t value)
163 value_list_t vl = VALUE_LIST_INIT;
165 values[0].counter = value;
169 vl.time = time (NULL);
170 strcpy (vl.host, hostname_g);
171 strcpy (vl.plugin, "cpu");
172 snprintf (vl.plugin_instance, sizeof (vl.type_instance),
174 vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
175 strcpy (vl.type, "cpu");
176 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
178 plugin_dispatch_values (&vl);
181 static int cpu_read (void)
183 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
186 kern_return_t status;
188 #if PROCESSOR_CPU_LOAD_INFO
189 processor_cpu_load_info_data_t cpu_info;
190 mach_msg_type_number_t cpu_info_len;
192 #if PROCESSOR_TEMPERATURE
193 processor_info_data_t cpu_temp;
194 mach_msg_type_number_t cpu_temp_len;
199 for (cpu = 0; cpu < cpu_list_len; cpu++)
201 #if PROCESSOR_CPU_LOAD_INFO
203 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
205 if ((status = processor_info (cpu_list[cpu],
206 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
207 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
209 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
213 if (cpu_info_len < CPU_STATE_MAX)
215 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
219 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
220 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
221 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
222 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
223 #endif /* PROCESSOR_CPU_LOAD_INFO */
224 #if PROCESSOR_TEMPERATURE
226 * Not all Apple computers do have this ability. To minimize
227 * the messages sent to the syslog we do an exponential
228 * stepback if `processor_info' fails. We still try ~once a day
231 if (cpu_temp_retry_counter > 0)
233 cpu_temp_retry_counter--;
237 cpu_temp_len = PROCESSOR_INFO_MAX;
239 status = processor_info (cpu_list[cpu],
240 PROCESSOR_TEMPERATURE,
242 cpu_temp, &cpu_temp_len);
243 if (status != KERN_SUCCESS)
245 ERROR ("cpu plugin: processor_info failed: %s",
246 mach_error_string (status));
248 cpu_temp_retry_counter = cpu_temp_retry_step;
249 cpu_temp_retry_step *= 2;
250 if (cpu_temp_retry_step > cpu_temp_retry_max)
251 cpu_temp_retry_step = cpu_temp_retry_max;
256 if (cpu_temp_len != 1)
258 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
263 cpu_temp_retry_counter = 0;
264 cpu_temp_retry_step = 1;
266 DEBUG ("cpu_temp = %i", (int) cpu_temp);
267 #endif /* PROCESSOR_TEMPERATURE */
269 /* #endif PROCESSOR_CPU_LOAD_INFO */
271 #elif defined(KERNEL_LINUX)
273 counter_t user, nice, syst, idle;
274 counter_t wait, intr, sitr; /* sitr == soft interrupt */
281 if ((fh = fopen ("/proc/stat", "r")) == NULL)
284 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
285 sstrerror (errno, errbuf, sizeof (errbuf)));
289 while (fgets (buf, 1024, fh) != NULL)
291 if (strncmp (buf, "cpu", 3))
293 if ((buf[3] < '0') || (buf[3] > '9'))
296 numfields = strsplit (buf, fields, 9);
300 cpu = atoi (fields[0] + 3);
301 user = atoll (fields[1]);
302 nice = atoll (fields[2]);
303 syst = atoll (fields[3]);
304 idle = atoll (fields[4]);
306 submit (cpu, "user", user);
307 submit (cpu, "nice", nice);
308 submit (cpu, "system", syst);
309 submit (cpu, "idle", idle);
313 wait = atoll (fields[5]);
314 intr = atoll (fields[6]);
315 sitr = atoll (fields[7]);
317 submit (cpu, "wait", wait);
318 submit (cpu, "interrupt", intr);
319 submit (cpu, "softirq", sitr);
322 submit (cpu, "steal", atoll (fields[8]));
327 /* #endif defined(KERNEL_LINUX) */
329 #elif defined(HAVE_LIBKSTAT)
331 counter_t user, syst, idle, wait;
332 static cpu_stat_t cs;
337 for (cpu = 0; cpu < numcpu; cpu++)
339 if (kstat_read (kc, ksp[cpu], &cs) == -1)
340 continue; /* error message? */
342 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
343 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
344 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
345 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
347 submit (ksp[cpu]->ks_instance, "user", user);
348 submit (ksp[cpu]->ks_instance, "system", syst);
349 submit (ksp[cpu]->ks_instance, "idle", idle);
350 submit (ksp[cpu]->ks_instance, "wait", wait);
352 /* #endif defined(HAVE_LIBKSTAT) */
354 #elif defined(HAVE_SYSCTLBYNAME)
355 long cpuinfo[CPUSTATES];
358 cpuinfo_size = sizeof (cpuinfo);
360 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
363 ERROR ("cpu plugin: sysctlbyname failed: %s.",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
368 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
370 submit (0, "user", cpuinfo[CP_USER]);
371 submit (0, "nice", cpuinfo[CP_NICE]);
372 submit (0, "system", cpuinfo[CP_SYS]);
373 submit (0, "idle", cpuinfo[CP_IDLE]);
379 void module_register (void)
381 plugin_register_init ("cpu", init);
382 plugin_register_read ("cpu", cpu_read);
383 } /* void module_register */