Updating cpufreq.c from clang-format
[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,
123                            const char *type_instance, value_t value) {
124   value_list_t vl = VALUE_LIST_INIT;
125
126   vl.values = &value;
127   vl.values_len = 1;
128   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
129   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
130   if (type != NULL)
131     sstrncpy(vl.type, type, sizeof(vl.type));
132   if (type_instance != NULL)
133     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
134
135   plugin_dispatch_values(&vl);
136 }
137
138 static int cpufreq_read(void) {
139   for (int i = 0; i < num_cpu; i++) {
140     char filename[PATH_MAX];
141     FILE *fh;
142     long long t;
143     char buffer[DATA_MAX_NAME_LEN];
144     /* Read cpu frequency */
145     snprintf(filename, sizeof(filename),
146              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
147
148     value_t v;
149     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
150       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
151       continue;
152     }
153
154     /* convert kHz to Hz */
155     v.gauge *= 1000.0;
156
157     cpufreq_submit(i, "cpufreq", NULL, v);
158
159     /* Read total transitions for cpu frequency */
160     if (report_total_trans) {
161       snprintf(filename, sizeof(filename),
162                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
163       fh = fopen(filename, "r");
164       if (fh == NULL)
165         continue;
166       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
167         if (!sscanf(buffer, "%lli", &t)) {
168           fclose(fh);
169           return 0;
170         }
171         snprintf(buffer, sizeof(buffer), "%lli", t - t_data[i].transitions);
172         t_data[i].transitions = t;
173       }
174       if (parse_value(buffer, &v, DS_TYPE_GAUGE) != 0) {
175         WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
176         fclose(fh);
177         continue;
178       }
179       fclose(fh);
180
181       cpufreq_submit(i, "transitions", NULL, v);
182     }
183
184     /* Determine time in state for cpu during previous interval
185      * Reported in 10mS as unit.
186      */
187     if (report_time_in_state) {
188       int j = 0;
189       char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
190       value_t val;
191
192       snprintf(filename, sizeof(filename),
193                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
194       fh = fopen(filename, "r");
195       if (fh == NULL)
196         continue;
197       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
198         if (!sscanf(buffer, "%s%lli", state, &t)) {
199           fclose(fh);
200           return 0;
201         }
202         snprintf(time, sizeof(time), "%lli", t - t_data[i].time_prev[j]);
203         if (parse_value(time, &val, DS_TYPE_GAUGE) != 0) {
204           WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
205           fclose(fh);
206           continue;
207         }
208         cpufreq_submit(i, "time_in_state", state, val);
209         t_data[i].time_prev[j] = t;
210         j++;
211       }
212       fclose(fh);
213     }
214   }
215   return 0;
216 } /* int cpufreq_read */
217
218 void module_register(void) {
219   plugin_register_init("cpufreq", cpufreq_init);
220   plugin_register_read("cpufreq", cpufreq_read);
221 }