3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Simon Kuhnle
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 * Simon Kuhnle <simon at blarzwurst.de>
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 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
55 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
56 # ifdef HAVE_SYS_SYSCTL_H
57 # include <sys/sysctl.h>
60 # ifdef HAVE_SYS_DKSTAT_H
61 # include <sys/dkstat.h>
64 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
72 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
75 # if defined(CTL_HW) && defined(HW_NCPU) \
76 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
77 # define CAN_USE_SYSCTL 1
79 # define CAN_USE_SYSCTL 0
82 # define CAN_USE_SYSCTL 0
86 # include <statgrab.h>
89 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
90 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
91 # error "No applicable input method."
94 #ifdef PROCESSOR_CPU_LOAD_INFO
95 static mach_port_t port_host;
96 static processor_port_array_t cpu_list;
97 static mach_msg_type_number_t cpu_list_len;
99 #if PROCESSOR_TEMPERATURE
100 static int cpu_temp_retry_counter = 0;
101 static int cpu_temp_retry_step = 1;
102 static int cpu_temp_retry_max = 1;
103 #endif /* PROCESSOR_TEMPERATURE */
104 /* #endif PROCESSOR_CPU_LOAD_INFO */
106 #elif defined(KERNEL_LINUX)
107 /* no variables needed */
108 /* #endif KERNEL_LINUX */
110 #elif defined(HAVE_LIBKSTAT)
111 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
112 # define MAX_NUMCPU 256
113 extern kstat_ctl_t *kc;
114 static kstat_t *ksp[MAX_NUMCPU];
116 /* #endif HAVE_LIBKSTAT */
120 /* #endif CAN_USE_SYSCTL */
122 #elif defined(HAVE_SYSCTLBYNAME)
124 /* #endif HAVE_SYSCTLBYNAME */
126 #elif defined(HAVE_LIBSTATGRAB)
127 /* no variables needed */
128 #endif /* HAVE_LIBSTATGRAB */
130 static int init (void)
132 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
133 kern_return_t status;
135 port_host = mach_host_self ();
137 /* FIXME: Free `cpu_list' if it's not NULL */
138 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
140 ERROR ("cpu plugin: host_processors returned %i", (int) status);
145 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
146 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
148 cpu_temp_retry_max = 86400 / interval_g;
149 /* #endif PROCESSOR_CPU_LOAD_INFO */
151 #elif defined(HAVE_LIBKSTAT)
159 /* Solaris doesn't count linear.. *sigh* */
160 for (numcpu = 0, ksp_chain = kc->kc_chain;
161 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
162 ksp_chain = ksp_chain->ks_next)
163 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
164 ksp[numcpu++] = ksp_chain;
165 /* #endif HAVE_LIBKSTAT */
169 int mib[2] = {CTL_HW, HW_NCPU};
173 numcpu_size = sizeof (numcpu);
175 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
176 &numcpu, &numcpu_size, NULL, 0);
180 WARNING ("cpu plugin: sysctl: %s",
181 sstrerror (errno, errbuf, sizeof (errbuf)));
184 /* #endif CAN_USE_SYSCTL */
186 #elif defined (HAVE_SYSCTLBYNAME)
189 numcpu_size = sizeof (numcpu);
191 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
194 WARNING ("cpu plugin: sysctlbyname: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
200 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
201 /* #endif HAVE_SYSCTLBYNAME */
203 #elif defined(HAVE_LIBSTATGRAB)
204 /* nothing to initialize */
205 #endif /* HAVE_LIBSTATGRAB */
210 static void submit (int cpu_num, const char *type_instance, counter_t value)
213 value_list_t vl = VALUE_LIST_INIT;
215 values[0].counter = value;
219 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
220 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
221 ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
223 sstrncpy (vl.type, "cpu", sizeof (vl.type));
224 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
226 plugin_dispatch_values (&vl);
229 static int cpu_read (void)
231 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
234 kern_return_t status;
236 #if PROCESSOR_CPU_LOAD_INFO
237 processor_cpu_load_info_data_t cpu_info;
238 mach_msg_type_number_t cpu_info_len;
240 #if PROCESSOR_TEMPERATURE
241 processor_info_data_t cpu_temp;
242 mach_msg_type_number_t cpu_temp_len;
247 for (cpu = 0; cpu < cpu_list_len; cpu++)
249 #if PROCESSOR_CPU_LOAD_INFO
251 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
253 if ((status = processor_info (cpu_list[cpu],
254 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
255 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
257 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
261 if (cpu_info_len < CPU_STATE_MAX)
263 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
267 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
268 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
269 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
270 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
271 #endif /* PROCESSOR_CPU_LOAD_INFO */
272 #if PROCESSOR_TEMPERATURE
274 * Not all Apple computers do have this ability. To minimize
275 * the messages sent to the syslog we do an exponential
276 * stepback if `processor_info' fails. We still try ~once a day
279 if (cpu_temp_retry_counter > 0)
281 cpu_temp_retry_counter--;
285 cpu_temp_len = PROCESSOR_INFO_MAX;
287 status = processor_info (cpu_list[cpu],
288 PROCESSOR_TEMPERATURE,
290 cpu_temp, &cpu_temp_len);
291 if (status != KERN_SUCCESS)
293 ERROR ("cpu plugin: processor_info failed: %s",
294 mach_error_string (status));
296 cpu_temp_retry_counter = cpu_temp_retry_step;
297 cpu_temp_retry_step *= 2;
298 if (cpu_temp_retry_step > cpu_temp_retry_max)
299 cpu_temp_retry_step = cpu_temp_retry_max;
304 if (cpu_temp_len != 1)
306 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
311 cpu_temp_retry_counter = 0;
312 cpu_temp_retry_step = 1;
314 DEBUG ("cpu_temp = %i", (int) cpu_temp);
315 #endif /* PROCESSOR_TEMPERATURE */
317 /* #endif PROCESSOR_CPU_LOAD_INFO */
319 #elif defined(KERNEL_LINUX)
321 counter_t user, nice, syst, idle;
322 counter_t wait, intr, sitr; /* sitr == soft interrupt */
329 if ((fh = fopen ("/proc/stat", "r")) == NULL)
332 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
333 sstrerror (errno, errbuf, sizeof (errbuf)));
337 while (fgets (buf, 1024, fh) != NULL)
339 if (strncmp (buf, "cpu", 3))
341 if ((buf[3] < '0') || (buf[3] > '9'))
344 numfields = strsplit (buf, fields, 9);
348 cpu = atoi (fields[0] + 3);
349 user = atoll (fields[1]);
350 nice = atoll (fields[2]);
351 syst = atoll (fields[3]);
352 idle = atoll (fields[4]);
354 submit (cpu, "user", user);
355 submit (cpu, "nice", nice);
356 submit (cpu, "system", syst);
357 submit (cpu, "idle", idle);
361 wait = atoll (fields[5]);
362 intr = atoll (fields[6]);
363 sitr = atoll (fields[7]);
365 submit (cpu, "wait", wait);
366 submit (cpu, "interrupt", intr);
367 submit (cpu, "softirq", sitr);
370 submit (cpu, "steal", atoll (fields[8]));
375 /* #endif defined(KERNEL_LINUX) */
377 #elif defined(HAVE_LIBKSTAT)
379 counter_t user, syst, idle, wait;
380 static cpu_stat_t cs;
385 for (cpu = 0; cpu < numcpu; cpu++)
387 if (kstat_read (kc, ksp[cpu], &cs) == -1)
388 continue; /* error message? */
390 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
391 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
392 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
393 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
395 submit (ksp[cpu]->ks_instance, "user", user);
396 submit (ksp[cpu]->ks_instance, "system", syst);
397 submit (ksp[cpu]->ks_instance, "idle", idle);
398 submit (ksp[cpu]->ks_instance, "wait", wait);
400 /* #endif defined(HAVE_LIBKSTAT) */
403 uint64_t cpuinfo[numcpu][CPUSTATES];
410 ERROR ("cpu plugin: Could not determine number of "
411 "installed CPUs using sysctl(3).");
415 memset (cpuinfo, 0, sizeof (cpuinfo));
417 #if defined(KERN_CPTIME2)
419 for (i = 0; i < numcpu; i++) {
420 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
422 cpuinfo_size = sizeof (cpuinfo[0]);
424 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
425 cpuinfo[i], &cpuinfo_size, NULL, 0);
428 ERROR ("cpu plugin: sysctl failed: %s.",
429 sstrerror (errno, errbuf, sizeof (errbuf)));
435 #endif /* defined(KERN_CPTIME2) */
437 int mib[] = {CTL_KERN, KERN_CPTIME};
438 long cpuinfo_tmp[CPUSTATES];
440 cpuinfo_size = sizeof(cpuinfo_tmp);
442 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
443 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
447 ERROR ("cpu plugin: sysctl failed: %s.",
448 sstrerror (errno, errbuf, sizeof (errbuf)));
452 for(i = 0; i < CPUSTATES; i++) {
453 cpuinfo[0][i] = cpuinfo_tmp[i];
457 for (i = 0; i < numcpu; i++) {
458 submit (i, "user", cpuinfo[i][CP_USER]);
459 submit (i, "nice", cpuinfo[i][CP_NICE]);
460 submit (i, "system", cpuinfo[i][CP_SYS]);
461 submit (i, "idle", cpuinfo[i][CP_IDLE]);
462 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
464 /* #endif CAN_USE_SYSCTL */
466 #elif defined(HAVE_SYSCTLBYNAME)
467 long cpuinfo[CPUSTATES];
470 cpuinfo_size = sizeof (cpuinfo);
472 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
475 ERROR ("cpu plugin: sysctlbyname failed: %s.",
476 sstrerror (errno, errbuf, sizeof (errbuf)));
480 submit (0, "user", cpuinfo[CP_USER]);
481 submit (0, "nice", cpuinfo[CP_NICE]);
482 submit (0, "system", cpuinfo[CP_SYS]);
483 submit (0, "idle", cpuinfo[CP_IDLE]);
484 submit (0, "interrupt", cpuinfo[CP_INTR]);
485 /* #endif HAVE_SYSCTLBYNAME */
487 #elif defined(HAVE_LIBSTATGRAB)
489 cs = sg_get_cpu_stats ();
493 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
497 submit (0, "idle", (counter_t) cs->idle);
498 submit (0, "nice", (counter_t) cs->nice);
499 submit (0, "swap", (counter_t) cs->swap);
500 submit (0, "system", (counter_t) cs->kernel);
501 submit (0, "user", (counter_t) cs->user);
502 submit (0, "wait", (counter_t) cs->iowait);
503 #endif /* HAVE_LIBSTATGRAB */
508 void module_register (void)
510 plugin_register_init ("cpu", init);
511 plugin_register_read ("cpu", cpu_read);
512 } /* void module_register */