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