4c852a53fea2405d66a3ec6a92829d5ea20aebb6
1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005-2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #ifdef HAVE_MACH_KERN_RETURN_H
27 # include <mach/kern_return.h>
28 #endif
29 #ifdef HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
31 #endif
32 #ifdef HAVE_MACH_HOST_PRIV_H
33 # include <mach/host_priv.h>
34 #endif
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
37 #endif
38 #ifdef HAVE_MACH_PROCESSOR_INFO_H
39 # include <mach/processor_info.h>
40 #endif
41 #ifdef HAVE_MACH_PROCESSOR_H
42 # include <mach/processor.h>
43 #endif
44 #ifdef HAVE_MACH_VM_MAP_H
45 # include <mach/vm_map.h>
46 #endif
48 #ifdef HAVE_LIBKSTAT
49 # include <sys/sysinfo.h>
50 #endif /* HAVE_LIBKSTAT */
52 #ifdef HAVE_SYSCTLBYNAME
53 # ifdef HAVE_SYS_SYSCTL_H
54 # include <sys/sysctl.h>
55 # endif
57 # ifdef HAVE_SYS_DKSTAT_H
58 # include <sys/dkstat.h>
59 # endif
61 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
62 # define CP_USER 0
63 # define CP_NICE 1
64 # define CP_SYS 2
65 # define CP_INTR 3
66 # define CP_IDLE 4
67 # define CPUSTATES 5
68 # endif
69 #endif /* HAVE_SYSCTLBYNAME */
71 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
72 # define CPU_HAVE_READ 1
73 #else
74 # define CPU_HAVE_READ 0
75 #endif
77 #if CPU_HAVE_READ
78 #ifdef PROCESSOR_CPU_LOAD_INFO
79 static mach_port_t port_host;
80 static processor_port_array_t cpu_list;
81 static mach_msg_type_number_t cpu_list_len;
83 #if PROCESSOR_TEMPERATURE
84 static int cpu_temp_retry_counter = 0;
85 static int cpu_temp_retry_step = 1;
86 static int cpu_temp_retry_max = 1;
87 #endif /* PROCESSOR_TEMPERATURE */
88 /* #endif PROCESSOR_CPU_LOAD_INFO */
90 #elif defined(KERNEL_LINUX)
91 /* no variables needed */
92 /* #endif KERNEL_LINUX */
94 #elif defined(HAVE_LIBKSTAT)
95 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
96 # define MAX_NUMCPU 256
97 extern kstat_ctl_t *kc;
98 static kstat_t *ksp[MAX_NUMCPU];
99 static int numcpu;
100 /* #endif HAVE_LIBKSTAT */
102 #elif defined(HAVE_SYSCTLBYNAME)
103 static int numcpu;
104 #endif /* HAVE_SYSCTLBYNAME */
106 static int init (void)
107 {
108 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
109 kern_return_t status;
111 port_host = mach_host_self ();
113 /* FIXME: Free `cpu_list' if it's not NULL */
114 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
115 {
116 ERROR ("cpu plugin: host_processors returned %i", (int) status);
117 cpu_list_len = 0;
118 return (-1);
119 }
121 DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
122 INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
124 cpu_temp_retry_max = 86400 / interval_g;
125 /* #endif PROCESSOR_CPU_LOAD_INFO */
127 #elif defined(HAVE_LIBKSTAT)
128 kstat_t *ksp_chain;
130 numcpu = 0;
132 if (kc == NULL)
133 return (-1);
135 /* Solaris doesn't count linear.. *sigh* */
136 for (numcpu = 0, ksp_chain = kc->kc_chain;
137 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
138 ksp_chain = ksp_chain->ks_next)
139 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
140 ksp[numcpu++] = ksp_chain;
141 /* #endif HAVE_LIBKSTAT */
143 #elif defined (HAVE_SYSCTLBYNAME)
144 size_t numcpu_size;
146 numcpu_size = sizeof (numcpu);
148 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
149 {
150 char errbuf[1024];
151 WARNING ("cpu plugin: sysctlbyname: %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
153 return (-1);
154 }
156 if (numcpu != 1)
157 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
158 #endif
160 return (0);
161 } /* int init */
163 static void submit (int cpu_num, const char *type_instance, counter_t value)
164 {
165 value_t values[1];
166 value_list_t vl = VALUE_LIST_INIT;
168 values[0].counter = value;
170 vl.values = values;
171 vl.values_len = 1;
172 vl.time = time (NULL);
173 strcpy (vl.host, hostname_g);
174 strcpy (vl.plugin, "cpu");
175 snprintf (vl.plugin_instance, sizeof (vl.type_instance),
176 "%i", cpu_num);
177 vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
178 strcpy (vl.type_instance, type_instance);
180 plugin_dispatch_values ("cpu", &vl);
181 }
183 static int cpu_read (void)
184 {
185 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
186 int cpu;
188 kern_return_t status;
190 #if PROCESSOR_CPU_LOAD_INFO
191 processor_cpu_load_info_data_t cpu_info;
192 mach_msg_type_number_t cpu_info_len;
193 #endif
194 #if PROCESSOR_TEMPERATURE
195 processor_info_data_t cpu_temp;
196 mach_msg_type_number_t cpu_temp_len;
197 #endif
199 host_t cpu_host;
201 for (cpu = 0; cpu < cpu_list_len; cpu++)
202 {
203 #if PROCESSOR_CPU_LOAD_INFO
204 cpu_host = 0;
205 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
207 if ((status = processor_info (cpu_list[cpu],
208 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
209 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
210 {
211 ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
212 continue;
213 }
215 if (cpu_info_len < CPU_STATE_MAX)
216 {
217 ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
218 continue;
219 }
221 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
222 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
223 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
224 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
225 #endif /* PROCESSOR_CPU_LOAD_INFO */
226 #if PROCESSOR_TEMPERATURE
227 /*
228 * Not all Apple computers do have this ability. To minimize
229 * the messages sent to the syslog we do an exponential
230 * stepback if `processor_info' fails. We still try ~once a day
231 * though..
232 */
233 if (cpu_temp_retry_counter > 0)
234 {
235 cpu_temp_retry_counter--;
236 continue;
237 }
239 cpu_temp_len = PROCESSOR_INFO_MAX;
241 status = processor_info (cpu_list[cpu],
242 PROCESSOR_TEMPERATURE,
243 &cpu_host,
244 cpu_temp, &cpu_temp_len);
245 if (status != KERN_SUCCESS)
246 {
247 ERROR ("cpu plugin: processor_info failed: %s",
248 mach_error_string (status));
250 cpu_temp_retry_counter = cpu_temp_retry_step;
251 cpu_temp_retry_step *= 2;
252 if (cpu_temp_retry_step > cpu_temp_retry_max)
253 cpu_temp_retry_step = cpu_temp_retry_max;
255 continue;
256 }
258 if (cpu_temp_len != 1)
259 {
260 DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
261 (int) cpu_temp_len);
262 continue;
263 }
265 cpu_temp_retry_counter = 0;
266 cpu_temp_retry_step = 1;
268 DEBUG ("cpu_temp = %i", (int) cpu_temp);
269 #endif /* PROCESSOR_TEMPERATURE */
270 }
271 /* #endif PROCESSOR_CPU_LOAD_INFO */
273 #elif defined(KERNEL_LINUX)
274 int cpu;
275 counter_t user, nice, syst, idle;
276 counter_t wait, intr, sitr; /* sitr == soft interrupt */
277 FILE *fh;
278 char buf[1024];
280 char *fields[9];
281 int numfields;
283 static complain_t complain_obj;
285 if ((fh = fopen ("/proc/stat", "r")) == NULL)
286 {
287 char errbuf[1024];
288 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
289 "fopen (/proc/stat) failed: %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
291 return (-1);
292 }
294 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
295 "fopen (/proc/stat) succeeded.");
297 while (fgets (buf, 1024, fh) != NULL)
298 {
299 if (strncmp (buf, "cpu", 3))
300 continue;
301 if ((buf[3] < '0') || (buf[3] > '9'))
302 continue;
304 numfields = strsplit (buf, fields, 9);
305 if (numfields < 5)
306 continue;
308 cpu = atoi (fields[0] + 3);
309 user = atoll (fields[1]);
310 nice = atoll (fields[2]);
311 syst = atoll (fields[3]);
312 idle = atoll (fields[4]);
314 submit (cpu, "user", user);
315 submit (cpu, "nice", nice);
316 submit (cpu, "system", syst);
317 submit (cpu, "idle", idle);
319 if (numfields >= 8)
320 {
321 wait = atoll (fields[5]);
322 intr = atoll (fields[6]);
323 sitr = atoll (fields[7]);
325 submit (cpu, "wait", wait);
326 submit (cpu, "interrupt", intr);
327 submit (cpu, "softirq", sitr);
329 if (numfields >= 9)
330 submit (cpu, "steal", atoll (fields[8]));
331 }
332 }
334 fclose (fh);
335 /* #endif defined(KERNEL_LINUX) */
337 #elif defined(HAVE_LIBKSTAT)
338 int cpu;
339 counter_t user, syst, idle, wait;
340 static cpu_stat_t cs;
342 if (kc == NULL)
343 return;
345 for (cpu = 0; cpu < numcpu; cpu++)
346 {
347 if (kstat_read (kc, ksp[cpu], &cs) == -1)
348 continue; /* error message? */
350 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
351 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
352 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
353 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
355 submit (ksp[cpu]->ks_instance, "user", user);
356 submit (ksp[cpu]->ks_instance, "system", syst);
357 submit (ksp[cpu]->ks_instance, "idle", idle);
358 submit (ksp[cpu]->ks_instance, "wait", wait);
359 }
360 /* #endif defined(HAVE_LIBKSTAT) */
362 #elif defined(HAVE_SYSCTLBYNAME)
363 long cpuinfo[CPUSTATES];
364 size_t cpuinfo_size;
366 static complain_t complain_obj;
368 cpuinfo_size = sizeof (cpuinfo);
370 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
371 {
372 char errbuf[1024];
373 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
374 "sysctlbyname failed: %s.",
375 sstrerror (errno, errbuf, sizeof (errbuf)));
376 return (-1);
377 }
379 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
380 "sysctlbyname succeeded.");
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
390 return (0);
391 }
392 #endif /* CPU_HAVE_READ */
394 void module_register (void)
395 {
396 #if CPU_HAVE_READ
397 plugin_register_init ("cpu", init);
398 plugin_register_read ("cpu", cpu_read);
399 #endif /* CPU_HAVE_READ */
400 } /* void module_register */