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   value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
34   long long transition_prev;
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   /* Check for stats module and disable if not present. */
49   for (int i = 0; i < num_cpu; i++) {
50     char filename[256];
51     int status;
52     status = snprintf(filename, sizeof(filename),
53                       "/sys/devices/system/cpu/cpu%d/cpufreq/"
54                       "stats/time_in_state",
55                       num_cpu);
56     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
57       report_time_in_state = 0;
58
59     status = snprintf(filename, sizeof(filename),
60                       "/sys/devices/system/cpu/cpu%d/cpufreq/"
61                       "stats/total_transitions",
62                       num_cpu);
63     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
64       report_total_trans = 0;
65
66     /* Initialize total transitions for cpu frequency */
67     if (report_total_trans) {
68       value_t v;
69       snprintf(filename, sizeof(filename),
70                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
71       if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
72         WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
73         continue;
74       }
75       t_data[i].transition_prev = v.counter;
76     }
77   }
78   return 0;
79 }
80
81 static int cpufreq_init(void) {
82   int status;
83   char filename[256];
84
85   num_cpu = 0;
86
87   while (1) {
88     status = snprintf(filename, sizeof(filename),
89                       "/sys/devices/system/cpu/cpu%d/cpufreq/"
90                       "scaling_cur_freq",
91                       num_cpu);
92     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
93       break;
94
95     if (access(filename, R_OK))
96       break;
97
98     num_cpu++;
99   }
100
101   INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
102   counter_init();
103
104   if (num_cpu == 0)
105     plugin_unregister_read("cpufreq");
106
107   return 0;
108 } /* int cpufreq_init */
109
110 static void cpufreq_submit(int cpu_num, const char *type,
111                            const char *type_instance, value_t *value) {
112   value_list_t vl = VALUE_LIST_INIT;
113
114   vl.values = value;
115   vl.values_len = 1;
116   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
117   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
118   if (type != NULL)
119     sstrncpy(vl.type, type, sizeof(vl.type));
120   if (type_instance != NULL)
121     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
122
123   plugin_dispatch_values(&vl);
124 }
125
126 static int cpufreq_read(void) {
127   for (int i = 0; i < num_cpu; i++) {
128     char filename[PATH_MAX];
129     FILE *fh;
130     long long t;
131     char buffer[DATA_MAX_NAME_LEN];
132     /* Read cpu frequency */
133     snprintf(filename, sizeof(filename),
134              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
135
136     value_t v;
137     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
138       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
139       continue;
140     }
141
142     /* convert kHz to Hz */
143     v.gauge *= 1000.0;
144
145     cpufreq_submit(i, "cpufreq", NULL, &v);
146
147     /* Read total transitions for cpu frequency */
148     if (report_total_trans) {
149       snprintf(filename, sizeof(filename),
150                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
151       if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
152         WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
153         continue;
154       }
155       counter_t c = counter_diff(t_data[i].transition_prev, v.counter);
156       t_data[i].transition_prev = v.counter;
157       cpufreq_submit(i, "transitions", NULL, &(value_t){.counter = c});
158     }
159
160     /*
161      * Determine percentage time in each state for cpu during previous interval.
162      */
163     if (report_time_in_state) {
164       int j = 0;
165       char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
166
167       snprintf(filename, sizeof(filename),
168                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
169       fh = fopen(filename, "r");
170       if (fh == NULL)
171         continue;
172       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
173         if (!sscanf(buffer, "%s%lli", state, &t)) {
174           fclose(fh);
175           return 0;
176         }
177         snprintf(time, sizeof(time), "%lli", t);
178         if (parse_value(time, &v, DS_TYPE_DERIVE) != 0) {
179           WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
180           continue;
181         }
182         cdtime_t now = cdtime();
183         gauge_t g;
184         if (j < MAX_AVAIL_FREQS) {
185           if (value_to_rate(&g, v, DS_TYPE_DERIVE, now,
186                             &t_data[i].time_state[j]) != 0) {
187             continue;
188             j++;
189           }
190           cpufreq_submit(i, "percent", state, &(value_t){.gauge = g});
191         }
192         j++;
193       }
194       fclose(fh);
195     }
196   }
197   return 0;
198 } /* int cpufreq_read */
199
200 void module_register(void) {
201   plugin_register_init("cpufreq", cpufreq_init);
202   plugin_register_read("cpufreq", cpufreq_read);
203 }