2 * collectd - src/cpufreq.c
3 * Copyright (C) 2005,2006 Peter Holik
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Peter Holik <peter at holik.at>
27 #define MODULE_NAME "cpufreq"
29 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
32 # define CPUFREQ_HAVE_READ 0
35 static char *cpufreq_file = "cpufreq-%s.rrd";
37 static char *ds_def[] =
39 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":0:U",
42 static int ds_num = 1;
45 static int num_cpu = 0;
50 static void cpufreq_init (void)
54 char filename[BUFSIZE];
60 status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", num_cpu);
61 if (status < 1 || status >= BUFSIZE)
64 if (access(filename, R_OK))
70 syslog (LOG_INFO, MODULE_NAME" found %d cpu(s)", num_cpu);
71 #endif /* defined(KERNEL_LINUX) */
76 static void cpufreq_write (char *host, char *inst, char *val)
81 status = snprintf (file, BUFSIZE, cpufreq_file, inst);
82 if (status < 1 || status >= BUFSIZE)
85 rrd_update_file (host, file, val, ds_def, ds_num);
89 static void cpufreq_submit (int cpu_num, unsigned long long val)
94 if (snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, val) >= BUFSIZE)
96 snprintf (cpu, 16, "%i", cpu_num);
98 plugin_submit (MODULE_NAME, cpu, buf);
101 static void cpufreq_read (void)
105 unsigned long long val;
108 char filename[BUFSIZE];
111 for (i = 0; i < num_cpu; i++)
113 status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
114 if (status < 1 || status >= BUFSIZE)
117 if ((fp = fopen (filename, "r")) == NULL)
119 syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
123 if (fgets (buffer, 16, fp) == NULL)
125 syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
131 syslog (LOG_WARNING, "cpufreq: fclose: %s", strerror (errno));
133 /* You're seeing correctly: The file is reporting kHz values.. */
134 val = atoll (buffer) * 1000;
136 cpufreq_submit (i, val);
138 #endif /* defined(KERNEL_LINUX) */
143 #define cpufreq_read NULL
147 void module_register (void)
149 plugin_register (MODULE_NAME, cpufreq_init, cpufreq_read, cpufreq_write);