Merge branch 'collectd-5.3' into collectd-5.4
[collectd.git] / src / cpu.c
1 /**
2  * collectd - src/cpu.c
3  * Copyright (C) 2005-2010  Florian octo Forster
4  * Copyright (C) 2008       Oleg King
5  * Copyright (C) 2009       Simon Kuhnle
6  * Copyright (C) 2009       Manuel Sanmartin
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at verplant.org>
23  *   Oleg King <king2 at kaluga.ru>
24  *   Simon Kuhnle <simon at blarzwurst.de>
25  *   Manuel Sanmartin
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31
32 #ifdef HAVE_MACH_KERN_RETURN_H
33 # include <mach/kern_return.h>
34 #endif
35 #ifdef HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
37 #endif
38 #ifdef HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
40 #endif
41 #if HAVE_MACH_MACH_ERROR_H
42 #  include <mach/mach_error.h>
43 #endif
44 #ifdef HAVE_MACH_PROCESSOR_INFO_H
45 # include <mach/processor_info.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_H
48 # include <mach/processor.h>
49 #endif
50 #ifdef HAVE_MACH_VM_MAP_H
51 # include <mach/vm_map.h>
52 #endif
53
54 #ifdef HAVE_LIBKSTAT
55 # include <sys/sysinfo.h>
56 #endif /* HAVE_LIBKSTAT */
57
58 #if (defined(HAVE_SYSCTL) && HAVE_SYSCTL) \
59         || (defined(HAVE_SYSCTLBYNAME) && HAVE_SYSCTLBYNAME)
60 # ifdef HAVE_SYS_SYSCTL_H
61 #  include <sys/sysctl.h>
62 # endif
63
64 # ifdef HAVE_SYS_DKSTAT_H
65 #  include <sys/dkstat.h>
66 # endif
67
68 # if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
69 #  define CP_USER   0
70 #  define CP_NICE   1
71 #  define CP_SYS    2
72 #  define CP_INTR   3
73 #  define CP_IDLE   4
74 #  define CPUSTATES 5
75 # endif
76 #endif /* HAVE_SYSCTL || HAVE_SYSCTLBYNAME */
77
78 #if HAVE_SYSCTL
79 # if defined(CTL_HW) && defined(HW_NCPU) \
80         && defined(CTL_KERN) && defined(KERN_CPTIME) && defined(CPUSTATES)
81 #  define CAN_USE_SYSCTL 1
82 # else
83 #  define CAN_USE_SYSCTL 0
84 # endif
85 #else
86 # define CAN_USE_SYSCTL 0
87 #endif
88
89 #if HAVE_STATGRAB_H
90 # include <statgrab.h>
91 #endif
92
93 # ifdef HAVE_PERFSTAT
94 #  include <sys/protosw.h>
95 #  include <libperfstat.h>
96 # endif /* HAVE_PERFSTAT */
97
98 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
99         && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
100 # error "No applicable input method."
101 #endif
102
103 #ifdef PROCESSOR_CPU_LOAD_INFO
104 static mach_port_t port_host;
105 static processor_port_array_t cpu_list;
106 static mach_msg_type_number_t cpu_list_len;
107 /* #endif PROCESSOR_CPU_LOAD_INFO */
108
109 #elif defined(KERNEL_LINUX)
110 /* no variables needed */
111 /* #endif KERNEL_LINUX */
112
113 #elif defined(HAVE_LIBKSTAT)
114 /* colleague tells me that Sun doesn't sell systems with more than 100 or so CPUs.. */
115 # define MAX_NUMCPU 256
116 extern kstat_ctl_t *kc;
117 static kstat_t *ksp[MAX_NUMCPU];
118 static int numcpu;
119 /* #endif HAVE_LIBKSTAT */
120
121 #elif CAN_USE_SYSCTL
122 static int numcpu;
123 /* #endif CAN_USE_SYSCTL */
124
125 #elif defined(HAVE_SYSCTLBYNAME)
126 static int numcpu;
127 #  ifdef HAVE_SYSCTL_KERN_CP_TIMES
128 static int maxcpu;
129 #  endif /* HAVE_SYSCTL_KERN_CP_TIMES */
130 /* #endif HAVE_SYSCTLBYNAME */
131
132 #elif defined(HAVE_LIBSTATGRAB)
133 /* no variables needed */
134 /* #endif  HAVE_LIBSTATGRAB */
135
136 #elif defined(HAVE_PERFSTAT)
137 static perfstat_cpu_t *perfcpu;
138 static int numcpu;
139 static int pnumcpu;
140 #endif /* HAVE_PERFSTAT */
141
142 static int init (void)
143 {
144 #if PROCESSOR_CPU_LOAD_INFO
145         kern_return_t status;
146
147         port_host = mach_host_self ();
148
149         /* FIXME: Free `cpu_list' if it's not NULL */
150         if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS)
151         {
152                 ERROR ("cpu plugin: host_processors returned %i", (int) status);
153                 cpu_list_len = 0;
154                 return (-1);
155         }
156
157         DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors");
158         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
159
160         cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());
161 /* #endif PROCESSOR_CPU_LOAD_INFO */
162
163 #elif defined(HAVE_LIBKSTAT)
164         kstat_t *ksp_chain;
165
166         numcpu = 0;
167
168         if (kc == NULL)
169                 return (-1);
170
171         /* Solaris doesn't count linear.. *sigh* */
172         for (numcpu = 0, ksp_chain = kc->kc_chain;
173                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
174                         ksp_chain = ksp_chain->ks_next)
175                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
176                         ksp[numcpu++] = ksp_chain;
177 /* #endif HAVE_LIBKSTAT */
178
179 #elif CAN_USE_SYSCTL
180         size_t numcpu_size;
181         int mib[2] = {CTL_HW, HW_NCPU};
182         int status;
183
184         numcpu = 0;
185         numcpu_size = sizeof (numcpu);
186
187         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
188                         &numcpu, &numcpu_size, NULL, 0);
189         if (status == -1)
190         {
191                 char errbuf[1024];
192                 WARNING ("cpu plugin: sysctl: %s",
193                                 sstrerror (errno, errbuf, sizeof (errbuf)));
194                 return (-1);
195         }
196 /* #endif CAN_USE_SYSCTL */
197
198 #elif defined (HAVE_SYSCTLBYNAME)
199         size_t numcpu_size;
200
201         numcpu_size = sizeof (numcpu);
202
203         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
204         {
205                 char errbuf[1024];
206                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
207                                 sstrerror (errno, errbuf, sizeof (errbuf)));
208                 return (-1);
209         }
210
211 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
212         numcpu_size = sizeof (maxcpu);
213
214         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
215         {
216                 char errbuf[1024];
217                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
218                                 sstrerror (errno, errbuf, sizeof (errbuf)));
219                 return (-1);
220         }
221 #else
222         if (numcpu != 1)
223                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
224 #endif
225 /* #endif HAVE_SYSCTLBYNAME */
226
227 #elif defined(HAVE_LIBSTATGRAB)
228         /* nothing to initialize */
229 /* #endif HAVE_LIBSTATGRAB */
230
231 #elif defined(HAVE_PERFSTAT)
232         /* nothing to initialize */
233 #endif /* HAVE_PERFSTAT */
234
235         return (0);
236 } /* int init */
237
238 static void submit (int cpu_num, const char *type_instance, derive_t value)
239 {
240         value_t values[1];
241         value_list_t vl = VALUE_LIST_INIT;
242
243         values[0].derive = value;
244
245         vl.values = values;
246         vl.values_len = 1;
247         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
248         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
249         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
250                         "%i", cpu_num);
251         sstrncpy (vl.type, "cpu", sizeof (vl.type));
252         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
253
254         plugin_dispatch_values (&vl);
255 }
256
257 static int cpu_read (void)
258 {
259 #if PROCESSOR_CPU_LOAD_INFO
260         int cpu;
261
262         kern_return_t status;
263         
264         processor_cpu_load_info_data_t cpu_info;
265         mach_msg_type_number_t         cpu_info_len;
266
267         host_t cpu_host;
268
269         for (cpu = 0; cpu < cpu_list_len; cpu++)
270         {
271                 cpu_host = 0;
272                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
273
274                 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
275                                 (processor_info_t) &cpu_info, &cpu_info_len);
276                 if (status != KERN_SUCCESS)
277                 {
278                         ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
279                                         mach_error_string (status));
280                         continue;
281                 }
282
283                 if (cpu_info_len < CPU_STATE_MAX)
284                 {
285                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
286                         continue;
287                 }
288
289                 submit (cpu, "user", (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
290                 submit (cpu, "nice", (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
291                 submit (cpu, "system", (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
292                 submit (cpu, "idle", (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
293         }
294 /* #endif PROCESSOR_CPU_LOAD_INFO */
295
296 #elif defined(KERNEL_LINUX)
297         int cpu;
298         derive_t user, nice, syst, idle;
299         derive_t wait, intr, sitr; /* sitr == soft interrupt */
300         FILE *fh;
301         char buf[1024];
302
303         char *fields[9];
304         int numfields;
305
306         if ((fh = fopen ("/proc/stat", "r")) == NULL)
307         {
308                 char errbuf[1024];
309                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
310                                 sstrerror (errno, errbuf, sizeof (errbuf)));
311                 return (-1);
312         }
313
314         while (fgets (buf, 1024, fh) != NULL)
315         {
316                 if (strncmp (buf, "cpu", 3))
317                         continue;
318                 if ((buf[3] < '0') || (buf[3] > '9'))
319                         continue;
320
321                 numfields = strsplit (buf, fields, 9);
322                 if (numfields < 5)
323                         continue;
324
325                 cpu = atoi (fields[0] + 3);
326                 user = atoll (fields[1]);
327                 nice = atoll (fields[2]);
328                 syst = atoll (fields[3]);
329                 idle = atoll (fields[4]);
330
331                 submit (cpu, "user", user);
332                 submit (cpu, "nice", nice);
333                 submit (cpu, "system", syst);
334                 submit (cpu, "idle", idle);
335
336                 if (numfields >= 8)
337                 {
338                         wait = atoll (fields[5]);
339                         intr = atoll (fields[6]);
340                         sitr = atoll (fields[7]);
341
342                         submit (cpu, "wait", wait);
343                         submit (cpu, "interrupt", intr);
344                         submit (cpu, "softirq", sitr);
345
346                         if (numfields >= 9)
347                                 submit (cpu, "steal", atoll (fields[8]));
348                 }
349         }
350
351         fclose (fh);
352 /* #endif defined(KERNEL_LINUX) */
353
354 #elif defined(HAVE_LIBKSTAT)
355         int cpu;
356         derive_t user, syst, idle, wait;
357         static cpu_stat_t cs;
358
359         if (kc == NULL)
360                 return (-1);
361
362         for (cpu = 0; cpu < numcpu; cpu++)
363         {
364                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
365                         continue; /* error message? */
366
367                 idle = (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
368                 user = (derive_t) cs.cpu_sysinfo.cpu[CPU_USER];
369                 syst = (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
370                 wait = (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
371
372                 submit (ksp[cpu]->ks_instance, "user", user);
373                 submit (ksp[cpu]->ks_instance, "system", syst);
374                 submit (ksp[cpu]->ks_instance, "idle", idle);
375                 submit (ksp[cpu]->ks_instance, "wait", wait);
376         }
377 /* #endif defined(HAVE_LIBKSTAT) */
378
379 #elif CAN_USE_SYSCTL
380         uint64_t cpuinfo[numcpu][CPUSTATES];
381         size_t cpuinfo_size;
382         int status;
383         int i;
384
385         if (numcpu < 1)
386         {
387                 ERROR ("cpu plugin: Could not determine number of "
388                                 "installed CPUs using sysctl(3).");
389                 return (-1);
390         }
391
392         memset (cpuinfo, 0, sizeof (cpuinfo));
393
394 #if defined(KERN_CPTIME2)
395         if (numcpu > 1) {
396                 for (i = 0; i < numcpu; i++) {
397                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
398
399                         cpuinfo_size = sizeof (cpuinfo[0]);
400
401                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
402                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
403                         if (status == -1) {
404                                 char errbuf[1024];
405                                 ERROR ("cpu plugin: sysctl failed: %s.",
406                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
407                                 return (-1);
408                         }
409                 }
410         }
411         else
412 #endif /* defined(KERN_CPTIME2) */
413         {
414                 int mib[] = {CTL_KERN, KERN_CPTIME};
415                 long cpuinfo_tmp[CPUSTATES];
416
417                 cpuinfo_size = sizeof(cpuinfo_tmp);
418
419                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
420                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
421                 if (status == -1)
422                 {
423                         char errbuf[1024];
424                         ERROR ("cpu plugin: sysctl failed: %s.",
425                                         sstrerror (errno, errbuf, sizeof (errbuf)));
426                         return (-1);
427                 }
428
429                 for(i = 0; i < CPUSTATES; i++) {
430                         cpuinfo[0][i] = cpuinfo_tmp[i];
431                 }
432         }
433
434         for (i = 0; i < numcpu; i++) {
435                 submit (i, "user",      cpuinfo[i][CP_USER]);
436                 submit (i, "nice",      cpuinfo[i][CP_NICE]);
437                 submit (i, "system",    cpuinfo[i][CP_SYS]);
438                 submit (i, "idle",      cpuinfo[i][CP_IDLE]);
439                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
440         }
441 /* #endif CAN_USE_SYSCTL */
442 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
443         long cpuinfo[maxcpu][CPUSTATES];
444         size_t cpuinfo_size;
445         int i;
446
447         memset (cpuinfo, 0, sizeof (cpuinfo));
448
449         cpuinfo_size = sizeof (cpuinfo);
450         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
451         {
452                 char errbuf[1024];
453                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
454                                 sstrerror (errno, errbuf, sizeof (errbuf)));
455                 return (-1);
456         }
457
458         for (i = 0; i < numcpu; i++) {
459                 submit (i, "user", cpuinfo[i][CP_USER]);
460                 submit (i, "nice", cpuinfo[i][CP_NICE]);
461                 submit (i, "system", cpuinfo[i][CP_SYS]);
462                 submit (i, "idle", cpuinfo[i][CP_IDLE]);
463                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
464         }
465 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
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",   (derive_t) cs->idle);
498         submit (0, "nice",   (derive_t) cs->nice);
499         submit (0, "swap",   (derive_t) cs->swap);
500         submit (0, "system", (derive_t) cs->kernel);
501         submit (0, "user",   (derive_t) cs->user);
502         submit (0, "wait",   (derive_t) cs->iowait);
503 /* #endif HAVE_LIBSTATGRAB */
504
505 #elif defined(HAVE_PERFSTAT)
506         perfstat_id_t id;
507         int i, cpus;
508
509         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
510         if(numcpu == -1)
511         {
512                 char errbuf[1024];
513                 WARNING ("cpu plugin: perfstat_cpu: %s",
514                         sstrerror (errno, errbuf, sizeof (errbuf)));
515                 return (-1);
516         }
517         
518         if (pnumcpu != numcpu || perfcpu == NULL) 
519         {
520                 if (perfcpu != NULL) 
521                         free(perfcpu);
522                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
523         }
524         pnumcpu = numcpu;
525
526         id.name[0] = '\0';
527         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
528         {
529                 char errbuf[1024];
530                 WARNING ("cpu plugin: perfstat_cpu: %s",
531                         sstrerror (errno, errbuf, sizeof (errbuf)));
532                 return (-1);
533         }
534
535         for (i = 0; i < cpus; i++) 
536         {
537                 submit (i, "idle",   (derive_t) perfcpu[i].idle);
538                 submit (i, "system", (derive_t) perfcpu[i].sys);
539                 submit (i, "user",   (derive_t) perfcpu[i].user);
540                 submit (i, "wait",   (derive_t) perfcpu[i].wait);
541         }
542 #endif /* HAVE_PERFSTAT */
543
544         return (0);
545 }
546
547 void module_register (void)
548 {
549         plugin_register_init ("cpu", init);
550         plugin_register_read ("cpu", cpu_read);
551 } /* void module_register */