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 long long time_prev[MAX_AVAIL_FREQS];
34 long long transitions;
37 /* Flags denoting capability of reporting stats. */
38 unsigned report_time_in_state, report_total_trans;
40 static int counter_init(void){
41 t_data = calloc(num_cpu, sizeof(struct thread_data));
45 report_time_in_state = 1;
46 report_total_trans = 1;
48 /* Initialize time in state counters */
49 for (int i = 0; i < num_cpu; i++) {
53 char state[DATA_MAX_NAME_LEN], buffer[DATA_MAX_NAME_LEN];
56 snprintf(filename, sizeof(filename),
57 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
58 fh = fopen(filename, "r");
60 report_time_in_state = 0;
63 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
64 if (!sscanf(buffer, "%s%lli", state, &t)) {
68 t_data[i].time_prev[j] = t;
73 /* Initialize total transitions for cpu frequency */
74 snprintf(filename, sizeof(filename),
75 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
76 fh = fopen(filename, "r");
78 report_total_trans = 0;
81 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
82 if (!sscanf(buffer, "%lli", &t)) {
86 t_data[i].transitions = t;
93 static int cpufreq_init(void) {
100 status = snprintf(filename, sizeof(filename),
101 "/sys/devices/system/cpu/cpu%d/cpufreq/"
104 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
107 if (access(filename, R_OK))
113 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
117 plugin_unregister_read("cpufreq");
120 } /* int cpufreq_init */
122 static void cpufreq_submit(int cpu_num, const char *type, 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);
137 static int cpufreq_read(void) {
138 for (int i = 0; i < num_cpu; i++) {
139 char filename[PATH_MAX];
142 char buffer[DATA_MAX_NAME_LEN];
143 /* Read cpu frequency */
144 snprintf(filename, sizeof(filename),
145 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
148 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
149 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
153 /* convert kHz to Hz */
156 cpufreq_submit(i, "cpufreq", NULL, v);
158 /* Read total transitions for cpu frequency */
159 if (report_total_trans) {
160 snprintf(filename, sizeof(filename),
161 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
162 fh = fopen(filename, "r");
165 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
166 if (!sscanf(buffer, "%lli", &t)) {
170 snprintf(buffer, sizeof(buffer), "%lli", t - t_data[i].transitions);
171 t_data[i].transitions = t;
173 if (parse_value(buffer, &v, DS_TYPE_GAUGE) != 0) {
174 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
180 cpufreq_submit(i, "transitions", NULL, v);
183 /* Determine time in state for cpu during previous interval
184 * Reported in 10mS as unit.
186 if (report_time_in_state) {
188 char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
191 snprintf(filename, sizeof(filename),
192 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
193 fh = fopen(filename, "r");
196 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
197 if (!sscanf(buffer, "%s%lli", state, &t)) {
201 snprintf(time, sizeof(time), "%lli", t - t_data[i].time_prev[j]);
202 if (parse_value(time, &val, DS_TYPE_GAUGE) != 0) {
203 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
207 cpufreq_submit(i, "time_in_state", state, val);
208 t_data[i].time_prev[j] = t;
215 } /* int cpufreq_read */
217 void module_register(void) {
218 plugin_register_init("cpufreq", cpufreq_init);
219 plugin_register_read("cpufreq", cpufreq_read);