Merge branch 'collectd-5.5' into collectd-5.6
[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 MODULE_NAME "cpufreq"
29
30 static int num_cpu = 0;
31
32 static int cpufreq_init(void) {
33   int status;
34   char filename[256];
35
36   num_cpu = 0;
37
38   while (1) {
39     status = ssnprintf(filename, sizeof(filename),
40                        "/sys/devices/system/cpu/cpu%d/cpufreq/"
41                        "scaling_cur_freq",
42                        num_cpu);
43     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
44       break;
45
46     if (access(filename, R_OK))
47       break;
48
49     num_cpu++;
50   }
51
52   INFO("cpufreq plugin: Found %d CPU%s", num_cpu, (num_cpu == 1) ? "" : "s");
53
54   if (num_cpu == 0)
55     plugin_unregister_read("cpufreq");
56
57   return (0);
58 } /* int cpufreq_init */
59
60 static void cpufreq_submit(int cpu_num, double value) {
61   value_t values[1];
62   value_list_t vl = VALUE_LIST_INIT;
63
64   values[0].gauge = value;
65
66   vl.values = values;
67   vl.values_len = 1;
68   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
69   sstrncpy(vl.plugin, "cpufreq", sizeof(vl.plugin));
70   sstrncpy(vl.type, "cpufreq", sizeof(vl.type));
71   ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%i", cpu_num);
72
73   plugin_dispatch_values(&vl);
74 }
75
76 static int cpufreq_read(void) {
77   int status;
78   unsigned long long val;
79   FILE *fp;
80   char filename[256];
81   char buffer[16];
82
83   for (int i = 0; i < num_cpu; i++) {
84     status = ssnprintf(filename, sizeof(filename),
85                        "/sys/devices/system/cpu/cpu%d/cpufreq/"
86                        "scaling_cur_freq",
87                        i);
88     if ((status < 1) || ((unsigned int)status >= sizeof(filename)))
89       return (-1);
90
91     if ((fp = fopen(filename, "r")) == NULL) {
92       char errbuf[1024];
93       WARNING("cpufreq: fopen (%s): %s", filename,
94               sstrerror(errno, errbuf, sizeof(errbuf)));
95       return (-1);
96     }
97
98     if (fgets(buffer, 16, fp) == NULL) {
99       char errbuf[1024];
100       WARNING("cpufreq: fgets: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
101       fclose(fp);
102       return (-1);
103     }
104
105     if (fclose(fp)) {
106       char errbuf[1024];
107       WARNING("cpufreq: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
108     }
109
110     /* You're seeing correctly: The file is reporting kHz values.. */
111     val = atoll(buffer) * 1000;
112
113     cpufreq_submit(i, val);
114   }
115
116   return (0);
117 } /* int cpufreq_read */
118
119 void module_register(void) {
120   plugin_register_init("cpufreq", cpufreq_init);
121   plugin_register_read("cpufreq", cpufreq_read);
122 }