d9453aa31f95d403870f343d642636b487917ad6
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"
25 #include "utils_debug.h"
27 #ifdef HAVE_MACH_KERN_RETURN_H
28 # include <mach/kern_return.h>
29 #endif
30 #ifdef HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
32 #endif
33 #ifdef HAVE_MACH_HOST_PRIV_H
34 # include <mach/host_priv.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
38 #endif
39 #ifdef HAVE_MACH_PROCESSOR_INFO_H
40 # include <mach/processor_info.h>
41 #endif
42 #ifdef HAVE_MACH_PROCESSOR_H
43 # include <mach/processor.h>
44 #endif
45 #ifdef HAVE_MACH_VM_MAP_H
46 # include <mach/vm_map.h>
47 #endif
49 #ifdef HAVE_LIBKSTAT
50 # include <sys/sysinfo.h>
51 #endif /* HAVE_LIBKSTAT */
53 #ifdef HAVE_SYSCTLBYNAME
54 # ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
56 # endif
58 # ifdef HAVE_SYS_DKSTAT_H
59 # include <sys/dkstat.h>
60 # endif
62 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
63 # define CP_USER 0
64 # define CP_NICE 1
65 # define CP_SYS 2
66 # define CP_INTR 3
67 # define CP_IDLE 4
68 # define CPUSTATES 5
69 # endif
70 #endif /* HAVE_SYSCTLBYNAME */
72 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
73 # define CPU_HAVE_READ 1
74 #else
75 # define CPU_HAVE_READ 0
76 #endif
78 static data_source_t dsrc[5] =
79 {
80 {"user", DS_TYPE_COUNTER, 0, 4294967295.0},
81 {"nice", DS_TYPE_COUNTER, 0, 4294967295.0},
82 {"syst", DS_TYPE_COUNTER, 0, 4294967295.0},
83 {"idle", DS_TYPE_COUNTER, 0, 4294967295.0},
84 {"wait", DS_TYPE_COUNTER, 0, 4294967295.0}
85 };
87 static data_set_t ds =
88 {
89 "cpu", 5, dsrc
90 };
92 #if CPU_HAVE_READ
93 #ifdef PROCESSOR_CPU_LOAD_INFO
94 static mach_port_t port_host;
95 static processor_port_array_t cpu_list;
96 static mach_msg_type_number_t cpu_list_len;
98 #if PROCESSOR_TEMPERATURE
99 static int cpu_temp_retry_counter = 0;
100 static int cpu_temp_retry_step = 1;
101 static int cpu_temp_retry_max = 1;
102 #endif /* PROCESSOR_TEMPERATURE */
103 /* #endif PROCESSOR_CPU_LOAD_INFO */
105 #elif defined(KERNEL_LINUX)
106 /* no variables needed */
107 /* #endif KERNEL_LINUX */
109 #elif defined(HAVE_LIBKSTAT)
110 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
111 # define MAX_NUMCPU 256
112 extern kstat_ctl_t *kc;
113 static kstat_t *ksp[MAX_NUMCPU];
114 static int numcpu;
115 /* #endif HAVE_LIBKSTAT */
117 #elif defined(HAVE_SYSCTLBYNAME)
118 static int numcpu;
119 #endif /* HAVE_SYSCTLBYNAME */
121 static int init (void)
122 {
123 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
124 kern_return_t status;
125 int collectd_step;
127 port_host = mach_host_self ();
129 /* FIXME: Free `cpu_list' if it's not NULL */
130 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
131 {
132 syslog (LOG_ERR, "cpu plugin: host_processors returned %i\n", (int) status);
133 cpu_list_len = 0;
134 return (-1);
135 }
137 DBG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
138 syslog (LOG_INFO, "cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
140 collectd_step = atoi (COLLECTD_STEP);
141 if ((collectd_step > 0) && (collectd_step <= 86400))
142 cpu_temp_retry_max = 86400 / collectd_step;
144 /* #endif PROCESSOR_CPU_LOAD_INFO */
146 #elif defined(HAVE_LIBKSTAT)
147 kstat_t *ksp_chain;
149 numcpu = 0;
151 if (kc == NULL)
152 return (-1);
154 /* Solaris doesn't count linear.. *sigh* */
155 for (numcpu = 0, ksp_chain = kc->kc_chain;
156 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
157 ksp_chain = ksp_chain->ks_next)
158 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
159 ksp[numcpu++] = ksp_chain;
160 /* #endif HAVE_LIBKSTAT */
162 #elif defined (HAVE_SYSCTLBYNAME)
163 size_t numcpu_size;
165 numcpu_size = sizeof (numcpu);
167 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
168 {
169 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
170 return (-1);
171 }
173 if (numcpu != 1)
174 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
175 #endif
177 return (0);
178 } /* int init */
180 static void submit (int cpu_num, unsigned long long user,
181 unsigned long long nice, unsigned long long syst,
182 unsigned long long idle, unsigned long long wait)
183 {
184 value_t values[5];
185 value_list_t vl = VALUE_LIST_INIT;
187 values[0].counter = user;
188 values[1].counter = nice;
189 values[2].counter = syst;
190 values[3].counter = idle;
191 values[4].counter = wait;
193 vl.values = values;
194 vl.values_len = 2;
195 vl.time = time (NULL);
196 strcpy (vl.host, hostname);
197 strcpy (vl.plugin, "cpu");
198 strcpy (vl.plugin_instance, "");
199 snprintf (vl.type_instance, sizeof (vl.type_instance),
200 "%i", cpu_num);
201 vl.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
203 plugin_dispatch_values ("cpu", &vl);
204 }
206 static int cpu_read (void)
207 {
208 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
209 int cpu;
211 kern_return_t status;
213 #if PROCESSOR_CPU_LOAD_INFO
214 processor_cpu_load_info_data_t cpu_info;
215 mach_msg_type_number_t cpu_info_len;
216 #endif
217 #if PROCESSOR_TEMPERATURE
218 processor_info_data_t cpu_temp;
219 mach_msg_type_number_t cpu_temp_len;
220 #endif
222 host_t cpu_host;
224 for (cpu = 0; cpu < cpu_list_len; cpu++)
225 {
226 #if PROCESSOR_CPU_LOAD_INFO
227 cpu_host = 0;
228 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
230 if ((status = processor_info (cpu_list[cpu],
231 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
232 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
233 {
234 syslog (LOG_ERR, "cpu plugin: processor_info failed with status %i", (int) status);
235 continue;
236 }
238 if (cpu_info_len < CPU_STATE_MAX)
239 {
240 syslog (LOG_ERR, "cpu plugin: processor_info returned only %i elements..", cpu_info_len);
241 continue;
242 }
244 submit (cpu, cpu_info.cpu_ticks[CPU_STATE_USER],
245 cpu_info.cpu_ticks[CPU_STATE_NICE],
246 cpu_info.cpu_ticks[CPU_STATE_SYSTEM],
247 cpu_info.cpu_ticks[CPU_STATE_IDLE],
248 0ULL);
249 #endif /* PROCESSOR_CPU_LOAD_INFO */
250 #if PROCESSOR_TEMPERATURE
251 /*
252 * Not all Apple computers do have this ability. To minimize
253 * the messages sent to the syslog we do an exponential
254 * stepback if `processor_info' fails. We still try ~once a day
255 * though..
256 */
257 if (cpu_temp_retry_counter > 0)
258 {
259 cpu_temp_retry_counter--;
260 continue;
261 }
263 cpu_temp_len = PROCESSOR_INFO_MAX;
265 status = processor_info (cpu_list[cpu],
266 PROCESSOR_TEMPERATURE,
267 &cpu_host,
268 cpu_temp, &cpu_temp_len);
269 if (status != KERN_SUCCESS)
270 {
271 syslog (LOG_ERR, "cpu plugin: processor_info failed: %s",
272 mach_error_string (status));
274 cpu_temp_retry_counter = cpu_temp_retry_step;
275 cpu_temp_retry_step *= 2;
276 if (cpu_temp_retry_step > cpu_temp_retry_max)
277 cpu_temp_retry_step = cpu_temp_retry_max;
279 continue;
280 }
282 if (cpu_temp_len != 1)
283 {
284 DBG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
285 (int) cpu_temp_len);
286 continue;
287 }
289 cpu_temp_retry_counter = 0;
290 cpu_temp_retry_step = 1;
292 DBG ("cpu_temp = %i", (int) cpu_temp);
293 #endif /* PROCESSOR_TEMPERATURE */
294 }
295 /* #endif PROCESSOR_CPU_LOAD_INFO */
297 #elif defined(KERNEL_LINUX)
298 int cpu;
299 unsigned long long user, nice, syst, idle;
300 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
301 FILE *fh;
302 char buf[1024];
304 char *fields[9];
305 int numfields;
307 static complain_t complain_obj;
309 if ((fh = fopen ("/proc/stat", "r")) == NULL)
310 {
311 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
312 "fopen (/proc/stat) failed: %s",
313 strerror (errno));
314 return (-1);
315 }
317 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
318 "fopen (/proc/stat) succeeded.");
320 while (fgets (buf, 1024, fh) != NULL)
321 {
322 if (strncmp (buf, "cpu", 3))
323 continue;
324 if ((buf[3] < '0') || (buf[3] > '9'))
325 continue;
327 numfields = strsplit (buf, fields, 9);
328 if (numfields < 5)
329 continue;
331 cpu = atoi (fields[0] + 3);
332 user = atoll (fields[1]);
333 nice = atoll (fields[2]);
334 syst = atoll (fields[3]);
335 idle = atoll (fields[4]);
337 if (numfields >= 8)
338 {
339 wait = atoll (fields[5]);
340 intr = atoll (fields[6]);
341 sitr = atoll (fields[7]);
343 /* I doubt anyone cares about the time spent in
344 * interrupt handlers.. */
345 syst += intr + sitr;
346 }
347 else
348 {
349 wait = 0LL;
350 }
352 submit (cpu, user, nice, syst, idle, wait);
353 }
355 fclose (fh);
356 /* #endif defined(KERNEL_LINUX) */
358 #elif defined(HAVE_LIBKSTAT)
359 int cpu;
360 unsigned long long user, syst, idle, wait;
361 static cpu_stat_t cs;
363 if (kc == NULL)
364 return;
366 for (cpu = 0; cpu < numcpu; cpu++)
367 {
368 if (kstat_read (kc, ksp[cpu], &cs) == -1)
369 continue; /* error message? */
371 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
372 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
373 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
374 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
376 submit (ksp[cpu]->ks_instance,
377 user, 0LL, syst, idle, wait);
378 }
379 /* #endif defined(HAVE_LIBKSTAT) */
381 #elif defined(HAVE_SYSCTLBYNAME)
382 long cpuinfo[CPUSTATES];
383 size_t cpuinfo_size;
385 static complain_t complain_obj;
387 cpuinfo_size = sizeof (cpuinfo);
389 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
390 {
391 plugin_complain (LOG_ERR, &complain_obj, "cpu plugin: "
392 "sysctlbyname failed: %s.",
393 strerror (errno));
394 return;
395 }
397 plugin_relief (LOG_NOTICE, &complain_obj, "cpu plugin: "
398 "sysctlbyname succeeded.");
400 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
402 /* FIXME: Instance is always `0' */
403 submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
404 #endif
406 return (0);
407 }
408 #endif /* CPU_HAVE_READ */
410 void module_register (void)
411 {
412 plugin_register_data_set (&ds);
414 #if CPU_HAVE_READ
415 plugin_register_init ("cpu", init);
416 plugin_register_read ("cpu", cpu_read);
417 #endif /* CPU_HAVE_READ */
418 }