aa969b0938e75f2c1d3485e7a00e2d24a1c91f9b
[collectd.git] / src / cpufreq.c
1 /**
2  * collectd - src/cpufreq.c
3  * Copyright (C) 2005-2007  Peter Holik
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  **/
22
23 #include "collectd.h"
24
25 #include "common.h"
26 #include "plugin.h"
27
28 #define MAX_AVAIL_FREQS 20
29
30 static int num_cpu;
31
32 struct thread_data {
33   long long time_prev[MAX_AVAIL_FREQS];
34   long long transitions;
35 } *t_data;
36
37 /* Flags denoting capability of reporting stats. */
38 unsigned report_time_in_state, report_total_trans;
39
40 static int counter_init(void){
41   t_data = calloc(num_cpu, sizeof(struct thread_data));
42   if (t_data == NULL)
43         return 0;
44
45   report_time_in_state = 1;
46   report_total_trans = 1;
47
48   /* Initialize time in state counters */
49   for (int i = 0; i < num_cpu; i++) {
50     char filename[256];
51     FILE *fh;
52     int j = 0;
53     char state[DATA_MAX_NAME_LEN], buffer[DATA_MAX_NAME_LEN];
54     long long t;
55
56     snprintf(filename, sizeof(filename),
57               "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
58     fh = fopen(filename, "r");
59     if (fh == NULL) {
60       report_time_in_state = 0;
61       return 0;
62     }
63     while (fgets(buffer, sizeof(buffer), fh) != NULL) {
64       if (!sscanf(buffer, "%s%lli", state, &t)) {
65         fclose(fh);
66         return 0;
67       }
68       t_data[i].time_prev[j] = t;
69       j++;
70     }
71     fclose(fh);
72
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");
77     if (fh == NULL) {
78       report_total_trans = 0;
79       return 0;
80     }
81     while (fgets(buffer, sizeof(buffer), fh) != NULL) {
82       if (!sscanf(buffer, "%lli", &t)) {
83         fclose(fh);
84         return 0;
85       }
86       t_data[i].transitions = t;
87     }
88     fclose(fh);
89   }
90   return 0;
91 }
92
93 static int cpufreq_init(void) {
94   int status;
95   char filename[256];
96
97   num_cpu = 0;
98
99   while (1) {
100     status = snprintf(filename, sizeof(filename),
101                       "/sys/devices/system/cpu/cpu%d/cpufreq/"
102                       "scaling_cur_freq",
103                       num_cpu);
104     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
105       break;
106
107     if (access(filename, R_OK))
108       break;
109
110     num_cpu++;
111   }
112
113   INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
114   counter_init();
115
116   if (num_cpu == 0)
117     plugin_unregister_read("cpufreq");
118
119   return 0;
120 } /* int cpufreq_init */
121
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;
124
125   vl.values = &value;
126   vl.values_len = 1;
127   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
128   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
129   if (type != NULL)
130     sstrncpy(vl.type, type, sizeof(vl.type));
131   if (type_instance != NULL)
132   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
133
134   plugin_dispatch_values(&vl);
135 }
136
137 static int cpufreq_read(void) {
138   for (int i = 0; i < num_cpu; i++) {
139     char filename[PATH_MAX];
140     FILE *fh;
141     long long t;
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);
146
147     value_t v;
148     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
149       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
150       continue;
151     }
152
153     /* convert kHz to Hz */
154     v.gauge *= 1000.0;
155
156     cpufreq_submit(i, "cpufreq", NULL, v);
157
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");
163       if (fh == NULL)
164         continue;
165       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
166         if (!sscanf(buffer, "%lli", &t)) {
167           fclose(fh);
168           return 0;
169         }
170         snprintf(buffer, sizeof(buffer), "%lli", t - t_data[i].transitions);
171         t_data[i].transitions = t;
172       }
173       if (parse_value(buffer, &v, DS_TYPE_GAUGE) != 0) {
174         WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
175         fclose(fh);
176         continue;
177       }
178       fclose(fh);
179
180       cpufreq_submit(i, "transitions", NULL, v);
181     }
182
183     /* Determine time in state for cpu during previous interval
184      * Reported in 10mS as unit.
185      */
186     if (report_time_in_state) {
187       int j = 0;
188       char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
189       value_t val;
190
191       snprintf(filename, sizeof(filename),
192               "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
193       fh = fopen(filename, "r");
194       if (fh == NULL)
195         continue;
196       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
197         if (!sscanf(buffer, "%s%lli", state, &t)) {
198           fclose(fh);
199           return 0;
200         }
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);
204           fclose(fh);
205           continue;
206         }
207         cpufreq_submit(i, "time_in_state", state, val);
208         t_data[i].time_prev[j] = t;
209         j++;
210       }
211       fclose(fh);
212     }
213   }
214   return 0;
215 } /* int cpufreq_read */
216
217 void module_register(void) {
218   plugin_register_init("cpufreq", cpufreq_init);
219   plugin_register_read("cpufreq", cpufreq_read);
220 }