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>
26 #include "utils/common/common.h"
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
34 #define MAX_AVAIL_FREQS 20
39 value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
42 /* Flags denoting capability of reporting CPU frequency statistics. */
43 static bool report_p_stats = false;
45 static void cpufreq_stats_init(void) {
46 cpu_data = calloc(num_cpu, sizeof(*cpu_data));
50 report_p_stats = true;
52 /* Check for stats module and disable if not present. */
53 for (int i = 0; i < num_cpu; i++) {
54 char filename[PATH_MAX];
56 snprintf(filename, sizeof(filename),
57 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
58 if (access(filename, R_OK)) {
59 NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
60 "statistics will not be reported. Check if `cpufreq-stats' kernel "
63 report_p_stats = false;
67 snprintf(filename, sizeof(filename),
68 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
69 if (access(filename, R_OK)) {
70 NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
71 "statistics will not be reported. Check if `cpufreq-stats' kernel "
74 report_p_stats = false;
80 #endif /* KERNEL_LINUX */
82 static int cpufreq_init(void) {
84 char filename[PATH_MAX];
89 int status = snprintf(filename, sizeof(filename),
90 "/sys/devices/system/cpu/cpu%d/cpufreq/"
93 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
96 if (access(filename, R_OK))
102 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
103 cpufreq_stats_init();
106 plugin_unregister_read("cpufreq");
108 char mib[] = "dev.cpu.0.freq";
110 size_t cf_len = sizeof(cpufreq);
112 if(sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0)
113 plugin_unregister_read("cpufreq");
117 } /* int cpufreq_init */
119 static void cpufreq_submit(int cpu_num, const char *type,
120 const char *type_instance, value_t *value) {
121 value_list_t vl = VALUE_LIST_INIT;
125 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
126 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
128 sstrncpy(vl.type, type, sizeof(vl.type));
129 if (type_instance != NULL)
130 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
132 plugin_dispatch_values(&vl);
136 static void cpufreq_read_stats(int cpu) {
137 char filename[PATH_MAX];
138 /* Read total transitions for cpu frequency */
139 snprintf(filename, sizeof(filename),
140 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
143 if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
144 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
147 cpufreq_submit(cpu, "transitions", NULL, &v);
149 /* Determine percentage time in each state for cpu during previous
151 snprintf(filename, sizeof(filename),
152 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
154 FILE *fh = fopen(filename, "r");
156 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
161 cdtime_t now = cdtime();
162 char buffer[DATA_MAX_NAME_LEN];
164 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
165 unsigned int frequency;
166 unsigned long long time;
169 * State time units is 10ms. To get rate of seconds per second
170 * we have to divide by 100. To get percents we have to multiply it
171 * by 100 back. So, just use parsed value directly.
173 if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
174 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
178 char state[DATA_MAX_NAME_LEN];
179 snprintf(state, sizeof(state), "%u", frequency);
181 if (state_index >= MAX_AVAIL_FREQS) {
182 NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
183 "Plugin needs to be recompiled. Please open a bug report for "
185 (state_index + 1), MAX_AVAIL_FREQS);
190 if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
191 &(cpu_data[cpu].time_state[state_index])) == 0) {
193 * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
194 * That produces gaps on charts.
198 cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
204 #endif /* KERNEL_LINUX */
206 static int cpufreq_read(void) {
208 for (int cpu = 0; cpu < num_cpu; cpu++) {
209 char filename[PATH_MAX];
210 /* Read cpu frequency */
211 snprintf(filename, sizeof(filename),
212 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
215 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
216 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
220 /* convert kHz to Hz */
223 cpufreq_submit(cpu, "cpufreq", NULL, &v);
226 cpufreq_read_stats(cpu);
229 /* FreeBSD currently only has 1 freq setting. See BUGS in cpufreq(4) */
230 char mib[] = "dev.cpu.0.freq";
232 size_t cf_len = sizeof(cpufreq);
234 if(sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
235 WARNING("cpufreq plugin: reading \"%s\" failed.", mib);
240 /* convert Mhz to Hz */
241 v.gauge = cpufreq * 1000000.0;
243 cpufreq_submit(0, "cpufreq", NULL, &v);
246 } /* int cpufreq_read */
248 void module_register(void) {
249 plugin_register_init("cpufreq", cpufreq_init);
250 plugin_register_read("cpufreq", cpufreq_read);