X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcpufreq.c;h=a2cc3ad242ab72058888e069c65cddc438c2e559;hb=e1bfa71aca1f37c2f293dc9adb44065c6e7a9ad9;hp=797bee706d52de0535684a5930c8d02e97061cf9;hpb=4ff510ffa41e47f31c695c83f045fd79efecf5f4;p=collectd.git diff --git a/src/cpufreq.c b/src/cpufreq.c index 797bee70..a2cc3ad2 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -1,6 +1,6 @@ /** * collectd - src/cpufreq.c - * Copyright (C) 2005 Peter Holik + * Copyright (C) 2005-2007 Peter Holik * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -21,131 +21,82 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#define MODULE_NAME "cpufreq" - -#if defined(KERNEL_LINUX) -# define CPUFREQ_HAVE_READ 1 -#else -# define CPUFREQ_HAVE_READ 0 -#endif - -static char *cpufreq_file = "cpufreq-%s.rrd"; - -static char *ds_def[] = -{ - "DS:value:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int ds_num = 1; - -#ifdef KERNEL_LINUX static int num_cpu = 0; -#endif -#define BUFSIZE 256 - -static void cpufreq_init (void) +static int cpufreq_init (void) { -#ifdef KERNEL_LINUX int status; - char filename[BUFSIZE]; + char filename[256]; num_cpu = 0; while (1) { - status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", num_cpu); - if (status < 1 || status >= BUFSIZE) + 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)) + if (access (filename, R_OK)) break; num_cpu++; } - syslog (LOG_INFO, MODULE_NAME" found %d cpu(s)", num_cpu); -#endif /* defined(KERNEL_LINUX) */ - - return; -} + INFO ("cpufreq plugin: Found %d CPU%s", num_cpu, + (num_cpu == 1) ? "" : "s"); -static void cpufreq_write (char *host, char *inst, char *val) -{ - int status; - char file[BUFSIZE]; + if (num_cpu == 0) + plugin_unregister_read ("cpufreq"); - status = snprintf (file, BUFSIZE, cpufreq_file, inst); - if (status < 1 || status >= BUFSIZE) - return; + return (0); +} /* int cpufreq_init */ - rrd_update_file (host, file, val, ds_def, ds_num); -} - -#if CPUFREQ_HAVE_READ -static void cpufreq_submit (int cpu_num, unsigned long long val) +static void cpufreq_submit (int cpu_num, value_t value) { - char buf[BUFSIZE]; - char cpu[16]; + value_list_t vl = VALUE_LIST_INIT; - if (snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, val) >= BUFSIZE) - return; - snprintf (cpu, 16, "%i", cpu_num); + vl.values = &value; + 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_submit (MODULE_NAME, cpu, buf); + plugin_dispatch_values (&vl); } -static void cpufreq_read (void) +static int cpufreq_read (void) { -#ifdef KERNEL_LINUX - int status; - unsigned long long val; - int i = 0; - FILE *fp; - char filename[BUFSIZE]; - char buffer[16]; - - for (i = 0; i < num_cpu; i++) + for (int i = 0; i < num_cpu; i++) { - status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i); - if (status < 1 || status >= BUFSIZE) - return; - - if ((fp = fopen (filename, "r")) == NULL) - { - syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno)); - return; - } + char filename[PATH_MAX]; + ssnprintf (filename, sizeof (filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i); - if (fgets (buffer, 16, fp) == NULL) + value_t v; + if (parse_value_file (filename, &v, DS_TYPE_GAUGE) != 0) { - syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno)); - return; + WARNING ("cpufreq plugin: Reading \"%s\" failed.", filename); + continue; } - if (fclose (fp)) - syslog (LOG_WARNING, "cpufreq: fclose: %s", strerror (errno)); - - /* You're seeing correctly: The file is reporting kHz values.. */ - val = atoll (buffer) * 1000; + /* convert kHz to Hz */ + v.gauge *= 1000.0; - cpufreq_submit (i, val); + cpufreq_submit (i, v); } -#endif /* defined(KERNEL_LINUX) */ - return; -} -#else -#define cpufreq_read NULL -#endif -#undef BUFSIZE + return (0); +} /* int cpufreq_read */ void module_register (void) { - plugin_register (MODULE_NAME, cpufreq_init, cpufreq_read, cpufreq_write); + plugin_register_init ("cpufreq", cpufreq_init); + plugin_register_read ("cpufreq", cpufreq_read); } - -#undef MODULE_NAME