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/sysctl.h>
30 #include <sys/types.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 WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
114 plugin_unregister_read("cpufreq");
119 } /* int cpufreq_init */
121 static void cpufreq_submit(int cpu_num, const char *type,
122 const char *type_instance, value_t *value) {
123 value_list_t vl = VALUE_LIST_INIT;
127 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
128 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
130 sstrncpy(vl.type, type, sizeof(vl.type));
131 if (type_instance != NULL)
132 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
134 plugin_dispatch_values(&vl);
138 static void cpufreq_read_stats(int cpu) {
139 char filename[PATH_MAX];
140 /* Read total transitions for cpu frequency */
141 snprintf(filename, sizeof(filename),
142 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
145 if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
146 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
149 cpufreq_submit(cpu, "transitions", NULL, &v);
151 /* Determine percentage time in each state for cpu during previous
153 snprintf(filename, sizeof(filename),
154 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
156 FILE *fh = fopen(filename, "r");
158 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
163 cdtime_t now = cdtime();
164 char buffer[DATA_MAX_NAME_LEN];
166 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
167 unsigned int frequency;
168 unsigned long long time;
171 * State time units is 10ms. To get rate of seconds per second
172 * we have to divide by 100. To get percents we have to multiply it
173 * by 100 back. So, just use parsed value directly.
175 if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
176 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
180 char state[DATA_MAX_NAME_LEN];
181 snprintf(state, sizeof(state), "%u", frequency);
183 if (state_index >= MAX_AVAIL_FREQS) {
184 NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
185 "Plugin needs to be recompiled. Please open a bug report for "
187 (state_index + 1), MAX_AVAIL_FREQS);
192 if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
193 &(cpu_data[cpu].time_state[state_index])) == 0) {
195 * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
196 * That produces gaps on charts.
200 cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
206 #endif /* KERNEL_LINUX */
208 static int cpufreq_read(void) {
210 for (int cpu = 0; cpu < num_cpu; cpu++) {
211 char filename[PATH_MAX];
212 /* Read cpu frequency */
213 snprintf(filename, sizeof(filename),
214 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
217 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
218 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
222 /* convert kHz to Hz */
225 cpufreq_submit(cpu, "cpufreq", NULL, &v);
228 cpufreq_read_stats(cpu);
231 /* FreeBSD currently only has 1 freq setting. See BUGS in cpufreq(4) */
232 char mib[] = "dev.cpu.0.freq";
234 size_t cf_len = sizeof(cpufreq);
236 if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
237 WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
242 /* convert Mhz to Hz */
243 v.gauge = cpufreq * 1000000.0;
245 cpufreq_submit(0, "cpufreq", NULL, &v);
248 } /* int cpufreq_read */
250 void module_register(void) {
251 plugin_register_init("cpufreq", cpufreq_init);
252 plugin_register_read("cpufreq", cpufreq_read);