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 cpu_data = calloc(num_cpu, sizeof(struct cpu_data_t));
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++) {
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",
54 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
55 report_time_in_state = 0;
57 status = snprintf(filename, sizeof(filename),
58 "/sys/devices/system/cpu/cpu%d/cpufreq/"
59 "stats/total_transitions",
61 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
62 report_total_trans = 0;
68 static int cpufreq_init(void) {
69 char filename[PATH_MAX];
74 int status = snprintf(filename, sizeof(filename),
75 "/sys/devices/system/cpu/cpu%d/cpufreq/"
78 if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
81 if (access(filename, R_OK))
87 INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
91 plugin_unregister_read("cpufreq");
94 } /* int cpufreq_init */
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;
102 sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
103 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num);
105 sstrncpy(vl.type, type, sizeof(vl.type));
106 if (type_instance != NULL)
107 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
109 plugin_dispatch_values(&vl);
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);
120 if (parse_value_file(filename, &v, DS_TYPE_GAUGE) != 0) {
121 WARNING("cpufreq plugin: Reading \"%s\" failed.", filename);
125 /* convert kHz to Hz */
128 cpufreq_submit(i, "cpufreq", NULL, &v);
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);
138 cpufreq_submit(i, "counter", "transitions", &v);
142 * Determine percentage time in each state for cpu during previous interval.
144 if (report_time_in_state) {
146 cdtime_t now = cdtime();
147 char state[DATA_MAX_NAME_LEN];
149 snprintf(filename, sizeof(filename),
150 "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", i);
152 FILE *fh = fopen(filename, "r");
156 char buffer[DATA_MAX_NAME_LEN]; //128
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);
166 if (j < MAX_AVAIL_FREQS) {
168 if (value_to_rate(&g, (value_t){.counter = time}, DS_TYPE_COUNTER, now, &cpu_data[i].time_state[j]) != 0) {
172 cpufreq_submit(i, "percent", state, &(value_t){.gauge = g});
180 } /* int cpufreq_read */
182 void module_register(void) {
183 plugin_register_init("cpufreq", cpufreq_init);
184 plugin_register_read("cpufreq", cpufreq_read);