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(struct cpu_data_t));
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 int cpufreq_read(void) {
120 for (int cpu = 0; cpu < num_cpu; cpu++) {
121 char filename[PATH_MAX];
122 /* Read cpu frequency */
123 snprintf(filename, sizeof(filename),
124 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
127 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
128 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
132 /* convert kHz to Hz */
135 cpufreq_submit(cpu, "cpufreq", NULL, &v);
137 if (report_p_stats) {
138 /* Read total transitions for cpu frequency */
139 snprintf(filename, sizeof(filename),
140 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
141 if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
142 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
145 cpufreq_submit(cpu, "counter", "transitions", &v);
147 /* Determine percentage time in each state for cpu during previous
149 snprintf(filename, sizeof(filename),
150 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state",
153 FILE *fh = fopen(filename, "r");
155 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
160 cdtime_t now = cdtime();
161 char buffer[DATA_MAX_NAME_LEN];
163 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
164 unsigned int frequency;
165 unsigned long long time;
168 * State time units is 10ms. To get rate of seconds per second
169 * we have to divide by 100. To get percents we have to multiply it
170 * by 100 back. So, just use parsed value directly.
172 if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
173 ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
177 char state[DATA_MAX_NAME_LEN];
178 snprintf(state, sizeof(state), "%u", frequency);
180 if (state_index >= MAX_AVAIL_FREQS) {
181 NOTICE("cpufreq plugin: Found too much frequency states (%d > %d). "
182 "Plugin needs to be recompiled. Please open a bug report for "
184 (state_index + 1), MAX_AVAIL_FREQS);
189 if (value_to_rate(&g, (value_t){.counter = time}, DS_TYPE_COUNTER, now,
190 &(cpu_data[cpu].time_state[state_index])) == 0) {
191 cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
199 } /* int cpufreq_read */
201 void module_register(void) {
202 plugin_register_init("cpufreq", cpufreq_init);
203 plugin_register_read("cpufreq", cpufreq_read);