X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fcpufreq.c;h=f95b2828a5246806476fc3626809b4457692683c;hp=3e3244cc41f38dfd33f118991b24ef3de2183710;hb=0e1e19f9e05c1b6a17f4a534cd0abd592fcab02e;hpb=ffd3d91a3523381554d2199d97cb0fa538d1985b diff --git a/src/cpufreq.c b/src/cpufreq.c index 3e3244cc..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,6 +134,7 @@ 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 */ @@ -172,14 +191,22 @@ static void cpufreq_read_stats(int cpu) { 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) { +#if KERNEL_LINUX for (int cpu = 0; cpu < num_cpu; cpu++) { char filename[PATH_MAX]; /* Read cpu frequency */ @@ -200,6 +227,23 @@ static int cpufreq_read(void) { 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 */