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