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];
34 long long transition_prev;
37 /* Flags denoting capability of reporting stats. */
38 unsigned report_time_in_state, report_total_trans;
40 static int counter_init(void) {
41 t_data = calloc(num_cpu, sizeof(struct thread_data));
45 report_time_in_state = 1;
46 report_total_trans = 1;
48 /* Check for stats module and disable if not present. */
49 for (int i = 0; i < num_cpu; i++) {
52 status = snprintf(filename, sizeof(filename),
53 "/sys/devices/system/cpu/cpu%d/cpufreq/"
54 "stats/time_in_state",
56 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
57 report_time_in_state = 0;
59 status = snprintf(filename, sizeof(filename),
60 "/sys/devices/system/cpu/cpu%d/cpufreq/"
61 "stats/total_transitions",
63 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
64 report_total_trans = 0;
66 /* Initialize total transitions for cpu frequency */
67 if (report_total_trans) {
69 snprintf(filename, sizeof(filename),
70 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
71 if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
72 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
75 t_data[i].transition_prev = v.counter;
81 static int cpufreq_init(void) {
88 status = snprintf(filename, sizeof(filename),
89 "/sys/devices/system/cpu/cpu%d/cpufreq/"
92 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
95 if (access(filename, R_OK))
101 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
105 plugin_unregister_read("cpufreq");
108 } /* int cpufreq_init */
110 static void cpufreq_submit(int cpu_num, const char *type,
111 const char *type_instance, value_t *value) {
112 value_list_t vl = VALUE_LIST_INIT;
116 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
117 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
119 sstrncpy(vl.type, type, sizeof(vl.type));
120 if (type_instance != NULL)
121 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
123 plugin_dispatch_values(&vl);
126 static int cpufreq_read(void) {
127 for (int i = 0; i < num_cpu; i++) {
128 char filename[PATH_MAX];
131 char buffer[DATA_MAX_NAME_LEN];
132 /* Read cpu frequency */
133 snprintf(filename, sizeof(filename),
134 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
137 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
138 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
142 /* convert kHz to Hz */
145 cpufreq_submit(i, "cpufreq", NULL, &v);
147 /* Read total transitions for cpu frequency */
148 if (report_total_trans) {
149 snprintf(filename, sizeof(filename),
150 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/total_trans", i);
151 if (parse_value_file(filename, &v, DS_TYPE_COUNTER) != 0) {
152 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
155 counter_t c = counter_diff(t_data[i].transition_prev, v.counter);
156 t_data[i].transition_prev = v.counter;
157 cpufreq_submit(i, "transitions", NULL, &(value_t){.counter = c});
161 * Determine percentage time in each state for cpu during previous interval.
163 if (report_time_in_state) {
165 char state[DATA_MAX_NAME_LEN], time[DATA_MAX_NAME_LEN];
167 snprintf(filename, sizeof(filename),
168 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
169 fh = fopen(filename, "r");
172 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
173 if (!sscanf(buffer, "%s%lli", state, &t)) {
177 snprintf(time, sizeof(time), "%lli", t);
178 if (parse_value(time, &v, DS_TYPE_DERIVE) != 0) {
179 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
182 cdtime_t now = cdtime();
184 if (j < MAX_AVAIL_FREQS) {
185 if (value_to_rate(&g, v, DS_TYPE_DERIVE, now, &t_data[i].time_state[j]) != 0 ){
189 cpufreq_submit(i, "percent", state, &(value_t){.gauge = g});
197 } /* int cpufreq_read */
199 void module_register(void) {
200 plugin_register_init("cpufreq", cpufreq_init);
201 plugin_register_read("cpufreq", cpufreq_read);