Merge branch 'master' into collectd-4
[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 #include "common.h"
25 #include "plugin.h"
26
27 #define MODULE_NAME "cpufreq"
28
29 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
31 #else
32 # define CPUFREQ_HAVE_READ 0
33 #endif
34
35 #if CPUFREQ_HAVE_READ
36 #ifdef KERNEL_LINUX
37 static int num_cpu = 0;
38 #endif
39
40 static int cpufreq_init (void)
41 {
42 #ifdef KERNEL_LINUX
43         int status;
44         char filename[256];
45
46         num_cpu = 0;
47
48         while (1)
49         {
50                 status = snprintf (filename, sizeof (filename),
51                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
52                                 "scaling_cur_freq", num_cpu);
53                 if (status < 1 || status >= sizeof (filename))
54                         break;
55
56                 if (access (filename, R_OK))
57                         break;
58
59                 num_cpu++;
60         }
61
62         INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
63                         (num_cpu == 1) ? "" : "s");
64
65         if (num_cpu == 0)
66                 plugin_unregister_read ("cpufreq");
67 #endif /* defined(KERNEL_LINUX) */
68
69         return (0);
70 } /* int cpufreq_init */
71
72 static void cpufreq_submit (int cpu_num, double value)
73 {
74         value_t values[1];
75         value_list_t vl = VALUE_LIST_INIT;
76
77         values[0].gauge = value;
78
79         vl.values = values;
80         vl.values_len = 1;
81         vl.time = time (NULL);
82         strcpy (vl.host, hostname_g);
83         strcpy (vl.plugin, "cpufreq");
84         snprintf (vl.type_instance, sizeof (vl.type_instance),
85                         "%i", cpu_num);
86
87         plugin_dispatch_values ("cpufreq", &vl);
88 }
89
90 static int cpufreq_read (void)
91 {
92 #ifdef KERNEL_LINUX
93         int status;
94         unsigned long long val;
95         int i = 0;
96         FILE *fp;
97         char filename[256];
98         char buffer[16];
99
100         for (i = 0; i < num_cpu; i++)
101         {
102                 status = snprintf (filename, sizeof (filename),
103                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
104                                 "scaling_cur_freq", i);
105                 if (status < 1 || status >= sizeof (filename))
106                         return (-1);
107
108                 if ((fp = fopen (filename, "r")) == NULL)
109                 {
110                         char errbuf[1024];
111                         WARNING ("cpufreq: fopen (%s): %s", filename,
112                                         sstrerror (errno, errbuf,
113                                                 sizeof (errbuf)));
114                         return (-1);
115                 }
116
117                 if (fgets (buffer, 16, fp) == NULL)
118                 {
119                         char errbuf[1024];
120                         WARNING ("cpufreq: fgets: %s",
121                                         sstrerror (errno, errbuf,
122                                                 sizeof (errbuf)));
123                         fclose (fp);
124                         return (-1);
125                 }
126
127                 if (fclose (fp))
128                 {
129                         char errbuf[1024];
130                         WARNING ("cpufreq: fclose: %s",
131                                         sstrerror (errno, errbuf,
132                                                 sizeof (errbuf)));
133                 }
134
135
136                 /* You're seeing correctly: The file is reporting kHz values.. */
137                 val = atoll (buffer) * 1000;
138
139                 cpufreq_submit (i, val);
140         }
141 #endif /* defined(KERNEL_LINUX) */
142
143         return (0);
144 } /* int cpufreq_read */
145 #endif /* CPUFREQ_HAVE_READ */
146 #undef BUFSIZE
147
148 void module_register (void)
149 {
150 #if CPUFREQ_HAVE_READ
151         plugin_register_init ("cpufreq", cpufreq_init);
152         plugin_register_read ("cpufreq", cpufreq_read);
153 #endif /* CPUFREQ_HAVE_READ */
154 }