1a4c62edd0afe8aca78477eb5c468de02067449b
1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005 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; either version 2 of the License, or (at your
8 * option) any later version.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "cpu"
29 #ifdef HAVE_MACH_KERN_RETURN_H
30 # include <mach/kern_return.h>
31 #endif
32 #ifdef HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
34 #endif
35 #ifdef HAVE_MACH_HOST_PRIV_H
36 # include <mach/host_priv.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
45 #ifdef HAVE_LIBKSTAT
46 # include <sys/sysinfo.h>
47 #endif /* HAVE_LIBKSTAT */
49 #ifdef HAVE_SYSCTLBYNAME
50 # ifdef HAVE_SYS_SYSCTL_H
51 # include <sys/sysctl.h>
52 # endif
54 # ifdef HAVE_SYS_DKSTAT_H
55 # include <sys/dkstat.h>
56 # endif
58 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
59 # define CP_USER 0
60 # define CP_NICE 1
61 # define CP_SYS 2
62 # define CP_INTR 3
63 # define CP_IDLE 4
64 # define CPUSTATES 5
65 # endif
66 #endif /* HAVE_SYSCTLBYNAME */
68 #if defined(PROCESSOR_CPU_LOAD_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_SYSCTLBYNAME)
69 # define CPU_HAVE_READ 1
70 #else
71 # define CPU_HAVE_READ 0
72 #endif
74 #ifdef PROCESSOR_CPU_LOAD_INFO
75 static mach_port_t port_host;
76 static processor_port_array_t cpu_list;
77 static mach_msg_type_number_t cpu_list_len;
78 #endif
80 #ifdef HAVE_LIBKSTAT
81 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
82 # define MAX_NUMCPU 256
83 extern kstat_ctl_t *kc;
84 static kstat_t *ksp[MAX_NUMCPU];
85 static int numcpu;
86 #endif /* HAVE_LIBKSTAT */
88 #ifdef HAVE_SYSCTLBYNAME
89 static int numcpu;
90 #endif /* HAVE_SYSCTLBYNAME */
92 static char *cpu_filename = "cpu-%s.rrd";
94 static char *ds_def[] =
95 {
96 "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:U",
97 "DS:nice:COUNTER:"COLLECTD_HEARTBEAT":0:U",
98 "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:U",
99 "DS:idle:COUNTER:"COLLECTD_HEARTBEAT":0:U",
100 "DS:wait:COUNTER:"COLLECTD_HEARTBEAT":0:U",
101 NULL
102 };
103 static int ds_num = 5;
105 static void cpu_init (void)
106 {
107 #ifdef PROCESSOR_CPU_LOAD_INFO
108 port_host = mach_host_self ();
110 if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
111 {
112 fprintf (stderr, "host_processors returned %i\n", (int) status);
113 cpu_list_len = 0;
114 return (-1);
115 }
117 DBG ("host_processors returned %i %s", (int) list_len, list_len == 1 ? "processor" : "processors");
118 syslog (LOG_INFO, "Plugin `cpu' found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
119 /* #endif PROCESSOR_CPU_LOAD_INFO */
121 #elif defined(HAVE_LIBKSTAT)
122 kstat_t *ksp_chain;
124 numcpu = 0;
126 if (kc == NULL)
127 return;
129 /* Solaris doesn't count linear.. *sigh* */
130 for (numcpu = 0, ksp_chain = kc->kc_chain;
131 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
132 ksp_chain = ksp_chain->ks_next)
133 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
134 ksp[numcpu++] = ksp_chain;
135 /* #endif HAVE_LIBKSTAT */
137 #elif defined (HAVE_SYSCTLBYNAME)
138 size_t numcpu_size;
140 numcpu_size = sizeof (numcpu);
142 if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
143 {
144 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
145 return;
146 }
148 if (numcpu != 1)
149 syslog (LOG_NOTICE, "cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
150 #endif
152 return;
153 }
155 static void cpu_write (char *host, char *inst, char *val)
156 {
157 char file[512];
158 int status;
160 status = snprintf (file, 512, cpu_filename, inst);
161 if (status < 1)
162 return;
163 else if (status >= 512)
164 return;
166 rrd_update_file (host, file, val, ds_def, ds_num);
167 }
169 #if CPU_HAVE_READ
170 #define BUFSIZE 512
171 static void cpu_submit (int cpu_num, unsigned long long user,
172 unsigned long long nice, unsigned long long syst,
173 unsigned long long idle, unsigned long long wait)
174 {
175 char buf[BUFSIZE];
176 char cpu[16];
178 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
179 user, nice, syst, idle, wait) >= BUFSIZE)
180 return;
181 snprintf (cpu, 16, "%i", cpu_num);
183 plugin_submit (MODULE_NAME, cpu, buf);
184 }
185 #undef BUFSIZE
187 static void cpu_read (void)
188 {
189 #ifdef PROCESSOR_CPU_LOAD_INFO
190 int cpu;
192 kern_return_t status;
194 processor_cpu_load_info_data_t cpu_info;
195 processor_cpu_load_info_t cpu_info_ptr;
196 mach_msg_type_number_t cpu_info_len;
198 host_t cpu_host;
200 for (cpu = 0; cpu < cpu_list_len; cpu++)
201 {
202 cpu_host = 0
203 cpu_info_ptr = &cpu_info;
204 cpu_info_len = sizeof (cpu_info);
206 if ((status = processor_info (list[i],
207 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
208 (processor_info_t) cpu_info_ptr, &cpu_info_len)) != KERN_SUCCESS)
209 {
210 syslog (LOG_ERR, "processor_info failed with status %i\n", (int) status);
211 continue;
212 }
214 if (cpu_info_len < CPU_STATE_MAX)
215 {
216 syslog (LOG_ERR, "processor_info returned only %i elements..\n", cpu_info_len);
217 continue;
218 }
220 cpu_submit (i, cpu_info.cpu_ticks[CPU_STATE_USER],
221 cpu_info.cpu_ticks[CPU_STATE_NICE],
222 cpu_info.cpu_ticks[CPU_STATE_SYSTEM],
223 cpu_info.cpu_ticks[CPU_STATE_IDLE],
224 0ULL);
225 }
226 /* #endif PROCESSOR_CPU_LOAD_INFO */
228 #elif defined(KERNEL_LINUX)
229 # define BUFSIZE 1024
230 int cpu;
231 unsigned long long user, nice, syst, idle;
232 unsigned long long wait, intr, sitr; /* sitr == soft interrupt */
233 FILE *fh;
234 char buf[BUFSIZE];
236 char *fields[9];
237 int numfields;
239 if ((fh = fopen ("/proc/stat", "r")) == NULL)
240 {
241 syslog (LOG_WARNING, "cpu: fopen: %s", strerror (errno));
242 return;
243 }
245 while (fgets (buf, BUFSIZE, fh) != NULL)
246 {
247 if (strncmp (buf, "cpu", 3))
248 continue;
249 if ((buf[3] < '0') || (buf[3] > '9'))
250 continue;
252 numfields = strsplit (buf, fields, 9);
253 if (numfields < 5)
254 continue;
256 cpu = atoi (fields[0] + 3);
257 user = atoll (fields[1]);
258 nice = atoll (fields[2]);
259 syst = atoll (fields[3]);
260 idle = atoll (fields[4]);
262 if (numfields >= 8)
263 {
264 wait = atoll (fields[5]);
265 intr = atoll (fields[6]);
266 sitr = atoll (fields[7]);
268 /* I doubt anyone cares about the time spent in
269 * interrupt handlers.. */
270 syst += intr + sitr;
271 }
272 else
273 {
274 wait = 0LL;
275 }
277 cpu_submit (cpu, user, nice, syst, idle, wait);
278 }
280 fclose (fh);
281 #undef BUFSIZE
282 /* #endif defined(KERNEL_LINUX) */
284 #elif defined(HAVE_LIBKSTAT)
285 int cpu;
286 unsigned long long user, syst, idle, wait;
287 static cpu_stat_t cs;
289 if (kc == NULL)
290 return;
292 for (cpu = 0; cpu < numcpu; cpu++)
293 {
294 if (kstat_read (kc, ksp[cpu], &cs) == -1)
295 continue; /* error message? */
297 idle = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_IDLE];
298 user = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_USER];
299 syst = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_KERNEL];
300 wait = (unsigned long long) cs.cpu_sysinfo.cpu[CPU_WAIT];
302 cpu_submit (ksp[cpu]->ks_instance,
303 user, 0LL, syst, idle, wait);
304 }
305 /* #endif defined(HAVE_LIBKSTAT) */
307 #elif defined(HAVE_SYSCTLBYNAME)
308 long cpuinfo[CPUSTATES];
309 size_t cpuinfo_size;
311 cpuinfo_size = sizeof (cpuinfo);
313 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
314 {
315 syslog (LOG_WARNING, "cpu: sysctlbyname: %s", strerror (errno));
316 return;
317 }
319 cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
321 /* FIXME: Instance is always `0' */
322 cpu_submit (0, cpuinfo[CP_USER], cpuinfo[CP_NICE], cpuinfo[CP_SYS], cpuinfo[CP_IDLE], 0LL);
323 #endif
325 return;
326 }
327 #else
328 # define cpu_read NULL
329 #endif /* CPU_HAVE_READ */
331 void module_register (void)
332 {
333 plugin_register (MODULE_NAME, cpu_init, cpu_read, cpu_write);
334 }
336 #undef MODULE_NAME