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