virt: Fetch block info stats from libvirt only if needed
[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(*cpu_data));
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 void cpufreq_read_stats(int cpu) {
120   char filename[PATH_MAX];
121   /* Read total transitions for cpu frequency */
122   snprintf(filename, sizeof(filename),
123            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", cpu);
124
125   value_t v;
126   if (parse_value_file(filename, &v, DS_TYPE_DERIVE) != 0) {
127     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
128     return;
129   }
130   cpufreq_submit(cpu, "transitions", NULL, &v);
131
132   /* Determine percentage time in each state for cpu during previous
133    * interval. */
134   snprintf(filename, sizeof(filename),
135            "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
136
137   FILE *fh = fopen(filename, "r");
138   if (fh == NULL) {
139     ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
140     return;
141   }
142
143   int state_index = 0;
144   cdtime_t now = cdtime();
145   char buffer[DATA_MAX_NAME_LEN];
146
147   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
148     unsigned int frequency;
149     unsigned long long time;
150
151     /*
152      * State time units is 10ms. To get rate of seconds per second
153      * we have to divide by 100. To get percents we have to multiply it
154      * by 100 back. So, just use parsed value directly.
155      */
156     if (!sscanf(buffer, "%u%llu", &frequency, &time)) {
157       ERROR("cpufreq plugin: Reading \"%s\" failed.", filename);
158       break;
159     }
160
161     char state[DATA_MAX_NAME_LEN];
162     snprintf(state, sizeof(state), "%u", frequency);
163
164     if (state_index >= MAX_AVAIL_FREQS) {
165       NOTICE("cpufreq plugin: Found too many frequency states (%d > %d). "
166              "Plugin needs to be recompiled. Please open a bug report for "
167              "this.",
168              (state_index + 1), MAX_AVAIL_FREQS);
169       break;
170     }
171
172     gauge_t g;
173     if (value_to_rate(&g, (value_t){.derive = time}, DS_TYPE_DERIVE, now,
174                       &(cpu_data[cpu].time_state[state_index])) == 0) {
175       /*
176        * Due to some inaccuracy reported value can be a bit greatrer than 100.1.
177        * That produces gaps on charts.
178        */
179       if (g > 100.1)
180         g = 100.1;
181       cpufreq_submit(cpu, "percent", state, &(value_t){.gauge = g});
182     }
183     state_index++;
184   }
185   fclose(fh);
186 }
187
188 static int cpufreq_read(void) {
189   for (int cpu = 0; cpu < num_cpu; cpu++) {
190     char filename[PATH_MAX];
191     /* Read cpu frequency */
192     snprintf(filename, sizeof(filename),
193              "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
194
195     value_t v;
196     if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
197       WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
198       continue;
199     }
200
201     /* convert kHz to Hz */
202     v.gauge *= 1000.0;
203
204     cpufreq_submit(cpu, "cpufreq", NULL, &v);
205
206     if (report_p_stats)
207       cpufreq_read_stats(cpu);
208   }
209   return 0;
210 } /* int cpufreq_read */
211
212 void module_register(void) {
213   plugin_register_init("cpufreq", cpufreq_init);
214   plugin_register_read("cpufreq", cpufreq_read);
215 }