/**
* 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
* Peter Holik <peter at holik.at>
**/
-#include "cpufreq.h"
+#include "collectd.h"
-/*
- * Originally written by Peter Holik
- */
-
-#if COLLECT_CPUFREQ
-#define MODULE_NAME "cpufreq"
-
-#include "plugin.h"
#include "common.h"
+#include "plugin.h"
-static int num_cpu = 0;
-
-static char *cpufreq_file = "cpufreq-%s.rrd";
+static int num_cpu;
-static char *ds_def[] =
-{
- "DS:value:GAUGE:25:0:U",
- NULL
-};
-static int ds_num = 1;
+static int cpufreq_init(void) {
+ int status;
+ char filename[256];
-extern time_t curtime;
+ num_cpu = 0;
-#define BUFSIZE 256
+ while (1) {
+ status = snprintf(filename, sizeof(filename),
+ "/sys/devices/system/cpu/cpu%d/cpufreq/"
+ "scaling_cur_freq",
+ num_cpu);
+ if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
+ break;
-void cpufreq_init (void)
-{
- int status;
- char filename[BUFSIZE];
+ if (access(filename, R_OK))
+ break;
- num_cpu = 0;
+ num_cpu++;
+ }
- while (1)
- {
- status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", num_cpu);
- if (status < 1 || status >= BUFSIZE)
- break;
+ INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
- if (access(filename, R_OK))
- break;
+ if (num_cpu == 0)
+ plugin_unregister_read("cpufreq");
- num_cpu++;
- }
+ return 0;
+} /* int cpufreq_init */
- syslog (LOG_INFO, MODULE_NAME" found %d cpu(s)", num_cpu);
-}
+static void cpufreq_submit(int cpu_num, value_t value) {
+ value_list_t vl = VALUE_LIST_INIT;
-void cpufreq_write (char *host, char *inst, char *val)
-{
- int status;
- char file[BUFSIZE];
+ vl.values = &value;
+ vl.values_len = 1;
+ sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
+ sstrncpy(vl.type, "cpufreq", sizeof(vl.type));
+ snprintf(vl.type_instance, sizeof(vl.type_instance), "%i", cpu_num);
- status = snprintf (file, BUFSIZE, cpufreq_file, inst);
- if (status < 1 || status >= BUFSIZE)
- return;
-
- rrd_update_file (host, file, val, ds_def, ds_num);
+ plugin_dispatch_values(&vl);
}
-void cpufreq_submit (int cpu_num, unsigned long long val)
-{
- char buf[BUFSIZE];
- char cpu[16];
+static int cpufreq_read(void) {
+ for (int i = 0; i < num_cpu; i++) {
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename),
+ "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
- if (snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, val) >= BUFSIZE)
- return;
- snprintf (cpu, 16, "%i", cpu_num);
+ value_t v;
+ if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
+ WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
+ continue;
+ }
- plugin_submit (MODULE_NAME, cpu, buf);
-}
+ /* convert kHz to Hz */
+ v.gauge *= 1000.0;
-void cpufreq_read (void)
-{
- int status;
- unsigned long long val;
- int i = 0;
- FILE *fp;
- char filename[BUFSIZE];
- char buffer[16];
-
- for (i = 0; i < num_cpu; i++)
- {
- status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", i);
- if (status < 1 || status >= BUFSIZE)
- return;
-
- if ((fp = fopen (filename, "r")) == NULL)
- {
- syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
- return;
- }
-
- if (fgets (buffer, 16, fp) == NULL)
- {
- syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
- return;
- }
-
- 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;
-
- cpufreq_submit (i, val);
- }
-}
-#undef BUFSIZE
+ cpufreq_submit(i, v);
+ }
-void module_register (void)
-{
- plugin_register (MODULE_NAME, cpufreq_init, cpufreq_read, cpufreq_write);
-}
+ return 0;
+} /* int cpufreq_read */
-#undef MODULE_NAME
-#endif /* COLLECT_CPUFREQ */
+void module_register(void) {
+ plugin_register_init("cpufreq", cpufreq_init);
+ plugin_register_read("cpufreq", cpufreq_read);
+}