Fix compile time issues
[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 "plugin.h"
26 #include "utils/common/common.h"
27
28 #if KERNEL_FREEBSD
29 #include <sys/sysctl.h>
30 #include <sys/types.h>
31 #endif
32
33 #if KERNEL_LINUX
34 #define MAX_AVAIL_FREQS 20
35
36 static int num_cpu;
37
38 struct cpu_data_t {
39   value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
40 } * cpu_data;
41
42 /* Flags denoting capability of reporting CPU frequency statistics. */
43 static bool report_p_stats = false;
44
45 static void cpufreq_stats_init(void) {
46   cpu_data = calloc(num_cpu, sizeof(*cpu_data));
47   if (cpu_data == NULL)
48     return;
49
50   report_p_stats = true;
51
52   /* Check for stats module and disable if not present. */
53   for (int i = 0; i < num_cpu; i++) {
54     char filename[PATH_MAX];
55
56     snprintf(filename, sizeof(filename),
57              "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
58     if (access(filename, R_OK)) {
59       NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
60              "statistics will not be reported. Check if `cpufreq-stats' kernel "
61              "module is loaded.",
62              filename);
63       report_p_stats = false;
64       break;
65     }
66
67     snprintf(filename, sizeof(filename),
68              "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
69     if (access(filename, R_OK)) {
70       NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
71              "statistics will not be reported. Check if `cpufreq-stats' kernel "
72              "module is loaded.",
73              filename);
74       report_p_stats = false;
75       break;
76     }
77   }
78   return;
79 }
80 #endif /* KERNEL_LINUX */
81
82 static int cpufreq_init(void) {
83 #if KERNEL_LINUX
84   char filename[PATH_MAX];
85
86   num_cpu = 0;
87
88   while (1) {
89     int status = snprintf(filename, sizeof(filename),
90                           "/sys/devices/system/cpu/cpu%d/cpufreq/"
91                           "scaling_cur_freq",
92                           num_cpu);
93     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
94       break;
95
96     if (access(filename, R_OK))
97       break;
98
99     num_cpu++;
100   }
101
102   INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
103   cpufreq_stats_init();
104
105   if (num_cpu == 0)
106     plugin_unregister_read("cpufreq");
107 #elif KERNEL_FREEBSD
108   char mib[] = "dev.cpu.0.freq";
109   int cpufreq;
110   size_t cf_len = sizeof(cpufreq);
111
112   if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
113     WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
114     plugin_unregister_read("cpufreq");
115   }
116 #endif
117
118   return 0;
119 } /* int cpufreq_init */
120
121 static void cpufreq_submit(int cpu_num, const char *type,
122                            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 #if KERNEL_LINUX
138 static void cpufreq_read_stats(int cpu) {
139   char filename[PATH_MAX];
140   /* Read total transitions for cpu frequency */
141   snprintf(filename, sizeof(filename),
142            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
143
144   value_t v;
145   if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
146     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
147     return;
148   }
149   cpufreq_submit(cpu, "transitions", NULL, &v);
150
151   /* Determine percentage time in each state for cpu during previous
152    * interval. */
153   snprintf(filename, sizeof(filename),
154            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
155
156   FILE *fh = fopen(filename, "r");
157   if (fh == NULL) {
158     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
159     return;
160   }
161
162   int state_index = 0;
163   cdtime_t now = cdtime();
164   char buffer[DATA_MAX_NAME_LEN];
165
166   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
167     unsigned int frequency;
168     unsigned long long time;
169
170     /*
171      * State time units is 10ms. To get rate of seconds per second
172      * we have to divide by 100. To get percents we have to multiply it
173      * by 100 back. So, just use parsed value directly.
174      */
175     if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
176       ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
177       break;
178     }
179
180     char state[DATA_MAX_NAME_LEN];
181     snprintf(state, sizeof(state), "%u", frequency);
182
183     if (state_index >= MAX_AVAIL_FREQS) {
184       NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
185              "Plugin needs to be recompiled. Please open a bug report for "
186              "this.",
187              (state_index + 1), MAX_AVAIL_FREQS);
188       break;
189     }
190
191     gauge_t g;
192     if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
193                       &(cpu_data[cpu].time_state[state_index])) == 0) {
194       /*
195        * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
196        * That produces gaps on charts.
197        */
198       if (g > 100.1)
199         g = 100.1;
200       cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
201     }
202     state_index++;
203   }
204   fclose(fh);
205 }
206 #endif /* KERNEL_LINUX */
207
208 static int cpufreq_read(void) {
209 #if KERNEL_LINUX
210   for (int cpu = 0; cpu < num_cpu; cpu++) {
211     char filename[PATH_MAX];
212     /* Read cpu frequency */
213     snprintf(filename, sizeof(filename),
214              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
215
216     value_t v;
217     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
218       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
219       continue;
220     }
221
222     /* convert kHz to Hz */
223     v.gauge *= 1000.0;
224
225     cpufreq_submit(cpu, "cpufreq", NULL, &v);
226
227     if (report_p_stats)
228       cpufreq_read_stats(cpu);
229   }
230 #elif KERNEL_FREEBSD
231   /* FreeBSD currently only has 1 freq setting.  See BUGS in cpufreq(4) */
232   char mib[] = "dev.cpu.0.freq";
233   int cpufreq;
234   size_t cf_len = sizeof(cpufreq);
235
236   if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
237     WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
238     return 0;
239   }
240
241   value_t v;
242   /* convert Mhz to Hz */
243   v.gauge = cpufreq * 1000000.0;
244
245   cpufreq_submit(0, "cpufreq", NULL, &v);
246 #endif
247   return 0;
248 } /* int cpufreq_read */
249
250 void module_register(void) {
251   plugin_register_init("cpufreq", cpufreq_init);
252   plugin_register_read("cpufreq", cpufreq_read);
253 }