src/libcollectdclient/: Check if EILSEQ is defined.
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2009  Florian octo Forster
4  * Copyright (C) 2009 Simon Kuhnle
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
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  *   Simon Kuhnle <simon at blarzwurst.de>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #ifdef HAVE_MACH_KERN_RETURN_H
29 # include <mach/kern_return.h>
30 #endif
31 #ifdef HAVE_MACH_MACH_INIT_H
32 # include <mach/mach_init.h>
33 #endif
34 #ifdef HAVE_MACH_HOST_PRIV_H
35 # include <mach/host_priv.h>
36 #endif
37 #if HAVE_MACH_MACH_ERROR_H
38 #  include <mach/mach_error.h>
39 #endif
40 #ifdef HAVE_MACH_PROCESSOR_INFO_H
41 # include <mach/processor_info.h>
42 #endif
43 #ifdef HAVE_MACH_PROCESSOR_H
44 # include <mach/processor.h>
45 #endif
46 #ifdef HAVE_MACH_VM_MAP_H
47 # include <mach/vm_map.h>
48 #endif
49
50 #ifdef HAVE_LIBKSTAT
51 # include <sys/sysinfo.h>
52 #endif /* HAVE_LIBKSTAT */
53
54 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
55         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
56 # ifdef HAVE_SYS_SYSCTL_H
57 #  include <sys/sysctl.h>
58 # endif
59
60 # ifdef HAVE_SYS_DKSTAT_H
61 #  include <sys/dkstat.h>
62 # endif
63
64 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
65 #  define CP_USER   0
66 #  define CP_NICE   1
67 #  define CP_SYS    2
68 #  define CP_INTR   3
69 #  define CP_IDLE   4
70 #  define CPUSTATES 5
71 # endif
72 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
73
74 #if HAVE_SYSCTL
75 # if defined(CTL_HW) && defined(HW_NCPU) \
76         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
77 #  define CAN_USE_SYSCTL 1
78 # else
79 #  define CAN_USE_SYSCTL 0
80 # endif
81 #else
82 # define CAN_USE_SYSCTL 0
83 #endif
84
85 #if HAVE_STATGRAB_H
86 # include <statgrab.h>
87 #endif
88
89 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
90         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
91 # error "No applicable input method."
92 #endif
93
94 #ifdef PROCESSOR_CPU_LOAD_INFO
95 static mach_port_t port_host;
96 static processor_port_array_t cpu_list;
97 static mach_msg_type_number_t cpu_list_len;
98
99 #if PROCESSOR_TEMPERATURE
100 static int cpu_temp_retry_counter = 0;
101 static int cpu_temp_retry_step    = 1;
102 static int cpu_temp_retry_max     = 1;
103 #endif /* PROCESSOR_TEMPERATURE */
104 /* #endif PROCESSOR_CPU_LOAD_INFO */
105
106 #elif defined(KERNEL_LINUX)
107 /* no variables needed */
108 /* #endif KERNEL_LINUX */
109
110 #elif defined(HAVE_LIBKSTAT)
111 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
112 # define MAX_NUMCPU 256
113 extern kstat_ctl_t *kc;
114 static kstat_t *ksp[MAX_NUMCPU];
115 static int numcpu;
116 /* #endif HAVE_LIBKSTAT */
117
118 #elif CAN_USE_SYSCTL
119 static int numcpu;
120 /* #endif CAN_USE_SYSCTL */
121
122 #elif defined(HAVE_SYSCTLBYNAME)
123 static int numcpu;
124 /* #endif HAVE_SYSCTLBYNAME */
125
126 #elif defined(HAVE_LIBSTATGRAB)
127 /* no variables needed */
128 #endif /* HAVE_LIBSTATGRAB */
129
130 static int init (void)
131 {
132 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
133         kern_return_t status;
134
135         port_host = mach_host_self ();
136
137         /* FIXME: Free `cpu_list' if it's not NULL */
138         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
139         {
140                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
141                 cpu_list_len = 0;
142                 return (-1);
143         }
144
145         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
146         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
147
148         cpu_temp_retry_max = 86400 / interval_g;
149 /* #endif PROCESSOR_CPU_LOAD_INFO */
150
151 #elif defined(HAVE_LIBKSTAT)
152         kstat_t *ksp_chain;
153
154         numcpu = 0;
155
156         if (kc == NULL)
157                 return (-1);
158
159         /* Solaris doesn't count linear.. *sigh* */
160         for (numcpu = 0, ksp_chain = kc->kc_chain;
161                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
162                         ksp_chain = ksp_chain->ks_next)
163                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
164                         ksp[numcpu++] = ksp_chain;
165 /* #endif HAVE_LIBKSTAT */
166
167 #elif CAN_USE_SYSCTL
168         size_t numcpu_size;
169         int mib[2] = {CTL_HW, HW_NCPU};
170         int status;
171
172         numcpu = 0;
173         numcpu_size = sizeof (numcpu);
174
175         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
176                         &numcpu, &numcpu_size, NULL, 0);
177         if (status == -1)
178         {
179                 char errbuf[1024];
180                 WARNING ("cpu plugin: sysctl: %s",
181                                 sstrerror (errno, errbuf, sizeof (errbuf)));
182                 return (-1);
183         }
184 /* #endif CAN_USE_SYSCTL */
185
186 #elif defined (HAVE_SYSCTLBYNAME)
187         size_t numcpu_size;
188
189         numcpu_size = sizeof (numcpu);
190
191         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
192         {
193                 char errbuf[1024];
194                 WARNING ("cpu plugin: sysctlbyname: %s",
195                                 sstrerror (errno, errbuf, sizeof (errbuf)));
196                 return (-1);
197         }
198
199         if (numcpu != 1)
200                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
201 /* #endif HAVE_SYSCTLBYNAME */
202
203 #elif defined(HAVE_LIBSTATGRAB)
204         /* nothing to initialize */
205 #endif /* HAVE_LIBSTATGRAB */
206
207         return (0);
208 } /* int init */
209
210 static void submit (int cpu_num, const char *type_instance, counter_t value)
211 {
212         value_t values[1];
213         value_list_t vl = VALUE_LIST_INIT;
214
215         values[0].counter = value;
216
217         vl.values = values;
218         vl.values_len = 1;
219         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
220         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
221         ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
222                         "%i", cpu_num);
223         sstrncpy (vl.type, "cpu", sizeof (vl.type));
224         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
225
226         plugin_dispatch_values (&vl);
227 }
228
229 static int cpu_read (void)
230 {
231 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
232         int cpu;
233
234         kern_return_t status;
235         
236 #if PROCESSOR_CPU_LOAD_INFO
237         processor_cpu_load_info_data_t cpu_info;
238         mach_msg_type_number_t         cpu_info_len;
239 #endif
240 #if PROCESSOR_TEMPERATURE
241         processor_info_data_t          cpu_temp;
242         mach_msg_type_number_t         cpu_temp_len;
243 #endif
244
245         host_t cpu_host;
246
247         for (cpu = 0; cpu < cpu_list_len; cpu++)
248         {
249 #if PROCESSOR_CPU_LOAD_INFO
250                 cpu_host = 0;
251                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
252
253                 if ((status = processor_info (cpu_list[cpu],
254                                                 PROCESSOR_CPU_LOAD_INFO, &cpu_host,
255                                                 (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS)
256                 {
257                         ERROR ("cpu plugin: processor_info failed with status %i", (int) status);
258                         continue;
259                 }
260
261                 if (cpu_info_len < CPU_STATE_MAX)
262                 {
263                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
264                         continue;
265                 }
266
267                 submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
268                 submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
269                 submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
270                 submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
271 #endif /* PROCESSOR_CPU_LOAD_INFO */
272 #if PROCESSOR_TEMPERATURE
273                 /*
274                  * Not all Apple computers do have this ability. To minimize
275                  * the messages sent to the syslog we do an exponential
276                  * stepback if `processor_info' fails. We still try ~once a day
277                  * though..
278                  */
279                 if (cpu_temp_retry_counter > 0)
280                 {
281                         cpu_temp_retry_counter--;
282                         continue;
283                 }
284
285                 cpu_temp_len = PROCESSOR_INFO_MAX;
286
287                 status = processor_info (cpu_list[cpu],
288                                 PROCESSOR_TEMPERATURE,
289                                 &cpu_host,
290                                 cpu_temp, &cpu_temp_len);
291                 if (status != KERN_SUCCESS)
292                 {
293                         ERROR ("cpu plugin: processor_info failed: %s",
294                                         mach_error_string (status));
295
296                         cpu_temp_retry_counter = cpu_temp_retry_step;
297                         cpu_temp_retry_step *= 2;
298                         if (cpu_temp_retry_step > cpu_temp_retry_max)
299                                 cpu_temp_retry_step = cpu_temp_retry_max;
300
301                         continue;
302                 }
303
304                 if (cpu_temp_len != 1)
305                 {
306                         DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?",
307                                         (int) cpu_temp_len);
308                         continue;
309                 }
310
311                 cpu_temp_retry_counter = 0;
312                 cpu_temp_retry_step    = 1;
313
314                 DEBUG ("cpu_temp = %i", (int) cpu_temp);
315 #endif /* PROCESSOR_TEMPERATURE */
316         }
317 /* #endif PROCESSOR_CPU_LOAD_INFO */
318
319 #elif defined(KERNEL_LINUX)
320         int cpu;
321         counter_t user, nice, syst, idle;
322         counter_t wait, intr, sitr; /* sitr == soft interrupt */
323         FILE *fh;
324         char buf[1024];
325
326         char *fields[9];
327         int numfields;
328
329         if ((fh = fopen ("/proc/stat", "r")) == NULL)
330         {
331                 char errbuf[1024];
332                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
333                                 sstrerror (errno, errbuf, sizeof (errbuf)));
334                 return (-1);
335         }
336
337         while (fgets (buf, 1024, fh) != NULL)
338         {
339                 if (strncmp (buf, "cpu", 3))
340                         continue;
341                 if ((buf[3] < '0') || (buf[3] > '9'))
342                         continue;
343
344                 numfields = strsplit (buf, fields, 9);
345                 if (numfields < 5)
346                         continue;
347
348                 cpu = atoi (fields[0] + 3);
349                 user = atoll (fields[1]);
350                 nice = atoll (fields[2]);
351                 syst = atoll (fields[3]);
352                 idle = atoll (fields[4]);
353
354                 submit (cpu, "user", user);
355                 submit (cpu, "nice", nice);
356                 submit (cpu, "system", syst);
357                 submit (cpu, "idle", idle);
358
359                 if (numfields >= 8)
360                 {
361                         wait = atoll (fields[5]);
362                         intr = atoll (fields[6]);
363                         sitr = atoll (fields[7]);
364
365                         submit (cpu, "wait", wait);
366                         submit (cpu, "interrupt", intr);
367                         submit (cpu, "softirq", sitr);
368
369                         if (numfields >= 9)
370                                 submit (cpu, "steal", atoll (fields[8]));
371                 }
372         }
373
374         fclose (fh);
375 /* #endif defined(KERNEL_LINUX) */
376
377 #elif defined(HAVE_LIBKSTAT)
378         int cpu;
379         counter_t user, syst, idle, wait;
380         static cpu_stat_t cs;
381
382         if (kc == NULL)
383                 return (-1);
384
385         for (cpu = 0; cpu < numcpu; cpu++)
386         {
387                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
388                         continue; /* error message? */
389
390                 idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
391                 user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER];
392                 syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
393                 wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
394
395                 submit (ksp[cpu]->ks_instance, "user", user);
396                 submit (ksp[cpu]->ks_instance, "system", syst);
397                 submit (ksp[cpu]->ks_instance, "idle", idle);
398                 submit (ksp[cpu]->ks_instance, "wait", wait);
399         }
400 /* #endif defined(HAVE_LIBKSTAT) */
401
402 #elif CAN_USE_SYSCTL
403         uint64_t cpuinfo[numcpu][CPUSTATES];
404         size_t cpuinfo_size;
405         int status;
406         int i;
407
408         if (numcpu < 1)
409         {
410                 ERROR ("cpu plugin: Could not determine number of "
411                                 "installed CPUs using sysctl(3).");
412                 return (-1);
413         }
414
415         memset (cpuinfo, 0, sizeof (cpuinfo));
416
417 #if defined(KERN_CPTIME2)
418         if (numcpu > 1) {
419                 for (i = 0; i < numcpu; i++) {
420                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
421
422                         cpuinfo_size = sizeof (cpuinfo[0]);
423
424                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
425                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
426                         if (status == -1) {
427                                 char errbuf[1024];
428                                 ERROR ("cpu plugin: sysctl failed: %s.",
429                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
430                                 return (-1);
431                         }
432                 }
433         }
434         else
435 #endif /* defined(KERN_CPTIME2) */
436         {
437                 int mib[] = {CTL_KERN, KERN_CPTIME};
438                 long cpuinfo_tmp[CPUSTATES];
439
440                 cpuinfo_size = sizeof(cpuinfo_tmp);
441
442                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
443                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
444                 if (status == -1)
445                 {
446                         char errbuf[1024];
447                         ERROR ("cpu plugin: sysctl failed: %s.",
448                                         sstrerror (errno, errbuf, sizeof (errbuf)));
449                         return (-1);
450                 }
451
452                 for(i = 0; i < CPUSTATES; i++) {
453                         cpuinfo[0][i] = cpuinfo_tmp[i];
454                 }
455         }
456
457         for (i = 0; i < numcpu; i++) {
458                 submit (i, "user",      cpuinfo[i][CP_USER]);
459                 submit (i, "nice",      cpuinfo[i][CP_NICE]);
460                 submit (i, "system",    cpuinfo[i][CP_SYS]);
461                 submit (i, "idle",      cpuinfo[i][CP_IDLE]);
462                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
463         }
464 /* #endif CAN_USE_SYSCTL */
465
466 #elif defined(HAVE_SYSCTLBYNAME)
467         long cpuinfo[CPUSTATES];
468         size_t cpuinfo_size;
469
470         cpuinfo_size = sizeof (cpuinfo);
471
472         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
473         {
474                 char errbuf[1024];
475                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
476                                 sstrerror (errno, errbuf, sizeof (errbuf)));
477                 return (-1);
478         }
479
480         submit (0, "user", cpuinfo[CP_USER]);
481         submit (0, "nice", cpuinfo[CP_NICE]);
482         submit (0, "system", cpuinfo[CP_SYS]);
483         submit (0, "idle", cpuinfo[CP_IDLE]);
484         submit (0, "interrupt", cpuinfo[CP_INTR]);
485 /* #endif HAVE_SYSCTLBYNAME */
486
487 #elif defined(HAVE_LIBSTATGRAB)
488         sg_cpu_stats *cs;
489         cs = sg_get_cpu_stats ();
490
491         if (cs == NULL)
492         {
493                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
494                 return (-1);
495         }
496
497         submit (0, "idle",   (counter_t) cs->idle);
498         submit (0, "nice",   (counter_t) cs->nice);
499         submit (0, "swap",   (counter_t) cs->swap);
500         submit (0, "system", (counter_t) cs->kernel);
501         submit (0, "user",   (counter_t) cs->user);
502         submit (0, "wait",   (counter_t) cs->iowait);
503 #endif /* HAVE_LIBSTATGRAB */
504
505         return (0);
506 }
507
508 void module_register (void)
509 {
510         plugin_register_init ("cpu", init);
511         plugin_register_read ("cpu", cpu_read);
512 } /* void module_register */