2 * collectd - src/cpufreq.c
3 * Copyright (C) 2005-2007 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>
28 static int num_cpu = 0;
30 static int cpufreq_init(void) {
37 status = snprintf(filename, sizeof(filename),
38 "/sys/devices/system/cpu/cpu%d/cpufreq/"
41 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
44 if (access(filename, R_OK))
50 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
53 plugin_unregister_read("cpufreq");
56 } /* int cpufreq_init */
58 static void cpufreq_submit(int cpu_num, value_t value) {
59 value_list_t vl = VALUE_LIST_INIT;
63 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
64 sstrncpy(vl.type, "cpufreq", sizeof(vl.type));
65 snprintf(vl.type_instance, sizeof(vl.type_instance), "%i", cpu_num);
67 plugin_dispatch_values(&vl);
70 static int cpufreq_read(void) {
71 for (int i = 0; i < num_cpu; i++) {
72 char filename[PATH_MAX];
73 snprintf(filename, sizeof(filename),
74 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
77 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
78 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
82 /* convert kHz to Hz */
89 } /* int cpufreq_read */
91 void module_register(void) {
92 plugin_register_init("cpufreq", cpufreq_init);
93 plugin_register_read("cpufreq", cpufreq_read);