From: Pavel Rochnyack Date: Sun, 21 Oct 2018 16:51:12 +0000 (+0700) Subject: cpufreq plugin: Address code review comments X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=11ce5d9e615cf2a58ea25e7571079ee8b03e1de3 cpufreq plugin: Address code review comments --- diff --git a/src/cpufreq.c b/src/cpufreq.c index 6c2689ff..3e3244cc 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -116,6 +116,69 @@ static void cpufreq_submit(int cpu_num, const char *type, plugin_dispatch_values(&vl); } +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) { + cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g}); + } + state_index++; + } + fclose(fh); +} + static int cpufreq_read(void) { for (int cpu = 0; cpu < num_cpu; cpu++) { char filename[PATH_MAX]; @@ -134,66 +197,8 @@ static int cpufreq_read(void) { cpufreq_submit(cpu, "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", cpu); - if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) { - ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); - continue; - } - cpufreq_submit(cpu, "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", - cpu); - - FILE *fh = fopen(filename, "r"); - if (fh == NULL) { - ERROR("cpufreq plugin: Reading \"%s\" failed.", filename); - continue; - } - - 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 much 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){.counter = time}, DS_TYPE_COUNTER, now, - &(cpu_data[cpu].time_state[state_index])) == 0) { - cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g}); - } - state_index++; - } - fclose(fh); - } + if (report_p_stats) + cpufreq_read_stats(cpu); } return 0; } /* int cpufreq_read */ diff --git a/src/types.db b/src/types.db index 0370b7f0..e9de64fc 100644 --- a/src/types.db +++ b/src/types.db @@ -260,6 +260,7 @@ total_threads value:DERIVE:0:U total_time_in_ms value:DERIVE:0:U total_values value:DERIVE:0:U turbo_enabled value:GAUGE:0:1 +transitions value:DERIVE:0:U uptime value:GAUGE:0:4294967295 uncore_ratio value:GAUGE:0:U users value:GAUGE:0:65535