X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcpufreq.c;h=f95b2828a5246806476fc3626809b4457692683c;hb=0e73c26f5670b2f11eecfdadaf545bcc7f260658;hp=1441f0b9be8849ed59681ddc92fd7b6ec4202568;hpb=a03f5073767f2d2716c40b8d739426440bb8bf5f;p=collectd.git diff --git a/src/cpufreq.c b/src/cpufreq.c index 1441f0b9..f95b2828 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -22,9 +22,15 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" +#if KERNEL_FREEBSD +#include +#include +#endif + +#if KERNEL_LINUX #define MAX_AVAIL_FREQS 20 static int num_cpu; @@ -37,7 +43,7 @@ struct cpu_data_t { static bool report_p_stats = false; static void cpufreq_stats_init(void) { - cpu_data = calloc(num_cpu, sizeof(struct cpu_data_t)); + cpu_data = calloc(num_cpu, sizeof(*cpu_data)); if (cpu_data == NULL) return; @@ -71,8 +77,10 @@ static void cpufreq_stats_init(void) { } return; } +#endif /* KERNEL_LINUX */ static int cpufreq_init(void) { +#if KERNEL_LINUX char filename[PATH_MAX]; num_cpu = 0; @@ -96,6 +104,16 @@ static int cpufreq_init(void) { if (num_cpu == 0) plugin_unregister_read("cpufreq"); +#elif KERNEL_FREEBSD + char mib[] = "dev.cpu.0.freq"; + int cpufreq; + size_t cf_len = sizeof(cpufreq); + + if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) { + WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib); + plugin_unregister_read("cpufreq"); + } +#endif return 0; } /* int cpufreq_init */ @@ -116,12 +134,84 @@ static void cpufreq_submit(int cpu_num, const char *type, plugin_dispatch_values(&vl); } +#if KERNEL_LINUX +static void cpufreq_read_stats(int cpu) { + char filename[PATH_MAX]; + /* Read total transitions for cpu frequency */ + snprintf(filename, sizeof(filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu); + + value_t v; + if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) { + ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); + return; + } + cpufreq_submit(cpu, "transitions", NULL, &v); + + /* Determine percentage time in each state for cpu during previous + * interval. */ + snprintf(filename, sizeof(filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu); + + FILE *fh = fopen(filename, "r"); + if (fh == NULL) { + ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); + return; + } + + int state_index = 0; + cdtime_t now = cdtime(); + char buffer[DATA_MAX_NAME_LEN]; + + while (fgets(buffer, sizeof(buffer), fh) != NULL) { + unsigned int frequency; + unsigned long long time; + + /* + * State time units is 10ms. To get rate of seconds per second + * we have to divide by 100. To get percents we have to multiply it + * by 100 back. So, just use parsed value directly. + */ + if (!sscanf(buffer, "%u%llu", &frequency, &time)) { + ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); + break; + } + + char state[DATA_MAX_NAME_LEN]; + snprintf(state, sizeof(state), "%u", frequency); + + if (state_index >= MAX_AVAIL_FREQS) { + NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). " + "Plugin needs to be recompiled. Please open a bug report for " + "this.", + (state_index + 1), MAX_AVAIL_FREQS); + break; + } + + gauge_t g; + if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now, + &(cpu_data[cpu].time_state[state_index])) == 0) { + /* + * Due to some inaccuracy reported value can be a bit greatrer than 100.1. + * That produces gaps on charts. + */ + if (g > 100.1) + g = 100.1; + cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g}); + } + state_index++; + } + fclose(fh); +} +#endif /* KERNEL_LINUX */ + static int cpufreq_read(void) { - for (int i = 0; i < num_cpu; i++) { +#if KERNEL_LINUX + for (int cpu = 0; cpu < num_cpu; cpu++) { char filename[PATH_MAX]; /* Read cpu frequency */ snprintf(filename, sizeof(filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i); + "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu); value_t v; if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) { @@ -132,67 +222,28 @@ static int cpufreq_read(void) { /* convert kHz to Hz */ v.gauge *= 1000.0; - cpufreq_submit(i, "cpufreq", NULL, &v); - - if (report_p_stats) { - /* Read total transitions for cpu frequency */ - snprintf(filename, sizeof(filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i); - if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) { - ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); - continue; - } - cpufreq_submit(i, "counter", "transitions", &v); - - /* Determine percentage time in each state for cpu during previous - * interval. */ - snprintf(filename, sizeof(filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i); - - FILE *fh = fopen(filename, "r"); - if (fh == NULL) { - ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); - continue; - } - - int j = 0; - cdtime_t now = cdtime(); - char buffer[DATA_MAX_NAME_LEN]; - - while (fgets(buffer, sizeof(buffer), fh) != NULL) { - unsigned long long time; - char state[DATA_MAX_NAME_LEN]; - - /* - * State time units is 10ms. To get rate of seconds per second - * we have to divide by 100. To get percents we have to multiply it - * by 100 back. So, just use parsed value directly. - */ - if (!sscanf(buffer, "%s%llu", state, &time)) { - ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); - break; - } - - if (j < MAX_AVAIL_FREQS) { - gauge_t g; - if (value_to_rate(&g, (value_t){.counter = time}, DS_TYPE_COUNTER, - now, &(cpu_data[i].time_state[j])) != 0) { - j++; - continue; - } - cpufreq_submit(i, "percent", state, &(value_t){.gauge = g}); - } else { - NOTICE("cpufreq plugin: Found too much frequency states (%d > %d). " - "Plugin needs to be recompiled. Please open a bug report for " - "this.", - (j + 1), MAX_AVAIL_FREQS); - break; - } - j++; - } - fclose(fh); - } + cpufreq_submit(cpu, "cpufreq", NULL, &v); + + if (report_p_stats) + cpufreq_read_stats(cpu); } +#elif KERNEL_FREEBSD + /* FreeBSD currently only has 1 freq setting. See BUGS in cpufreq(4) */ + char mib[] = "dev.cpu.0.freq"; + int cpufreq; + size_t cf_len = sizeof(cpufreq); + + if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) { + WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib); + return 0; + } + + value_t v; + /* convert Mhz to Hz */ + v.gauge = cpufreq * 1000000.0; + + cpufreq_submit(0, "cpufreq", NULL, &v); +#endif return 0; } /* int cpufreq_read */