6c2689ffceab317be88076a70021abc7fce23c84
[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 cpu_data_t {
33   value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
34 } * cpu_data;
35
36 /* Flags denoting capability of reporting CPU frequency statistics. */
37 static bool report_p_stats = false;
38
39 static void cpufreq_stats_init(void) {
40   cpu_data = calloc(num_cpu, sizeof(struct cpu_data_t));
41   if (cpu_data == NULL)
42     return;
43
44   report_p_stats = true;
45
46   /* Check for stats module and disable if not present. */
47   for (int i = 0; i < num_cpu; i++) {
48     char filename[PATH_MAX];
49
50     snprintf(filename, sizeof(filename),
51              "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
52     if (access(filename, R_OK)) {
53       NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
54              "statistics will not be reported. Check if `cpufreq-stats' kernel "
55              "module is loaded.",
56              filename);
57       report_p_stats = false;
58       break;
59     }
60
61     snprintf(filename, sizeof(filename),
62              "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
63     if (access(filename, R_OK)) {
64       NOTICE("cpufreq plugin: File %s not exists or no access. P-State "
65              "statistics will not be reported. Check if `cpufreq-stats' kernel "
66              "module is loaded.",
67              filename);
68       report_p_stats = false;
69       break;
70     }
71   }
72   return;
73 }
74
75 static int cpufreq_init(void) {
76   char filename[PATH_MAX];
77
78   num_cpu = 0;
79
80   while (1) {
81     int status = snprintf(filename, sizeof(filename),
82                           "/sys/devices/system/cpu/cpu%d/cpufreq/"
83                           "scaling_cur_freq",
84                           num_cpu);
85     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
86       break;
87
88     if (access(filename, R_OK))
89       break;
90
91     num_cpu++;
92   }
93
94   INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
95   cpufreq_stats_init();
96
97   if (num_cpu == 0)
98     plugin_unregister_read("cpufreq");
99
100   return 0;
101 } /* int cpufreq_init */
102
103 static void cpufreq_submit(int cpu_num, const char *type,
104                            const char *type_instance, value_t *value) {
105   value_list_t vl = VALUE_LIST_INIT;
106
107   vl.values = value;
108   vl.values_len = 1;
109   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
110   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
111   if (type != NULL)
112     sstrncpy(vl.type, type, sizeof(vl.type));
113   if (type_instance != NULL)
114     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
115
116   plugin_dispatch_values(&vl);
117 }
118
119 static int cpufreq_read(void) {
120   for (int cpu = 0; cpu < num_cpu; cpu++) {
121     char filename[PATH_MAX];
122     /* Read cpu frequency */
123     snprintf(filename, sizeof(filename),
124              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
125
126     value_t v;
127     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
128       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
129       continue;
130     }
131
132     /* convert kHz to Hz */
133     v.gauge *= 1000.0;
134
135     cpufreq_submit(cpu, "cpufreq", NULL, &v);
136
137     if (report_p_stats) {
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       if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
142         ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
143         continue;
144       }
145       cpufreq_submit(cpu, "counter", "transitions", &v);
146
147       /* Determine percentage time in each state for cpu during previous
148        * interval. */
149       snprintf(filename, sizeof(filename),
150                "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state",
151                cpu);
152
153       FILE *fh = fopen(filename, "r");
154       if (fh == NULL) {
155         ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
156         continue;
157       }
158
159       int state_index = 0;
160       cdtime_t now = cdtime();
161       char buffer[DATA_MAX_NAME_LEN];
162
163       while (fgets(buffer, sizeof(buffer), fh) != NULL) {
164         unsigned int frequency;
165         unsigned long long time;
166
167         /*
168          * State time units is 10ms. To get rate of seconds per second
169          * we have to divide by 100. To get percents we have to multiply it
170          * by 100 back. So, just use parsed value directly.
171          */
172         if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
173           ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
174           break;
175         }
176
177         char state[DATA_MAX_NAME_LEN];
178         snprintf(state, sizeof(state), "%u", frequency);
179
180         if (state_index >= MAX_AVAIL_FREQS) {
181           NOTICE("cpufreq plugin: Found too much frequency states (%d > %d). "
182                  "Plugin needs to be recompiled. Please open a bug report for "
183                  "this.",
184                  (state_index + 1), MAX_AVAIL_FREQS);
185           break;
186         }
187
188         gauge_t g;
189         if (value_to_rate(&g, (value_t){.counter = time}, DS_TYPE_COUNTER, now,
190                           &(cpu_data[cpu].time_state[state_index])) == 0) {
191           cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
192         }
193         state_index++;
194       }
195       fclose(fh);
196     }
197   }
198   return 0;
199 } /* int cpufreq_read */
200
201 void module_register(void) {
202   plugin_register_init("cpufreq", cpufreq_init);
203   plugin_register_read("cpufreq", cpufreq_read);
204 }