X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcpu.c;h=9c432d1791d0090191842aa364c48d761b5da72e;hb=183223fd3c46819703437f9989f0fa12491aa4d0;hp=bee6f177c11a6f3a3c81535de070e4b0e25b8088;hpb=4c3b7ef63ca81b0d015dae398ae5c2e63b3e1b67;p=collectd.git diff --git a/src/cpu.c b/src/cpu.c index bee6f177..9c432d17 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -221,18 +221,26 @@ static int init (void) port_host = mach_host_self (); - /* FIXME: Free `cpu_list' if it's not NULL */ - if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS) + status = host_processors (port_host, &cpu_list, &cpu_list_len); + if (status == KERN_INVALID_ARGUMENT) { - ERROR ("cpu plugin: host_processors returned %i", (int) status); + ERROR ("cpu plugin: Don't have a privileged host control port. " + "The most common cause for this problem is " + "that collectd is running without root " + "privileges, which are required to read CPU " + "load information. " + ""); + cpu_list_len = 0; + return (-1); + } + if (status != KERN_SUCCESS) + { + ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status); cpu_list_len = 0; return (-1); } - DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors"); INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s"); - - cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ()); /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(HAVE_LIBKSTAT) @@ -419,6 +427,9 @@ static void aggregate (gauge_t *sum_by_state) /* {{{ */ RATE_ADD (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate, this_cpu_states[state].rate); } + if (!isnan (this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate)) + this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].has_value = 1; + RATE_ADD (sum_by_state[COLLECTD_CPU_STATE_ACTIVE], this_cpu_states[COLLECTD_CPU_STATE_ACTIVE].rate); } } /* }}} void aggregate */ @@ -471,29 +482,15 @@ static void cpu_commit_without_aggregation (void) /* {{{ */ for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) { size_t cpu_num; - if (report_by_cpu) { - for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) - { - cpu_state_t *s = get_cpu_state (cpu_num, state); - if (!s->has_value) - continue; - - submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive); - } - } else { - derive_t derive_total = 0; - for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) - { - cpu_state_t *s = get_cpu_state (cpu_num, state); - - if (!s->has_value) - continue; + for (cpu_num = 0; cpu_num < global_cpu_num; cpu_num++) + { + cpu_state_t *s = get_cpu_state (cpu_num, state); - derive_total += s->conv.last_value.derive; + if (!s->has_value) + continue; - } - submit_derive (-1, (int) state, derive_total); + submit_derive ((int) cpu_num, (int) state, s->conv.last_value.derive); } } } /* }}} void cpu_commit_without_aggregation */ @@ -502,11 +499,11 @@ static void cpu_commit_without_aggregation (void) /* {{{ */ static void cpu_commit (void) /* {{{ */ { gauge_t global_rates[COLLECTD_CPU_STATE_MAX] = { - NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN + NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */ }; size_t cpu_num; - if (report_by_state && !report_percent) + if (report_by_state && report_by_cpu && !report_percent) { cpu_commit_without_aggregation (); return; @@ -528,7 +525,7 @@ static void cpu_commit (void) /* {{{ */ }; size_t state; - for (state = 0; state < COLLECTD_CPU_STATE_ACTIVE; state++) + for (state = 0; state < COLLECTD_CPU_STATE_MAX; state++) if (this_cpu_states[state].has_value) local_rates[state] = this_cpu_states[state].rate; @@ -539,11 +536,12 @@ static void cpu_commit (void) /* {{{ */ /* Adds a derive value to the internal state. This should be used by each read * function for each state. At the end of the iteration, the read function * should call cpu_commit(). */ -static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now) /* {{{ */ +static int cpu_stage (size_t cpu_num, size_t state, derive_t d, cdtime_t now) /* {{{ */ { int status; cpu_state_t *s; - value_t v; + gauge_t rate = NAN; + value_t val = {.derive = d}; if (state >= COLLECTD_CPU_STATE_ACTIVE) return (EINVAL); @@ -557,12 +555,11 @@ static int cpu_stage (size_t cpu_num, size_t state, derive_t value, cdtime_t now s = get_cpu_state (cpu_num, state); - v.gauge = NAN; - status = value_to_rate (&v, value, &s->conv, DS_TYPE_DERIVE, now); + status = value_to_rate (&rate, val, DS_TYPE_DERIVE, now, &s->conv); if (status != 0) return (status); - s->rate = v.gauge; + s->rate = rate; s->has_value = 1; return (0); } /* }}} int cpu_stage */ @@ -595,16 +592,16 @@ static int cpu_read (void) continue; } - if (cpu_info_len < COLLECTD_CPU_STATE_MAX) + if (cpu_info_len < CPU_STATE_MAX) { ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len); continue; } - cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_USER], now); - cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_NICE], now); - cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_SYSTEM], now); - cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[COLLECTD_CPU_STATE_IDLE], now); + cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER], now); + cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE], now); + cpu_stage (cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM], now); + cpu_stage (cpu, COLLECTD_CPU_STATE_IDLE, (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE], now); } /* }}} #endif PROCESSOR_CPU_LOAD_INFO */ @@ -817,8 +814,7 @@ static int cpu_read (void) if (pnumcpu != numcpu || perfcpu == NULL) { - if (perfcpu != NULL) - free(perfcpu); + free(perfcpu); perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t)); } pnumcpu = numcpu;