2 * collectd - src/cpufreq.c
3 * Copyright (C) 2005-2007 Peter Holik
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.
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.
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
20 * Peter Holik <peter at holik.at>
28 #define MAX_AVAIL_FREQS 20
33 value_to_rate_state_t time_state[MAX_AVAIL_FREQS];
36 /* Flags denoting capability of reporting stats. */
37 unsigned report_time_in_state, report_total_trans;
39 static int counter_init(void) {
40 t_data = calloc(num_cpu, sizeof(struct thread_data));
44 report_time_in_state = 1;
45 report_total_trans = 1;
47 /* Check for stats module and disable if not present. */
48 for (int i = 0; i < num_cpu; i++) {
51 status = snprintf(filename, sizeof(filename),
52 "/sys/devices/system/cpu/cpu%d/cpufreq/"
53 "stats/time_in_state",
55 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
56 report_time_in_state = 0;
58 status = snprintf(filename, sizeof(filename),
59 "/sys/devices/system/cpu/cpu%d/cpufreq/"
60 "stats/total_transitions",
62 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
63 report_total_trans = 0;
69 static int cpufreq_init(void) {
76 status = snprintf(filename, sizeof(filename),
77 "/sys/devices/system/cpu/cpu%d/cpufreq/"
80 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
83 if (access(filename, R_OK))
89 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
93 plugin_unregister_read("cpufreq");
96 } /* int cpufreq_init */
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;
104 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
105 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
107 sstrncpy(vl.type, type, sizeof(vl.type));
108 if (type_instance != NULL)
109 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
111 plugin_dispatch_values(&vl);
114 static int cpufreq_read(void) {
115 for (int i = 0; i < num_cpu; i++) {
116 char filename[PATH_MAX];
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);
125 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
126 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
130 /* convert kHz to Hz */
133 cpufreq_submit(i, "cpufreq", NULL, &v);
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);
143 cpufreq_submit(i, "counter", "transitions", &v);
147 * Determine percentage time in each state for cpu during previous interval.
149 if (report_time_in_state) {
151 char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
153 snprintf(filename, sizeof(filename),
154 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
155 fh = fopen(filename, "r");
158 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
159 if (!sscanf(buffer, "%s%lli", state, &t)) {
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);
168 cdtime_t now = cdtime();
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) {
176 cpufreq_submit(i, "percent", state, &(value_t){.gauge = g});
184 } /* int cpufreq_read */
186 void module_register(void) {
187 plugin_register_init("cpufreq", cpufreq_init);
188 plugin_register_read("cpufreq", cpufreq_read);