3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Oleg King <king2 at kaluga.ru>
28 #ifdef HAVE_MACH_KERN_RETURN_H
29 # include <mach/kern_return.h>
31 #ifdef HAVE_MACH_MACH_INIT_H
32 # include <mach/mach_init.h>
34 #ifdef HAVE_MACH_HOST_PRIV_H
35 # include <mach/host_priv.h>
37 #if HAVE_MACH_MACH_ERROR_H
38 # include <mach/mach_error.h>
40 #ifdef HAVE_MACH_PROCESSOR_INFO_H
41 # include <mach/processor_info.h>
43 #ifdef HAVE_MACH_PROCESSOR_H
44 # include <mach/processor.h>
46 #ifdef HAVE_MACH_VM_MAP_H
47 # include <mach/vm_map.h>
51 # include <sys/sysinfo.h>
52 #endif /* HAVE_LIBKSTAT */
54 #ifdef HAVE_SYSCTLBYNAME
55 # ifdef HAVE_SYS_SYSCTL_H
56 # include <sys/sysctl.h>
59 # ifdef HAVE_SYS_DKSTAT_H
60 # include <sys/dkstat.h>
63 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
71 #endif /* HAVE_SYSCTLBYNAME */
74 # include <statgrab.h>
77 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
78 && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
79 # error "No applicable input method."
82 #ifdef PROCESSOR_CPU_LOAD_INFO
83 static mach_port_t port_host;
84 static processor_port_array_t cpu_list;
85 static mach_msg_type_number_t cpu_list_len;
87 #if PROCESSOR_TEMPERATURE
88 static int cpu_temp_retry_counter = 0;
89 static int cpu_temp_retry_step = 1;
90 static int cpu_temp_retry_max = 1;
91 #endif /* PROCESSOR_TEMPERATURE */
92 /* #endif PROCESSOR_CPU_LOAD_INFO */
94 #elif defined(KERNEL_LINUX)
95 /* no variables needed */
96 /* #endif KERNEL_LINUX */
98 #elif defined(HAVE_LIBKSTAT)
99 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
100 # define MAX_NUMCPU 256
101 extern kstat_ctl_t *kc;
102 static kstat_t *ksp[MAX_NUMCPU];
104 /* #endif HAVE_LIBKSTAT */
106 #elif defined(HAVE_SYSCTLBYNAME)
108 /* #endif HAVE_SYSCTLBYNAME */
110 #elif defined(HAVE_LIBSTATGRAB)
111 /* no variables needed */
112 #endif /* HAVE_LIBSTATGRAB */
114 static int init (void)
116 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
117 kern_return_t status;
119 port_host = mach_host_self ();
121 /* FIXME: Free `cpu_list' if it's not NULL */
122 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
124 ERROR ("cpu plugin: host_processors returned %i", (int) status);
129 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
130 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
132 cpu_temp_retry_max = 86400 / interval_g;
133 /* #endif PROCESSOR_CPU_LOAD_INFO */
135 #elif defined(HAVE_LIBKSTAT)
143 /* Solaris doesn't count linear.. *sigh* */
144 for (numcpu = 0, ksp_chain = kc->kc_chain;
145 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
146 ksp_chain = ksp_chain->ks_next)
147 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
148 ksp[numcpu++] = ksp_chain;
149 /* #endif HAVE_LIBKSTAT */
151 #elif defined (HAVE_SYSCTLBYNAME)
154 numcpu_size = sizeof (numcpu);
156 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
159 WARNING ("cpu plugin: sysctlbyname: %s",
160 sstrerror (errno, errbuf, sizeof (errbuf)));
165 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
166 /* #endif HAVE_SYSCTLBYNAME */
168 #elif defined(HAVE_LIBSTATGRAB)
169 /* nothing to initialize */
170 #endif /* HAVE_LIBSTATGRAB */
175 static void submit (int cpu_num, const char *type_instance, counter_t value)
178 value_list_t vl = VALUE_LIST_INIT;
180 values[0].counter = value;
184 vl.time = time (NULL);
185 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
186 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
187 ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
189 sstrncpy (vl.type, "cpu", sizeof (vl.type));
190 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
192 plugin_dispatch_values (&vl);
195 static int cpu_read (void)
197 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
200 kern_return_t status;
202 #if PROCESSOR_CPU_LOAD_INFO
203 processor_cpu_load_info_data_t cpu_info;
204 mach_msg_type_number_t cpu_info_len;
206 #if PROCESSOR_TEMPERATURE
207 processor_info_data_t cpu_temp;
208 mach_msg_type_number_t cpu_temp_len;
213 for (cpu = 0; cpu < cpu_list_len; cpu++)
215 #if PROCESSOR_CPU_LOAD_INFO
217 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
219 if ((status = processor_info (cpu_list[cpu],
220 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
221 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
223 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
227 if (cpu_info_len < CPU_STATE_MAX)
229 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
233 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
234 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
235 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
236 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
237 #endif /* PROCESSOR_CPU_LOAD_INFO */
238 #if PROCESSOR_TEMPERATURE
240 * Not all Apple computers do have this ability. To minimize
241 * the messages sent to the syslog we do an exponential
242 * stepback if `processor_info' fails. We still try ~once a day
245 if (cpu_temp_retry_counter > 0)
247 cpu_temp_retry_counter--;
251 cpu_temp_len = PROCESSOR_INFO_MAX;
253 status = processor_info (cpu_list[cpu],
254 PROCESSOR_TEMPERATURE,
256 cpu_temp, &cpu_temp_len);
257 if (status != KERN_SUCCESS)
259 ERROR ("cpu plugin: processor_info failed: %s",
260 mach_error_string (status));
262 cpu_temp_retry_counter = cpu_temp_retry_step;
263 cpu_temp_retry_step *= 2;
264 if (cpu_temp_retry_step > cpu_temp_retry_max)
265 cpu_temp_retry_step = cpu_temp_retry_max;
270 if (cpu_temp_len != 1)
272 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
277 cpu_temp_retry_counter = 0;
278 cpu_temp_retry_step = 1;
280 DEBUG ("cpu_temp = %i", (int) cpu_temp);
281 #endif /* PROCESSOR_TEMPERATURE */
283 /* #endif PROCESSOR_CPU_LOAD_INFO */
285 #elif defined(KERNEL_LINUX)
287 counter_t user, nice, syst, idle;
288 counter_t wait, intr, sitr; /* sitr == soft interrupt */
295 if ((fh = fopen ("/proc/stat", "r")) == NULL)
298 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
299 sstrerror (errno, errbuf, sizeof (errbuf)));
303 while (fgets (buf, 1024, fh) != NULL)
305 if (strncmp (buf, "cpu", 3))
307 if ((buf[3] < '0') || (buf[3] > '9'))
310 numfields = strsplit (buf, fields, 9);
314 cpu = atoi (fields[0] + 3);
315 user = atoll (fields[1]);
316 nice = atoll (fields[2]);
317 syst = atoll (fields[3]);
318 idle = atoll (fields[4]);
320 submit (cpu, "user", user);
321 submit (cpu, "nice", nice);
322 submit (cpu, "system", syst);
323 submit (cpu, "idle", idle);
327 wait = atoll (fields[5]);
328 intr = atoll (fields[6]);
329 sitr = atoll (fields[7]);
331 submit (cpu, "wait", wait);
332 submit (cpu, "interrupt", intr);
333 submit (cpu, "softirq", sitr);
336 submit (cpu, "steal", atoll (fields[8]));
341 /* #endif defined(KERNEL_LINUX) */
343 #elif defined(HAVE_LIBKSTAT)
345 counter_t user, syst, idle, wait;
346 static cpu_stat_t cs;
351 for (cpu = 0; cpu < numcpu; cpu++)
353 if (kstat_read (kc, ksp[cpu], &cs) == -1)
354 continue; /* error message? */
356 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
357 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
358 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
359 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
361 submit (ksp[cpu]->ks_instance, "user", user);
362 submit (ksp[cpu]->ks_instance, "system", syst);
363 submit (ksp[cpu]->ks_instance, "idle", idle);
364 submit (ksp[cpu]->ks_instance, "wait", wait);
366 /* #endif defined(HAVE_LIBKSTAT) */
368 #elif defined(HAVE_SYSCTLBYNAME)
369 long cpuinfo[CPUSTATES];
372 cpuinfo_size = sizeof (cpuinfo);
374 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
377 ERROR ("cpu plugin: sysctlbyname failed: %s.",
378 sstrerror (errno, errbuf, sizeof (errbuf)));
382 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
384 submit (0, "user", cpuinfo[CP_USER]);
385 submit (0, "nice", cpuinfo[CP_NICE]);
386 submit (0, "system", cpuinfo[CP_SYS]);
387 submit (0, "idle", cpuinfo[CP_IDLE]);
388 /* #endif HAVE_SYSCTLBYNAME */
390 #elif defined(HAVE_LIBSTATGRAB)
392 cs = sg_get_cpu_stats ();
396 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
400 submit (0, "idle", (counter_t) cs->idle);
401 submit (0, "nice", (counter_t) cs->nice);
402 submit (0, "swap", (counter_t) cs->swap);
403 submit (0, "system", (counter_t) cs->kernel);
404 submit (0, "user", (counter_t) cs->user);
405 submit (0, "wait", (counter_t) cs->iowait);
406 #endif /* HAVE_LIBSTATGRAB */
411 void module_register (void)
413 plugin_register_init ("cpu", init);
414 plugin_register_read ("cpu", cpu_read);
415 } /* void module_register */