X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcpufreq.c;h=80f586db640e55937d4de7bedf889d888c6a0e4c;hb=75d4c13daf6c1226710b64ff3a793afa31721b81;hp=b92b1d095226c6a9deea13fa6731d5a68b213478;hpb=e8999694aac7184ac4eea29564a2892f188c4171;p=collectd.git diff --git a/src/cpufreq.c b/src/cpufreq.c index b92b1d09..80f586db 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -21,6 +21,7 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" @@ -28,110 +29,94 @@ static int num_cpu = 0; -static int cpufreq_init (void) -{ - int status; - char filename[256]; +static int cpufreq_init(void) { + int status; + char filename[256]; - num_cpu = 0; + num_cpu = 0; - while (1) - { - status = ssnprintf (filename, sizeof (filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/" - "scaling_cur_freq", num_cpu); - if ((status < 1) || ((unsigned int)status >= sizeof (filename))) - break; + while (1) { + status = ssnprintf(filename, sizeof(filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/" + "scaling_cur_freq", + num_cpu); + if ((status < 1) || ((unsigned int)status >= sizeof(filename))) + break; - if (access (filename, R_OK)) - break; + if (access(filename, R_OK)) + break; - num_cpu++; - } + num_cpu++; + } - INFO ("cpufreq plugin: Found %d CPU%s", num_cpu, - (num_cpu == 1) ? "" : "s"); + INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s"); - if (num_cpu == 0) - plugin_unregister_read ("cpufreq"); + if (num_cpu == 0) + plugin_unregister_read("cpufreq"); - return (0); + return (0); } /* int cpufreq_init */ -static void cpufreq_submit (int cpu_num, double value) -{ - value_t values[1]; - value_list_t vl = VALUE_LIST_INIT; +static void cpufreq_submit(int cpu_num, double value) { + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = value; + values[0].gauge = value; - vl.values = values; - vl.values_len = 1; - sstrncpy (vl.host, hostname_g, sizeof (vl.host)); - sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin)); - sstrncpy (vl.type, "cpufreq", sizeof (vl.type)); - ssnprintf (vl.type_instance, sizeof (vl.type_instance), - "%i", cpu_num); + vl.values = values; + vl.values_len = 1; + sstrncpy(vl.host, hostname_g, sizeof(vl.host)); + sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin)); + sstrncpy(vl.type, "cpufreq", sizeof(vl.type)); + ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%i", cpu_num); - plugin_dispatch_values (&vl); + plugin_dispatch_values(&vl); } -static int cpufreq_read (void) -{ - int status; - unsigned long long val; - int i = 0; - FILE *fp; - char filename[256]; - char buffer[16]; - - for (i = 0; i < num_cpu; i++) - { - status = ssnprintf (filename, sizeof (filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/" - "scaling_cur_freq", i); - if ((status < 1) || ((unsigned int)status >= sizeof (filename))) - return (-1); - - if ((fp = fopen (filename, "r")) == NULL) - { - char errbuf[1024]; - WARNING ("cpufreq: fopen (%s): %s", filename, - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); - } - - if (fgets (buffer, 16, fp) == NULL) - { - char errbuf[1024]; - WARNING ("cpufreq: fgets: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - fclose (fp); - return (-1); - } - - if (fclose (fp)) - { - char errbuf[1024]; - WARNING ("cpufreq: fclose: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - } - - - /* You're seeing correctly: The file is reporting kHz values.. */ - val = atoll (buffer) * 1000; - - cpufreq_submit (i, val); - } - - return (0); +static int cpufreq_read(void) { + int status; + unsigned long long val; + FILE *fp; + char filename[256]; + char buffer[16]; + + for (int i = 0; i < num_cpu; i++) { + status = ssnprintf(filename, sizeof(filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/" + "scaling_cur_freq", + i); + if ((status < 1) || ((unsigned int)status >= sizeof(filename))) + return (-1); + + if ((fp = fopen(filename, "r")) == NULL) { + char errbuf[1024]; + WARNING("cpufreq: fopen (%s): %s", filename, + sstrerror(errno, errbuf, sizeof(errbuf))); + return (-1); + } + + if (fgets(buffer, 16, fp) == NULL) { + char errbuf[1024]; + WARNING("cpufreq: fgets: %s", sstrerror(errno, errbuf, sizeof(errbuf))); + fclose(fp); + return (-1); + } + + if (fclose(fp)) { + char errbuf[1024]; + WARNING("cpufreq: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf))); + } + + /* You're seeing correctly: The file is reporting kHz values.. */ + val = atoll(buffer) * 1000; + + cpufreq_submit(i, val); + } + + return (0); } /* int cpufreq_read */ -void module_register (void) -{ - plugin_register_init ("cpufreq", cpufreq_init); - plugin_register_read ("cpufreq", cpufreq_read); +void module_register(void) { + plugin_register_init("cpufreq", cpufreq_init); + plugin_register_read("cpufreq", cpufreq_read); }