Also fix query_plans_by_table
[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         status = host_processors (port_host, &cpu_list, &cpu_list_len);
150         if (status == KERN_INVALID_ARGUMENT)
151         {
152                 ERROR ("cpu plugin: Don't have a privileged host control port. "
153                                 "The most common cause for this problem is "
154                                 "that collectd is running without root "
155                                 "privileges, which are required to read CPU "
156                                 "load information. "
157                                 "<https://collectd.org/bugs/22>");
158                 cpu_list_len = 0;
159                 return (-1);
160         }
161         if (status != KERN_SUCCESS)
162         {
163                 ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status);
164                 cpu_list_len = 0;
165                 return (-1);
166         }
167
168         INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s");
169 /* #endif PROCESSOR_CPU_LOAD_INFO */
170
171 #elif defined(HAVE_LIBKSTAT)
172         kstat_t *ksp_chain;
173
174         numcpu = 0;
175
176         if (kc == NULL)
177                 return (-1);
178
179         /* Solaris doesn't count linear.. *sigh* */
180         for (numcpu = 0, ksp_chain = kc->kc_chain;
181                         (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
182                         ksp_chain = ksp_chain->ks_next)
183                 if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0)
184                         ksp[numcpu++] = ksp_chain;
185 /* #endif HAVE_LIBKSTAT */
186
187 #elif CAN_USE_SYSCTL
188         size_t numcpu_size;
189         int mib[2] = {CTL_HW, HW_NCPU};
190         int status;
191
192         numcpu = 0;
193         numcpu_size = sizeof (numcpu);
194
195         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
196                         &numcpu, &numcpu_size, NULL, 0);
197         if (status == -1)
198         {
199                 char errbuf[1024];
200                 WARNING ("cpu plugin: sysctl: %s",
201                                 sstrerror (errno, errbuf, sizeof (errbuf)));
202                 return (-1);
203         }
204 /* #endif CAN_USE_SYSCTL */
205
206 #elif defined (HAVE_SYSCTLBYNAME)
207         size_t numcpu_size;
208
209         numcpu_size = sizeof (numcpu);
210
211         if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0)
212         {
213                 char errbuf[1024];
214                 WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s",
215                                 sstrerror (errno, errbuf, sizeof (errbuf)));
216                 return (-1);
217         }
218
219 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
220         numcpu_size = sizeof (maxcpu);
221
222         if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0)
223         {
224                 char errbuf[1024];
225                 WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s",
226                                 sstrerror (errno, errbuf, sizeof (errbuf)));
227                 return (-1);
228         }
229 #else
230         if (numcpu != 1)
231                 NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);
232 #endif
233 /* #endif HAVE_SYSCTLBYNAME */
234
235 #elif defined(HAVE_LIBSTATGRAB)
236         /* nothing to initialize */
237 /* #endif HAVE_LIBSTATGRAB */
238
239 #elif defined(HAVE_PERFSTAT)
240         /* nothing to initialize */
241 #endif /* HAVE_PERFSTAT */
242
243         return (0);
244 } /* int init */
245
246 static void submit (int cpu_num, const char *type_instance, derive_t value)
247 {
248         value_t values[1];
249         value_list_t vl = VALUE_LIST_INIT;
250
251         values[0].derive = value;
252
253         vl.values = values;
254         vl.values_len = 1;
255         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
256         sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
257         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
258                         "%i", cpu_num);
259         sstrncpy (vl.type, "cpu", sizeof (vl.type));
260         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
261
262         plugin_dispatch_values (&vl);
263 }
264
265 static int cpu_read (void)
266 {
267 #if PROCESSOR_CPU_LOAD_INFO
268         int cpu;
269
270         kern_return_t status;
271
272         processor_cpu_load_info_data_t cpu_info;
273         mach_msg_type_number_t         cpu_info_len;
274
275         host_t cpu_host;
276
277         for (cpu = 0; cpu < cpu_list_len; cpu++)
278         {
279                 cpu_host = 0;
280                 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
281
282                 status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
283                                 (processor_info_t) &cpu_info, &cpu_info_len);
284                 if (status != KERN_SUCCESS)
285                 {
286                         ERROR ("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
287                                         mach_error_string (status));
288                         continue;
289                 }
290
291                 if (cpu_info_len < CPU_STATE_MAX)
292                 {
293                         ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len);
294                         continue;
295                 }
296
297                 submit (cpu, "user", (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER]);
298                 submit (cpu, "nice", (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE]);
299                 submit (cpu, "system", (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]);
300                 submit (cpu, "idle", (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]);
301         }
302 /* #endif PROCESSOR_CPU_LOAD_INFO */
303
304 #elif defined(KERNEL_LINUX)
305         int cpu;
306         derive_t user, nice, syst, idle;
307         derive_t wait, intr, sitr; /* sitr == soft interrupt */
308         FILE *fh;
309         char buf[1024];
310
311         char *fields[9];
312         int numfields;
313
314         if ((fh = fopen ("/proc/stat", "r")) == NULL)
315         {
316                 char errbuf[1024];
317                 ERROR ("cpu plugin: fopen (/proc/stat) failed: %s",
318                                 sstrerror (errno, errbuf, sizeof (errbuf)));
319                 return (-1);
320         }
321
322         while (fgets (buf, 1024, fh) != NULL)
323         {
324                 if (strncmp (buf, "cpu", 3))
325                         continue;
326                 if ((buf[3] < '0') || (buf[3] > '9'))
327                         continue;
328
329                 numfields = strsplit (buf, fields, 9);
330                 if (numfields < 5)
331                         continue;
332
333                 cpu = atoi (fields[0] + 3);
334                 user = atoll (fields[1]);
335                 nice = atoll (fields[2]);
336                 syst = atoll (fields[3]);
337                 idle = atoll (fields[4]);
338
339                 submit (cpu, "user", user);
340                 submit (cpu, "nice", nice);
341                 submit (cpu, "system", syst);
342                 submit (cpu, "idle", idle);
343
344                 if (numfields >= 8)
345                 {
346                         wait = atoll (fields[5]);
347                         intr = atoll (fields[6]);
348                         sitr = atoll (fields[7]);
349
350                         submit (cpu, "wait", wait);
351                         submit (cpu, "interrupt", intr);
352                         submit (cpu, "softirq", sitr);
353
354                         if (numfields >= 9)
355                                 submit (cpu, "steal", atoll (fields[8]));
356                 }
357         }
358
359         fclose (fh);
360 /* #endif defined(KERNEL_LINUX) */
361
362 #elif defined(HAVE_LIBKSTAT)
363         int cpu;
364         derive_t user, syst, idle, wait;
365         static cpu_stat_t cs;
366
367         if (kc == NULL)
368                 return (-1);
369
370         for (cpu = 0; cpu < numcpu; cpu++)
371         {
372                 if (kstat_read (kc, ksp[cpu], &cs) == -1)
373                         continue; /* error message? */
374
375                 idle = (derive_t) cs.cpu_sysinfo.cpu[CPU_IDLE];
376                 user = (derive_t) cs.cpu_sysinfo.cpu[CPU_USER];
377                 syst = (derive_t) cs.cpu_sysinfo.cpu[CPU_KERNEL];
378                 wait = (derive_t) cs.cpu_sysinfo.cpu[CPU_WAIT];
379
380                 submit (ksp[cpu]->ks_instance, "user", user);
381                 submit (ksp[cpu]->ks_instance, "system", syst);
382                 submit (ksp[cpu]->ks_instance, "idle", idle);
383                 submit (ksp[cpu]->ks_instance, "wait", wait);
384         }
385 /* #endif defined(HAVE_LIBKSTAT) */
386
387 #elif CAN_USE_SYSCTL
388         uint64_t cpuinfo[numcpu][CPUSTATES];
389         size_t cpuinfo_size;
390         int status;
391         int i;
392
393         if (numcpu < 1)
394         {
395                 ERROR ("cpu plugin: Could not determine number of "
396                                 "installed CPUs using sysctl(3).");
397                 return (-1);
398         }
399
400         memset (cpuinfo, 0, sizeof (cpuinfo));
401
402 #if defined(KERN_CPTIME2)
403         if (numcpu > 1) {
404                 for (i = 0; i < numcpu; i++) {
405                         int mib[] = {CTL_KERN, KERN_CPTIME2, i};
406
407                         cpuinfo_size = sizeof (cpuinfo[0]);
408
409                         status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
410                                         cpuinfo[i], &cpuinfo_size, NULL, 0);
411                         if (status == -1) {
412                                 char errbuf[1024];
413                                 ERROR ("cpu plugin: sysctl failed: %s.",
414                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
415                                 return (-1);
416                         }
417                 }
418         }
419         else
420 #endif /* defined(KERN_CPTIME2) */
421         {
422                 int mib[] = {CTL_KERN, KERN_CPTIME};
423                 long cpuinfo_tmp[CPUSTATES];
424
425                 cpuinfo_size = sizeof(cpuinfo_tmp);
426
427                 status = sysctl (mib, STATIC_ARRAY_SIZE (mib),
428                                         &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
429                 if (status == -1)
430                 {
431                         char errbuf[1024];
432                         ERROR ("cpu plugin: sysctl failed: %s.",
433                                         sstrerror (errno, errbuf, sizeof (errbuf)));
434                         return (-1);
435                 }
436
437                 for(i = 0; i < CPUSTATES; i++) {
438                         cpuinfo[0][i] = cpuinfo_tmp[i];
439                 }
440         }
441
442         for (i = 0; i < numcpu; i++) {
443                 submit (i, "user",      cpuinfo[i][CP_USER]);
444                 submit (i, "nice",      cpuinfo[i][CP_NICE]);
445                 submit (i, "system",    cpuinfo[i][CP_SYS]);
446                 submit (i, "idle",      cpuinfo[i][CP_IDLE]);
447                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
448         }
449 /* #endif CAN_USE_SYSCTL */
450 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES)
451         long cpuinfo[maxcpu][CPUSTATES];
452         size_t cpuinfo_size;
453         int i;
454
455         memset (cpuinfo, 0, sizeof (cpuinfo));
456
457         cpuinfo_size = sizeof (cpuinfo);
458         if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
459         {
460                 char errbuf[1024];
461                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
462                                 sstrerror (errno, errbuf, sizeof (errbuf)));
463                 return (-1);
464         }
465
466         for (i = 0; i < numcpu; i++) {
467                 submit (i, "user", cpuinfo[i][CP_USER]);
468                 submit (i, "nice", cpuinfo[i][CP_NICE]);
469                 submit (i, "system", cpuinfo[i][CP_SYS]);
470                 submit (i, "idle", cpuinfo[i][CP_IDLE]);
471                 submit (i, "interrupt", cpuinfo[i][CP_INTR]);
472         }
473 /* #endif HAVE_SYSCTL_KERN_CP_TIMES */
474 #elif defined(HAVE_SYSCTLBYNAME)
475         long cpuinfo[CPUSTATES];
476         size_t cpuinfo_size;
477
478         cpuinfo_size = sizeof (cpuinfo);
479
480         if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0)
481         {
482                 char errbuf[1024];
483                 ERROR ("cpu plugin: sysctlbyname failed: %s.",
484                                 sstrerror (errno, errbuf, sizeof (errbuf)));
485                 return (-1);
486         }
487
488         submit (0, "user", cpuinfo[CP_USER]);
489         submit (0, "nice", cpuinfo[CP_NICE]);
490         submit (0, "system", cpuinfo[CP_SYS]);
491         submit (0, "idle", cpuinfo[CP_IDLE]);
492         submit (0, "interrupt", cpuinfo[CP_INTR]);
493 /* #endif HAVE_SYSCTLBYNAME */
494
495 #elif defined(HAVE_LIBSTATGRAB)
496         sg_cpu_stats *cs;
497         cs = sg_get_cpu_stats ();
498
499         if (cs == NULL)
500         {
501                 ERROR ("cpu plugin: sg_get_cpu_stats failed.");
502                 return (-1);
503         }
504
505         submit (0, "idle",   (derive_t) cs->idle);
506         submit (0, "nice",   (derive_t) cs->nice);
507         submit (0, "swap",   (derive_t) cs->swap);
508         submit (0, "system", (derive_t) cs->kernel);
509         submit (0, "user",   (derive_t) cs->user);
510         submit (0, "wait",   (derive_t) cs->iowait);
511 /* #endif HAVE_LIBSTATGRAB */
512
513 #elif defined(HAVE_PERFSTAT)
514         perfstat_id_t id;
515         int i, cpus;
516
517         numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
518         if(numcpu == -1)
519         {
520                 char errbuf[1024];
521                 WARNING ("cpu plugin: perfstat_cpu: %s",
522                         sstrerror (errno, errbuf, sizeof (errbuf)));
523                 return (-1);
524         }
525
526         if (pnumcpu != numcpu || perfcpu == NULL)
527         {
528                 if (perfcpu != NULL)
529                         free(perfcpu);
530                 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
531         }
532         pnumcpu = numcpu;
533
534         id.name[0] = '\0';
535         if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
536         {
537                 char errbuf[1024];
538                 WARNING ("cpu plugin: perfstat_cpu: %s",
539                         sstrerror (errno, errbuf, sizeof (errbuf)));
540                 return (-1);
541         }
542
543         for (i = 0; i < cpus; i++)
544         {
545                 submit (i, "idle",   (derive_t) perfcpu[i].idle);
546                 submit (i, "system", (derive_t) perfcpu[i].sys);
547                 submit (i, "user",   (derive_t) perfcpu[i].user);
548                 submit (i, "wait",   (derive_t) perfcpu[i].wait);
549         }
550 #endif /* HAVE_PERFSTAT */
551
552         return (0);
553 }
554
555 void module_register (void)
556 {
557         plugin_register_init ("cpu", init);
558         plugin_register_read ("cpu", cpu_read);
559 } /* void module_register */