Debian Codegraph @

src/cpu.c

raw
1 /**
2 * collectd - src/cpu.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2008 Oleg King
5 * Copyright (C) 2009 Simon Kuhnle
6 * Copyright (C) 2009 Manuel Sanmartin
7 * Copyright (C) 2013-2014 Pierre-Yves Ritschard
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; only version 2 of the License is applicable.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * Authors:
23 * Florian octo Forster <octo at collectd.org>
24 * Oleg King <king2 at kaluga.ru>
25 * Simon Kuhnle <simon at blarzwurst.de>
26 * Manuel Sanmartin
27 * Pierre-Yves Ritschard <pyr at spootnik.org>
28 **/
29
30 #include "collectd.h"
31
32 #include "plugin.h"
33 #include "utils/common/common.h"
34
35 #ifdef HAVE_MACH_KERN_RETURN_H
36 #include <mach/kern_return.h>
37 #endif
38 #ifdef HAVE_MACH_MACH_INIT_H
39 #include <mach/mach_init.h>
40 #endif
41 #ifdef HAVE_MACH_HOST_PRIV_H
42 #include <mach/host_priv.h>
43 #endif
44 #if HAVE_MACH_MACH_ERROR_H
45 #include <mach/mach_error.h>
46 #endif
47 #ifdef HAVE_MACH_PROCESSOR_INFO_H
48 #include <mach/processor_info.h>
49 #endif
50 #ifdef HAVE_MACH_PROCESSOR_H
51 #include <mach/processor.h>
52 #endif
53 #ifdef HAVE_MACH_VM_MAP_H
54 #include <mach/vm_map.h>
55 #endif
56
57 #ifdef HAVE_LIBKSTAT
58 #include <sys/sysinfo.h>
59 #endif /* HAVE_LIBKSTAT */
60
61 #if (defined(HAVE_SYSCTL) && defined(HAVE_SYSCTLBYNAME)) || defined(__OpenBSD__)
62 /* Implies BSD variant */
63 #include <sys/sysctl.h>
64 #endif
65
66 #ifdef HAVE_SYS_DKSTAT_H
67 /* implies BSD variant */
68 #include <sys/dkstat.h>
69
70 #if !defined(CP_USER) || !defined(CP_NICE) || !defined(CP_SYS) || \
71 !defined(CP_INTR) || !defined(CP_IDLE) || !defined(CPUSTATES)
72 #define CP_USER 0
73 #define CP_NICE 1
74 #define CP_SYS 2
75 #define CP_INTR 3
76 #define CP_IDLE 4
77 #define CPUSTATES 5
78 #endif
79 #endif /* HAVE_SYS_DKSTAT_H */
80
81 #if (defined(HAVE_SYSCTL) && defined(HAVE_SYSCTLBYNAME)) || defined(__OpenBSD__)
82 /* Implies BSD variant */
83 #if defined(CTL_HW) && defined(HW_NCPU) && defined(CTL_KERN) && \
84 (defined(KERN_CPTIME) || defined(KERN_CP_TIME)) && defined(CPUSTATES)
85 #define CAN_USE_SYSCTL 1
86 #else
87 #define CAN_USE_SYSCTL 0
88 #endif
89 #else
90 #define CAN_USE_SYSCTL 0
91 #endif /* HAVE_SYSCTL_H && HAVE_SYSCTLBYNAME || __OpenBSD__ */
92
93 #define COLLECTD_CPU_STATE_USER 0
94 #define COLLECTD_CPU_STATE_SYSTEM 1
95 #define COLLECTD_CPU_STATE_WAIT 2
96 #define COLLECTD_CPU_STATE_NICE 3
97 #define COLLECTD_CPU_STATE_SWAP 4
98 #define COLLECTD_CPU_STATE_INTERRUPT 5
99 #define COLLECTD_CPU_STATE_SOFTIRQ 6
100 #define COLLECTD_CPU_STATE_STEAL 7
101 #define COLLECTD_CPU_STATE_GUEST 8
102 #define COLLECTD_CPU_STATE_GUEST_NICE 9
103 #define COLLECTD_CPU_STATE_IDLE 10
104 #define COLLECTD_CPU_STATE_ACTIVE 11 /* sum of (!idle) */
105 #define COLLECTD_CPU_STATE_MAX 12 /* #states */
106
107 #if HAVE_STATGRAB_H
108 #include <statgrab.h>
109 #endif
110
111 #ifdef HAVE_PERFSTAT
112 #include <libperfstat.h>
113 #include <sys/protosw.h>
114 #endif /* HAVE_PERFSTAT */
115
116 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
117 !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && \
118 !HAVE_PERFSTAT
119 #error "No applicable input method."
120 #endif
121
122 static const char *cpu_state_names[] = {
123 "user", "system", "wait", "nice", "swap", "interrupt",
124 "softirq", "steal", "guest", "guest_nice", "idle", "active"};
125
126 #ifdef PROCESSOR_CPU_LOAD_INFO
127 static mach_port_t port_host;
128 static processor_port_array_t cpu_list;
129 static mach_msg_type_number_t cpu_list_len;
130 /* #endif PROCESSOR_CPU_LOAD_INFO */
131
132 #elif defined(KERNEL_LINUX)
133 /* no variables needed */
134 /* #endif KERNEL_LINUX */
135
136 #elif defined(HAVE_LIBKSTAT)
137 #if HAVE_KSTAT_H
138 #include <kstat.h>
139 #endif
140 /* colleague tells me that Sun doesn't sell systems with more than 100 or so
141 * CPUs.. */
142 #define MAX_NUMCPU 256
143 extern kstat_ctl_t *kc;
144 static kstat_t *ksp[MAX_NUMCPU];
145 static int numcpu;
146 /* #endif HAVE_LIBKSTAT */
147
148 #elif CAN_USE_SYSCTL
149 /* Only possible for (Open) BSD variant */
150 static int numcpu;
151 /* #endif CAN_USE_SYSCTL */
152
153 #elif defined(HAVE_SYSCTLBYNAME)
154 /* Implies BSD variant */
155 static int numcpu;
156 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
157 static int maxcpu;
158 #endif /* HAVE_SYSCTL_KERN_CP_TIMES */
159 /* #endif HAVE_SYSCTLBYNAME */
160
161 #elif defined(HAVE_LIBSTATGRAB)
162 /* no variables needed */
163 /* #endif HAVE_LIBSTATGRAB */
164
165 #elif defined(HAVE_PERFSTAT)
166 #define TOTAL_IDLE 0
167 #define TOTAL_USER 1
168 #define TOTAL_SYS 2
169 #define TOTAL_WAIT 3
170 #define TOTAL_STAT_NUM 4
171 static value_to_rate_state_t total_conv[TOTAL_STAT_NUM];
172 static perfstat_cpu_t *perfcpu;
173 static int numcpu;
174 static int pnumcpu;
175 #endif /* HAVE_PERFSTAT */
176
177 #define RATE_ADD(sum, val) \
178 do { \
179 if (isnan(sum)) \
180 (sum) = (val); \
181 else if (!isnan(val)) \
182 (sum) += (val); \
183 } while (0)
184
185 struct cpu_state_s {
186 value_to_rate_state_t conv;
187 gauge_t rate;
188 bool has_value;
189 };
190 typedef struct cpu_state_s cpu_state_t;
191
192 static cpu_state_t *cpu_states;
193 static size_t cpu_states_num; /* #cpu_states allocated */
194
195 /* Highest CPU number in the current iteration. Used by the dispatch logic to
196 * determine how many CPUs there were. Reset to 0 by cpu_reset(). */
197 static size_t global_cpu_num;
198
199 static bool report_by_cpu = true;
200 static bool report_by_state = true;
201 static bool report_percent;
202 static bool report_num_cpu;
203 static bool report_guest;
204 static bool subtract_guest = true;
205
206 static const char *config_keys[] = {"ReportByCpu", "ReportByState",
207 "ReportNumCpu", "ValuesPercentage",
208 "ReportGuestState", "SubtractGuestState"};
209 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
210
211 static int cpu_config(char const *key, char const *value) /* {{{ */
212 {
213 if (strcasecmp(key, "ReportByCpu") == 0)
214 report_by_cpu = IS_TRUE(value);
215 else if (strcasecmp(key, "ValuesPercentage") == 0)
216 report_percent = IS_TRUE(value);
217 else if (strcasecmp(key, "ReportByState") == 0)
218 report_by_state = IS_TRUE(value);
219 else if (strcasecmp(key, "ReportNumCpu") == 0)
220 report_num_cpu = IS_TRUE(value);
221 else if (strcasecmp(key, "ReportGuestState") == 0)
222 report_guest = IS_TRUE(value);
223 else if (strcasecmp(key, "SubtractGuestState") == 0)
224 subtract_guest = IS_TRUE(value);
225 else
226 return -1;
227
228 return 0;
229 } /* }}} int cpu_config */
230
231 static int init(void) {
232 #if PROCESSOR_CPU_LOAD_INFO
233 kern_return_t status;
234
235 port_host = mach_host_self();
236
237 status = host_processors(port_host, &cpu_list, &cpu_list_len);
238 if (status == KERN_INVALID_ARGUMENT) {
239 ERROR("cpu plugin: Don't have a privileged host control port. "
240 "The most common cause for this problem is "
241 "that collectd is running without root "
242 "privileges, which are required to read CPU "
243 "load information. "
244 "<https://collectd.org/bugs/22>");
245 cpu_list_len = 0;
246 return -1;
247 }
248 if (status != KERN_SUCCESS) {
249 ERROR("cpu plugin: host_processors() failed with status %d.", (int)status);
250 cpu_list_len = 0;
251 return -1;
252 }
253
254 INFO("cpu plugin: Found %i processor%s.", (int)cpu_list_len,
255 cpu_list_len == 1 ? "" : "s");
256 /* #endif PROCESSOR_CPU_LOAD_INFO */
257
258 #elif defined(HAVE_LIBKSTAT)
259 kstat_t *ksp_chain;
260
261 numcpu = 0;
262
263 if (kc == NULL)
264 return -1;
265
266 /* Solaris doesn't count linear.. *sigh* */
267 for (numcpu = 0, ksp_chain = kc->kc_chain;
268 (numcpu < MAX_NUMCPU) && (ksp_chain != NULL);
269 ksp_chain = ksp_chain->ks_next)
270 if (strncmp(ksp_chain->ks_module, "cpu_stat", 8) == 0)
271 ksp[numcpu++] = ksp_chain;
272 /* #endif HAVE_LIBKSTAT */
273
274 #elif CAN_USE_SYSCTL
275 /* Only on (Open) BSD variant */
276 size_t numcpu_size;
277 int mib[2] = {CTL_HW, HW_NCPU};
278 int status;
279
280 numcpu = 0;
281 numcpu_size = sizeof(numcpu);
282
283 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &numcpu, &numcpu_size, NULL, 0);
284 if (status == -1) {
285 WARNING("cpu plugin: sysctl: %s", STRERRNO);
286 return -1;
287 }
288 /* #endif CAN_USE_SYSCTL */
289
290 #elif defined(HAVE_SYSCTLBYNAME)
291 /* Only on BSD varient */
292 size_t numcpu_size;
293
294 numcpu_size = sizeof(numcpu);
295
296 if (sysctlbyname("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) {
297 WARNING("cpu plugin: sysctlbyname(hw.ncpu): %s", STRERRNO);
298 return -1;
299 }
300
301 #ifdef HAVE_SYSCTL_KERN_CP_TIMES
302 numcpu_size = sizeof(maxcpu);
303
304 if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) {
305 WARNING("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s", STRERRNO);
306 return -1;
307 }
308 #else
309 if (numcpu != 1)
310 NOTICE("cpu: Only one processor supported when using `sysctlbyname' (found "
311 "%i)",
312 numcpu);
313 #endif
314 /* #endif HAVE_SYSCTLBYNAME */
315
316 #elif defined(HAVE_LIBSTATGRAB)
317 /* nothing to initialize */
318 /* #endif HAVE_LIBSTATGRAB */
319
320 #elif defined(HAVE_PERFSTAT)
321 /* nothing to initialize */
322 #endif /* HAVE_PERFSTAT */
323
324 return 0;
325 } /* int init */
326
327 static void submit_value(int cpu_num, int cpu_state, const char *type,
328 value_t value) {
329 value_list_t vl = VALUE_LIST_INIT;
330
331 vl.values = &value;
332 vl.values_len = 1;
333
334 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
335 sstrncpy(vl.type, type, sizeof(vl.type));
336 sstrncpy(vl.type_instance, cpu_state_names[cpu_state],
337 sizeof(vl.type_instance));
338
339 if (cpu_num >= 0) {
340 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
341 }
342 plugin_dispatch_values(&vl);
343 }
344
345 static void submit_percent(int cpu_num, int cpu_state, gauge_t value) {
346 /* This function is called for all known CPU states, but each read
347 * method will only report a subset. The remaining states are left as
348 * NAN and we ignore them here. */
349 if (isnan(value))
350 return;
351
352 submit_value(cpu_num, cpu_state, "percent", (value_t){.gauge = value});
353 }
354
355 static void submit_derive(int cpu_num, int cpu_state, derive_t value) {
356 submit_value(cpu_num, cpu_state, "cpu", (value_t){.derive = value});
357 }
358
359 /* Takes the zero-index number of a CPU and makes sure that the module-global
360 * cpu_states buffer is large enough. Returne ENOMEM on erorr. */
361 static int cpu_states_alloc(size_t cpu_num) /* {{{ */
362 {
363 cpu_state_t *tmp;
364 size_t sz;
365
366 sz = (((size_t)cpu_num) + 1) * COLLECTD_CPU_STATE_MAX;
367 assert(sz > 0);
368
369 /* We already have enough space. */
370 if (cpu_states_num >= sz)
371 return 0;
372
373 tmp = realloc(cpu_states, sz * sizeof(*cpu_states));
374 if (tmp == NULL) {
375 ERROR("cpu plugin: realloc failed.");
376 return ENOMEM;
377 }
378 cpu_states = tmp;
379 tmp = cpu_states + cpu_states_num;
380
381 memset(tmp, 0, (sz - cpu_states_num) * sizeof(*cpu_states));
382 cpu_states_num = sz;
383 return 0;
384 } /* }}} cpu_states_alloc */
385
386 static cpu_state_t *get_cpu_state(size_t cpu_num, size_t state) /* {{{ */
387 {
388 size_t index = ((cpu_num * COLLECTD_CPU_STATE_MAX) + state);
389
390 if (index >= cpu_states_num)
391 return NULL;
392
393 return &cpu_states[index];
394 } /* }}} cpu_state_t *get_cpu_state */
395
396 #if defined(HAVE_PERFSTAT) /* {{{ */
397 /* populate global aggregate cpu rate */
398 static int total_rate(gauge_t *sum_by_state, size_t state, derive_t d,
399 value_to_rate_state_t *conv, cdtime_t now) {
400 gauge_t rate = NAN;
401 int status =
402 value_to_rate(&rate, (value_t){.derive = d}, DS_TYPE_DERIVE, now, conv);
403 if (status != 0)
404 return status;
405
406 sum_by_state[state] = rate;
407
408 if (state != COLLECTD_CPU_STATE_IDLE)
409 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE], sum_by_state[state]);
410 return 0;
411 }
412 #endif /* }}} HAVE_PERFSTAT */
413
414 /* Populates the per-CPU COLLECTD_CPU_STATE_ACTIVE rate and the global
415 * rate_by_state
416 * array. */
417 static void aggregate(gauge_t *sum_by_state) /* {{{ */
418 {
419 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
420 sum_by_state[state] = NAN;
421
422 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
423 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
424
425 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate = NAN;
426
427 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
428 if (!this_cpu_states[state].has_value)
429 continue;
430
431 RATE_ADD(sum_by_state[state], this_cpu_states[state].rate);
432 if (state != COLLECTD_CPU_STATE_IDLE)
433 RATE_ADD(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate,
434 this_cpu_states[state].rate);
435 }
436
437 if (!isnan(this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate))
438 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = true;
439
440 RATE_ADD(sum_by_state[COLLECTD_CPU_STATE_ACTIVE],
441 this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate);
442 }
443
444 #if defined(HAVE_PERFSTAT) /* {{{ */
445 cdtime_t now = cdtime();
446 perfstat_cpu_total_t cputotal = {0};
447
448 if (!perfstat_cpu_total(NULL, &cputotal, sizeof(cputotal), 1)) {
449 WARNING("cpu plugin: perfstat_cpu_total: %s", STRERRNO);
450 return;
451 }
452
453 /* Reset COLLECTD_CPU_STATE_ACTIVE */
454 sum_by_state[COLLECTD_CPU_STATE_ACTIVE] = NAN;
455
456 /* Physical Processor Utilization */
457 total_rate(sum_by_state, COLLECTD_CPU_STATE_IDLE, (derive_t)cputotal.pidle,
458 &total_conv[TOTAL_IDLE], now);
459 total_rate(sum_by_state, COLLECTD_CPU_STATE_USER, (derive_t)cputotal.puser,
460 &total_conv[TOTAL_USER], now);
461 total_rate(sum_by_state, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cputotal.psys,
462 &total_conv[TOTAL_SYS], now);
463 total_rate(sum_by_state, COLLECTD_CPU_STATE_WAIT, (derive_t)cputotal.pwait,
464 &total_conv[TOTAL_WAIT], now);
465 #endif /* }}} HAVE_PERFSTAT */
466 } /* }}} void aggregate */
467
468 /* Commits (dispatches) the values for one CPU or the global aggregation.
469 * cpu_num is the index of the CPU to be committed or -1 in case of the global
470 * aggregation. rates is a pointer to COLLECTD_CPU_STATE_MAX gauge_t values
471 * holding the
472 * current rate; each rate may be NAN. Calculates the percentage of each state
473 * and dispatches the metric. */
474 static void cpu_commit_one(int cpu_num, /* {{{ */
475 gauge_t rates[static COLLECTD_CPU_STATE_MAX]) {
476 gauge_t sum;
477
478 sum = rates[COLLECTD_CPU_STATE_ACTIVE];
479 RATE_ADD(sum, rates[COLLECTD_CPU_STATE_IDLE]);
480
481 if (!report_by_state) {
482 gauge_t percent = 100.0 * rates[COLLECTD_CPU_STATE_ACTIVE] / sum;
483 submit_percent(cpu_num, COLLECTD_CPU_STATE_ACTIVE, percent);
484 return;
485 }
486
487 for (size_t state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
488 gauge_t percent = 100.0 * rates[state] / sum;
489 submit_percent(cpu_num, state, percent);
490 }
491 } /* }}} void cpu_commit_one */
492
493 /* Commits the number of cores */
494 static void cpu_commit_num_cpu(gauge_t value) /* {{{ */
495 {
496 value_list_t vl = VALUE_LIST_INIT;
497
498 vl.values = &(value_t){.gauge = value};
499 vl.values_len = 1;
500
501 sstrncpy(vl.plugin, "cpu", sizeof(vl.plugin));
502 sstrncpy(vl.type, "count", sizeof(vl.type));
503
504 plugin_dispatch_values(&vl);
505 } /* }}} void cpu_commit_num_cpu */
506
507 /* Resets the internal aggregation. This is called by the read callback after
508 * each iteration / after each call to cpu_commit(). */
509 static void cpu_reset(void) /* {{{ */
510 {
511 for (size_t i = 0; i < cpu_states_num; i++)
512 cpu_states[i].has_value = false;
513
514 global_cpu_num = 0;
515 } /* }}} void cpu_reset */
516
517 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
518 static void cpu_commit_without_aggregation(void) /* {{{ */
519 {
520 for (int state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) {
521 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
522 cpu_state_t *s = get_cpu_state(cpu_num, state);
523
524 if (!s->has_value)
525 continue;
526
527 submit_derive((int)cpu_num, (int)state, s->conv.last_value.derive);
528 }
529 }
530 } /* }}} void cpu_commit_without_aggregation */
531
532 /* Aggregates the internal state and dispatches the metrics. */
533 static void cpu_commit(void) /* {{{ */
534 {
535 gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = {
536 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
537 };
538
539 if (report_num_cpu)
540 cpu_commit_num_cpu((gauge_t)global_cpu_num);
541
542 if (report_by_state && report_by_cpu && !report_percent) {
543 cpu_commit_without_aggregation();
544 return;
545 }
546
547 aggregate(global_rates);
548
549 if (!report_by_cpu) {
550 cpu_commit_one(-1, global_rates);
551 return;
552 }
553
554 for (size_t cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) {
555 cpu_state_t *this_cpu_states = get_cpu_state(cpu_num, 0);
556 gauge_t local_rates[COLLECTD_CPU_STATE_MAX] = {
557 NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN};
558
559 for (size_t state = 0; state < COLLECTD_CPU_STATE_MAX; state++)
560 if (this_cpu_states[state].has_value)
561 local_rates[state] = this_cpu_states[state].rate;
562
563 cpu_commit_one((int)cpu_num, local_rates);
564 }
565 } /* }}} void cpu_commit */
566
567 /* Adds a derive value to the internal state. This should be used by each read
568 * function for each state. At the end of the iteration, the read function
569 * should call cpu_commit(). */
570 static int cpu_stage(size_t cpu_num, size_t state, derive_t d,
571 cdtime_t now) /* {{{ */
572 {
573 int status;
574 cpu_state_t *s;
575 gauge_t rate = NAN;
576 value_t val = {.derive = d};
577
578 if (state >= COLLECTD_CPU_STATE_ACTIVE)
579 return EINVAL;
580
581 status = cpu_states_alloc(cpu_num);
582 if (status != 0)
583 return status;
584
585 if (global_cpu_num <= cpu_num)
586 global_cpu_num = cpu_num + 1;
587
588 s = get_cpu_state(cpu_num, state);
589
590 status = value_to_rate(&rate, val, DS_TYPE_DERIVE, now, &s->conv);
591 if (status != 0)
592 return status;
593
594 s->rate = rate;
595 s->has_value = true;
596 return 0;
597 } /* }}} int cpu_stage */
598
599 static int cpu_read(void) {
600 cdtime_t now = cdtime();
601
602 #if PROCESSOR_CPU_LOAD_INFO /* {{{ */
603 kern_return_t status;
604
605 processor_cpu_load_info_data_t cpu_info;
606 mach_msg_type_number_t cpu_info_len;
607
608 host_t cpu_host;
609
610 for (mach_msg_type_number_t cpu = 0; cpu < cpu_list_len; cpu++) {
611 cpu_host = 0;
612 cpu_info_len = PROCESSOR_BASIC_INFO_COUNT;
613
614 status = processor_info(cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host,
615 (processor_info_t)&cpu_info, &cpu_info_len);
616 if (status != KERN_SUCCESS) {
617 ERROR("cpu plugin: processor_info (PROCESSOR_CPU_LOAD_INFO) failed: %s",
618 mach_error_string(status));
619 continue;
620 }
621
622 if (cpu_info_len < CPU_STATE_MAX) {
623 ERROR("cpu plugin: processor_info returned only %i elements..",
624 cpu_info_len);
625 continue;
626 }
627
628 cpu_stage(cpu, COLLECTD_CPU_STATE_USER,
629 (derive_t)cpu_info.cpu_ticks[CPU_STATE_USER], now);
630 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE,
631 (derive_t)cpu_info.cpu_ticks[CPU_STATE_NICE], now);
632 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM,
633 (derive_t)cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now);
634 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE,
635 (derive_t)cpu_info.cpu_ticks[CPU_STATE_IDLE], now);
636 }
637 /* }}} #endif PROCESSOR_CPU_LOAD_INFO */
638
639 #elif defined(KERNEL_LINUX) /* {{{ */
640 int cpu;
641 FILE *fh;
642 char buf[1024];
643
644 char *fields[11];
645 int numfields;
646
647 if ((fh = fopen("/proc/stat", "r")) == NULL) {
648 ERROR("cpu plugin: fopen (/proc/stat) failed: %s", STRERRNO);
649 return -1;
650 }
651
652 while (fgets(buf, 1024, fh) != NULL) {
653 if (strncmp(buf, "cpu", 3))
654 continue;
655 if ((buf[3] < '0') || (buf[3] > '9'))
656 continue;
657
658 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
659 if (numfields < 5)
660 continue;
661
662 cpu = atoi(fields[0] + 3);
663
664 /* Do not stage User and Nice immediately: we may need to alter them later:
665 */
666 long long user_value = atoll(fields[1]);
667 long long nice_value = atoll(fields[2]);
668 cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
669 cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
670
671 if (numfields >= 8) {
672 cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
673 cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
674 now);
675 cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
676 now);
677 }
678
679 if (numfields >= 9) { /* Steal (since Linux 2.6.11) */
680 cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]), now);
681 }
682
683 if (numfields >= 10) { /* Guest (since Linux 2.6.24) */
684 if (report_guest) {
685 long long value = atoll(fields[9]);
686 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST, (derive_t)value, now);
687 /* Guest is included in User; optionally subtract Guest from User: */
688 if (subtract_guest) {
689 user_value -= value;
690 if (user_value < 0)
691 user_value = 0;
692 }
693 }
694 }
695
696 if (numfields >= 11) { /* Guest_nice (since Linux 2.6.33) */
697 if (report_guest) {
698 long long value = atoll(fields[10]);
699 cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST_NICE, (derive_t)value, now);
700 /* Guest_nice is included in Nice; optionally subtract Guest_nice from
701 Nice: */
702 if (subtract_guest) {
703 nice_value -= value;
704 if (nice_value < 0)
705 nice_value = 0;
706 }
707 }
708 }
709
710 /* Eventually stage User and Nice: */
711 cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)user_value, now);
712 cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)nice_value, now);
713 }
714 fclose(fh);
715 /* }}} #endif defined(KERNEL_LINUX) */
716
717 #elif defined(HAVE_LIBKSTAT) /* {{{ */
718 static cpu_stat_t cs;
719
720 if (kc == NULL)
721 return -1;
722
723 for (int cpu = 0; cpu < numcpu; cpu++) {
724 if (kstat_read(kc, ksp[cpu], &cs) == -1)
725 continue; /* error message? */
726
727 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_IDLE,
728 (derive_t)cs.cpu_sysinfo.cpu[CPU_IDLE], now);
729 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_USER,
730 (derive_t)cs.cpu_sysinfo.cpu[CPU_USER], now);
731 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_SYSTEM,
732 (derive_t)cs.cpu_sysinfo.cpu[CPU_KERNEL], now);
733 cpu_stage(ksp[cpu]->ks_instance, COLLECTD_CPU_STATE_WAIT,
734 (derive_t)cs.cpu_sysinfo.cpu[CPU_WAIT], now);
735 }
736 /* }}} #endif defined(HAVE_LIBKSTAT) */
737
738 #elif CAN_USE_SYSCTL /* {{{ */
739 /* Only on (Open) BSD variant */
740 uint64_t cpuinfo[numcpu][CPUSTATES];
741 size_t cpuinfo_size;
742 int status;
743
744 if (numcpu < 1) {
745 ERROR("cpu plugin: Could not determine number of "
746 "installed CPUs using sysctl(3).");
747 return -1;
748 }
749
750 memset(cpuinfo, 0, sizeof(cpuinfo));
751
752 #if defined(KERN_CP_TIME) && defined(KERNEL_NETBSD)
753 {
754 int mib[] = {CTL_KERN, KERN_CP_TIME};
755
756 cpuinfo_size = sizeof(cpuinfo[0]) * numcpu * CPUSTATES;
757 status = sysctl(mib, 2, cpuinfo, &cpuinfo_size, NULL, 0);
758 if (status == -1) {
759 char errbuf[1024];
760
761 ERROR("cpu plugin: sysctl failed: %s.",
762 sstrerror(errno, errbuf, sizeof(errbuf)));
763 return -1;
764 }
765 if (cpuinfo_size == (sizeof(cpuinfo[0]) * CPUSTATES)) {
766 numcpu = 1;
767 }
768 }
769 #else /* defined(KERN_CP_TIME) && defined(KERNEL_NETBSD) */
770 #if defined(KERN_CPTIME2)
771 if (numcpu > 1) {
772 for (int i = 0; i < numcpu; i++) {
773 int mib[] = {CTL_KERN, KERN_CPTIME2, i};
774
775 cpuinfo_size = sizeof(cpuinfo[0]);
776
777 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), cpuinfo[i], &cpuinfo_size,
778 NULL, 0);
779 if (status == -1) {
780 ERROR("cpu plugin: sysctl failed: %s.", STRERRNO);
781 return -1;
782 }
783 }
784 } else
785 #endif /* defined(KERN_CPTIME2) */
786 {
787 int mib[] = {CTL_KERN, KERN_CPTIME};
788 long cpuinfo_tmp[CPUSTATES];
789
790 cpuinfo_size = sizeof(cpuinfo_tmp);
791
792 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &cpuinfo_tmp, &cpuinfo_size,
793 NULL, 0);
794 if (status == -1) {
795 ERROR("cpu plugin: sysctl failed: %s.", STRERRNO);
796 return -1;
797 }
798
799 for (int i = 0; i < CPUSTATES; i++) {
800 cpuinfo[0][i] = cpuinfo_tmp[i];
801 }
802 }
803 #endif /* defined(KERN_CP_TIME) && defined(KERNEL_NETBSD) */
804
805 for (int i = 0; i < numcpu; i++) {
806 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
807 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
808 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
809 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
810 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
811 now);
812 }
813 /* }}} #endif CAN_USE_SYSCTL */
814
815 #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) /* {{{ \
816 */
817 /* Only on BSD variant */
818 long cpuinfo[maxcpu][CPUSTATES];
819 size_t cpuinfo_size;
820
821 memset(cpuinfo, 0, sizeof(cpuinfo));
822
823 cpuinfo_size = sizeof(cpuinfo);
824 if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
825 ERROR("cpu plugin: sysctlbyname failed: %s.", STRERRNO);
826 return -1;
827 }
828
829 for (int i = 0; i < numcpu; i++) {
830 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);
831 cpu_stage(i, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[i][CP_NICE], now);
832 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[i][CP_SYS], now);
833 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[i][CP_IDLE], now);
834 cpu_stage(i, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[i][CP_INTR],
835 now);
836 }
837 /* }}} #endif HAVE_SYSCTL_KERN_CP_TIMES */
838
839 #elif defined(HAVE_SYSCTLBYNAME) /* {{{ */
840 /* Only on BSD variant */
841 long cpuinfo[CPUSTATES];
842 size_t cpuinfo_size;
843
844 cpuinfo_size = sizeof(cpuinfo);
845
846 if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) {
847 ERROR("cpu plugin: sysctlbyname failed: %s.", STRERRNO);
848 return -1;
849 }
850
851 cpu_stage(0, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[CP_USER], now);
852 cpu_stage(0, COLLECTD_CPU_STATE_NICE, (derive_t)cpuinfo[CP_NICE], now);
853 cpu_stage(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cpuinfo[CP_SYS], now);
854 cpu_stage(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cpuinfo[CP_IDLE], now);
855 cpu_stage(0, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)cpuinfo[CP_INTR], now);
856 /* }}} #endif HAVE_SYSCTLBYNAME */
857
858 #elif defined(HAVE_LIBSTATGRAB) /* {{{ */
859 sg_cpu_stats *cs;
860 cs = sg_get_cpu_stats();
861
862 if (cs == NULL) {
863 ERROR("cpu plugin: sg_get_cpu_stats failed.");
864 return -1;
865 }
866
867 cpu_state(0, COLLECTD_CPU_STATE_IDLE, (derive_t)cs->idle);
868 cpu_state(0, COLLECTD_CPU_STATE_NICE, (derive_t)cs->nice);
869 cpu_state(0, COLLECTD_CPU_STATE_SWAP, (derive_t)cs->swap);
870 cpu_state(0, COLLECTD_CPU_STATE_SYSTEM, (derive_t)cs->kernel);
871 cpu_state(0, COLLECTD_CPU_STATE_USER, (derive_t)cs->user);
872 cpu_state(0, COLLECTD_CPU_STATE_WAIT, (derive_t)cs->iowait);
873 /* }}} #endif HAVE_LIBSTATGRAB */
874
875 #elif defined(HAVE_PERFSTAT) /* {{{ */
876 perfstat_id_t id;
877 int cpus;
878
879 numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
880 if (numcpu == -1) {
881 WARNING("cpu plugin: perfstat_cpu: %s", STRERRNO);
882 return -1;
883 }
884
885 if (pnumcpu != numcpu || perfcpu == NULL) {
886 free(perfcpu);
887 perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
888 }
889 pnumcpu = numcpu;
890
891 id.name[0] = '\0';
892 if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0) {
893 WARNING("cpu plugin: perfstat_cpu: %s", STRERRNO);
894 return -1;
895 }
896
897 for (int i = 0; i < cpus; i++) {
898 cpu_stage(i, COLLECTD_CPU_STATE_IDLE, (derive_t)perfcpu[i].idle, now);
899 cpu_stage(i, COLLECTD_CPU_STATE_SYSTEM, (derive_t)perfcpu[i].sys, now);
900 cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)perfcpu[i].user, now);
901 cpu_stage(i, COLLECTD_CPU_STATE_WAIT, (derive_t)perfcpu[i].wait, now);
902 }
903 #endif /* }}} HAVE_PERFSTAT */
904
905 cpu_commit();
906 cpu_reset();
907 return 0;
908 }
909
910 void module_register(void) {
911 plugin_register_init("cpu", init);
912 plugin_register_config("cpu", cpu_config, config_keys, config_keys_num);
913 plugin_register_read("cpu", cpu_read);
914 } /* void module_register */