3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Oleg King <king2 at kaluga.ru>
23 * Simon Kuhnle <simon at blarzwurst.de>
30 #ifdef HAVE_MACH_KERN_RETURN_H
31 # include <mach/kern_return.h>
33 #ifdef HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #ifdef HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
39 #if HAVE_MACH_MACH_ERROR_H
40 # include <mach/mach_error.h>
42 #ifdef HAVE_MACH_PROCESSOR_INFO_H
43 # include <mach/processor_info.h>
45 #ifdef HAVE_MACH_PROCESSOR_H
46 # include <mach/processor.h>
48 #ifdef HAVE_MACH_VM_MAP_H
49 # include <mach/vm_map.h>
53 # include <sys/sysinfo.h>
54 #endif /* HAVE_LIBKSTAT */
56 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
57 || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
58 # ifdef HAVE_SYS_SYSCTL_H
59 # include <sys/sysctl.h>
62 # ifdef HAVE_SYS_DKSTAT_H
63 # include <sys/dkstat.h>
66 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
74 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
77 # if defined(CTL_HW) && defined(HW_NCPU) \
78 && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
79 # define CAN_USE_SYSCTL 1
81 # define CAN_USE_SYSCTL 0
84 # define CAN_USE_SYSCTL 0
88 # include <statgrab.h>
91 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
92 && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
93 # error "No applicable input method."
96 #ifdef PROCESSOR_CPU_LOAD_INFO
97 static mach_port_t port_host;
98 static processor_port_array_t cpu_list;
99 static mach_msg_type_number_t cpu_list_len;
101 #if PROCESSOR_TEMPERATURE
102 static int cpu_temp_retry_counter = 0;
103 static int cpu_temp_retry_step = 1;
104 static int cpu_temp_retry_max = 1;
105 #endif /* PROCESSOR_TEMPERATURE */
106 /* #endif PROCESSOR_CPU_LOAD_INFO */
108 #elif defined(KERNEL_LINUX)
109 /* no variables needed */
110 /* #endif KERNEL_LINUX */
112 #elif defined(HAVE_LIBKSTAT)
113 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
114 # define MAX_NUMCPU 256
115 extern kstat_ctl_t *kc;
116 static kstat_t *ksp[MAX_NUMCPU];
118 /* #endif HAVE_LIBKSTAT */
122 /* #endif CAN_USE_SYSCTL */
124 #elif defined(HAVE_SYSCTLBYNAME)
126 # ifdef HAVE_SYSCTL_KERN_CP_TIMES
128 # endif /* HAVE_SYSCTL_KERN_CP_TIMES */
129 /* #endif HAVE_SYSCTLBYNAME */
131 #elif defined(HAVE_LIBSTATGRAB)
132 /* no variables needed */
133 #endif /* HAVE_LIBSTATGRAB */
135 static int init (void)
137 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
138 kern_return_t status;
140 port_host = mach_host_self ();
142 /* FIXME: Free `cpu_list' if it's not NULL */
143 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
145 ERROR ("cpu plugin: host_processors returned %i", (int) status);
150 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
151 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
153 cpu_temp_retry_max = 86400 / interval_g;
154 /* #endif PROCESSOR_CPU_LOAD_INFO */
156 #elif defined(HAVE_LIBKSTAT)
164 /* Solaris doesn't count linear.. *sigh* */
165 for (numcpu = 0, ksp_chain = kc->kc_chain;
166 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
167 ksp_chain = ksp_chain->ks_next)
168 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
169 ksp[numcpu++] = ksp_chain;
170 /* #endif HAVE_LIBKSTAT */
174 int mib[2] = {CTL_HW, HW_NCPU};
178 numcpu_size = sizeof (numcpu);
180 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
181 &numcpu, &numcpu_size, NULL, 0);
185 WARNING ("cpu plugin: sysctl: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
189 /* #endif CAN_USE_SYSCTL */
191 #elif defined (HAVE_SYSCTLBYNAME)
194 numcpu_size = sizeof (numcpu);
196 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
199 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
200 sstrerror (errno, errbuf, sizeof (errbuf)));
204 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
205 numcpu_size = sizeof (maxcpu);
207 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
210 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
211 sstrerror (errno, errbuf, sizeof (errbuf)));
216 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
218 /* #endif HAVE_SYSCTLBYNAME */
220 #elif defined(HAVE_LIBSTATGRAB)
221 /* nothing to initialize */
222 #endif /* HAVE_LIBSTATGRAB */
227 static void submit (int cpu_num, const char *type_instance, counter_t value)
230 value_list_t vl = VALUE_LIST_INIT;
232 values[0].counter = value;
236 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
237 sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
238 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
240 sstrncpy (vl.type, "cpu", sizeof (vl.type));
241 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
243 plugin_dispatch_values (&vl);
246 static int cpu_read (void)
248 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
251 kern_return_t status;
253 #if PROCESSOR_CPU_LOAD_INFO
254 processor_cpu_load_info_data_t cpu_info;
255 mach_msg_type_number_t cpu_info_len;
257 #if PROCESSOR_TEMPERATURE
258 processor_info_data_t cpu_temp;
259 mach_msg_type_number_t cpu_temp_len;
264 for (cpu = 0; cpu < cpu_list_len; cpu++)
266 #if PROCESSOR_CPU_LOAD_INFO
268 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
270 if ((status = processor_info (cpu_list[cpu],
271 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
272 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
274 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
278 if (cpu_info_len < CPU_STATE_MAX)
280 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
284 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
285 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
286 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
287 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
288 #endif /* PROCESSOR_CPU_LOAD_INFO */
289 #if PROCESSOR_TEMPERATURE
291 * Not all Apple computers do have this ability. To minimize
292 * the messages sent to the syslog we do an exponential
293 * stepback if `processor_info' fails. We still try ~once a day
296 if (cpu_temp_retry_counter > 0)
298 cpu_temp_retry_counter--;
302 cpu_temp_len = PROCESSOR_INFO_MAX;
304 status = processor_info (cpu_list[cpu],
305 PROCESSOR_TEMPERATURE,
307 cpu_temp, &cpu_temp_len);
308 if (status != KERN_SUCCESS)
310 ERROR ("cpu plugin: processor_info failed: %s",
311 mach_error_string (status));
313 cpu_temp_retry_counter = cpu_temp_retry_step;
314 cpu_temp_retry_step *= 2;
315 if (cpu_temp_retry_step > cpu_temp_retry_max)
316 cpu_temp_retry_step = cpu_temp_retry_max;
321 if (cpu_temp_len != 1)
323 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
328 cpu_temp_retry_counter = 0;
329 cpu_temp_retry_step = 1;
331 DEBUG ("cpu_temp = %i", (int) cpu_temp);
332 #endif /* PROCESSOR_TEMPERATURE */
334 /* #endif PROCESSOR_CPU_LOAD_INFO */
336 #elif defined(KERNEL_LINUX)
338 counter_t user, nice, syst, idle;
339 counter_t wait, intr, sitr; /* sitr == soft interrupt */
346 if ((fh = fopen ("/proc/stat", "r")) == NULL)
349 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
350 sstrerror (errno, errbuf, sizeof (errbuf)));
354 while (fgets (buf, 1024, fh) != NULL)
356 if (strncmp (buf, "cpu", 3))
358 if ((buf[3] < '0') || (buf[3] > '9'))
361 numfields = strsplit (buf, fields, 9);
365 cpu = atoi (fields[0] + 3);
366 user = atoll (fields[1]);
367 nice = atoll (fields[2]);
368 syst = atoll (fields[3]);
369 idle = atoll (fields[4]);
371 submit (cpu, "user", user);
372 submit (cpu, "nice", nice);
373 submit (cpu, "system", syst);
374 submit (cpu, "idle", idle);
378 wait = atoll (fields[5]);
379 intr = atoll (fields[6]);
380 sitr = atoll (fields[7]);
382 submit (cpu, "wait", wait);
383 submit (cpu, "interrupt", intr);
384 submit (cpu, "softirq", sitr);
387 submit (cpu, "steal", atoll (fields[8]));
392 /* #endif defined(KERNEL_LINUX) */
394 #elif defined(HAVE_LIBKSTAT)
396 counter_t user, syst, idle, wait;
397 static cpu_stat_t cs;
402 for (cpu = 0; cpu < numcpu; cpu++)
404 if (kstat_read (kc, ksp[cpu], &cs) == -1)
405 continue; /* error message? */
407 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
408 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
409 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
410 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
412 submit (ksp[cpu]->ks_instance, "user", user);
413 submit (ksp[cpu]->ks_instance, "system", syst);
414 submit (ksp[cpu]->ks_instance, "idle", idle);
415 submit (ksp[cpu]->ks_instance, "wait", wait);
417 /* #endif defined(HAVE_LIBKSTAT) */
420 uint64_t cpuinfo[numcpu][CPUSTATES];
427 ERROR ("cpu plugin: Could not determine number of "
428 "installed CPUs using sysctl(3).");
432 memset (cpuinfo, 0, sizeof (cpuinfo));
434 #if defined(KERN_CPTIME2)
436 for (i = 0; i < numcpu; i++) {
437 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
439 cpuinfo_size = sizeof (cpuinfo[0]);
441 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
442 cpuinfo[i], &cpuinfo_size, NULL, 0);
445 ERROR ("cpu plugin: sysctl failed: %s.",
446 sstrerror (errno, errbuf, sizeof (errbuf)));
452 #endif /* defined(KERN_CPTIME2) */
454 int mib[] = {CTL_KERN, KERN_CPTIME};
455 long cpuinfo_tmp[CPUSTATES];
457 cpuinfo_size = sizeof(cpuinfo_tmp);
459 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
460 &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
464 ERROR ("cpu plugin: sysctl failed: %s.",
465 sstrerror (errno, errbuf, sizeof (errbuf)));
469 for(i = 0; i < CPUSTATES; i++) {
470 cpuinfo[0][i] = cpuinfo_tmp[i];
474 for (i = 0; i < numcpu; i++) {
475 submit (i, "user", cpuinfo[i][CP_USER]);
476 submit (i, "nice", cpuinfo[i][CP_NICE]);
477 submit (i, "system", cpuinfo[i][CP_SYS]);
478 submit (i, "idle", cpuinfo[i][CP_IDLE]);
479 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
481 /* #endif CAN_USE_SYSCTL */
482 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
483 long cpuinfo[maxcpu][CPUSTATES];
487 memset (cpuinfo, 0, sizeof (cpuinfo));
489 cpuinfo_size = sizeof (cpuinfo);
490 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
493 ERROR ("cpu plugin: sysctlbyname failed: %s.",
494 sstrerror (errno, errbuf, sizeof (errbuf)));
498 for (i = 0; i < numcpu; i++) {
499 submit (i, "user", cpuinfo[i][CP_USER]);
500 submit (i, "nice", cpuinfo[i][CP_NICE]);
501 submit (i, "system", cpuinfo[i][CP_SYS]);
502 submit (i, "idle", cpuinfo[i][CP_IDLE]);
503 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
505 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
506 #elif defined(HAVE_SYSCTLBYNAME)
507 long cpuinfo[CPUSTATES];
510 cpuinfo_size = sizeof (cpuinfo);
512 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
515 ERROR ("cpu plugin: sysctlbyname failed: %s.",
516 sstrerror (errno, errbuf, sizeof (errbuf)));
520 submit (0, "user", cpuinfo[CP_USER]);
521 submit (0, "nice", cpuinfo[CP_NICE]);
522 submit (0, "system", cpuinfo[CP_SYS]);
523 submit (0, "idle", cpuinfo[CP_IDLE]);
524 submit (0, "interrupt", cpuinfo[CP_INTR]);
525 /* #endif HAVE_SYSCTLBYNAME */
527 #elif defined(HAVE_LIBSTATGRAB)
529 cs = sg_get_cpu_stats ();
533 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
537 submit (0, "idle", (counter_t) cs->idle);
538 submit (0, "nice", (counter_t) cs->nice);
539 submit (0, "swap", (counter_t) cs->swap);
540 submit (0, "system", (counter_t) cs->kernel);
541 submit (0, "user", (counter_t) cs->user);
542 submit (0, "wait", (counter_t) cs->iowait);
543 #endif /* HAVE_LIBSTATGRAB */
548 void module_register (void)
550 plugin_register_init ("cpu", init);
551 plugin_register_read ("cpu", cpu_read);
552 } /* void module_register */