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 #define MAX_AVAIL_FREQS 20
33 value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
36 /* Flags denoting capability of reporting CPU frequency statistics. */
37 static bool report_p_stats = false;
39 static void cpufreq_stats_init(void) {
40 cpu_data = calloc(num_cpu, sizeof(*cpu_data));
44 report_p_stats = true;
46 /* Check for stats module and disable if not present. */
47 for (int i = 0; i < num_cpu; i++) {
48 char filename[PATH_MAX];
50 snprintf(filename, sizeof(filename),
51 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
52 if (access(filename, R_OK)) {
53 NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
54 "statistics will not be reported. Check if `cpufreq-stats' kernel "
57 report_p_stats = false;
61 snprintf(filename, sizeof(filename),
62 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
63 if (access(filename, R_OK)) {
64 NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
65 "statistics will not be reported. Check if `cpufreq-stats' kernel "
68 report_p_stats = false;
75 static int cpufreq_init(void) {
76 char filename[PATH_MAX];
81 int status = snprintf(filename, sizeof(filename),
82 "/sys/devices/system/cpu/cpu%d/cpufreq/"
85 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
88 if (access(filename, R_OK))
94 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
98 plugin_unregister_read("cpufreq");
101 } /* int cpufreq_init */
103 static void cpufreq_submit(int cpu_num, const char *type,
104 const char *type_instance, value_t *value) {
105 value_list_t vl = VALUE_LIST_INIT;
109 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
110 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
112 sstrncpy(vl.type, type, sizeof(vl.type));
113 if (type_instance != NULL)
114 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
116 plugin_dispatch_values(&vl);
119 static void cpufreq_read_stats(int cpu) {
120 char filename[PATH_MAX];
121 /* Read total transitions for cpu frequency */
122 snprintf(filename, sizeof(filename),
123 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
126 if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
127 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
130 cpufreq_submit(cpu, "transitions", NULL, &v);
132 /* Determine percentage time in each state for cpu during previous
134 snprintf(filename, sizeof(filename),
135 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
137 FILE *fh = fopen(filename, "r");
139 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
144 cdtime_t now = cdtime();
145 char buffer[DATA_MAX_NAME_LEN];
147 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
148 unsigned int frequency;
149 unsigned long long time;
152 * State time units is 10ms. To get rate of seconds per second
153 * we have to divide by 100. To get percents we have to multiply it
154 * by 100 back. So, just use parsed value directly.
156 if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
157 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
161 char state[DATA_MAX_NAME_LEN];
162 snprintf(state, sizeof(state), "%u", frequency);
164 if (state_index >= MAX_AVAIL_FREQS) {
165 NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
166 "Plugin needs to be recompiled. Please open a bug report for "
168 (state_index + 1), MAX_AVAIL_FREQS);
173 if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
174 &(cpu_data[cpu].time_state[state_index])) == 0) {
176 * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
177 * That produces gaps on charts.
181 cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
188 static int cpufreq_read(void) {
189 for (int cpu = 0; cpu < num_cpu; cpu++) {
190 char filename[PATH_MAX];
191 /* Read cpu frequency */
192 snprintf(filename, sizeof(filename),
193 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
196 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
197 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
201 /* convert kHz to Hz */
204 cpufreq_submit(cpu, "cpufreq", NULL, &v);
207 cpufreq_read_stats(cpu);
210 } /* int cpufreq_read */
212 void module_register(void) {
213 plugin_register_init("cpufreq", cpufreq_init);
214 plugin_register_read("cpufreq", cpufreq_read);