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