Let plugin_dispatch_values() set value_list.time in case of 'now'.
[collectd.git] / src / cpu.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
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
47
48 #ifdef HAVE_LIBKSTAT
49 # include <sys/sysinfo.h>
50 #endif /* HAVE_LIBKSTAT */
51
52 #ifdef HAVE_SYSCTLBYNAME
53 # ifdef HAVE_SYS_SYSCTL_H
54 #  include <sys/sysctl.h>
55 # endif
56
57 # ifdef HAVE_SYS_DKSTAT_H
58 #  include <sys/dkstat.h>
59 # endif
60
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 */
70
71 #if HAVE_STATGRAB_H
72 # include <statgrab.h>
73 #endif
74
75 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
76         && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
77 # error "No applicable input method."
78 #endif
79
80 #ifdef PROCESSOR_CPU_LOAD_INFO
81 static mach_port_t port_host;
82 static processor_port_array_t cpu_list;
83 static mach_msg_type_number_t cpu_list_len;
84
85 #if PROCESSOR_TEMPERATURE
86 static int cpu_temp_retry_counter = 0;
87 static int cpu_temp_retry_step    = 1;
88 static int cpu_temp_retry_max     = 1;
89 #endif /* PROCESSOR_TEMPERATURE */
90 /* #endif PROCESSOR_CPU_LOAD_INFO */
91
92 #elif defined(KERNEL_LINUX)
93 /* no variables needed */
94 /* #endif KERNEL_LINUX */
95
96 #elif defined(HAVE_LIBKSTAT)
97 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
98 # define MAX_NUMCPU 256
99 extern kstat_ctl_t *kc;
100 static kstat_t *ksp[MAX_NUMCPU];
101 static int numcpu;
102 /* #endif HAVE_LIBKSTAT */
103
104 #elif defined(HAVE_SYSCTLBYNAME)
105 static int numcpu;
106 /* #endif HAVE_SYSCTLBYNAME */
107
108 #elif defined(HAVE_LIBSTATGRAB)
109 /* no variables needed */
110 #endif /* HAVE_LIBSTATGRAB */
111
112 static int init (void)
113 {
114 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
115         kern_return_t status;
116
117         port_host = mach_host_self ();
118
119         /* FIXME: Free `cpu_list' if it's not NULL */
120         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
121         {
122                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
123                 cpu_list_len = 0;
124                 return (-1);
125         }
126
127         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
128         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
129
130         cpu_temp_retry_max = 86400 / interval_g;
131 /* #endif PROCESSOR_CPU_LOAD_INFO */
132
133 #elif defined(HAVE_LIBKSTAT)
134         kstat_t *ksp_chain;
135
136         numcpu = 0;
137
138         if (kc == NULL)
139                 return (-1);
140
141         /* Solaris doesn't count linear.. *sigh* */
142         for (numcpu = 0, ksp_chain = kc->kc_chain;
143                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
144                         ksp_chain = ksp_chain->ks_next)
145                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
146                         ksp[numcpu++] = ksp_chain;
147 /* #endif HAVE_LIBKSTAT */
148
149 #elif defined (HAVE_SYSCTLBYNAME)
150         size_t numcpu_size;
151
152         numcpu_size = sizeof (numcpu);
153
154         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
155         {
156                 char errbuf[1024];
157                 WARNING ("cpu plugin: sysctlbyname: %s",
158                                 sstrerror (errno, errbuf, sizeof (errbuf)));
159                 return (-1);
160         }
161
162         if (numcpu != 1)
163                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
164 /* #endif HAVE_SYSCTLBYNAME */
165
166 #elif defined(HAVE_LIBSTATGRAB)
167         /* nothing to initialize */
168 #endif /* HAVE_LIBSTATGRAB */
169
170         return (0);
171 } /* int init */
172
173 static void submit (int cpu_num, const char *type_instance, counter_t value)
174 {
175         value_t values[1];
176         value_list_t vl = VALUE_LIST_INIT;
177
178         values[0].counter = value;
179
180         vl.values = values;
181         vl.values_len = 1;
182         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
183         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
184         ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
185                         "%i", cpu_num);
186         sstrncpy (vl.type, "cpu", sizeof (vl.type));
187         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
188
189         plugin_dispatch_values (&vl);
190 }
191
192 static int cpu_read (void)
193 {
194 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
195         int cpu;
196
197         kern_return_t status;
198         
199 #if PROCESSOR_CPU_LOAD_INFO
200         processor_cpu_load_info_data_t cpu_info;
201         mach_msg_type_number_t         cpu_info_len;
202 #endif
203 #if PROCESSOR_TEMPERATURE
204         processor_info_data_t          cpu_temp;
205         mach_msg_type_number_t         cpu_temp_len;
206 #endif
207
208         host_t cpu_host;
209
210         for (cpu = 0; cpu < cpu_list_len; cpu++)
211         {
212 #if PROCESSOR_CPU_LOAD_INFO
213                 cpu_host = 0;
214                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
215
216                 if ((status = processor_info (cpu_list[cpu],
217                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
218                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
219                 {
220                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
221                         continue;
222                 }
223
224                 if (cpu_info_len < CPU_STATE_MAX)
225                 {
226                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
227                         continue;
228                 }
229
230                 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
231                 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
232                 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
233                 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
234 #endif /* PROCESSOR_CPU_LOAD_INFO */
235 #if PROCESSOR_TEMPERATURE
236                 /*
237                  * Not all Apple computers do have this ability. To minimize
238                  * the messages sent to the syslog we do an exponential
239                  * stepback if `processor_info' fails. We still try ~once a day
240                  * though..
241                  */
242                 if (cpu_temp_retry_counter > 0)
243                 {
244                         cpu_temp_retry_counter--;
245                         continue;
246                 }
247
248                 cpu_temp_len = PROCESSOR_INFO_MAX;
249
250                 status = processor_info (cpu_list[cpu],
251                                 PROCESSOR_TEMPERATURE,
252                                 &cpu_host,
253                                 cpu_temp, &cpu_temp_len);
254                 if (status != KERN_SUCCESS)
255                 {
256                         ERROR ("cpu plugin: processor_info failed: %s",
257                                         mach_error_string (status));
258
259                         cpu_temp_retry_counter = cpu_temp_retry_step;
260                         cpu_temp_retry_step *= 2;
261                         if (cpu_temp_retry_step > cpu_temp_retry_max)
262                                 cpu_temp_retry_step = cpu_temp_retry_max;
263
264                         continue;
265                 }
266
267                 if (cpu_temp_len != 1)
268                 {
269                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
270                                         (int) cpu_temp_len);
271                         continue;
272                 }
273
274                 cpu_temp_retry_counter = 0;
275                 cpu_temp_retry_step    = 1;
276
277                 DEBUG ("cpu_temp = %i", (int) cpu_temp);
278 #endif /* PROCESSOR_TEMPERATURE */
279         }
280 /* #endif PROCESSOR_CPU_LOAD_INFO */
281
282 #elif defined(KERNEL_LINUX)
283         int cpu;
284         counter_t user, nice, syst, idle;
285         counter_t wait, intr, sitr; /* sitr == soft interrupt */
286         FILE *fh;
287         char buf[1024];
288
289         char *fields[9];
290         int numfields;
291
292         if ((fh = fopen ("/proc/stat", "r")) == NULL)
293         {
294                 char errbuf[1024];
295                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
296                                 sstrerror (errno, errbuf, sizeof (errbuf)));
297                 return (-1);
298         }
299
300         while (fgets (buf, 1024, fh) != NULL)
301         {
302                 if (strncmp (buf, "cpu", 3))
303                         continue;
304                 if ((buf[3] < '0') || (buf[3] > '9'))
305                         continue;
306
307                 numfields = strsplit (buf, fields, 9);
308                 if (numfields < 5)
309                         continue;
310
311                 cpu = atoi (fields[0] + 3);
312                 user = atoll (fields[1]);
313                 nice = atoll (fields[2]);
314                 syst = atoll (fields[3]);
315                 idle = atoll (fields[4]);
316
317                 submit (cpu, "user", user);
318                 submit (cpu, "nice", nice);
319                 submit (cpu, "system", syst);
320                 submit (cpu, "idle", idle);
321
322                 if (numfields >= 8)
323                 {
324                         wait = atoll (fields[5]);
325                         intr = atoll (fields[6]);
326                         sitr = atoll (fields[7]);
327
328                         submit (cpu, "wait", wait);
329                         submit (cpu, "interrupt", intr);
330                         submit (cpu, "softirq", sitr);
331
332                         if (numfields >= 9)
333                                 submit (cpu, "steal", atoll (fields[8]));
334                 }
335         }
336
337         fclose (fh);
338 /* #endif defined(KERNEL_LINUX) */
339
340 #elif defined(HAVE_LIBKSTAT)
341         int cpu;
342         counter_t user, syst, idle, wait;
343         static cpu_stat_t cs;
344
345         if (kc == NULL)
346                 return (-1);
347
348         for (cpu = 0; cpu < numcpu; cpu++)
349         {
350                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
351                         continue; /* error message? */
352
353                 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
354                 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
355                 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
356                 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
357
358                 submit (ksp[cpu]->ks_instance, "user", user);
359                 submit (ksp[cpu]->ks_instance, "system", syst);
360                 submit (ksp[cpu]->ks_instance, "idle", idle);
361                 submit (ksp[cpu]->ks_instance, "wait", wait);
362         }
363 /* #endif defined(HAVE_LIBKSTAT) */
364
365 #elif defined(HAVE_SYSCTLBYNAME)
366         long cpuinfo[CPUSTATES];
367         size_t cpuinfo_size;
368
369         cpuinfo_size = sizeof (cpuinfo);
370
371         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
372         {
373                 char errbuf[1024];
374                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
375                                 sstrerror (errno, errbuf, sizeof (errbuf)));
376                 return (-1);
377         }
378
379         cpuinfo[CP_SYS] += cpuinfo[CP_INTR];
380
381         submit (0, "user", cpuinfo[CP_USER]);
382         submit (0, "nice", cpuinfo[CP_NICE]);
383         submit (0, "system", cpuinfo[CP_SYS]);
384         submit (0, "idle", cpuinfo[CP_IDLE]);
385 /* #endif HAVE_SYSCTLBYNAME */
386
387 #elif defined(HAVE_LIBSTATGRAB)
388        sg_cpu_stats *cs;
389        cs = sg_get_cpu_stats ();
390
391        if (cs == NULL)
392        {
393                ERROR ("cpu plugin: sg_get_cpu_stats failed.");
394                return (-1);
395        }
396
397        submit (0, "idle",   (counter_t) cs->idle);
398        submit (0, "nice",   (counter_t) cs->nice);
399        submit (0, "swap",   (counter_t) cs->swap);
400        submit (0, "system", (counter_t) cs->kernel);
401        submit (0, "user",   (counter_t) cs->user);
402        submit (0, "wait",   (counter_t) cs->iowait);
403 #endif /* HAVE_LIBSTATGRAB */
404
405         return (0);
406 }
407
408 void module_register (void)
409 {
410         plugin_register_init ("cpu", init);
411         plugin_register_read ("cpu", cpu_read);
412 } /* void module_register */