0f6cd32bce1ba28835f126f6d0a99afb9f5b8668
[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/types.h>
30 #include <sys/sysctl.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     plugin_unregister_read("cpufreq");
114 #endif
115
116   return 0;
117 } /* int cpufreq_init */
118
119 static void cpufreq_submit(int cpu_num, const char *type,
120                            const char *type_instance, value_t *value) {
121   value_list_t vl = VALUE_LIST_INIT;
122
123   vl.values = value;
124   vl.values_len = 1;
125   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
126   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
127   if (type != NULL)
128     sstrncpy(vl.type, type, sizeof(vl.type));
129   if (type_instance != NULL)
130     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
131
132   plugin_dispatch_values(&vl);
133 }
134
135 #if KERNEL_LINUX
136 static void cpufreq_read_stats(int cpu) {
137   char filename[PATH_MAX];
138   /* Read total transitions for cpu frequency */
139   snprintf(filename, sizeof(filename),
140            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
141
142   value_t v;
143   if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
144     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
145     return;
146   }
147   cpufreq_submit(cpu, "transitions", NULL, &v);
148
149   /* Determine percentage time in each state for cpu during previous
150    * interval. */
151   snprintf(filename, sizeof(filename),
152            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
153
154   FILE *fh = fopen(filename, "r");
155   if (fh == NULL) {
156     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
157     return;
158   }
159
160   int state_index = 0;
161   cdtime_t now = cdtime();
162   char buffer[DATA_MAX_NAME_LEN];
163
164   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
165     unsigned int frequency;
166     unsigned long long time;
167
168     /*
169      * State time units is 10ms. To get rate of seconds per second
170      * we have to divide by 100. To get percents we have to multiply it
171      * by 100 back. So, just use parsed value directly.
172      */
173     if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
174       ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
175       break;
176     }
177
178     char state[DATA_MAX_NAME_LEN];
179     snprintf(state, sizeof(state), "%u", frequency);
180
181     if (state_index >= MAX_AVAIL_FREQS) {
182       NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
183              "Plugin needs to be recompiled. Please open a bug report for "
184              "this.",
185              (state_index + 1), MAX_AVAIL_FREQS);
186       break;
187     }
188
189     gauge_t g;
190     if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
191                       &(cpu_data[cpu].time_state[state_index])) == 0) {
192       /*
193        * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
194        * That produces gaps on charts.
195        */
196       if (g > 100.1)
197         g = 100.1;
198       cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
199     }
200     state_index++;
201   }
202   fclose(fh);
203 }
204 #endif /* KERNEL_LINUX */
205
206 static int cpufreq_read(void) {
207 #if KERNEL_LINUX
208   for (int cpu = 0; cpu < num_cpu; cpu++) {
209     char filename[PATH_MAX];
210     /* Read cpu frequency */
211     snprintf(filename, sizeof(filename),
212              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
213
214     value_t v;
215     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
216       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
217       continue;
218     }
219
220     /* convert kHz to Hz */
221     v.gauge *= 1000.0;
222
223     cpufreq_submit(cpu, "cpufreq", NULL, &v);
224
225     if (report_p_stats)
226       cpufreq_read_stats(cpu);
227   }
228 #elif KERNEL_FREEBSD
229   /* FreeBSD currently only has 1 freq setting.  See BUGS in cpufreq(4) */
230   char mib[] = "dev.cpu.0.freq";
231   int cpufreq;
232   size_t cf_len = sizeof(cpufreq);
233
234   if(sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
235     WARNING("cpufreq plugin: reading \"%s\" failed.", mib);
236     return 0;
237   }
238
239   value_t v;
240   /* convert Mhz to Hz */
241   v.gauge = cpufreq * 1000000.0;
242
243   cpufreq_submit(0, "cpufreq", NULL, &v);
244 #endif
245   return 0;
246 } /* int cpufreq_read */
247
248 void module_register(void) {
249   plugin_register_init("cpufreq", cpufreq_init);
250   plugin_register_read("cpufreq", cpufreq_read);
251 }