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>
27 #define MODULE_NAME "cpufreq"
29 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
32 # define CPUFREQ_HAVE_READ 0
35 static data_source_t data_source[1] =
37 {"value", DS_TYPE_GAUGE, 0, NAN}
40 static data_set_t data_set =
42 "cpufreq", 1, data_source
47 static int num_cpu = 0;
50 static int cpufreq_init (void)
60 status = snprintf (filename, sizeof (filename),
61 "/sys/devices/system/cpu/cpu%d/cpufreq/"
62 "scaling_cur_freq", num_cpu);
63 if (status < 1 || status >= sizeof (filename))
66 if (access (filename, R_OK))
72 INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
73 (num_cpu == 1) ? "" : "s");
76 plugin_unregister_read ("cpufreq");
77 #endif /* defined(KERNEL_LINUX) */
80 } /* int cpufreq_init */
82 static void cpufreq_submit (int cpu_num, double value)
85 value_list_t vl = VALUE_LIST_INIT;
87 values[0].gauge = value;
91 vl.time = time (NULL);
92 strcpy (vl.host, hostname_g);
93 strcpy (vl.plugin, "cpufreq");
94 snprintf (vl.type_instance, sizeof (vl.type_instance),
97 plugin_dispatch_values ("cpufreq", &vl);
100 static int cpufreq_read (void)
104 unsigned long long val;
110 for (i = 0; i < num_cpu; i++)
112 status = snprintf (filename, sizeof (filename),
113 "/sys/devices/system/cpu/cpu%d/cpufreq/"
114 "scaling_cur_freq", i);
115 if (status < 1 || status >= sizeof (filename))
118 if ((fp = fopen (filename, "r")) == NULL)
121 WARNING ("cpufreq: fopen (%s): %s", filename,
122 sstrerror (errno, errbuf,
127 if (fgets (buffer, 16, fp) == NULL)
130 WARNING ("cpufreq: fgets: %s",
131 sstrerror (errno, errbuf,
140 WARNING ("cpufreq: fclose: %s",
141 sstrerror (errno, errbuf,
146 /* You're seeing correctly: The file is reporting kHz values.. */
147 val = atoll (buffer) * 1000;
149 cpufreq_submit (i, val);
151 #endif /* defined(KERNEL_LINUX) */
154 } /* int cpufreq_read */
155 #endif /* CPUFREQ_HAVE_READ */
158 void module_register (void)
160 plugin_register_data_set (&data_set);
162 #if CPUFREQ_HAVE_READ
163 plugin_register_init ("cpufreq", cpufreq_init);
164 plugin_register_read ("cpufreq", cpufreq_read);
165 #endif /* CPUFREQ_HAVE_READ */